diff matchms_filtering_wrapper.py @ 13:ca5a8db023e1 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 98223db312c30b0e121a1422a9534a3db3fbf0c0
author recetox
date Thu, 14 Dec 2023 13:45:58 +0000
parents 13de8005adba
children da15e8ea3b28
line wrap: on
line diff
--- a/matchms_filtering_wrapper.py	Mon Dec 04 19:12:43 2023 +0000
+++ b/matchms_filtering_wrapper.py	Thu Dec 14 13:45:58 2023 +0000
@@ -6,6 +6,7 @@
     add_retention_index, add_retention_time, clean_compound_name
 from matchms.filtering import default_filters, normalize_intensities, reduce_to_number_of_peaks, select_by_mz, \
     select_by_relative_intensity
+from matchms.filtering.filter_utils.derive_precursor_mz_and_parent_mass import derive_precursor_mz_from_parent_mass
 from matchms.importing import load_from_mgf, load_from_msp
 
 
@@ -39,6 +40,9 @@
                         help="Remove spectra that does not contain SMILES.")
     parser.add_argument("-require_inchi", action='store_true',
                         help="Remove spectra that does not contain INCHI.")
+    parser.add_argument("-derive_precursor_mz_from_parent_mass", action='store_true',
+                        help="Derives the precursor_mz from the parent mass and adduct or charge.")
+    parser.add_argument("--estimate_from_adduct", type=str, help="estimate from adduct.")
     parser.add_argument("-reduce_to_top_n_peaks", action='store_true',
                         help="reduce to top n peaks filter.")
     parser.add_argument("--n_max", type=int, help="Maximum number of peaks. Remove peaks if more peaks are found.")
@@ -51,6 +55,7 @@
             or args.mz_range
             or args.require_smiles
             or args.require_inchi
+            or args.derive_precursor_mz_from_parent_mass
             or args.reduce_to_top_n_peaks):
         raise ValueError('No filter selected.')
 
@@ -84,6 +89,11 @@
         if args.reduce_to_top_n_peaks:
             spectrum = reduce_to_number_of_peaks(spectrum_in=spectrum, n_max=args.n_max)
 
+        if args.derive_precursor_mz_from_parent_mass:
+            spectrum.set("parent_mass", float(spectrum.get('parent_mass')))
+            precursor_mz = derive_precursor_mz_from_parent_mass(spectrum, args.estimate_from_adduct)
+            spectrum.set("precursor_mz", precursor_mz)
+
         if args.require_smiles and spectrum is not None:
             spectrum = require_key(spectrum, "smiles")