Skip to content

Commit

Permalink
Merge branch 'devel' of https://github.com/pachterlab/voyager into devel
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamoses committed Dec 2, 2024
2 parents 6a1ad30 + c0409b2 commit 3cfcd44
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion R/SFEMethod-bivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#' @importFrom DelayedArray sweep
.lee_mat <- function(x, y = NULL, listw, zero.policy = TRUE, ...) {
# X has genes in rows
if (is(listw, "listw"))
if (inherits(listw, "listw"))
W <- listw2sparse(listw)
else W <- listw
x <- .scale_n(x)
Expand Down
4 changes: 2 additions & 2 deletions R/SFEMethod-multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ multispati_rsp <- function(x, listw, nfposi = 30L, nfnega = 30L, scale = TRUE) {
stop("At least one of nfposi and nfnega must be positive.")
}
x <- sweep(x, 2, colMeans(x))
if (is(x, "dgeMatrix")) x <- as.matrix(x)
if (inherits(x, "dgeMatrix")) x <- as.matrix(x)
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 = "/")
}
if (inherits(listw, "Matrix") || is.matrix(listw))
W <- listw
else if (is(listw, "listw"))
else if (inherits(listw, "listw"))
W <- listw2sparse(listw)
else
stop("listw must be either a listw object or an adjacency matrix.")
Expand Down
2 changes: 1 addition & 1 deletion R/bivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ setMethod("calculateBivariate", "ANY",
out <- reorganize_fun(type)(out, name = name, ...)
}
}
if (length(out) == 1L && !is(out, "DataFrame")) out <- out[[1]]
if (length(out) == 1L && !inherits(out, "DataFrame")) out <- out[[1]]
out
})

Expand Down
4 changes: 2 additions & 2 deletions R/multivariate.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ setMethod("calculateMultivariate", "SpatialFeatureExperiment",

is_vector <- all(vapply(out, is.vector, FUN.VALUE = logical(1)))
is_matrix <- all(vapply(out, is.matrix, FUN.VALUE = logical(1)))
is_df <- all(vapply(out, function(o) is.data.frame(o) | is(o, "DataFrame"),
is_df <- all(vapply(out, function(o) is.data.frame(o) | inherits(o, "DataFrame"),
FUN.VALUE = logical(1)))
if (is_vector) {
out <- unlist(out)
Expand Down Expand Up @@ -223,7 +223,7 @@ runMultivariate <- function(x, type, colGraphName = 1L,
reducedDim(x, name) <- out
} else {
if (is.vector(out)) colData(x)[[name]] <- out
if (is.data.frame(out) || is(out, "DataFrame")) {
if (is.data.frame(out) || inherits(out, "DataFrame")) {
inds <- !grepl(name, names(out))
names(out)[inds] <- paste(name, names(out)[inds], sep = "_")
colData(x) <- cbind(colData(x), out)
Expand Down
2 changes: 1 addition & 1 deletion R/plot-univar-downstream.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
warning("Too few points for stat_density2d, not plotting contours.")
}
)
if (is(p_test, "ggplot_built")) {
if (inherits(p_test, "ggplot_built")) {
p <- p + geom_density2d(color = contour_color, bins = bins, ...)
}
p <- p +
Expand Down
10 changes: 5 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ getDivergeRange <- function(values, diverge_center = 0) {

#' @importFrom sf st_as_sfc st_bbox st_intersection
.bbox_sample <- function(df, bbox) {
if (is(df, "sf")) {
if (inherits(df, "sf")) {
# Only for one sample
bbox_use <- st_as_sfc(st_bbox(bbox))
suppressWarnings(df <- st_intersection(df, bbox_use))
Expand Down Expand Up @@ -659,7 +659,7 @@ getDivergeRange <- function(values, diverge_center = 0) {
# the bbox is a large part of the image that also needs to be written to
# disk
imgs <- lapply(imgs, function(x) {
if (is(x, "SpatRasterImage")) {
if (inherits(x, "SpatRasterImage")) {
tot_area <- ext(x) |> st_bbox() |> st_as_sfc() |> st_area()
bb_area <- bbox |> st_bbox() |> st_as_sfc() |> st_area()
bb_prop <- bb_area/tot_area
Expand All @@ -684,10 +684,10 @@ getDivergeRange <- function(values, diverge_center = 0) {
}
# All convert to SpatRaster
imgs <- lapply(imgs, function(img) {
if (is(img, "BioFormatsImage")) {
if (inherits(img, "BioFormatsImage")) {
res_use <- .find_res(img, maxcell)
spi <- toSpatRasterImage(img, resolution = res_use, save_geotiff = FALSE)
} else if (is(img, "ExtImage")) {
} else if (inherits(img, "ExtImage")) {
spi <- toSpatRasterImage(img, save_geotiff = FALSE)
} else spi <- img
spi |> resample_spat(maxcell)
Expand Down Expand Up @@ -772,7 +772,7 @@ getDivergeRange <- function(values, diverge_center = 0) {
img_df <- .get_img_df(sfe, sample_id, image_id, channel, bbox, maxcell,
normalize_channels)
} else img_df <- NULL
if (is(img_df, "DataFrame") && !nrow(img_df)) img_df <- NULL
if (inherits(img_df, "DataFrame") && !nrow(img_df)) img_df <- NULL
.wrap_spatial_plots(
df, annot_df, img_df, channel, type_annot, values, aes_use,
annot_aes, annot_fixed, tx_fixed, size, shape, linewidth, linetype, alpha,
Expand Down
4 changes: 2 additions & 2 deletions R/plotLocalResult.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ plotLocalResult <- function(sfe, name, features, attribute = NULL,
channel, bbox, maxcell,
normalize_channels)
else img_df <- NULL
if (is(img_df, "DataFrame") && !nrow(img_df)) img_df <- NULL
if (inherits(img_df, "DataFrame") && !nrow(img_df)) img_df <- NULL
if (!is.null(rowGeometryName)) {
tx_df <- .get_tx_df(sfe, data_dir = NULL, tech = NULL, file = NULL,
sample_id = sample_id, spatialCoordsNames = c("X", "Y"),
Expand All @@ -229,7 +229,7 @@ plotLocalResult <- function(sfe, name, features, attribute = NULL,
)
}
# Add title to not to confuse with gene expression
if (is(out, "patchwork")) {
if (inherits(out, "patchwork")) {
out <- out + plot_annotation(title = title_use)
} else {
out <- out + ggtitle(title_use)
Expand Down
2 changes: 1 addition & 1 deletion R/plotSpatialReducedDim.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ spatialReducedDim <- function(sfe, dimred, ncomponents = NULL,
normalize_channels = normalize_channels, rowGeometryName = rowGeometryName,
rowGeometryFeatures = rowGeometryFeatures, tx_file = tx_file, ...
)
if (is(out, "patchwork")) {
if (inherits(out, "patchwork")) {
out <- out + plot_annotation(title = dimred)
} else {
out <- out + ggtitle(dimred)
Expand Down
10 changes: 5 additions & 5 deletions R/univariate-internal.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Internal function for univariate metrics
.calc_univar <- function(x, fun, BPPARAM, ...) {
if (is(x, "DFrame") || is.data.frame(x)) {
if (is(x, "sf")) x <- st_drop_geometry(x)
if (inherits(x, "DFrame") || is.data.frame(x)) {
if (inherits(x, "sf")) x <- st_drop_geometry(x)
x <- t(as.matrix(x))
if (anyNA(x)) {
stop("Only numeric columns without NA (within the sample_id) can be used.")
Expand All @@ -17,12 +17,12 @@
#' @importFrom stats as.formula terms
.get_coords_df <- function(x, df, sample_id, exprs_values,
swap_rownames, ...) {
if (!is(df, "sf") || st_geometry_type(df, by_geometry = FALSE) != "POINT") {
if (is(df, "sf")) df <- st_drop_geometry(df)
if (!inherits(df, "sf") || st_geometry_type(df, by_geometry = FALSE) != "POINT") {
if (inherits(df, "sf")) df <- st_drop_geometry(df)
# Can't use list columns as regressors
inds_keep <- vapply(df, is.atomic, FUN.VALUE = logical(1))
df <- df[,inds_keep]
if (is(df, "DataFrame")) df <- as.data.frame(df)
if (inherits(df, "DataFrame")) df <- as.data.frame(df)
colnames_use <- spatialCoordsNames(x)
geo <- df2sf(spatialCoords(x)[x$sample_id == sample_id,], colnames_use)
oth_names <- setdiff(names(df), colnames_use)
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
}

.get_not_na_items <- function(df, features, colname_use) {
if (is(df, "sf")) df <- st_drop_geometry(df)
if (inherits(df, "sf")) df <- st_drop_geometry(df)
if (!colname_use %in% names(df)) {
return(NULL)
}
Expand Down

0 comments on commit 3cfcd44

Please sign in to comment.