Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gnomAD batch query example
#!/usr/bin/env python
import requests
import pprint
prettyprint = pprint.PrettyPrinter(indent=2).pprint
def fetch(jsondata, url="https://gnomad.broadinstitute.org/api"):
# The server gives a generic error message if the content type isn't
# explicitly set
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=jsondata, headers=headers)
json = response.json()
if "errors" in json:
raise Exception(str(json["errors"]))
return json
def get_variant_list(gene_id, dataset="gnomad_r2_1"):
# Note that this is GraphQL, not JSON.
fmt_graphql = """
{
gene(gene_id: "%s") {
variants(dataset: %s) {
consequence
pos
rsid
variant_id: variantId
}
}
}
"""
# This part will be JSON encoded, but with the GraphQL part left as a
# glob of text.
req_variantlist = {
"query": fmt_graphql % (gene_id, dataset),
"variables": {}
}
response = fetch(req_variantlist)
return response["data"]["gene"]["variants"]
prettyprint(get_variant_list("ENSG00000010610"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment