-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ensembl_Methods.py
executable file
·62 lines (47 loc) · 1.87 KB
/
Ensembl_Methods.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
### Ensembl Client
### Version: Beta
### Python : 2.7
### Author : [email protected]
### API : http://rest.ensembl.org/
import sys
import httplib2
import json
http = httplib2.Http(".cache")
server = "http://rest.ensembl.org"
def check_response(resp):
if not resp.status == 200:
print("Invalid response: ", resp.status)
sys.exit()
def decode(content):
decoded = json.loads(content)
print repr(decoded)
def request(server, ext, method="GET", content_type="application/json"):
return http.request(server+ext, method, headers={"Content-Type":content_type})
def get_archive(ensembl_stable_id): # ...callback=None):
'''Uses the given identifier to return the archived sequence'''
ext = "/archive/id/"+ensembl_stable_id+"?"
resp, content = request(server, ext)
check_response(resp)
decode(content)
#Example:
#get_archive('ENSG00000157764')
def get_gentree(ensembl_gentree_id): # ...aligned=0, callback=None, compara='multi', nh_format='simple', sequence='protein'):
'''Retrieves a gene tree dump for a gene tree stable identifier'''
ext = "/genetree/id/"+ensembl_gentree_id+"?"
resp, content = request(server, ext)
check_response(resp)
decode(content)
#Example
#get_gentree('ENSGT00390000003602')
def get_xrefs(species, symbol): # ...callback=None, db_type='core', external_db=None, object_type=None):
'''Looks up an external symbol and returns all Ensembl objects linked to it.
This can be a display name for a gene/transcript/translation,
a synonym or an externally linked reference.
If a gene's transcript is linked to the supplied symbol
the service will return both gene and transcript (it supports transient links).'''
ext = "/xrefs/symbol/homo_sapiens/BRCA2?"
resp, content = request(server, ext)
check_response(resp)
decode(content)
#Example:
#get_xrefs('homo_sapiens','BRCA2')