comparison ms2deepscore_similarity.xml @ 0:58c95171a0d3 draft default tip

planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/ms2deepscore commit 4bd610e0cbbcbed51a6bfb880179777fc8034fd6
author recetox
date Mon, 02 Sep 2024 12:12:18 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:58c95171a0d3
1 <tool id="ms2deepscore_similarity" name="MS2DeepScore Similarity" version="@TOOL_VERSION@+galaxy0">
2 <description>Compute similarity scores using a pre-trained MS2DeepScore model</description>
3 <macros>
4 <import>macros.xml</import>
5 </macros>
6 <expand macro="creator"/>
7 <expand macro="edam" />
8
9 <requirements>
10 <requirement type="package" version="@TOOL_VERSION@">ms2deepscore</requirement>
11 <requirement type="package" version="@ONNX_VERSION@">onnx</requirement>
12 </requirements>
13
14 <command detect_errors="exit_code"><![CDATA[
15 python3 ${python_wrapper}
16 ]]></command>
17 <configfiles>
18 <configfile name="python_wrapper">
19 @init_logger@
20
21 import onnx
22 import torch
23 from ms2deepscore import MS2DeepScore
24 from matchms import calculate_scores
25 from matchms.importing import load_from_msp, scores_from_json
26 from ms2deepscore.models.SiameseSpectralModel import SiameseSpectralModel
27 from ms2deepscore.SettingsMS2Deepscore import SettingsMS2Deepscore
28
29 onnx_model = onnx.load("$model")
30
31 # Extract the initializers (weights and biases)
32 initializers = {init.name: onnx.numpy_helper.to_array(init) for init in onnx_model.graph.initializer}
33
34 # Convert NumPy arrays to PyTorch tensors
35 state_dict = {name: torch.tensor(np_array) for name, np_array in initializers.items()}
36
37 @json_load@
38
39 model = SiameseSpectralModel(settings=SettingsMS2Deepscore(**model_params))
40 model.load_state_dict(state_dict)
41 model.eval()
42
43 #if $scores.use_scores == "True"
44 scores_in = scores_from_json("${scores_in}")
45 references=scores_in.references
46 queries=scores_in.queries
47 #else
48 references=list(load_from_msp("$references"))
49 queries=list(load_from_msp("$queries"))
50 #end if
51
52 similarity = MS2DeepScore(model)
53 scores = calculate_scores(references, queries, similarity)
54
55 #if $scores.use_scores == "True"
56 scores_in._scores.add_dense_matrix(scores.to_array(), "MS2DeepScore")
57 scores_in.to_json("$similarity_scores")
58 #else
59 scores.to_json("$similarity_scores")
60 #end if
61 </configfile>
62 </configfiles>
63
64 <inputs>
65 <expand macro="input_param" />
66 </inputs>
67
68 <outputs>
69 <data label="ms2deepscore scores of ${on_string}" name="similarity_scores" format="json"/>
70 </outputs>
71
72 <tests>
73 <test expect_num_outputs="1">
74 <param name="use_scores" value="False"/>
75 <param name="references" value="inp_filtered_library.msp" ftype="msp"/>
76 <param name="queries" value="inp_filtered_spectra.msp" ftype="msp"/>
77 <param name="model" value="Trained_model.onnx" ftype="onnx"/>
78 <param name="model_param" value="Model_Parameter_JSON.json" ftype="json"/>
79 <output name="similarity_scores" value="msp_json_score_out.json" ftype="json" compare="sim_size" />
80 </test>
81 <test expect_num_outputs="1">
82 <param name="use_scores" value="True"/>
83 <param name="scores_in" value="ri_match_60.json" ftype="json"/>
84 <param name="model" value="Trained_model.onnx" ftype="onnx"/>
85 <param name="model_param" value="Model_Parameter_JSON.json" ftype="json"/>
86 <output name="similarity_scores" value="usescore_json_score_out.json" ftype="json" compare="sim_size" />
87 </test>
88 </tests>
89
90 <help>
91 <![CDATA[
92 Info
93 ====
94 Use a MS2DeepScore model for spectral similarity calculation.
95 For security reasons, only ONNX models are supported to be used.
96 If you want to use your own model, you can convert it to ONNX using the code contained in the 'MS2DeepScore Model Training' tool.
97
98 About
99 =====
100 @HELP@
101 ]]>
102 </help>
103 <expand macro="citations"/>
104 </tool>