Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matchms add key, matchms remove key: Added multiple keys handling #502

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions tools/matchms/matchms_add_key.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<tool id="matchms_add_key" name="matchms add key" version="@TOOL_VERSION@+galaxy1" profile="21.09">
<tool id="matchms_add_key" name="matchms add key" version="@TOOL_VERSION@+galaxy2" profile="21.09">
<description>Set metadata key in MSP to static value</description>

<macros>
Expand Down Expand Up @@ -29,13 +29,14 @@ import matchms
from matchms.importing import load_from_msp
from matchms.exporting import save_as_msp


#set keys_dict = ({str($k.key): str($k.value) for $k in $attribute_repeat})
matchms.Metadata.set_key_replacements({})

spectra = list(load_from_msp("${spectral_library}", metadata_harmonization = "False"))
new_spectra = []
for spectrum in spectra:
spectrum.set("${key}", "${value}")
for key, value in ($keys_dict).items():
spectrum.set(key, value)
new_spectra.append(spectrum)
save_as_msp(new_spectra, "${output}")
</configfile>
Expand All @@ -44,17 +45,18 @@ save_as_msp(new_spectra, "${output}")
<inputs>
<param label="Spectra file" name="spectral_library" type="data" format="msp"
help="Mass spectral library file to add key." />

<param label="Attribute Name" name="key" type="text" value="" help="Name of the attribute which will be assigned to all spectra records in the MSP." />
<param label="Value" name="value" type="text" value="" help="Value of the attribute which will be assigned to all spectra records in the MSP." >
<sanitizer>
<valid initial="default">
<add value="{}" />
<add value="[]" />
<add value="\" />
</valid>
</sanitizer>
</param>
<repeat name="attribute_repeat" title="Keys to add">
<param label="Attribute Name" name="key" type="text" value="" help="Name of the attribute which will be assigned to all spectra records in the MSP." />
<param label="Value" name="value" type="text" value="" help="Value of the attribute which will be assigned to all spectra records in the MSP." >
<sanitizer>
<valid initial="default">
<add value="{}" />
<add value="[]" />
<add value="\" />
</valid>
</sanitizer>
</param>
</repeat>
</inputs>

<outputs>
Expand All @@ -71,8 +73,14 @@ save_as_msp(new_spectra, "${output}")
</test>
<test>
<param name="spectral_library" value="add_key/add_key_test2.msp" ftype="msp"/>
<param name="key" value="adduct"/>
<param name="value" value="[M]+"/>
<repeat name="attribute_repeat">
<param name="key" value="adduct"/>
<param name="value" value="[M]+"/>
</repeat>
<repeat name="attribute_repeat">
<param name="key" value="tool_used"/>
<param name="value" value="matchms"/>
</repeat>
<output name="output" file="add_key/add_key_test2_out.msp" ftype="msp"/>
</test>
</tests>
Expand Down
33 changes: 24 additions & 9 deletions tools/matchms/matchms_remove_key.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<tool id="matchms_remove_key" name="matchms remove key" version="@TOOL_VERSION@+galaxy0" profile="21.09">
<tool id="matchms_remove_key" name="matchms remove key" version="@TOOL_VERSION@+galaxy1" profile="21.09">
<description>Remove metadata entry for all spectra in a library</description>

<macros>
Expand Down Expand Up @@ -26,15 +26,19 @@
import matchms
@init_logger@


#set keys_list = ([str($k.key) for $k in $attribute_repeat])

matchms.Metadata.set_key_replacements({})
key = "${key}".lower()
spectra = list(matchms.importing.load_from_msp("${spectral_library}", metadata_harmonization = "False"))
new_spectra = []
for spectrum in spectra:
if spectrum.get(key) is not None:
metadata = spectrum.metadata
del metadata[key]
spectrum.metadata = metadata
for key in $keys_list:
key = key.lower()
if spectrum.get(key) is not None:
metadata = spectrum.metadata
del metadata[key]
spectrum.metadata = metadata
new_spectra.append(spectrum)
matchms.exporting.save_as_msp(new_spectra, "${output}")
</configfile>
Expand All @@ -43,8 +47,9 @@ matchms.exporting.save_as_msp(new_spectra, "${output}")
<inputs>
<param label="Spectra file" name="spectral_library" type="data" format="msp"
help="Mass spectral library file to add key." />

<param label="Attribute Name" name="key" type="text" value="" help="Name of the attribute which will be removed from all spectra records in the MSP." />
<repeat name="attribute_repeat" title="Keys to remove">
<param label="Attribute Name" name="key" type="text" value="" help="Name of the attribute which will be removed from all spectra records in the MSP." />
</repeat>
</inputs>

<outputs>
Expand All @@ -56,7 +61,17 @@ matchms.exporting.save_as_msp(new_spectra, "${output}")
<test>
<param name="spectral_library" value="filtering/input.msp" ftype="msp"/>
<param name="key" value="ionmode"/>
<output name="output" file="out_matchms_remove_key.msp" ftype="msp" compare="sim_size"/>
<output name="output" file="remove_key/out_matchms_remove_key.msp" ftype="msp"/>
</test>
<test>
<param name="spectral_library" value="filtering/input.msp" ftype="msp"/>
<repeat name="attribute_repeat">
<param name="key" value="ionmode"/>
</repeat>
<repeat name="attribute_repeat">
<param name="key" value="spectrumtype"/>
</repeat>
<output name="output" file="remove_key/out2_matchms_remove_key.msp" ftype="msp"/>
</test>
</tests>

Expand Down
2 changes: 2 additions & 0 deletions tools/matchms/test-data/add_key/add_key_test2_out.msp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ INSTRUMENT_TYPE: GC-EI-Orbitrap
CHARGE: 1
PARENT_MASS: 251.08595400000002
ADDUCT: [M]+
TOOL_USED: matchms
NUM PEAKS: 3
250.07765 0.3282529462971431
252.09323 1.0
Expand All @@ -39,6 +40,7 @@ INSTRUMENT_TYPE: GC-EI-Orbitrap
CHARGE: 1
PARENT_MASS: 177.070224
ADDUCT: [M]+
TOOL_USED: matchms
NUM PEAKS: 5
152.0619 0.1657993569424221
176.062 0.24558560966311757
Expand Down
188 changes: 0 additions & 188 deletions tools/matchms/test-data/out_matchms_remove_key.msp

This file was deleted.

Loading
Loading