Skip to content

Commit 692909d

Browse files
authored
Merge pull request #446 from stitam/stralerts
Fix chembl_query() for the compound structural alerts resource
2 parents 5dc2797 + e1d6695 commit 692909d

4 files changed

Lines changed: 33 additions & 20 deletions

File tree

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
## MINOR IMPROVEMENTS
1212

1313
* `chembl_query()` now returns a named list and uses better formatting for nested output.
14+
* 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.
15+
16+
## BUG FIXES
17+
18+
* `chembl_query()` did not work with the "compound_structural_alerts" resource. This has been fixed.
1419

1520
# webchem 1.3.1
1621

R/chembl.R

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ chembl_files <- function(version = "latest") {
191191
#' @param query character; a vector of ChEMBL IDs.
192192
#' @param resource character; the ChEMBL resource to query. Use
193193
#' [chembl_resources()] to see all available resources.
194+
#' @param tidy logical; attempt to convert output to a simpler structure.
194195
#' @param cache_file character; the name of the cache file without the file
195196
#' extension. If \code{NULL}, results are not cached.
196197
#' @param verbose logical; should a verbose output be printed on the console?
@@ -245,8 +246,8 @@ chembl_files <- function(version = "latest") {
245246
#' chembl_query("CHEMBL1", resource = "chembl_id_lookup")
246247
#' # Resource: compound_record - requires record ID
247248
#' chembl_query("1", resource = "compound_record")
248-
#' # Resource: compound_structural_alert - requires compound structural alert ID
249-
#' chembl_query("79048021", resource = "compound_structural_alert")
249+
#' # Resource: compound_structural_alert - requires compound ChEMBL ID
250+
#' chembl_query("CHEMBL266429", resource = "compound_structural_alert")
250251
#' # Resource: document - requires document ChEMBL ID
251252
#' chembl_query("CHEMBL1158643", resource = "document")
252253
#' # Resource: document_similarity - requires document 1 ChEMBL ID
@@ -294,6 +295,7 @@ chembl_files <- function(version = "latest") {
294295
#' @export
295296
chembl_query <- function(query,
296297
resource = "molecule",
298+
tidy = TRUE,
297299
cache_file = NULL,
298300
similarity = 70,
299301
verbose = getOption("verbose"),
@@ -317,17 +319,16 @@ chembl_query <- function(query,
317319
query <- chembl_validate_query(query, resource, verbose)
318320
if (is.na(query)) return(NA)
319321
if (verbose) webchem_message("query", query, appendLF = FALSE)
320-
if (resource == "similarity") {
321-
url <- ifelse(
322-
test_service_down, "",
323-
paste0(stem, "/", resource, "/", query, "/", similarity, ".json")
324-
)
322+
if (test_service_down) {
323+
url <- ""
324+
} else if (resource == "similarity") {
325+
url <- paste0(stem, "/", resource, "/", query, "/", similarity, ".json")
326+
} else if (resource == "compound_structural_alert") {
327+
url <- paste0(stem, "/", resource, ".json?molecule_chembl_id=", query)
325328
} else {
326-
url <- ifelse(
327-
test_service_down, "",
328-
paste0(stem, "/", resource, "/", query, ".json")
329-
)
329+
url <- paste0(stem, "/", resource, "/", query, ".json")
330330
}
331+
331332
webchem_sleep(type = "API")
332333
res <- try(httr::RETRY("GET",
333334
url,
@@ -344,7 +345,9 @@ chembl_query <- function(query,
344345
}
345346
if (verbose) message(httr::message_for_status(res))
346347
cont <- httr::content(res, type = "application/json")
347-
cont <- format_chembl(cont)
348+
if (tidy) {
349+
cont <- format_chembl(cont)
350+
}
348351
return(cont)
349352
}
350353
if (is.null(cache_file)) {
@@ -488,7 +491,6 @@ chembl_validate_query <- function(query, resource, verbose) {
488491
"activity",
489492
"binding_site",
490493
"compund_record",
491-
"compound_structural_alert",
492494
"drug_indication",
493495
"drug_warning",
494496
"mechanism",
@@ -506,6 +508,7 @@ chembl_validate_query <- function(query, resource, verbose) {
506508
"assay",
507509
"biotherapeutic",
508510
"chembl_id_lookup",
511+
"compound_structural_alert",
509512
"document",
510513
"document_similarity",
511514
"drug",
@@ -621,6 +624,7 @@ format_chembl <- function(cont) {
621624
"biotherapeutic",
622625
"biocomponents",
623626
"chembl_release",
627+
"compound_structural_alerts",
624628
"cross_references",
625629
"go_slims",
626630
"indication_refs",
@@ -844,7 +848,7 @@ chembl_example_query <- function(resource) {
844848
cell_line = c("CHEMBL3307241", "CHEMBL3307242"),
845849
chembl_id_lookup = "CHEMBL1",
846850
compound_record = "1",
847-
compound_structural_alert = "79048021",
851+
compound_structural_alert = "CHEMBL266429",
848852
document = c("CHEMBL1158643", "CHEMBL1132398", "CHEMBL5303573", "CHEMBL3639173"),
849853
document_similarity = "CHEMBL1148466",
850854
drug = "CHEMBL2",

man/chembl_query.Rd

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-chembl.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ test_that("chembl_query() examples", {
6363
o7 <- chembl_query("CHEMBL1", resource = "chembl_id_lookup")
6464
# Resource: compound_record - requires record ID
6565
o8 <- chembl_query("1", resource = "compound_record")
66-
# Resource: compound_structural_alert - requires compound structural alert ID
67-
o9 <- chembl_query("79048021", resource = "compound_structural_alert")
66+
# Resource: compound_structural_alert - requires compound ChEMBL ID
67+
o9 <- chembl_query("CHEMBL266429", resource = "compound_structural_alert", tidy = FALSE)
6868
# Resource: document - requires document ChEMBL ID
6969
o10 <- chembl_query("CHEMBL1158643", resource = "document")
7070
# Resource: document_similarity - requires document 1 ChEMBL ID
@@ -91,7 +91,8 @@ test_that("chembl_query() examples", {
9191
# Resource: protein_classification - requires protein class ID
9292
o22 <- chembl_query("1", resource = "protein_classification")
9393
# Resource: similarity - requires SMILES
94-
o23 <- chembl_query("CC(=O)Oc1ccccc1C(=O)O", resource = "similarity")
94+
o23 <- suppressWarnings(
95+
chembl_query("CC(=O)Oc1ccccc1C(=O)O", resource = "similarity"))
9596
# Resource: source - requires source ID
9697
o24 <- chembl_query("1", resource = "source")
9798
# Resource: substructure - requires SMILES
@@ -119,7 +120,7 @@ test_that("chembl_query() examples", {
119120
expect_true(inherits(o6, "list") & length(o6[[1]]) == 11)
120121
expect_true(inherits(o7, "list") & length(o7[[1]]) == 5)
121122
expect_true(inherits(o8, "list") & length(o8[[1]]) == 6)
122-
#expect_true(inherits(o9, "list") & length(o9[[1]]) == 3)
123+
expect_true(inherits(o9, "list") & length(o9[[1]][[1]]) == 7)
123124
expect_true(inherits(o10, "list") & length(o10[[1]]) == 19)
124125
expect_true(inherits(o11, "list") & length(o11[[1]]) == 4)
125126
expect_true(inherits(o12, "list") & length(o12[[1]]) == 30)

0 commit comments

Comments
 (0)