alabaster.spatial 1.2.0
The SpatialExperiment
class (from the SpatialExperiment package) provides a representation of spatial transcriptomics data that is compatible with Bioconductor’s SummarizedExperiment
ecosystem.
The alabaster.spatial package contains methods to save and load SpatialExperiment
objects into and out of file.
Check out the alabaster.base for more details on the motivation and concepts of the alabaster framework.
To demonstrate, we’ll use the example dataset provided in the SpatialExperiment package:
library(SpatialExperiment)
example(read10xVisium, echo=FALSE)
# Making the column names unique.
colnames(spe) <- make.unique(colnames(spe))
spe
## class: SpatialExperiment
## dim: 50 99
## metadata(0):
## assays(1): counts
## rownames(50): ENSMUSG00000051951 ENSMUSG00000089699 ...
## ENSMUSG00000005886 ENSMUSG00000101476
## rowData names(1): symbol
## colnames(99): AAACAACGAATAGTTC-1 AAACAAGTATCTCCCA-1 ...
## AAAGTCGACCCTCAGT-1.1 AAAGTGCCATCAATTA-1.1
## colData names(4): in_tissue array_row array_col sample_id
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : pxl_col_in_fullres pxl_row_in_fullres
## imgData names(4): sample_id image_id data scaleFactor
We call the usual stageObject()
function to save its contents to file:
library(alabaster.spatial)
tmp <- tempfile()
dir.create(tmp, showWarnings=FALSE)
meta <- stageObject(spe, dir=tmp, "spe", stage.only=TRUE)
.writeMetadata(meta, tmp)
## $type
## [1] "local"
##
## $path
## [1] "spe/experiment.json"
list.files(tmp, recursive=TRUE)
## [1] "spe/assay-1/matrix.h5" "spe/assay-1/matrix.h5.json"
## [3] "spe/coldata/simple.csv.gz" "spe/coldata/simple.csv.gz.json"
## [5] "spe/experiment.json" "spe/rowdata/simple.csv.gz"
## [7] "spe/rowdata/simple.csv.gz.json" "spe/spatial/coords/array.h5"
## [9] "spe/spatial/coords/array.h5.json" "spe/spatial/images/image-1.png"
## [11] "spe/spatial/images/image-1.png.json" "spe/spatial/images/image-2.png"
## [13] "spe/spatial/images/image-2.png.json"
This goes through the usual saving process for SingleCellExperiment
s, with an additional saving step for the image data (see the spatial/
subdirectory).
We can then load it back in using the loadObject()
function:
remeta <- acquireMetadata(tmp, "spe/experiment.json")
roundtrip <- loadObject(remeta, tmp)
spe2
## class: SpatialExperiment
## dim: 50 99
## metadata(0):
## assays(1): counts
## rownames(50): ENSMUSG00000051951 ENSMUSG00000089699 ...
## ENSMUSG00000005886 ENSMUSG00000101476
## rowData names(1): symbol
## colnames(99): AAACAACGAATAGTTC-1 AAACAAGTATCTCCCA-1 ...
## AAAGTCGACCCTCAGT-1 AAAGTGCCATCAATTA-1
## colData names(4): in_tissue array_row array_col sample_id
## reducedDimNames(0):
## mainExpName: NULL
## altExpNames(0):
## spatialCoords names(2) : pxl_col_in_fullres pxl_row_in_fullres
## imgData names(4): sample_id image_id data scaleFactor
plot(imgRaster(getImg(spe2, "section1")))
More details on the metadata and on-disk layout are provided in the schema.
sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: America/New_York
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] HDF5Array_1.30.0 DelayedArray_0.28.0
## [3] alabaster.spatial_1.2.0 alabaster.base_1.2.0
## [5] Matrix_1.6-1.1 SpatialExperiment_1.12.0
## [7] SingleCellExperiment_1.24.0 SummarizedExperiment_1.32.0
## [9] Biobase_2.62.0 GenomicRanges_1.54.0
## [11] GenomeInfoDb_1.38.0 IRanges_2.36.0
## [13] S4Vectors_0.40.0 BiocGenerics_0.48.0
## [15] MatrixGenerics_1.14.0 matrixStats_1.0.0
## [17] BiocStyle_2.30.0
##
## loaded via a namespace (and not attached):
## [1] rjson_0.2.21 xfun_0.40
## [3] bslib_0.5.1 rhdf5_2.46.0
## [5] lattice_0.22-5 rhdf5filters_1.14.0
## [7] tools_4.3.1 bitops_1.0-7
## [9] curl_5.1.0 parallel_4.3.1
## [11] R.oo_1.25.0 sparseMatrixStats_1.14.0
## [13] dqrng_0.3.1 GenomeInfoDbData_1.2.11
## [15] compiler_4.3.1 statmod_1.5.0
## [17] alabaster.se_1.2.0 codetools_0.2-19
## [19] htmltools_0.5.6.1 sass_0.4.7
## [21] alabaster.matrix_1.2.0 RCurl_1.98-1.12
## [23] yaml_2.3.7 crayon_1.5.2
## [25] jquerylib_0.1.4 R.utils_2.12.2
## [27] BiocParallel_1.36.0 jsonvalidate_1.3.2
## [29] cachem_1.0.8 limma_3.58.0
## [31] magick_2.8.1 abind_1.4-5
## [33] locfit_1.5-9.8 digest_0.6.33
## [35] bookdown_0.36 fastmap_1.1.1
## [37] grid_4.3.1 cli_3.6.1
## [39] SparseArray_1.2.0 magrittr_2.0.3
## [41] S4Arrays_1.2.0 edgeR_4.0.0
## [43] DelayedMatrixStats_1.24.0 rmarkdown_2.25
## [45] XVector_0.42.0 DropletUtils_1.22.0
## [47] R.methodsS3_1.8.2 beachmat_2.18.0
## [49] alabaster.sce_1.2.0 evaluate_0.22
## [51] knitr_1.44 V8_4.4.0
## [53] rlang_1.1.1 Rcpp_1.0.11
## [55] scuttle_1.12.0 BiocManager_1.30.22
## [57] alabaster.ranges_1.2.0 alabaster.schemas_1.2.0
## [59] jsonlite_1.8.7 R6_2.5.1
## [61] Rhdf5lib_1.24.0 zlibbioc_1.48.0