diff --git a/R/multi_gene.R b/R/multi_gene.R index 95e3e2d..da55557 100644 --- a/R/multi_gene.R +++ b/R/multi_gene.R @@ -17,7 +17,7 @@ #' @param minCount numeric(1) passed to passed to \code{spatialLIBD::vis_gene} #' @param ... Parameters accepted by \code{spot_plot}, excluding #' \code{is_discrete} or \code{var_name}, which are handled internally -#' +#' #' @return A \code{ggplot} object containing a "spot plot" of the specified #' sample and genes #' @@ -25,44 +25,43 @@ #' @author Nicholas J. Eagles #' @import SpatialExperiment SummarizedExperiment Matrix #' @family Spot plots summarizing expression of multiple genes simultaneously -#' -#' @examples -#' +#' +#' @examples +#' #' # Grab an example SpatialExperiment and suppose all of its spots should be #' # plotted (for spatialNAc, 'exclude_overlapping' will only have genuinely #' # overlapping spots be TRUE) #' spe <- spatialLIBD::fetch_data(type = "spatialDLPFC_Visium_example_subset") #' spe$exclude_overlapping <- FALSE -#' -#' white_matter_genes = c( +#' +#' white_matter_genes <- c( #' "ENSG00000197971", "ENSG00000131095", "ENSG00000123560", #' "ENSG00000171885" #' ) #' spot_plot_z_score( -#' spe = spe, -#' genes = white_matter_genes, -#' sample_id = unique(spe)$sample_id[1], -#' assayname = 'logcounts' +#' spe = spe, +#' genes = white_matter_genes, +#' sample_id = unique(spe)$sample_id[1], +#' assayname = "logcounts" #' ) -spot_plot_z_score = function( - spe, genes, sample_id, assayname = "logcounts", minCount = 0, ... - ) { +spot_plot_z_score <- function(spe, genes, sample_id, assayname = "logcounts", minCount = 0, ...) { # Check validity of arguments .multi_gene_validity_check( spe, genes, sample_id, assayname, minCount, ... ) - spe = spe[genes, spe$sample_id == sample_id] + spe <- spe[genes, spe$sample_id == sample_id] # For each spot, average expression Z-scores across all selected genes - a = assays(spe)[[assayname]] - gene_z = (a - rowMeans(a)) / rowSds(a) - spe$temp_var = colMeans(gene_z, na.rm = TRUE) + a <- assays(spe)[[assayname]] + gene_z <- (a - rowMeans(a)) / rowSds(a) + spe$temp_var <- colMeans(gene_z, na.rm = TRUE) # Plot spatial distribution of averaged expression Z-scores for this # sample - p = spot_plot( - spe, sample_id, var_name = 'Z_score', is_discrete = FALSE, + p <- spot_plot( + spe, sample_id, + var_name = "Z_score", is_discrete = FALSE, minCount = minCount, assayname = assayname, ... ) @@ -77,7 +76,7 @@ spot_plot_z_score = function( #' is plotted across the entire capture area. #' #' @inheritParams spot_plot_z_score -#' +#' #' @return A \code{ggplot} object containing a "spot plot" of the specified #' sample and genes #' @@ -85,41 +84,40 @@ spot_plot_z_score = function( #' @author Nicholas J. Eagles #' @import SpatialExperiment SummarizedExperiment Matrix #' @family Spot plots summarizing expression of multiple genes simultaneously -#' -#' @examples -#' +#' +#' @examples +#' #' # Grab an example SpatialExperiment and suppose all of its spots should be #' # plotted (for spatialNAc, 'exclude_overlapping' will only have genuinely #' # overlapping spots be TRUE) #' spe <- spatialLIBD::fetch_data(type = "spatialDLPFC_Visium_example_subset") #' spe$exclude_overlapping <- FALSE -#' -#' white_matter_genes = c( +#' +#' white_matter_genes <- c( #' "ENSG00000197971", "ENSG00000131095", "ENSG00000123560", #' "ENSG00000171885" #' ) #' spot_plot_sparsity( -#' spe = spe, -#' genes = white_matter_genes, -#' sample_id = unique(spe)$sample_id[1] +#' spe = spe, +#' genes = white_matter_genes, +#' sample_id = unique(spe)$sample_id[1] #' ) -spot_plot_sparsity = function( - spe, genes, sample_id, assayname = "counts", minCount = 0.1, ... - ) { +spot_plot_sparsity <- function(spe, genes, sample_id, assayname = "counts", minCount = 0.1, ...) { # Check validity of arguments .multi_gene_validity_check( spe, genes, sample_id, assayname, minCount, ... ) - spe = spe[genes, spe$sample_id == sample_id] + spe <- spe[genes, spe$sample_id == sample_id] # For each spot, compute proportion of marker genes with nonzero # expression spe$prop_nonzero_marker <- colMeans(assays(spe)[[assayname]] > 0) # Plot spatial distribution of this proportion - p = spot_plot( - spe, sample_id, var_name = 'prop_nonzero_marker', + p <- spot_plot( + spe, sample_id, + var_name = "prop_nonzero_marker", is_discrete = FALSE, minCount = minCount, assayname = assayname, ... ) @@ -132,16 +130,14 @@ spot_plot_sparsity = function( #' @inheritParams spot_plot_z_score #' @import SpatialExperiment SummarizedExperiment #' @return NULL -.multi_gene_validity_check = function( - spe, genes, sample_id, assayname, minCount, ... - ) { +.multi_gene_validity_check <- function(spe, genes, sample_id, assayname, minCount, ...) { # 'genes' if (!all(genes %in% rownames(spe))) { stop("The SpatialExperiment does not contain the selected genes in its rownames") } # 'sample_id' - if (!('sample_id' %in% colnames(colData(spe))) || !(sample_id %in% spe$sample_id)) { + if (!("sample_id" %in% colnames(colData(spe))) || !(sample_id %in% spe$sample_id)) { stop(paste("'spe$sample_id' must exist and contain the ID", sample_id)) } @@ -151,7 +147,7 @@ spot_plot_sparsity = function( } # Not-allowed '...' parameters - if (any(c('var_name', 'is_discrete') %in% names(list(...)))) { + if (any(c("var_name", "is_discrete") %in% names(list(...)))) { stop("The 'var_name' and 'is_discrete' parameters are internally handled and may not be specified through '...' arguments") } } diff --git a/tests/testthat/test-multi_gene.R b/tests/testthat/test-multi_gene.R index cc2a646..b3e4e46 100644 --- a/tests/testthat/test-multi_gene.R +++ b/tests/testthat/test-multi_gene.R @@ -2,18 +2,18 @@ library(SpatialExperiment) # Given a SpatialExperiment and multi-gene-plotting function 'plot_fun' # from this package, run tests and return NULL -test_plot_fun = function(spe, plot_fun) { +test_plot_fun <- function(spe, plot_fun) { # Bad gene names (gene symbols are not in rownames) sample_id <- unique(spe$sample_id)[1] - genes = 'MOBP' + genes <- "MOBP" expect_error( plot_fun(spe, genes, sample_id), "does not contain the selected genes" ) - + # Bad sample ID - sample_id = 'asdasdasdasd' - genes = rownames(spe)[1:10] + sample_id <- "asdasdasdasd" + genes <- rownames(spe)[1:10] expect_error( plot_fun(spe, genes, sample_id), "must exist and contain the ID" @@ -21,7 +21,7 @@ test_plot_fun = function(spe, plot_fun) { # Bad assay name sample_id <- unique(spe$sample_id)[1] - genes = rownames(spe)[1:10] + genes <- rownames(spe)[1:10] expect_error( plot_fun(spe, genes, sample_id, assayname = "asfasdad"), "is not an assay" @@ -48,5 +48,9 @@ test_plot_fun = function(spe, plot_fun) { spe <- fetch_data(type = "spatialDLPFC_Visium_example_subset") spe$exclude_overlapping <- FALSE -test_that("spot_plot_z_score", { test_plot_fun(spe, spot_plot_z_score) }) -test_that("spot_plot_sparsity", { test_plot_fun(spe, spot_plot_sparsity) }) +test_that("spot_plot_z_score", { + test_plot_fun(spe, spot_plot_z_score) +}) +test_that("spot_plot_sparsity", { + test_plot_fun(spe, spot_plot_sparsity) +})