Debian Codegraph @

docs/source/code_inc/queryOBO.inc

raw
1 .. code-block:: python
2
3 #!/usr/bin/env python
4 # -*- coding: utf-8 -*-
5
6
7 import argparse
8 from collections import defaultdict
9
10 import pymzml.obo
11
12
13 FIELDNAMES = ["id", "name", "def", "is_a"]
14
15
16 def main(args):
17 """
18 Use this script to interrogate the OBO database files.
19
20 usage:
21
22 ./queryOBO.py [-h] [-v VERSION] query
23
24 Example::
25
26 $ ./queryOBO.py'scan time'
27 MS:1000016
28 scan time
29 'The time taken for an acquisition by scanning analyzers.' [PSI:MS]
30 Is a: MS:1000503 ! scan attribute
31
32 Example::
33
34 $ ./queryOBO.py 1000016
35 MS:1000016
36 scan time
37 "The time taken for an acquisition by scanning analyzers." [PSI:MS]
38 MS:1000503 ! scan attribute
39
40
41 """
42 obo = pymzml.obo.OboTranslator(version=args.version)
43 obo.parseOBO()
44 if args.query.isdigit():
45 print(search_by_id(obo, args.query))
46 else:
47 for ix, match in enumerate(search_by_name(obo, args.query)):
48 print("#{0}".format(ix))
49
50 for fieldname in ("id", "name", "def"):
51 print(match[fieldname])
52
53 if "is_a" in match:
54 print("Is a:", match["is_a"])
55
56
57 def search_by_name(obo, name):
58 print("Searching for {0}".format(name.lower()))
59 matches = []
60 for lookup in obo.lookups:
61 for key in lookup.keys():
62 if name.lower() in key.lower():
63 match = defaultdict(str)
64
65 for fieldname in FIELDNAMES:
66 if fieldname in lookup[key].keys():
67 match[fieldname] = lookup[key][fieldname]
68
69 matches.append(match)
70
71 return matches
72
73
74 def search_by_id(obo, id):
75 key = "MS:{0}".format(id)
76 return_value = ""
77 for lookup in obo.lookups:
78 if key in lookup:
79 if obo.MS_tag_regex.match(key):
80 for fn in FIELDNAMES:
81 if fn in lookup[key].keys():
82 return_value += "{0}\n".format(lookup[key][fn])
83 return return_value
84
85
86 if __name__ == "__main__":
87 argparser = argparse.ArgumentParser(usage=__doc__)
88 argparser.add_argument(
89 "query", help="an accession or part of an OBO term name to look for"
90 )
91 argparser.add_argument(
92 "-v",
93 "--version",
94 default="1.1.0",
95 help="""
96 the version of the OBO to use; valid options are 1.0.0, 1.1.0, and 1.2,
97 default is 1.1.0
98 """,
99 )
100
101 args = argparser.parse_args()
102
103 main(args)