annotate server.py @ 1:7e3085fc60c1 draft default tip

master branch Updating
author lain
date Wed, 30 Aug 2023 14:21:18 +0000
parents b58b229c4cbf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1 #!/usr/bin/env python3
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
2
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
3 import atexit
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
4 import csv
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
5 import http.server
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
6 import json
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
7 import logging
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
8 import os
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
9 import re
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
10 import shutil
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
11 import socketserver
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
12 import sys
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
13 import tempfile
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
14 import yaml
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
15
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
16 TAB_LIST_PLACEHOLDER = "TAB_LIST_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
17 MS_PEAK_VALUES_PLACEHOLDER = "MS_PEAK_VALUES_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
18 COMPOUND_NAME_PLACEHOLDER = "COMPOUND_NAME_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
19 TAB_INDEX_PLACEHOLDER = "TAB_INDEX_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
20 EMBED_JS_PLACEHOLDER = "EMBED_JS"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
21 ACTIVE_TAB_PLACEHOLDER = "ACTIVE_TAB_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
22 ADD_SPECTRUM_FORM = "ADD_SPECTRUM_FORM"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
23 PRODUCE_JSON_PLACEHOLDER = "PRODUCE_JSON_PLACEHOLDER"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
24
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
25 COMPOUND_REF = "compound-ref"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
26 COMPOUND_MIX = "compound-mix"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
27
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
28 END_MS_PEAK_VALUES_PLACEHOLDER = " ]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
29 MS_DATA_COLUMN_NUMBER = 9
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
30 DEFAULT_MS_PEAK_VALUES = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
31 "[\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
32 + (" [" + ','.join([' ""'] * MS_DATA_COLUMN_NUMBER) + "],\n") * 17
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
33 + END_MS_PEAK_VALUES_PLACEHOLDER
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
34 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
35
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
36 FRAGNOT_HEADER = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
37 "m/z": "fragment_mz",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
38 "absolute_intensity": "abs_intensity",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
39 "relative_intensity": "rel_intensity",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
40 "theo_mass": "",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
41 "delta_ppm": "ppm",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
42 "rdbequiv": "",
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
43 "composition": "fragment",
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
44 "attribution": "composition",
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
45 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
46
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
47 MS_2_SNOOP_HEADER = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
48 "name": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
49 "inchikey": str,
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
50 # "composition": str,
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
51 "composition": lambda *args:"",
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
52 "fragment": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
53 "fragment_mz": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
54 "ppm": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
55 "fileid": str,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
56 "correlation": str,
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
57 "abs_intensity": lambda x:float(x), # * 100,
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
58 "rel_intensity": lambda x:float(x) * 100 if x != "" else "",
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
59 "valid_corelation": str
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
60 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
61
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
62
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
63 class ConfigException(ValueError):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
64 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
65 An exception raised when something went wrong in the config and we
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
66 cannot continue - i.e: when there's no token for peakforest
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
67 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
68
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
69 class YAMLConfig(dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
70
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
71 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
72 Dictionary that handles key with dot in them:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
73 test["truc.chose"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
74 is equivalant to
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
75 test["truc"]["chose"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
76 Assignation works too.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
77 Add the possibility to use placeholders:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
78 --- yaml
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
79 test: {{ truc.chose }}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
80 truc:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
81 chose: bidule
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
82 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
83 here, test's value is "bidule"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
84 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
85
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
86 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
87 meta_conf = kwargs.pop("__meta_config__", {})
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
88 self._debug = meta_conf.get("__debug__", False)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
89 self._stream_name = meta_conf.get("__debug_stream__", "stdout")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
90 self._debug_stream = getattr(sys, self._stream_name)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
91 self._only_root_debug = meta_conf.get("__only_root_debug__", False)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
92 if "__root__" in kwargs:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
93 if self._only_root_debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
94 self._debug = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
95 self._name = kwargs.pop("__name__")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
96 self._debugger("Is not root config.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
97 self._root = kwargs.pop("__root__")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
98 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
99 self._name = "root"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
100 self._debugger("Is root config.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
101 self._root = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
102 super().__init__(*args, **kwargs)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
103 for key, value in self.copy().items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
104 if isinstance(value, dict) and not isinstance(value, YAMLConfig):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
105 self._debugger(f"Parsing sub-config for {key}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
106 self[key] = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
107 self._replace_placeholders(self)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
108 self._extract_defaults()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
109
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
110 def _propagate(self, sub_dict, name):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
111 if isinstance(sub_dict, dict) and not isinstance(sub_dict, self.__class__):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
112 return YAMLConfig(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
113 **sub_dict,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
114 __name__=name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
115 __root__=self._root,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
116 __meta_config__={
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
117 "__debug__": self._debug,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
118 "__debug_stream__": self._stream_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
119 "__only_root_debug__": self._only_root_debug,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
120 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
121 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
122 return sub_dict
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
123
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
124 def _debugger(self, message):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
125 if self._debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
126 self._debug_stream.write(f"[{self._name}]: {message}\n")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
127 self._debug_stream.flush()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
128
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
129 def __getattr__(self, attr):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
130 if attr in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
131 return self[attr]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
132 if '.' in attr:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
133 attr, sub = attr.split('.', 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
134 return getattr(getattr(self, attr), sub)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
135 return super().__getattribute__(attr)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
136
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
137 def _replace_placeholders(self, subpart):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
138 self._debugger("Replacing placeholders...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
139 for sub_key, sub_item in subpart.copy().items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
140 if isinstance(sub_item, str):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
141 for placeholder in re.findall("{{ (?P<placeholder>.*?) }}", sub_item):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
142 if placeholder not in self._root:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
143 self._debugger(f"Could not fine replacement for {placeholder}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
144 continue
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
145 replacement = self._root[placeholder]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
146 if isinstance(replacement, str):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
147 self._debugger(f"Found placeholder: {placeholder} -> {replacement}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
148 sub_item = sub_item.replace(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
149 "{{ " + placeholder + " }}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
150 replacement
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
151 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
152 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
153 self._debugger(f"Found placeholder: {placeholder} -> {replacement.__class__.__name__}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
154 sub_item = self._propagate(replacement, placeholder)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
155 dict.__setitem__(subpart, sub_key, sub_item)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
156 elif isinstance(sub_item, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
157 super().__setitem__(sub_key, self._propagate(sub_item, sub_key))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
158
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
159 def _extract_defaults(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
160 if self._root is not self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
161 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
162 if "defaults" not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
163 self._debugger("No defaults here.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
164 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
165 if "arguments" not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
166 self._debugger("Arguments creation...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
167 self["arguments"] = self._propagate({}, "arguments")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
168 self._debugger("Populating arguments with defaults values")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
169 for key, value in self.defaults.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
170 if key not in self:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
171 if isinstance(value, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
172 value = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
173 self.arguments[key] = value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
174 self._debugger(f"Default {key} = {value}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
175
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
176 def __setitem__(self, key, value):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
177 if isinstance(value, dict):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
178 value = self._propagate(value, key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
179 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
180 return super().__setitem__(key, value)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
181 curent = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
182 key, subkey = key.rsplit(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
183 self[key][subkey] = value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
184
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
185 def __getitem__(self, key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
186 if super().__contains__(key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
187 return super().__getitem__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
188 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
189 return super().__getitem__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
190 curent = self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
191 while "." in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
192 key, subkey = key.split(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
193 curent = curent[key]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
194 key = subkey
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
195 if subkey not in curent:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
196 curent[subkey] = self._propagate({}, subkey)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
197 result = curent[subkey]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
198 return result
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
199
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
200 def __contains__(self, key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
201 if "." not in key:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
202 return super().__contains__(key)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
203 key, subkey = key.split(".", 1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
204 if not super().__contains__(key):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
205 return False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
206 return subkey in self[key]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
207
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
208 def copy(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
209 return {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
210 key: (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
211 value if not isinstance(value, dict)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
212 else value.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
213 ) for key, value in self.items()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
214 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
215
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
216 class YAMLParameters(YAMLConfig):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
217
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
218 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
219 Parses parameters from the command line and put them
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
220 in the config.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
221 Uses the config to know which parameter is recognized, or not,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
222 to know the metadata (author, version),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
223 which command is a flag, is optional, the help strings, etc...
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
224 Assigns default small parameter if not defined in the "shortcut"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
225 section of the config file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
226 CLI config must be in the root section "parameters":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
227 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
228 parameters:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
229 mandatory:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
230 input: input file path
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
231 flags:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
232 help: Show this help
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
233 optional:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
234 method: "default is {{ defaults.method }}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
235 meta:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
236 author: Lain Pavot
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
237 version: 1.2.0
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
238 shortcuts:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
239 help: h
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
240 ## will autogenerate -i for input and -m for method
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
241 ---
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
242 default parameters are searched in the "default" root section.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
243 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
244
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
245 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
246 super().__init__(*args, **kwargs)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
247 self._errors = list()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
248 if not self.parameters.shortcuts:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
249 self.parameters["shortcuts"] = YAMLConfig()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
250 self._mandatory = self.parameters.mandatory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
251 self._optional = self.parameters.optional
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
252 self._flags = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
253 flag: False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
254 for flag in self.parameters.flags
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
255 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
256 self._all_params = self._optional.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
257 self._all_params.update(self._mandatory)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
258 self._all_params.update(self._flags)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
259 self._small_params = dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
260 self._determine_small_params()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
261
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
262 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
263 def in_error(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
264 return bool(self._errors)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
265
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
266 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
267 def sorted_keys(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
268 return sorted(self._all_params.keys())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
269
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
270 @property
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
271 def sorted_items(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
272 return sorted(self._all_params.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
273
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
274 def _determine_small_params(self, verbose=False):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
275 self._small_params = (self.parameters.shortcuts or {}).copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
276 chars = list(map(chr, range(97, 123))) + list(map(chr, range(65, 91)))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
277 all_params = self._all_params.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
278 for long, short in self._small_params.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
279 chars.remove(short)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
280 del all_params[long]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
281 for param in all_params.copy().keys():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
282 for operation in (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
283 lambda x:x[0], ## select first char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
284 lambda x:x.split('-', 1)[-1][0], ## first char after -
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
285 lambda x:x.split('_', 1)[-1][0], ## first char after _
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
286 lambda x:x.split('.', 1)[-1][0], ## first char after .
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
287 lambda x:x[0].upper(), ## select first char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
288 lambda x:x.split('-', 1)[-1][0].upper(), ## first char after -
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
289 lambda x:x.split('_', 1)[-1][0].upper(), ## first char after _
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
290 lambda x:x.split('.', 1)[-1][0].upper(), ## first char after .
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
291 lambda x: chars[0], ## first letter in the alphabet
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
292 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
293 char = operation(param)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
294 if char not in self._small_params.values():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
295 self._small_params[param] = char
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
296 chars.remove(char)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
297 del all_params[param]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
298 break
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
299
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
300 def _get_parameter_index(self, parameter, original):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
301 if f"--{parameter}" in sys.argv:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
302 return sys.argv.index(f"--{parameter}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
303 parameter = self._small_params[original]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
304 if f"-{parameter}" in sys.argv:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
305 return sys.argv.index(f"-{parameter}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
306 return None
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
307
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
308 def as_parameter(self, string):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
309 return (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
310 string
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
311 .replace('.', '-')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
312 .replace('_', '-')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
313 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
314
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
315 def show_version(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
316 print(self.parameters.meta.version)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
317
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
318 def show_help(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
319 parameters = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
320 f"-{self._small_params[arg]}|--{self.as_parameter(arg)} {arg}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
321 for arg in self._mandatory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
322 ] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
323 f"[-{self._small_params[arg]}|--{self.as_parameter(arg)} {arg}]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
324 for arg in self._optional
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
325 ] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
326 f"[-{self._small_params[arg]}|--{self.as_parameter(arg)}]"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
327 for arg in self._flags
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
328 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
329 print(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
330 f"Usage: {__file__} " + ' '.join(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
331 + "\n\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
332 + '\n'.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
333 f" -{self._small_params[args]}|--{self.as_parameter(args)}: {help_str}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
334 for args, help_str in self.sorted_items
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
335 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
336 + "\n\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
337 + '\n'.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
338 f"{key}: {value}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
339 for key, value in self.parameters.meta.items()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
340 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
341 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
342 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
343
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
344 def parse_args(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
345 errors = list()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
346 for kind in ("mandatory", "optional", "flags"):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
347 keys = list(sorted(getattr(self, f"_{kind}").keys()))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
348 for original_param, actual_param in zip(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
349 keys,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
350 map(self.as_parameter, keys),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
351 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
352 if original_param in self.defaults:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
353 self.arguments[original_param] = self.defaults[original_param]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
354 elif kind == "flags":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
355 self.arguments[original_param] = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
356 parser = getattr(self, f"parse_{kind}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
357 if (error := parser(original_param, actual_param)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
358 errors.append(error)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
359 self._errors = errors
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
360 return self
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
361
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
362 def parse_mandatory(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
363 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
364 return f"The parameter --{actual} is mandatory."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
365 if index == len(sys.argv) - 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
366 return f"The parameter --{actual} needs a value."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
367 self.arguments[original] = sys.argv[index + 1]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
368
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
369 def parse_optional(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
370 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
371 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
372 if index == len(sys.argv) - 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
373 return f"The parameter --{actual} needs a value."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
374 self.arguments[original] = sys.argv[index + 1]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
375
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
376 def parse_flags(self, original, actual):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
377 if (index := self._get_parameter_index(actual, original)) is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
378 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
379 self.arguments[original] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
380
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
381 def parse_config(**kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
382 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
383 opens the config file, extract it using pyyaml's safe loader
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
384 and tries to extract and apply a maximum of informations/directives
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
385 from the config:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
386 - token retrieval
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
387 - workdir management
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
388 - tempfile management
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
389 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
390 root_dir = os.path.dirname(os.path.abspath(__file__))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
391 with open(os.path.join(root_dir, "config.yml")) as config_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
392 config = YAMLConfig(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
393 **yaml.load(config_file.read(), Loader=yaml.SafeLoader),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
394 **kwargs
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
395 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
396
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
397 if not config.token.value:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
398 if config.token.use_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
399 if (not os.path.exists(path := config.token.file_path)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
400 raise ConfigException("Missing token value or token file.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
401 with open(path) as token_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
402 config.token["value"] = token_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
403 elif config.defaults.peakforest.token:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
404 config.token["value"] = config.defaults.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
405
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
406 if config.workdir.create_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
407 tmp_dir = tempfile.mkdtemp()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
408 atexit.register(lambda:shutil.rmtree(tmp_dir))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
409 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
410 tmp_dir = tempfile.gettempdir()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
411 config.workdir["tmp_dir"] = tmp_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
412
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
413 config["root_dir"] = root_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
414 config["tab_list"] = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
415 config["form_template"] = os.path.join(root_dir, config.templates.form)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
416 config["meta_template"] = os.path.join(root_dir, config.templates.main)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
417 config["js_template"] = os.path.join(root_dir, config.templates.js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
418 config["tab_list_template"] = os.path.join(root_dir, config.templates.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
419 config["placeholders"] = dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
420 config.placeholders[MS_PEAK_VALUES_PLACEHOLDER] = DEFAULT_MS_PEAK_VALUES
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
421 config.placeholders[TAB_INDEX_PLACEHOLDER] = "1"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
422 config.placeholders[ACTIVE_TAB_PLACEHOLDER] = "active"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
423 config.placeholders[ADD_SPECTRUM_FORM] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
424 config.placeholders[EMBED_JS_PLACEHOLDER] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
425 config.placeholders[TAB_LIST_PLACEHOLDER] = ""
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
426 # config.placeholders["DEFAULT_MIN_MZ"] = "50"
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
427 # config.placeholders["DEFAULT_MAX_MZ"] = "500"
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
428 config.placeholders["DEFAULT_RESOLUTION_LOW"] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
429 config.placeholders["DEFAULT_RESOLUTION_HIGH"] = "selected=\"selected\""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
430 config.placeholders["DEFAULT_RESOLUTION_UNSET"] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
431 return config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
432
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
433 def parse_parameters(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
434 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
435 parses command line and checks provided values are acceptable/usable.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
436 Raises some error if not.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
437 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
438 parameters = YAMLParameters(**config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
439 parameters.parse_args()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
440
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
441 parameters["json_result"] = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
442
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
443 get_logger(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
444
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
445 arguments = parameters.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
446 if arguments.help:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
447 parameters.show_help()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
448 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
449
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
450 if arguments.version:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
451 parameters.show_version()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
452 sys.exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
453
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
454 if parameters.in_error:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
455 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
456 "Some errors occured during parameters extraction: \n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
457 + '\n'.join(parameters.errors)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
458 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
459
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
460 parameters.placeholders["DEFAULT_MIN_RT"] = str(arguments.rt_min)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
461 parameters.placeholders["DEFAULT_MAX_RT"] = str(arguments.rt_max)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
462
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
463 if arguments.sample_type == COMPOUND_MIX:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
464 parameters["form_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
465 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
466 parameters.templates.form_mix
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
467 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
468 parameters["meta_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
469 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
470 parameters.templates.main_mix
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
471 )
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
472 else: # elif arguments.sample_type == COMPOUND_REF:
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
473 parameters["form_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
474 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
475 parameters.templates.form_ref
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
476 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
477 parameters["meta_template"] = os.path.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
478 parameters["root_dir"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
479 parameters.templates.main_ref
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
480 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
481
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
482 arguments["produce_json"] = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
483 "output_json" in arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
484 and arguments["output_json"] != ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
485 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
486 if arguments.produce_json:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
487 parameters.placeholders[PRODUCE_JSON_PLACEHOLDER] = "true"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
488 parameters.json_result = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
489 arguments["output_json"] = os.path.abspath(arguments["output_json"])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
490 atexit.register(save_json, parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
491 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
492 parameters.placeholders[PRODUCE_JSON_PLACEHOLDER] = "false"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
493
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
494 if arguments.run_dry_html:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
495 arguments["do_run_dry"] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
496 parameters.generated["html"] = os.path.abspath(arguments.run_dry_html)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
497
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
498 if arguments.run_dry_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
499 arguments["do_run_dry"] = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
500 parameters.generated["js"] = os.path.abspath(arguments.run_dry_js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
501
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
502 if arguments.do_run_dry:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
503 parameters.logger.info("Dry run. Server will ne be run.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
504 if arguments.run_dry_html:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
505 parameters.logger.info(f"HTML file will be put in {arguments.run_dry_html}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
506 if arguments.run_dry_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
507 parameters.logger.info(f"JS file will be put in {arguments.run_dry_js}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
508
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
509 if arguments.peakforest.token:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
510 config.token["value"] = arguments.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
511 if not config.token.value:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
512 raise ConfigException(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
513 "No token provided. We will not be able to connect to peakforest."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
514 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
515
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
516 if os.path.exists(arguments.input):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
517 single_file = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
518 file_paths = [arguments.input]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
519 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
520 path_list = arguments.input.split(',')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
521 if all(map(os.path.exists, path_list)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
522 single_file = False
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
523 file_paths = path_list
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
524 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
525 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
526 f"Some files cannot be found: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
527 + ', '.join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
528 path for path in path_list
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
529 if not os.path.exists(path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
530 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
531 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
532 arguments["input"] = list(map(os.path.abspath, file_paths))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
533
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
534 if single_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
535 arguments["name"] = [arguments.name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
536 arguments["raw_metadata"] = [arguments.raw_metadata]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
537 parameters.logger.info(f"Single file processing: {arguments.input}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
538 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
539 parameters.logger.info(f"Multiple file processing:")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
540 arguments["raw_metadata"] = arguments.raw_metadata.split(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
541 arguments.raw_metadata_sep
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
542 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
543 if not arguments.name:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
544 arguments["name"] = arguments["raw_metadata"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
545 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
546 arguments["name"] = arguments.name.split(',')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
547 for i in range(len(arguments.name)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
548 parameters.logger.info(f" - file: {arguments.input[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
549 parameters.logger.info(f" - name: {arguments.name[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
550 parameters.logger.info(f" - metadata: {arguments.raw_metadata[i]}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
551 parameters.logger.info(f" ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
552 if (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
553 len(arguments.name) != len(arguments.raw_metadata)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
554 or len(arguments.name) != len(arguments.input)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
555 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
556 raise ValueError(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
557 "name, raw_metadata and input parameters have different lengths: \n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
558 f"input is {len(arguments.input)} elements long, "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
559 f"raw_metadata is {len(arguments.raw_metadata)} elements long "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
560 f"and name is {len(arguments.name)} elements long."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
561 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
562 if arguments.spectrum_type == "LC_MS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
563 arguments["scan_type"] = "ms"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
564 elif arguments.spectrum_type == "LC_MSMS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
565 arguments["scan_type"] = "ms2"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
566 if arguments.method == "test":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
567 if arguments.spectrum_type == "LC_MS":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
568 arguments["method"] = "cf_pfem_urine_qtof"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
569 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
570 arguments["method"] = "cf_pfem_urine_method1_qtof-msms"
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
571 arguments["method"] = "toulouse-metatoul-agromix__ft-esi__msms"
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
572 if arguments["sample_type"] == COMPOUND_MIX:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
573 check_mix_compound_files(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
574 more_info_in_logs(parameters)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
575 return parameters
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
576
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
577 def check_mix_compound_files(parameters):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
578 arguments = parameters.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
579 try:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
580 numbarz = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
581 list(map(int, os.path.basename(metadata).split("_", 1)[0].split("-")))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
582 for metadata in arguments.raw_metadata
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
583 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
584 except ValueError:
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
585 ## file does not start with `[0-9]+-[0-9]+_.*`: probably
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
586 ## a ms2snoop file.
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
587 return
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
588 # parameters.logger.error(
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
589 # "Metadata/file names does not start with `[0-9]+-[0-9]+_.*` . "
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
590 # "This is necessary in the case of compounds mix."
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
591 # )
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
592 # sys.exit(-1)
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
593 runs, samples = zip(*numbarz)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
594 if not all(runs[0] == i for i in runs[1:]):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
595 parameters.logger.error(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
596 "Run numbers in metadata/file names are not identical. "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
597 "You mixed some files."
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
598 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
599 sys.exit(-1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
600 length = len(samples)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
601 if list(sorted(samples)) != list(range(1, length+1)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
602 if not all(samples.count(i) == 1 for i in samples):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
603 parameters.logger.error("Some samples are duplicated. ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
604 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
605 parameters.logger.error("Some samples files are missing. ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
606 sys.exit(-1)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
607
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
608 def more_info_in_logs(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
609 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
610 if arguments.embed_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
611 config.logger.info(f"JS will be embed in HTML page to form a HTML bundle.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
612 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
613 config.logger.info(f"JS are separated files, needed to be served.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
614 config.logger.info(f"Choosen parameters:")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
615 config.logger.info(f" - method: {arguments.method}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
616 config.logger.info(f" - peakforest instance: {arguments.peakforest.url}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
617 config.logger.info(f" - polarity instance: {arguments.polarity}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
618 config.logger.info(f" - spectrum type: {arguments.spectrum_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
619 config.logger.info(f" - scan type: {arguments.scan_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
620 config.logger.info(f" - produce JSON: {arguments.produce_json}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
621 config.logger.info(f" - sample type: {arguments.sample_type}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
622
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
623 def process_all_files(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
624 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
625 for each file and its metadata, read and process them,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
626 then fills the meta html template file with the whole result.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
627 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
628 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
629 extra_defaults = [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
630 process_fragnot_metadata(metadata, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
631 for metadata in arguments.raw_metadata
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
632 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
633 for i, name in enumerate(arguments.name):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
634 extra_defaults[i]["name"] = name
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
635
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
636 if not extra_defaults:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
637 extra_defaults = [{}] * len(arguments.input)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
638
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
639 index = 0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
640 for input_path, extra_default in zip(arguments.input, extra_defaults):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
641 config.logger.info(f"Processing file at {input_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
642 curent_defaults = arguments.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
643 curent_defaults.update(extra_default)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
644 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
645 config.logger.info(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
646 "[VERBOSE] Defaults for curent file: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
647 + ';'.join(f"{key}={value}" for key, value in curent_defaults.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
648 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
649 tsv_content, tsv_data_extractor = read_input(input_path, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
650 index = process_tsv(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
651 tsv_content,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
652 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
653 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
654 defaults_data = curent_defaults,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
655 index = index+1,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
656 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
657 if arguments.embed_js:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
658 config.logger.info(f"Embeding JS in HTML file... ")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
659 for index in range(len(config.tab_list)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
660 config.placeholders[EMBED_JS_PLACEHOLDER] += "<script type='text/javascript'>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
661 with open(f"add-one-spectrum-{index+1}.js") as js_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
662 config.placeholders[EMBED_JS_PLACEHOLDER] += js_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
663 config.placeholders[EMBED_JS_PLACEHOLDER] += "</script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
664 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
665 config.logger.info(f" - add-one-spectrum-{index+1}.js embed.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
666 config.placeholders[TAB_LIST_PLACEHOLDER] = "\n".join(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
667 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
668 config.placeholders[EMBED_JS_PLACEHOLDER] += "<script type='text/javascript'>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
669 config.placeholders[EMBED_JS_PLACEHOLDER] += "</script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
670 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n".join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
671 [""] + [
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
672 " "*12 + f"<script src=\"./add-one-spectrum-{index+1}.js\"></script>"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
673 for index in range(len(config.tab_list))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
674 ]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
675 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
676 config.placeholders[EMBED_JS_PLACEHOLDER] += "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
677 config.placeholders[TAB_LIST_PLACEHOLDER] = "\n".join(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
678
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
679 fill_template("meta_template", "pf_path", config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
680
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
681 def fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
682 template_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
683 output_name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
684 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
685 additional_placeholders=dict()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
686 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
687 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
688 Fills a template, replaces the placeholders.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
689 Either outputs the result in a given file, or returns it if path is none.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
690 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
691 template_path = config[template_name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
692 config.logger.debug(f"Filling template {template_name} at {template_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
693 with open(template_path) as template_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
694 template_content = template_file.read()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
695 placeholders = config.placeholders.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
696 placeholders.update(additional_placeholders)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
697 for placeholder, replacement in placeholders.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
698 if not placeholder.startswith(config.templates.placeholders.start):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
699 placeholder = placeholder.join((
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
700 config.templates.placeholders.start,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
701 config.templates.placeholders.stop
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
702 ))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
703 template_content = template_content.replace(placeholder, replacement)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
704 if output_name is None:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
705 config.logger.debug(f"Returning template content")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
706 return template_content
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
707 output_path = config[output_name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
708 if "{{ index }}" in output_path:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
709 index_value = additional_placeholders["{{ index }}"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
710 config.logger.debug(f"Changing index value for {index_value}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
711 output_path = output_path.replace("{{ index }}", index_value)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
712 config.logger.debug(f"Full output path {output_path}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
713 with open(output_path, "w") as output_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
714 output_file.write(template_content)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
715
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
716 def read_input(input_path, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
717 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
718 reads a tsv file and determin its processor, based on its header.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
719 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
720 with open(input_path) as input_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
721 config.logger.info(f"Reading {input_path}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
722 tsv_file = csv.reader(input_file, delimiter='\t')
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
723 header = next(tsv_file)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
724 tsv_file = list(tsv_file)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
725 config.logger.info(f"Header is: {', '.join(header)}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
726 if header == list(FRAGNOT_HEADER):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
727 config.logger.info(f"Fragnot recognized.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
728 processor = fragnot_extractor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
729 return uniformize_fragnot(tsv_file, header), processor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
730 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
731 config.logger.info(f"MS2Snoop recognized.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
732 processor = ms2snoop_extractor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
733 return uniformize_ms2snoop(tsv_file, header), processor
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
734
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
735 def uniformize_fragnot(content, header):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
736 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
737 sorts fragnot data so they appear always in the same order
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
738 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
739 return sorted(content, key=lambda x:(float(x[0]), float(x[4])))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
740
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
741 def uniformize_ms2snoop(content, header):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
742 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
743 sorts ms2snoop data so they appear always in the same order
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
744 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
745 return sorted(content, key=lambda x:(x[0], float(x[4])))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
746
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
747 def process_fragnot_metadata(raw_metadata, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
748 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
749 Tries to extract informations from the metadata provided by fragnot
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
750 files names.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
751 Heavily based on regex defined in conf file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
752 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
753 regex = config.regex.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
754 del regex["values"]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
755 result = {}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
756 config.logger.info(f"Extracting info from {raw_metadata}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
757 count = 0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
758 for name, expression in regex.items():
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
759 if (match := re.search(expression, raw_metadata)):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
760 result[name] = match[name]
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
761 count += 1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
762 did = "+ did"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
763 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
764 did = "- did not"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
765 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
766 config.logger.info(f" {did} match {expression}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
767 config.logger.info(f"{count} useful informations extracted.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
768 return result
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
769
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
770 def process_tsv(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
771 tsv_content,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
772 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
773 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
774 defaults_data={},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
775 index=1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
776 ):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
777 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
778 processes one tsv file, containing one or multiple compounds.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
779 Creation of the peak table for each compound
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
780 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
781 tsv_content = list(tsv_content)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
782 curent_name, ms_data = get_ms_data(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
783 tsv_content[0],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
784 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
785 defaults_data,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
786 config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
787 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
788 _, second_ms_data = get_ms_data(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
789 tsv_content[1],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
790 tsv_data_extractor,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
791 defaults_data,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
792 config
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
793 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
794 ms_peak_table = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
795 config.logger.info(f"Processing compound {curent_name}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
796
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
797 for line in tsv_content:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
798 name, new_ms_data = get_ms_data(line, tsv_data_extractor, defaults_data, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
799 if name != curent_name:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
800 new_compound(curent_name, index, ms_data, config, ms_peak_table)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
801 curent_name = name
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
802 index += 1
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
803 config.logger.info(f"Processing compound {curent_name}...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
804 ms_peak_table = []
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
805 ms_data = new_ms_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
806 ms_peak_table.append(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
807 ", ".join(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
808 f'"{value}"' if value not in ("na", "NA")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
809 else '""'
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
810 for value in (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
811 ms_data["fragment_mz"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
812 ms_data["abs_intensity"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
813 ms_data["rel_intensity"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
814 ms_data["ppm"],
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
815 ms_data["fragment"],
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
816 ms_data["composition"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
817 str(ms_data["valid_corelation"] == "TRUE").lower(),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
818 "true" if ms_data.get("correlation") == "1" else "false"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
819 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
820 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
821 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
822 new_compound(curent_name, index, ms_data, config, ms_peak_table)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
823 return index
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
824
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
825 def get_ms_data(line, extractor, defaults, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
826 ms_data = defaults.copy()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
827 ms_data.update(extractor(config, *line))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
828 return ms_data["name"], ms_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
829
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
830 def new_compound(name, index, ms_data, config, ms_peak_table):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
831 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
832 aggregates informations to form the peak table,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
833 adds the compound to the tab list,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
834 creates the js file for this tab
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
835 """
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
836 ignore_multiple_parent_ion(ms_peak_table)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
837 determine_min_max_mz(ms_peak_table)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
838 guess_relative_intensities(ms_peak_table)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
839 accept_all_fragments_if_all_false(ms_peak_table)
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
840 config.placeholders[MS_PEAK_VALUES_PLACEHOLDER] = f"""[
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
841 {','.join('['+line+']' for line in ms_peak_table)}
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
842 ]"""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
843 tab_list = fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
844 "tab_list_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
845 None,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
846 config, {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
847 COMPOUND_NAME_PLACEHOLDER: name,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
848 TAB_INDEX_PLACEHOLDER: str(index),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
849 })
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
850 config.tab_list.append(tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
851 create_js_file(index, ms_data, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
852 config.placeholders[ADD_SPECTRUM_FORM] += fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
853 "form_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
854 None,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
855 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
856 {TAB_INDEX_PLACEHOLDER: str(index)},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
857 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
858 if index == 1:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
859 config.placeholders[ACTIVE_TAB_PLACEHOLDER] = ""
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
860
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
861 def determine_min_max_mz(ms_peak_table):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
862 mz_list = [
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
863 float(ms_peak_table[i].split(", ")[0].strip("\""))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
864 for i in range(len(ms_peak_table))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
865 ]
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
866 config.placeholders["DEFAULT_MIN_MZ"] = str(min(mz_list))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
867 config.placeholders["DEFAULT_MAX_MZ"] = str(max(mz_list))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
868
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
869 def guess_relative_intensities(ms_peak_table):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
870 if all(
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
871 ms_peak_table[i].split(", ")[2].strip("\"") == ""
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
872 for i in range(len(ms_peak_table))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
873 ):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
874 absolute_intensities = [
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
875 float(ms_peak_table[i].split(", ")[1].strip("\""))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
876 for i in range(len(ms_peak_table))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
877 ]
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
878 greatest = float(max(absolute_intensities))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
879 relative_intensities = [
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
880 intensity / greatest * 100
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
881 for intensity in absolute_intensities
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
882 ]
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
883 replace_ms_table_value(ms_peak_table, 2, relative_intensities)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
884
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
885 def ignore_multiple_parent_ion(ms_peak_table):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
886 if len([
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
887 None for x in ms_peak_table
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
888 if x.split(", ")[7].strip("\"") == "true"
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
889 ]) > 1:
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
890 ## if more than one is the precursor, then none is the precursor
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
891 replace_ms_table_value(ms_peak_table, 7, "\"false\"")
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
892
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
893 def accept_all_fragments_if_all_false(ms_peak_table):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
894 if all(
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
895 ms_peak_table[i].split(", ")[6].strip("\"") == "false"
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
896 for i in range(len(ms_peak_table))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
897 ):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
898 replace_ms_table_value(ms_peak_table, 6, "true")
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
899
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
900 def replace_ms_table_value(ms_peak_table, index, value, sep=", "):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
901 length = len(ms_peak_table)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
902 if not isinstance(value, list):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
903 if isinstance(value, str):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
904 value = [value.join('""')] * length
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
905 else:
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
906 value = [str(value)] * length
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
907 if not isinstance(value[0], str):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
908 value = [str(x) for x in value]
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
909 count = ms_peak_table[0].count(sep)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
910 endindex = count - index
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
911 neg_endindex = -endindex
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
912 for i in range(length):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
913 ms_peak_table[i] = sep.join((
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
914 *ms_peak_table[i].split(sep, index)[:index],
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
915 value[i],
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
916 *ms_peak_table[i].rsplit(sep, endindex)[neg_endindex:]
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
917 ))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
918
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
919 def fragnot_extractor(config, *line):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
920 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
921 Fragnot processor - extracts one fragnot line of content and
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
922 produces a uniformised output.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
923 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
924 fragnot_data = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
925 FRAGNOT_HEADER[header]: line[i].strip()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
926 for i, header in enumerate(FRAGNOT_HEADER)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
927 }
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
928 # fragnot_data["composition"] = "unknown"
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
929 fragnot_data["valid_corelation"] = config.arguments.validation
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
930 return fragnot_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
931
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
932 def ms2snoop_extractor(config, *line):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
933 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
934 Fragnot processor - extracts one ms2snoop line of content and
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
935 produces a uniformised output.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
936 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
937 ms2snoop_data = {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
938 header: MS_2_SNOOP_HEADER[header](line[i])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
939 for i, header in enumerate(MS_2_SNOOP_HEADER)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
940 }
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
941 return ms2snoop_data
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
942
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
943 def create_js_file(index, ms_data, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
944 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
945 fills the js template file for one tab (compound)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
946 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
947 if (method := ms_data["method"]):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
948 method = f'"{method}"'
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
949 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
950 method = "null"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
951 if config.arguments.verbose:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
952 config.logger.info(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
953 "[VERBOSE] "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
954 + ';'.join(f"{key}={value}" for key, value in ms_data.items())
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
955 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
956 fill_template(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
957 "js_template",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
958 "js_file",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
959 config,
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
960 {
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
961 TAB_INDEX_PLACEHOLDER: str(index),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
962 "INCHIKEY_PLACEHOLDER": ms_data["inchikey"],
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
963 "DEFAULT_DATA": f"""{{
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
964 name: "{ms_data["name"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
965 inchikey: "{ms_data["inchikey"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
966 method: {method},
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
967 spectrum_type: "{ms_data["spectrum_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
968 scan_type: "{ms_data["scan_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
969 polarity: "{ms_data["polarity"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
970 resolution: "{ms_data["resolution"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
971 sample_type: "{ms_data["sample_type"]}",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
972 }}""",
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
973 "{{ index }}": str(index)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
974 },
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
975 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
976
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
977 def prepare_workplace(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
978 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
979 prepares the directory we will work in.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
980 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
981 if config.workdir.work_in_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
982 os.chdir(config.workdir.tmp_dir)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
983 config.logger.info(f"Moving to {os.getcwd()}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
984 if config.workdir.generate_in_tmp:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
985 gen_dir = config.workdir.tmp_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
986 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
987 gen_dir = tempfile.gettempdir()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
988 config.workdir.tmp_dir = gen_dir
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
989 shutil.copy(os.path.join(config["root_dir"], "src", "common.js"), gen_dir)
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
990 config.logger.info(f"Outputs will be generated in {config.workdir.tmp_dir}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
991 return gen_dir
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
992
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
993 def get_hander_for(directory, config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
994 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
995 generates the handler class for the directory we provide.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
996 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
997 config["json_result"] = [{}] * len(config.tab_list)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
998
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
999 class HTTPHandler(http.server.SimpleHTTPRequestHandler):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1000
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1001 def __init__(self, *args, **kwargs):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1002 super().__init__(*args, **kwargs, directory=directory)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1003
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1004 def do_POST(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1005 content_length = int(self.headers.get("Content-Length"))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1006 json_bytes = self.rfile.read(content_length).decode("utf-8")
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1007 # json_list = json.loads(json_bytes)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1008 # for i, obj in enumerate(json_list):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1009 # print(obj)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1010 # if obj:
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1011 # config["json_result"][i] = obj
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1012 json_obj = json.loads(json_bytes)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1013 config["json_result"][json_obj["index"]] = json_obj["object"]
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1014 save_json(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1015 self.send_head()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1016 self.wfile.write(json_bytes.encode("utf-8"))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1017 return
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1018
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1019 def do_GET(self):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1020 if self.path == "/quit":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1021 self.path = "/"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1022 super().do_GET()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1023 exit(0)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1024 self.path = os.path.join(directory, self.path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1025 if self.path == "/":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1026 self.path = config.generated.html
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1027 return super().do_GET()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1028
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1029 return HTTPHandler
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1030
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1031
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1032 def save_json(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1033 json_string = json.dumps(config["json_result"])
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1034 print(json_string)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1035 with open(config.arguments.output_json, "w") as json_file:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1036 json_file.write(json_string)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1037
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1038 def run_server(config):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1039 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1040 prepare and runs the server, with the handler for the given directory
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1041 """
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1042 ip, port = config.arguments.ip, int(config.arguments.port)
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1043 config.logger.debug(f"IP and port: {ip}:{port}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1044 socketserver.TCPServer.allow_reuse_address = True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1045 config.logger.debug(f"Allow reuse adress.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1046 handler = get_hander_for(config.workdir.tmp_dir, config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1047 config.logger.debug(f"Created server handler for {config.workdir.tmp_dir}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1048 config.logger.debug(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1049 f"Content of directory {config.workdir.tmp_dir}: "
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1050 + "\n"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1051 + '\n'.join(sorted(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1052 f" - {path}"for path in os.listdir(config.workdir.tmp_dir)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1053 ))
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1054 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1055 config.logger.debug(f"Creating TCP server...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1056 server = socketserver.TCPServer((ip, port), handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1057 if ip == "0.0.0.0":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1058 displayed_ip = "localhost"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1059 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1060 displayed_ip = ip
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1061 config.logger.debug(f"Serving...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1062 print()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1063 print(f"http://{displayed_ip}:{port}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1064 server.serve_forever()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1065
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1066 def get_logger(config, dummy=False):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1067 dummy_log = lambda msg:dummy and config.logger.info(msg)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1068 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1069 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1070 logger = logging.getLogger(__file__)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1071 if arguments.debug:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1072 dummy_log(f"Output debug info.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1073 level = logging.DEBUG
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1074 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1075 level = logging.INFO
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1076 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1077 logger.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1078 formatter = logging.Formatter(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1079 "%(asctime)s - %(levelname)s - %(message)s"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1080 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1081 if arguments.logging.std == "err":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1082 dummy_log(f"Handler added to output logs in stderr.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1083 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1084 handler = logging.StreamHandler(sys.stderr)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1085 handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1086 handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1087 logger.addHandler(handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1088 elif arguments.logging.std == "out":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1089 dummy_log(f"Handler added to output logs in stdout.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1090 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1091 handler = logging.StreamHandler(sys.stdout)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1092 handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1093 handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1094 logger.addHandler(handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1095 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1096 dummy_log(f"Logs will not be output in stderr not stdout.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1097 if (path := arguments.logging.file.path):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1098 dummy_log(f"Add log file: {arguments.logging.file.path}.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1099 if not arguments.logging.file.append:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1100 dummy_log(f"Log file content cleaned.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1101 with open(path, "w"):pass
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1102 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1103 dummy_log(f"Logs appended to log file.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1104 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1105 file_handler = logging.FileHandler(filename=path)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1106 file_handler.setLevel(level)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1107 file_handler.setFormatter(formatter)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1108 logger.addHandler(file_handler)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1109 if not dummy:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1110 config["logger"] = logger
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1111 starting_sequence(logger)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1112 get_logger(config, dummy=True)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1113 return logger
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1114
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1115 def starting_sequence(logger):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1116 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1117 logger.info("starting...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1118 logger.info("program...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1119 logger.info("MS2PF is running...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1120 logger.info("*bip* *bop* am a robot")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1121 atexit.register(stoping_sequence, logger)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1122
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1123 def stoping_sequence(logger):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1124 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1125 logger.info("ending...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1126 logger.info("program...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1127 logger.info("MS2PF is shuting down...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1128 logger.info("...robot")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1129 logger.info("*bip* *bop*")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1130 logger.info("shutdown")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1131 logger.info("...")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1132
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1133 if __name__ == "__main__":
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1134 print(os.listdir("test-data"))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1135
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1136 if not os.path.exists("config.yml"):
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1137 shutil.copy("config.default.yml", "config.yml")
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1138
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1139 base_config = parse_config()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1140 config = parse_parameters(base_config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1141
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1142 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1143 The config contains result of the parsed config file.
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1144 """
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1145 arguments = config.arguments
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1146
1
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1147 if arguments.pid:
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1148 print(arguments.pid)
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1149 with open(arguments.pid, "w") as pid_file:
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1150 pid_file.write(str(os.getpid()))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1151 atexit.register(lambda:os.unlink(arguments.pid))
7e3085fc60c1 master branch Updating
lain
parents: 0
diff changeset
1152
0
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1153 config.logger.info(f"Starting MS2PF from {os.getcwd()}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1154
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1155 gen_dir = prepare_workplace(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1156
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1157 config["pf_path"] = os.path.join(gen_dir, config.generated.html)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1158 config.logger.info(f"HTML output file will be {config.pf_path}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1159 config["js_file"] = os.path.join(gen_dir, config.generated.js)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1160 config.logger.info(f"JS output files will like {config.js_file}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1161 config.placeholders["PF_URL_PLACEHOLDER"] = arguments.peakforest.url
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1162 config.placeholders["PF_TOKEN_PLACEHOLDER"] = (
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1163 arguments.peakforest.token
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1164 or config.token.value
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1165 )
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1166 if (token := config.placeholders.PF_TOKEN_PLACEHOLDER):
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1167 config.logger.info(f"Using a token for authentification - length: {len(token)}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1168 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1169 config.logger.info(f"No token provided for peakforest authentification.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1170
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1171 process_all_files(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1172
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1173 if not arguments.do_run_dry:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1174 config.logger.debug(f"Running the server.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1175 if arguments.firefox or arguments.chromium:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1176 config.logger.debug(f"Running the server.")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1177 import threading
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1178 import time
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1179 if arguments.firefox:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1180 browser = "firefox"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1181 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1182 browser = "chromium"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1183 if (ip := config.network.ip) == "0.0.0.0":
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1184 ip = "localhost"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1185 adress = f"http://{ip}:{config.network.port}"
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1186 threading.Thread(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1187 target=lambda:(
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1188 time.sleep(1),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1189 os.system(f"{browser} {adress}")
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1190 ),
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1191 daemon=True
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1192 ).start()
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1193 run_server(config)
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1194 else:
b58b229c4cbf planemo upload commit 523a9c8df173302ad38e9f15e7d82eab01736551-dirty
lain
parents:
diff changeset
1195 config.logger.debug(f"Server not run.")