Transfer SpatialExperiment object to a Seurat object for preparation for DR.SC model fitting.
spe2seurat(spe, verbose = TRUE)
a SpatialExperiment object, at least including the raw gene count expression matrix ans sptial coordinates.
an optional logical value, whether output the information.
Return a Seurat object, where the spatial coordinates information is saved in the metadata of Seurat, named "row" and "col".
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
#'#' Fit DR-SC model
library(DR.SC)
library(Seurat)
seu <- NormalizeData(seu)
seu <- FindVariableFeatures(seu)
seu <- DR.SC(seu = seu, K=4)
#> Neighbors were identified for 0 out of 50 spots.
#> Fit DR-SC model...
#> Using accurate PCA to obtain initial values
#> Finish DR-SC model fitting