comparison matchms_remove_spectra.xml @ 1:c24ebd41dd53 draft

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 2abca9206b76bf42e6322d751ed3a31a6cc003a4
author recetox
date Tue, 04 Jun 2024 07:10:48 +0000
parents 80df426e7e47
children fbe335d27d25
comparison
equal deleted inserted replaced
0:80df426e7e47 1:c24ebd41dd53
1 <tool id="matchms_remove_spectra" name="matchms remove spectra" version="@TOOL_VERSION@+galaxy0" profile="21.09"> 1 <tool id="matchms_remove_spectra" name="matchms remove spectra" version="@TOOL_VERSION@+galaxy1" profile="21.09">
2 <description>Filters spectra based on metadata presence</description> 2 <description>Filters spectra based on metadata presence</description>
3 3
4 <macros> 4 <macros>
5 <import>macros.xml</import> 5 <import>macros.xml</import>
6 </macros> 6 </macros>
26 from matchms.filtering.filter_utils.smile_inchi_inchikey_conversions import is_valid_inchi, is_valid_smiles, is_valid_inchikey 26 from matchms.filtering.filter_utils.smile_inchi_inchikey_conversions import is_valid_inchi, is_valid_smiles, is_valid_inchikey
27 from matchms.filtering import require_compound_name, require_formula, require_precursor_mz, \ 27 from matchms.filtering import require_compound_name, require_formula, require_precursor_mz, \
28 require_retention_time, require_retention_index, require_valid_annotation 28 require_retention_time, require_retention_index, require_valid_annotation
29 from matchms.importing import load_from_mgf, load_from_msp 29 from matchms.importing import load_from_mgf, load_from_msp
30 30
31 #set metadata_fields = str("', '").join([str($f) for $f in $metadata_fields]) 31 #set metadata_fields = [str($f) for $f in $metadata_fields]
32 required_metadata = "$metadata_fields" 32 required_metadata = [x for x in $metadata_fields]
33
34 if "$spectra.ext" == "msp": 33 if "$spectra.ext" == "msp":
35 spectra = list(load_from_msp("${spectra}")) 34 spectra = list(load_from_msp("${spectra}"))
36 elif "$spectra.ext" == 'mgf': 35 elif "$spectra.ext" == 'mgf':
37 spectra = list(load_from_mgf("${spectra}")) 36 spectra = list(load_from_mgf("${spectra}"))
38 else: 37 else:
39 raise ValueError(f'File format {$spectra.ext} not supported for mass spectra file.') 38 raise ValueError(f'File format {$spectra.ext} not supported for mass spectra file.')
40 39
41 filtered_spectra = [] 40 filtered_spectra = []
42 removed_spectra = [] 41 removed_spectra = []
43 keep = False
44 42
45 for spectrum in spectra: 43 for spectrum in spectra:
44 keep = True
45
46 if 'smiles' in required_metadata: 46 if 'smiles' in required_metadata:
47 keep = is_valid_smiles(spectrum.get('smiles')) 47 keep = keep and is_valid_smiles(spectrum.get('smiles', '').strip())
48 48
49 if 'inchi' in required_metadata: 49 if 'inchi' in required_metadata:
50 keep = is_valid_inchi(spectrum.get('inchi')) 50 keep = keep and is_valid_inchi(spectrum.get('inchi', '').strip())
51 51
52 if 'inchikey' in required_metadata: 52 if 'inchikey' in required_metadata:
53 keep = is_valid_inchikey(spectrum.get('inchikey')) 53 inchikey = spectrum.get('inchikey', '')
54 keep = keep and is_valid_inchikey(inchikey.strip())
54 55
55 if 'precursor_mz' in required_metadata: 56 if 'precursor_mz' in required_metadata:
56 result = require_precursor_mz(spectrum) 57 result = require_precursor_mz(spectrum)
57 if result is not None: 58 keep = keep and result is not None
58 keep = True
59 else:
60 keep = False
61 59
62 if 'valid_annotation' in required_metadata: 60 if 'valid_annotation' in required_metadata:
63 result = require_valid_annotation(spectrum) 61 result = require_valid_annotation(spectrum)
64 if result is not None: 62 keep = keep and result is not None
65 keep = True 63
66 else:
67 keep = False
68
69 if 'formula' in required_metadata: 64 if 'formula' in required_metadata:
70 result = require_formula(spectrum) 65 result = require_formula(spectrum)
71 if result is not None: 66 keep = keep and result is not None
72 keep = True
73 else:
74 keep = False
75 67
76 if 'compound_name' in required_metadata: 68 if 'compound_name' in required_metadata:
77 result = require_compound_name(spectrum) 69 result = require_compound_name(spectrum)
78 if result is not None: 70 keep = keep and result is not None
79 keep = True 71
80 else:
81 keep = False
82
83 if 'retention_time' in required_metadata: 72 if 'retention_time' in required_metadata:
84 result = require_retention_time(spectrum) 73 result = require_retention_time(spectrum)
85 if result is not None: 74 keep = keep and result is not None
86 keep = True
87 else:
88 keep = False
89 75
90 if 'retention_index' in required_metadata: 76 if 'retention_index' in required_metadata:
91 result = require_retention_index(spectrum) 77 result = require_retention_index(spectrum)
92 if result is not None: 78 if result is not None:
93 keep = True 79 keep = keep and float(result.get('retention_index', 0)) > 0
94 else: 80 else:
95 keep = False 81 keep = False
96 82
97 if keep: 83 if keep:
98 filtered_spectra.append(spectrum) 84 filtered_spectra.append(spectrum)