diff --git a/R/fetch_data.R b/R/fetch_data.R index 8f859e5b..94b4bea8 100644 --- a/R/fetch_data.R +++ b/R/fetch_data.R @@ -84,29 +84,30 @@ #' #> 172.28 MB #' } fetch_data <- - function(type = c( - "sce", - "sce_layer", - "modeling_results", - "sce_example", - "spe", - "spatialDLPFC_Visium", - "spatialDLPFC_Visium_example_subset", - "spatialDLPFC_Visium_pseudobulk", - "spatialDLPFC_Visium_modeling_results", - "spatialDLPFC_Visium_SPG", - "spatialDLPFC_snRNAseq", - "Visium_SPG_AD_Visium_wholegenome_spe", - "Visium_SPG_AD_Visium_targeted_spe", - "Visium_SPG_AD_Visium_wholegenome_pseudobulk_spe", - "Visium_SPG_AD_Visium_wholegenome_modeling_results", - "Visium_LS_spe", - "Visium_LS_spaceranger", - "Visium_LS_ImageJ_out" - ), - destdir = tempdir(), - eh = ExperimentHub::ExperimentHub(), - bfc = BiocFileCache::BiocFileCache()) { + function( + type = c( + "sce", + "sce_layer", + "modeling_results", + "sce_example", + "spe", + "spatialDLPFC_Visium", + "spatialDLPFC_Visium_example_subset", + "spatialDLPFC_Visium_pseudobulk", + "spatialDLPFC_Visium_modeling_results", + "spatialDLPFC_Visium_SPG", + "spatialDLPFC_snRNAseq", + "Visium_SPG_AD_Visium_wholegenome_spe", + "Visium_SPG_AD_Visium_targeted_spe", + "Visium_SPG_AD_Visium_wholegenome_pseudobulk_spe", + "Visium_SPG_AD_Visium_wholegenome_modeling_results", + "Visium_LS_spe", + "Visium_LS_spaceranger", + "Visium_LS_ImageJ_out" + ), + destdir = tempdir(), + eh = ExperimentHub::ExperimentHub(), + bfc = BiocFileCache::BiocFileCache()) { ## Some variables sce <- sce_layer <- modeling_results <- sce_sub <- spe <- NULL diff --git a/R/run_app.R b/R/run_app.R index 8e7dfaf0..4352ce03 100644 --- a/R/run_app.R +++ b/R/run_app.R @@ -215,7 +215,8 @@ #' spe_stitched$ManualAnnotation <- "NA" #' #' ## Run the app with this stitched data -#' run_app(spe = spe_stitched, +#' run_app( +#' spe = spe_stitched, #' sce_layer = NULL, modeling_results = NULL, sig_genes = NULL, #' title = "visiumStitched example data", #' spe_discrete_vars = c("capture_area", "scran_quick_cluster", "ManualAnnotation"), @@ -224,55 +225,56 @@ #' is_stitched = TRUE #' ) #' } -run_app <- function(spe = fetch_data(type = "spe"), - sce_layer = fetch_data(type = "sce_layer"), - modeling_results = fetch_data(type = "modeling_results"), - sig_genes = sig_genes_extract_all( - n = nrow(sce_layer), - modeling_results = modeling_results, - sce_layer = sce_layer - ), - docs_path = system.file("app", "www", package = "spatialLIBD"), - title = "spatialLIBD", - spe_discrete_vars = c( - "spatialLIBD", - "GraphBased", - "ManualAnnotation", - "Maynard", - "Martinowich", - paste0("SNN_k50_k", 4:28), - "SpatialDE_PCA", - "SpatialDE_pool_PCA", - "HVG_PCA", - "pseudobulk_PCA", - "markers_PCA", - "SpatialDE_UMAP", - "SpatialDE_pool_UMAP", - "HVG_UMAP", - "pseudobulk_UMAP", - "markers_UMAP", - "SpatialDE_PCA_spatial", - "SpatialDE_pool_PCA_spatial", - "HVG_PCA_spatial", - "pseudobulk_PCA_spatial", - "markers_PCA_spatial", - "SpatialDE_UMAP_spatial", - "SpatialDE_pool_UMAP_spatial", - "HVG_UMAP_spatial", - "pseudobulk_UMAP_spatial", - "markers_UMAP_spatial" - ), - spe_continuous_vars = c( - "cell_count", - "sum_umi", - "sum_gene", - "expr_chrM", - "expr_chrM_ratio" - ), - default_cluster = "spatialLIBD", - auto_crop_default = TRUE, - is_stitched = FALSE, - ...) { +run_app <- function( + spe = fetch_data(type = "spe"), + sce_layer = fetch_data(type = "sce_layer"), + modeling_results = fetch_data(type = "modeling_results"), + sig_genes = sig_genes_extract_all( + n = nrow(sce_layer), + modeling_results = modeling_results, + sce_layer = sce_layer + ), + docs_path = system.file("app", "www", package = "spatialLIBD"), + title = "spatialLIBD", + spe_discrete_vars = c( + "spatialLIBD", + "GraphBased", + "ManualAnnotation", + "Maynard", + "Martinowich", + paste0("SNN_k50_k", 4:28), + "SpatialDE_PCA", + "SpatialDE_pool_PCA", + "HVG_PCA", + "pseudobulk_PCA", + "markers_PCA", + "SpatialDE_UMAP", + "SpatialDE_pool_UMAP", + "HVG_UMAP", + "pseudobulk_UMAP", + "markers_UMAP", + "SpatialDE_PCA_spatial", + "SpatialDE_pool_PCA_spatial", + "HVG_PCA_spatial", + "pseudobulk_PCA_spatial", + "markers_PCA_spatial", + "SpatialDE_UMAP_spatial", + "SpatialDE_pool_UMAP_spatial", + "HVG_UMAP_spatial", + "pseudobulk_UMAP_spatial", + "markers_UMAP_spatial" + ), + spe_continuous_vars = c( + "cell_count", + "sum_umi", + "sum_gene", + "expr_chrM", + "expr_chrM_ratio" + ), + default_cluster = "spatialLIBD", + auto_crop_default = TRUE, + is_stitched = FALSE, + ...) { ## Run the checks in the relevant order stopifnot(length(default_cluster) == 1) stopifnot(default_cluster %in% spe_discrete_vars) diff --git a/R/vis_grid_clus.R b/R/vis_grid_clus.R index b3c5086a..c84dbac4 100644 --- a/R/vis_grid_clus.R +++ b/R/vis_grid_clus.R @@ -47,23 +47,24 @@ #' cowplot::plot_grid(plotlist = p_list, ncol = 2) #' } vis_grid_clus <- - function(spe, - clustervar, - pdf_file, - sort_clust = TRUE, - colors = NULL, - return_plots = FALSE, - spatial = TRUE, - height = 24, - width = 36, - image_id = "lowres", - alpha = NA, - sample_order = unique(spe$sample_id), - point_size = 2, - auto_crop = TRUE, - na_color = "#CCCCCC40", - is_stitched = FALSE, - ...) { + function( + spe, + clustervar, + pdf_file, + sort_clust = TRUE, + colors = NULL, + return_plots = FALSE, + spatial = TRUE, + height = 24, + width = 36, + image_id = "lowres", + alpha = NA, + sample_order = unique(spe$sample_id), + point_size = 2, + auto_crop = TRUE, + na_color = "#CCCCCC40", + is_stitched = FALSE, + ...) { stopifnot(all(sample_order %in% unique(spe$sample_id))) if (sort_clust) { diff --git a/R/vis_grid_gene.R b/R/vis_grid_gene.R index 8ddddbfb..bf32dd7a 100644 --- a/R/vis_grid_gene.R +++ b/R/vis_grid_gene.R @@ -35,25 +35,26 @@ #' cowplot::plot_grid(plotlist = p_list, ncol = 2) #' } vis_grid_gene <- - function(spe, - geneid = rowData(spe)$gene_search[1], - pdf_file, - assayname = "logcounts", - minCount = 0, - return_plots = FALSE, - spatial = TRUE, - viridis = TRUE, - height = 24, - width = 36, - image_id = "lowres", - alpha = NA, - cont_colors = if (viridis) viridisLite::viridis(21) else c("aquamarine4", "springgreen", "goldenrod", "red"), - sample_order = unique(spe$sample_id), - point_size = 2, - auto_crop = TRUE, - na_color = "#CCCCCC40", - is_stitched = FALSE, - ...) { + function( + spe, + geneid = rowData(spe)$gene_search[1], + pdf_file, + assayname = "logcounts", + minCount = 0, + return_plots = FALSE, + spatial = TRUE, + viridis = TRUE, + height = 24, + width = 36, + image_id = "lowres", + alpha = NA, + cont_colors = if (viridis) viridisLite::viridis(21) else c("aquamarine4", "springgreen", "goldenrod", "red"), + sample_order = unique(spe$sample_id), + point_size = 2, + auto_crop = TRUE, + na_color = "#CCCCCC40", + is_stitched = FALSE, + ...) { stopifnot(all(sample_order %in% unique(spe$sample_id))) plots <- lapply(sample_order, function(sampleid) { diff --git a/man/run_app.Rd b/man/run_app.Rd index 5b1d6c6a..5c8f19f0 100644 --- a/man/run_app.Rd +++ b/man/run_app.Rd @@ -24,6 +24,7 @@ run_app( "expr_chrM_ratio"), default_cluster = "spatialLIBD", auto_crop_default = TRUE, + is_stitched = FALSE, ... ) } @@ -75,6 +76,13 @@ automatically cropping the images. Set this to \code{FALSE} if your images do no follow the Visium grid size expectations, which are key for enabling auto-cropping.} +\item{is_stitched}{A \code{logical(1)} vector: If \code{TRUE}, expects a +\link[SpatialExperiment:SpatialExperiment]{SpatialExperiment-class} built +with \code{visiumStitched::build_spe()}. +\url{http://research.libd.org/visiumStitched/reference/build_spe.html}; in +particular, expects a logical colData column \code{exclude_overlapping} +specifying which spots to exclude from the plot. Sets \code{auto_crop = FALSE}.} + \item{...}{Other arguments passed to the list of golem options for running the application.} } @@ -233,5 +241,43 @@ if (enough_ram(9e9)) { ## * https://github.com/LieberInstitute/spatialDLPFC/tree/main/code/deploy_app_k09_position_noWM ## * https://github.com/LieberInstitute/spatialDLPFC/tree/main/code/deploy_app_k16 ## * https://github.com/LieberInstitute/spatialDLPFC/tree/main/code/analysis_IF/03_spatialLIBD_app + + +## Example for an object with multiple capture areas stitched together with +## . +spe_stitched <- fetch_data("Visium_LS_spe") + +## Inspect this object +spe_stitched + +## Notice the use of "exclude_overlapping" +table(spe_stitched$exclude_overlapping, useNA = "ifany") + +## Drop NAs +spe_stitched <- spe_stitched[, !is.na(spe_stitched$exclude_overlapping)] + +## Make it compatible with spatialLIBD::run_app() +spe_stitched <- add_key(spe_stitched) +spe_stitched$sum_umi <- colSums(logcounts(spe_stitched)) +spe_stitched$sum_gene <- colSums(logcounts(spe_stitched) > 0) +rowData(spe_stitched)$gene_name <- rowData(spe_stitched)$symbol +rowData(spe_stitched)$gene_id <- rownames(spe_stitched) +rowData(spe_stitched)$gene_search <- paste0(rowData(spe_stitched)$gene_name, "; ", rowData(spe_stitched)$gene_id) +is_mito <- seq_len(1000) +spe_stitched$expr_chrM <- colSums(logcounts(spe_stitched)[is_mito, , drop = FALSE]) +spe_stitched$expr_chrM_ratio <- spe_stitched$expr_chrM / spe_stitched$sum_umi +## Add a variable for saving the manual annotations +spe_stitched$ManualAnnotation <- "NA" + +## Run the app with this stitched data +run_app( + spe = spe_stitched, + sce_layer = NULL, modeling_results = NULL, sig_genes = NULL, + title = "visiumStitched example data", + spe_discrete_vars = c("capture_area", "scran_quick_cluster", "ManualAnnotation"), + spe_continuous_vars = c("sum_umi", "sum_gene", "expr_chrM", "expr_chrM_ratio"), + default_cluster = "scran_quick_cluster", + is_stitched = TRUE +) } } diff --git a/man/vis_grid_clus.Rd b/man/vis_grid_clus.Rd index c9410866..19dd2c3a 100644 --- a/man/vis_grid_clus.Rd +++ b/man/vis_grid_clus.Rd @@ -20,6 +20,7 @@ vis_grid_clus( point_size = 2, auto_crop = TRUE, na_color = "#CCCCCC40", + is_stitched = FALSE, ... ) } @@ -76,6 +77,13 @@ alpha blending already, which will make non-NA values pop up more and the NA values will show with a lighter color. This behavior is lost when \code{alpha} is set to a non-\code{NA} value.} +\item{is_stitched}{A \code{logical(1)} vector: If \code{TRUE}, expects a +\link[SpatialExperiment:SpatialExperiment]{SpatialExperiment-class} built +with \code{visiumStitched::build_spe()}. +\url{http://research.libd.org/visiumStitched/reference/build_spe.html}; in +particular, expects a logical colData column \code{exclude_overlapping} +specifying which spots to exclude from the plot. Sets \code{auto_crop = FALSE}.} + \item{...}{Passed to \link[base:paste]{paste0()} for making the title of the plot following the \code{sampleid}.} } diff --git a/man/vis_grid_gene.Rd b/man/vis_grid_gene.Rd index 0ed8c520..a102cbbe 100644 --- a/man/vis_grid_gene.Rd +++ b/man/vis_grid_gene.Rd @@ -23,6 +23,7 @@ vis_grid_gene( point_size = 2, auto_crop = TRUE, na_color = "#CCCCCC40", + is_stitched = FALSE, ... ) } @@ -93,6 +94,13 @@ alpha blending already, which will make non-NA values pop up more and the NA values will show with a lighter color. This behavior is lost when \code{alpha} is set to a non-\code{NA} value.} +\item{is_stitched}{A \code{logical(1)} vector: If \code{TRUE}, expects a +\link[SpatialExperiment:SpatialExperiment]{SpatialExperiment-class} built +with \code{visiumStitched::build_spe()}. +\url{http://research.libd.org/visiumStitched/reference/build_spe.html}; in +particular, expects a logical colData column \code{exclude_overlapping} +specifying which spots to exclude from the plot. Sets \code{auto_crop = FALSE}.} + \item{...}{Passed to \link[base:paste]{paste0()} for making the title of the plot following the \code{sampleid}.} }