Transfer SpatialExperiment object to a Seurat list object for preparation for PRECAST model fitting; see our [PRECAST package website](https://feiyoung.github.io/PRECAST/index.html) for more usage of PRECAST.

spe2seuratList(spe, batch = NULL, verbose = TRUE)

Arguments

spe

a SpatialExperiment object or a list consisting of SpatialExperiment objects. If spe is a SpatialExperiment object, it must at least contain the batch(sampel) id in the colData (i.e., sample_id), the raw gene count expression matrix and spatial coordinates. And the batch must be specified (i.e., batch='sample_id'). If spe is a list consisting of multiple SpatialExperiment objects, then each object represents a data batch.

batch

a optional argument, NULL or a string. Only if spe is a list, batch can be NULL.

verbose

an optional logical value, whether output the information.

Value

Return a list consisting of Seurat objects, where the spatial coordinates information is saved in the metadata of Seurat, named "row" and "col".

Examples

suppressPackageStartupMessages( library(SpatialExperiment))
dir <- system.file(
  file.path("extdata", "10xVisium", "section1"),
  package = "SpatialExperiment")

# read in counts
fnm <- file.path(dir, "raw_feature_bc_matrix")
sce <- DropletUtils::read10xCounts(fnm)

# read in image data
img <- readImgData(
  path = file.path(dir, "spatial"),
  sample_id="foo")

# read in spatial coordinates
fnm <- file.path(dir, "spatial", "tissue_positions_list.csv")
xyz <- read.csv(fnm, header = FALSE,
                col.names = c(
                  "barcode", "in_tissue", "array_row", "array_col",
                  "pxl_row_in_fullres", "pxl_col_in_fullres"))

# construct observation & feature metadata
rd <- S4Vectors::DataFrame(
  symbol = rowData(sce)$Symbol)

# construct 'SpatialExperiment'
(spe <- SpatialExperiment(
  assays = list(counts = assay(sce)),
  colData = colData(sce), rowData = rd, imgData = img,
  spatialData=DataFrame(xyz),
  spatialCoordsNames=c("pxl_col_in_fullres", "pxl_row_in_fullres"),
  sample_id="foo"))
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> class: SpatialExperiment 
#> dim: 50 50 
#> metadata(0):
#> assays(1): counts
#> rownames(50): ENSMUSG00000051951 ENSMUSG00000089699 ...
#>   ENSMUSG00000005886 ENSMUSG00000101476
#> rowData names(1): symbol
#> colnames: NULL
#> colData names(3): Sample Barcode sample_id
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> reducedDimNames(0):
#> mainExpName: NULL
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> altExpNames(0):
#> spatialData names(4) : barcode in_tissue array_row array_col
#> spatialCoords names(2) : pxl_col_in_fullres pxl_row_in_fullres
#> imgData names(4): sample_id image_id data scaleFactor
colnames(spe) <- paste0("spot", 1:ncol(spe))


## Transfer a SpatialExperiment to a seuList
colData(spe)$batch_id <- rep(c("a", "b"), each=25)
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
seuList1 <- spe2seuratList(spe, batch = 'batch_id')
#> Transfer one SpatialExperiment object by its samples(batches)  to a Seurat list object for preparation for PRECAST model fitting.
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> Transfer SpatialExperiment object to a Seurat object
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> Found more than one class "SpatialImage" in cache; using the first, from namespace 'SeuratObject'
#> Also defined by ‘SpatialExperiment’
#> Transfer SpatialExperiment object to a Seurat object
seuList1
#> [[1]]
#> An object of class Seurat 
#> 50 features across 25 samples within 1 assay 
#> Active assay: RNA (50 features, 0 variable features)
#> 
#> [[2]]
#> An object of class Seurat 
#> 50 features across 25 samples within 1 assay 
#> Active assay: RNA (50 features, 0 variable features)
#> 
## Transfer a list of SpatialExperiment to a seuList
seuList2 <- spe2seuratList(list(spe, spe))
#> Transfer one SpatialExperiment object by its samples(batches)  to a Seurat list object for preparation for PRECAST model fitting.
#> 
  |                                                  | 0 % ~calculating  
#> Transfer SpatialExperiment object to a Seurat object
#> 
  |+++++++++++++++++++++++++                         | 50% ~00s          
#> Transfer SpatialExperiment object to a Seurat object
#> 
  |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s  
seuList2
#> [[1]]
#> An object of class Seurat 
#> 50 features across 50 samples within 1 assay 
#> Active assay: RNA (50 features, 0 variable features)
#> 
#> [[2]]
#> An object of class Seurat 
#> 50 features across 50 samples within 1 assay 
#> Active assay: RNA (50 features, 0 variable features)
#> 
## Create PRECAST object
library(PRECAST)
Pobject <- CreatePRECASTObject(seuList=seuList2, selectGenesMethod = "HVGs",
                               premin.spots = 0, premin.features = 0,
                               postmin.features = 0, postmin.spots = 0, verbose = F)
#> 
  |                                                  | 0 % ~calculating  
  |+++++++++++++++++++++++++                         | 50% ~00s          
  |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s  
#>  
#>  
#> 
  |                                                  | 0 % ~calculating  
  |+++++++++++++++++++++++++                         | 50% ~00s          
  |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s  
#> Warning: Number of genes in one of sample is less than 2000, so set minimum number of SVGs as gene.number=31
#> 
  |                                                  | 0 % ~calculating  
  |+++++++++++++++++++++++++                         | 50% ~00s          
  |++++++++++++++++++++++++++++++++++++++++++++++++++| 100% elapsed=00s