Hi,
I’m trying to access OpenTargets data through API but keep getting a 400 error. I’ve tried two approaches getting the same error:
-
First, I constructed the query using the playground and then adapted it as an R script following the examples provided at the EBI OpenTargets API training course from September 2021. I got the error 400.
-
Then, to check if the error was related to the script I had created, I downloaded the example scripts provided at that same training course, getting the same error.
I’ll post here both scripts:
EBI training example:
# Install relevant library for HTTP requests
library(httr)
# Set gene_id variable
gene_id <- "ENSG00000091831"
# Build query string
query_string = "
query target($ensemblId: String!){
target(ensemblId: $ensemblId){
id
approvedSymbol
approvedName
bioType
genomicLocation{
chromosome
start
end
strand
}
}
}
"
# Set base URL of GraphQL API endpoint
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"
# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = gene_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 data to RStudio console
print(content(r)$data)
The query I’m interested in:
library(httr)
ensemblId <- "ENSG00000064012"
query_string <- "
query search{
target($ensemblId: String!) {
functionDescriptions
knownDrugs {
uniqueDrugs
count
rows {
drugType
mechanismOfAction
disease {
name
}
drug{
name
id
isApproved
}
}
}
}
}
"
base_url <- "https://api.platform.opentargets.org/api/v4/graphql"
# Set variables object of arguments to be passed to endpoint
variables <- list("ensemblId" = ensemblId)
# 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 data to RStudio console
print(content(r)$data)