masscube 1.2.3__tar.gz → 1.2.4__tar.gz

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.
Files changed (27) hide show
  1. {masscube-1.2.3 → masscube-1.2.4}/PKG-INFO +2 -13
  2. {masscube-1.2.3 → masscube-1.2.4}/pyproject.toml +2 -2
  3. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/feature_detection.py +19 -12
  4. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/params.py +3 -3
  5. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/raw_data_utils.py +72 -80
  6. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/workflows.py +4 -1
  7. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/PKG-INFO +2 -13
  8. {masscube-1.2.3 → masscube-1.2.4}/LICENSE +0 -0
  9. {masscube-1.2.3 → masscube-1.2.4}/README.md +0 -0
  10. {masscube-1.2.3 → masscube-1.2.4}/setup.cfg +0 -0
  11. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/__init__.py +0 -0
  12. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/alignment.py +0 -0
  13. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/annotation.py +0 -0
  14. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/classifier_builder.py +0 -0
  15. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/feature_evaluation.py +0 -0
  16. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/feature_grouping.py +0 -0
  17. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/mzpkl.py +0 -0
  18. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/network.py +0 -0
  19. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/normalization.py +0 -0
  20. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/stats.py +0 -0
  21. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/utils_functions.py +0 -0
  22. {masscube-1.2.3 → masscube-1.2.4}/src/masscube/visualization.py +0 -0
  23. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/SOURCES.txt +0 -0
  24. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/dependency_links.txt +0 -0
  25. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/entry_points.txt +0 -0
  26. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/requires.txt +0 -0
  27. {masscube-1.2.3 → masscube-1.2.4}/src/masscube.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.1
2
2
  Name: masscube
3
- Version: 1.2.3
3
+ Version: 1.2.4
4
4
  Summary: Accurate and fast data processing for metabolomics
5
5
  Author-email: Huaxu Yu <yhxchem@outlook.com>
6
6
  Maintainer-email: Huaxu Yu <yhxchem@outlook.com>
@@ -12,17 +12,6 @@ Classifier: Operating System :: OS Independent
12
12
  Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
- Requires-Dist: numpy>=1.24
16
- Requires-Dist: pandas>=2.2.1
17
- Requires-Dist: pyteomics==4.6.3
18
- Requires-Dist: scipy>=1.10.1
19
- Requires-Dist: tqdm>=4.66.1
20
- Requires-Dist: lxml>=4.9.3
21
- Requires-Dist: matplotlib>=3.8.2
22
- Requires-Dist: ms_entropy==1.3.4
23
- Requires-Dist: scikit-learn>=1.3.2
24
- Requires-Dist: statsmodels>=0.14.2
25
- Requires-Dist: umap-learn>=0.5.7
26
15
 
27
16
  # MassCube
28
17
 
@@ -1,10 +1,10 @@
1
1
  [build-system]
2
- requires = ["setuptools>=61.0"]
2
+ requires = ["setuptools<66", "wheel"]
3
3
  build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "masscube"
7
- version = "1.2.3"
7
+ version = "1.2.4"
8
8
  authors = [
9
9
  { name="Huaxu Yu", email="yhxchem@outlook.com" },
10
10
  ]
@@ -266,8 +266,8 @@ def detect_features(d):
266
266
  return final_features
267
267
 
268
268
 
269
- def segment_feature(feature, sigma=1.2, prominence_ratio=0.05, distance=10, peak_height_tol=1000,
270
- length_tol=5, sse_tol=0.6):
269
+ def segment_feature(feature, sigma=1.2, prominence_ratio=0.05, distance=5, peak_height_tol=1000,
270
+ length_tol=5, sse_tol=0.3):
271
271
  """
272
272
  Function to segment a feature into multiple features based on the edge detection.
273
273
 
@@ -293,14 +293,10 @@ def segment_feature(feature, sigma=1.2, prominence_ratio=0.05, distance=10, peak
293
293
  """
294
294
 
295
295
  # if peak height is too low or the length is too short, skip segmentation
296
- if feature.peak_height < peak_height_tol or feature.length < length_tol:
297
- return [feature]
298
-
299
- # correction for peak with limited scan number
300
296
  peak_tmp = feature.signals[:,1]
301
- if np.sum(peak_tmp > peak_height_tol) < 12:
302
- sigma = 0.5*sigma
303
- distance = 3
297
+ dp = np.sum(peak_tmp > peak_height_tol)
298
+ if feature.peak_height < peak_height_tol or dp < length_tol:
299
+ return [feature]
304
300
 
305
301
  # add zero to the front and the end of the signal to facilitate the edge detection
306
302
  peak_tmp = np.concatenate(([0], peak_tmp, [0]))
@@ -308,11 +304,22 @@ def segment_feature(feature, sigma=1.2, prominence_ratio=0.05, distance=10, peak
308
304
  feature.sse = squared_error_to_smoothed_curve(original_signal=peak_tmp, fit_signal=ss)
309
305
  if feature.sse > sse_tol:
310
306
  return [feature]
311
- peaks, _ = find_peaks(ss, prominence=np.max(ss)*prominence_ratio, distance=distance)
307
+
308
+ # correction of prominence ratio and sigma based on noise level and data points
309
+ prominence_ratio = np.clip(0.03, prominence_ratio * feature.sse * 20, 0.1)
310
+ prominence = np.max(ss)*prominence_ratio
311
+ sigma = np.clip(0.5, sigma * dp / 33, 1.2)
312
+ ss = gaussian_filter1d(peak_tmp, sigma=sigma) # recalculate the smoothed signal
313
+
314
+ peaks, _ = find_peaks(ss, prominence=prominence, distance=distance)
315
+
316
+ # baseline filter
312
317
  peaks = peaks - 1 # correct the index
318
+
319
+ baseline = np.median(peak_tmp)
313
320
 
314
- # the resulting peaks should have a height larger than peak_height_tol
315
- peaks = peaks[feature.signals[peaks,1] > peak_height_tol]
321
+ # the resulting peaks should have a height larger than baseline
322
+ peaks = peaks[feature.signals[peaks,1] > baseline]
316
323
 
317
324
  if len(peaks) < 2:
318
325
  return [feature]
@@ -61,7 +61,7 @@ class Params:
61
61
  # feature detection
62
62
  self.mz_tol_ms1 = 0.01 # m/z tolerance for MS1, default is 0.01
63
63
  self.mz_tol_ms2 = 0.015 # m/z tolerance for MS2, default is 0.015
64
- self.feature_gap_tol = 30 # gap tolerance within a feature, default is 30 (i.e. 30 consecutive scans without signal), integer
64
+ self.feature_gap_tol = 10 # gap tolerance within a feature, default is 10 (i.e. 10 consecutive scans without signal), integer
65
65
  self.batch_size = 100 # batch size for parallel processing, default is 100, integer
66
66
  self.percent_cpu_to_use = 0.8 # percentage of CPU to use, default is 0.8, float
67
67
 
@@ -80,8 +80,8 @@ class Params:
80
80
  self.scan_number_cutoff = 5 # feature with non-zero scan number greater than the cutoff will be aligned, default is 5
81
81
  self.detection_rate_cutoff = 0.1 # features detected need to be >rate*(qc+sample), default rate is 0.1
82
82
  self.merge_features = True # whether to merge features with almost the same m/z and RT, default is True
83
- self.mz_tol_merge_features = 0.012 # m/z tolerance for merging features, default is 0.012
84
- self.rt_tol_merge_features = 0.05 # RT tolerance for merging features, default is 0.05
83
+ self.mz_tol_merge_features = 0.01 # m/z tolerance for merging features, default is 0.01
84
+ self.rt_tol_merge_features = 0.02 # RT tolerance for merging features, default is 0.02
85
85
  self.group_features_after_alignment = True # whether to group features after alignment, default is False
86
86
  self.fill_gaps = True # whether to fill the gaps in the aligned features, default is True
87
87
  self.gap_filling_method = "local_maximum" # method for gap filling, default is " local_maximum", string
@@ -142,53 +142,38 @@ class MSData:
142
142
  time_unit = scans[0]['scanList']['scan'][0]['scan start time'].unit_info
143
143
 
144
144
  for idx, spec in enumerate(scans):
145
- # get level of mass spectrum
146
- level = spec['ms level']
147
- if level not in self.params.scan_levels:
148
- continue
149
-
150
- # convert scan time to minute
151
- try:
145
+
146
+ # get time
147
+ if "scan start time" in spec['scanList']['scan'][0]:
152
148
  scan_time = spec['scanList']['scan'][0]['scan start time']
153
- except:
149
+ elif "scan time" in spec['scanList']['scan'][0]:
154
150
  scan_time = spec['scanList']['scan'][0]['scan time'] # not a standard format
151
+
155
152
  if time_unit == 'second':
156
- scan_time /= 60
157
- # skip scans outside the defined retention time range
158
- if scan_time < self.params.rt_lower_limit or scan_time > self.params.rt_upper_limit:
153
+ scan_time /= 60 # convert to minute
154
+
155
+ # get level of mass spectrum
156
+ level = spec['ms level']
157
+
158
+ # skip scans not in the defined scan levels or outside the defined retention time range
159
+ if (level not in self.params.scan_levels) or (scan_time < self.params.rt_lower_limit) or (scan_time > self.params.rt_upper_limit):
160
+ self.scans.append(Scan(level=level, id=idx, scan_time=scan_time, signals=None, precursor_mz=None))
159
161
  continue
160
162
 
161
163
  signals = np.array([spec['m/z array'], spec['intensity array']], dtype=np.float32).T
162
-
163
- # process signals based on parameters and scan level
164
- if level == 1:
165
- signals = clean_signals(signals, intensity_range=[self.params.ms1_abs_int_tol, np.inf])
166
- if self.params.centroid_mz_tol is not None:
167
- signals = centroid_signals(signals, mz_tol=self.params.centroid_mz_tol)
168
- precursor_mz = None
169
- self.ms1_idx.append(idx)
170
- self.ms1_time_arr.append(scan_time)
171
- if len(signals) > 0:
172
- self.base_peak_arr.append(signals[np.argmax(signals[:, 1])])
173
- else:
174
- self.base_peak_arr.append([0, 0])
175
- else:
164
+
165
+ if level == 2:
176
166
  precursor_mz = spec['precursorList']['precursor'][0]['selectedIonList']['selectedIon'][0]['selected ion m/z']
177
- if len(signals) == 0:
178
- int_upper = self.params.ms2_abs_int_tol
179
- else:
180
- int_upper = max(self.params.ms2_abs_int_tol, np.max(signals[:, 1]) * self.params.ms2_rel_int_tol)
181
- signals = clean_signals(signals, mz_range=[0, precursor_mz - self.params.precursor_mz_offset],
182
- intensity_range=[int_upper, np.inf])
183
- if self.params.centroid_mz_tol is not None:
184
- signals = centroid_signals(signals, mz_tol=self.params.centroid_mz_tol)
185
- self.ms2_idx.append(idx)
167
+ else:
168
+ precursor_mz = None
186
169
 
187
- self.scans.append(Scan(level=level, id=idx, scan_time=scan_time, signals=signals,
188
- precursor_mz=precursor_mz))
189
-
190
- self.ms1_time_arr = np.array(self.ms1_time_arr)
191
- self.base_peak_arr = np.array(self.base_peak_arr)
170
+ self.scans.append(_preprocess_signals_to_scan(level=level, id=idx, scan_time=scan_time, signals=signals,
171
+ params=self.params, precursor_mz=precursor_mz))
172
+
173
+ self.ms1_idx = np.array([s.id for s in self.scans if s.level == 1 and s.signals is not None and len(s.signals) > 0])
174
+ self.ms2_idx = np.array([s.id for s in self.scans if s.level == 2 and s.signals is not None and len(s.signals) > 0])
175
+ self.ms1_time_arr = np.array([self.scans[i].time for i in self.ms1_idx])
176
+ self.base_peak_arr = np.array([self.scans[i].signals[np.argmax(self.scans[i].signals[:, 1])] for i in self.ms1_idx])
192
177
 
193
178
 
194
179
  def extract_scan_mzxml(self, scans):
@@ -207,50 +192,35 @@ class MSData:
207
192
  time_unit = scans[0]["retentionTime"].unit_info
208
193
 
209
194
  for idx, spec in enumerate(scans):
210
- # get level of mass spectrum
211
- level = spec['msLevel']
212
- if level not in self.params.scan_levels:
213
- continue
214
195
 
215
- # convert scan time to minute
196
+ # get time
216
197
  scan_time = spec["retentionTime"]
198
+
217
199
  if time_unit == 'second':
218
- scan_time = scan_time / 60
219
- # skip scans outside the defined retention time range
220
- if scan_time < self.params.rt_lower_limit or scan_time > self.params.rt_upper_limit:
200
+ scan_time = scan_time / 60 # convert to minute
201
+
202
+ # get level of mass spectrum
203
+ level = spec['msLevel']
204
+
205
+ # skip scans not in the defined scan levels or outside the defined retention time range
206
+ if (level not in self.params.scan_levels) or (scan_time < self.params.rt_lower_limit) or (scan_time > self.params.rt_upper_limit):
207
+ self.scans.append(Scan(level=level, id=idx, scan_time=scan_time, signals=None, precursor_mz=None))
221
208
  continue
222
209
 
223
210
  signals = np.array([spec['m/z array'], spec['intensity array']], dtype=np.float32).T
224
211
 
225
- # process signals based on parameters and scan level
226
- if level == 1:
227
- signals = clean_signals(signals, intensity_range=[self.params.ms1_abs_int_tol, np.inf])
228
- if self.params.centroid_mz_tol is not None:
229
- signals = centroid_signals(signals, mz_tol=self.params.centroid_mz_tol)
230
- precursor_mz = None
231
- self.ms1_idx.append(idx)
232
- self.ms1_time_arr.append(scan_time)
233
- if len(signals) > 0:
234
- self.base_peak_arr.append(signals[np.argmax(signals[:, 1])])
235
- else:
236
- self.base_peak_arr.append([0, 0])
237
- else:
212
+ if level == 2:
238
213
  precursor_mz = spec['precursorMz'][0]['precursorMz']
239
- if len(signals) == 0:
240
- int_upper = self.params.ms2_abs_int_tol
241
- else:
242
- int_upper = max(self.params.ms2_abs_int_tol, np.max(signals[:, 1]) * self.params.ms2_rel_int_tol)
243
- signals = clean_signals(signals, mz_range=[0, precursor_mz - self.params.precursor_mz_offset],
244
- intensity_range=[int_upper, np.inf])
245
- if self.params.centroid_mz_tol is not None:
246
- signals = centroid_signals(signals, mz_tol=self.params.centroid_mz_tol)
247
- self.ms2_idx.append(idx)
248
-
249
- self.scans.append(Scan(level=level, id=idx, scan_time=scan_time, signals=signals,
250
- precursor_mz=precursor_mz))
214
+ else:
215
+ precursor_mz = None
216
+
217
+ self.scans.append(_preprocess_signals_to_scan(level=level, id=idx, scan_time=scan_time, signals=signals,
218
+ params=self.params, precursor_mz=precursor_mz))
251
219
 
252
- self.ms1_time_arr = np.array(self.ms1_time_arr)
253
- self.base_peak_arr = np.array(self.base_peak_arr)
220
+ self.ms1_idx = np.array([s.id for s in self.scans if s.level == 1 and s.signals is not None and len(s.signals) > 0])
221
+ self.ms2_idx = np.array([s.id for s in self.scans if s.level == 2 and s.signals is not None and len(s.signals) > 0])
222
+ self.ms1_time_arr = np.array([self.scans[i].time for i in self.ms1_idx])
223
+ self.base_peak_arr = np.array([self.scans[i].signals[np.argmax(self.scans[i].signals[:, 1])] for i in self.ms1_idx])
254
224
 
255
225
 
256
226
  def drop_ms1_ions_by_intensity(self, int_tol):
@@ -287,13 +257,10 @@ class MSData:
287
257
  Parameters
288
258
  ----------
289
259
  iteration: int
290
- Number of iterations to segment features.
260
+ Number of iterations to segment features. Increase this number may introduce more false positives.
291
261
  """
292
262
 
293
- distance = 0.05/np.mean(np.diff(self.ms1_time_arr))
294
-
295
- if distance < 1:
296
- distance = 1
263
+ distance = np.clip(0.05 / np.mean(np.diff(self.ms1_time_arr)), 1, 5)
297
264
 
298
265
  for _ in range(iteration):
299
266
  self.features = [segment_feature(feature, peak_height_tol=self.params.ms1_abs_int_tol*3, distance=distance) for feature in self.features]
@@ -659,6 +626,9 @@ class MSData:
659
626
  Retention time tolerance.
660
627
  """
661
628
 
629
+ self.feature_mz_arr = np.array([feature.mz for feature in self.features])
630
+ self.feature_rt_arr = np.array([feature.rt for feature in self.features])
631
+
662
632
  if rt_target is None:
663
633
  tmp = np.abs(self.feature_mz_arr - mz_target) < mz_tol
664
634
  found_feature = [self.features[i] for i in np.where(tmp)[0]]
@@ -1015,4 +985,26 @@ def find_best_ms2(ms2_list):
1015
985
  else:
1016
986
  return ms2_list[max(range(len(total_ints)), key=total_ints.__getitem__)]
1017
987
  else:
1018
- return None
988
+ return None
989
+
990
+
991
+ def _preprocess_signals_to_scan(level, id, scan_time, signals, params, precursor_mz=None):
992
+ """
993
+ Function to generate a Scan object from signals.
994
+ """
995
+
996
+ if len(signals) == 0:
997
+ return Scan(level=level, id=id, scan_time=scan_time, signals=signals, precursor_mz=precursor_mz)
998
+
999
+ if level == 1:
1000
+ signals = clean_signals(signals, intensity_range=[params.ms1_abs_int_tol, np.inf])
1001
+
1002
+ elif level == 2:
1003
+ int_lower = max(params.ms2_abs_int_tol, np.max(signals[:, 1]) * params.ms2_rel_int_tol)
1004
+ signals = clean_signals(signals, mz_range=[0, precursor_mz - params.precursor_mz_offset],
1005
+ intensity_range=[int_lower, np.inf])
1006
+
1007
+ if params.centroid_mz_tol is not None:
1008
+ signals = centroid_signals(signals, mz_tol=params.centroid_mz_tol)
1009
+
1010
+ return Scan(level=level, id=id, scan_time=scan_time, signals=signals, precursor_mz=precursor_mz)
@@ -26,7 +26,7 @@ from .utils_functions import convert_signals_to_string
26
26
 
27
27
  # 1. Untargeted feature detection for a single file
28
28
  def process_single_file(file_name, params=None, segment_feature=True, group_features=False, evaluate_peak_shape=True,
29
- annotate_ms2=False, ms2_library_path=None, output_dir=None):
29
+ annotate_ms2=False, ms2_library_path=None, remove_noise=False, output_dir=None):
30
30
  """
31
31
  Untargeted data processing for a single file (mzML, mzXML, mzjson or compressed mzjson).
32
32
 
@@ -77,6 +77,9 @@ def process_single_file(file_name, params=None, segment_feature=True, group_feat
77
77
  d.summarize_features(cal_g_score=True, cal_a_score=True)
78
78
  else:
79
79
  d.summarize_features(cal_g_score=False, cal_a_score=False)
80
+
81
+ if remove_noise:
82
+ d.remove_noise()
80
83
 
81
84
  # STEP 4. MS2 annotation
82
85
  if annotate_ms2:
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.1
2
2
  Name: masscube
3
- Version: 1.2.3
3
+ Version: 1.2.4
4
4
  Summary: Accurate and fast data processing for metabolomics
5
5
  Author-email: Huaxu Yu <yhxchem@outlook.com>
6
6
  Maintainer-email: Huaxu Yu <yhxchem@outlook.com>
@@ -12,17 +12,6 @@ Classifier: Operating System :: OS Independent
12
12
  Requires-Python: >=3.8
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
- Requires-Dist: numpy>=1.24
16
- Requires-Dist: pandas>=2.2.1
17
- Requires-Dist: pyteomics==4.6.3
18
- Requires-Dist: scipy>=1.10.1
19
- Requires-Dist: tqdm>=4.66.1
20
- Requires-Dist: lxml>=4.9.3
21
- Requires-Dist: matplotlib>=3.8.2
22
- Requires-Dist: ms_entropy==1.3.4
23
- Requires-Dist: scikit-learn>=1.3.2
24
- Requires-Dist: statsmodels>=0.14.2
25
- Requires-Dist: umap-learn>=0.5.7
26
15
 
27
16
  # MassCube
28
17
 
File without changes
File without changes
File without changes
File without changes
File without changes