Skip to content

Commit 839b83f

Browse files
authored
Merge pull request #468 from stitam/chembl_versions
Improve handling of ChEMBL database versions
2 parents b0b8f8e + 366574c commit 839b83f

11 files changed

Lines changed: 174 additions & 107 deletions

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export(bcpc_query)
2929
export(cas)
3030
export(chebi_comp_entity)
3131
export(chembl_atc_classes)
32+
export(chembl_check_db_version)
3233
export(chembl_img)
3334
export(chembl_query)
3435
export(chembl_resources)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* `chembl_query()` can now perform both online queries (`mode = "ws"`, default) and offline retrievals (`mode = "offline"`) from a local ChEMBL database. Offline mode currently supports the following resources: `activity`, `assay`, `atc_class`, `binding_site`, `biotherapeutic`, `cell_line`, `chembl_id_lookup`, `compound_record`, `document`, `drug`,`drug_indication`, `drug_warning`, `go_slim`, `molecule`.
1313
* Results for online and offline queries are identical for most resources. If there are differences, the offline version throws informative warnings.
1414
* Added a new function `db_download_chembl()` for downloading ChEMBL for fully offline access.
15+
* Added a new function `chembl_check_db_version()` which retrieves a pinned default ChEMBL database version from .Renviron or .Rprofile and throws an error if none is set. Used for working with pinned versions of ChEMBL in offline mode.
1516

1617
## OTHER
1718

R/chembl.R

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#' Download ChEMBL database
22
#'
33
#' Download a version of the ChEMBL database for offline access.
4-
#' @param version character, the database release version.
4+
#' @param version character, the database release version. See Details for more
5+
#' information.
56
#' @param verbose logical; should verbose messages be printed to the console?
67
#' @return Downloads the requested database files.
8+
#' @details If \code{version = NULL} (default), the function calls
9+
#' [chembl_check_db_version()] to look for a pinned version to download, or
10+
#' stops with an error if it cannot find any. If \code{version = "latest"}, the
11+
#' function downloads the newest version currently published by ChEMBL. If a
12+
#' specific version is requested, the function downloads that version.
713
#' @note If a checksum file is available for the requested version it will be
814
#' used to check data integrity. To save storage space, webchem only retrieves
915
#' those files that are used by the package. If you need other files as well,
@@ -12,13 +18,27 @@
1218
#' \url{https://chembl.gitbook.io/chembl-interface-documentation/downloads}
1319
#' @examples
1420
#' \dontrun{
15-
#' db_download_chembl(version = "35", verbose = TRUE)
21+
#' db_download_chembl()
22+
#' db_download_chembl(version = "latest")
23+
#' db_download_chembl(version = "35")
1624
#' }
1725
#' @export
1826
db_download_chembl <- function(
19-
version = "latest",
27+
version = NULL,
2028
verbose = getOption("verbose")
2129
) {
30+
if (is.null(version)) {
31+
version <- try(chembl_check_db_version(), silent = TRUE)
32+
if (inherits(version, "try-error")) {
33+
stop("No default ChEMBL database version set. See ?db_download_chembl for more details.")
34+
}
35+
} else if (version == "latest") {
36+
status <- chembl_status(verbose = verbose)
37+
if (!is.list(status) && is.na(status)) {
38+
stop("Service not available.")
39+
}
40+
version <- strsplit(status$chembl_db_version, "_")[[1]][2]
41+
}
2242
# input validation
2343
if (!inherits(version, "chembl_version")) {
2444
version <- validate_chembl_version(version = version)
@@ -105,12 +125,11 @@ db_download_chembl <- function(
105125
#' @param version character; release version
106126
#' @return FTP URL for ChEMBL databas files
107127
#' @examples {
108-
#' chembl_dir_url(version = "latest")
109128
#' chembl_dir_url(version = "34")
110129
#' chembl_dir_url(version = "24.1")
111130
#' }
112131
#' @noRd
113-
chembl_dir_url <- function(version = "latest") {
132+
chembl_dir_url <- function(version) {
114133
if (!inherits(version, "chembl_version")) {
115134
version <- validate_chembl_version(version = version)
116135
}
@@ -135,17 +154,17 @@ chembl_dir_url <- function(version = "latest") {
135154

136155
#' Retrieve paths for ChEMBL database files
137156
#'
138-
#' @param version character; version of the database. Either "latest" (default)
139-
#' or a specific version number, e.g. "30".
157+
#' @param version character; version of the database.
140158
#' @return a data frame with three columns "url", "file" and "type". "url" is
141159
#' the download URL. "file" is the final path to the file within the download
142160
#' directory of the requested database version. "type" is the file type which
143161
#' guides further processing.
144162
#' @examples
145-
#' chembl_files("chembl", version = "latest")
146-
#' chembl_files("chembl", version = "30")
163+
#' \dontrun{
164+
#' chembl_files(version = "35")
165+
#' }
147166
#' @noRd
148-
chembl_files <- function(version = "latest") {
167+
chembl_files <- function(version) {
149168
if (!inherits(version, "chembl_version")) {
150169
version <- validate_chembl_version(version = version)
151170
}
@@ -202,8 +221,9 @@ chembl_files <- function(version = "latest") {
202221
#' used when mode = "ws". If NULL (default), results are not cached.
203222
#' @param similarity numeric; similarity threshold for similarity searches
204223
#' (default 70).
205-
#' @param version character; database version to use in "offline" mode (default
206-
#' "latest").
224+
#' @param version character; database version to use in "offline" mode. If
225+
#' `NULL` (default), [chembl_check_db_version()] resolves a default set via
226+
#' .Renviron or .Rprofile. Ignored when `mode = "ws"`.
207227
#' @param verbose logical; should a verbose output be printed on the console?
208228
#' @param ... additional arguments, only used for internal testing.
209229
#' @return The function returns a list of lists, where each element of the list
@@ -346,7 +366,7 @@ chembl_query <- function(
346366
output = "raw",
347367
cache_file = NULL,
348368
similarity = 70,
349-
version = "latest",
369+
version = NULL,
350370
verbose = getOption("verbose"),
351371
...) {
352372
resource <- match.arg(resource, chembl_resources())
@@ -739,16 +759,18 @@ chembl_resources <- function() {
739759
#' Connect local ChEMBL database
740760
#'
741761
#' @importFrom rlang .data
742-
#' @param version character; version of the database. Either "latest" (default)
743-
#' or a specific version number, e.g. "30".
762+
#' @param version character; version of the database. If `NULL` (default),
763+
#' [chembl_check_db_version()] resolves a default set via .Renviron or
764+
#' .Rprofile.
744765
#' @param ... Further args passed on to [DBI::dbConnect()]
745766
#' @return an object of class "SQLiteConnection".
746767
#' @examples
747768
#' \dontrun{
748-
#' con <- connect_chembl(version = "latest")
769+
#' con <- connect_chembl(version = "37")
749770
#' }
750771
#' @noRd
751-
connect_chembl <- function(version = "latest", ...) {
772+
connect_chembl <- function(version = NULL, ...) {
773+
if (is.null(version)) version <- chembl_check_db_version()
752774
if (!inherits(version, "chembl_version")) {
753775
version <- validate_chembl_version(version = version)
754776
}
@@ -861,21 +883,17 @@ format_chembl <- function(cont) {
861883

862884
#' Validate ChEMBL version
863885
#'
864-
#' @description Validates the provided ChEMBL version. If "latest" (default),
865-
#' returns the number of the lastest supported version (as a string). If the
866-
#' provided version is lower than the earliest supported version, stops with
867-
#' an error.
886+
#' @description Validates the provided ChEMBL version.
868887
#' @param version character; the ChEMBL version to validate.
869888
#' @return Validated version number as a string.
870889
#' @noRd
871-
validate_chembl_version <- function(version = "latest") {
890+
validate_chembl_version <- function(version) {
872891
assert(version, "character")
873892
stopifnot(length(version) == 1)
874-
if (version == "latest") version <- "36"
875893
version_num <- suppressWarnings(as.numeric(version))
876894
version_base <- as.character(floor(version_num))
877895
if (is.na(version_num)) {
878-
stop("Version must be 'latest' or coercible to numeric.")
896+
stop("Version must be coercible to numeric.")
879897
}
880898
if (version_num < 20) {
881899
stop("Version not supported. Try a more recent version.")

0 commit comments

Comments
 (0)