edward130603 / BayesSpace

Bayesian model for clustering and enhancing the resolution of spatial gene expression experiments.
http://edward130603.github.io/BayesSpace
Other
106 stars 21 forks source link

Running BayesSpace clustering on processed Seurat object #48

Closed jnmark closed 2 years ago

jnmark commented 3 years ago

Hi,

Thanks for developing this great tool! I was curious to know if you had any insights or suggestions on the most effective way to run BayesSpace on a fully processed Seurat object containing data from multiple batch corrected samples? Essentially, I would like to add in the clustering from BayesSpace into the Seurat object, so that all other associated metadata, cell deconvolution metrics in the object are maintained and we can really see how much BayesSpace improves those metrics.

Thanks!

edward130603 commented 3 years ago

I would suggest making a SingleCellExperiment object from your processed Seurat object and running BayesSpace. Then, you can transfer the spatial.cluster column which contains the BayesSpace clusters back into your Seurat object's metadata. I'll try to provide some sample code for how to do this.

edward130603 commented 3 years ago

Here is some code, hope this helps!

#Load libraries
library(Seurat)
#devtools::install_github('satijalab/seurat-data')
library(SeuratData)
library(BayesSpace)
InstallData("stxBrain")

#Seurat workflow
seurat = LoadData("stxBrain", type = "anterior1") #load seurat data
seurat = Seurat::FindVariableFeatures(seurat) #Simple Seurat workflow for PCs
seurat = Seurat::ScaleData(seurat)
seurat = Seurat::RunPCA(seurat)

#Convert to SCE
diet.seurat = Seurat::DietSeurat(seurat, graphs = "pca") #slim down Seurat obj prior to conversion
sce = as.SingleCellExperiment(diet.seurat) #convert seurat to SCE
colData(sce) = cbind(colData(sce), seurat@images$anterior1@coordinates) #add spatial info to SCE

#BayesSpace Workflow
sce = spatialPreprocess(sce, platform = "Visium", skip.PCA = T, log.normalize = F) #add BayesSpace metadata, without messing with PCA/logcounts
sce = spatialCluster(sce, nrep = 1000, burn.in = 100, q = 10) #quickly cluster via BayesSpace
clusterPlot(sce) #plot via BayesSpace

#Add BayesSpace results to Seurat
seurat@meta.data = cbind(seurat@meta.data, BayesSpace = sce$spatial.cluster) #add BayesSpace clusters to Seurat obj
SpatialDimPlot(seurat, "BayesSpace") #plot via Seurat
jnmark commented 3 years ago

Hi @edward130603, thanks -- that worked out for me! Just curious - how would this work for the enhanced clustering since that would generate more coordinates? Wouldn't there be a mismatch adding it back to the Seurat object and displaying?

edward130603 commented 3 years ago

Hi @jnmark. Yes, for the enhanced clustering you will have to create a new Seurat object from the enhanced SCE. Here is some code copied over from our vignette, where we used Seurat to do differential expression analysis on enhanced results:

  seurat <- Seurat::CreateSeuratObject(counts=logcounts(sce),
                                       assay='Spatial',
                                       meta.data=as.data.frame(colData(sce)))
  seurat <- Seurat::SetIdent(seurat, value = "spatial.cluster")
wfma commented 1 year ago

Just wanna say thanks folks for raising this issue! I am just getting my first spatial data and finally be able to run bayesspace (which i have been following for quite some times!)

wfma commented 1 year ago

just wanted to see if yall have any plans of making a wrapper for giotto also?

thanks!

jzheng25 commented 1 year ago

Thank you all for posting the issue as well as responding to it with a wonderful solution! I am adapting this now with merged samples (one seurat with multiple ST), and using the PCA obtained from merged and SCTransformed data.

ppm1337 commented 1 year ago

This solution doesn't work anymore due to the changes made in 7d6997c.

.find_neighbors searches for array_col and array_row instead of col and row. While it's true that the coordinates in Space Ranger's tissue_positions.csv are provided as such, the image coordinates stored in Seurat V3 objects provide tissue, row, col, imagerow, imagecol. You can check this with names(seurat@images$anterior1@coordinates) in your example provided above which doesn't work any longer because of spatialCluster failing with Error: subscript contains invalid names.

Tested with R 4.2.3, Seurat 4.3.0.1, SeuratData 0.2.2, SeuratObject 4.1.3 and BayesSpace 1.11.0.

akhmelevskaya commented 6 months ago

This solution doesn't work anymore due to the changes made in 7d6997c.

.find_neighbors searches for array_col and array_row instead of col and row. While it's true that the coordinates in Space Ranger's tissue_positions.csv are provided as such, the image coordinates stored in Seurat V3 objects provide tissue, row, col, imagerow, imagecol. You can check this with names(seurat@images$anterior1@coordinates) in your example provided above which doesn't work any longer because of spatialCluster failing with Error: subscript contains invalid names.

Tested with R 4.2.3, Seurat 4.3.0.1, SeuratData 0.2.2, SeuratObject 4.1.3 and BayesSpace 1.11.0.

Hi, I have the same error (Error: subscript contains invalid names) running spatialCluster on Seurat preprocessed object, is there a new way to do it? Or should I run BayesSpace pipeline and Seurat pipeline in parallel and just add BayesSpace clusters to seurat object, or would it be wrong?