POST allows you to run a query with multiple inputs at once. The output will be a dictionary of dictionaries.
import requests, sys
from pprint import pprint
server = "http://rest.ensembl.org"
ext = "/lookup/id"
headers={ "Content-Type" : "application/json", "Accept" : "application/json"}
r = requests.post(server+ext, headers=headers, data='{ "ids" : ["ENSG00000157764", "ENSG00000248378" ] }')
# error checking removed for space
decoded = r.json()
pprint (decoded)
There is a helper function in POST. You can specify both helper functions in your script and use whichever one you need.
def fetch_endpoint_POST(server, request, data, content_type='application/json'):
r = requests.post(server+request,
headers={ "Accept" : content_type},
data=data )
if not r.ok:
r.raise_for_status()
sys.exit()
if content_type == 'application/json':
return r.json()
else:
return r.text
In order to add optional parameters to your POST query, you can just add them onto the extention with a slash. For example if you wanted to mask UTRs when running the sequence_id_post endpoint, you could specify your extension as:
`ext = "sequence/id/mask_feature=1"`
Your input list for POST queries need to be a JSON list. You can create this from a list in Python using the JSON module:
`data = json.dumps({ "ids" : my_list })`
The Output from POST queries will be a dictionary of dictionaries. To access items, you could use your input list as your keys, or you could move through the dictionary with:
`for key, value in post_query.items():`
The following scripts inputs a list of variants in HGVS format into the VEP and gets out the IDs of known colocated variants, including failed variants (an optional parameter):
import requests, sys, json
from pprint import pprint
def fetch_endpoint(server, request, content_type):
r = requests.get(server+request, headers={ "Accept" : content_type})
if not r.ok:
r.raise_for_status()
sys.exit()
if content_type == 'application/json':
return r.json()
else:
return r.text
def fetch_endpoint_POST(server, request, data, content_type):
r = requests.post(server+request,
headers={ "Accept" : content_type},
data=data )
if not r.ok:
r.raise_for_status()
sys.exit()
if content_type == 'application/json':
return r.json()
else:
return r.text
# define the server, extension and content type
server = "http://rest.ensembl.org/"
con = "application/json"
vep_ext = "vep/homo_sapiens/hgvs/failed=1"
# create the list of HGVS annotations
hgvs = ["ENST00000366667.4:c.803T>C", "ENST00000335295.4:c.20A>T", "ENST00000415952.1:c.-149-34206G>T"]
# convert the list into json format
hgvs_json = json.dumps({ "hgvs_notations" : hgvs })
# run the query
post_vep = fetch_endpoint_POST(server, vep_ext, hgvs_json, con)
# move through the results
for variant in post_vep:
# get the data
input = variant['input']
colocated_list = []
for colocated in variant['colocated_variants']:
colocated_list.append(colocated['id'])
print (input + ": " + (', '.join(colocated_list)))
1. Fetch the all the transcripts of ESPN using the lookup endpoint. Fetch the cDNA sequences of all transcripts using a single POST request, and print in FASTA format.
# Exercise 6.1
2. You have the following list of variants:
rs1415919662, rs957333053, rs762944488, rs1372123943, rs553810871, rs1451237599, rs751376931
Get the variant class, evidence attributes, source and the most_severe_consequence for all variants using the variation POST endpoint.
# Exercise 6.2