File: queryOBO.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 (103 lines) | stat: -rwxr-xr-x 2,601 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
.. code-block:: python

	#!/usr/bin/env python
	# -*- coding: utf-8 -*-
	
	
	import argparse
	from collections import defaultdict
	
	import pymzml.obo
	
	
	FIELDNAMES = ["id", "name", "def", "is_a"]
	
	
	def main(args):
	    """
	    Use this script to interrogate the OBO database files.
	
	    usage:
	
	        ./queryOBO.py [-h] [-v VERSION] query
	
	    Example::
	
	      $ ./queryOBO.py'scan time'
	      MS:1000016
	      scan time
	      'The time taken for an acquisition by scanning analyzers.' [PSI:MS]
	      Is a: MS:1000503 ! scan attribute
	
	    Example::
	
	        $ ./queryOBO.py 1000016
	        MS:1000016
	        scan time
	        "The time taken for an acquisition by scanning analyzers." [PSI:MS]
	        MS:1000503 ! scan attribute
	
	
	    """
	    obo = pymzml.obo.OboTranslator(version=args.version)
	    obo.parseOBO()
	    if args.query.isdigit():
	        print(search_by_id(obo, args.query))
	    else:
	        for ix, match in enumerate(search_by_name(obo, args.query)):
	            print("#{0}".format(ix))
	
	            for fieldname in ("id", "name", "def"):
	                print(match[fieldname])
	
	            if "is_a" in match:
	                print("Is a:", match["is_a"])
	
	
	def search_by_name(obo, name):
	    print("Searching for {0}".format(name.lower()))
	    matches = []
	    for lookup in obo.lookups:
	        for key in lookup.keys():
	            if name.lower() in key.lower():
	                match = defaultdict(str)
	
	                for fieldname in FIELDNAMES:
	                    if fieldname in lookup[key].keys():
	                        match[fieldname] = lookup[key][fieldname]
	
	                matches.append(match)
	
	    return matches
	
	
	def search_by_id(obo, id):
	    key = "MS:{0}".format(id)
	    return_value = ""
	    for lookup in obo.lookups:
	        if key in lookup:
	            if obo.MS_tag_regex.match(key):
	                for fn in FIELDNAMES:
	                    if fn in lookup[key].keys():
	                        return_value += "{0}\n".format(lookup[key][fn])
	    return return_value
	
	
	if __name__ == "__main__":
	    argparser = argparse.ArgumentParser(usage=__doc__)
	    argparser.add_argument(
	        "query", help="an accession or part of an OBO term name to look for"
	    )
	    argparser.add_argument(
	        "-v",
	        "--version",
	        default="1.1.0",
	        help="""
	            the version of the OBO to use; valid options are 1.0.0, 1.1.0, and 1.2,
	            default is 1.1.0
	        """,
	    )
	
	    args = argparser.parse_args()
	
	    main(args)