From 1389bd0f159eefc5945b73f218db853164ea9a4e Mon Sep 17 00:00:00 2001 From: Nick-Eagles Date: Tue, 15 Oct 2024 09:02:55 -0400 Subject: [PATCH] Try using BiocFileCache instead of RDS files for persistence of sample info across examples (not tests yet) --- R/add_array_coords.R | 25 +++++++++++++++++++------ R/build_SpatialExperiment.R | 23 ++++++++++++++++++----- R/prep_fiji.R | 23 ++++++++++++++++++----- man/add_array_coords.Rd | 23 ++++++++++++++++++----- man/build_SpatialExperiment.Rd | 23 ++++++++++++++++++----- man/prep_fiji.Rd | 23 ++++++++++++++++++----- 6 files changed, 109 insertions(+), 31 deletions(-) diff --git a/R/add_array_coords.R b/R/add_array_coords.R index 8c9b20a..03582ca 100644 --- a/R/add_array_coords.R +++ b/R/add_array_coords.R @@ -63,10 +63,13 @@ #' ######################################################################## #' # Prepare sample_info #' ######################################################################## -#' -#' if (file.exists("sample_info.rds")) { -#' sample_info <- readRDS("sample_info.rds") -#' } else { +#' +#' # Cache sample_info across examples +#' bfc <- BiocFileCache::BiocFileCache() +#' cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') +#' +#' if (nrow(cached_file) == 0) { +#' # Then we need to construct the table of sample information #' sample_info <- dplyr::tibble( #' group = "Br2719", #' capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -92,8 +95,18 @@ #' #' ## Re-size images and add more information to the sample_info #' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) -#' -#' saveRDS(sample_info, "sample_info.rds") +#' +#' # Cache sample info for later +#' sample_info_path <- BiocFileCache::bfcnew( +#' bfc, "visiumStitched_sample_info", ext = "csv" +#' ) +#' readr::write_csv(sample_info, sample_info_path) +#' } else { +#' # Pull sample info from the cache +#' sample_info_path <- BiocFileCache::bfcquery( +#' bfc, 'visiumStitched_sample_info' +#' )$rpath +#' sample_info = readr::read_csv(sample_info_path) #' } #' #' ## Preparing Fiji coordinates and images for build_SpatialExperiment() diff --git a/R/build_SpatialExperiment.R b/R/build_SpatialExperiment.R index cde20de..ccf75df 100644 --- a/R/build_SpatialExperiment.R +++ b/R/build_SpatialExperiment.R @@ -39,9 +39,12 @@ #' # Prepare sample_info #' ######################################################################## #' -#' if (file.exists("sample_info.rds")) { -#' sample_info <- readRDS("sample_info.rds") -#' } else { +#' # Cache sample_info across examples +#' bfc <- BiocFileCache::BiocFileCache() +#' cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') +#' +#' if (nrow(cached_file) == 0) { +#' # Then we need to construct the table of sample information #' sample_info <- dplyr::tibble( #' group = "Br2719", #' capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -67,8 +70,18 @@ #' #' ## Re-size images and add more information to the sample_info #' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) -#' -#' saveRDS(sample_info, "sample_info.rds") +#' +#' # Cache sample info for later +#' sample_info_path <- BiocFileCache::bfcnew( +#' bfc, "visiumStitched_sample_info", ext = "csv" +#' ) +#' readr::write_csv(sample_info, sample_info_path) +#' } else { +#' # Pull sample info from the cache +#' sample_info_path <- BiocFileCache::bfcquery( +#' bfc, 'visiumStitched_sample_info' +#' )$rpath +#' sample_info = readr::read_csv(sample_info_path) #' } #' #' ## Preparing Fiji coordinates and images for build_SpatialExperiment() diff --git a/R/prep_fiji.R b/R/prep_fiji.R index cceb477..e4ef5ee 100644 --- a/R/prep_fiji.R +++ b/R/prep_fiji.R @@ -39,9 +39,12 @@ #' @author Nicholas J. Eagles #' #' @examples -#' if (file.exists("sample_info.rds")) { -#' sample_info <- readRDS("sample_info.rds") -#' } else { +#' # Cache sample_info across examples +#' bfc <- BiocFileCache::BiocFileCache() +#' cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') +#' +#' if (nrow(cached_file) == 0) { +#' # Then we need to construct the table of sample information #' sample_info <- dplyr::tibble( #' group = "Br2719", #' capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -67,8 +70,18 @@ #' #' ## Re-size images and add more information to the sample_info #' sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) -#' -#' saveRDS(sample_info, "sample_info.rds") +#' +#' # Cache sample info for later +#' sample_info_path <- BiocFileCache::bfcnew( +#' bfc, "visiumStitched_sample_info", ext = "csv" +#' ) +#' readr::write_csv(sample_info, sample_info_path) +#' } else { +#' # Pull sample info from the cache +#' sample_info_path <- BiocFileCache::bfcquery( +#' bfc, 'visiumStitched_sample_info' +#' )$rpath +#' sample_info = readr::read_csv(sample_info_path) #' } #' #' spe_input_dir <- tempdir() diff --git a/man/add_array_coords.Rd b/man/add_array_coords.Rd index 97d7598..6f2390e 100644 --- a/man/add_array_coords.Rd +++ b/man/add_array_coords.Rd @@ -70,9 +70,12 @@ if (!exists("spe")) { # Prepare sample_info ######################################################################## -if (file.exists("sample_info.rds")) { - sample_info <- readRDS("sample_info.rds") -} else { +# Cache sample_info across examples +bfc <- BiocFileCache::BiocFileCache() +cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') + +if (nrow(cached_file) == 0) { + # Then we need to construct the table of sample information sample_info <- dplyr::tibble( group = "Br2719", capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -98,8 +101,18 @@ if (file.exists("sample_info.rds")) { ## Re-size images and add more information to the sample_info sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) - - saveRDS(sample_info, "sample_info.rds") + + # Cache sample info for later + sample_info_path <- BiocFileCache::bfcnew( + bfc, "visiumStitched_sample_info", ext = "csv" + ) + readr::write_csv(sample_info, sample_info_path) +} else { + # Pull sample info from the cache + sample_info_path <- BiocFileCache::bfcquery( + bfc, 'visiumStitched_sample_info' + )$rpath + sample_info = readr::read_csv(sample_info_path) } ## Preparing Fiji coordinates and images for build_SpatialExperiment() diff --git a/man/build_SpatialExperiment.Rd b/man/build_SpatialExperiment.Rd index be7f8fe..d860bef 100644 --- a/man/build_SpatialExperiment.Rd +++ b/man/build_SpatialExperiment.Rd @@ -69,9 +69,12 @@ for visualization or downstream analysis. # Prepare sample_info ######################################################################## -if (file.exists("sample_info.rds")) { - sample_info <- readRDS("sample_info.rds") -} else { +# Cache sample_info across examples +bfc <- BiocFileCache::BiocFileCache() +cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') + +if (nrow(cached_file) == 0) { + # Then we need to construct the table of sample information sample_info <- dplyr::tibble( group = "Br2719", capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -97,8 +100,18 @@ if (file.exists("sample_info.rds")) { ## Re-size images and add more information to the sample_info sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) - - saveRDS(sample_info, "sample_info.rds") + + # Cache sample info for later + sample_info_path <- BiocFileCache::bfcnew( + bfc, "visiumStitched_sample_info", ext = "csv" + ) + readr::write_csv(sample_info, sample_info_path) +} else { + # Pull sample info from the cache + sample_info_path <- BiocFileCache::bfcquery( + bfc, 'visiumStitched_sample_info' + )$rpath + sample_info = readr::read_csv(sample_info_path) } ## Preparing Fiji coordinates and images for build_SpatialExperiment() diff --git a/man/prep_fiji.Rd b/man/prep_fiji.Rd index c9b2b40..935a5a6 100644 --- a/man/prep_fiji.Rd +++ b/man/prep_fiji.Rd @@ -63,9 +63,12 @@ Fiji output images }} \examples{ -if (file.exists("sample_info.rds")) { - sample_info <- readRDS("sample_info.rds") -} else { +# Cache sample_info across examples +bfc <- BiocFileCache::BiocFileCache() +cached_file <- BiocFileCache::bfcquery(bfc, 'visiumStitched_sample_info') + +if (nrow(cached_file) == 0) { + # Then we need to construct the table of sample information sample_info <- dplyr::tibble( group = "Br2719", capture_area = c("V13B23-283_A1", "V13B23-283_C1", "V13B23-283_D1") @@ -91,8 +94,18 @@ if (file.exists("sample_info.rds")) { ## Re-size images and add more information to the sample_info sample_info <- rescale_fiji_inputs(sample_info, out_dir = tempdir()) - - saveRDS(sample_info, "sample_info.rds") + + # Cache sample info for later + sample_info_path <- BiocFileCache::bfcnew( + bfc, "visiumStitched_sample_info", ext = "csv" + ) + readr::write_csv(sample_info, sample_info_path) +} else { + # Pull sample info from the cache + sample_info_path <- BiocFileCache::bfcquery( + bfc, 'visiumStitched_sample_info' + )$rpath + sample_info = readr::read_csv(sample_info_path) } spe_input_dir <- tempdir()