File: compare_spectra.inc

package info (click to toggle)
python-pymzml 2.5.2%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,792 kB
  • sloc: python: 6,495; pascal: 341; makefile: 233; sh: 30
file content (58 lines) | stat: -rwxr-xr-x 1,447 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.. code-block:: python

	#!/usr/bin/env python
	# -*- coding: utf-8 -*-
	
	import os
	
	import pymzml
	
	
	def main():
	    """
	    Compare multiple spectra and return the cosine distance between them.
	    The returned value is between 0 and 1, a returned value of 1
	    represents highest similarity.
	
	    usage:
	
	        ./compare_spectra.py
	
	    """
	    example_file = os.path.join(
	        os.path.dirname(__file__), os.pardir, "tests", "data", "example.mzML"
	    )
	    print(
	        """
	            Comparing spectra
	        """
	    )
	    # print(example_file)
	    run = pymzml.run.Reader(example_file)
	    tmp = []
	    for spec in run:
	        if spec.ms_level == 1:
	            print("Parsing spectrum lvl 1 has id {0}".format(spec.ID))
	            tmp.append(spec)
	            if len(tmp) >= 3:
	                break
	
	    print("Print total number of specs collected {0}".format(len(tmp)))
	    for compare_tuples in [(0, 1), (0, 2), (1, 2)]:
	        print(
	            "Cosine between spectra {0} & {1} is {2:1.4f}".format(
	                compare_tuples[0] + 1,
	                compare_tuples[1] + 1,
	                tmp[compare_tuples[0]].similarity_to(tmp[compare_tuples[1]]),
	            )
	        )
	
	    print(
	        "Cosine score between first spectrum against itself: {0:1.4f}".format(
	            tmp[0].similarity_to(tmp[0])
	        )
	    )
	
	
	if __name__ == "__main__":
	    main()