英文:
How to run a AR query for multiple arguments of a table or list (lets say we have a column with IDs) in R
问题
Install relevant library for HTTP requests
library(httr)
Set gene_id variable for AR (androgen receptor)
gene_id <- "ENSG00000169083"
Build query string to get general information about AR and genetic constraint and tractability assessments
query_string = "
query target($ensemblId: String!){
target(ensemblId: $ensemblId){
id
approvedSymbol
biotype
geneticConstraint {
constraintType
exp
obs
score
oe
oeLower
oeUpper
}
tractability {
id
modality
value
}
}
}
"
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)
英文:
How to modify this script to read form a list of IDs and return the results ?
# Install relevant library for HTTP requests
library(httr)
# Set gene_id variable for AR (androgen receptor)
gene_id <- "ENSG00000169083"
# Build query string to get general information about AR and genetic constraint and tractability assessments
query_string = "
query target($ensemblId: String!){
target(ensemblId: $ensemblId){
id
approvedSymbol
biotype
geneticConstraint {
constraintType
exp
obs
score
oe
oeLower
oeUpper
}
tractability {
id
modality
value
}
}
}
"
# 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)
I tried just a simple query from the documentation of Open Targets. Their graphQL API doesn't support multiple IDs.
答案1
得分: 0
遍历多个 ID 可能会像这样完成:
IDs <- c("ENSG00000169083", "ENSG00000169084", ...)
alldata <- lapply(IDs, function(gene_id) {
post_body <- list(query = query_string, variables = list("ensemblId" = gene_id))
res <- tryCatch(
POST(url=base_url, body=post_body, encode='json'),
error = function(e) e)
if (inherits(res, "error")) {
warning("基因", sQuote(gene_id, FALSE), "出错: ",
conditionMessage(res), call. = FALSE)
res <- NULL
} else if (status_code(res) != 200) {
warning("基因", sQuote(gene_id, FALSE), "返回异常: ",
status_code(res), call. = FALSE)
res <- NULL
} else {
res <- content(res)$data
}
res
})
从这里开始,你应该有一个包含每个 ID 一个元素的列表,然后你可以组合数据(使用 rbind
、bind_rows
、rbindlist
,或者可能使用一些特定于列表的方法)。
英文:
Iterating over multiple IDs might be done like this:
IDs <- c("ENSG00000169083", "ENSG00000169084", ...)
alldata <- lapply(IDs, function(gene_id) {
post_body <- list(query = query_string, variables = list("ensemblId" = gene_id))
res <- tryCatch(
POST(url=base_url, body=post_body, encode='json'),
error = function(e) e)
if (inherits(res, "error")) {
warning("error for gene ", sQuote(gene_id, FALSE), ": ",
conditionMessage(res), call. = FALSE)
res <- NULL
} else if (status_code(res) != 200) {
warning("abnormal return for gene ", sQuote(gene_id, FALSE), ": ",
status_code(res), call. = FALSE)
res <- NULL
} else {
res <- content(res)$data
}
res
})
From here, you should have a list
with one element per ID, over to you on combining the data (whether rbind
, bind_rows
, rbindlist
, or something list
-specific perhaps).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论