GraphQL API
The Open Targets Genetics API is a GraphQL API that powers the user interface and that supports language-agnostic access to our data.
Compared to a REST API, our GraphQL API offers five key benefits:
- 1.You can construct a query that returns only the fields that you need
- 2.You can build graphical queries that traverse a data graph through resolvable entities and this reduces the need for multiple queries
- 3.You can access the GraphQL API playground with built-in documentation to build your query and run it in real-time
- 4.You can view the schema that shows the available fields for each object along with a description and data type attribute
- 5.You only have to use
POST
requests with a query string and variables object
The base URL endpoint for the Genetics Portal GraphQL API is:
https://api.genetics.opentargets.org/graphql
Our GraphQL API supports queries for a single variant, gene, study, or study-variant pair. For more systematic queries (e.g. for multiple variants), please use our data downloads or our Google BigQuery instance - open-targets-prod.
GraphQL's query strings and variables object constructs, you can also access the data using any programming language that supports HTTP
POST
requests. While this is a valid approach, we discourage users from repeatedly querying the GraphQL API one entity at a time. Instead, our comprehensive datasets available for download provide a simpler and more performant strategy to achieve the same result.Python
R
#!/usr/bin/env python3
# Import relevant libraries for HTTP request and JSON formatting
import requests
import json
# Set study_id and variant_id variables
study_id = "GCST009240_9"
variant_id = "1_55052794_A_G"
# Build query string
query_string = """
query genePrioritisationUsingL2G($myVariantId: String!, $myStudyId: String! ){
studyLocus2GeneTable(studyId: $myStudyId, variantId: $myVariantId){
rows {
gene {
symbol
id
}
yProbaModel
yProbaDistance
yProbaInteraction
yProbaMolecularQTL
yProbaPathogenicity
hasColoc
distanceToLocus
}
}
}
"""
# Set variables object of arguments to be passed to endpoint
variables = {"myVariantId": variant_id, "myStudyId": study_id}
# Set base URL of Genetics Portal GraphQL API endpoint
base_url = "https://api.genetics.opentargets.org/graphql"
# Perform POST request and check status code of response
r = requests.post(base_url, json={"query": query_string, "variables": variables})
print(r.status_code)
# Transform API response into JSON
api_response_as_json = json.loads(r.text)
# Print first element of JSON response data
print(api_response_as_json["data"]["studyLocus2GeneTable"]["rows"][0])
# Install relevant library for HTTP requests
library(httr)
# Set study_id and variant_id variables
study_id <- "GCST009240_9"
variant_id <- "1_55052794_A_G"
# Build query string
query_string = "
query genePrioritisationUsingL2G($myVariantId: String!, $myStudyId: String! ){
studyLocus2GeneTable(studyId: $myStudyId, variantId: $myVariantId){
rows {
gene {
symbol
id
}
yProbaModel
yProbaDistance
yProbaInteraction
yProbaMolecularQTL
yProbaPathogenicity
hasColoc
distanceToLocus
}
}
}
"
# Set base URL of Genetics Portal GraphQL API endpoint
base_url <- "https://api.genetics.opentargets.org/graphql"
# Set variables object of arguments to be passed to endpoint
variables <- list("myStudyId" = study_id, "myVariantId" = variant_id)
# Construct POST request body object with query string and variables
post_body <- list(query = query_string, variables = variables)
# Perform POST request
r <- POST(url=base_url, body=post_body, encode='json')
# Print first entry of L2G data to RStudio console
head(content(r)$data$studyLocus2GeneTable$rows, 1)
For more information on how to use the GraphQL API and example queries based on actual use cases and research questions, check out the Open Targets Community.
Last modified 7mo ago