Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 18 additions & 14 deletions R/chembl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"),
Expand All @@ -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,
Expand All @@ -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)) {
Expand Down Expand Up @@ -488,7 +491,6 @@ chembl_validate_query <- function(query, resource, verbose) {
"activity",
"binding_site",
"compund_record",
"compound_structural_alert",
"drug_indication",
"drug_warning",
"mechanism",
Expand All @@ -506,6 +508,7 @@ chembl_validate_query <- function(query, resource, verbose) {
"assay",
"biotherapeutic",
"chembl_id_lookup",
"compound_structural_alert",
"document",
"document_similarity",
"drug",
Expand Down Expand Up @@ -621,6 +624,7 @@ format_chembl <- function(cont) {
"biotherapeutic",
"biocomponents",
"chembl_release",
"compound_structural_alerts",
"cross_references",
"go_slims",
"indication_refs",
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions man/chembl_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions tests/testthat/test-chembl.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down