Mercurial > repos > chrisb > gap_all_glycan_tools
comparison get_data/kegg_glycan/findKEGG.py @ 1:0a5e0df17054 draft default tip
Uploaded
author | chrisb |
---|---|
date | Fri, 06 May 2016 08:05:48 -0400 |
parents | 89592faa2875 |
children |
comparison
equal
deleted
inserted
replaced
0:89592faa2875 | 1:0a5e0df17054 |
---|---|
1 __author__ = 'cbarnett' | 1 __author__ = 'cbarnett' |
2 __license__ = "MIT" | 2 __license__ = "MIT" |
3 __version__ = "0.3" | 3 __version__ = "0.4" |
4 # http://www.kegg.jp/kegg/rest/keggapi.html | 4 # http://www.kegg.jp/kegg/rest/keggapi.html |
5 | 5 |
6 | 6 |
7 def find_entries_in_KEGG(db, query): | 7 def find_entries_in_KEGG(db, query): |
8 """ | 8 """ |
14 | 14 |
15 if db == [] or db == "" or db is None: | 15 if db == [] or db == "" or db is None: |
16 raise IOError("no db given") | 16 raise IOError("no db given") |
17 if query == [] or query == "" or query is None: | 17 if query == [] or query == "" or query is None: |
18 raise IOError("no query given") | 18 raise IOError("no query given") |
19 query = query.replace('\n', ' ') # in case of new lines | 19 query = query.replace('\n', '+') # in case of new lines, assume this means the user want to "AND" |
20 query = query.replace('\r', ' ') # in case of new lines | 20 query = query.replace('\r', '+') # in case of new lines, assume this means the user want to "AND" |
21 query = query.replace('ec: ', 'ec:') # in case of ec: spaces - e.g. issue "ec: 2.4.99.1" spaces are ok usually as allows for combination searching "2.4.99.1 2.4.99.6" | |
22 | |
23 import re | |
24 p = re.compile(' *\+ *') # ensure no unneccesary space in an AND query, otherwise incorrect behaviour from KEGG | |
25 queryfix = p.subn('+', query) | |
26 query=queryfix[0] | |
27 | |
28 if ' ' in query: | |
29 query='"'+query+'"' # if spaces, query must be placed in quotes, otherwise incorrect behaviour from KEGG | |
30 query = query.replace(' ', '%20') # previous behaviour was ignoring text after a space, rather convert to '%20' and pass on to KEGG REST service | |
21 uri = 'http://rest.kegg.jp/find/' | 31 uri = 'http://rest.kegg.jp/find/' |
22 fulluri = uri + db + "/" + query | 32 fulluri = uri + db + "/" + query |
23 try: | 33 try: |
24 response = urllib2.urlopen(fulluri).read() | 34 response = urllib2.urlopen(fulluri).read() |
25 except Exception as e: | 35 except Exception as e: |
26 raise urllib2.HTTPError(e.url, e.code, e.msg, e.hdrs, e.fp) | 36 raise urllib2.HTTPError(e.url, e.code, e.msg, e.hdrs, e.fp) |
27 if str(response.strip()) == "": | 37 if str(response.strip()) == "": |
28 return None | 38 return "" # change return from None to "" for easily writing to file |
29 return response | 39 return response |
30 | 40 |
31 | 41 |
32 if __name__ == "__main__": | 42 if __name__ == "__main__": |
33 from optparse import OptionParser | 43 from optparse import OptionParser |