Mercurial > repos > chrisb > gap_all_glycan_tools
comparison get_data/kegg_glycan/getkcfKEGG.py @ 0:89592faa2875 draft
Uploaded
author | chrisb |
---|---|
date | Wed, 23 Mar 2016 14:35:56 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:89592faa2875 |
---|---|
1 __author__ = 'cbarnett' | |
2 __license__ = "MIT" | |
3 __version = "0.3" | |
4 | |
5 | |
6 def helper_get_G(lineentry): | |
7 if "G" in lineentry.upper(): | |
8 splitline = lineentry.split() | |
9 for item in splitline: | |
10 if "G" in item: | |
11 return item | |
12 return None | |
13 | |
14 | |
15 def get_kcf_from_kegg(inputstream): | |
16 """ | |
17 :param inputstream: input stream containing gl entries | |
18 :return: list of kcf output and list of db entry output or empty list | |
19 """ | |
20 import urllib2 | |
21 | |
22 uri = 'http://rest.kegg.jp/get/' | |
23 if inputstream is None or inputstream == []: | |
24 raise IOError("empty input stream") | |
25 dbresponses = [] | |
26 kcfresponses = [] | |
27 for line in inputstream: | |
28 glentry = helper_get_G(line) | |
29 if glentry is not None: | |
30 try: | |
31 dbresponse = urllib2.urlopen(uri + glentry).read() | |
32 kcfresponse = urllib2.urlopen(uri + glentry + "/kcf").read() | |
33 except Exception as e: | |
34 raise urllib2.HTTPError(e.url, e.code, e.msg, e.hdrs, e.fp) | |
35 dbresponses.append(dbresponse) | |
36 kcfresponses.append(kcfresponse) | |
37 return kcfresponses, dbresponses | |
38 | |
39 | |
40 if __name__ == "__main__": | |
41 from optparse import OptionParser | |
42 | |
43 usage = "usage: python %prog [options]\n" | |
44 parser = OptionParser(usage=usage) | |
45 parser.add_option("-i", action="store", type="string", dest="i", default="input", | |
46 help="single or double column text file containing GL entries") | |
47 parser.add_option("-k", action="store", type="string", dest="k", default="kcf.output", | |
48 help="kcf output file name") | |
49 parser.add_option("-d", action="store", type="string", dest="d", default="db.output", | |
50 help="KEGG db entry in text format output file name") | |
51 (options, args) = parser.parse_args() | |
52 try: | |
53 instream = file(options.i, 'r') | |
54 except Exception as e: | |
55 raise IOError(e, "the input file specified does not exist. Use -h flag for help") | |
56 kcf, db = get_kcf_from_kegg(instream) | |
57 try: | |
58 kcfout = file(options.k, 'w') | |
59 dbout = file(options.d, 'w') | |
60 except Exception as e: | |
61 raise IOError(e, "cannot open output files. -h flag for help") | |
62 | |
63 kcfout.write("".join(kcf)) | |
64 dbout.write("".join(db)) | |
65 kcfout.close() | |
66 dbout.close() | |
67 |