If you can pull a datapoint from the json, you can use it as input for another endpoint.
For example, this script gets the symbol of a gene then looks up xrefs associated with it:
import requests, 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
server = "http://rest.ensembl.org/"
gene_ext = "lookup/id/ENSG00000157764?"
con = "application/json"
get_gene = fetch_endpoint(server, gene_ext, con)
symbol = get_gene['display_name']
xrefs_ext = "xrefs/symbol/human/" + symbol + "?"
get_xrefs = fetch_endpoint(server, xrefs_ext, con)
pprint (get_xrefs)
1. Using the script from 3.1, add a call to fetch and print the sequence for the gene ESPN in FASTA.
# Exercise 5.1
2. Print the stable ID of any regulatory features that overlap the region 1000 bp upstream of the ESPN gene. (Hints: lookup the gene first to get the coordinates, then check the strand of the gene to see which way is upstream, and use this to create coordinates to query.)
# Exercise 5.2