Skip to content

Commit ca5600f

Browse files
committed
Bug fixes: plotDimLoadings for only one PC, and allowing DelayedArray in multispati
1 parent 3cfcd44 commit ca5600f

9 files changed

+124
-10
lines changed

DESCRIPTION

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Voyager
22
Type: Package
33
Title: From geospatial to spatial omics
4-
Version: 1.9.1
4+
Version: 1.9.2
55
Authors@R:
66
c(person("Lambda", "Moses", email = "[email protected]",
77
role = c("aut", "cre"),
@@ -40,7 +40,7 @@ Imports:
4040
grid,
4141
lifecycle,
4242
Matrix,
43-
matrixStats,
43+
MatrixGenerics,
4444
memuse,
4545
methods,
4646
patchwork,
@@ -65,12 +65,14 @@ Suggests:
6565
BiocStyle,
6666
cowplot,
6767
data.table,
68+
DelayedMatrixStats,
6869
EBImage,
6970
ExperimentHub,
7071
ggh4x,
7172
gstat,
7273
hexbin,
7374
knitr,
75+
matrixStats,
7476
pheatmap,
7577
RBioFormats,
7678
rhdf5,

NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ importFrom(DelayedArray,sweep)
7373
importFrom(Matrix,colMeans)
7474
importFrom(Matrix,rowMeans)
7575
importFrom(Matrix,rowSums)
76+
importFrom(MatrixGenerics,colVars)
7677
importFrom(RSpectra,eigs_sym)
7778
importFrom(S4Vectors,"mcols<-")
7879
importFrom(S4Vectors,"metadata<-")
@@ -194,7 +195,6 @@ importFrom(grid,rasterGrob)
194195
importFrom(lifecycle,deprecate_warn)
195196
importFrom(lifecycle,deprecated)
196197
importFrom(lifecycle,is_present)
197-
importFrom(matrixStats,colVars)
198198
importFrom(memuse,Sys.meminfo)
199199
importFrom(methods,as)
200200
importFrom(methods,is)

R/SFEMethod-bivariate.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
.lee_mat <- function(x, y = NULL, listw, zero.policy = TRUE, ...) {
1414
# X has genes in rows
1515
if (inherits(listw, "listw"))
16-
W <- listw2sparse(listw)
16+
W <- spatialreg::as_dgRMatrix_listw(listw)
1717
else W <- listw
1818
x <- .scale_n(x)
1919
if (!is.null(y)) {

R/SFEMethod-multivariate.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' 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.
3232
#' @export
3333
#' @importFrom Matrix colMeans
34-
#' @importFrom matrixStats colVars
34+
#' @importFrom MatrixGenerics colVars
3535
#' @importFrom utils head tail
3636
#' @examples
3737
#' library(SFEData)
@@ -56,12 +56,12 @@ multispati_rsp <- function(x, listw, nfposi = 30L, nfnega = 30L, scale = TRUE) {
5656
if (scale) {
5757
# Note that dudi.pca divides by n instead of n-1 when scaling data
5858
n <- nrow(x)
59-
x <- sweep(x, 2, sqrt(matrixStats::colVars(x)*(n-1)/n), FUN = "/")
59+
x <- sweep(x, 2, sqrt(colVars(x)*(n-1)/n), FUN = "/")
6060
}
6161
if (inherits(listw, "Matrix") || is.matrix(listw))
6262
W <- listw
6363
else if (inherits(listw, "listw"))
64-
W <- listw2sparse(listw)
64+
W <- spatialreg::as_dgRMatrix_listw(listw)
6565
else
6666
stop("listw must be either a listw object or an adjacency matrix.")
6767
covar <- t(x) %*% (W + t(W)) %*% x / (2*nrow(x))

R/plot-non-spatial.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ ElbowPlot <- function(sce, ndims = 20, nfnega = 0, reduction = "PCA",
117117
.get_loadings_df <- function(sce, loadings, loading_cols, nfeatures, balanced,
118118
swap_rownames) {
119119
df <- cbind(as.data.frame(rowData(sce)[rownames(loadings),, drop = FALSE]),
120-
loadings[, loading_cols])
120+
loadings[, loading_cols, drop = FALSE])
121121
if (is.null(swap_rownames) || !swap_rownames %in% names(df)) {
122122
df$gene_show <- rownames(loadings)
123123
} else {

R/spatial-misc.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' eigenvalues without performing unnecessary work to find the full spectrum as
1111
#' done in base R's \code{\link{eigen}}.
1212
#'
13-
#' @inheritParams SpatialFeatureExperiment::listw2sparse
13+
#' @param listw A listw object for the spatial neighborhood graph.
1414
#' @return A numeric vector of minimum and maximum Moran's I given the spatial
1515
#' neighborhood graph.
1616
#' @note
@@ -19,7 +19,7 @@
1919
#' @export
2020
#' @importFrom RSpectra eigs_sym
2121
#' @importFrom spdep listw2mat
22-
#' @importFrom SpatialFeatureExperiment listw2sparse multi_listw2sparse
22+
#' @importFrom SpatialFeatureExperiment multi_listw2sparse
2323
#' @concept Spatial statistics
2424
#' @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.
2525
#' @examples
Loading

tests/testthat/test-multivariate.R

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
library(SFEData)
22
library(scater)
3+
library(DelayedArray)
34

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

65+
test_that("multispati_rsp for DelayedArray", {
66+
da <- t(DelayedArray(mat))
67+
out <- multispati_rsp(da, listw = g, nfposi = 2, nfnega = 2)
68+
expect_equal(colnames(out), paste0("PC", 1:4))
69+
expect_equal(rownames(out), colnames(sfe))
70+
expect_true(is.numeric(out))
71+
})
72+
6473
ref <- multispati_rsp(t(mat), listw = g, nfposi = 10, nfnega = 10)
6574
test_that("CalculateMultivariate for matrix", {
6675
out <- calculateMultivariate(mat, "multispati", listw = g,

tests/testthat/test-plot.R

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ library(ggplot2)
99
library(scran)
1010
library(EBImage)
1111
library(scales)
12+
spdep::set.SubgraphOption(FALSE)
13+
spdep::set.NoNeighbourOption(FALSE)
1214

1315
expect_ggplot <- function(description, g) {
1416
expect_s3_class(g, "ggplot")
@@ -467,6 +469,10 @@ test_that("plotDimLoadings for PCA", {
467469
ncol = 1))
468470
})
469471

472+
test_that("plotDimLoadings, when only plotting one PC", {
473+
expect_doppelganger("Only plotting one PC", plotDimLoadings(sfe_muscle, dims = 1))
474+
})
475+
470476
test_that("plotDimLoadings for multiple samples", {
471477
expect_doppelganger("Multispati loadings, 2 samples", {
472478
plotDimLoadings(sfe3, c(1:2, 19:20), swap_rownames = "symbol",

0 commit comments

Comments
 (0)