annotate get_data/kegg_glycan/linkKEGG.py @ 1:0a5e0df17054 draft default tip

Uploaded
author chrisb
date Fri, 06 May 2016 08:05:48 -0400
parents 89592faa2875
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
89592faa2875 Uploaded
chrisb
parents:
diff changeset
1 __author__ = 'cbarnett'
89592faa2875 Uploaded
chrisb
parents:
diff changeset
2 __license__ = "MIT"
1
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
3 __version = "0.3.1"
0
89592faa2875 Uploaded
chrisb
parents:
diff changeset
4 # http://www.kegg.jp/kegg/rest/keggapi.html
89592faa2875 Uploaded
chrisb
parents:
diff changeset
5
89592faa2875 Uploaded
chrisb
parents:
diff changeset
6
89592faa2875 Uploaded
chrisb
parents:
diff changeset
7 def linked_entries_from_kegg(targetdb="glycan", sourcedb="pathway"):
89592faa2875 Uploaded
chrisb
parents:
diff changeset
8 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
9 :param targetdb:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
10 :param sourcedb:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
11 :return: string of linked entries
89592faa2875 Uploaded
chrisb
parents:
diff changeset
12 """
89592faa2875 Uploaded
chrisb
parents:
diff changeset
13 import urllib2
89592faa2875 Uploaded
chrisb
parents:
diff changeset
14
89592faa2875 Uploaded
chrisb
parents:
diff changeset
15 uri = 'http://rest.kegg.jp/link/'
1
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
16
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
17 import re
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
18 p = re.compile(' *\+ *') # ensure no unneccessary space in an AND query
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
19 sdbfix = p.subn('+', sourcedb)
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
20 sourcedb=sdbfix[0]
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
21
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
22 if ' ' in sourcedb:
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
23 sourcedb = sourcedb.replace(' ', '%20') # previous behaviour was ignoring text after a space, rather convert to '%20' and pass on to KEGG REST service
0a5e0df17054 Uploaded
chrisb
parents: 0
diff changeset
24
0
89592faa2875 Uploaded
chrisb
parents:
diff changeset
25 fulluri = uri + targetdb + "/" + sourcedb
89592faa2875 Uploaded
chrisb
parents:
diff changeset
26 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
27 response = urllib2.urlopen(fulluri).read()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
28 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
29 raise urllib2.HTTPError(e.url, e.code, e.msg, e.hdrs, e.fp)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
30 if str(response.strip()) == "":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
31 return ""
89592faa2875 Uploaded
chrisb
parents:
diff changeset
32 return response
89592faa2875 Uploaded
chrisb
parents:
diff changeset
33
89592faa2875 Uploaded
chrisb
parents:
diff changeset
34
89592faa2875 Uploaded
chrisb
parents:
diff changeset
35 if __name__ == "__main__":
89592faa2875 Uploaded
chrisb
parents:
diff changeset
36 from optparse import OptionParser
89592faa2875 Uploaded
chrisb
parents:
diff changeset
37
89592faa2875 Uploaded
chrisb
parents:
diff changeset
38 usage = "usage: python %prog [options]\n"
89592faa2875 Uploaded
chrisb
parents:
diff changeset
39 parser = OptionParser(usage=usage)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
40 parser.add_option("-t", action="store", type="string", dest="t", default="glycan",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
41 help="target db name pathway | brite | module | ko | genome | <org> | compound | glycan | reaction | rpair | rclass | enzyme | disease | drug | dgroup | environ")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
42 parser.add_option("-s", action="store", type="string", dest="s", default="pathway",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
43 help="source db name or db entry e.g. map00010")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
44 parser.add_option("-o", action="store", type="string", dest="o", default="linked_entries.txt",
89592faa2875 Uploaded
chrisb
parents:
diff changeset
45 help="linked entries output in text format")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
46 (options, args) = parser.parse_args()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
47 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
48 outstream = file(options.o, 'w')
89592faa2875 Uploaded
chrisb
parents:
diff changeset
49 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
50 raise IOError(e, "the output file cannot be opened. Use -h flag for help")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
51 linked = linked_entries_from_kegg(targetdb=options.t, sourcedb=options.s)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
52 try:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
53 outstream.write(linked)
89592faa2875 Uploaded
chrisb
parents:
diff changeset
54 except Exception as e:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
55 raise IOError(e, "cannot open output files. -h flag for help")
89592faa2875 Uploaded
chrisb
parents:
diff changeset
56 finally:
89592faa2875 Uploaded
chrisb
parents:
diff changeset
57 outstream.close()
89592faa2875 Uploaded
chrisb
parents:
diff changeset
58
89592faa2875 Uploaded
chrisb
parents:
diff changeset
59