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+ }
0 commit comments