Skip to contents

Compute the neighborhood consolidation matrix and identify spatial homogeneous regions.

Usage

calculateConcordex(x, ...)

# S4 method for class 'ANY'
calculateConcordex(
  x,
  labels,
  ...,
  n_neighbors = 30,
  compute_similarity = FALSE,
  BLUSPARAM,
  BNINDEX,
  BNPARAM = KmknnParam(),
  BPPARAM = SerialParam()
)

# S4 method for class 'SummarizedExperiment'
calculateConcordex(x, labels, ..., assay.type = "logcounts")

# S4 method for class 'SingleCellExperiment'
calculateConcordex(x, labels, ..., use.dimred = NULL)

# S4 method for class 'SpatialExperiment'
calculateConcordex(x, labels, ..., use.spatial = TRUE)

Arguments

x

A SpatialExperiment, SpatialFeatureExperiment, SingleCellExperiment, or SummarizedExperiment object containing a count matrix.

Otherwise, a numeric matrix-like object containing counts for observations (e.g. cells/spots) on the rows and features on the columns.

...

Other parameters passed to default method

labels

Observation labels used to compute the neighborhood consolidation matrix. Continuous or discrete labels are allowed, and typically, integer labels are assumed to be discrete.

Labels can be specified as follows:

  • A vector or matrix-like object with one entry per observation If a matrix, the observations should be on the rows and the label identifiers on the columns.

  • If x inherits from SummarizedExperiment-class, a string or character vector specifying the names of columns in colData(x) or the name of a dimensionality reduction result (see reducedDimNames)

n_neighbors

Number of neighbors to expect for each observation. Defaults to 30.

compute_similarity

Logical. Whether to return the label similarity matrix. Only useful if discrete labels are provided.

BLUSPARAM

A BlusterParam-class object specifying the clustering algorithm to use to identify spatial homogeneous regions. If this parameter is not specified, then regions are not returned. By default, this parameter is missing.

BNINDEX

A BiocNeighborIndex object containing the precomputed index information, see findKNN.

BNPARAM

A BiocNeighborParam object specifying the algorithm to use. This can be missing if BNINDEX is supplied, see findKNN.

BPPARAM

A BiocParallelParam object specifying whether and how computing the metric for numerous observations shall be parallelized (see bpparam).

assay.type

String or integer scalar indicating the assay of x containing the counts.

use.dimred

Integer or string specifying the reduced dimensions to use for construction of the k-nearest neighbor graph. Note that if this is not NULL, reduced dimensions can not be used as labels to compute the neighborhood consolidation matrix.

use.spatial

Logical, should the spatial coordinates be used to compute the k-nearest neighbor graph?

Value

A sparse matrix

Examples

example(read10xVisium, "SpatialExperiment")
#> Loading required package: SingleCellExperiment
#> Loading required package: SummarizedExperiment
#> Loading required package: MatrixGenerics
#> Loading required package: matrixStats
#> 
#> Attaching package: ‘MatrixGenerics’
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
#>     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
#>     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
#>     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
#>     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
#>     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
#>     colWeightedMeans, colWeightedMedians, colWeightedSds,
#>     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
#>     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
#>     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
#>     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
#>     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
#>     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
#>     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
#>     rowWeightedSds, rowWeightedVars
#> Loading required package: GenomicRanges
#> Loading required package: stats4
#> Loading required package: BiocGenerics
#> 
#> Attaching package: ‘BiocGenerics’
#> The following objects are masked from ‘package:stats’:
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from ‘package:base’:
#> 
#>     Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
#>     as.data.frame, basename, cbind, colnames, dirname, do.call,
#>     duplicated, eval, evalq, get, grep, grepl, intersect, is.unsorted,
#>     lapply, mapply, match, mget, order, paste, pmax, pmax.int, pmin,
#>     pmin.int, rank, rbind, rownames, sapply, setdiff, table, tapply,
#>     union, unique, unsplit, which.max, which.min
#> Loading required package: S4Vectors
#> 
#> Attaching package: ‘S4Vectors’
#> The following object is masked from ‘package:utils’:
#> 
#>     findMatches
#> The following objects are masked from ‘package:base’:
#> 
#>     I, expand.grid, unname
#> Loading required package: IRanges
#> Loading required package: GenomeInfoDb
#> Loading required package: Biobase
#> Welcome to Bioconductor
#> 
#>     Vignettes contain introductory material; view with
#>     'browseVignettes()'. To cite Bioconductor, see
#>     'citation("Biobase")', and for packages 'citation("pkgname")'.
#> 
#> Attaching package: ‘Biobase’
#> The following object is masked from ‘package:MatrixGenerics’:
#> 
#>     rowMedians
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     anyMissing, rowMedians
#> 
#> 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] "/home/runner/work/_temp/Library/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
library(bluster)

## Setting BLUSPARAM clusters the consolidation
## matrix into SHRs
cdx <- calculateConcordex(
  spe, "in_tissue",
  n_neighbors=10,
  BLUSPARAM=KmeansParam(3)
)

## SHRs are an attribute of the result
shr <- attr(cdx, "shr")

## The label similarity matrix can be computed
## with `compute_similarity=TRUE`
cdx <- calculateConcordex(
  spe, "in_tissue",
  n_neighbors=10,
  compute_similarity=TRUE,
  BLUSPARAM=KmeansParam(3)
)