Skip to content

Instantly share code, notes, and snippets.

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=""):
# The server gives a generic error message if the content type isn't
# explicitly set
headers = {"Content-Type": "application/json"}
response =, 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) {
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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment