| ... | ... | @@ -2,11 +2,19 @@ |
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import urllib
|
|
|
|
import urllib2
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
|
|
|
|
# Python 2/3 adaptability
|
|
|
|
try:
|
|
|
|
from urllib.parse import urlparse, urlencode
|
|
|
|
from urllib.request import urlopen, Request
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
except ImportError:
|
|
|
|
from urlparse import urlparse
|
|
|
|
from urllib import urlencode
|
|
|
|
from urllib2 import urlopen, Request, HTTPError
|
|
|
|
|
|
|
|
class EnsemblRestClient(object):
|
|
|
|
def __init__(self, server='http://rest.ensembl.org', reqs_per_sec=15):
|
|
|
|
self.server = server
|
| ... | ... | @@ -22,7 +30,7 @@ class EnsemblRestClient(object): |
|
|
|
hdrs['Content-Type'] = 'application/json'
|
|
|
|
|
|
|
|
if params:
|
|
|
|
endpoint += '?' + urllib.urlencode(params)
|
|
|
|
endpoint += '?' + urlencode(params)
|
|
|
|
|
|
|
|
data = None
|
|
|
|
|
| ... | ... | @@ -35,14 +43,14 @@ class EnsemblRestClient(object): |
|
|
|
self.req_count = 0
|
|
|
|
|
|
|
|
try:
|
|
|
|
request = urllib2.Request(self.server + endpoint, headers=hdrs)
|
|
|
|
response = urllib2.urlopen(request)
|
|
|
|
request = Request(self.server + endpoint, headers=hdrs)
|
|
|
|
response = urlopen(request)
|
|
|
|
content = response.read()
|
|
|
|
if content:
|
|
|
|
data = json.loads(content)
|
|
|
|
self.req_count += 1
|
|
|
|
|
|
|
|
except urllib2.HTTPError, e:
|
|
|
|
except HTTPError as e:
|
|
|
|
# check if we are being rate limited by the server
|
|
|
|
if e.code == 429:
|
|
|
|
if 'Retry-After' in e.headers:
|
| ... | ... | @@ -56,7 +64,7 @@ class EnsemblRestClient(object): |
|
|
|
|
|
|
|
def get_variants(self, species, symbol):
|
|
|
|
genes = self.perform_rest_action(
|
|
|
|
'/xrefs/symbol/{0}/{1}'.format(species, symbol),
|
|
|
|
endpoint='/xrefs/symbol/{0}/{1}'.format(species, symbol),
|
|
|
|
params={'object_type': 'gene'}
|
|
|
|
)
|
|
|
|
if genes:
|
| ... | ... | @@ -74,7 +82,7 @@ def run(species, symbol): |
|
|
|
variants = client.get_variants(species, symbol)
|
|
|
|
if variants:
|
|
|
|
for v in variants:
|
|
|
|
print '{seq_region_name}:{start}-{end}:{strand} ==> {id} ({consequence_type})'.format(**v);
|
|
|
|
print('{seq_region_name}:{start}-{end}:{strand} ==> {id} ({consequence_type})'.format(**v))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) == 3:
|
| ... | ... | |