22import toml
33from pathlib import Path
44from multiprocessing import Pool
5+ from concurrent .futures import ThreadPoolExecutor
56import click
67import warnings
78import pandas as pd
1011from corems .mass_spectra .input .corems_hdf5 import ReadCoreMSHDFMassSpectra
1112from corems .mass_spectra .output .export import LipidomicsExport
1213
14+
1315from metaMS .lipid_metadata_prepper import get_lipid_library , _to_flashentropy
1416from metaMS .lcms_functions import (
1517 instantiate_lcms_obj ,
@@ -139,13 +141,17 @@ def run_lipid_sp_ms1(file_in, out_path, params_toml, scan_translator):
139141 mz_dict : dict
140142 Dict with keys "positive" and "negative" and values of lists of precursor mzs
141143 """
142-
143- myLCMSobj = instantiate_lcms_obj (file_in )
144+ click .echo ("in run_lipid_sp_ms1...." )
145+ myLCMSobj = instantiate_lcms_obj (file_in )
146+ click .echo ("calling set_params_on_lcms_obj" )
144147 set_params_on_lcms_obj (myLCMSobj , params_toml )
148+ click .echo ("calling check_scan_translator" )
145149 check_scan_translator (myLCMSobj , scan_translator )
150+ click .echo ("calling add_mass_features" )
146151 add_mass_features (myLCMSobj , scan_translator )
152+ click .echo ("calling remove_unprocessed_data" )
147153 myLCMSobj .remove_unprocessed_data ()
148- #Finally, perform molecular formula search on all ms1 spectra associated with mass features
154+ # Finally, perform molecular formula search on all ms1 spectra associated with mass features
149155 molecular_formula_search (myLCMSobj )
150156 export_results (myLCMSobj , out_path = out_path , final = False )
151157 precursor_mz_list = list (
@@ -422,10 +428,10 @@ def run_lcms_lipidomics_workflow(
422428 cores = lipid_workflow_params .cores
423429 params_toml = lipid_workflow_params .corems_toml_path
424430 scan_translator = lipid_workflow_params .scan_translator_path
425-
431+ use_threads = False
426432 # Limit cores to 1 if the file type is .raw
427- if any (".raw" in file for file in files_list ):
428- cores = 1
433+ if any (".raw" in file . name for file in files_list ):
434+ use_threads = True
429435
430436 click .echo ("Starting lipidomics workflow for " + str (len (files_list )) + " file(s), using " + str (cores ) + " core(s)" )
431437 # Run signal processing, get associated ms1, add associated ms2, do ms1 molecular search, and export intermediate results
@@ -440,17 +446,35 @@ def run_lcms_lipidomics_workflow(
440446 )
441447 mz_dicts .append (mz_dict )
442448 elif cores > 1 :
443- with Pool (cores ) as pool :
444- args = [
445- (
446- str (file_in ),
447- str (file_out ),
448- params_toml ,
449- scan_translator ,
450- )
451- for file_in , file_out in zip (files_list , out_paths_list )
452- ]
453- mz_dicts = pool .starmap (run_lipid_sp_ms1 , args )
449+ click .echo ("Entering multiple cores if condition...." )
450+ if use_threads :
451+ click .echo ("Using threads..." )
452+ with ThreadPoolExecutor (max_workers = cores ) as executor :
453+ click .echo ("In with executor..." )
454+ args = [
455+ (
456+ str (file_in ),
457+ str (file_out ),
458+ params_toml ,
459+ scan_translator ,
460+ )
461+ for file_in , file_out in zip (files_list , out_paths_list )
462+ ]
463+ mz_dicts = list (executor .map (lambda arg : run_lipid_sp_ms1 (* arg ), args ))
464+ click .echo ("Out with executor..." )
465+ else :
466+ with Pool (cores ) as pool :
467+ click .echo ("In with processing pool..." )
468+ args = [
469+ (
470+ str (file_in ),
471+ str (file_out ),
472+ params_toml ,
473+ scan_translator ,
474+ )
475+ for file_in , file_out in zip (files_list , out_paths_list )
476+ ]
477+ mz_dicts = pool .starmap (run_lipid_sp_ms1 , args )
454478
455479 # Prepare metadata for searching
456480 click .echo ("Preparing metadata for ms2 spectral search" )
@@ -469,4 +493,4 @@ def run_lcms_lipidomics_workflow(
469493 args = [(file_out , metadata , scan_translator ) for file_out in out_paths_list ]
470494 pool .starmap (run_lipid_ms2 , args )
471495
472- click .echo ("Lipidomics workflow complete" )
496+ click .echo ("Lipidomics workflow complete" )
0 commit comments