Transfer Seurat 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.
seu2seuList(seu, batch, verbose = TRUE)
a Seurat object with multiple data bathes information. It must at least contain the batch(sampel) id in the meta.data (i.e., sample_id), the raw gene count expression matrix ans sptial coordinates. And the batch must be specified (i.e., batch='sample_id').
a string to specify the data batch field in the meta.data of seu.
an optional logical value, whether output the information.
Return a list of multiple Seurat objects, where for each Seurat object, the spatial coordinates information is saved in the metadata of Seurat, named "row" and "col".
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))
seu <- spe2seurat(spe)
#> Transfer SpatialExperiment object to a Seurat object
head(seu)
#> orig.ident nCount_RNA nFeature_RNA
#> spot1 SeuratProject 9 7
#> spot2 SeuratProject 22 13
#> spot3 SeuratProject 11 9
#> spot4 SeuratProject 20 12
#> spot5 SeuratProject 18 9
#> spot6 SeuratProject 1 1
#> spot7 SeuratProject 7 6
#> spot8 SeuratProject 2 2
#> spot9 SeuratProject 1 1
#> spot10 SeuratProject 3 2
#> Sample
#> spot1 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot2 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot3 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot4 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot5 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot6 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot7 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot8 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot9 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot10 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> Barcode sample_id row col
#> spot1 AAACAACGAATAGTTC-1 foo 2312 1252
#> spot2 AAACAAGTATCTCCCA-1 foo 8230 7237
#> spot3 AAACAATCTACTAGCA-1 foo 4170 1611
#> spot4 AAACACCAATAACTGC-1 foo 2519 8315
#> spot5 AAACAGAGCGACTCCT-1 foo 7679 2927
#> spot6 AAACAGCTTTCAGAAG-1 foo 1831 6400
#> spot7 AAACAGGGTCTATATT-1 foo 2106 6879
#> spot8 AAACAGTGTTCCTGGG-1 foo 4170 9991
#> spot9 AAACATGGTGAGAGGA-1 foo 1212 8674
#> spot10 AAACATTTCCCGGATT-1 foo 7886 8554
seu2 <- seu
seu2$sample_id <- paste0(seu2$sample_id, "2")
library(SeuratObject)
seu2 <- RenameCells(seu2, paste0(colnames(seu2), "2") )
seu_all <- merge(seu, seu2)
head(seu_all@meta.data)
#> orig.ident nCount_RNA nFeature_RNA
#> spot1 SeuratProject 9 7
#> spot2 SeuratProject 22 13
#> spot3 SeuratProject 11 9
#> spot4 SeuratProject 20 12
#> spot5 SeuratProject 18 9
#> spot6 SeuratProject 1 1
#> Sample
#> spot1 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot2 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot3 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot4 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot5 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> spot6 B:/ProgramFiles/R/R-4.1.2/library/SpatialExperiment/extdata/10xVisium/section1/raw_feature_bc_matrix
#> Barcode sample_id row col
#> spot1 AAACAACGAATAGTTC-1 foo 2312 1252
#> spot2 AAACAAGTATCTCCCA-1 foo 8230 7237
#> spot3 AAACAATCTACTAGCA-1 foo 4170 1611
#> spot4 AAACACCAATAACTGC-1 foo 2519 8315
#> spot5 AAACAGAGCGACTCCT-1 foo 7679 2927
#> spot6 AAACAGCTTTCAGAAG-1 foo 1831 6400
table(seu_all$sample_id)
#>
#> foo foo2
#> 50 50
seuList <- seu2seuList(seu_all, batch='sample_id')
#> Transfer one Seurat object by its samples(batches) to a Seurat list object for preparation for PRECAST model fitting.
seuList
#> [[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=seuList, 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