SpatialExperiment icon indicating copy to clipboard operation
SpatialExperiment copied to clipboard

Convert Seurat to SpatialExperiment

Open MarcElosua opened this issue 2 years ago • 3 comments

Good afternoon SpatialExperiment team,

Thank you so much for building up SpatialExperiment! As I'm transition from Seurat to SpatialExperiment I wondered f there was a way to convert Seurat objecs to SpatialExperiments. As I didn't see any function doing that I put together a little function to help me convert my data. Is creating such a function in the works?

I'm leaving a simple example here in case anyone wonders how I did it or if it can help in any way:

library(SpatialExperiment)
library(Seurat)
library(SeuratData)
# BiocManager::iBiocManager::install("SpatialExperiment")nstall("SpatialExperiment")
library(dplyr)

# SeuratData::InstallData(ds = "stxBrain")
SeuratData::LoadData(ds = "tonsilref")
se1 <- SeuratData::LoadData(ds = "stxBrain", type = "posterior1")
se2 <- SeuratData::LoadData(ds = "stxBrain", type = "anterior1")

## Function
seurat_to_spe <- function(seu, sample_id, img_id) {
    ## Convert to SCE
    sce <- Seurat::as.SingleCellExperiment(seu)
    
    ## Extract spatial coordinates
    spatialCoords <- as.matrix(
        seu@images[[img_id]]@coordinates[, c("imagecol", "imagerow")])
    
    ## Extract and process image data
    img <- SpatialExperiment::SpatialImage(
        x = as.raster(seu@images[[img_id]]@image))
    
    imgData <- DataFrame(
        sample_id = sample_id,
        image_id = img_id,
        data = I(list(img)),
        scaleFactor = seu@images[[img_id]]@scale.factors$lowres)
    
    # Convert to SpatialExperiment
    spe <- SpatialExperiment(
        assays = assays(sce),
        rowData = rowData(sce),
        colData = colData(sce),
        metadata = metadata(sce),
        reducedDims = reducedDims(sce),
        altExps = altExps(sce),
        sample_id = sample_id,
        spatialCoords = spatialCoords,
        imgData = imgData
    )
    # indicate all spots are on the tissue
    spe$in_tissue <- 1
    spe$sample_id <- sample_id
    # Return Spatial Experiment object
    spe
}

se_ls <- list("posterior1" = se1, "anterior1" = se2)
spe_ls <- lapply(names(se_ls), function(i) {
    seurat_to_spatialexperiment(seu = se_ls[[i]], sample_id = i, img_id = i)
})

spe <- Reduce(cbind, spe_ls)
spe

Thanks a lot for the great work! Marc

MarcElosua avatar Sep 19 '22 16:09 MarcElosua

Thank you so much for sharing this @MarcElosua !

erkinacar5 avatar Nov 02 '22 09:11 erkinacar5

Hi @MarcElosua,

sorry for the late answer, this is really useful, the only thing that brakes me from including it in the package are the dependencies (we already have a lot of them), maybe we can think of creating a tools package built around the SpatialExperiment for including functions like this one.

Also, the other problem with Seurat is related to continuous updates they do to the class, without considering their previous versions... And this could be a real problem.

Maybe with @HelenaLC and @lmweber we can discuss a little bit about this...

drighelli avatar Nov 02 '22 09:11 drighelli

Thank you very much to for the code. Thanks to it I have solved the problem with spatal deconvolution which I was struggling over for months.

MarcinRuc avatar Apr 30 '24 14:04 MarcinRuc