Skip to content

Commit

Permalink
Style code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Eagles committed Nov 2, 2023
1 parent a4133b9 commit b69abb5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 47 deletions.
74 changes: 35 additions & 39 deletions R/multi_gene.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,51 @@
#' @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
#'
#' @export
#' @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, ...
)

Expand All @@ -77,49 +76,48 @@ 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
#'
#' @export
#' @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, ...
)

Expand All @@ -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))
}

Expand All @@ -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")
}
}
20 changes: 12 additions & 8 deletions tests/testthat/test-multi_gene.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ 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"
)

# 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"
Expand All @@ -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)
})

0 comments on commit b69abb5

Please sign in to comment.