Skip to content

Commit 00f61f4

Browse files
committed
Rework how we get tic and ms levels via ImportMassSpectraThermoMSFileReader
Fixes #212
1 parent 9da2168 commit 00f61f4

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

corems/mass_spectra/input/rawFileReader.py

+45-26
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,40 @@ def get_filter_for_scan_num(self, scan_number: int) -> List[str]:
307307

308308
return str(scan_label).split()
309309

310+
def get_ms_level_for_scan_num(self, scan_number: int) -> str:
311+
"""
312+
Get the MS order for the given scan number.
313+
314+
Parameters:
315+
-----------
316+
scan_number : int
317+
The scan number
318+
319+
Returns:
320+
--------
321+
int
322+
The MS order type (1 for MS, 2 for MS2, etc.)
323+
"""
324+
scan_filter = self.iRawDataPlus.GetFilterForScanNumber(scan_number)
325+
326+
msordertype = {
327+
MSOrderType.Ms: 1,
328+
MSOrderType.Ms2: 2,
329+
MSOrderType.Ms3: 3,
330+
MSOrderType.Ms4: 4,
331+
MSOrderType.Ms5: 5,
332+
MSOrderType.Ms6: 6,
333+
MSOrderType.Ms7: 7,
334+
MSOrderType.Ms8: 8,
335+
MSOrderType.Ms9: 9,
336+
MSOrderType.Ms10: 10,
337+
}
338+
339+
if scan_filter.MSOrder in msordertype:
340+
return msordertype[scan_filter.MSOrder]
341+
else:
342+
raise Exception("MS Order Type not found")
343+
310344
def check_full_scan(self, scan_number: int) -> bool:
311345
# scan_filter.ScanMode 0 = FULL
312346
scan_filter = self.iRawDataPlus.GetFilterForScanNumber(scan_number)
@@ -1286,31 +1320,16 @@ def get_scan_df(self):
12861320
# This automatically brings in all the data
12871321
self.chromatogram_settings.scans = (-1, -1)
12881322

1289-
# Get scan df info; starting with bulk ms1 and ms2 scans
1290-
ms1_tic_data, _ = self.get_tic(ms_type="MS", peak_detection=False, smooth=False)
1291-
ms1_scan_dict = {
1292-
"scan": ms1_tic_data.scans,
1293-
"scan_time": ms1_tic_data.time,
1294-
"tic": ms1_tic_data.tic,
1295-
}
1296-
ms1_tic_df = pd.DataFrame.from_dict(ms1_scan_dict)
1297-
ms1_tic_df["ms_level"] = "ms1"
1298-
1299-
ms2_tic_data, _ = self.get_tic(
1300-
ms_type="MS2", peak_detection=False, smooth=False
1301-
)
1302-
ms2_scan_dict = {
1303-
"scan": ms2_tic_data.scans,
1304-
"scan_time": ms2_tic_data.time,
1305-
"tic": ms2_tic_data.tic,
1323+
# Get scan df info; starting with TIC data
1324+
tic_data, _ = self.get_tic(ms_type="all", peak_detection=False, smooth=False)
1325+
tic_data = {
1326+
"scan": tic_data.scans,
1327+
"scan_time": tic_data.time,
1328+
"tic": tic_data.tic,
13061329
}
1307-
ms2_tic_df = pd.DataFrame.from_dict(ms2_scan_dict)
1308-
ms2_tic_df["ms_level"] = "ms2"
1309-
1310-
scan_df = (
1311-
pd.concat([ms1_tic_df, ms2_tic_df], axis=0).sort_values(by="scan").reindex()
1312-
)
1313-
1330+
scan_df = pd.DataFrame.from_dict(tic_data)
1331+
scan_df["ms_level"] = None
1332+
13141333
# get scan text
13151334
scan_filter_df = pd.DataFrame.from_dict(
13161335
self.get_all_filters()[0], orient="index"
@@ -1330,11 +1349,11 @@ def get_scan_df(self):
13301349
)
13311350
scan_df["precursor_mz"] = scan_df.scan_text.str.extract(r"(\d+\.\d+)@")
13321351
scan_df["precursor_mz"] = scan_df["precursor_mz"].astype(float)
1333-
scan_df["ms_level"] = scan_df["ms_level"].str.replace("ms", "").astype(int)
13341352

1335-
# Assign each scan as centroid or profile
1353+
# Assign each scan as centroid or profile and add ms_level
13361354
scan_df["ms_format"] = None
13371355
for i in scan_df.scan.to_list():
1356+
scan_df.loc[scan_df.scan == i, "ms_level"] = self.get_ms_level_for_scan_num(i)
13381357
if self.iRawDataPlus.IsCentroidScanFromScanNumber(i):
13391358
scan_df.loc[scan_df.scan == i, "ms_format"] = "centroid"
13401359
else:

0 commit comments

Comments
 (0)