Skip to contents

The subsetting method for SpatialExperiment objects ensures that spatial data attributes (spatialCoords and imgData) are subsetted correctly to match rows and columns with the remainder of the object.

Arguments

x

a SpatialExperiment object

i

row indices for subsetting

j

column indices for subsetting

Value

a SpatialExperiment object

subset

[:

subsetting method

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

dim(spe)
#> [1] 50 99

set.seed(123)
idx <- sample(ncol(spe), 10)
sub <- spe[, idx]
dim(sub)
#> [1] 50 10
colData(sub)
#> DataFrame with 10 rows and 4 columns
#>                    in_tissue array_row array_col   sample_id
#>                    <logical> <integer> <integer> <character>
#> AAACTGCTGGCTCCAA-1      TRUE        45        67    section1
#> AAACTCGTGATATAAG-1     FALSE        23       113    section2
#> AAACAAGTATCTCCCA-1      TRUE        50       102    section2
#> AAACCGGGTAGGTACC-1      TRUE        42        28    section1
#> AAACGAAGAACATACC-1      TRUE         6        64    section2
#> AAAGGCTCTCGCGCCG-1      TRUE        55        55    section1
#> AAAGTGCCATCAATTA-1     FALSE        63       125    section1
#> AAAGGGATGTAGCAAG-1      TRUE        24        62    section1
#> AAAGTAGCATTGCTCA-1      TRUE        51        27    section2
#> AAACGGGTTGGTATCC-1     FALSE         1        23    section1
spatialCoords(sub)
#>                    pxl_col_in_fullres pxl_row_in_fullres
#> AAACTGCTGGCTCCAA-1               5821               6639
#> AAACTCGTGATATAAG-1               8986               4005
#> AAACAAGTATCTCCCA-1               8230               7237
#> AAACCGGGTAGGTACC-1               3138               6280
#> AAACGAAGAACATACC-1               5615               1970
#> AAAGGCTCTCGCGCCG-1               4996               7836
#> AAAGTGCCATCAATTA-1               9812               8793
#> AAAGGGATGTAGCAAG-1               5477               4125
#> AAAGTAGCATTGCTCA-1               3069               7357
#> AAACGGGTTGGTATCC-1               2794               1371