-
Notifications
You must be signed in to change notification settings - Fork 1
/
restclient.py
83 lines (68 loc) · 2.54 KB
/
restclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sys
import urllib
import urllib2
import json
import time
class EnsemblRestClient(object):
def __init__(self, server='http://beta.rest.ensembl.org', reqs_per_sec=3):
self.server = server
self.reqs_per_sec = reqs_per_sec
self.req_count = 0
self.last_req = 0
def perform_rest_action(self, endpoint, hdrs=None, params=None):
if hdrs is None:
hdrs = {}
if 'Content-Type' not in hdrs:
hdrs['Content-Type'] = 'application/json'
if params:
endpoint += '?' + urllib.urlencode(params)
data = None
# check if we need to rate limit ourselves
if self.req_count >= self.reqs_per_sec:
delta = time.time() - self.last_req
if delta < 1:
time.sleep(1 - delta)
self.last_req = time.time()
self.req_count = 0
try:
request = urllib2.Request(self.server + endpoint, headers=hdrs)
response = urllib2.urlopen(request)
content = response.read()
if content:
data = json.loads(content)
self.req_count += 1
except urllib2.HTTPError, e:
# check if we are being rate limited by the server
if e.code == 429:
if 'Retry-After' in e.headers:
retry = e.headers['Retry-After']
time.sleep(float(retry))
self.perform_rest_action(endpoint, hdrs, params)
else:
sys.stderr.write('Request failed for {0}: Status code: {1.code} Reason: {1.reason}\n'.format(endpoint, e))
return data
def get_variants(self, species, symbol):
genes = self.perform_rest_action(
'/xrefs/symbol/{0}/{1}'.format(species, symbol),
params={'object_type': 'gene'}
)
if genes:
stable_id = genes[0]['id']
variants = self.perform_rest_action(
'/feature/id/{0}'.format(stable_id),
params={'feature': 'variation'}
)
return variants
return None
def run(species, symbol):
client = EnsemblRestClient()
variants = client.get_variants(species, symbol)
if variants:
for v in variants:
print '{seq_region_name}:{start}-{end}:{strand} ==> {ID} ({consequence_type})'.format(**v);
if __name__ == '__main__':
if len(sys.argv) == 3:
species, symbol = sys.argv[1:]
else:
species, symbol = 'human', 'BRAF'
run(species, symbol)