Skip to content

Commit 1bda200

Browse files
committed
refactor: add code to retry download
1 parent 6073921 commit 1bda200

19 files changed

+163
-11
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ local_data
1010
NEWS.md
1111
README.Rmd
1212
logo.png
13+
pkgdown/*
14+
_pkgdown.yml

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: MsBackendMetaboLights
22
Title: Retrieve Mass Spectrometry Data from MetaboLights
3-
Version: 1.1.1
3+
Version: 1.1.2
44
Authors@R:
55
c(person(given = "Johannes", family = "Rainer",
66
email = "Johannes.Rainer@eurac.edu",
@@ -27,7 +27,8 @@ Imports:
2727
ProtGenerics,
2828
BiocFileCache,
2929
S4Vectors,
30-
methods
30+
methods,
31+
progress
3132
Suggests:
3233
testthat,
3334
rmarkdown,

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ importFrom(methods,as)
2323
importFrom(methods,callNextMethod)
2424
importFrom(methods,new)
2525
importFrom(methods,validObject)
26+
importFrom(progress,progress_bar)
27+
importFrom(utils,capture.output)
2628
importFrom(utils,read.table)
2729
importMethodsFrom(BiocFileCache,"bfcmeta<-")
2830
importMethodsFrom(BiocFileCache,bfcmetalist)

NEWS.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# MsBackendMetaboLights 1.1
2+
3+
## Changes in 1.1.2
4+
5+
- Fetch and cache each data file individually.
6+
- Retry retrieval of data from MetaboLights up to 3 times before throwing an
7+
error message, with an increasing time period between attempts.
8+
19
# MsBackendMetaboLights 1.0
210

311
## Changes in 1.0.1
@@ -29,4 +37,4 @@
2937

3038
## Changes in 0.0.1
3139

32-
- Add utility functions to retrieve information from MetaboLights.
40+
- Add utility functions to retrieve information from MetaboLights.

R/MsBackendMetaboLights.R

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@
4545
#' will check the local cache and eventually download missing data files from
4646
#' the MetaboLights repository.
4747
#'
48+
#' @note
49+
#'
50+
#' To account for high server load and eventually failing or rejected
51+
#' downloads from the MetaboLights ftp server, the download functions
52+
#' repeatedly retry to download a file. An error is thrown if download fails
53+
#' for 3 consecutive attempts. Between each attemp, the function waits
54+
#' for an increasing time period (5 seconds between the first and second
55+
#' and 10 seconds between the 2nd and 3rd attempt). This time period can
56+
#' also be configured with the `"metabolights.sleep_mult"` option, which
57+
#' defines the *sleep time multiplicator* (defaults to 5).
58+
#'
4859
#' @param object an instance of `MsBackendMetaboLights`.
4960
#'
5061
#' @param mtblsId `character(1)` with the ID of a single MetaboLights data
@@ -419,13 +430,15 @@ mtbls_list_files <- function(x = character(), pattern = NULL) {
419430
cu <- new_handle()
420431
handle_setopt(cu, ftp_use_epsv = TRUE, dirlistonly = TRUE)
421432
tryCatch({
422-
con <- curl(url = mtbls_ftp_path(x, mustWork = FALSE), "r", handle = cu)
433+
con <- .retry(
434+
curl(url = mtbls_ftp_path(x, mustWork = FALSE), "r", handle = cu),
435+
sleep_mult = .sleep_mult())
423436
}, error = function(e) {
424437
stop("Failed to connect to MetaboLights. No internet connection? ",
425438
"Does the data set \"", x, "\" exist?\n - ", e$message,
426439
call. = FALSE)
427440
})
428-
fls <- readLines(con)
441+
fls <- .retry(readLines(con), sleep_mult = .sleep_mult())
429442
close(con)
430443
if (length(pattern))
431444
fls[grepl(pattern, fls)]
@@ -446,9 +459,10 @@ mtbls_list_files <- function(x = character(), pattern = NULL) {
446459
fpath <- mtbls_ftp_path(x, mustWork = FALSE)
447460
a_fls <- mtbls_list_files(x, pattern = "^a_")
448461
res <- lapply(a_fls, function(z) {
449-
read.table(paste0(fpath, z),
450-
sep = "\t", header = TRUE,
451-
check.names = FALSE)
462+
.retry(read.table(paste0(fpath, z),
463+
sep = "\t", header = TRUE,
464+
check.names = FALSE),
465+
sleep_mult = .sleep_mult())
452466
})
453467
names(res) <- a_fls
454468
res
@@ -534,10 +548,20 @@ mtbls_cached_data_files <- function(mtblsId = character(),
534548
#' file/table
535549
#' - `"rpath"`: the name of the cached data file (full local path)
536550
#'
551+
#' @note
552+
#'
553+
#' Download from MsBackendMetaboLights is tried 3 times with an increasing time
554+
#' delay between tries that can be configured using the
555+
#' `"metabolights.sleep_mult"` option.
556+
#'
537557
#' @importFrom BiocFileCache BiocFileCache
538558
#'
559+
#' @importFrom progress progress_bar
560+
#'
539561
#' @importMethodsFrom BiocFileCache bfcrpath bfcmeta<-
540562
#'
563+
#' @importFrom utils capture.output
564+
#'
541565
#' @noRd
542566
.mtbls_data_files <- function(mtblsId = character(), assayName = character(),
543567
pattern = "mzML$|CDF$|mzXML$",
@@ -576,7 +600,20 @@ mtbls_cached_data_files <- function(mtblsId = character(),
576600
}
577601
## Cache files
578602
bfc <- BiocFileCache()
579-
lfiles <- bfcrpath(bfc, paste0(fpath, ffiles), fname = "exact")
603+
pb <- progress_bar$new(format = paste0("[:bar] :current/:",
604+
"total (:percent) in ",
605+
":elapsed"),
606+
total = length(ffiles), clear = FALSE)
607+
lfiles <- unlist(lapply(ffiles, function(z) {
608+
pb$tick()
609+
invisible(capture.output(suppressMessages(
610+
f <- .retry(bfcrpath(bfc, paste0(fpath, z), fname = "exact",
611+
progress = list()),
612+
sleep_mult = .sleep_mult()))))
613+
f
614+
}))
615+
616+
## lfiles <- bfcrpath(bfc, paste0(fpath, ffiles), fname = "exact")
580617
## Add and store metadata to the cached files
581618
mdata <- data.frame(
582619
rid = names(lfiles),
@@ -616,4 +653,4 @@ mtbls_cached_data_files <- function(mtblsId = character(),
616653
"parameters.", call. = FALSE)
617654
res[, c("rid", "mtbls_id", "mtbls_assay_name",
618655
"derived_spectral_data_file", "rpath")]
619-
}
656+
}

R/retry_download.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#' Retry an expression `ntimes` times waiting an increasing amount of time
2+
#' between tries, i.e. waiting for `Sys.sleep(i * sleep_mult)` seconds between
3+
#' each try. If `expr` fails for `ntimes` times the error will be thrown.
4+
#'
5+
#' @param expr Expression to be evaluated.
6+
#'
7+
#' @param ntimes `integer(1)` with the number of times to try.
8+
#'
9+
#' @param sleep_mult `numeric(1)` multiplier to define the increasing waiting
10+
#' time.
11+
#'
12+
#' @note
13+
#'
14+
#' Warnings are suppressed.
15+
#'
16+
#' @author Johannes Rainer
17+
#'
18+
#' @importFrom methods is
19+
#'
20+
#' @noRd
21+
.retry <- function(expr, ntimes = 3, sleep_mult = 0) {
22+
res <- NULL
23+
for (i in seq_len(ntimes)) {
24+
res <- suppressWarnings(tryCatch(expr, error = function(e) e))
25+
if (is(res, "simpleError")) {
26+
if (i == ntimes)
27+
stop(res)
28+
message("i: ", i)
29+
Sys.sleep(i * sleep_mult)
30+
} else break
31+
}
32+
res
33+
}
34+
35+
.sleep_mult <- function() {
36+
as.integer(getOption("metabolights.sleep_mult", default = 5))
37+
}

logo.png

161 KB
Loading

man/MsBackendMetaboLights.Rd

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

man/figures/logo.png

161 KB
Loading
23.1 KB
Loading

0 commit comments

Comments
 (0)