0
|
1 __author__ = 'cbarnett'
|
|
2 __license__ = "MIT"
|
|
3 __version__ = "0.3"
|
|
4 # http://www.kegg.jp/kegg/rest/keggapi.html
|
|
5
|
|
6
|
|
7 def find_entries_in_KEGG(db, query):
|
|
8 """
|
|
9 :param db: db to search by default is glycan
|
|
10 :param query:
|
|
11 :return: string of linked entries
|
|
12 """
|
|
13 import urllib2
|
|
14
|
|
15 if db == [] or db == "" or db is None:
|
|
16 raise IOError("no db given")
|
|
17 if query == [] or query == "" or query is None:
|
|
18 raise IOError("no query given")
|
|
19 query = query.replace('\n', ' ') # in case of new lines
|
|
20 query = query.replace('\r', ' ') # in case of new lines
|
|
21 uri = 'http://rest.kegg.jp/find/'
|
|
22 fulluri = uri + db + "/" + query
|
|
23 try:
|
|
24 response = urllib2.urlopen(fulluri).read()
|
|
25 except Exception as e:
|
|
26 raise urllib2.HTTPError(e.url, e.code, e.msg, e.hdrs, e.fp)
|
|
27 if str(response.strip()) == "":
|
|
28 return None
|
|
29 return response
|
|
30
|
|
31
|
|
32 if __name__ == "__main__":
|
|
33 from optparse import OptionParser
|
|
34
|
|
35 usage = "usage: python %prog [options]\n"
|
|
36 parser = OptionParser(usage=usage)
|
|
37 parser.add_option("-d", action="store", type="string", dest="d", default="glycan",
|
|
38 help="db name, options are: pathway | brite | module | ko | genome | <org> | compound | glycan | reaction | rpair | rclass | enzyme | disease | drug | dgroup | environ")
|
|
39 parser.add_option("-q", action="store", type="string", dest="q", default="glucose",
|
|
40 help="query e.g. glucose")
|
|
41 parser.add_option("-o", action="store", type="string", dest="o", default="found_entries.txt",
|
|
42 help="entries from search output in text format")
|
|
43 (options, args) = parser.parse_args()
|
|
44 try:
|
|
45 outstream = file(options.o, 'w')
|
|
46 except Exception as e:
|
|
47 raise IOError(e, "the output file cannot be opened. Use -h flag for help")
|
|
48 results = find_entries_in_KEGG(db=options.d, query=options.q)
|
|
49 try:
|
|
50 outstream.write(results)
|
|
51 except Exception as e:
|
|
52 raise IOError(e, "cannot open output files. -h flag for help")
|
|
53 finally:
|
|
54 outstream.close()
|
|
55
|
|
56
|