seurat
seurat copied to clipboard
Issue: Error in xtfrm.data.frame(x) when using SpatiallyVariableFeatures function
Description: I encountered an error while using the selection.method = "moransi" option in the SpatiallyVariableFeatures() function. The error message states: "Error in xtfrm.data.frame(x) : cannot xtfrm data frames." This issue arises when executing the following code:
Steps to reproduce:
Load the brain dataset. Execute the code snippet provided below, which includes the FindSpatiallyVariableFeatures() and SpatiallyVariableFeatures() functions with the selection.method parameter set to "moransi". Observe the error message: "Error in xtfrm.data.frame(x) : cannot xtfrm data frames."
library(Seurat)
library(SeuratData)
library(ggplot2)
library(patchwork)
library(dplyr)
brain <- LoadData("stxBrain", type = "anterior1")
brain <- SCTransform(brain, assay = "Spatial", verbose = FALSE)
# rerun normalization to store sctransform residuals for all genes
brain <- SCTransform(brain, assay = "Spatial", return.only.var.genes = FALSE, verbose = FALSE)
# also run standard log normalization for comparison
brain <- NormalizeData(brain, verbose = FALSE, assay = "Spatial")
# Computes the correlation of the log normalized data and sctransform residuals with the
# number of UMIs
brain <- GroupCorrelation(brain, group.assay = "Spatial", assay = "Spatial", slot = "data", do.plot = FALSE)
brain <- GroupCorrelation(brain, group.assay = "Spatial", assay = "SCT", slot = "scale.data", do.plot = FALSE)
p1 <- GroupCorrelationPlot(brain, assay = "Spatial", cor = "nCount_Spatial_cor") + ggtitle("Log Normalization") +
theme(plot.title = element_text(hjust = 0.5))
p2 <- GroupCorrelationPlot(brain, assay = "SCT", cor = "nCount_Spatial_cor") + ggtitle("SCTransform Normalization") +
theme(plot.title = element_text(hjust = 0.5))
p1 + p2
SpatialFeaturePlot(brain, features = c("Hpca", "Ttr"))
library(ggplot2)
plot <- SpatialFeaturePlot(brain, features = c("Ttr")) + theme(legend.text = element_text(size = 0),
legend.title = element_text(size = 20), legend.key.size = unit(1, "cm"))
# jpeg(filename = "../output/images/spatial_vignette_ttr.jpg", height = 700, width = 1200, quality = 50)
print(plot)
dev.off()
p1 <- SpatialFeaturePlot(brain, features = "Ttr", pt.size.factor = 1)
p2 <- SpatialFeaturePlot(brain, features = "Ttr", alpha = c(0.1, 1))
p1 + p2
brain <- RunPCA(brain, assay = "SCT", verbose = FALSE)
brain <- FindNeighbors(brain, reduction = "pca", dims = 1:30)
brain <- FindClusters(brain, verbose = FALSE)
brain <- RunUMAP(brain, reduction = "pca", dims = 1:30)
p1 <- DimPlot(brain, reduction = "umap", label = TRUE)
p2 <- SpatialDimPlot(brain, label = TRUE, label.size = 3)
p1 + p2
SpatialDimPlot(brain, cells.highlight = CellsByIdentities(object = brain, idents = c(2, 1, 4, 3,
5, 8)), facet.highlight = TRUE, ncol = 3)
de_markers <- FindMarkers(brain, ident.1 = 5, ident.2 = 6)
SpatialFeaturePlot(object = brain, features = rownames(de_markers)[1:3], alpha = c(0.1, 1), ncol = 3)
brain <- FindSpatiallyVariableFeatures(brain, assay = "SCT", features = VariableFeatures(brain)[1:1000],
selection.method = "moransi")
Computing Moran's I |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=27s
SpatiallyVariableFeatures(brain, selection.method = "moransi")
Error in xtfrm.data.frame(x) : cannot xtfrm data frames
sessionInfo()
`
`
Possible Solution:
SpatiallyVariableFeatures_workaround <- function(object, assay="SCT", selection.method = "moransi") {
#' This is work around function to replace SeuratObject::SpatiallyVariableFeatures function.
#' return ranked list of Spatially Variable Features
# Check if object is a Seurat object
if (!inherits(object, "Seurat")) {
stop("object must be a Seurat object")
}
# Check if assay is a valid assay
if (!assay %in% names(object@assays)) {
stop("assay must be a valid assay")
}
# Extract meta.features from the specified object and assay
data <- object@assays[[assay]]@meta.features
# Select columns starting with the provided col_prefix
moransi_cols <- grep(paste0("^", selection.method), colnames(data), value = TRUE)
# Filter rows where "moransi.spatially.variable" is TRUE
filtered_data <- data[data[[paste0(selection.method, ".spatially.variable")]], moransi_cols]
# Sort filtered data by "moransi.spatially.variable.rank" column in ascending order
sorted_data <- filtered_data[order(filtered_data[[paste0(selection.method, ".spatially.variable.rank")]]), ]
# Return row names of the sorted data frame
rownames(sorted_data)
}
head(SpatiallyVariableFeatures_workaround(brain, selection.method = "moransi"), 6)
[1] "Calb2" "Gng4" "Ttr" "S100a5" "Nrgn" "Fabp7"
The issue I notice here is that the "Fabp7" is not in the original Vignette.
Please share any suggestions you may have!
Possible Solution:
SpatiallyVariableFeatures_workaround <- function(object, assay="SCT", selection.method = "moransi") { #' This is work around function to replace SeuratObject::SpatiallyVariableFeatures function. #' return ranked list of Spatially Variable Features # Check if object is a Seurat object if (!inherits(object, "Seurat")) { stop("object must be a Seurat object") } # Check if assay is a valid assay if (!assay %in% names(object@assays)) { stop("assay must be a valid assay") } # Extract meta.features from the specified object and assay data <- object@assays[[assay]]@meta.features # Select columns starting with the provided col_prefix moransi_cols <- grep(paste0("^", selection.method), colnames(data), value = TRUE) # Filter rows where "moransi.spatially.variable" is TRUE filtered_data <- data[data[[paste0(selection.method, ".spatially.variable")]], moransi_cols] # Sort filtered data by "moransi.spatially.variable.rank" column in ascending order sorted_data <- filtered_data[order(filtered_data[[paste0(selection.method, ".spatially.variable.rank")]]), ] # Return row names of the sorted data frame rownames(sorted_data) } head(SpatiallyVariableFeatures_workaround(brain, selection.method = "moransi"), 6)
[1] "Calb2" "Gng4" "Ttr" "S100a5" "Nrgn" "Fabp7"
The issue I notice here is that the "Fabp7" is not in the original Vignette.Please share any suggestions you may have!
Because you did not avoid NA
s: logical subsetting will always return NAs. Changes should be made.
# Filter rows where "moransi.spatially.variable" is TRUE
filtered_data <- data[
data[[paste0(selection.method, ".spatially.variable")]] & (!is.na(data[[paste0(selection.method, ".spatially.variable")]])),
moransi_cols
]
I also have the same issue in version 5.0.1. Thank you for your solution!!!
Thank you, solved the issue I was encountering
Hello everyone,
also
top.clusters <- head(SpatiallyVariableFeatures(cortex, selection.method = "moransi"), 4)
gives the same error (Error in xtfrm.data.frame(x) : cannot xtfrm data frames). But using the SpatiallyVariableFeatures_workaround solution gives back:
Error in order(filtered_data[[paste0(selection.method, ".spatially.variable.rank")]]) : argument 1 is not a vector
Is it possible to solve it?
whien I run filtered_data <- data[
- data[[paste0(selection.method, ".spatially.variable")]] & (!is.na(data[[paste0(selection.method, ".spatially.variable")]])),
- moransi_cols
- ], new issue intercome😧 Error in h(simpleError(msg, call)): Error evaluating argument 'i' while selecting method for function '[': Error evaluating argument 'i' while selecting method for function '[[': object 'selection.method' not found
whien I run filtered_data <- data[
- data[[paste0(selection.method, ".spatially.variable")]] & (!is.na(data[[paste0(selection.method, ".spatially.variable")]])),
- moransi_cols
- ], new issue intercome😧 Error in h(simpleError(msg, call)): Error evaluating argument 'i' while selecting method for function '[': Error evaluating argument 'i' while selecting method for function '[[': object 'selection.method' not found
Error message: "object 'selection.method' not found." Did you run this command separately? This command is part of a function as proposed by @nilesh-iiita. Refer to https://github.com/satijalab/seurat/issues/7422#issuecomment-1581369083.