diff --git a/NEWS.md b/NEWS.md index cf6971d2..6d110a59 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,11 @@ ## MINOR IMPROVEMENTS * `chembl_query()` now returns a named list and uses better formatting for nested output. +* Added new argument `tidy = TRUE` to `chembl_query()` so we can now control whether we want to try to convert the output to a flat format. + +## BUG FIXES + +* `chembl_query()` did not work with the "compound_structural_alerts" resource. This has been fixed. # webchem 1.3.1 diff --git a/R/chembl.R b/R/chembl.R index b3625983..a214b630 100644 --- a/R/chembl.R +++ b/R/chembl.R @@ -191,6 +191,7 @@ chembl_files <- function(version = "latest") { #' @param query character; a vector of ChEMBL IDs. #' @param resource character; the ChEMBL resource to query. Use #' [chembl_resources()] to see all available resources. +#' @param tidy logical; attempt to convert output to a simpler structure. #' @param cache_file character; the name of the cache file without the file #' extension. If \code{NULL}, results are not cached. #' @param verbose logical; should a verbose output be printed on the console? @@ -245,8 +246,8 @@ chembl_files <- function(version = "latest") { #' chembl_query("CHEMBL1", resource = "chembl_id_lookup") #' # Resource: compound_record - requires record ID #' chembl_query("1", resource = "compound_record") -#' # Resource: compound_structural_alert - requires compound structural alert ID -#' chembl_query("79048021", resource = "compound_structural_alert") +#' # Resource: compound_structural_alert - requires compound ChEMBL ID +#' chembl_query("CHEMBL266429", resource = "compound_structural_alert") #' # Resource: document - requires document ChEMBL ID #' chembl_query("CHEMBL1158643", resource = "document") #' # Resource: document_similarity - requires document 1 ChEMBL ID @@ -294,6 +295,7 @@ chembl_files <- function(version = "latest") { #' @export chembl_query <- function(query, resource = "molecule", + tidy = TRUE, cache_file = NULL, similarity = 70, verbose = getOption("verbose"), @@ -317,17 +319,16 @@ chembl_query <- function(query, query <- chembl_validate_query(query, resource, verbose) if (is.na(query)) return(NA) if (verbose) webchem_message("query", query, appendLF = FALSE) - if (resource == "similarity") { - url <- ifelse( - test_service_down, "", - paste0(stem, "/", resource, "/", query, "/", similarity, ".json") - ) + if (test_service_down) { + url <- "" + } else if (resource == "similarity") { + url <- paste0(stem, "/", resource, "/", query, "/", similarity, ".json") + } else if (resource == "compound_structural_alert") { + url <- paste0(stem, "/", resource, ".json?molecule_chembl_id=", query) } else { - url <- ifelse( - test_service_down, "", - paste0(stem, "/", resource, "/", query, ".json") - ) + url <- paste0(stem, "/", resource, "/", query, ".json") } + webchem_sleep(type = "API") res <- try(httr::RETRY("GET", url, @@ -344,7 +345,9 @@ chembl_query <- function(query, } if (verbose) message(httr::message_for_status(res)) cont <- httr::content(res, type = "application/json") - cont <- format_chembl(cont) + if (tidy) { + cont <- format_chembl(cont) + } return(cont) } if (is.null(cache_file)) { @@ -488,7 +491,6 @@ chembl_validate_query <- function(query, resource, verbose) { "activity", "binding_site", "compund_record", - "compound_structural_alert", "drug_indication", "drug_warning", "mechanism", @@ -506,6 +508,7 @@ chembl_validate_query <- function(query, resource, verbose) { "assay", "biotherapeutic", "chembl_id_lookup", + "compound_structural_alert", "document", "document_similarity", "drug", @@ -621,6 +624,7 @@ format_chembl <- function(cont) { "biotherapeutic", "biocomponents", "chembl_release", + "compound_structural_alerts", "cross_references", "go_slims", "indication_refs", @@ -844,7 +848,7 @@ chembl_example_query <- function(resource) { cell_line = c("CHEMBL3307241", "CHEMBL3307242"), chembl_id_lookup = "CHEMBL1", compound_record = "1", - compound_structural_alert = "79048021", + compound_structural_alert = "CHEMBL266429", document = c("CHEMBL1158643", "CHEMBL1132398", "CHEMBL5303573", "CHEMBL3639173"), document_similarity = "CHEMBL1148466", drug = "CHEMBL2", diff --git a/man/chembl_query.Rd b/man/chembl_query.Rd index 81608310..8e8c0748 100644 --- a/man/chembl_query.Rd +++ b/man/chembl_query.Rd @@ -7,6 +7,7 @@ chembl_query( query, resource = "molecule", + tidy = TRUE, cache_file = NULL, similarity = 70, verbose = getOption("verbose"), @@ -19,6 +20,8 @@ chembl_query( \item{resource}{character; the ChEMBL resource to query. Use [chembl_resources()] to see all available resources.} +\item{tidy}{logical; attempt to convert output to a simpler structure.} + \item{cache_file}{character; the name of the cache file without the file extension. If \code{NULL}, results are not cached.} @@ -83,8 +86,8 @@ chembl_query("CHEMBL3307241", resource = "cell_line") chembl_query("CHEMBL1", resource = "chembl_id_lookup") # Resource: compound_record - requires record ID chembl_query("1", resource = "compound_record") -# Resource: compound_structural_alert - requires compound structural alert ID -chembl_query("79048021", resource = "compound_structural_alert") +# Resource: compound_structural_alert - requires compound ChEMBL ID +chembl_query("CHEMBL266429", resource = "compound_structural_alert") # Resource: document - requires document ChEMBL ID chembl_query("CHEMBL1158643", resource = "document") # Resource: document_similarity - requires document 1 ChEMBL ID diff --git a/tests/testthat/test-chembl.R b/tests/testthat/test-chembl.R index fa7bbe90..b955ced1 100644 --- a/tests/testthat/test-chembl.R +++ b/tests/testthat/test-chembl.R @@ -63,8 +63,8 @@ test_that("chembl_query() examples", { o7 <- chembl_query("CHEMBL1", resource = "chembl_id_lookup") # Resource: compound_record - requires record ID o8 <- chembl_query("1", resource = "compound_record") - # Resource: compound_structural_alert - requires compound structural alert ID - o9 <- chembl_query("79048021", resource = "compound_structural_alert") + # Resource: compound_structural_alert - requires compound ChEMBL ID + o9 <- chembl_query("CHEMBL266429", resource = "compound_structural_alert", tidy = FALSE) # Resource: document - requires document ChEMBL ID o10 <- chembl_query("CHEMBL1158643", resource = "document") # Resource: document_similarity - requires document 1 ChEMBL ID @@ -91,7 +91,8 @@ test_that("chembl_query() examples", { # Resource: protein_classification - requires protein class ID o22 <- chembl_query("1", resource = "protein_classification") # Resource: similarity - requires SMILES - o23 <- chembl_query("CC(=O)Oc1ccccc1C(=O)O", resource = "similarity") + o23 <- suppressWarnings( + chembl_query("CC(=O)Oc1ccccc1C(=O)O", resource = "similarity")) # Resource: source - requires source ID o24 <- chembl_query("1", resource = "source") # Resource: substructure - requires SMILES @@ -119,7 +120,7 @@ test_that("chembl_query() examples", { expect_true(inherits(o6, "list") & length(o6[[1]]) == 11) expect_true(inherits(o7, "list") & length(o7[[1]]) == 5) expect_true(inherits(o8, "list") & length(o8[[1]]) == 6) - #expect_true(inherits(o9, "list") & length(o9[[1]]) == 3) + expect_true(inherits(o9, "list") & length(o9[[1]][[1]]) == 7) expect_true(inherits(o10, "list") & length(o10[[1]]) == 19) expect_true(inherits(o11, "list") & length(o11[[1]]) == 4) expect_true(inherits(o12, "list") & length(o12[[1]]) == 30)