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