-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmsql_engine.py
More file actions
746 lines (564 loc) · 28.3 KB
/
Copy pathmsql_engine.py
File metadata and controls
746 lines (564 loc) · 28.3 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
import msql_parser
import os
import pandas as pd
import numpy as np
import copy
import logging
from tqdm import tqdm
import ray
from py_expression_eval import Parser
import msql_fileloading
math_parser = Parser()
console = logging.StreamHandler()
console.setLevel(logging.INFO)
def DEBUG_MSG(msg):
import sys
print(msg, file=sys.stderr, flush=True)
def init_ray():
if not ray.is_initialized():
ray.init(ignore_reinit_error=True, object_store_memory=8000000000)
def _load_data(input_filename, cache=False):
"""
Loading data generically
Args:
input_filename ([type]): [description]
cache (bool, optional): [description]. Defaults to False.
Returns:
[type]: [description]
"""
if cache:
ms1_filename = input_filename + "_ms1.msql.feather"
ms2_filename = input_filename + "_ms2.msql.feather"
if os.path.exists(ms1_filename):
ms1_df = pd.read_feather(ms1_filename)
ms2_df = pd.read_feather(ms2_filename)
return ms1_df, ms2_df
# Actually loading
if input_filename[-5:] == ".mzML":
ms1_df, ms2_df = msql_fileloading._load_data_mzML(input_filename)
if input_filename[-6:] == ".mzXML":
ms1_df, ms2_df = msql_fileloading._load_data_mzXML(input_filename)
if input_filename[-5:] == ".json":
ms1_df, ms2_df = msql_fileloading._load_data_gnps_json(input_filename)
if input_filename[-4:] == ".mgf":
ms1_df, ms2_df = msql_fileloading._load_data_mgf(input_filename)
# Saving Cache
if cache:
ms1_filename = input_filename + "_ms1.msql.feather"
ms2_filename = input_filename + "_ms2.msql.feather"
if not os.path.exists(ms1_filename):
ms1_df.to_feather(ms1_filename)
ms2_df.to_feather(ms2_filename)
return ms1_df, ms2_df
def _get_ppm_tolerance(qualifiers):
if qualifiers is None:
return None
if "qualifierppmtolerance" in qualifiers:
ppm = qualifiers["qualifierppmtolerance"]["value"]
return ppm
return None
def _get_da_tolerance(qualifiers):
if qualifiers is None:
return None
if "qualifiermztolerance" in qualifiers:
return qualifiers["qualifiermztolerance"]["value"]
return None
def _get_mz_tolerance(qualifiers, mz):
if qualifiers is None:
return 0.1
if "qualifierppmtolerance" in qualifiers:
ppm = qualifiers["qualifierppmtolerance"]["value"]
mz_tol = abs(ppm * mz / 1000000)
return mz_tol
if "qualifiermztolerance" in qualifiers:
return qualifiers["qualifiermztolerance"]["value"]
return 0.1
def _get_minintensity(qualifier):
"""
Returns absolute min and relative min
Args:
qualifier ([type]): [description]
Returns:
[type]: [description]
"""
if qualifier is None:
return 0, 0
if "qualifierintensityvalue" in qualifier:
return qualifier["qualifierintensityvalue"]["value"], 0
if "qualifierintensitypercent" in qualifier:
return 0, qualifier["qualifierintensitypercent"]["value"] / 100
return 0, 0
def _get_intensitymatch_range(qualifiers, match_intensity):
min_intensity = 0
max_intensity = 0
if "qualifierintensitytolpercent" in qualifiers:
tolerance_percent = qualifiers["qualifierintensitytolpercent"]["value"]
tolerance_value = float(tolerance_percent) / 100 * match_intensity
min_intensity = match_intensity - tolerance_value
max_intensity = match_intensity + tolerance_value
return min_intensity, max_intensity
def _filter_intensitymatch(ms_filtered_df, register_dict, condition):
if "qualifiers" in condition:
if "qualifierintensitymatch" in condition["qualifiers"] and \
"qualifierintensitytolpercent" in condition["qualifiers"]:
qualifier_expression = condition["qualifiers"]["qualifierintensitymatch"]["value"]
qualifier_variable = qualifier_expression[0] #TODO: This assumes the variable is the first character in the expression, likely a bad assumption
grouped_df = ms_filtered_df.groupby("scan").sum().reset_index()
filtered_grouped_scans = []
for grouped_scan in grouped_df.to_dict(orient="records"):
# Reading from the register
key = "scan:{}:variable:{}".format(grouped_scan["scan"], qualifier_variable)
if key in register_dict:
register_value = register_dict[key]
evaluated_new_expression = math_parser.parse(qualifier_expression).evaluate({
qualifier_variable : register_value
})
min_match_intensity, max_match_intensity = _get_intensitymatch_range(condition["qualifiers"], evaluated_new_expression)
scan_intensity = grouped_scan["i"]
#print(key, scan_intensity, qualifier_expression, min_match_intensity, max_match_intensity, grouped_scan)
if scan_intensity > min_match_intensity and \
scan_intensity < max_match_intensity:
filtered_grouped_scans.append(grouped_scan)
else:
# Its not in the register, which means we don't find it
continue
return pd.DataFrame(filtered_grouped_scans)
return ms_filtered_df
def _set_intensity_register(ms_filtered_df, register_dict, condition):
if "qualifiers" in condition:
if "qualifierintensityreference" in condition["qualifiers"]:
qualifier_variable = condition["qualifiers"]["qualifierintensitymatch"]["value"]
grouped_df = ms_filtered_df.groupby("scan").sum().reset_index()
for grouped_scan in grouped_df.to_dict(orient="records"):
# Saving into the register
key = "scan:{}:variable:{}".format(grouped_scan["scan"], qualifier_variable)
register_dict[key] = grouped_scan["i"]
return
def process_query(input_query, input_filename, path_to_grammar="msql.ebnf", cache=True, parallel=True):
parsed_dict = msql_parser.parse_msql(input_query, path_to_grammar=path_to_grammar)
return _evalute_variable_query(parsed_dict, input_filename, cache=cache, parallel=parallel)
def _determine_mz_max(mz, ppm_tol, da_tol):
da_tol = da_tol if da_tol < 10000 else 0
ppm_tol = ppm_tol if ppm_tol < 10000 else 0
# We are going to make the bins half of the actual tolerance
half_delta = max(mz * ppm_tol / 1000000, da_tol) / 2
half_delta = half_delta if half_delta > 0 else 0.05
return mz + half_delta
def _evalute_variable_query(parsed_dict, input_filename, cache=True, parallel=True):
# Lets check if there is a variable in here, the only one allowed is X
for condition in parsed_dict["conditions"]:
try:
if "querytype" in condition["value"][0]:
subquery_val_df = _evalute_variable_query(
condition["value"][0], input_filename, cache=cache
)
condition["value"] = list(
subquery_val_df["precmz"]
) # Flattening results
except:
pass
# Here we will check if there is a variable in the expression
variable_properties = {}
variable_properties["has_variable"] = False
variable_properties["ppm_tolerance"] = 100000
variable_properties["da_tolerance"] = 100000
variable_properties["query_ms1"] = False
variable_properties["query_ms2"] = False
for condition in parsed_dict["conditions"]:
for value in condition["value"]:
try:
# Checking if X is in any string
if "X" in value[0]:
if value == "X":
# This is the main varaible, not expression containing it
if condition["type"] == "ms1mzcondition":
variable_properties["query_ms1"] = True
if condition["type"] == "ms2productcondition":
variable_properties["query_ms2"] = True
if condition["type"] == "ms2neutrallosscondition":
variable_properties["query_ms2"] = True
variable_properties["has_variable"] = True
#mz_tolerance = _get_mz_tolerance(condition.get("qualifiers", None), 1000000)
ppm_tolerance = _get_ppm_tolerance(condition.get("qualifiers", None))
da_tolerance = _get_da_tolerance(condition.get("qualifiers", None))
if da_tolerance is not None:
variable_properties["da_tolerance"] = min(variable_properties["da_tolerance"], da_tolerance)
if ppm_tolerance is not None:
variable_properties["ppm_tolerance"] = min(variable_properties["ppm_tolerance"], ppm_tolerance)
continue
except TypeError:
# This is when the target is actually a float
pass
ms1_df, ms2_df = _load_data(input_filename, cache=cache)
# Here we are going to translate the variable query into a concrete query based upon the data
all_concrete_queries = []
if variable_properties["has_variable"]:
# Here we could do a pre-query without any of the other conditions
presearch_parse = copy.deepcopy(parsed_dict)
non_variable_conditions = []
for condition in presearch_parse["conditions"]:
for value in condition["value"]:
try:
# Checking if X is in any string
if "X" in value[0]:
continue
except TypeError:
# This is when the target is actually a float
pass
non_variable_conditions.append(condition)
presearch_parse["conditions"] = non_variable_conditions
ms1_df, ms2_df = _executeconditions_query(presearch_parse, input_filename, cache=cache)
variable_x_ms1_df = ms1_df
# TODO: Checking if we can prefilter the X variable, if there are conditions
for condition in parsed_dict["conditions"]:
if not condition["conditiontype"] == "where":
continue
if not "X" in condition["value"]:
continue
# Filtering MS1 peaks only to consider contention for X
if condition["type"] == "ms1mzcondition":
min_int, min_intpercent = _get_minintensity(condition.get("qualifiers", None))
variable_x_ms1_df = ms1_df[
(ms1_df["i"] > min_int) &
(ms1_df["i_norm"] > min_intpercent)]
# Here we will start with the smallest mass and then go up
masses_considered_df = pd.DataFrame()
if variable_properties["query_ms1"]:
masses_considered_df["mz"] = pd.concat([variable_x_ms1_df["mz"]])
if variable_properties["query_ms2"]:
masses_considered_df["mz"] = pd.concat([ms2_df["mz"]])
masses_considered_df["mz_max"] = masses_considered_df["mz"].apply(lambda x: _determine_mz_max(x, variable_properties["ppm_tolerance"], variable_properties["da_tolerance"]))
masses_considered_df = masses_considered_df.sort_values("mz")
masses_list = masses_considered_df.to_dict(orient="records")
running_max_mz = 0
for masses_obj in tqdm(masses_list):
if running_max_mz > masses_obj["mz"]:
continue
# Writing new query
substituted_parse = copy.deepcopy(parsed_dict)
mz_val = masses_obj["mz"]
for condition in substituted_parse["conditions"]:
for i, value in enumerate(condition["value"]):
try:
if "X" in value:
new_value = math_parser.parse(value).evaluate({
"X" : mz_val
})
condition["value"][i] = new_value
except TypeError:
# This is when the target is actually a float
pass
# DEBUG
# if mz_val < 614.75 or mz_val > 614.8:
# continue
substituted_parse["comment"] = str(mz_val)
all_concrete_queries.append(substituted_parse)
# Let's consider this mz
running_max_mz = masses_obj["mz_max"]
# DELTA_VAL = 0.1
# # Lets iterate through all values of the variable
# #MAX_MZ = 10
# #MAX_MZ = 200
# MAX_MZ = 1000
# for i in tqdm(range(int(MAX_MZ / DELTA_VAL))):
# x_val = i * DELTA_VAL + 150
# # Writing new query
# substituted_parse = copy.deepcopy(parsed_dict)
# for condition in substituted_parse["conditions"]:
# for i, value in enumerate(condition["value"]):
# try:
# if "X" in value:
# if "+" in value:
# new_value = x_val + float(value.split("+")[-1])
# else:
# new_value = x_val
# # print("SUBSTITUTE", condition, value, i, new_value)
# condition["value"][i] = new_value
# except TypeError:
# # This is when the target is actually a float
# pass
# #print(substituted_parse)
# all_concrete_queries.append(substituted_parse)
else:
all_concrete_queries.append(parsed_dict)
print("TOTAL QUERIES", len(all_concrete_queries))
# Perfoming the filtering of conditions
results_ms1_list = []
results_ms2_list = []
# Ray Parallel Version
if ray.is_initialized() and parallel:
futures = [_executeconditions_query_ray.remote(concrete_query, input_filename, ms1_input_df=ms1_df, ms2_input_df=ms2_df, cache=cache) for concrete_query in all_concrete_queries]
all_ray_results = ray.get(futures)
results_ms1_list, results_ms2_list = zip(*all_ray_results)
else:
# Serial Version
for concrete_query in tqdm(all_concrete_queries):
results_ms1_df, results_ms2_df = _executeconditions_query(concrete_query, input_filename, ms1_input_df=ms1_df, ms2_input_df=ms2_df, cache=cache)
results_ms1_list.append(results_ms1_df)
results_ms2_list.append(results_ms2_df)
aggregated_ms1_df = pd.concat(results_ms1_list)
aggregated_ms2_df = pd.concat(results_ms2_list)
# reduce redundancy
aggregated_ms1_df = aggregated_ms1_df.drop_duplicates()
aggregated_ms2_df = aggregated_ms2_df.drop_duplicates()
# Collating all results
return _executecollate_query(parsed_dict, aggregated_ms1_df, aggregated_ms2_df)
@ray.remote
def _executeconditions_query_ray(parsed_dict, input_filename, ms1_input_df=None, ms2_input_df=None, cache=True):
return _executeconditions_query(parsed_dict, input_filename, ms1_input_df=ms1_input_df, ms2_input_df=ms2_input_df, cache=cache)
def _executeconditions_query(parsed_dict, input_filename, ms1_input_df=None, ms2_input_df=None, cache=True):
# This function attempts to find the data that the query specifies in the conditions
#import json
#print("parsed_dict", json.dumps(parsed_dict, indent=4))
# Let's apply this to real data
if ms1_input_df is None and ms2_input_df is None:
ms1_df, ms2_df = _load_data(input_filename, cache=cache)
else:
ms1_df = ms1_input_df
ms2_df = ms2_input_df
# In order to handle intensities, we will make sure to sort all conditions with
# with the conditions that are the reference intensity first, then subsequent conditions
# that have an intensity match will reference the saved reference intensities
reference_conditions_register = {} # This will hold all the reference intensity values
# This helps sort the qualifiers
reference_conditions = []
nonreference_conditions = []
for condition in parsed_dict["conditions"]:
if "qualifiers" in condition:
if "qualifierintensityreference" in condition["qualifiers"]:
reference_conditions.append(condition)
continue
nonreference_conditions.append(condition)
all_conditions = reference_conditions + nonreference_conditions
# These are for the WHERE clause, first lets filter by RT
for condition in all_conditions:
if not condition["conditiontype"] == "where":
continue
#logging.error("WHERE CONDITION", condition)
# RT Filters
if condition["type"] == "rtmincondition":
rt = condition["value"][0]
ms2_df = ms2_df[ms2_df["rt"] > rt]
ms1_df = ms1_df[ms1_df["rt"] > rt]
continue
if condition["type"] == "rtmaxcondition":
rt = condition["value"][0]
ms2_df = ms2_df[ms2_df["rt"] < rt]
ms1_df = ms1_df[ms1_df["rt"] < rt]
continue
# These are for the WHERE clause
for condition in all_conditions:
if not condition["conditiontype"] == "where":
continue
#logging.error("WHERE CONDITION", condition)
# Filtering MS2 Product Ions
if condition["type"] == "ms2productcondition":
filtered_scans = set()
for mz in condition["value"]:
mz_tol = _get_mz_tolerance(condition.get("qualifiers", None), mz)
mz_min = mz - mz_tol
mz_max = mz + mz_tol
min_int, min_intpercent = _get_minintensity(condition.get("qualifiers", None))
ms2_filtered_df = ms2_df[
(ms2_df["mz"] > mz_min) &
(ms2_df["mz"] < mz_max) &
(ms2_df["i"] > min_int) &
(ms2_df["i_norm"] > min_intpercent)
]
# Setting the intensity match register
_set_intensity_register(ms2_filtered_df, reference_conditions_register, condition)
# Applying the intensity match
ms2_filtered_df = _filter_intensitymatch(ms2_filtered_df, reference_conditions_register, condition)
if len(ms2_filtered_df) > 0:
# Getting union of all scans
filtered_scans = filtered_scans.union(set(ms2_filtered_df["scan"]))
# Filtering the actual data structures
ms2_df = ms2_df[ms2_df["scan"].isin(filtered_scans)]
# Filtering the MS1 data now
ms1_scans = set(ms2_df["ms1scan"])
ms1_df = ms1_df[ms1_df["scan"].isin(ms1_scans)]
continue
# Filtering MS2 Precursor m/z
if condition["type"] == "ms2precursorcondition":
mz = condition["value"][0]
mz_tol = _get_mz_tolerance(condition.get("qualifiers", None), mz)
mz_min = mz - mz_tol
mz_max = mz + mz_tol
ms2_df = ms2_df[(
ms2_df["precmz"] > mz_min) &
(ms2_df["precmz"] < mz_max)
]
# Filtering the MS1 data now
ms1_scans = set(ms2_df["ms1scan"])
ms1_df = ms1_df[ms1_df["scan"].isin(ms1_scans)]
continue
# Filtering MS2 Neutral Loss
if condition["type"] == "ms2neutrallosscondition":
filtered_scans = set()
for mz in condition["value"]:
mz_tol = _get_mz_tolerance(condition.get("qualifiers", None), mz)
nl_min = mz - mz_tol
nl_max = mz + mz_tol
min_int, min_intpercent = _get_minintensity(condition.get("qualifiers", None))
ms2_filtered_df = ms2_df[
((ms2_df["precmz"] - ms2_df["mz"]) > nl_min) &
((ms2_df["precmz"] - ms2_df["mz"]) < nl_max) &
(ms2_df["i"] > min_int) &
(ms2_df["i_norm"] > min_intpercent)
]
# Setting the intensity match register
_set_intensity_register(ms2_filtered_df, reference_conditions_register, condition)
# Applying the intensity match
ms2_filtered_df = _filter_intensitymatch(ms2_filtered_df, reference_conditions_register, condition)
if len(ms2_filtered_df) > 0:
# Getting union of all scans
filtered_scans = filtered_scans.union(set(ms2_filtered_df["scan"]))
# Filtering the actual data structures
ms2_df = ms2_df[ms2_df["scan"].isin(filtered_scans)]
# Filtering the MS1 data now
ms1_scans = set(ms2_df["ms1scan"])
ms1_df = ms1_df[ms1_df["scan"].isin(ms1_scans)]
continue
# finding MS1 peaks
if condition["type"] == "ms1mzcondition":
filtered_scans = set()
for mz in condition["value"]:
mz_tol = _get_mz_tolerance(condition.get("qualifiers", None), mz)
mz_min = mz - mz_tol
mz_max = mz + mz_tol
min_int, min_intpercent = _get_minintensity(condition.get("qualifiers", None))
ms1_filtered_df = ms1_df[
(ms1_df["mz"] > mz_min) &
(ms1_df["mz"] < mz_max) &
(ms1_df["i"] > min_int) &
(ms1_df["i_norm"] > min_intpercent)]
#print("YYY", mz_min, mz_max, min_int, min_intpercent, len(ms1_filtered_df))
# Setting the intensity match register
_set_intensity_register(ms1_filtered_df, reference_conditions_register, condition)
# Applying the intensity match
ms1_filtered_df = _filter_intensitymatch(ms1_filtered_df, reference_conditions_register, condition)
if len(ms1_filtered_df) > 0:
# Getting union of all scans
filtered_scans = filtered_scans.union(set(ms1_filtered_df["scan"]))
if filtered_scans == 0:
return pd.DataFrame(), pd.DataFrame()
# Filtering the actual data structures
ms1_df = ms1_df[ms1_df["scan"].isin(filtered_scans)]
ms2_df = ms2_df[ms2_df["ms1scan"].isin(filtered_scans)]
continue
if condition["type"] == "rtmincondition":
continue
if condition["type"] == "rtmaxcondition":
continue
raise Exception("CONDITION NOT HANDLED")
# These are for the FILTER clause
for condition in all_conditions:
if not condition["conditiontype"] == "filter":
continue
#logging.error("FILTER CONDITION", condition)
# filtering MS1 peaks
if condition["type"] == "ms1mzcondition":
mz = condition["value"][0]
mz_tol = 0.1
mz_min = mz - mz_tol
mz_max = mz + mz_tol
ms1_df = ms1_df[(ms1_df["mz"] > mz_min) & (ms1_df["mz"] < mz_max)]
if condition["type"] == "ms2productcondition":
mz = condition["value"][0]
mz_tol = _get_mz_tolerance(condition.get("qualifiers", None), mz)
mz_min = mz - mz_tol
mz_max = mz + mz_tol
min_int, min_intpercent = _get_minintensity(condition.get("qualifiers", None))
ms2_df = ms2_df[(ms2_df["mz"] > mz_min) & (ms2_df["mz"] < mz_max) & (ms2_df["i"] > min_int) & (ms2_df["i_norm"] > min_intpercent)]
if "comment" in parsed_dict:
ms1_df["comment"] = parsed_dict["comment"]
ms2_df["comment"] = parsed_dict["comment"]
return ms1_df, ms2_df
def _executecollate_query(parsed_dict, ms1_df, ms2_df):
# This function takes the dataframes from executing the conditions and returns the proper formatted version
# collating the results
if parsed_dict["querytype"]["function"] is None:
if parsed_dict["querytype"]["datatype"] == "datams1data":
return ms1_df
if parsed_dict["querytype"]["datatype"] == "datams2data":
return ms2_df
else:
# Applying function
if parsed_dict["querytype"]["function"] == "functionscansum":
# TODO: Fix how this scan is done so the result values for most things actually make sense
if parsed_dict["querytype"]["datatype"] == "datams1data":
if len(ms1_df) == 0:
return ms1_df
ms1sum_df = ms1_df.groupby("scan").sum().reset_index()
ms1_df = ms1_df.groupby("scan").first().reset_index()
ms1_df["i"] = ms1sum_df["i"]
return ms1_df
if parsed_dict["querytype"]["datatype"] == "datams2data":
if len(ms2_df) == 0:
return ms2_df
ms2sum_df = ms2_df.groupby("scan").sum().reset_index()
ms2_df = ms2_df.groupby("scan").first().reset_index()
ms2_df["i"] = ms2sum_df["i"]
return ms2_df
if parsed_dict["querytype"]["function"] == "functionscanmz":
if len(ms2_df) == 0:
return ms2_df
result_df = pd.DataFrame()
result_df["precmz"] = list(set(ms2_df["precmz"]))
return result_df
if parsed_dict["querytype"]["function"] == "functionscannum":
result_df = pd.DataFrame()
if parsed_dict["querytype"]["datatype"] == "datams1data":
result_df["scan"] = list(set(ms1_df["scan"]))
if parsed_dict["querytype"]["datatype"] == "datams2data":
result_df["scan"] = list(set(ms2_df["scan"]))
return result_df
if parsed_dict["querytype"]["function"] == "functionscaninfo":
result_df = pd.DataFrame()
if parsed_dict["querytype"]["datatype"] == "datams1data":
groupby_columns = ["scan"]
kept_columns = ["scan", "rt"]
if "comment" in ms1_df:
groupby_columns.append("comment")
kept_columns.append("comment")
result_df = ms1_df.groupby(groupby_columns).first().reset_index()
result_df = result_df[kept_columns]
ms1sum_df = ms1_df.groupby(groupby_columns).sum().reset_index()
result_df["i"] = ms1sum_df["i"]
if parsed_dict["querytype"]["datatype"] == "datams2data":
kept_columns = ["scan", "precmz", "ms1scan", "rt"]
groupby_columns = ["scan"]
if "comment" in ms2_df:
groupby_columns.append("comment")
kept_columns.append("comment")
result_df = ms2_df.groupby(groupby_columns).first().reset_index()
result_df = result_df[kept_columns]
ms2sum_df = ms2_df.groupby(groupby_columns).sum().reset_index()
result_df["i"] = ms2sum_df["i"]
# Lets try to remove duplicates
if "comment" in result_df:
result_df["truncated_comment"] = result_df["comment"].astype(float).astype(int)
result_df = result_df.drop_duplicates(subset=["scan", "truncated_comment"])
result_df = result_df.drop("truncated_comment", axis=1)
return result_df
if parsed_dict["querytype"]["function"] == "functionscanrangesum":
result_list = []
if parsed_dict["querytype"]["datatype"] == "datams1data":
ms1_df["bin"] = ms1_df["mz"].apply(lambda x: int(x / 0.1))
all_bins = set(ms1_df["bin"])
for bin in all_bins:
ms1_filtered_df = ms1_df[ms1_df["bin"] == bin]
ms1sum_df = ms1_filtered_df.groupby("scan").sum().reset_index()
ms1_filtered_df = (
ms1_filtered_df.groupby("scan").first().reset_index()
)
ms1_filtered_df["i"] = ms1sum_df["i"]
result_list.append(ms1_filtered_df)
return pd.concat(result_list)
if parsed_dict["querytype"]["datatype"] == "datams2data":
ms2_df = ms2_df.groupby("scan").sum()
ms2sum_df = ms2_df.groupby("scan").sum()
ms2_df = ms2_df.groupby("scan").first().reset_index()
ms2_df["i"] = ms2sum_df["i"]
return ms2_df
print("APPLYING FUNCTION")