Skip to content

Convert Seurat to SpatialExperiment #115

Open

Description

@MarcElosua

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

Activity

  1. erkinacar5 commented on Nov 2, 2022

    @erkinacar5

    Thank you so much for sharing this @MarcElosua !

  2. drighelli commented on Nov 2, 2022

    @drighelli
    Owner

    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...

  3. MarcinRuc commented on Apr 30, 2024

    @MarcinRuc

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions