masscube 1.2.6__tar.gz → 1.2.8__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.
- {masscube-1.2.6 → masscube-1.2.8}/PKG-INFO +1 -1
- {masscube-1.2.6 → masscube-1.2.8}/pyproject.toml +1 -1
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/alignment.py +5 -2
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/annotation.py +52 -14
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/normalization.py +8 -4
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/stats.py +2 -1
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/visualization.py +3 -3
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/workflows.py +1 -2
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/PKG-INFO +1 -1
- {masscube-1.2.6 → masscube-1.2.8}/LICENSE +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/README.md +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/setup.cfg +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/__init__.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/classifier_builder.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/feature_detection.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/feature_evaluation.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/feature_grouping.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/mzpkl.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/network.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/params.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/raw_data_utils.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube/utils_functions.py +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/SOURCES.txt +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/dependency_links.txt +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/entry_points.txt +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/requires.txt +0 -0
- {masscube-1.2.6 → masscube-1.2.8}/src/masscube.egg-info/top_level.txt +0 -0
|
@@ -412,13 +412,16 @@ def output_feature_to_msp(feature_table, output_path):
|
|
|
412
412
|
f.write("\n")
|
|
413
413
|
continue
|
|
414
414
|
|
|
415
|
-
if feature_table['annotation'][i] is None:
|
|
415
|
+
if feature_table['annotation'][i] is None or feature_table['annotation'][i] != feature_table['annotation'][i]:
|
|
416
416
|
name = "Unknown"
|
|
417
417
|
else:
|
|
418
418
|
name = str(feature_table['annotation'][i])
|
|
419
419
|
|
|
420
420
|
peaks = re.findall(r"\d+\.\d+", feature_table['MS2'][i])
|
|
421
|
-
|
|
421
|
+
try:
|
|
422
|
+
f.write("NAME: " + name + "\n")
|
|
423
|
+
except:
|
|
424
|
+
f.write("NAME: Unknown\n")
|
|
422
425
|
f.write("PRECURSORMZ: " + str(feature_table['m/z'][i]) + "\n")
|
|
423
426
|
f.write("PRECURSORTYPE: " + str(feature_table['adduct'][i]) + "\n")
|
|
424
427
|
f.write("RETENTIONTIME: " + str(feature_table['RT'][i]) + "\n")
|
|
@@ -219,8 +219,15 @@ def annotate_aligned_features(features, params, num=5):
|
|
|
219
219
|
entropy_search = load_ms2_db(params.ms2_library_path)
|
|
220
220
|
|
|
221
221
|
# ion mode filtering
|
|
222
|
-
ion_mode_mask =
|
|
223
|
-
|
|
222
|
+
ion_mode_mask = []
|
|
223
|
+
for i in range(len(entropy_search.precursor_mz_array)):
|
|
224
|
+
if 'ion_mode' in entropy_search[i]:
|
|
225
|
+
ion_mode_mask.append(1 if (entropy_search[i]['ion_mode'].lower() == params.ion_mode) else 0)
|
|
226
|
+
elif 'ionmode' in entropy_search[i]:
|
|
227
|
+
ion_mode_mask.append(1 if (entropy_search[i]['ionmode'].lower() == params.ion_mode) else 0)
|
|
228
|
+
else:
|
|
229
|
+
ion_mode_mask.append(0)
|
|
230
|
+
ion_mode_mask = np.array(ion_mode_mask, dtype=bool)
|
|
224
231
|
|
|
225
232
|
if params.consider_rt:
|
|
226
233
|
rt_arr = np.zeros(len(entropy_search.precursor_mz_array))+np.inf
|
|
@@ -326,20 +333,27 @@ def annotate_features(d, sim_tol=None, fuzzy_search=True, ms2_library_path=None,
|
|
|
326
333
|
"""
|
|
327
334
|
|
|
328
335
|
if ms2_library_path is None:
|
|
329
|
-
|
|
336
|
+
entropy_search = load_ms2_db(d.params.ms2_library_path)
|
|
330
337
|
else:
|
|
331
|
-
|
|
338
|
+
entropy_search = load_ms2_db(ms2_library_path)
|
|
332
339
|
|
|
333
340
|
# ion mode filtering
|
|
334
|
-
ion_mode_mask =
|
|
335
|
-
|
|
341
|
+
ion_mode_mask = []
|
|
342
|
+
for i in range(len(entropy_search.precursor_mz_array)):
|
|
343
|
+
if 'ion_mode' in entropy_search[i]:
|
|
344
|
+
ion_mode_mask.append(1 if (entropy_search[i]['ion_mode'].lower() == d.params.ion_mode) else 0)
|
|
345
|
+
elif 'ionmode' in entropy_search[i]:
|
|
346
|
+
ion_mode_mask.append(1 if (entropy_search[i]['ionmode'].lower() == d.params.ion_mode) else 0)
|
|
347
|
+
else:
|
|
348
|
+
ion_mode_mask.append(0)
|
|
349
|
+
ion_mode_mask = np.array(ion_mode_mask, dtype=bool)
|
|
336
350
|
|
|
337
351
|
if sim_tol is None:
|
|
338
352
|
sim_tol = d.params.ms2_sim_tol
|
|
339
353
|
|
|
340
354
|
if consider_rt:
|
|
341
|
-
rt_arr = np.zeros(len(
|
|
342
|
-
for i, ms2 in enumerate(
|
|
355
|
+
rt_arr = np.zeros(len(entropy_search.precursor_mz_array))+np.inf
|
|
356
|
+
for i, ms2 in enumerate(entropy_search):
|
|
343
357
|
if 'retention_time' in ms2:
|
|
344
358
|
rt_arr[i] = ms2['retention_time']
|
|
345
359
|
|
|
@@ -350,10 +364,10 @@ def annotate_features(d, sim_tol=None, fuzzy_search=True, ms2_library_path=None,
|
|
|
350
364
|
|
|
351
365
|
matched = None
|
|
352
366
|
matched_peak_num = None
|
|
353
|
-
signals =
|
|
367
|
+
signals = entropy_search.clean_spectrum_for_search(precursor_mz=f.mz, peaks=f.ms2.signals, precursor_ions_removal_da=2.0)
|
|
354
368
|
if len(signals) == 0:
|
|
355
369
|
continue
|
|
356
|
-
scores, peak_nums =
|
|
370
|
+
scores, peak_nums = entropy_search.identity_search(precursor_mz=f.mz, peaks=signals, ms1_tolerance_in_da=d.params.mz_tol_ms1,
|
|
357
371
|
ms2_tolerance_in_da=d.params.mz_tol_ms2, output_matched_peak_number=True)
|
|
358
372
|
scores = ion_mode_mask
|
|
359
373
|
if consider_rt:
|
|
@@ -361,7 +375,7 @@ def annotate_features(d, sim_tol=None, fuzzy_search=True, ms2_library_path=None,
|
|
|
361
375
|
scores_rt = scores * rt_boo
|
|
362
376
|
idx = np.argmax(scores_rt)
|
|
363
377
|
if scores_rt[idx] > sim_tol:
|
|
364
|
-
matched =
|
|
378
|
+
matched = entropy_search[idx]
|
|
365
379
|
matched = {k.lower():v for k,v in matched.items()}
|
|
366
380
|
matched_peak_num = peak_nums[idx]
|
|
367
381
|
_assign_annotation_results_to_feature(f, score=scores_rt[idx], matched=matched, matched_peak_num=matched_peak_num,
|
|
@@ -370,18 +384,18 @@ def annotate_features(d, sim_tol=None, fuzzy_search=True, ms2_library_path=None,
|
|
|
370
384
|
if matched is None:
|
|
371
385
|
idx = np.argmax(scores)
|
|
372
386
|
if scores[idx] > sim_tol:
|
|
373
|
-
matched =
|
|
387
|
+
matched = entropy_search[idx]
|
|
374
388
|
matched = {k.lower():v for k,v in matched.items()}
|
|
375
389
|
matched_peak_num = peak_nums[idx]
|
|
376
390
|
_assign_annotation_results_to_feature(f, score=scores[idx], matched=matched, matched_peak_num=matched_peak_num,
|
|
377
391
|
search_mode='identity_search')
|
|
378
392
|
|
|
379
393
|
if matched is None and fuzzy_search:
|
|
380
|
-
scores =
|
|
394
|
+
scores = entropy_search.hybrid_search(precursor_mz=f.mz, peaks=signals, ms1_tolerance_in_da=d.params.mz_tol_ms1,
|
|
381
395
|
ms2_tolerance_in_da=d.params.mz_tol_ms2)
|
|
382
396
|
idx = np.argmax(scores)
|
|
383
397
|
if scores[idx] > sim_tol:
|
|
384
|
-
matched =
|
|
398
|
+
matched = entropy_search[idx]
|
|
385
399
|
matched_peak_num = None
|
|
386
400
|
_assign_annotation_results_to_feature(f, score=scores[idx], matched=matched, matched_peak_num=matched_peak_num,
|
|
387
401
|
search_mode='fuzzy_search')
|
|
@@ -579,6 +593,30 @@ def index_msp_to_pkl(msp_path, output_path=None):
|
|
|
579
593
|
pickle.dump(entropy_search, open(os.path.join(output_path, file_name + ".pkl"), 'wb'))
|
|
580
594
|
|
|
581
595
|
|
|
596
|
+
def index_json_to_pkl(json_path, output_path=None):
|
|
597
|
+
"""
|
|
598
|
+
A function to index JSON file to pickle format.
|
|
599
|
+
|
|
600
|
+
Parameters
|
|
601
|
+
----------
|
|
602
|
+
json_path : str
|
|
603
|
+
The path to the JSON file.
|
|
604
|
+
output_path : str
|
|
605
|
+
The path to the output pickle file.
|
|
606
|
+
"""
|
|
607
|
+
|
|
608
|
+
file_name = os.path.basename(json_path).split(".")[0]
|
|
609
|
+
|
|
610
|
+
if output_path is None:
|
|
611
|
+
output_path = os.path.dirname(json_path)
|
|
612
|
+
|
|
613
|
+
db = json.load(open(json_path, 'r'))
|
|
614
|
+
entropy_search = FlashEntropySearch()
|
|
615
|
+
entropy_search.build_index(db)
|
|
616
|
+
|
|
617
|
+
pickle.dump(entropy_search, open(os.path.join(output_path, file_name + ".pkl"), 'wb'))
|
|
618
|
+
|
|
619
|
+
|
|
582
620
|
def _correct_db(db):
|
|
583
621
|
"""
|
|
584
622
|
Correct the MS2 database by changing the key names.
|
|
@@ -331,6 +331,9 @@ def lowess_normalization(array, is_qc, it=3, batch_ids=None, frac=None):
|
|
|
331
331
|
|
|
332
332
|
# only use qc data > 0 for normalization
|
|
333
333
|
valid_idx = is_qc & (array > 0)
|
|
334
|
+
|
|
335
|
+
if np.sum(valid_idx) < 3:
|
|
336
|
+
return {'model': None, 'fit_curve': None, 'normed_arr': array}
|
|
334
337
|
|
|
335
338
|
if batch_ids is None:
|
|
336
339
|
batch_ids = np.zeros(len(array), dtype=int)
|
|
@@ -372,9 +375,10 @@ def lowess_normalization(array, is_qc, it=3, batch_ids=None, frac=None):
|
|
|
372
375
|
tmp_normed = int_arr_corr[is_qc]
|
|
373
376
|
tmp = tmp[tmp > 0]
|
|
374
377
|
tmp_normed = tmp_normed[tmp_normed > 0]
|
|
375
|
-
if (len(tmp) == 0) or (len(tmp_normed) == 0)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
378
|
+
if (len(tmp) == 0) or (len(tmp_normed) == 0):
|
|
379
|
+
if np.std(tmp_normed) / np.mean(tmp_normed) > np.std(tmp) / np.mean(tmp):
|
|
380
|
+
int_arr_corr = array
|
|
381
|
+
models = [None] * len(unique_batch_ids)
|
|
382
|
+
fitted_y = np.ones(len(array))
|
|
379
383
|
|
|
380
384
|
return {'model': models, 'fit_curve': fitted_y, 'normed_arr': int_arr_corr}
|
|
@@ -194,7 +194,7 @@ def umap_analysis(feature_table, params):
|
|
|
194
194
|
df = df[(~df['is_qc']) & (~df['is_blank'])]
|
|
195
195
|
n = df.iloc[:, 0].values
|
|
196
196
|
data_arr = feature_table[n].values # samples in columns and features in rows
|
|
197
|
-
keys = [i for i in df.columns[1:] if i not in
|
|
197
|
+
keys = [i for i in df.columns[1:] if i not in PROJECT_KEYS]
|
|
198
198
|
|
|
199
199
|
# UMAP analysis
|
|
200
200
|
data_arr = data_arr.T
|
|
@@ -270,3 +270,4 @@ def generate_random_color(num):
|
|
|
270
270
|
|
|
271
271
|
|
|
272
272
|
COLORS = ["#FF5050", "#0078F0", "#00B050", "#FFC000", "#7030A0", "#FF00FF", "#00B0F0", "#FF0000", "#00FF00", "#0000FF"]
|
|
273
|
+
PROJECT_KEYS = ['is_qc', 'is_blank', 'analytical_order', 'time', 'ABSOLUTE_PATH', 'VALID', 'batch_id', 'SINGLE_FILE_PATH']
|
|
@@ -310,10 +310,10 @@ def plot_lowess_normalization(arr, fit_curve, arr_new, is_qc_or_sample, qc_idx,
|
|
|
310
310
|
# use color band to show the 95% confidence interval
|
|
311
311
|
tmp_new = arr_new[qc_idx]
|
|
312
312
|
tmp_new = tmp_new[tmp_new > 0]
|
|
313
|
-
y_up = np.median(tmp_new) + 1.96 * np.std(tmp_new)
|
|
314
|
-
y_down = np.median(tmp_new) - 1.96 * np.std(tmp_new)
|
|
315
|
-
plt.fill_between(v, y_down, y_up, color='lightblue', alpha=0.5)
|
|
316
313
|
if len(tmp_new) > 2:
|
|
314
|
+
y_up = np.median(tmp_new) + 1.96 * np.std(tmp_new)
|
|
315
|
+
y_down = np.median(tmp_new) - 1.96 * np.std(tmp_new)
|
|
316
|
+
plt.fill_between(v, y_down, y_up, color='lightblue', alpha=0.5)
|
|
317
317
|
rsd = np.std(tmp_new) / np.mean(tmp_new) * 100
|
|
318
318
|
else:
|
|
319
319
|
rsd = np.nan
|
|
@@ -254,10 +254,10 @@ def untargeted_metabolomics_workflow(path=None, return_results=False, only_proce
|
|
|
254
254
|
output_feature_table(feature_table, output_path)
|
|
255
255
|
# output the acquired MS2 spectra to a MSP file (designed for MassWiki)
|
|
256
256
|
output_path = os.path.join(params.project_file_dir, "features.msp")
|
|
257
|
+
output_feature_to_msp(feature_table, output_path)
|
|
257
258
|
# output features as pickle file to the project directory
|
|
258
259
|
with open(os.path.join(params.project_file_dir, "aligned_features.pkl"), "wb") as f:
|
|
259
260
|
pickle.dump(features, f)
|
|
260
|
-
output_feature_to_msp(feature_table, output_path)
|
|
261
261
|
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
|
|
262
262
|
else:
|
|
263
263
|
print("Step 3: Feature alignment is skipped. Using the existing aligned feature table.")
|
|
@@ -267,7 +267,6 @@ def untargeted_metabolomics_workflow(path=None, return_results=False, only_proce
|
|
|
267
267
|
# STEP 5. signal normalization
|
|
268
268
|
if params.signal_normalization:
|
|
269
269
|
print("Step 5: Running signal normalization...")
|
|
270
|
-
print(params.plot_normalization)
|
|
271
270
|
if params.plot_normalization:
|
|
272
271
|
feature_table = signal_normalization(feature_table, params.sample_metadata, params.signal_norm_method, output_plot_path=params.normalization_dir)
|
|
273
272
|
else:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|