annotate matchms_wrapper.py @ 0:6a736abe431f draft

"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
author recetox
date Mon, 07 Dec 2020 20:12:13 +0000
parents
children 4aecfd6b319b
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 import pandas
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
5 from matchms import calculate_scores
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 FingerprintSimilarity,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
11 IntersectMz,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
12 ModifiedCosine,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
13 ParentMassMatch
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
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
16
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
17 def main(argv):
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
18 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
19 parser.add_argument(
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
20 "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
21 )
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
22 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
23 parser.add_argument("output_filename", type=str, help="Path where to store the output .csv.")
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
24 parser.add_argument("similarity_metric", type=str, help='Metric to use for matching.')
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
25
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
26 args = parser.parse_args()
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 if args.similarity_metric == 'CosineGreedy':
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
29 similarity_metric = CosineGreedy()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
30 elif args.similarity_metric == 'CosineHungarian':
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
31 similarity_metric = CosineHungarian()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
32 elif args.similarity_metric == 'FingerprintSimilarity':
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
33 similarity_metric = FingerprintSimilarity()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
34 elif args.similarity_metric == 'IntersectMz':
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
35 similarity_metric = IntersectMz()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
36 elif args.similarity_metric == 'ModifiedCosine':
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
37 similarity_metric = ModifiedCosine()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
38 else:
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
39 similarity_metric = ParentMassMatch()
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
40
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
41 reference_spectra = [
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
42 spectrum for spectrum in load_from_msp(args.references_filename)
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 queries_spectra = [spectrum for spectrum in load_from_msp(args.queries_filename)]
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
45
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
46 scores = calculate_scores(
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
47 references=reference_spectra,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
48 queries=queries_spectra,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
49 similarity_function=similarity_metric,
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
50 )
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
51
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
52 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
53 reference_names = [spectra.metadata['name'] for spectra in scores.references]
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
54 dataframe = pandas.DataFrame(data=scores.scores, index=reference_names, columns=query_names)
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
55 dataframe.to_csv(args.output_filename, sep=';')
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
56 return 0
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
57
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
58
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
59 if __name__ == "__main__":
6a736abe431f "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/matchms commit 25fba33c37b26a6e9321f043c61b3c0dd392a53c"
recetox
parents:
diff changeset
60 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
61 pass