forked from EMSL-Computing/CoreMS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGC-MS NetCDF.py
More file actions
217 lines (144 loc) · 5.99 KB
/
Copy pathGC-MS NetCDF.py
File metadata and controls
217 lines (144 loc) · 5.99 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
import warnings
warnings.filterwarnings("ignore")
import sys
sys.path.append("./")
from pathlib import Path
from multiprocessing import Pool
import cProfile
from corems.molecular_id.input.nistMSI import ReadNistMSI
from corems.mass_spectra.input.andiNetCDF import ReadAndiNetCDF
from corems.molecular_id.search.compoundSearch import LowResMassSpectralMatch
from corems.mass_spectra.calc.GC_RI_Calibration import get_rt_ri_pairs
from support_code.filefinder import get_dirname, get_filename
import glob
def start_sql_from_file():
ref_lib_path = Path.cwd() / "tests/tests_data/gcms/" / "PNNLMetV20191015.MSL"
sql_obj = ReadNistMSI(ref_lib_path).get_sqlLite_obj()
return sql_obj
def sql_database(file_location):
sqlLite_obj = ReadNistMSI(file_location).get_sqlLite_obj()
min_max_rt = (18.037, 18.037)
min_max_ri = (1637.30, 1737.30)
sqlLite_obj.query_min_max_ri((1637.30, 1638.30))
sqlLite_obj.query_min_max_rt((17.111, 18.111))
sqlLite_obj.query_min_max_ri_and_rt((1637.30, 1638.30), (17.111, 18.111))
def stand_alone():
file_path = get_filename()
reader_gcms = ReadAndiNetCDF(file_path)
reader_gcms.run()
gcms = reader_gcms.get_gcms_obj()
gcms.process_chromatogram()
def get_gcms(file_path):
reader_gcms = ReadAndiNetCDF(file_path)
reader_gcms.run()
gcms = reader_gcms.get_gcms_obj()
# gcms.process_chromatogram()
return gcms
def get_reference_dict(calibration_file_path=False):
from PySide2.QtWidgets import QFileDialog, QApplication
from PySide2.QtCore import Qt
if not calibration_file_path:
app = QApplication(sys.argv)
file_dialog = QFileDialog()
file_dialog.setWindowFlags(Qt.WindowStaysOnTopHint)
file_path = file_dialog.getOpenFileName(None, "FAMES REF FILE", filter="*.cdf")[0]
file_dialog.close()
app.exit()
else:
file_path = calibration_file_path
if not file_path:
return None
else:
gcms_ref_obj = get_gcms(file_path)
# sql_obj = start_sql_from_file()
rt_ri_pairs = get_rt_ri_pairs(gcms_ref_obj) # sql_obj=sql_obj)
# !!!!!! READ !!!!! use the previous two lines if db/pnnl_lowres_gcms_compounds.sqlite does not exist
# and comment the next line
# rt_ri_pairs = get_rt_ri_pairs(gcms_ref_obj)
return rt_ri_pairs, file_path
def run(args):
file_path, ref_dict, cal_file_path = args
gcms = get_gcms(file_path)
gcms.process_chromatogram()
gcms.calibrate_ri(ref_dict, cal_file_path)
# sql_obj = start_sql_from_file()
lowResSearch = LowResMassSpectralMatch(gcms) # sql_obj=sql_obj)
# !!!!!! READ !!!!! use the previous two lines if db/pnnl_lowres_gcms_compounds.sqlite does not exist
# and comment the next line
# lowResSearch = LowResMassSpectralMatch(gcms)
lowResSearch.run()
return gcms
def auto_calibrate_and_search(file_locations, output_file_name, jobs, calibration_file_path):
ref_dict, cal_file_path = get_reference_dict(calibration_file_path=calibration_file_path)
if ref_dict:
# run in multiprocessing mode
pool = Pool(jobs)
args = [(file_path, ref_dict, cal_file_path) for file_path in file_locations]
gcmss = pool.map(run, args)
pool.close()
pool.join()
for gcms in gcmss:
gcms.to_hdf()
gcms.to_csv(output_file_name)
# print(output_file_name)
def calibrate_and_search(out_put_file_name, jobs):
from PySide2.QtWidgets import QFileDialog
from PySide2.QtCore import Qt
ref_dict, cal_file_path = get_reference_dict()
if ref_dict:
file_dialog = QFileDialog()
file_dialog.setWindowFlags(Qt.WindowStaysOnTopHint)
if file_dialog:
file_locations = file_dialog.getOpenFileNames(None, "Standard Compounds Files", filter="*.cdf")
file_dialog.close()
# run in multiprocessing mode
pool = Pool(jobs)
args = [(file_path, ref_dict, cal_file_path) for file_path in file_locations[0]]
gcmss = pool.map(run, args)
pool.close()
pool.join()
for file_index, gcms in enumerate(gcmss):
file_path = Path(file_locations[0][file_index])
# print(out_put_file_name)
gcms.to_csv(out_put_file_name, write_metadata=True, id_label="emsl:")
# gcms.to_excel(out_put_file_name)
# gcms.to_pandas(out_put_file_name)
gcms.to_hdf()
# df = gcms.get_dataframe()
# json_data = gcms.to_json()
# print(json_data)
# gcms.plot_processed_chromatogram()
# gcms.plot_gc_peaks()
# gcms.plot_chromatogram()
# gcms.plot_smoothed_chromatogram()
# gcms.plot_baseline_subtraction()
# gcms.plot_detected_baseline()
# matplotlib.pyplot.show()
def worker(args):
cProfile.runctx('run(args)', globals(), locals(), 'gc-ms.prof')
def auto_process(jobs):
import os
rootdir = get_dirname()
out_put_file_names = list(os.walk(rootdir))[0][1]
# print(out_put_file_names[0])
for out_put_file_name in out_put_file_names:
# print(out_put_file_name)
file_locations = glob.glob(str((rootdir / out_put_file_name)) + "/*.cdf")
calibration_file_path = ''
for file_path in file_locations:
if "FAME" in file_path:
calibration_file_path = file_path
if calibration_file_path:
auto_calibrate_and_search(file_locations, out_put_file_name, jobs, calibration_file_path)
else:
print("Could not find a calibration experimental file for {}".format(out_put_file_name))
if __name__ == '__main__':
# import matplotlib
# matplotlib.use('TkAgg')
# %%
cores = 4
out_put_file_group_name = 'sql_test'
calibrate_and_search(out_put_file_group_name, cores)
# start_sql_from_file()
# auto_process(cores)
# stand_alone()