From ca5600f7329414b3c95acbb427745c66a98fa852 Mon Sep 17 00:00:00 2001 From: Lambda Moses Date: Mon, 23 Dec 2024 00:04:48 -0500 Subject: [PATCH] Bug fixes: plotDimLoadings for only one PC, and allowing DelayedArray in multispati --- DESCRIPTION | 6 +- NAMESPACE | 2 +- R/SFEMethod-bivariate.R | 2 +- R/SFEMethod-multivariate.R | 6 +- R/plot-non-spatial.R | 2 +- R/spatial-misc.R | 4 +- .../_snaps/plot/only-plotting-one-pc.svg | 97 +++++++++++++++++++ tests/testthat/test-multivariate.R | 9 ++ tests/testthat/test-plot.R | 6 ++ 9 files changed, 124 insertions(+), 10 deletions(-) create mode 100644 tests/testthat/_snaps/plot/only-plotting-one-pc.svg diff --git a/DESCRIPTION b/DESCRIPTION index df8247b4a..172cbe431 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = "dl3764@columbia.edu", role = c("aut", "cre"), @@ -40,7 +40,7 @@ Imports: grid, lifecycle, Matrix, - matrixStats, + MatrixGenerics, memuse, methods, patchwork, @@ -65,12 +65,14 @@ Suggests: BiocStyle, cowplot, data.table, + DelayedMatrixStats, EBImage, ExperimentHub, ggh4x, gstat, hexbin, knitr, + matrixStats, pheatmap, RBioFormats, rhdf5, diff --git a/NAMESPACE b/NAMESPACE index bfe74fb21..c9ee05182 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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<-") @@ -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) diff --git a/R/SFEMethod-bivariate.R b/R/SFEMethod-bivariate.R index 15bad2d76..df4ac8df6 100644 --- a/R/SFEMethod-bivariate.R +++ b/R/SFEMethod-bivariate.R @@ -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)) { diff --git a/R/SFEMethod-multivariate.R b/R/SFEMethod-multivariate.R index cc3e83b94..ae92e01bc 100644 --- a/R/SFEMethod-multivariate.R +++ b/R/SFEMethod-multivariate.R @@ -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) @@ -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)) diff --git a/R/plot-non-spatial.R b/R/plot-non-spatial.R index 3b2a212ba..5a43c1f51 100644 --- a/R/plot-non-spatial.R +++ b/R/plot-non-spatial.R @@ -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 { diff --git a/R/spatial-misc.R b/R/spatial-misc.R index 5ef76dbe5..a59c7926f 100644 --- a/R/spatial-misc.R +++ b/R/spatial-misc.R @@ -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 @@ -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 diff --git a/tests/testthat/_snaps/plot/only-plotting-one-pc.svg b/tests/testthat/_snaps/plot/only-plotting-one-pc.svg new file mode 100644 index 000000000..0538480c6 --- /dev/null +++ b/tests/testthat/_snaps/plot/only-plotting-one-pc.svg @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +PC1 + + + + + + +-0.12 +-0.08 +-0.04 +0.00 +ENSMUSG00000060600 +ENSMUSG00000020475 +ENSMUSG00000027559 +ENSMUSG00000023456 +ENSMUSG00000032060 +ENSMUSG00000054091 +ENSMUSG00000086040 +ENSMUSG00000023846 +ENSMUSG00000020738 +ENSMUSG00000027533 + + + + + + + + + + +Loading +Gene +Only plotting one PC + + diff --git a/tests/testthat/test-multivariate.R b/tests/testthat/test-multivariate.R index e15fb5335..403c261c8 100644 --- a/tests/testthat/test-multivariate.R +++ b/tests/testthat/test-multivariate.R @@ -1,5 +1,6 @@ library(SFEData) library(scater) +library(DelayedArray) sfe <- McKellarMuscleData("small") sfe <- sfe[,sfe$in_tissue] @@ -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, diff --git a/tests/testthat/test-plot.R b/tests/testthat/test-plot.R index 964318657..23b9cada3 100644 --- a/tests/testthat/test-plot.R +++ b/tests/testthat/test-plot.R @@ -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") @@ -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",