annotate matchms_wrapper.py @ 2:a7c9fc186f8c draft

"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
author recetox
date Mon, 19 Apr 2021 08:31:42 +0000
parents 4aecfd6b319b
children 57959596262d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
1 import argparse
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
2 import sys
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
3
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
4 from matchms import calculate_scores
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
5 from matchms.filtering import add_precursor_mz
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
6 from matchms.importing import load_from_msp
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
7 from matchms.similarity import (
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
8 CosineGreedy,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
9 CosineHungarian,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
10 ModifiedCosine,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
11 )
1
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
12 from pandas import DataFrame
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
13
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
14
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
15 def main(argv):
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
16 parser = argparse.ArgumentParser(description="Compute MSP similarity scores")
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
17 parser.add_argument(
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
18 "references_filename", type=str, help="Path to reference MSP library."
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
19 )
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
20 parser.add_argument("queries_filename", type=str, help="Path to query spectra.")
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
21 parser.add_argument("similarity_metric", type=str, help='Metric to use for matching.')
1
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
22 parser.add_argument("output_filename_scores", type=str, help="Path where to store the output .csv scores.")
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
23 parser.add_argument("output_filename_matches", type=str, help="Path where to store the output .csv matches.")
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
24 parser.add_argument("tolerance", type=float, help="Tolerance to use for peak matching.")
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
25 parser.add_argument("mz_power", type=float, help="The power to raise mz to in the cosine function.")
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
26 parser.add_argument("intensity_power", type=float, help="The power to raise intensity to in the cosine function.")
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
27
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
28 args = parser.parse_args()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
29
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
30 reference_spectra = load_from_msp(args.references_filename)
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
31 queries_spectra = load_from_msp(args.queries_filename)
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
32
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
33 if args.similarity_metric == 'CosineGreedy':
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
34 similarity_metric = CosineGreedy(args.tolerance, args.mz_power, args.intensity_power)
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
35 elif args.similarity_metric == 'CosineHungarian':
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
36 similarity_metric = CosineHungarian(args.tolerance, args.mz_power, args.intensity_power)
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
37 elif args.similarity_metric == 'ModifiedCosine':
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
38 similarity_metric = ModifiedCosine(args.tolerance, args.mz_power, args.intensity_power)
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
39 reference_spectra = map(add_precursor_mz, reference_spectra)
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
40 queries_spectra = map(add_precursor_mz, queries_spectra)
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
41 else:
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
42 return -1
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
43
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
44 scores = calculate_scores(
2
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
45 references=list(reference_spectra),
a7c9fc186f8c "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 557e6558b93e63fd0b70443164d2d624cc05c319"
recetox
parents: 1
diff changeset
46 queries=list(queries_spectra),
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
47 similarity_function=similarity_metric,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
48 )
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
49
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
50 query_names = [spectra.metadata['name'] for spectra in scores.queries]
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
51 reference_names = [spectra.metadata['name'] for spectra in scores.references]
1
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
52
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
53 # Write scores to dataframe
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
54 dataframe_scores = DataFrame(data=[entry["score"] for entry in scores.scores], index=reference_names, columns=query_names)
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
55 dataframe_scores.to_csv(args.output_filename_scores, sep=';')
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
56
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
57 # Write number of matches to dataframe
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
58 dataframe_matches = DataFrame(data=[entry["matches"] for entry in scores.scores], index=reference_names, columns=query_names)
4aecfd6b319b "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit d110cb008c3703945fe3718465de36278fa34652"
recetox
parents: 0
diff changeset
59 dataframe_matches.to_csv(args.output_filename_matches, sep=';')
0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
60 return 0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
61
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
62
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
63 if __name__ == "__main__":
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
64 main(argv=sys.argv[1:])
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
65 pass