Skip to content

Commit

Permalink
Bug fixes: plotDimLoadings for only one PC, and allowing DelayedArray…
Browse files Browse the repository at this point in the history
… in multispati
  • Loading branch information
lambdamoses committed Dec 23, 2024
1 parent 3cfcd44 commit ca5600f
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 10 deletions.
6 changes: 4 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Voyager
Type: Package
Title: From geospatial to spatial omics
Version: 1.9.1
Version: 1.9.2
Authors@R:
c(person("Lambda", "Moses", email = "[email protected]",
role = c("aut", "cre"),
Expand Down Expand Up @@ -40,7 +40,7 @@ Imports:
grid,
lifecycle,
Matrix,
matrixStats,
MatrixGenerics,
memuse,
methods,
patchwork,
Expand All @@ -65,12 +65,14 @@ Suggests:
BiocStyle,
cowplot,
data.table,
DelayedMatrixStats,
EBImage,
ExperimentHub,
ggh4x,
gstat,
hexbin,
knitr,
matrixStats,
pheatmap,
RBioFormats,
rhdf5,
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ importFrom(DelayedArray,sweep)
importFrom(Matrix,colMeans)
importFrom(Matrix,rowMeans)
importFrom(Matrix,rowSums)
importFrom(MatrixGenerics,colVars)
importFrom(RSpectra,eigs_sym)
importFrom(S4Vectors,"mcols<-")
importFrom(S4Vectors,"metadata<-")
Expand Down Expand Up @@ -194,7 +195,6 @@ importFrom(grid,rasterGrob)
importFrom(lifecycle,deprecate_warn)
importFrom(lifecycle,deprecated)
importFrom(lifecycle,is_present)
importFrom(matrixStats,colVars)
importFrom(memuse,Sys.meminfo)
importFrom(methods,as)
importFrom(methods,is)
Expand Down
2 changes: 1 addition & 1 deletion R/SFEMethod-bivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
.lee_mat <- function(x, y = NULL, listw, zero.policy = TRUE, ...) {
# X has genes in rows
if (inherits(listw, "listw"))
W <- listw2sparse(listw)
W <- spatialreg::as_dgRMatrix_listw(listw)
else W <- listw
x <- .scale_n(x)
if (!is.null(y)) {
Expand Down
6 changes: 3 additions & 3 deletions R/SFEMethod-multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' Dray, S., Said, S. and Debias, F. (2008) Spatial ordination of vegetation data using a generalization of Wartenberg's multivariate spatial correlation. Journal of vegetation science, 19, 45-56.
#' @export
#' @importFrom Matrix colMeans
#' @importFrom matrixStats colVars
#' @importFrom MatrixGenerics colVars
#' @importFrom utils head tail
#' @examples
#' library(SFEData)
Expand All @@ -56,12 +56,12 @@ multispati_rsp <- function(x, listw, nfposi = 30L, nfnega = 30L, scale = TRUE) {
if (scale) {
# Note that dudi.pca divides by n instead of n-1 when scaling data
n <- nrow(x)
x <- sweep(x, 2, sqrt(matrixStats::colVars(x)*(n-1)/n), FUN = "/")
x <- sweep(x, 2, sqrt(colVars(x)*(n-1)/n), FUN = "/")
}
if (inherits(listw, "Matrix") || is.matrix(listw))
W <- listw
else if (inherits(listw, "listw"))
W <- listw2sparse(listw)
W <- spatialreg::as_dgRMatrix_listw(listw)
else
stop("listw must be either a listw object or an adjacency matrix.")
covar <- t(x) %*% (W + t(W)) %*% x / (2*nrow(x))
Expand Down
2 changes: 1 addition & 1 deletion R/plot-non-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ ElbowPlot <- function(sce, ndims = 20, nfnega = 0, reduction = "PCA",
.get_loadings_df <- function(sce, loadings, loading_cols, nfeatures, balanced,
swap_rownames) {
df <- cbind(as.data.frame(rowData(sce)[rownames(loadings),, drop = FALSE]),
loadings[, loading_cols])
loadings[, loading_cols, drop = FALSE])
if (is.null(swap_rownames) || !swap_rownames %in% names(df)) {
df$gene_show <- rownames(loadings)
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/spatial-misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' eigenvalues without performing unnecessary work to find the full spectrum as
#' done in base R's \code{\link{eigen}}.
#'
#' @inheritParams SpatialFeatureExperiment::listw2sparse
#' @param listw A listw object for the spatial neighborhood graph.
#' @return A numeric vector of minimum and maximum Moran's I given the spatial
#' neighborhood graph.
#' @note
Expand All @@ -19,7 +19,7 @@
#' @export
#' @importFrom RSpectra eigs_sym
#' @importFrom spdep listw2mat
#' @importFrom SpatialFeatureExperiment listw2sparse multi_listw2sparse
#' @importFrom SpatialFeatureExperiment multi_listw2sparse
#' @concept Spatial statistics
#' @references de Jong, P., Sprenger, C., & van Veen, F. (1984). On extreme values of Moran's I and Geary's C. Geographical Analysis, 16(1), 17-24.
#' @examples
Expand Down
97 changes: 97 additions & 0 deletions tests/testthat/_snaps/plot/only-plotting-one-pc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/testthat/test-multivariate.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library(SFEData)
library(scater)
library(DelayedArray)

sfe <- McKellarMuscleData("small")
sfe <- sfe[,sfe$in_tissue]
Expand Down Expand Up @@ -61,6 +62,14 @@ test_that("Correct output structure of multispati_rsp, only negative", {
expect_true(all(eigs < 0))
})

test_that("multispati_rsp for DelayedArray", {
da <- t(DelayedArray(mat))
out <- multispati_rsp(da, listw = g, nfposi = 2, nfnega = 2)
expect_equal(colnames(out), paste0("PC", 1:4))
expect_equal(rownames(out), colnames(sfe))
expect_true(is.numeric(out))
})

ref <- multispati_rsp(t(mat), listw = g, nfposi = 10, nfnega = 10)
test_that("CalculateMultivariate for matrix", {
out <- calculateMultivariate(mat, "multispati", listw = g,
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ library(ggplot2)
library(scran)
library(EBImage)
library(scales)
spdep::set.SubgraphOption(FALSE)
spdep::set.NoNeighbourOption(FALSE)

expect_ggplot <- function(description, g) {
expect_s3_class(g, "ggplot")
Expand Down Expand Up @@ -467,6 +469,10 @@ test_that("plotDimLoadings for PCA", {
ncol = 1))
})

test_that("plotDimLoadings, when only plotting one PC", {
expect_doppelganger("Only plotting one PC", plotDimLoadings(sfe_muscle, dims = 1))
})

test_that("plotDimLoadings for multiple samples", {
expect_doppelganger("Multispati loadings, 2 samples", {
plotDimLoadings(sfe3, c(1:2, 19:20), swap_rownames = "symbol",
Expand Down

0 comments on commit ca5600f

Please sign in to comment.