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

Exporting Visium data from Seurat into BayesSpace #53

Closed PIR015 closed 2 years ago

PIR015 commented 3 years ago

Hi,

I have done the Seurat workflow for individual samples obtained with Visium Spatial Transcriptomics and have then merged them all into a Seurat object.

From the step of converting to SCE as mentioned in a previous post, I came across an error.

CODE:

Convert to SCE

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

When running the last line, I came across this error: Error in DataFrame(..., check.names = FALSE) : different row counts implied by arguments

My merged Seurat object has got 5 images (slice1, slice1.1, etc), could this be an issue?

Is there a way to do this with merged data instead of data from one single sample, keeping the spatial information from each separate? Many thanks in advance!

edward130603 commented 3 years ago

Yes, it seems like your error occured because the two arguments to cbind don't have the same number of rows. colData(sce) would have the total # of rows for all slices while the second argument only has the coordinates for slice1. Can you first do something like this:

coord1 = mergedBaselinesH@images$slice1@coordinates
coord2 = mergedBaselinesH@images$slice1.1@coordinates
coord3 = mergedBaselinesH@images$slice1.2@coordinates #replace 1.2, 1.3, 1.4 with actual names of your slices
coord4 = mergedBaselinesH@images$slice1.3@coordinates
coord5 = mergedBaselinesH@images$slice1.4@coordinates
coords = cbind(coord1, coord2, coord3, coord4, coord5)

colData(sce) = cbind(colData(sce), coords)

Does this work? Note your original last line seems to have an extra c( and so there's a parenthesis mismatch.

PIR015 commented 3 years ago

Thank you for the quick response. I have tried that and have gotten a different error:

coords = cbind(coord1, coord2, coord3, coord4, coord5) Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 1397, 1993, 478, 874, 551

Because of the number of rows being different I have also tried merge() instead, two different ways, and gotten another set of errors:

coords <- merge(coord1, coord2, coord3, coord4, coord5) Error in fix.by(by.x, x) : 'by' must specify one or more columns as numbers, names or logical coords <- merge(x= coord1, c(coord2, coord3, coord4, coord5)) Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1993, 478, 874, 551

Thank you in advance!

edward130603 commented 3 years ago

Oops sorry that should be rbind instead of cbind:

coords = rbind(coord1, coord2, coord3, coord4, coord5)

The order of the slices should be the same as in your seurat object.

PIR015 commented 3 years ago

Hi,

This has worked, thank you!

I have another question. Is there a way to subset this per slice? As in, using the data provided from the Seurat object (mergedBaselines) and apply it to the different slices using BayesSpace? My "slices" are different patient samples of the same condition, the mergedBaselines has all the gene expression data from the slices merged into one to find all clusters for the merged data. For example, for SpatialDimPlot(mergedBaselines) I get the clustering spots over the images. I would like to do the same but for BayesSpace. Is this possible?

The output I get from clusterPlot(sce) doesn't seem to match any of my image slices.

Thank you once again!

edward130603 commented 3 years ago

To subset your SingleCellExperiment, you can use this:

sce1 = sce[ , sce$sample == "slide1"] #replace sample with the name of the column containing sample IDs
sce1.1 = sce[ , sce$sample == "slide1.1"] #replace "slide1" or "slide1.1" with each sample ID 
#etc for each slide

Running the BayesSpace workflow for each single slice object (e.g. sce1, sce1.1) will provide the clustering results for each individual slice. If you wish to perform a joint clustering for all of your slices, then you will need to offset the spatial coordinates so that the slices are not overlaid on top of each other. We have a joint clustering vignette available on our website that provides instructions on how to add the offset, as well as other relevant steps.

clusterPlot should give you the expected visualizations in this case, though our plotting functions do not overlay spots over images. If you wish to have the image background, I would suggest copying the BayesSpace clustering output into your Seurat object's metadata and replotting. Here is some sample code to do so:

seurat@meta.data = cbind(seurat@meta.data, BayesSpace = sce$spatial.cluster) #add BayesSpace clusters to Seurat obj
SpatialDimPlot(seurat, "BayesSpace") #plot via Seurat

Here, seurat is the Seurat object and sce is the SingleCellExperiment. Note that if you run BayesSpace on individual slices, you will need to do some subsetting in Seurat as well so that the metadata gets copied over correctly.

If you run into any issues with these steps, please reach out again!