-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensembl.py
48 lines (36 loc) · 1.08 KB
/
ensembl.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
#!/usr/bin/env python
## ENSEMBL API
## ACCESS AND FUNCTIONS
# %% IMPORTS
import requests
# %% FUNCTIONS
def gen_query_url(endpoint ='lookup/symbol'):
'''
generates endpoint for symbol lookup
'''
scheme = 'http'
host = 'rest.ensembl.org'
url = '{}://{}/{}'.format(scheme, host, endpoint)
return url
def sym_lookup(url, sp, sym):
'''
Converts given HUGO symbol to ensembl ID
src: http://rest.ensembl.org/documentation/info/symbol_lookup
'''
url = url + f"/{sp}/{sym}"
payload = {"content-type" : "application/json", "expand": 0} ## set expand to 1 for more info
r = requests.get(url=url, params=payload )
# print(f"URL:{r.url}")
# print(f"BODY:{r.raw}")
r.encoding = 'utf-8'
jsn_res = r.json()
# print(f" JSON Res:{jsn_res}")
return jsn_res
# %% TEST
# symbol = "BRCA1"
# species = "human"
# url = gen_query_url(endpoint = 'lookup/symbol')
# sym_lookup(url, species, symbol)
# %% CHANGELOG
## v0.1 [12/26/2020]
## added basic rest url generator and symbol query