Skip to contents

The SpatialExperiment class provides a family of methods to get and set spatial data attributes in SpatialExperiment objects. Spatial attributes include spatialCoords, imgData, and scaleFactors, as well as methods to rotate and mirror SpatialExperiment objects and their spatial coordinates.

Usage

# S4 method for class 'SpatialExperiment'
spatialData(x)

# S4 method for class 'SpatialExperiment,DFrame'
spatialData(x) <- value

# S4 method for class 'SpatialExperiment,`NULL`'
spatialData(x) <- value

# S4 method for class 'SpatialExperiment'
spatialDataNames(x)

# S4 method for class 'SpatialExperiment,character'
spatialDataNames(x) <- value

# S4 method for class 'SpatialExperiment,`NULL`'
spatialDataNames(x) <- value

# S4 method for class 'SpatialExperiment'
spatialCoords(x)

# S4 method for class 'SpatialExperiment,matrix'
spatialCoords(x) <- value

# S4 method for class 'SpatialExperiment,`NULL`'
spatialCoords(x) <- value

# S4 method for class 'SpatialExperiment'
spatialCoordsNames(x)

# S4 method for class 'SpatialExperiment,character'
spatialCoordsNames(x) <- value

# S4 method for class 'SpatialExperiment,`NULL`'
spatialCoordsNames(x) <- value

# S4 method for class 'SpatialExperiment'
scaleFactors(x, sample_id = TRUE, image_id = TRUE)

# S4 method for class 'SpatialExperiment'
x$name

# S4 method for class 'SpatialExperiment'
imgData(x)

# S4 method for class 'SpatialExperiment,DataFrame'
imgData(x) <- value

# S4 method for class 'SpatialExperiment,`NULL`'
imgData(x) <- value

Arguments

x

A SpatialExperiment object.

value

Replacement value for replacement methods.

sample_id

Logical value or character vector specifying sample identifier(s) for scaleFactors. Default = TRUE (all samples).

image_id

Logical value or character vector specifying image identifier(s) for scaleFactors. Default = TRUE (all images).

name

The name of the colData column to extract.

Value

Return value varies depending on method, as described below.

Details

Additional details for each type of data attribute are provided below.

Note: spatialData and spatialDataNames (previously used to store a subset of columns from colData) have been deprecated. All columns should be stored in either spatialCoords (numeric matrix containing spatial coordinates) or colData (all other columns). The spatialData/spatialDataNames functionality has been retained for backward compatibility but may be removed in the future.

See rotateCoords, mirrorCoords, rotateObject, or mirrorObject for details on methods to rotate and mirror SpatialExperiment objects and their spatialCoords.

spatialData and spatialCoords methods

spatialData(x) <- value:

The spatialData setter expects a DataFrame. If the input does not contain an in_tissue column, this will be included with a default value of 1.

spatialCoords(x):

Getter for numeric matrix of spatial coordinates.

spatialCoords(x) <- value:

Setter for numeric matrix of spatial coordinates.

spatialDataNames and spatialCoordsNames methods

spatialDataNames(x):

Returns the names of the colData associated with the spatial information, which are stored in the int_metadata.

spatialDataNames(x) <- value:

Setter to replace column names in the spatialData DataFrame.

spatialCoordsNames(x):

Returns the defined names of the spatial coordinates (e.g. c("x", "y")).

spatialCoordsNames(x) <- value:

Setter to define the names of the spatial coordinate columns.

imgData methods

imgData(x):

Getter to return the imgData DataFrame.

imgData(x) <- value:

Setter to provide a DataFrame object as imgData of the SpatialExperiment object.

Other methods

scaleFactors(x, sample_id, image_id):

Getter to return the scale factors associated with the sample_id(s) and image_id(s) provided. This is related to the stored image(s) in the SpatialExperiment imgData structure. See argument descriptions for further details.

Examples

example(read10xVisium)
#> 
#> rd10xV> dir <- system.file(
#> rd10xV+   file.path("extdata", "10xVisium"), 
#> rd10xV+   package = "SpatialExperiment")
#> 
#> rd10xV> sample_ids <- c("section1", "section2")
#> 
#> rd10xV> samples <- file.path(dir, sample_ids, "outs")
#> 
#> rd10xV> list.files(samples[1])
#> [1] "raw_feature_bc_matrix" "spatial"              
#> 
#> rd10xV> list.files(file.path(samples[1], "spatial"))
#> [1] "scalefactors_json.json"    "tissue_lowres_image.png"  
#> [3] "tissue_positions_list.csv"
#> 
#> rd10xV> file.path(samples[1], "raw_feature_bc_matrix")
#> [1] "/tmp/RtmppdZi9A/temp_libpathcf263c794d1/SpatialExperiment/extdata/10xVisium/section1/outs/raw_feature_bc_matrix"
#> 
#> rd10xV> (spe <- read10xVisium(samples, sample_ids, 
#> rd10xV+   type = "sparse", data = "raw", 
#> rd10xV+   images = "lowres", load = FALSE))
#> 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
#> 
#> rd10xV> # base directory 'outs/' from Space Ranger can also be omitted
#> rd10xV> samples2 <- file.path(dir, sample_ids)
#> 
#> rd10xV> (spe2 <- read10xVisium(samples2, sample_ids, 
#> rd10xV+   type = "sparse", data = "raw", 
#> rd10xV+   images = "lowres", load = FALSE))
#> 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
#> 
#> rd10xV> # tabulate number of spots mapped to tissue
#> rd10xV> cd <- colData(spe)
#> 
#> rd10xV> table(
#> rd10xV+   in_tissue = cd$in_tissue, 
#> rd10xV+   sample_id = cd$sample_id)
#>          sample_id
#> in_tissue section1 section2
#>     FALSE       28       27
#>     TRUE        22       22
#> 
#> rd10xV> # view available images
#> rd10xV> imgData(spe)
#> DataFrame with 2 rows and 4 columns
#>     sample_id    image_id   data scaleFactor
#>   <character> <character> <list>   <numeric>
#> 1    section1      lowres   ####   0.0510334
#> 2    section2      lowres   ####   0.0510334

# spatialCoords returns a numeric matrix
head(spatialCoords(spe))
#>                    pxl_col_in_fullres pxl_row_in_fullres
#> AAACAACGAATAGTTC-1               2312               1252
#> AAACAAGTATCTCCCA-1               8230               7237
#> AAACAATCTACTAGCA-1               4170               1611
#> AAACACCAATAACTGC-1               2519               8315
#> AAACAGAGCGACTCCT-1               7679               2927
#> AAACAGCTTTCAGAAG-1               1831               6400

# change spatial coordinate names
spatialCoordsNames(spe)
#> [1] "pxl_col_in_fullres" "pxl_row_in_fullres"
spatialCoordsNames(spe) <- c("x", "y")
head(spatialCoords(spe))
#>                       x    y
#> AAACAACGAATAGTTC-1 2312 1252
#> AAACAAGTATCTCCCA-1 8230 7237
#> AAACAATCTACTAGCA-1 4170 1611
#> AAACACCAATAACTGC-1 2519 8315
#> AAACAGAGCGACTCCT-1 7679 2927
#> AAACAGCTTTCAGAAG-1 1831 6400

# imgData and scale factors
imgData(spe)
#> DataFrame with 2 rows and 4 columns
#>     sample_id    image_id   data scaleFactor
#>   <character> <character> <list>   <numeric>
#> 1    section1      lowres   ####   0.0510334
#> 2    section2      lowres   ####   0.0510334
scaleFactors(spe)
#> [1] 0.05103343 0.05103343

# tabulate number of spots mapped to tissue
cd <- colData(spe)
table(
  in_tissue = cd$in_tissue, 
  sample_id = cd$sample_id)
#>          sample_id
#> in_tissue section1 section2
#>     FALSE       28       27
#>     TRUE        22       22