How to run a AR query for multiple arguments of a table or list (lets say we have a column with IDs) in R

huangapple go评论85阅读模式
英文:

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 ?

  1. # Install relevant library for HTTP requests
  2. library(httr)
  3. # Set gene_id variable for AR (androgen receptor)
  4. gene_id &lt;- &quot;ENSG00000169083&quot;
  5. # Build query string to get general information about AR and genetic constraint and tractability assessments
  6. query_string = &quot;
  7. query target($ensemblId: String!){
  8. target(ensemblId: $ensemblId){
  9. id
  10. approvedSymbol
  11. biotype
  12. geneticConstraint {
  13. constraintType
  14. exp
  15. obs
  16. score
  17. oe
  18. oeLower
  19. oeUpper
  20. }
  21. tractability {
  22. id
  23. modality
  24. value
  25. }
  26. }
  27. }
  28. &quot;
  29. # Set base URL of GraphQL API endpoint
  30. base_url &lt;- &quot;https://api.platform.opentargets.org/api/v4/graphql&quot;
  31. # Set variables object of arguments to be passed to endpoint
  32. variables &lt;- list(&quot;ensemblId&quot; = gene_id)
  33. # Construct POST request body object with query string and variables
  34. post_body &lt;- list(query = query_string, variables = variables)
  35. # Perform POST request
  36. r &lt;- POST(url=base_url, body=post_body, encode=&#39;json&#39;)
  37. # Print data to RStudio console
  38. 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 可能会像这样完成:

  1. IDs <- c("ENSG00000169083", "ENSG00000169084", ...)
  2. alldata <- lapply(IDs, function(gene_id) {
  3. post_body <- list(query = query_string, variables = list("ensemblId" = gene_id))
  4. res <- tryCatch(
  5. POST(url=base_url, body=post_body, encode='json'),
  6. error = function(e) e)
  7. if (inherits(res, "error")) {
  8. warning("基因", sQuote(gene_id, FALSE), "出错: ",
  9. conditionMessage(res), call. = FALSE)
  10. res <- NULL
  11. } else if (status_code(res) != 200) {
  12. warning("基因", sQuote(gene_id, FALSE), "返回异常: ",
  13. status_code(res), call. = FALSE)
  14. res <- NULL
  15. } else {
  16. res <- content(res)$data
  17. }
  18. res
  19. })

从这里开始,你应该有一个包含每个 ID 一个元素的列表,然后你可以组合数据(使用 rbindbind_rowsrbindlist,或者可能使用一些特定于列表的方法)。

英文:

Iterating over multiple IDs might be done like this:

  1. IDs &lt;- c(&quot;ENSG00000169083&quot;, &quot;ENSG00000169084&quot;, ...)
  2. alldata &lt;- lapply(IDs, function(gene_id) {
  3. post_body &lt;- list(query = query_string, variables = list(&quot;ensemblId&quot; = gene_id))
  4. res &lt;- tryCatch(
  5. POST(url=base_url, body=post_body, encode=&#39;json&#39;),
  6. error = function(e) e)
  7. if (inherits(res, &quot;error&quot;)) {
  8. warning(&quot;error for gene &quot;, sQuote(gene_id, FALSE), &quot;: &quot;,
  9. conditionMessage(res), call. = FALSE)
  10. res &lt;- NULL
  11. } else if (status_code(res) != 200) {
  12. warning(&quot;abnormal return for gene &quot;, sQuote(gene_id, FALSE), &quot;: &quot;,
  13. status_code(res), call. = FALSE)
  14. res &lt;- NULL
  15. } else {
  16. res &lt;- content(res)$data
  17. }
  18. res
  19. })

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).

huangapple
  • 本文由 发表于 2023年3月31日 18:16:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定