Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GohtaAihara committed May 21, 2024
1 parent 7251ac0 commit 1dd5a43
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
3 changes: 2 additions & 1 deletion R/data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' Preprocessed MERFISH dataset of the mouse preoptic area for a bregma -0.29 slice
#' from a female naive animal (Animal ID = 1).
#' from a female naive animal (Animal ID = 1, Animal Sex = "Female",
#' # Behavior = "Naive", Bregma = "-0.29").
#'
#' @format \code{SpatialExperiment} object where \code{assay} slot contains genes-by-cells
#' matrix with preprocessed gene expression (total RNA counts per cell divided by
Expand Down
40 changes: 25 additions & 15 deletions R/main.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#' @importFrom sf st_make_grid st_coordinates st_centroid st_as_sf st_intersects
#' @importFrom BiocParallel MulticoreParam bpstart bplapply bpstop
#' @importFrom Matrix rowMeans rowSums
#' @importFrom methods as
#'
#' @export
#'
Expand Down Expand Up @@ -104,7 +105,7 @@ rasterizeMatrix <- function(data, pos, bbox, resolution = 100, square = TRUE, fu
## check if cellnames in data and pos are identical if they are provided in colnames and rownames respectively
stopifnot((is.null(colnames(data)) | is.null(rownames(pos))) | identical(colnames(data), rownames(pos)))|

## check to see if bbox input is of class numeric or bbox, if numeric, convert to st_bbox
## check to see if bbox input is of class numeric or bbox, if numeric, convert to st_bbox
stopifnot("bbox must be of class 'numeric' or 'bbox' (as generated by sf::st_bbox)" = class(bbox) %in% c('numeric', 'bbox'))
if (class(bbox)=='numeric') {
stopifnot("Bounding box for rasterization must be defined by a numeric vector of length four, with xmin, ymin, xmax and ymax values or object of class bbox (as generated by sf::st_bbox)" = length(bbox)==4)
Expand Down Expand Up @@ -270,13 +271,15 @@ rasterizeMatrix <- function(data, pos, bbox, resolution = 100, square = TRUE, fu
#' assayNames(merfish_mousePOA)
#'
#' # rasterize a single SpatialExperiment object
#' # make sure to specify the assay_name argument when the input SpatialExperiment object has multiple assay names (assay_name is used here as an example)
#' # make sure to specify the assay_name argument when the input SpatialExperiment
#' # object has multiple assay names (assay_name is used here as an example)
#' out <- rasterizeGeneExpression(merfish_mousePOA, assay_name = "volnorm", fun = "mean")
#'
#' # rasterize a single SpatialExperiment object with user-defined resolution and hexagonal pixels
#' out <- rasterizeGeneExpression(merfish_mousePOA, assay_name = "volnorm", resolution = 200, square = FALSE, fun = "mean")
#'
#' # rasterize a list of SpatialExperiment objects (in this case, permutated datasets with 3 different rotations)
#' # rasterize a list of SpatialExperiment objects (in this case, permutated datasets
#' # with 3 different rotations)
#' spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
#' out_list <- rasterizeGeneExpression(spe_list, assay_name = "volnorm", resolution = 100, square = TRUE, fun = "mean")
#'
Expand Down Expand Up @@ -437,7 +440,8 @@ rasterizeGeneExpression <- function(input, assay_name = NULL, resolution = 100,
#'
#' data("merfish_mousePOA")
#'
#' # check assay names for this particular SpatialExperiment object (you can see that cell-type labels are stored in the "celltype" column)
#' # check assay names for this particular SpatialExperiment object (you can see
#' # that cell-type labels are stored in the "celltype" column)
#' head(colData(merfish_mousePOA))
#'
#' # rasterize a single SpatialExperiment object
Expand All @@ -447,7 +451,8 @@ rasterizeGeneExpression <- function(input, assay_name = NULL, resolution = 100,
#' # rasterize a single SpatialExperiment object with user-defined resolution and hexagonal pixels
#' out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", resolution = 200, square = FALSE, fun = "sum")
#'
#' # rasterize a list of SpatialExperiment objects (in this case, permutated datasets with 3 different rotations)
#' # rasterize a list of SpatialExperiment objects (in this case, permutated datasets
#' # with 3 different rotations)
#' spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
#' out_list <- rasterizeCellType(spe_list, col_name = "celltype", resolution = 100, square = TRUE, fun = "sum")
#'
Expand Down Expand Up @@ -594,7 +599,8 @@ rasterizeCellType <- function(input, col_name, resolution = 100, square = TRUE,
#' data("merfish_mousePOA")
#'
#' # create a list of 3 permutated datasets rotated at 0 (original), 120, and 240 degrees
#' # this output can directly be fed into rasterizeGeneExpression or rasterizeCellType functions to rasterize all 3 permutations at once with the same pixel coordinates
#' # this output can directly be fed into rasterizeGeneExpression or rasterizeCellType
#' # functions to rasterize all 3 permutations at once with the same pixel coordinates
#' spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
#'
#' # create a list of 5 permutated datasets rotated at 0 (original), 72, 144, 216, 288 degrees
Expand Down Expand Up @@ -754,7 +760,9 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {
#' # rasterize gene expression
#' out <- rasterizeGeneExpression(merfish_mousePOA, assay_name = "volnorm", fun = "mean")
#'
#' # plot total rasterized gene expression per pixel (there is only one assay_name in out and default for feature_name argument is "sum"; therefore, these arguments are not specified)
#' # plot total rasterized gene expression per pixel (there is only one assay_name
#' # in out and default for feature_name argument is "sum"; therefore, these arguments
#' # are not specified)
#' plotRaster(out, name = "total rasterized gexp")
#'
#' # plot rasterized expression of a specific gene/feature per pixel
Expand All @@ -763,8 +771,10 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {
#' # rasterize cell-type labels with user-defined resolution and hexagonal pixels
#' out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", resolution = 50, square = FALSE, fun = "sum")
#'
#' # plot total cell counts per pixel (there is only one assay_name in out and default for feature_name argument is "sum"; therefore, these arguments are not specified)
#' # here, let's use additional parameters for ggplot2::scale_fill_viridis_c so that it would have a different color scheme from gene expression plots
#' # plot total cell counts per pixel (there is only one assay_name in out and default
#' # for feature_name argument is "sum"; therefore, these arguments are not specified)
#' # here, let's use additional parameters for ggplot2::scale_fill_viridis_c so
#' # that it would have a different color scheme from gene expression plots
#' plotRaster(out, name = "total cell counts", option = "inferno")
#'
#' # plot specific cell type's cell counts per pixel
Expand All @@ -785,27 +795,27 @@ plotRaster <- function(input, assay_name = NULL, feature_name = "sum", factor_le
df_sf <- sf::st_sf(geometry = colData(input)$geometry, row.names = rownames(colData(input)))
# add pixel values
if (feature_name == "sum") {
df_sf <- cbind(df_sf, fill = colSums(mat))
df_sf <- cbind(df_sf, fill_var = colSums(mat))
} else if (feature_name == "mean") {
df_sf <- cbind(df_sf, fill = colMeans(mat))
df_sf <- cbind(df_sf, fill_var = colMeans(mat))
} else {
stopifnot(is.character(feature_name))
stopifnot("feature_name does not exist in the input SpatialExperiment object's assay slot" = feature_name %in% rownames(mat))
df_sf <- cbind(df_sf, fill = mat[feature_name,])
df_sf <- cbind(df_sf, fill_var = mat[feature_name,])
}

if (is.null(factor_levels)) {
plt <- ggplot2::ggplot() +
ggplot2::coord_fixed() +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill)) +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill_var)) +
ggplot2::scale_fill_viridis_c(...) +
ggplot2::theme_bw() +
ggplot2::theme(panel.grid = ggplot2::element_blank())
} else {
df_sf$fill <- factor(df_sf$fill, levels = factor_levels)
df_sf$fill_var <- factor(df_sf$fill_var, levels = factor_levels)
plt <- ggplot2::ggplot() +
ggplot2::coord_fixed() +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill)) +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill_var)) +
ggplot2::scale_fill_viridis_d(...) +
ggplot2::theme_bw() +
ggplot2::theme(panel.grid = ggplot2::element_blank())
Expand Down
5 changes: 3 additions & 2 deletions vignettes/formatting-SpatialExperiment-for-SEraster.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ In the original work, [Moffitt J. and Bambah-Mukku D. et al. (2018), "Molecular,
library(SpatialExperiment)
library(Matrix)
library(ggplot2)
library(ggrastr)
```

## Load the subsetted dataset
Expand Down Expand Up @@ -88,7 +89,7 @@ df_plt <- data.frame(pos, total_gexp = colSums(mat))
ggplot(df_plt, aes(x = x, y = y, color = total_gexp)) +
coord_fixed() +
geom_point(size = 1.5, stroke = 0) +
rasterise(geom_point(size = 1.5, stroke = 0)) +
scale_color_viridis_c(name = "total gene expression") +
theme_bw() +
theme(panel.grid = element_blank(),
Expand All @@ -104,7 +105,7 @@ df_plt <- data.frame(pos, celltype = meta$celltype)
ggplot(df_plt, aes(x = x, y = y, color = celltype)) +
coord_fixed() +
geom_point(size = 1.5, stroke = 0) +
rasterise(geom_point(size = 1.5, stroke = 0)) +
theme_bw() +
theme(panel.grid = element_blank(),
axis.title = element_blank(),
Expand Down
3 changes: 2 additions & 1 deletion vignettes/getting-started-with-SEraster.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ library(SpatialExperiment)
library(nnSVG)
library(CooccurrenceAffinity)
library(ggplot2)
library(ggrastr)
```

## Load example dataset
Expand All @@ -63,7 +64,7 @@ df <- data.frame(spatialCoords(merfish_mousePOA), celltype = colData(merfish_mou
ggplot(df, aes(x = x, y = y, col = celltype)) +
coord_fixed() +
geom_point(size = 1.5) +
rasterise(geom_point(size = 1.5)) +
guides(col = guide_legend(override.aes = list(size = 3))) +
labs(x = "x (μm)",
y = "y (μm)",
Expand Down

0 comments on commit 1dd5a43

Please sign in to comment.