CoreMS 4.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- corems/__init__.py +63 -0
- corems/chroma_peak/__init__.py +0 -0
- corems/chroma_peak/calc/ChromaPeakCalc.py +480 -0
- corems/chroma_peak/calc/__init__.py +0 -0
- corems/chroma_peak/calc/subset.py +196 -0
- corems/chroma_peak/factory/__init__.py +0 -0
- corems/chroma_peak/factory/chroma_peak_classes.py +1178 -0
- corems/encapsulation/__init__.py +0 -0
- corems/encapsulation/constant.py +1283 -0
- corems/encapsulation/factory/__init__.py +0 -0
- corems/encapsulation/factory/parameters.py +392 -0
- corems/encapsulation/factory/processingSetting.py +1244 -0
- corems/encapsulation/input/__init__.py +0 -0
- corems/encapsulation/input/parameter_from_json.py +579 -0
- corems/encapsulation/output/__init__.py +0 -0
- corems/encapsulation/output/parameter_to_dict.py +142 -0
- corems/encapsulation/output/parameter_to_json.py +332 -0
- corems/mass_spectra/__init__.py +0 -0
- corems/mass_spectra/calc/GC_Calc.py +84 -0
- corems/mass_spectra/calc/GC_Deconvolution.py +558 -0
- corems/mass_spectra/calc/GC_RI_Calibration.py +44 -0
- corems/mass_spectra/calc/MZSearch.py +249 -0
- corems/mass_spectra/calc/SignalProcessing.py +656 -0
- corems/mass_spectra/calc/__init__.py +0 -0
- corems/mass_spectra/calc/lc_calc.py +5790 -0
- corems/mass_spectra/calc/lc_calc_operations.py +1127 -0
- corems/mass_spectra/factory/GC_Class.py +791 -0
- corems/mass_spectra/factory/__init__.py +0 -0
- corems/mass_spectra/factory/chromat_data.py +51 -0
- corems/mass_spectra/factory/lc_class.py +2721 -0
- corems/mass_spectra/input/__init__.py +0 -0
- corems/mass_spectra/input/andiNetCDF.py +200 -0
- corems/mass_spectra/input/boosterHDF5.py +216 -0
- corems/mass_spectra/input/brukerSolarix.py +153 -0
- corems/mass_spectra/input/brukerSolarix_utils.py +73 -0
- corems/mass_spectra/input/corems_hdf5.py +1710 -0
- corems/mass_spectra/input/massList.py +133 -0
- corems/mass_spectra/input/mzml.py +668 -0
- corems/mass_spectra/input/parserbase.py +239 -0
- corems/mass_spectra/input/rawFileReader.py +1839 -0
- corems/mass_spectra/output/__init__.py +0 -0
- corems/mass_spectra/output/export.py +2800 -0
- corems/mass_spectrum/__init__.py +0 -0
- corems/mass_spectrum/calc/AutoRecalibration.py +237 -0
- corems/mass_spectrum/calc/Calibration.py +602 -0
- corems/mass_spectrum/calc/CalibrationCalc.py +253 -0
- corems/mass_spectrum/calc/KendrickGroup.py +239 -0
- corems/mass_spectrum/calc/MassErrorPrediction.py +436 -0
- corems/mass_spectrum/calc/MassSpectrumCalc.py +303 -0
- corems/mass_spectrum/calc/MeanResolvingPowerFilter.py +212 -0
- corems/mass_spectrum/calc/NoiseCalc.py +371 -0
- corems/mass_spectrum/calc/NoiseCalc_Bayes.py +93 -0
- corems/mass_spectrum/calc/PeakPicking.py +994 -0
- corems/mass_spectrum/calc/__init__.py +0 -0
- corems/mass_spectrum/factory/MassSpectrumClasses.py +1753 -0
- corems/mass_spectrum/factory/__init__.py +0 -0
- corems/mass_spectrum/input/__init__.py +0 -0
- corems/mass_spectrum/input/baseClass.py +531 -0
- corems/mass_spectrum/input/boosterHDF5.py +161 -0
- corems/mass_spectrum/input/coremsHDF5.py +475 -0
- corems/mass_spectrum/input/massList.py +402 -0
- corems/mass_spectrum/input/numpyArray.py +133 -0
- corems/mass_spectrum/output/__init__.py +0 -0
- corems/mass_spectrum/output/export.py +841 -0
- corems/molecular_formula/__init__.py +0 -0
- corems/molecular_formula/calc/MolecularFormulaCalc.py +885 -0
- corems/molecular_formula/calc/__init__.py +0 -0
- corems/molecular_formula/factory/MolecularFormulaFactory.py +895 -0
- corems/molecular_formula/factory/__init__.py +0 -0
- corems/molecular_formula/input/__init__.py +0 -0
- corems/molecular_formula/input/masslist_ref.py +355 -0
- corems/molecular_id/__init__.py +0 -0
- corems/molecular_id/calc/ClusterFilter.py +251 -0
- corems/molecular_id/calc/MolecularFilter.py +122 -0
- corems/molecular_id/calc/SpectralSimilarity.py +632 -0
- corems/molecular_id/calc/__init__.py +0 -0
- corems/molecular_id/calc/math_distance.py +1637 -0
- corems/molecular_id/data/FAMES_REF.msp +980 -0
- corems/molecular_id/data/PNNLMetV20191015.msp +157267 -0
- corems/molecular_id/factory/EI_SQL.py +650 -0
- corems/molecular_id/factory/MolecularLookupTable.py +914 -0
- corems/molecular_id/factory/__init__.py +0 -0
- corems/molecular_id/factory/classification.py +884 -0
- corems/molecular_id/factory/lipid_molecular_metadata.py +50 -0
- corems/molecular_id/factory/molecularSQL.py +827 -0
- corems/molecular_id/factory/spectrum_search_results.py +119 -0
- corems/molecular_id/input/__init__.py +0 -0
- corems/molecular_id/input/nistMSI.py +148 -0
- corems/molecular_id/search/__init__.py +0 -0
- corems/molecular_id/search/compoundSearch.py +214 -0
- corems/molecular_id/search/database_interfaces.py +1527 -0
- corems/molecular_id/search/findOxygenPeaks.py +330 -0
- corems/molecular_id/search/lcms_spectral_search.py +348 -0
- corems/molecular_id/search/molecularFormulaSearch.py +1117 -0
- corems/molecular_id/search/priorityAssignment.py +723 -0
- corems/ms_peak/__init__.py +0 -0
- corems/ms_peak/calc/MSPeakCalc.py +1010 -0
- corems/ms_peak/calc/__init__.py +0 -0
- corems/ms_peak/factory/MSPeakClasses.py +542 -0
- corems/ms_peak/factory/__init__.py +0 -0
- corems/transient/__init__.py +0 -0
- corems/transient/calc/TransientCalc.py +362 -0
- corems/transient/calc/__init__.py +0 -0
- corems/transient/factory/TransientClasses.py +457 -0
- corems/transient/factory/__init__.py +0 -0
- corems/transient/input/__init__.py +0 -0
- corems/transient/input/brukerSolarix.py +461 -0
- corems/transient/input/midasDatFile.py +172 -0
- corems-4.0.0.dist-info/METADATA +475 -0
- corems-4.0.0.dist-info/RECORD +171 -0
- corems-4.0.0.dist-info/WHEEL +5 -0
- corems-4.0.0.dist-info/licenses/LICENSE +22 -0
- corems-4.0.0.dist-info/top_level.txt +4 -0
- examples/archive/scripts/CoreMS_tutorial.py +94 -0
- examples/archive/scripts/DI HR-MS Halogens Bruker.py +196 -0
- examples/archive/scripts/DI HR-MS MassList.py +385 -0
- examples/archive/scripts/GC-MS MetabRef.py +213 -0
- examples/archive/scripts/GC-MS NetCDF.py +217 -0
- examples/archive/scripts/HR-MS Thermo Raw 21T.py +136 -0
- examples/archive/scripts/LC-ICPMS_metal_peaks.py +297 -0
- examples/archive/scripts/LCMS-Thermo.py +460 -0
- examples/archive/scripts/LCMS_isotopes.py +283 -0
- examples/archive/scripts/MSParams_example.py +21 -0
- examples/archive/scripts/Molecular Formula Data Aggreation.py +84 -0
- examples/archive/scripts/Single Mz Search.py +69 -0
- examples/test_notebooks.py +145 -0
- ext_lib/ChemstationMSFileReader.dll +0 -0
- ext_lib/ChemstationMSFileReader.xml +126 -0
- ext_lib/RawFileReaderLicense.doc +0 -0
- ext_lib/ThermoFisher.CommonCore.BackgroundSubtraction.dll +0 -0
- ext_lib/ThermoFisher.CommonCore.BackgroundSubtraction.xml +2307 -0
- ext_lib/ThermoFisher.CommonCore.Data.dll +0 -0
- ext_lib/ThermoFisher.CommonCore.Data.xml +28974 -0
- ext_lib/ThermoFisher.CommonCore.MassPrecisionEstimator.dll +0 -0
- ext_lib/ThermoFisher.CommonCore.MassPrecisionEstimator.xml +241 -0
- ext_lib/ThermoFisher.CommonCore.RawFileReader.dll +0 -0
- ext_lib/ThermoFisher.CommonCore.RawFileReader.xml +31174 -0
- ext_lib/__init__.py +0 -0
- ext_lib/dotnet/OpenMcdf.Extensions.dll +0 -0
- ext_lib/dotnet/OpenMcdf.dll +0 -0
- ext_lib/dotnet/OpenMcdf.xml +1154 -0
- ext_lib/dotnet/System.IO.FileSystem.AccessControl.dll +0 -0
- ext_lib/dotnet/System.IO.FileSystem.AccessControl.xml +506 -0
- ext_lib/dotnet/System.Security.AccessControl.dll +0 -0
- ext_lib/dotnet/System.Security.AccessControl.xml +2043 -0
- ext_lib/dotnet/System.Security.Principal.Windows.dll +0 -0
- ext_lib/dotnet/System.Security.Principal.Windows.xml +1011 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.BackgroundSubtraction.dll +0 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.BackgroundSubtraction.xml +2307 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.Data.dll +0 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.Data.xml +29148 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.MassPrecisionEstimator.dll +0 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.MassPrecisionEstimator.xml +241 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.RawFileReader.dll +0 -0
- ext_lib/dotnet/ThermoFisher.CommonCore.RawFileReader.xml +31492 -0
- ext_lib/version +18 -0
- support_code/atom_parsers/AtomsDescription.py +359 -0
- support_code/atom_parsers/CreateAtomsDescription.py +56 -0
- support_code/nmdc/filefinder.py +90 -0
- support_code/nmdc/lipidomics/lipidomics_workflow.py +748 -0
- support_code/nmdc/lipidomics/manifest_examples.py +49 -0
- support_code/nmdc/metabolomics/gcms_workflow.py +158 -0
- support_code/nmdc/metabolomics/lcms_metabolomics_targeted_search.py +59 -0
- support_code/nmdc/metabolomics/lcms_metabolomics_workflow.py +248 -0
- support_code/nmdc/metabolomics/metabolomics_collection.py +628 -0
- support_code/nmdc/metadata/dms_api.py +42 -0
- support_code/nmdc/nom/archived_scripts/nmdc_metadata_gen.py +288 -0
- support_code/nmdc/nom/archived_scripts/nom_grow_workflow.py +209 -0
- support_code/nmdc/nom/nom_workflow.py +312 -0
- support_code/windows_only_importers/BrukerCompassXtract.py +180 -0
- support_code/windows_only_importers/ThermoMSFileReader.py +405 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class FreqDomain_Calibration:
|
|
5
|
+
"""Frequency Domain Calibration class for mass spectrum.
|
|
6
|
+
|
|
7
|
+
Parameters
|
|
8
|
+
----------
|
|
9
|
+
mass_spectrum : MassSpectrum
|
|
10
|
+
The mass spectrum object.
|
|
11
|
+
selected_mass_peaks : list
|
|
12
|
+
List of selected mass peaks.
|
|
13
|
+
include_isotopologue : bool, optional
|
|
14
|
+
Flag to include isotopologues, by default False.
|
|
15
|
+
|
|
16
|
+
Attributes
|
|
17
|
+
----------
|
|
18
|
+
mz_exp : ndarray
|
|
19
|
+
Array of experimental m/z values.
|
|
20
|
+
mz_calc : ndarray
|
|
21
|
+
Array of calculated m/z values.
|
|
22
|
+
freq_exp : ndarray
|
|
23
|
+
Array of experimental frequencies.
|
|
24
|
+
mass_spectrum : MassSpectrum
|
|
25
|
+
The mass spectrum object.
|
|
26
|
+
freq_exp_ms : ndarray
|
|
27
|
+
Array of experimental frequencies for mass spectrum.
|
|
28
|
+
|
|
29
|
+
Methods
|
|
30
|
+
-------
|
|
31
|
+
* recal_mass_spec(mz_domain, Aterm, Bterm, Cterm).
|
|
32
|
+
Recalibrate the mass spectrum with the given parameters.
|
|
33
|
+
* linear().
|
|
34
|
+
Perform linear calibration.
|
|
35
|
+
* quadratic(iteration=False).
|
|
36
|
+
Perform quadratic calibration.
|
|
37
|
+
* ledford_calibration(iteration=False).
|
|
38
|
+
Perform Ledford calibration.
|
|
39
|
+
* step_fit(steps=4).
|
|
40
|
+
Perform step fit calibration.
|
|
41
|
+
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(self, mass_spectrum, selected_mass_peaks, include_isotopologue=False):
|
|
45
|
+
self.selected_mspeaks = selected_mass_peaks
|
|
46
|
+
error = list()
|
|
47
|
+
freq_exp = list()
|
|
48
|
+
mz_calc = list()
|
|
49
|
+
mz_exp = list()
|
|
50
|
+
|
|
51
|
+
for mspeak in selected_mass_peaks:
|
|
52
|
+
if not include_isotopologue:
|
|
53
|
+
molecular_formulas = [
|
|
54
|
+
formula for formula in mspeak if not formula.is_isotopologue
|
|
55
|
+
]
|
|
56
|
+
else:
|
|
57
|
+
molecular_formulas = mspeak
|
|
58
|
+
|
|
59
|
+
for molecular_formula in molecular_formulas:
|
|
60
|
+
freq_exp.append(mspeak.freq_exp)
|
|
61
|
+
error.append(molecular_formula.mz_error)
|
|
62
|
+
mz_calc.append(molecular_formula.mz_calc)
|
|
63
|
+
mz_exp.append(mspeak.mz_exp)
|
|
64
|
+
|
|
65
|
+
self.mz_exp = np.array(mz_exp)
|
|
66
|
+
self.mz_calc = np.array(mz_calc)
|
|
67
|
+
self.freq_exp = np.array(freq_exp)
|
|
68
|
+
self.mass_spectrum = mass_spectrum
|
|
69
|
+
self.freq_exp_ms = np.array([mspeak.freq_exp for mspeak in mass_spectrum])
|
|
70
|
+
|
|
71
|
+
def recal_mass_spec(self, mz_domain, Aterm, Bterm, Cterm):
|
|
72
|
+
"""Recalibrate the mass spectrum with the given parameters.
|
|
73
|
+
|
|
74
|
+
Parameters
|
|
75
|
+
----------
|
|
76
|
+
mz_domain : ndarray
|
|
77
|
+
Array of m/z values for recalibration.
|
|
78
|
+
Aterm : float
|
|
79
|
+
Aterm parameter for recalibration.
|
|
80
|
+
Bterm : float
|
|
81
|
+
Bterm parameter for recalibration.
|
|
82
|
+
Cterm : float
|
|
83
|
+
Cterm parameter for recalibration.
|
|
84
|
+
|
|
85
|
+
"""
|
|
86
|
+
self.mass_spectrum._calibration_terms = (Aterm, Bterm, 0)
|
|
87
|
+
self.mass_spectrum.mz_cal = mz_domain
|
|
88
|
+
|
|
89
|
+
def linear(self):
|
|
90
|
+
"""Perform linear calibration."""
|
|
91
|
+
matrix = np.vstack([1 / self.freq_exp, np.ones(len(self.freq_exp))]).T
|
|
92
|
+
Aterm, Bterm = np.linalg.lstsq(matrix, self.mz_calc, rcond=None)[0]
|
|
93
|
+
if self.mass_spectrum.parameters.mass_spectrum.verbose_processing:
|
|
94
|
+
print("%.2f Aterm, %.2f Bterm" % (Aterm, Bterm))
|
|
95
|
+
print("Linear Calibration %.2f Aterm, %.2f Bterm " % (Aterm, Bterm))
|
|
96
|
+
mz_domain = (Aterm / self.freq_exp_ms) + Bterm
|
|
97
|
+
self.recal_mass_spec(mz_domain, Aterm, Bterm, 0)
|
|
98
|
+
|
|
99
|
+
def quadratic(self, iteration: bool = False):
|
|
100
|
+
"""Perform quadratic calibration.
|
|
101
|
+
|
|
102
|
+
Parameters
|
|
103
|
+
----------
|
|
104
|
+
iteration : bool, optional
|
|
105
|
+
Flag to perform iterative calibration, by default False.
|
|
106
|
+
|
|
107
|
+
"""
|
|
108
|
+
mz_calc = self.mz_calc
|
|
109
|
+
freq_exp = self.freq_exp
|
|
110
|
+
mz_exp = self.mz_exp
|
|
111
|
+
|
|
112
|
+
error = ((mz_exp - mz_calc) / mz_calc) * 1000000
|
|
113
|
+
last_rms = np.sqrt(np.mean(error**2))
|
|
114
|
+
while True:
|
|
115
|
+
matrix = np.vstack(
|
|
116
|
+
[1 / freq_exp, 1 / np.power(freq_exp, 2), np.ones(len(freq_exp))]
|
|
117
|
+
).T
|
|
118
|
+
Aterm, Bterm, Cterm = np.linalg.lstsq(matrix, self.mz_calc, rcond=None)[0]
|
|
119
|
+
mz_exp = (Aterm / (freq_exp)) + (Bterm / np.power((freq_exp), 2)) + Cterm
|
|
120
|
+
error = ((mz_exp - mz_calc) / mz_calc) * 1000000
|
|
121
|
+
rms = np.sqrt(np.mean(error**2))
|
|
122
|
+
std = np.std(error)
|
|
123
|
+
if self.mass_spectrum.parameters.mass_spectrum.verbose_processing:
|
|
124
|
+
print("%.2f Aterm, %.2f Bterm" % (Aterm, Bterm))
|
|
125
|
+
print(
|
|
126
|
+
"Quadratic Calibration %.2f RMS, %.2f std, %.2f Aterm, %.2f Bterm "
|
|
127
|
+
% (rms, std, Aterm, Bterm)
|
|
128
|
+
)
|
|
129
|
+
if rms < last_rms:
|
|
130
|
+
last_rms = rms
|
|
131
|
+
freq_exp = (
|
|
132
|
+
Aterm
|
|
133
|
+
+ np.sqrt(np.power(-Aterm, 2) - (4 * Cterm * (mz_exp - Bterm)))
|
|
134
|
+
) / (2 * mz_exp)
|
|
135
|
+
|
|
136
|
+
mz_domain = (
|
|
137
|
+
(Aterm / (self.freq_exp_ms))
|
|
138
|
+
+ (Bterm / np.power((self.freq_exp_ms), 2))
|
|
139
|
+
+ Cterm
|
|
140
|
+
)
|
|
141
|
+
self.recal_mass_spec(mz_domain, Aterm, Bterm, Cterm)
|
|
142
|
+
if not iteration:
|
|
143
|
+
break
|
|
144
|
+
else:
|
|
145
|
+
break
|
|
146
|
+
|
|
147
|
+
def ledford_calibration(self, iteration: bool = False):
|
|
148
|
+
"""Perform Ledford calibration.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
iteration : bool, optional
|
|
153
|
+
Flag to perform iterative calibration, by default False.
|
|
154
|
+
|
|
155
|
+
"""
|
|
156
|
+
mz_calc = self.mz_calc
|
|
157
|
+
freq_exp = self.freq_exp
|
|
158
|
+
mz_exp = self.mz_exp
|
|
159
|
+
|
|
160
|
+
error = ((mz_exp - self.mz_calc) / self.mz_calc) * 1000000
|
|
161
|
+
last_rms = np.sqrt(np.mean(error**2))
|
|
162
|
+
while True:
|
|
163
|
+
matrix = np.vstack([1 / freq_exp, 1 / np.power(freq_exp, 2)]).T
|
|
164
|
+
Aterm, Bterm = np.linalg.lstsq(matrix, self.mz_calc, rcond=None)[0]
|
|
165
|
+
|
|
166
|
+
mz_exp = (Aterm / (freq_exp)) + (Bterm / np.power((freq_exp), 2))
|
|
167
|
+
error = ((mz_exp - mz_calc) / mz_calc) * 1000000
|
|
168
|
+
rms = np.sqrt(np.mean(error**2))
|
|
169
|
+
std = np.std(error)
|
|
170
|
+
if self.mass_spectrum.parameters.mass_spectrum.verbose_processing:
|
|
171
|
+
print("%.2f Aterm, %.2f Bterm" % (Aterm, Bterm))
|
|
172
|
+
print(
|
|
173
|
+
"Ledford Calibration %.2f RMS, %.2f std, %.2f Aterm, %.2f Bterm "
|
|
174
|
+
% (rms, std, Aterm, Bterm)
|
|
175
|
+
)
|
|
176
|
+
if rms < last_rms:
|
|
177
|
+
last_rms = rms
|
|
178
|
+
freq_exp = (
|
|
179
|
+
Aterm + np.sqrt(np.power(-Aterm, 2) - (4 * mz_exp - Bterm))
|
|
180
|
+
) / (2 * mz_exp)
|
|
181
|
+
mz_domain = (Aterm / (self.freq_exp_ms)) + (
|
|
182
|
+
Bterm / np.power((self.freq_exp_ms), 2)
|
|
183
|
+
)
|
|
184
|
+
self.recal_mass_spec(mz_domain, Aterm, Bterm, 0)
|
|
185
|
+
if not iteration:
|
|
186
|
+
break
|
|
187
|
+
else:
|
|
188
|
+
break
|
|
189
|
+
|
|
190
|
+
def step_fit(self, steps: int = 4):
|
|
191
|
+
"""Perform step fit calibration.
|
|
192
|
+
|
|
193
|
+
Parameters
|
|
194
|
+
----------
|
|
195
|
+
steps : int, optional
|
|
196
|
+
Number of steps for step fit calibration, by default 4.
|
|
197
|
+
|
|
198
|
+
"""
|
|
199
|
+
|
|
200
|
+
def f_to_mz(f, A, B, C, a):
|
|
201
|
+
return (A / f) + (B / np.power(f, 2)) + (C * a / np.power(f, 2))
|
|
202
|
+
|
|
203
|
+
def mz_to_f(m, A, B, C):
|
|
204
|
+
return -A - m / B
|
|
205
|
+
|
|
206
|
+
tuple_indexes = [
|
|
207
|
+
(i, i + steps) for i in range(0, len(self.selected_mspeaks) - steps, steps)
|
|
208
|
+
]
|
|
209
|
+
|
|
210
|
+
for current_index, tuple_index in enumerate(tuple_indexes):
|
|
211
|
+
mspeak_ii, mspeak_fi = tuple_index
|
|
212
|
+
freq_exp = list()
|
|
213
|
+
mz_calc = list()
|
|
214
|
+
mz_exp = list()
|
|
215
|
+
abu = list()
|
|
216
|
+
|
|
217
|
+
for i in range(mspeak_ii, mspeak_fi + 1):
|
|
218
|
+
best_formula = self.selected_mspeaks[i].best_molecular_formula_candidate
|
|
219
|
+
|
|
220
|
+
freq_exp.append(self.selected_mspeaks[i].freq_exp)
|
|
221
|
+
mz_calc.append(best_formula.mz_calc)
|
|
222
|
+
mz_exp.append(self.selected_mspeaks[i].mz_exp)
|
|
223
|
+
abu.append(self.selected_mspeaks[i].abundance)
|
|
224
|
+
|
|
225
|
+
freq_exp = np.array(freq_exp)
|
|
226
|
+
mz_calc = np.array(mz_calc)
|
|
227
|
+
mz_exp = np.array(mz_exp)
|
|
228
|
+
abu = np.array(abu)
|
|
229
|
+
|
|
230
|
+
if current_index == len(tuple_indexes) - 1:
|
|
231
|
+
ms_peaks_indexes = (self.selected_mspeaks[mspeak_ii].index, 0)
|
|
232
|
+
|
|
233
|
+
elif current_index == 0:
|
|
234
|
+
ms_peaks_indexes = (
|
|
235
|
+
len(self.mass_spectrum) - 1,
|
|
236
|
+
self.selected_mspeaks[mspeak_fi].index - 1,
|
|
237
|
+
)
|
|
238
|
+
else:
|
|
239
|
+
ms_peaks_indexes = (
|
|
240
|
+
self.selected_mspeaks[mspeak_ii].index,
|
|
241
|
+
self.selected_mspeaks[mspeak_fi].index - 1,
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
final_index, start_index = ms_peaks_indexes
|
|
245
|
+
|
|
246
|
+
matrix = np.vstack([1 / freq_exp, 1 / np.power(freq_exp, 2)]).T
|
|
247
|
+
A, B = np.linalg.lstsq(matrix, mz_calc, rcond=None)[0]
|
|
248
|
+
C = 0
|
|
249
|
+
|
|
250
|
+
for mspeak in self.mass_spectrum[start_index:final_index]:
|
|
251
|
+
mspeak.mz_cal = f_to_mz(mspeak.freq_exp, A, B, C, 0)
|
|
252
|
+
|
|
253
|
+
self.mass_spectrum.is_calibrated = True
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
class KendrickGrouping:
|
|
2
|
+
"""Class for Kendrick grouping of mass spectra.
|
|
3
|
+
|
|
4
|
+
Methods
|
|
5
|
+
-------
|
|
6
|
+
* mz_odd_even_index_lists().
|
|
7
|
+
Get odd and even indexes lists.
|
|
8
|
+
* calc_error(current, test).
|
|
9
|
+
Calculate the error between two values.
|
|
10
|
+
* populate_kendrick_index_dict_error(list_indexes, sort=True).
|
|
11
|
+
Populate the Kendrick index dictionary based on error.
|
|
12
|
+
* populate_kendrick_index_dict_rounding(list_indexes, sort=True).
|
|
13
|
+
Populate the Kendrick index dictionary based on rounding.
|
|
14
|
+
* sort_abundance_kendrick_dict(even_kendrick_group_index, odd_kendrick_group_index).
|
|
15
|
+
Sort the Kendrick index dictionary based on abundance.
|
|
16
|
+
* kendrick_groups_indexes(sort=True).
|
|
17
|
+
Get the Kendrick groups indexes dictionary.
|
|
18
|
+
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def mz_odd_even_index_lists(self):
|
|
22
|
+
"""Get odd and even indexes lists.
|
|
23
|
+
|
|
24
|
+
Returns
|
|
25
|
+
-------
|
|
26
|
+
tuple
|
|
27
|
+
A tuple containing the lists of even and odd indexes.
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
even_idx = []
|
|
31
|
+
odd_idx = []
|
|
32
|
+
|
|
33
|
+
for i, mspeak in enumerate(self.mspeaks):
|
|
34
|
+
if mspeak.nominal_mz_exp % 2 == 0:
|
|
35
|
+
even_idx.append(i)
|
|
36
|
+
else:
|
|
37
|
+
odd_idx.append(i)
|
|
38
|
+
|
|
39
|
+
return even_idx, odd_idx
|
|
40
|
+
|
|
41
|
+
def calc_error(self, current: float, test: float):
|
|
42
|
+
"""Calculate the error between two values.
|
|
43
|
+
|
|
44
|
+
Parameters
|
|
45
|
+
----------
|
|
46
|
+
current : float
|
|
47
|
+
The current value.
|
|
48
|
+
test : float
|
|
49
|
+
The test value.
|
|
50
|
+
|
|
51
|
+
Returns
|
|
52
|
+
-------
|
|
53
|
+
float
|
|
54
|
+
The calculated error.
|
|
55
|
+
|
|
56
|
+
"""
|
|
57
|
+
return ((test - current) / current) * 1e6
|
|
58
|
+
|
|
59
|
+
def populate_kendrick_index_dict_error(self, list_indexes: list, sort: bool = True):
|
|
60
|
+
"""Populate the Kendrick index dictionary based on error.
|
|
61
|
+
|
|
62
|
+
Parameters
|
|
63
|
+
----------
|
|
64
|
+
list_indexes : list
|
|
65
|
+
The list of indexes.
|
|
66
|
+
sort : bool, optional
|
|
67
|
+
Whether to sort the dictionary by abundance (default is True).
|
|
68
|
+
|
|
69
|
+
Returns
|
|
70
|
+
-------
|
|
71
|
+
dict
|
|
72
|
+
The Kendrick index dictionary.
|
|
73
|
+
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def error():
|
|
77
|
+
return abs(current_kmd_reference - next_mspeak.kmd)
|
|
78
|
+
|
|
79
|
+
already_found = []
|
|
80
|
+
|
|
81
|
+
all_results = []
|
|
82
|
+
|
|
83
|
+
for i in list_indexes:
|
|
84
|
+
result_indexes = []
|
|
85
|
+
|
|
86
|
+
mspeak = self.mspeaks[i]
|
|
87
|
+
|
|
88
|
+
current_kmd_reference = mspeak.kmd
|
|
89
|
+
|
|
90
|
+
for j in list_indexes:
|
|
91
|
+
if j not in already_found and j != i:
|
|
92
|
+
next_mspeak = self.mspeaks[j]
|
|
93
|
+
|
|
94
|
+
if error() <= 0.001:
|
|
95
|
+
result_indexes.append(j)
|
|
96
|
+
already_found.append(j)
|
|
97
|
+
|
|
98
|
+
current_kmd_reference = next_mspeak.kmd
|
|
99
|
+
|
|
100
|
+
if result_indexes and len(result_indexes) > 3:
|
|
101
|
+
already_found.append(i)
|
|
102
|
+
|
|
103
|
+
result_indexes.insert(0, i)
|
|
104
|
+
|
|
105
|
+
all_results.append(result_indexes)
|
|
106
|
+
else:
|
|
107
|
+
for w in result_indexes:
|
|
108
|
+
already_found.remove(w)
|
|
109
|
+
|
|
110
|
+
kendrick_group_index = {
|
|
111
|
+
i: indexes_list for i, indexes_list in enumerate(all_results)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
# return dictionary with the keys sorted by sum of the abundances
|
|
115
|
+
if sort:
|
|
116
|
+
return dict(
|
|
117
|
+
sorted(
|
|
118
|
+
kendrick_group_index.items(),
|
|
119
|
+
key=lambda it: sum([self.mspeaks[i].abundance for i in it[1]]),
|
|
120
|
+
reverse=False,
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
else:
|
|
125
|
+
return kendrick_group_index
|
|
126
|
+
|
|
127
|
+
def populate_kendrick_index_dict_rounding(
|
|
128
|
+
self, list_indexes: list, sort: bool = True
|
|
129
|
+
):
|
|
130
|
+
"""Populate the Kendrick index dictionary based on rounding.
|
|
131
|
+
|
|
132
|
+
Parameters
|
|
133
|
+
----------
|
|
134
|
+
list_indexes : list
|
|
135
|
+
The list of indexes.
|
|
136
|
+
sort : bool, optional
|
|
137
|
+
Whether to sort the dictionary by abundance (default is True).
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
dict
|
|
142
|
+
The Kendrick index dictionary.
|
|
143
|
+
|
|
144
|
+
"""
|
|
145
|
+
kendrick_group_index = {}
|
|
146
|
+
|
|
147
|
+
for i in list_indexes:
|
|
148
|
+
mspeak = self.mspeaks[i]
|
|
149
|
+
|
|
150
|
+
group = round(mspeak.kmd * 100)
|
|
151
|
+
|
|
152
|
+
if group not in kendrick_group_index:
|
|
153
|
+
kendrick_group_index[group] = [i]
|
|
154
|
+
|
|
155
|
+
else:
|
|
156
|
+
last_index = kendrick_group_index[group][-1]
|
|
157
|
+
|
|
158
|
+
if self.parameters.mass_spectrum.verbose_processing:
|
|
159
|
+
print(abs(mspeak.kmd - self.mspeaks[last_index].kmd))
|
|
160
|
+
|
|
161
|
+
if abs(mspeak.kmd - self.mspeaks[last_index].kmd) < 0.001:
|
|
162
|
+
kendrick_group_index[group].append(i)
|
|
163
|
+
|
|
164
|
+
# return dictionary with the keys sorted by sum of the abundances
|
|
165
|
+
if sort:
|
|
166
|
+
return dict(
|
|
167
|
+
sorted(
|
|
168
|
+
kendrick_group_index.items(),
|
|
169
|
+
key=lambda it: sum([self.mspeaks[i].abundance for i in it[1]]),
|
|
170
|
+
reverse=True,
|
|
171
|
+
)
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
else:
|
|
175
|
+
return kendrick_group_index
|
|
176
|
+
|
|
177
|
+
def sort_abundance_kendrick_dict(
|
|
178
|
+
self, even_kendrick_group_index: dict, odd_kendrick_group_index: dict
|
|
179
|
+
):
|
|
180
|
+
"""Sort the Kendrick index dictionary based on abundance.
|
|
181
|
+
|
|
182
|
+
Parameters
|
|
183
|
+
----------
|
|
184
|
+
even_kendrick_group_index : dict
|
|
185
|
+
The Kendrick index dictionary for even indexes.
|
|
186
|
+
odd_kendrick_group_index : dict
|
|
187
|
+
The Kendrick index dictionary for odd indexes.
|
|
188
|
+
|
|
189
|
+
Returns
|
|
190
|
+
-------
|
|
191
|
+
dict
|
|
192
|
+
The sorted Kendrick index dictionary.
|
|
193
|
+
|
|
194
|
+
"""
|
|
195
|
+
all_even_indexes = [i for v in even_kendrick_group_index.values() for i in v]
|
|
196
|
+
|
|
197
|
+
all_odd_indexes = [i for v in odd_kendrick_group_index.values() for i in v]
|
|
198
|
+
|
|
199
|
+
sum_even = sum([self.mspeaks[i].abundance for i in all_even_indexes])
|
|
200
|
+
|
|
201
|
+
sum_odd = sum([self.mspeaks[i].abundance for i in all_odd_indexes])
|
|
202
|
+
|
|
203
|
+
if sum_even >= sum_odd:
|
|
204
|
+
even_kendrick_group_index.update(odd_kendrick_group_index)
|
|
205
|
+
|
|
206
|
+
return even_kendrick_group_index
|
|
207
|
+
|
|
208
|
+
else:
|
|
209
|
+
odd_kendrick_group_index.update(even_kendrick_group_index)
|
|
210
|
+
|
|
211
|
+
return odd_kendrick_group_index
|
|
212
|
+
|
|
213
|
+
def kendrick_groups_indexes(self, sort: bool = True):
|
|
214
|
+
"""Get the Kendrick groups indexes dictionary.
|
|
215
|
+
|
|
216
|
+
Parameters
|
|
217
|
+
----------
|
|
218
|
+
sort : bool, optional
|
|
219
|
+
Whether to sort the dictionary by abundance (default is True).
|
|
220
|
+
|
|
221
|
+
Returns
|
|
222
|
+
-------
|
|
223
|
+
dict
|
|
224
|
+
The Kendrick groups indexes dictionary.
|
|
225
|
+
|
|
226
|
+
"""
|
|
227
|
+
even_idx, odd_idx = self.mz_odd_even_index_lists()
|
|
228
|
+
|
|
229
|
+
even_kendrick_group_index = self.populate_kendrick_index_dict_error(
|
|
230
|
+
even_idx, sort=sort
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
odd_kendrick_group_index = self.populate_kendrick_index_dict_error(
|
|
234
|
+
odd_idx, sort=sort
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
return self.sort_abundance_kendrick_dict(
|
|
238
|
+
even_kendrick_group_index, odd_kendrick_group_index
|
|
239
|
+
)
|