R/cache.R
746f8054
 .get_cache <- function() {
894a3533
     cache <- getOption("cBioCache", setCache(verbose = FALSE))
746f8054
 
     BiocFileCache::BiocFileCache(cache)
 }
 
 .cache_exists <- function(bfc, rname) {
b8dacae9
     file.exists(bfcrpath(bfc, rname, exact = TRUE))
746f8054
 }
 
c65461fd
 .checkSize <- function(cancer_study_id) {
 
     bfc <- .get_cache()
b8dacae9
     study_file <- bfcquery(bfc, cancer_study_id, "rname", exact = TRUE)$rpath
c65461fd
 
     URL <- paste0("http://download.cbioportal.org/", cancer_study_id, ".tar.gz")
 
     header <- httr::HEAD(URL)$headers
     header_bytes <- as.numeric(header$`content-length`)
 
     local_bytes <- file.size(study_file)
 
     message("url: ", header_bytes, " vs. local: ", local_bytes)
 
     identical(header_bytes, local_bytes)
 }
 
894a3533
 #' @name cBioCache
bb6736fa
 #'
 #' @title Manage cache / download directories for study data
 #'
 #' @description Managing data downloads is important to save disk space and
 #' re-downloading data files. This can be done effortlessly via the integrated
 #' BiocFileCache system.
 #'
894a3533
 #' @section cBioCache:
746f8054
 #' Get the directory location of the cache. It will prompt the user to create
 #' a cache if not already created. A specific directory can be used via
 #' \code{setCache}.
 #'
bb6736fa
 #' @section setCache:
 #' Specify the directory location of the data cache. By default, it will
746f8054
 #' got to the user's home/.cache and "appname" directory as specified by
3875b9c8
 #' \link{user_cache_dir}. (default appname: cBioPortalData)
bb6736fa
 #'
 #' @section removeCache:
 #' Some files may become corrupt when downloading, this function allows
746f8054
 #' the user to delete the tarball associated with a `cancer_study_id` in the
 #' cache.
bb6736fa
 #'
 #' @param directory The file location where the cache is located. Once set
 #' future downloads will go to this folder.
746f8054
 #' @param verbose Whether to print descriptive messages
 #' @param ask logical (default TRUE when interactive session) Confirm the file
 #' location of the cache directory
 #' @param cancer_study_id A single string from `studiesTable` associated
 #' with a study tarball
894a3533
 #' @param ... For `cBioCache`, arguments passed to `setCache`
bb6736fa
 #'
 #' @md
 #'
f9a94ce9
 #' @return cBioCache: The path to the cache location
bb6736fa
 #' @export
894a3533
 cBioCache <- function(...) {
     getOption("cBioCache", setCache(..., verbose = FALSE))
bb6736fa
 }
 
894a3533
 #' @rdname cBioCache
bb6736fa
 #' @export
 setCache <-
f2d2fc23
     function(directory = rappdirs::user_cache_dir("cBioPortalData"),
         verbose = TRUE,
         ask = interactive())
bb6736fa
 {
     stopifnot(is.character(directory),
         isSingleString(directory), !is.na(directory))
 
a0ae7101
     if (!dir.exists(directory)) {
         if (ask) {
             qtxt <- sprintf(
fb3cf4b6
                 "Create cBioPortalData cache at \n    %s? [y/n]: ",
a0ae7101
                 directory
             )
             answer <- .getAnswer(qtxt, allowed = c("y", "Y", "n", "N"))
             if ("n" == answer)
894a3533
                 stop("'cBioCache' directory not created. Use 'setCache'")
a0ae7101
         }
         dir.create(directory, recursive = TRUE, showWarnings = FALSE)
bb6736fa
     }
894a3533
     options("cBioCache" = directory)
bb6736fa
 
     if (verbose)
3875b9c8
         message("cBioPortalData cache directory set to:\n    ",
7bb1ba2f
             directory)
bb6736fa
     invisible(directory)
 }
 
894a3533
 #' @rdname cBioCache
bb6736fa
 #' @export
746f8054
 removeCache <- function(cancer_study_id) {
     bfc <- .get_cache()
b8dacae9
     rid <- bfcquery(bfc, cancer_study_id, "rname", exact = TRUE)$rid
746f8054
     if (length(rid)) {
         bfcremove(bfc, rid)
         message("Cache record: ", cancer_study_id, ".tar.gz removed")
     } else
         message("No record found: ", cancer_study_id, ".tar.gz")
bb6736fa
 }
f2d2fc23
 
faafacad
 .inputDigest <- function(cachecall, callname) {
     callst <- c(fun = callname, as.list(cachecall)[-c(1, 2)])
f2d2fc23
     digest::digest(callst, algo = "md5")
 }
 
 .getHashCache <- function(hashtag) {
     bfc <- .get_cache()
     rid <- bfcquery(bfc, hashtag, "rname", exact = TRUE)$rid
     if (!length(rid))
844cb47d
         BiocFileCache::bfcnew(bfc, hashtag, ext = ".rda")
f2d2fc23
     else
844cb47d
         BiocFileCache::bfcquery(bfc, hashtag, "rname", exact = TRUE)$rpath
f2d2fc23
 }