@@ -276,6 +276,10 @@ def run_molecular_formula(self, ms_peaks, **kwargs):
276276 ----------
277277 ms_peaks : list of MSPeak
278278 The list of mass spectrum peaks.
279+ **kwargs
280+ Additional keyword arguments.
281+ Most notably, print_time, which is a boolean flag to indicate whether to print the time
282+ and passed to the timeit decorator.
279283 """
280284 ion_charge = self .mass_spectrum_obj .polarity
281285 min_abundance = self .mass_spectrum_obj .min_abundance
@@ -285,7 +289,7 @@ def run_molecular_formula(self, ms_peaks, **kwargs):
285289 # reset average error, only relevant is average mass error method is being used
286290 SearchMolecularFormulaWorker (
287291 find_isotopologues = self .find_isotopologues
288- ).reset_error ()
292+ ).reset_error (self . mass_spectrum_obj )
289293
290294 # check database for all possible molecular formula combinations based on the setting passed to self.mass_spectrum_obj.molecular_search_settings
291295 classes = MolecularCombinations (self .sql_db ).runworker (
@@ -519,13 +523,21 @@ def __call__(self, args):
519523 """
520524 return self .find_formulas (* args ) # ,args[1]
521525
522- def reset_error (self ):
523- """Reset the error variables"""
526+ def reset_error (self , mass_spectrum_obj ):
527+ """Reset the error variables.
528+
529+ Parameters
530+ ----------
531+ mass_spectrum_obj : MassSpectrum
532+ The mass spectrum object.
533+
534+ Notes
535+ -----
536+ This function resets the error variables for the given mass spectrum object.
537+ """
524538 global last_error , last_dif , closest_error , error_average , nbValues
525539 last_error , last_dif , closest_error , nbValues = 0.0 , 0.0 , 0.0 , 0.0
526540
527- error_average = 0
528-
529541 def set_last_error (self , error , mass_spectrum_obj ):
530542 """Set the last error.
531543
@@ -846,13 +858,13 @@ def mass_by_ion_type(possible_formula_obj):
846858 return mspeak_assigned_index
847859
848860
849- class SearchMolecularFormulasLC () :
861+ class SearchMolecularFormulasLC :
850862 """Class for searching molecular formulas in a LC object.
851863
852864 Parameters
853865 ----------
854- lcms_obj : LC
855- The LC object.
866+ lcms_obj : LCMSBase
867+ The LCMSBase object.
856868 sql_db : MolForm_SQL, optional
857869 The SQL database object, by default None.
858870 first_hit : bool, optional
@@ -863,11 +875,20 @@ class SearchMolecularFormulasLC():
863875 Methods
864876 -------
865877
878+ * search_spectra_against_candidates().
879+ Search a list of mass spectra against a list of candidate formulas with a given ion type and charge.
880+ * bulk_run_molecular_formula_search().
881+ Run the molecular formula search on the given list of mass spectra.
882+ Pulls the settings from the LCMSBase object to set ion type and charge to search for.
883+ * run_mass_feature_search().
884+ Run the molecular formula search on mass features.
885+ Calls bulk_run_molecular_formula_search() with specified mass spectra and mass peaks.
866886 * run_untargeted_worker_ms1().
867887 Run untargeted molecular formula search on the ms1 mass spectrum.
888+ DEPRECATED: use run_mass_feature_search() or bulk_run_molecular_formula_search() instead.
868889 * run_target_worker_ms1().
869890 Run targeted molecular formula search on the ms1 mass spectrum.
870-
891+ DEPRECATED: use run_mass_feature_search() or bulk_run_molecular_formula_search() instead.
871892 """
872893
873894 def __init__ (self , lcms_obj , sql_db = None , first_hit = False , find_isotopologues = True ):
@@ -947,6 +968,7 @@ def bulk_run_molecular_formula_search(self, mass_spectrum_list, ms_peaks_list, m
947968
948969 nominal_mzs = [x .nominal_mz for x in mass_spectrum_list ]
949970 nominal_mzs = list (set ([item for sublist in nominal_mzs for item in sublist ]))
971+ verbose = self .lcms_obj .parameters .mass_spectrum [mass_spectrum_setting_key ].molecular_search .verbose_processing
950972
951973 # reset average error, only relevant is average mass error method is being used
952974 SearchMolecularFormulaWorker (
@@ -973,7 +995,7 @@ def bulk_run_molecular_formula_search(self, mass_spectrum_list, ms_peaks_list, m
973995 ion_charge ,
974996 )
975997
976- pbar = tqdm .tqdm (classe_chunk )
998+ pbar = tqdm .tqdm (classe_chunk , disable = not verbose )
977999 for classe_tuple in pbar :
9781000 # class string is a json serialized dict
9791001 classe_str = classe_tuple [0 ]
@@ -1048,10 +1070,13 @@ def bulk_run_molecular_formula_search(self, mass_spectrum_list, ms_peaks_list, m
10481070 self .sql_db .close ()
10491071
10501072 def run_mass_feature_search (self ):
1051- """Run the molecular formula search on the mass features."""
1073+ """Run the molecular formula search on the mass features.
1074+
1075+ Calls bulk_run_molecular_formula_search() with specified mass spectra and mass peaks.
1076+ """
10521077 mass_features_df = self .lcms_obj .mass_features_to_df ()
10531078
1054- # Get the list of mass spectrum to search and peaks to search with each mass spectrum
1079+ # Get the list of mass spectrum ( and peaks to search with each mass spectrum) for all mass features
10551080 scan_list = mass_features_df .apex_scan .unique ()
10561081 mass_spectrum_list = [self .lcms_obj ._ms [x ] for x in scan_list ]
10571082 ms_peaks = []
@@ -1061,71 +1086,14 @@ def run_mass_feature_search(self):
10611086 self .lcms_obj .mass_features [x ].ms1_peak for x in mf_df_scan .index .tolist ()
10621087 ]
10631088 ms_peaks .append (peaks_to_search )
1089+
1090+ # Run the molecular formula search
10641091 self .bulk_run_molecular_formula_search (mass_spectrum_list , ms_peaks )
10651092
10661093 def run_untargeted_worker_ms1 (self ):
10671094 """Run untargeted molecular formula search on the ms1 mass spectrum."""
1068- # do molecular formula based on the parameters set for ms1 search
1069- for peak in self .lcms_obj :
1070- self .mass_spectrum_obj = peak .mass_spectrum
1071- self .run_molecular_formula (
1072- peak .mass_spectrum .sort_by_abundance (),
1073- print_time = self .lcms_obj .parameters .lc_ms .verbose_processing
1074- )
1095+ raise NotImplementedError ("run_untargeted_worker_ms1 search is not implemented in CoreMS 3.0 and greater" )
10751096
10761097 def run_target_worker_ms1 (self ):
10771098 """Run targeted molecular formula search on the ms1 mass spectrum."""
1078- # do molecular formula based on the external molecular reference list
1079- verbose = self .lcms_obj .parameters .lc_ms .verbose_processing
1080- if verbose :
1081- pbar = tqdm .tqdm (self .lcms_obj )
1082-
1083- for peak in self .lcms_obj :
1084- if verbose :
1085- pbar .set_description_str (
1086- desc = f"Started molecular formulae search for mass spectrum at RT { peak .retention_time } s" ,
1087- refresh = True ,
1088- )
1089-
1090- self .mass_spectrum_obj = peak .mass_spectrum
1091-
1092- ion_charge = self .mass_spectrum_obj .polarity
1093-
1094- candidate_formulas = peak .targeted_molecular_formulas
1095-
1096- for i in candidate_formulas :
1097- if verbose :
1098- print (i )
1099- if self .mass_spectrum_obj .molecular_search_settings .isProtonated :
1100- ion_type = Labels .protonated_de_ion
1101-
1102- # ms_peaks_assigned = self.search_mol_formulas(peak.targeted_molecular_formulas, ion_type, find_isotopologues=True)
1103-
1104- self .search_mol_formulas (
1105- candidate_formulas , ion_type , find_isotopologues = True
1106- )
1107-
1108- if self .mass_spectrum_obj .molecular_search_settings .isRadical :
1109- ion_type = Labels .radical_ion
1110-
1111- # ms_peaks_assigned = self.search_mol_formulas(peak.targeted_molecular_formulas, ion_type, find_isotopologues=True)
1112- self .search_mol_formulas (
1113- candidate_formulas , ion_type , find_isotopologues = True
1114- )
1115-
1116- if self .mass_spectrum_obj .molecular_search_settings .isAdduct :
1117- ion_type = Labels .adduct_ion
1118-
1119- adduct_list = (
1120- self .mass_spectrum_obj .molecular_search_settings .adduct_atoms_neg
1121- if ion_charge < 0
1122- else self .mass_spectrum_obj .molecular_search_settings .adduct_atoms_pos
1123- )
1124-
1125- for adduct_atom in adduct_list :
1126- self .search_mol_formulas (
1127- candidate_formulas ,
1128- ion_type ,
1129- find_isotopologues = True ,
1130- adduct_atom = adduct_atom ,
1131- )
1099+ raise NotImplementedError ("run_target_worker_ms1 formula search is not yet implemented in CoreMS 3.0 and greater" )
0 commit comments