Debian Codegraph @

docs/source/code_inc/compare_spectra.inc

raw
1 .. code-block:: python
2
3 #!/usr/bin/env python
4 # -*- coding: utf-8 -*-
5
6 import os
7
8 import pymzml
9
10
11 def main():
12 """
13 Compare multiple spectra and return the cosine distance between them.
14 The returned value is between 0 and 1, a returned value of 1
15 represents highest similarity.
16
17 usage:
18
19 ./compare_spectra.py
20
21 """
22 example_file = os.path.join(
23 os.path.dirname(__file__), os.pardir, "tests", "data", "example.mzML"
24 )
25 print(
26 """
27 Comparing spectra
28 """
29 )
30 # print(example_file)
31 run = pymzml.run.Reader(example_file)
32 tmp = []
33 for spec in run:
34 if spec.ms_level == 1:
35 print("Parsing spectrum lvl 1 has id {0}".format(spec.ID))
36 tmp.append(spec)
37 if len(tmp) >= 3:
38 break
39
40 print("Print total number of specs collected {0}".format(len(tmp)))
41 for compare_tuples in [(0, 1), (0, 2), (1, 2)]:
42 print(
43 "Cosine between spectra {0} & {1} is {2:1.4f}".format(
44 compare_tuples[0] + 1,
45 compare_tuples[1] + 1,
46 tmp[compare_tuples[0]].similarity_to(tmp[compare_tuples[1]]),
47 )
48 )
49
50 print(
51 "Cosine score between first spectrum against itself: {0:1.4f}".format(
52 tmp[0].similarity_to(tmp[0])
53 )
54 )
55
56
57 if __name__ == "__main__":
58 main()