Skip to content

Commit d3d0eaa

Browse files
committed
Merge branch 'master' into 182_precursor_request_patch
2 parents 293b7ce + 6791b0c commit d3d0eaa

26 files changed

Lines changed: 691 additions & 761 deletions

corems/__init__.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,22 @@
2121

2222

2323

24-
def timeit(method):
25-
def timed(*args, **kw):
26-
ts = time.time()
27-
result = method(*args, **kw)
28-
te = time.time()
29-
if "log_time" in kw:
30-
name = kw.get("log_name", method.__name__.upper())
31-
kw["log_time"][name] = int((te - ts) * 1000)
32-
else:
33-
print("%r %2.2f ms" % (method.__name__, (te - ts) * 1000))
34-
return result
35-
36-
return timed
24+
def timeit(print_time=True):
25+
def decorator(method):
26+
def timed(*args, **kw):
27+
# Extract print_time from kwargs if provided
28+
local_print_time = kw.pop('print_time', print_time)
29+
ts = time.time()
30+
result = method(*args, **kw)
31+
te = time.time()
32+
if "log_time" in kw:
33+
name = kw.get("log_name", method.__name__.upper())
34+
kw["log_time"][name] = int((te - ts) * 1000)
35+
elif local_print_time:
36+
print("%r %2.2f ms" % (method.__name__, (te - ts) * 1000))
37+
return result
38+
return timed
39+
return decorator
3740

3841

3942
class SuppressPrints:

corems/encapsulation/factory/parameters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
)
1414
from corems.encapsulation.factory.processingSetting import DataInputSetting
1515

16+
def hush_output():
17+
"""Toggle all the verbose_processing flags to False on the MSParameters, GCMSParameters and LCMSParameters classes"""
18+
MSParameters.molecular_search.verbose_processing = False
19+
MSParameters.mass_spectrum.verbose_processing = False
20+
GCMSParameters.gc_ms.verbose_processing = False
21+
LCMSParameters.lc_ms.verbose_processing = False
1622

1723
def reset_ms_parameters():
1824
"""Reset the MSParameter class to the default values"""

corems/encapsulation/factory/processingSetting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,10 @@ class MolecularFormulaSearchSettings:
816816
m/z error average. Default is 0.0.
817817
used_atom_valences : dict, optional
818818
Dictionary of atoms and valences. Default is {'C': 4, '13C': 4, 'H': 1, 'O': 2, '18O': 2, 'N': 3, 'S': 2, '34S': 2, 'P': 3, 'Cl': 1, '37Cl': 1, 'Br': 1, 'Na': 1, 'F': 1, 'K': 0}.
819+
verbose_processing: bool, optional
820+
If True, print verbose processing information. Default is True.
819821
"""
822+
verbose_processing: bool = True
820823

821824
use_isotopologue_filter: bool = False
822825

corems/mass_spectra/factory/GC_Class.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ def set_scans_number_from_data(self):
201201

202202
self.scans_number = sorted(self._ms.keys())
203203

204+
@property
205+
def parameters(self):
206+
"""GCMS Parameters"""
207+
return self._parameters
208+
209+
@parameters.setter
210+
def parameters(self, gcms_parameters_instance):
211+
self._parameters = gcms_parameters_instance
212+
213+
# Note: maintaining `parameter` for backwards compatibility,
214+
# but proper usage would reference `parameters` to conform
215+
# to other classes.
204216
@property
205217
def parameter(self):
206218
"""GCMS Parameters"""
@@ -213,20 +225,20 @@ def parameter(self, gcms_parameters_instance):
213225
@property
214226
def molecular_search_settings(self):
215227
"""Molecular Search Settings"""
216-
return self.parameter.molecular_search
228+
return self.parameters.molecular_search
217229

218230
@molecular_search_settings.setter
219231
def molecular_search_settings(self, settings_class_instance):
220-
self.parameter.molecular_search = settings_class_instance
232+
self.parameters.molecular_search = settings_class_instance
221233

222234
@property
223235
def chromatogram_settings(self):
224236
"""Chromatogram Settings"""
225-
return self.parameter.gc_ms
237+
return self.parameters.gc_ms
226238

227239
@chromatogram_settings.setter
228240
def chromatogram_settings(self, settings_class_instance):
229-
self.parameter.gc_ms = settings_class_instance
241+
self.parameters.gc_ms = settings_class_instance
230242

231243
@property
232244
def scans_number(self):

corems/mass_spectrum/calc/Calibration.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def __init__(self, mass_spectrum, ref_masslist, mzsegment=None):
6565

6666
# define reference mass list - bruker .ref format
6767
self.ref_mass_list_path = ref_masslist
68-
if self.mass_spectrum.percentile_assigned()[0] != 0:
68+
if self.mass_spectrum.percentile_assigned(mute_output=True)[0] != 0:
6969
warnings.warn(
7070
"Warning: calibrating spectra which have already been assigned may yield erroneous results"
7171
)
@@ -77,10 +77,6 @@ def __init__(self, mass_spectrum, ref_masslist, mzsegment=None):
7777
"MS Obj loaded - " + str(len(mass_spectrum.mspeaks)) + " peaks found."
7878
)
7979

80-
print(
81-
"MS Obj loaded - " + str(len(mass_spectrum.mspeaks)) + " peaks found."
82-
)
83-
8480
def load_ref_mass_list(self):
8581
"""Load reference mass list (Bruker format)
8682
@@ -228,7 +224,7 @@ def find_calibration_points(
228224
cal_peaks_mz = list(tmpdf.values)
229225
cal_refs_mz = list(tmpdf.index)
230226
elif calibration_ref_match_method == "merged":
231-
warnings.warn("Using experimental new reference mass list merging")
227+
#warnings.warn("Using experimental new reference mass list merging")
232228
# This is a new approach (August 2024) which uses Pandas 'merged_asof' to find the peaks closest in m/z between
233229
# reference and measured masses. This is a quicker way to match, and seems to get more matches.
234230
# It may not work as well when the data are far from correc initial mass

corems/mass_spectrum/calc/MassErrorPrediction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ def get_results(self):
9090

9191
def calc_error_dist(self):
9292
"""Calculate the error distribution."""
93+
verbose = self.mass_spectrum_obj.parameters.mass_spectrum.verbose_processing
9394
results_list = []
9495

9596
indexes_without_results = list(range(len(self.mass_spectrum_obj)))
9697
# loop trough mass spectrum
97-
98-
for peak_obj_idx, peak_obj in enumerate(tqdm(self.mass_spectrum_obj)):
98+
for peak_obj_idx, peak_obj in enumerate(tqdm(self.mass_spectrum_obj), disable=not verbose):
9999
# access ms peaks triplets ( peak_obj_idx -1, peak_obj_idx, and peak_obj_idx + 1)
100100
# check lower and upper boundaries to not excesses mass spectrum range
101101

corems/mass_spectrum/calc/MassSpectrumCalc.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ class MassSpecCalc(PeakPicking, NoiseThresholdCalc):
3838
Calculate the weight average molecular weight
3939
"""
4040

41-
def percentile_assigned(self, report_error: bool = False):
41+
def percentile_assigned(self, report_error: bool = False, mute_output: bool = False):
4242
"""Percentage of peaks which are assigned
4343
4444
Parameters
4545
-----------
4646
report_error: bool, optional
4747
Report the error of the assigned peaks. Default is False.
48+
mute_output: bool, optional
49+
Override the verbose setting. Default is False.
50+
If True, the function will silence results
4851
"""
4952
verbose = self.parameters.mass_spectrum.verbose_processing
5053
assign_abun = 0
@@ -68,15 +71,15 @@ def percentile_assigned(self, report_error: bool = False):
6871
total_relative_abundance = (assign_abun / (not_assign_abun + assign_abun)) * 100
6972
if report_error:
7073
rms_error = sqrt(mean(array(error) ** 2))
71-
if verbose:
74+
if verbose and not mute_output:
7275
print(
7376
"%i assigned peaks and %i unassigned peaks, total = %.2f %%, relative abundance = %.2f %%, RMS error (best candidate) (ppm) = %.3f"
7477
% (i, j, total_percent, total_relative_abundance, rms_error)
7578
)
7679
return i, j, total_percent, total_relative_abundance, rms_error
7780

7881
else:
79-
if verbose:
82+
if verbose and not mute_output:
8083
print(
8184
"%i assigned peaks and %i unassigned peaks , total = %.2f %%, relative abundance = %.2f %%"
8285
% (

corems/mass_spectrum/input/massList.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def add_molecular_formula(self, mass_spec_obj, dataframe):
175175
# Next, generate isotopologues from the parent
176176
isos = list(
177177
mono_mfobj.isotopologues(
178-
min_abundance=mass_spec_obj[df_index].abundance * 0.001,
178+
min_abundance=mass_spec_obj.abundance.min()*0.01,
179179
current_mono_abundance=mass_spec_obj[mono_index].abundance,
180180
dynamic_range=mass_spec_obj.dynamic_range,
181181
)

corems/molecular_id/factory/MolecularLookupTable.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,17 @@ def add_carbonsHydrogens(self, settings, existing_classes_objs):
364364
self.sql_db.session.execute(insert_query)
365365
self.sql_db.session.commit()
366366

367-
@timeit
368-
def runworker(self, molecular_search_settings):
367+
@timeit(print_time=True)
368+
def runworker(self, molecular_search_settings, **kwargs):
369369
"""Run the molecular formula lookup table worker.
370370
371371
Parameters
372372
----------
373373
molecular_search_settings : object
374374
An object containing user-defined settings.
375+
kwargs : dict
376+
A dictionary of keyword arguments.
377+
Most notably, the print_time argument which is passed to the timeit decorator.
375378
376379
Returns
377380
-------
@@ -380,6 +383,7 @@ def runworker(self, molecular_search_settings):
380383
381384
382385
"""
386+
verbose = molecular_search_settings.verbose_processing
383387

384388
classes_list, class_to_create, existing_classes_objs = (
385389
self.check_database_get_class_list(molecular_search_settings)
@@ -412,7 +416,7 @@ def runworker(self, molecular_search_settings):
412416
self.even_ch_dbe = [obj.dbe for obj in even_ch_obj]
413417

414418
all_results = list()
415-
for class_tuple in tqdm(class_to_create):
419+
for class_tuple in tqdm(class_to_create, disable = not verbose):
416420
results = self.populate_combinations(class_tuple, settings)
417421
all_results.extend(results)
418422
if settings.db_jobs == 1:
@@ -438,8 +442,8 @@ def runworker(self, molecular_search_settings):
438442
]
439443
p = multiprocessing.Pool(settings.db_jobs)
440444
for class_list in tqdm(
441-
p.imap_unordered(insert_database_worker, worker_args)
442-
):
445+
p.imap_unordered(insert_database_worker, worker_args), disable= not verbose
446+
):
443447
pass
444448
p.close()
445449
p.join()

corems/molecular_id/factory/molecularSQL.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ def get_dict_by_classes(
473473
if the number of classes and nominal_m/zs are higher than 999 the query will fail
474474
Solution: use postgres or split query
475475
"""
476+
verbose = molecular_search_settings.verbose_processing
476477

477478
def query_normal(class_list, len_adduct):
478479
"""query for normal database
@@ -565,10 +566,7 @@ def nominal_mass_by_ion_type(formula_obj):
565566

566567
elif ion_type == Labels.adduct_ion and adduct_atom:
567568
return int(formula_obj._adduct_mz(ion_charge, adduct_atom))
568-
569-
for formula_obj, ch_obj, classe_obj in tqdm.tqdm(
570-
formulas, desc="Loading molecular formula database"
571-
):
569+
for formula_obj, ch_obj, classe_obj in tqdm.tqdm(formulas, desc="Loading molecular formula database", disable = not verbose):
572570
nominal_mz = nominal_mass_by_ion_type(formula_obj)
573571

574572
if self.type != "normal":

0 commit comments

Comments
 (0)