Skip to content

Commit b6de888

Browse files
committed
Merge branch 'gcms-workflow' into 'master'
Update GC-MS workflow to leverage MetabRef API functionality See merge request mass-spectrometry/corems!148
2 parents 48ca1f7 + cee9c85 commit b6de888

3 files changed

Lines changed: 108 additions & 557 deletions

File tree

examples/scripts/GC-MS MetabRef.py

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,31 @@
11
import warnings
2+
23
warnings.filterwarnings("ignore")
34

45
import os
5-
from pathlib import Path
6-
from multiprocessing import Pool
76
import sys
7+
from multiprocessing import Pool
88

9-
import corems
9+
from corems.mass_spectra.calc.GC_RI_Calibration import get_rt_ri_pairs
1010
from corems.mass_spectra.input.andiNetCDF import ReadAndiNetCDF
1111
from corems.molecular_id.search.compoundSearch import LowResMassSpectralMatch
12-
from corems.mass_spectra.calc.GC_RI_Calibration import get_rt_ri_pairs
13-
14-
import metabrefapi
15-
16-
17-
def start_gcms_metabref_sql(normalize=False, url='sqlite://'):
18-
# Get MetabRef GCMS library
19-
metabref_lib = metabrefapi.get_metabref_gcms_library()
20-
21-
# Convert to CoreMS format
22-
corems_lib = metabrefapi.metabref_to_corems(metabref_lib, normalize=normalize)
23-
24-
# Convert to SQLite
25-
corems_lib = metabrefapi.corems_to_sqlite(corems_lib, url=url)
12+
from corems.molecular_id.search.database_interfaces import MetabRefGCInterface
2613

27-
return corems_lib
2814

15+
def start_gcms_metabref_sql(normalize=False, url="sqlite://"):
16+
# Initialize MetabRef interface
17+
metabref = MetabRefGCInterface()
2918

30-
def start_fames_metabref_sql(normalize=False, url='sqlite://'):
31-
# Get MetabRef GCMS library
32-
metabref_lib = metabrefapi.get_metabref_fames_library()
19+
# Pull contents into SQLite
20+
return metabref.get_library(format="sql")
3321

34-
# Convert to CoreMS format
35-
corems_lib = metabrefapi.metabref_to_corems(metabref_lib, normalize=normalize)
3622

37-
# Convert to SQLite
38-
corems_lib = metabrefapi.corems_to_sqlite(corems_lib, url=url)
23+
def start_fames_metabref_sql(normalize=False, url="sqlite://"):
24+
# Initialize MetabRef interface
25+
metabref = MetabRefGCInterface()
3926

40-
return corems_lib
27+
# Pull contents into SQLite
28+
return metabref.get_fames(format="sql")
4129

4230

4331
def get_gcms(filepath):
@@ -55,6 +43,7 @@ def get_gcms(filepath):
5543

5644
return gcms
5745

46+
5847
def stand_alone():
5948
# Determine filename
6049
filepath = get_filename()
@@ -73,15 +62,17 @@ def stand_alone():
7362

7463

7564
def get_reference_dict(calibration_filepath=False):
76-
from PySide2.QtWidgets import QFileDialog, QApplication
7765
from PySide2.QtCore import Qt
66+
from PySide2.QtWidgets import QApplication, QFileDialog
7867

7968
# Open dialog to select calibration file
8069
if not calibration_filepath:
8170
app = QApplication(sys.argv)
8271
file_dialog = QFileDialog()
8372
file_dialog.setWindowFlags(Qt.WindowStaysOnTopHint)
84-
filepath = file_dialog.getOpenFileName(None, "FAMES REF FILE", filter="*.cdf")[0]
73+
filepath = file_dialog.getOpenFileName(None, "FAMES REF FILE", filter="*.cdf")[
74+
0
75+
]
8576
file_dialog.close()
8677
app.exit()
8778

@@ -92,7 +83,7 @@ def get_reference_dict(calibration_filepath=False):
9283
# No filepath
9384
if not filepath:
9485
raise ValueError("Must supply calibration file.")
95-
86+
9687
# Parse supplied calibration data
9788
gcms_ref_obj = get_gcms(filepath)
9889

@@ -130,8 +121,12 @@ def run(args):
130121
return gcms
131122

132123

133-
def auto_calibrate_and_search(file_locations, output_filename, jobs, calibration_filepath):
134-
ref_dict, cal_filepath = get_reference_dict(calibration_filepath=calibration_filepath)
124+
def auto_calibrate_and_search(
125+
file_locations, output_filename, jobs, calibration_filepath
126+
):
127+
ref_dict, cal_filepath = get_reference_dict(
128+
calibration_filepath=calibration_filepath
129+
)
135130

136131
if ref_dict:
137132
# run in multiprocessing mode
@@ -147,35 +142,38 @@ def auto_calibrate_and_search(file_locations, output_filename, jobs, calibration
147142

148143

149144
def calibrate_and_search(filename, jobs):
150-
from PySide2.QtWidgets import QFileDialog, QApplication
151-
from PySide2.QtCore import Qt
152-
153145
import csv
154-
146+
147+
from PySide2.QtCore import Qt
148+
from PySide2.QtWidgets import QApplication, QFileDialog
149+
155150
ref_dict, cal_filepath = get_reference_dict()
156-
151+
157152
if ref_dict:
158153
file_dialog = QFileDialog()
159154
file_dialog.setWindowFlags(Qt.WindowStaysOnTopHint)
160-
155+
161156
if file_dialog:
162-
file_locations = file_dialog.getOpenFileNames(None, "Standard Compounds Files", filter="*.cdf")
157+
file_locations = file_dialog.getOpenFileNames(
158+
None, "Standard Compounds Files", filter="*.cdf"
159+
)
163160
file_dialog.close()
164-
161+
165162
# run in multiprocessing mode
166163
pool = Pool(jobs)
167-
args = [(filepath, ref_dict, cal_filepath) for filepath in file_locations[0]]
164+
args = [
165+
(filepath, ref_dict, cal_filepath) for filepath in file_locations[0]
166+
]
168167
gcmss = pool.map(run, args)
169168
pool.close()
170169
pool.join()
171170
for gcms in gcmss:
172-
173171
gcms.to_csv(filename)
174172
gcms.to_hdf()
175173

176174

177175
def worker(args):
178-
cProfile.runctx('run(args)', globals(), locals(), 'gc-ms.prof')
176+
cProfile.runctx("run(args)", globals(), locals(), "gc-ms.prof")
179177

180178

181179
def auto_process(jobs):
@@ -188,21 +186,28 @@ def auto_process(jobs):
188186
# print(output_filename)
189187

190188
file_locations = glob.glob(str((rootdir / output_filename)) + "/*.cdf")
191-
calibration_filepath = ''
189+
calibration_filepath = ""
192190
for filepath in file_locations:
193191
if "FAME" in filepath:
194192
calibration_filepath = filepath
195193
if calibration_filepath:
196-
auto_calibrate_and_search(file_locations, output_filename, jobs, calibration_filepath)
194+
auto_calibrate_and_search(
195+
file_locations, output_filename, jobs, calibration_filepath
196+
)
197197
else:
198-
print("Could not find a calibration experimental file for {}".format(output_filename))
198+
print(
199+
"Could not find a calibration experimental file for {}".format(
200+
output_filename
201+
)
202+
)
199203

200204

201-
if __name__ == '__main__':
205+
if __name__ == "__main__":
206+
# Set token
202207
TOKEN_PATH = "metabref.token"
203-
metabrefapi.set_token(TOKEN_PATH)
208+
MetabRefGCInterface().set_token(TOKEN_PATH)
204209

205210
jobs = 6
206-
filename = 'json_test'
211+
filename = "json_test"
207212

208213
calibrate_and_search(filename, jobs)

0 commit comments

Comments
 (0)