Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
GohtaAihara committed May 23, 2024
1 parent b8cbc8b commit e388ef4
Show file tree
Hide file tree
Showing 197 changed files with 1,712 additions and 226 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
^pkgdown$
^images
^data-raw$
^.github$
^vignettes/merfish_mousePOA_raw.RDS$
^vignettes/formatting-SpatialExperiment-for-SEraster.Rmd$
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
.Rhistory
.RData
.Ruserdata
R/.DS_Store
.DS_Store

.DS_Store
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ importFrom(ggplot2,scale_fill_viridis_d)
importFrom(ggplot2,theme)
importFrom(ggplot2,theme_bw)
importFrom(methods,as)
importFrom(methods,is)
importFrom(rearrr,midrange)
importFrom(rearrr,rotate_2d)
importFrom(sf,st_as_sf)
Expand Down
34 changes: 21 additions & 13 deletions R/main.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Define global variables
utils::globalVariables(c("fill"))

#' rasterizeMatrix
#'
#' @description Function to rasterize a given input matrix (both dense or sparse) based on a given position matrix.
Expand Down Expand Up @@ -57,7 +60,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
#' @importFrom methods as is
#'
#' @export
#'
Expand Down Expand Up @@ -107,7 +110,7 @@ rasterizeMatrix <- function(data, pos, bbox, resolution = 100, square = TRUE, fu

## 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') {
if (is(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)
bbox <- sf::st_bbox(c(xmin = bbox[1], ymin = bbox[2], xmax = bbox[3], ymax = bbox[4]))
}
Expand Down Expand Up @@ -276,12 +279,14 @@ rasterizeMatrix <- function(data, pos, bbox, resolution = 100, square = TRUE, fu
#' 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")
#' 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)
#' spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
#' out_list <- rasterizeGeneExpression(spe_list, assay_name = "volnorm", resolution = 100, square = TRUE, fun = "mean")
#' out_list <- rasterizeGeneExpression(spe_list, assay_name = "volnorm", resolution = 100,
#' square = TRUE, fun = "mean")
#'
rasterizeGeneExpression <- function(input, assay_name = NULL, resolution = 100, square = TRUE, fun = "mean", n_threads = 1, BPPARAM = NULL, verbose = FALSE) {
if (is.list(input)) {
Expand Down Expand Up @@ -449,12 +454,14 @@ rasterizeGeneExpression <- function(input, assay_name = NULL, resolution = 100,
#' out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", fun = "sum")
#'
#' # 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")
#' 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)
#' spe_list <- permutateByRotation(merfish_mousePOA, n_perm = 3)
#' out_list <- rasterizeCellType(spe_list, col_name = "celltype", resolution = 100, square = TRUE, fun = "sum")
#' out_list <- rasterizeCellType(spe_list, col_name = "celltype", resolution = 100,
#' square = TRUE, fun = "sum")
#'
rasterizeCellType <- function(input, col_name, resolution = 100, square = TRUE, fun = "sum", n_threads = 1, BPPARAM = NULL, verbose = FALSE) {
if (is.list(input)) {
Expand Down Expand Up @@ -769,7 +776,8 @@ permutateByRotation <- function(input, n_perm = 1, verbose = FALSE) {
#' plotRaster(out, feature_name = "Esr1", name = "Esr1")
#'
#' # rasterize cell-type labels with user-defined resolution and hexagonal pixels
#' out <- rasterizeCellType(merfish_mousePOA, col_name = "celltype", resolution = 50, square = FALSE, fun = "sum")
#' 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)
Expand All @@ -795,27 +803,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_var = colSums(mat))
df_sf <- cbind(df_sf, fill = colSums(mat))
} else if (feature_name == "mean") {
df_sf <- cbind(df_sf, fill_var = colMeans(mat))
df_sf <- cbind(df_sf, fill = 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_var = mat[feature_name,])
df_sf <- cbind(df_sf, fill = mat[feature_name,])
}

if (is.null(factor_levels)) {
plt <- ggplot2::ggplot() +
ggplot2::coord_fixed() +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill_var)) +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill)) +
ggplot2::scale_fill_viridis_c(...) +
ggplot2::theme_bw() +
ggplot2::theme(panel.grid = ggplot2::element_blank())
} else {
df_sf$fill_var <- factor(df_sf$fill_var, levels = factor_levels)
df_sf$fill <- factor(df_sf$fill, levels = factor_levels)
plt <- ggplot2::ggplot() +
ggplot2::coord_fixed() +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill_var)) +
ggplot2::geom_sf(data = df_sf, ggplot2::aes(fill = fill)) +
ggplot2::scale_fill_viridis_d(...) +
ggplot2::theme_bw() +
ggplot2::theme(panel.grid = ggplot2::element_blank())
Expand Down
10 changes: 6 additions & 4 deletions docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e388ef4

Please sign in to comment.