Open ayusyarif opened 2 months ago
Running into this issue as well. Trying to analyze spatial transcriptomics data, which is currently loaded into a Seurat object.
#Load packages
library(Seurat)
library(NICHES)
#Load in Seurat object
seu <- readRDS(file = "/Users/nmeskow/ST/seu.rds")
# Split object into conditions, to calculate NICHES objects separately
data.list <- SplitObject(seu, split.by="group_id")
# Run NICHES on each system and store/name the outputs
scc.list <- list()
for(i in 1:length(data.list)){
scc.list[[i]] <- RunNICHES(data.list[[i]], LR.database="fantom5", species="mouse", assay="Spatial", cell_types = "seurat_clusters", position.x = "x", position.y = "y", min.cells.per.ident = 0, min.cells.per.gene = NULL, CellToCell = F, CellToCellSpatial = T, NeighborhoodToCell = T, CellToNeighborhood = T)
}
Output:
Set cell types as Identity of object internally
Subsetting to populations with greater than 0 cells
10090 distinct cells from 14 celltypes to be analyzed
Limiting ground truth to genes within dataset
Mapping against 2227 ground truth signaling mechanisms
Compute edgelist based on mutual nearest neighbors.
Error in .make_numeric_version(x, strict, .standard_regexps()$valid_numeric_version) :
invalid non-character version specification 'x' (type: double)
Session info here:
R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS Sonoma 14.5
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] devtools_2.4.5 usethis_3.0.0 nicheDE_0.0.0.9000 cowplot_1.1.3 reshape2_1.4.4 stringr_1.5.1
[7] NICHES_1.1.0 scales_1.3.0 dplyr_1.1.4 ggplot2_3.5.1 Seurat_5.1.0 SeuratObject_5.0.2
[13] sp_2.1-4
loaded via a namespace (and not attached):
[1] RcppAnnoy_0.0.22 splines_4.4.1 later_1.3.2 tibble_3.2.1 polyclip_1.10-7
[6] fastDummies_1.7.4 lifecycle_1.0.4 rstatix_0.7.2 doParallel_1.0.17 globals_0.16.3
[11] processx_3.8.4 lattice_0.22-6 MASS_7.3-61 backports_1.5.0 magrittr_2.0.3
[16] plotly_4.10.4 rmarkdown_2.28 yaml_2.3.10 remotes_2.5.0 httpuv_1.6.15
[21] sctransform_0.4.1 spam_2.10-0 sessioninfo_1.2.2 pkgbuild_1.4.4 spatstat.sparse_3.1-0
[26] reticulate_1.39.0 pbapply_1.7-2 RColorBrewer_1.1-3 abind_1.4-8 pkgload_1.4.0
[31] Rtsne_0.17 purrr_1.0.2 BiocGenerics_0.50.0 circlize_0.4.16 IRanges_2.38.1
[36] S4Vectors_0.42.1 ggrepel_0.9.6 irlba_2.3.5.1 listenv_0.9.1 spatstat.utils_3.1-0
[41] goftest_1.2-3 RSpectra_0.16-2 spatstat.random_3.3-2 fitdistrplus_1.2-1 parallelly_1.38.0
[46] leiden_0.4.3.1 codetools_0.2-20 tidyselect_1.2.1 shape_1.4.6.1 farver_2.1.2
[51] matrixStats_1.4.1 stats4_4.4.1 spatstat.explore_3.3-2 jsonlite_1.8.8 GetoptLong_1.0.5
[56] ellipsis_0.3.2 progressr_0.14.0 ggridges_0.5.6 survival_3.7-0 iterators_1.0.14
[61] foreach_1.5.2 tools_4.4.1 ica_1.0-3 Rcpp_1.0.13 glue_1.7.0
[66] gridExtra_2.3 xfun_0.47 mgcv_1.9-1 withr_3.0.1 fastmap_1.2.0
[71] fansi_1.0.6 callr_3.7.6 digest_0.6.37 R6_2.5.1 mime_0.12
[76] colorspace_2.1-1 scattermore_1.2 tensor_1.5 spatstat.data_3.1-2 utf8_1.2.4
[81] tidyr_1.3.1 generics_0.1.3 data.table_1.16.0 httr_1.4.7 htmlwidgets_1.6.4
[86] uwot_0.2.2 pkgconfig_2.0.3 gtable_0.3.5 ComplexHeatmap_2.20.0 lmtest_0.9-40
[91] htmltools_0.5.8.1 carData_3.0-5 profvis_0.3.8 dotCall64_1.1-1 clue_0.3-65
[96] png_0.1-8 spatstat.univar_3.0-1 knitr_1.48 rstudioapi_0.16.0 rjson_0.2.23
[101] nlme_3.1-166 curl_5.2.2 zoo_1.8-12 cachem_1.1.0 GlobalOptions_0.1.2
[106] KernSmooth_2.23-24 parallel_4.4.1 miniUI_0.1.1.1 STdeconvolve_1.8.0 desc_1.4.3
[111] pillar_1.9.0 grid_4.4.1 vctrs_0.6.5 RANN_2.6.2 urlchecker_1.0.1
[116] promises_1.3.0 ggpubr_0.6.0 car_3.1-2 xtable_1.8-4 cluster_2.1.6
[121] evaluate_1.0.0 cli_3.6.3 compiler_4.4.1 rlang_1.1.4 crayon_1.5.3
[126] future.apply_1.11.2 ggsignif_0.6.4 ps_1.8.0 plyr_1.8.9 fs_1.6.4
[131] stringi_1.8.4 viridisLite_0.4.2 deldir_2.0-4 munsell_0.5.1 lazyeval_0.2.2
[136] spatstat.geom_3.3-3 Matrix_1.7-0 RcppHNSW_0.6.0 patchwork_1.3.0 future_1.34.0
[141] shiny_1.9.1 ROCR_1.0-11 igraph_2.0.3 broom_1.0.6 memoise_2.0.1
Hi, I think it is related to seurat compatibility. I solved this by modifying the Seurat::GetAssayData() inside the core function, in my case is RunCellToCellSpatial().
RunCellToCellSpatial <- function(sys.small,
ground.truth,
assay,
meta.data.to.map,
edgelist,
output_format
){
subunit.list <- matrix(data = 1,nrow = nrow(ground.truth$source.subunits),ncol = ncol(Seurat::GetAssayData(sys.small,assay,"data")[,edgelist$from])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list) <- colnames(Seurat::GetAssayData(sys.small,assay,"data")[,edgelist$from])
rownames(subunit.list) <- rownames(ground.truth$source.subunits)
non.na.indices <- !is.na(ground.truth$source.subunits) #Identify rows in the s-th column of the ground truth which are not NA
subunit.list[non.na.indices,] <- as.matrix(Seurat::GetAssayData(sys.small,assay,"data")[ground.truth$source.subunits[non.na.indices,],edgelist$from]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices
lig.data <- subunit.list
rm(subunit.list)
# Make receptor matrix
subunit.list <- matrix(data = 1,nrow = nrow(ground.truth$target.subunits),ncol = ncol(Seurat::GetAssayData(sys.small,assay,"data")[,edgelist$to])) #initialize a mechanism x barcode matrix of all NAs
colnames(subunit.list) <- colnames(Seurat::GetAssayData(sys.small,assay,"data")[,edgelist$to])
rownames(subunit.list) <- rownames(ground.truth$target.subunits)
non.na.indices <- !is.na(ground.truth$target.subunits) #Identify rows in the t-th column of the ground truth which are not NA
subunit.list[non.na.indices,] <- as.matrix(Seurat::GetAssayData(sys.small,assay,"data")[ground.truth$target.subunits[non.na.indices,],edgelist$to]) #For every row in the initialized matrix corresponding to the indices of the ground.truth which are not NA, replace with the rows from the Seurat object corresponding to the genes in the ground.truth at those indices
rec.data <- subunit.list
rm(subunit.list)
# Make SCC matrix
scc <- lig.data*rec.data
rownames(scc) <- paste(rownames(lig.data),rownames(rec.data),sep = '—')
colnames(scc) <- paste(colnames(lig.data),colnames(rec.data),sep = '—')
sending.cell.idents <- as.character(Seurat::Idents(sys.small)[colnames(lig.data)])
receiving.cell.idents <- as.character(Seurat::Idents(sys.small)[colnames(rec.data)])
dim(scc)
# Use this matrix to create a Seurat object:
demo <- Seurat::CreateSeuratObject(counts = as.matrix(scc),assay = 'CellToCellSpatial')
# JC: Seurat V5 will not create data slot automatically, the following step is to manually add this slot
demo <- NormalizeData(demo,assay = "CellToCellSpatial") # Seura Object need to be >= 5.0.1
demo@assays$CellToCellSpatial@layers$data <- demo@assays$CellToCellSpatial@layers$counts # Seura Object need to be >= 5.0.1
# Add key metadata
meta.data.to.add <- data.frame(SendingType = sending.cell.idents,
ReceivingType = receiving.cell.idents)
rownames(meta.data.to.add) <- colnames(scc)
meta.data.to.add$VectorType <- paste(meta.data.to.add$SendingType,
meta.data.to.add$ReceivingType,
sep = '—')
#Add metadata to the Seurat object
demo <- Seurat::AddMetaData(demo,metadata = meta.data.to.add)
# Gather and assemble additional metadata
if (!is.null(meta.data.to.map)){
# Identify sending and receiving barcodes
sending.barcodes <- colnames(lig.data)
receiving.barcodes <- colnames(rec.data)
# Pull and format sending and receiving metadata
# jc: possible bug, change object to sys.small
sending.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][sending.barcodes,])
receiving.metadata <- as.matrix(sys.small@meta.data[,meta.data.to.map,drop=FALSE][receiving.barcodes,])
# Make joint metadata
datArray <- abind::abind(sending.metadata,receiving.metadata,along=3)
joint.metadata <- as.matrix(apply(datArray,1:2,function(x)paste(x[1],"-",x[2])))
# Define column names
colnames(joint.metadata) <- paste(colnames(sending.metadata),'Joint',sep = '.')
colnames(sending.metadata) <- paste(colnames(sending.metadata),'Sending',sep='.')
colnames(receiving.metadata) <- paste(colnames(receiving.metadata),'Receiving',sep='.')
# Compile
meta.data.to.add.also <- cbind(sending.metadata,receiving.metadata,joint.metadata)
rownames(meta.data.to.add.also) <- paste(sending.barcodes,receiving.barcodes,sep='—')
# Add additional metadata
demo <- Seurat::AddMetaData(demo,metadata = as.data.frame(meta.data.to.add.also))
}
# Set initial identity
Seurat::Idents(demo) <- demo$VectorType
# How many vectors were captured by this sampling?
message(paste("\n",length(unique(demo$VectorType)),'distinct VectorTypes were computed, out of',length(table(Seurat::Idents(sys.small)))^2,'total possible'))
return(demo)
}
Best, Ayu
Hi,
This is a bug related to the version of Seurat. The recent version on the master branch should fix this.
Hi all,
Thanks for your help. I tried redownloading the master branch, but unfortunately this didn't fix the error. I dug into @ayusyarif 's comment and found that getSeuratAssay
was throwing an error for me when the return
command is used. I edited the wrapper function to remove return
, which seemed to fix things and enable compatibility with my current versions of R and Seurat:
getSeuratAssay <- function (object, assay, slot)
{
if (SeuratObject::Version(object) >= "5.0.0")
Seurat::GetAssayData(object, assay = assay, layer = slot)
else Seurat::GetAssayData(object, assay = assay, slot = slot)
}
Hi,
I got an error when running RunNiches() stating that
Compute edgelist based on mutual nearest neighbors. Error in .make_numeric_version(x, strict, .standard_regexps()$valid_numeric_version) : invalid non-character version specification 'x' (type: double)
does anyone know how to solve this problem? I have reinstalled the package but still same.
best regards, Ayu