The SpatialExperiment package provides classes and methods for single cell spatial data handling.
The VisiumExperiment class exteds the SpatialExperiment class by providing more specific attributes and methods for the 10x-Genomics Visium experiments data.
This vignettes shows how to create a VisiumExperiment class by loading a Mouse Coronal 10x-Genomics Visium spatial dataset.
Dataset downloaded from 10x-Genomics website .
if(!requireNamespace("BiocManager", quietly = TRUE))
    install.packages("BiocManager")
BiocManager::install("SpatialExperiment")library("SpatialExperiment")
library("Matrix")
library("rjson")Examples data files have been downloaded from the 10x site, Mouse adult brain
Loading features and count matrix.
barcodesFile <- system.file(file.path("extdata", "10x_visium",
                            "barcodes.tsv"), package="SpatialExperiment")
barcodesEx <- read.csv(barcodesFile, sep="\t", 
                     header=FALSE, col.names=c("Cell_ID"))
featuresFile <- system.file(file.path("extdata", "10x_visium",
                            "features.tsv"), package="SpatialExperiment")
featuresEx <- read.csv(featuresFile, sep="\t", 
                     header=FALSE, col.names=c("Feature_ID", "Feature_name", 
                                               "Feature_type"))
countsFile <- system.file(file.path("extdata", "10x_visium",
                            "matrix.mtx"), package="SpatialExperiment")
countsEx <- readMM(file=countsFile)Loading Spatial coordinates of the tissue section.
posFile <- system.file(file.path("extdata", "10x_visium",
                        "tissue_positions_list.tsv"), 
                        package="SpatialExperiment")
tissPosEx <- read.csv(posFile, 
                        sep="\t", header=FALSE, 
                        col.names=c("Cell_ID", "in_tissue", 
                                    "array_row", "array_col",
                                    "pxl_col_in_fullres", "pxl_row_in_fullres"))10x-Genomics Visium experiments comes out with images in different resolutions for tissue spots locations.
imageFilePath <- list.files(system.file(file.path("extdata", "10x_visium", 
                              "images"), package="SpatialExperiment"), 
                            full.names=TRUE)Scale factors for the given three 10x Visium image formats transformations.
scaleFile <- system.file(file.path("extdata", "10x_visium",
                        "scalefactors_json.json"), 
                        package="SpatialExperiment")
scalefactors <- fromJSON(file=scaleFile)Creating VisiumExperiment class within 10x Visium data.
ve <- VisiumExperiment(rowData=featuresEx, colData=barcodesEx, 
                            assays=c(counts=countsEx), 
                            spatialCoords=tissPosEx,
                            scaleFactors=scalefactors)
ve## class: VisiumExperiment 
## dim: 50 50 
## metadata(0):
## assays(1): counts
## rownames: NULL
## rowData names(3): Feature_ID Feature_name Feature_type
## colnames: NULL
## colData names(1): Cell_ID
## reducedDimNames(0):
## altExpNames(0):
## spatialCoordinates(6): Cell_ID in_tissue ... pxl_col_in_fullres
##   pxl_row_in_fullres
## inTissue(1): 22
## imagePaths(0):The spatialCoords methods can be used to retrieve the spatial coordinates., It can be used also to update the values.
spatialCoords(ve)## DataFrame with 50 rows and 6 columns
##                Cell_ID in_tissue array_row array_col pxl_col_in_fullres
##            <character> <integer> <integer> <integer>          <integer>
## 1   AAACAACGAATAGTTC-1         0         0        16               1252
## 2   AAACAAGTATCTCCCA-1         1        50       102               7237
## 3   AAACAATCTACTAGCA-1         1         3        43               1611
## 4   AAACACCAATAACTGC-1         1        59        19               8315
## 5   AAACAGAGCGACTCCT-1         1        14        94               2927
## ...                ...       ...       ...       ...                ...
## 46  AAAGGTCAACGACATG-1         0         0       112               1251
## 47  AAAGTAGCATTGCTCA-1         1        51        27               7357
## 48  AAAGTCACTGATGTAA-1         1        10        52               2449
## 49  AAAGTCGACCCTCAGT-1         1        37        15               5681
## 50  AAAGTGCCATCAATTA-1         0        63       125               8793
##     pxl_row_in_fullres
##              <integer>
## 1                 2312
## 2                 8230
## 3                 4170
## 4                 2519
## 5                 7679
## ...                ...
## 46                8917
## 47                3069
## 48                4789
## 49                2244
## 50                9812Image paths can be handled with the VisiumExperiment class by aid of the
imagePaths methods.
imagePaths(ve) <- imageFilePath
imagePaths(ve)## [1] "/tmp/RtmpzHSW63/Rinst2d1f10c5d97a/SpatialExperiment/extdata/10x_visium/images/tissue_lowres_image.png"Scale Factors stored during VisiumExperiment class creation can be get/set
with scaleFactors method.
scaleFactors(ve)## $spot_diameter_fullres
## [1] 89.44476
## 
## $tissue_hires_scalef
## [1] 0.1701114
## 
## $fiducial_diameter_fullres
## [1] 144.4877
## 
## $tissue_lowres_scalef
## [1] 0.05103343The isInTissue method can be used to know which and how many spots are covered by the tissue.
sum(isInTissue(ve))## [1] 22ve[isInTissue(ve)]## class: VisiumExperiment 
## dim: 22 50 
## metadata(0):
## assays(1): counts
## rownames: NULL
## rowData names(3): Feature_ID Feature_name Feature_type
## colnames: NULL
## colData names(1): Cell_ID
## reducedDimNames(0):
## altExpNames(0):
## spatialCoordinates(6): Cell_ID in_tissue ... pxl_col_in_fullres
##   pxl_row_in_fullres
## inTissue(1): 22
## imagePaths(1):
##   /tmp/RtmpzHSW63/Rinst2d1f10c5d97a/SpatialExperiment/extdata/10x_visium/images/tissue_lowres_image.pngsessionInfo()## R version 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.5 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.12-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.12-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        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       
## 
## attached base packages:
## [1] parallel  stats4    stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
##  [1] rjson_0.2.20                Matrix_1.2-18              
##  [3] SpatialExperiment_1.0.0     SingleCellExperiment_1.12.0
##  [5] SummarizedExperiment_1.20.0 Biobase_2.50.0             
##  [7] GenomicRanges_1.42.0        GenomeInfoDb_1.26.0        
##  [9] IRanges_2.24.0              S4Vectors_0.28.0           
## [11] BiocGenerics_0.36.0         MatrixGenerics_1.2.0       
## [13] matrixStats_0.57.0          BiocStyle_2.18.0           
## 
## loaded via a namespace (and not attached):
##  [1] knitr_1.30             XVector_0.30.0         magrittr_1.5          
##  [4] zlibbioc_1.36.0        lattice_0.20-41        rlang_0.4.8           
##  [7] stringr_1.4.0          tools_4.0.3            grid_4.0.3            
## [10] xfun_0.18              htmltools_0.5.0        yaml_2.2.1            
## [13] digest_0.6.27          bookdown_0.21          GenomeInfoDbData_1.2.4
## [16] BiocManager_1.30.10    bitops_1.0-6           RCurl_1.98-1.2        
## [19] evaluate_0.14          rmarkdown_2.5          DelayedArray_0.16.0   
## [22] stringi_1.5.3          compiler_4.0.3