Mercurial > repos > recetox > matchms_spectral_similarity
comparison matchms_filtering_wrapper.py @ 15:ee35dcec350a draft
planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 113433b8b9790405c2c5d054aee4a29a21b77dc7
author | recetox |
---|---|
date | Thu, 30 May 2024 18:12:09 +0000 |
parents | 44e7027714d7 |
children | 7627b40601f6 |
comparison
equal
deleted
inserted
replaced
14:5fae85d4a69a | 15:ee35dcec350a |
---|---|
1 import argparse | 1 import argparse |
2 import sys | 2 import sys |
3 | 3 |
4 from matchms.exporting import save_as_mgf, save_as_msp | 4 from matchms.exporting import save_as_mgf, save_as_msp |
5 from matchms.filtering import add_compound_name, add_fingerprint, add_losses, add_parent_mass, add_precursor_mz,\ | 5 from matchms.filtering import add_compound_name, add_fingerprint, add_losses, add_parent_mass, add_precursor_mz, \ |
6 add_retention_index, add_retention_time, clean_compound_name | 6 add_retention_index, add_retention_time, clean_compound_name |
7 from matchms.filtering import default_filters, normalize_intensities, reduce_to_number_of_peaks, select_by_mz, \ | 7 from matchms.filtering import default_filters, normalize_intensities, reduce_to_number_of_peaks, select_by_mz, \ |
8 select_by_relative_intensity | 8 select_by_relative_intensity |
9 from matchms.filtering.filter_utils.derive_precursor_mz_and_parent_mass import derive_precursor_mz_from_parent_mass | 9 from matchms.filtering.filter_utils.derive_precursor_mz_and_parent_mass import derive_precursor_mz_from_parent_mass |
10 from matchms.filtering.filter_utils.smile_inchi_inchikey_conversions import is_valid_inchi, is_valid_smiles | |
11 from matchms.importing import load_from_mgf, load_from_msp | 10 from matchms.importing import load_from_mgf, load_from_msp |
12 | |
13 | |
14 def require_key(spectrum, key, function): | |
15 value = spectrum.get(key) | |
16 if function(value): | |
17 return spectrum | |
18 | |
19 return None | |
20 | 11 |
21 | 12 |
22 def main(argv): | 13 def main(argv): |
23 parser = argparse.ArgumentParser(description="Compute MSP similarity scores") | 14 parser = argparse.ArgumentParser(description="Compute MSP similarity scores") |
24 parser.add_argument("--spectra", type=str, required=True, help="Mass spectra file to be filtered.") | 15 parser.add_argument("--spectra", type=str, required=True, help="Mass spectra file to be filtered.") |
36 parser.add_argument("--to_intensity", type=float, help="Upper bound for intensity filter") | 27 parser.add_argument("--to_intensity", type=float, help="Upper bound for intensity filter") |
37 parser.add_argument("-mz_range", action='store_true', | 28 parser.add_argument("-mz_range", action='store_true', |
38 help="Keep only peaks between set m/z range (keep if to_mz >= m/z >= from_mz).") | 29 help="Keep only peaks between set m/z range (keep if to_mz >= m/z >= from_mz).") |
39 parser.add_argument("--from_mz", type=float, help="Lower bound for m/z filter") | 30 parser.add_argument("--from_mz", type=float, help="Lower bound for m/z filter") |
40 parser.add_argument("--to_mz", type=float, help="Upper bound for m/z filter") | 31 parser.add_argument("--to_mz", type=float, help="Upper bound for m/z filter") |
41 parser.add_argument("-require_smiles", action='store_true', | |
42 help="Remove spectra that does not contain SMILES.") | |
43 parser.add_argument("-require_inchi", action='store_true', | |
44 help="Remove spectra that does not contain INCHI.") | |
45 parser.add_argument("-derive_precursor_mz_from_parent_mass", action='store_true', | 32 parser.add_argument("-derive_precursor_mz_from_parent_mass", action='store_true', |
46 help="Derives the precursor_mz from the parent mass and adduct or charge.") | 33 help="Derives the precursor_mz from the parent mass and adduct or charge.") |
47 parser.add_argument("--estimate_from_adduct", type=str, help="estimate from adduct.") | 34 parser.add_argument("--estimate_from_adduct", type=str, help="estimate from adduct.") |
48 parser.add_argument("-reduce_to_top_n_peaks", action='store_true', | 35 parser.add_argument("-reduce_to_top_n_peaks", action='store_true', |
49 help="reduce to top n peaks filter.") | 36 help="reduce to top n peaks filter.") |
53 if not (args.normalise_intensities | 40 if not (args.normalise_intensities |
54 or args.default_filters | 41 or args.default_filters |
55 or args.clean_metadata | 42 or args.clean_metadata |
56 or args.relative_intensity | 43 or args.relative_intensity |
57 or args.mz_range | 44 or args.mz_range |
58 or args.require_smiles | |
59 or args.require_inchi | |
60 or args.derive_precursor_mz_from_parent_mass | 45 or args.derive_precursor_mz_from_parent_mass |
61 or args.reduce_to_top_n_peaks): | 46 or args.reduce_to_top_n_peaks): |
62 raise ValueError('No filter selected.') | 47 raise ValueError('No filter selected.') |
63 | 48 |
64 if args.spectra_format == 'msp': | 49 if args.spectra_format == 'msp': |
94 if args.derive_precursor_mz_from_parent_mass: | 79 if args.derive_precursor_mz_from_parent_mass: |
95 spectrum.set("parent_mass", float(spectrum.get('parent_mass'))) | 80 spectrum.set("parent_mass", float(spectrum.get('parent_mass'))) |
96 precursor_mz = derive_precursor_mz_from_parent_mass(spectrum, args.estimate_from_adduct) | 81 precursor_mz = derive_precursor_mz_from_parent_mass(spectrum, args.estimate_from_adduct) |
97 spectrum.set("precursor_mz", precursor_mz) | 82 spectrum.set("precursor_mz", precursor_mz) |
98 | 83 |
99 if args.require_smiles and spectrum is not None: | |
100 spectrum = require_key(spectrum, "smiles", is_valid_smiles) | |
101 | |
102 if args.require_inchi and spectrum is not None: | |
103 spectrum = require_key(spectrum, "inchi", is_valid_inchi) | |
104 | |
105 if spectrum is not None: | 84 if spectrum is not None: |
106 filtered_spectra.append(spectrum) | 85 filtered_spectra.append(spectrum) |
107 | 86 |
108 if args.spectra_format == 'msp': | 87 if args.spectra_format == 'msp': |
109 save_as_msp(filtered_spectra, args.output) | 88 save_as_msp(filtered_spectra, args.output) |