forked from mohimanilab/MS2Planner
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath_finder.py
More file actions
302 lines (262 loc) · 8.97 KB
/
Copy pathpath_finder.py
File metadata and controls
302 lines (262 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import argparse
import logging
import sys
import numpy as np
import path_apex as apex
import path_baseline as baseline
import path_curve as curve
# improves the upper bound for maximum recursion
sys.setrecursionlimit(10000)
# set up log
logger = logging.getLogger('MS2Planner')
logger.setLevel(level=logging.INFO)
# set up handler
handler = logging.FileHandler('MS2Planner.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
# add MS2Planner arg parser
parser = argparse.ArgumentParser(description="Arguments for Path Finder.")
parser.add_argument(
"mode",
type=str,
help="Mode of path finder, baseline, apex and curve (dev), default is baseline",
)
parser.add_argument(
"input_filename",
type=str,
help="Feature table generated from .mzTab, before feature filtering",
)
parser.add_argument(
"outfile_name", type=str, help="Output file storing path information, could be .txt"
)
parser.add_argument(
"intensity", type=float, help="intensity cut-off for feature filtering"
)
parser.add_argument(
"intensity_ratio", type=float, help="intensity_ratio cut-off for filtering"
)
parser.add_argument("num_path", type=int, help="number of paths required")
parser.add_argument(
"-infile_raw", type=str, help="Raw .mzTab file with only samples (curve mode only)",
)
parser.add_argument(
"-intensity_accu",
type=float,
help="minimum requirement for feature intensity accumulation in a time range (apex and curve mode only)",
)
parser.add_argument(
"-win_len", type=float, help="window length of baseline method (baseline mode only)"
)
parser.add_argument(
"-restriction",
type=float,
nargs=2,
help="restriction grid for clustering (curve mode only)",
)
parser.add_argument(
"-isolation",
type=float,
help="isolation window in mz (baseline and apex mode only)",
)
parser.add_argument(
"-delay",
type=float,
help="delay switching from feature to next feature (apex and curve mode only)",
)
parser.add_argument(
"-min_scan",
type=float,
help="minimum scan time required (apex and curve mode)",
)
parser.add_argument(
"-max_scan",
type=float,
help="maximum scan time required (apex and curve mode)",
)
parser.add_argument(
"-cluster",
type=str,
help="maximum scan time required (apex and curve mode)",
)
parser.add_argument(
"-sample",
type=str,
help="name for the sample (used for MZmine3 full feature table)",
)
parser.add_argument(
"-bg",
type=str,
help="name for the background sample (used for MZmine3 fulle feature table)",
)
parser.add_argument(
"-suffix",
type=str,
help="'Area' or 'Height' (used for MZmine3 fulle feature table)",
)
parser.add_argument(
"-max_same_RT",
type=int,
help="'# Here we limit the number of feature with the same RT to the value max_same_RT",
)
args = parser.parse_args()
try:
mode = args.mode
infile = args.input_filename
outfile = args.outfile_name
intensity = args.intensity
intensity_ratio = args.intensity_ratio
num_path = args.num_path
isolation = args.isolation # all
intensity_accu = args.intensity_accu # curve and apex mode
delay = args.delay # all
window_len = args.win_len # baseline mode
infile_raw = args.infile_raw # curve mode
restriction = args.restriction # curve mode
min_scan = args.min_scan # curve and apex mode
max_scan = args.max_scan # curve and apex mode
cluster_mode = args.cluster # curve mode
sample_name = args.sample
bg_name = args.bg
suffix = args.suffix
max_same_RT = args.max_same_RT
except:
logger.error("error in parsing args", exc_info=sys.exc_info())
sys.exit()
if mode == "apex":
try:
intensity_accu = np.exp(np.log(intensity_accu) + 2.5)
except:
logger.error("intensity_accu argument is not valid",
exc_info=sys.exc_info)
sys.exit()
if window_len is not None:
logger.warning("win_len should not be input for apex mode")
if infile_raw is not None:
logger.warning("infile_raw should not be input for apex mode")
if restriction is not None:
logger.warning("restriction should not be input for apex mode")
if cluster_mode is not None:
logger.warning("restriction should not be input for apex mode")
try:
data, rt_mz_feature = apex.ReadFile(
infile, sample_name, bg_name, suffix)
except:
logger.error("error in reading data", exc_info=sys.exc_info())
sys.exit()
logger.info("=============")
logger.info("Apex mode begin")
logger.info("=============")
logger.info("File Read")
logger.info("=============")
try:
data = apex.DataFilter(data, intensity, intensity_ratio, max_same_RT)
except:
logger.error("error in filtering data", exc_info=sys.exc_info())
sys.exit()
logger.info("Begin Finding Path")
logger.info("=============")
try:
paths_rt, paths_mz, paths_charge, edge_intensity_dic = apex.PathGen(
data, intensity_accu, num_path, delay, min_scan, max_scan
)
except:
logger.error("error in generating path", exc_info=sys.exc_info())
sys.exit()
logger.info("Paths Generated")
logger.info("=============")
try:
if sample_name is None and bg_name is None:
apex.WriteFile(outfile, paths_rt, paths_mz, paths_charge,
edge_intensity_dic, isolation, delay, min_scan, max_scan)
else:
apex.WriteFileFormatted(outfile, paths_rt, paths_mz, paths_charge,
edge_intensity_dic, isolation, delay, min_scan, max_scan, rt_mz_feature)
except:
logger.error("error in generating path", exc_info=sys.exc_info())
sys.exit()
logger.info("File Written")
logger.info("=============")
if mode == "baseline":
if intensity_accu is not None:
logger.warning("intensity_accu should not be input for baseline mode")
if infile_raw is not None:
logger.warning("infile_raw should not be input for baseline mode")
if restriction is not None:
logger.warning("restriction should not be input for baseline mode")
if min_scan is not None:
logger.warning("min_scan should not be input for baseline mode")
if max_scan is not None:
logger.warning("max_scan should not be input for baseline mode")
try:
data, rt_mz_feature = baseline.ReadFile(
infile, sample_name, bg_name, suffix)
except:
logger.error("error in reading data", exc_info=sys.exc_info())
sys.exit()
logger.info("=============")
logger.info("Baseline mode begin")
logger.info("=============")
logger.info("File Read")
logger.info("=============")
try:
data = baseline.DataFilter(data, intensity, intensity_ratio, max_same_RT)
except:
logger.error("error in filtering data", exc_info=sys.exc_info())
sys.exit()
logger.info("Begin Finding Path")
logger.info("=============")
try:
path = baseline.PathGen(data, window_len, num_path, isolation, delay)
except:
logger.error("error in generating path", exc_info=sys.exc_info())
sys.exit()
logger.info("Paths Generated")
logger.info("=============")
try:
if sample_name is None and bg_name is None:
logger.info("Running WriteFile")
#logger.info(len(path))
baseline.WriteFile(outfile, path, num_path)
else:
baseline.WriteFileFormatted(outfile, path, num_path, rt_mz_feature)
except:
logger.error("error in generating path", exc_info=sys.exc_info())
sys.exit()
logger.info("File Written")
logger.info("=============")
if mode == "curve":
try:
intensity_accu = np.exp(np.log(intensity_accu) + 2.5)
except:
logger.error("intensity_accu argument is not valid",
exc_info=sys.exc_info)
sys.exit()
if window_len is not None:
logger.warning("win_len should not be input for apex mode")
logger.info("=============")
logger.info("Curve mode begin")
logger.info("restriction: (%.4f, %.4f)", restriction[0], restriction[1])
indice_his = curve.PathGen(
infile_raw,
infile,
outfile,
intensity,
intensity_ratio,
intensity_accu,
restriction,
num_path,
delay,
min_scan,
max_scan,
cluster_mode,
sample_name,
bg_name,
suffix,
isolation,
max_same_RT
)
logger.info("File Written")
logger.info("=============")