Skip to content

Instantly share code, notes, and snippets.

@ressy
Last active April 14, 2022 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ressy/6fd7f6ee6401ac8e703dc2709399869e to your computer and use it in GitHub Desktop.
Save ressy/6fd7f6ee6401ac8e703dc2709399869e to your computer and use it in GitHub Desktop.
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", reference_genome: GRCh38) {
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