| Title: | smoothclust |
|---|---|
| Description: | Tools for spatial domain analysis in spatial transcriptomics data. Includes spatial smoothing of gene expression profiles for downstream clustering and a boundary density (BD) metric for summarizing spatial domain organization. The smoothing function returns processed expression values for subsequent clustering. BD requires only spatial coordinates and categorical labels, and provides local and sample-level summaries of label transitions. |
| Authors: | Lukas M. Weber [aut, cre] (ORCID: <https://orcid.org/0000-0002-3282-1730>) |
| Maintainer: | Lukas M. Weber <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.11.0 |
| Built: | 2026-11-24 09:48:11 UTC |
| Source: | https://github.com/bioc/smoothclust |
Function for boundary density metric
boundary_density(spatial_coords, labels, k = 6, n_threads = 1, adjust = FALSE)boundary_density(spatial_coords, labels, k = 6, n_threads = 1, adjust = FALSE)
spatial_coords |
Numeric matrix containing spatial coordinates of
points, formatted as nrow = number of points, ncol = 2 (assuming x and y
dimensions). For example, 'spatial_coords = spatialCoords(spe)' if using a
|
labels |
Atomic vector or factor containing cluster labels for each
point. Labels must correspond to coordinate rows in the same order. Missing
values are not allowed. For example, 'labels <- colData(spe)$label' if
using a |
k |
Number of nearest neighbors to use in calculation, excluding the
focal location. This count is independent of the |
n_threads |
Number of threads to use for nearest-neighbor searches. Default = 1. |
adjust |
Whether to calculate the composition adjustment. With
|
Function to calculate the raw boundary density metric, defined as the average fraction of nearest neighbors per point that are from a different cluster. Raw BD summarizes observed label-transition frequency on the nearest-neighbor graph.
An optional analytic composition adjustment provides the sample-level relative BD. Relative BD is a composition-adjusted summary, calculated by dividing raw BD by its analytic expectation under random relabeling with the observed label counts. The expectation is calculated directly from label counts. Values below, equal to, or above 1 indicate fewer, the same, or more transitions than expected under this fixed-count random-label reference.
Returns a list containing values at each point (i.e. the number of
nearest neighbors that are from a different cluster, the number of nearest
neighbors, and the local raw boundary density) as well as the mean
discordant neighbor count and the sample-level raw boundary density. If
adjust = TRUE, the list also contains the expected boundary
density and sample-level relative boundary density.
spatial_coords <- cbind(c(0, 1, 3, 6), 0) labels <- c("A", "B", "A", "A") # calculate raw boundary density metric res <- boundary_density(spatial_coords, labels, k = 2) str(res) res$n_discordant res$mean_discordant res$boundary_density # calculate analytic composition adjustment res_adj <- boundary_density(spatial_coords, labels, k = 2, adjust = TRUE) res_adj$expected_boundary_density res_adj$relative_boundary_densityspatial_coords <- cbind(c(0, 1, 3, 6), 0) labels <- c("A", "B", "A", "A") # calculate raw boundary density metric res <- boundary_density(spatial_coords, labels, k = 2) str(res) res$n_discordant res$mean_discordant res$boundary_density # calculate analytic composition adjustment res_adj <- boundary_density(spatial_coords, labels, k = 2, adjust = TRUE) res_adj$expected_boundary_density res_adj$relative_boundary_density
Smooths expression profiles across spatial neighbors for downstream spatial domain analysis.
smoothclust( input, assay_name = "counts", spatial_coords = NULL, method = c("uniform", "kernel", "knn"), bandwidth = 0.05, k = 18, truncate = 0.05, n_threads = 1, normalize = TRUE, log = TRUE, size_factors = NULL, pseudo_count = 1, output_assay_name = NULL )smoothclust( input, assay_name = "counts", spatial_coords = NULL, method = c("uniform", "kernel", "knn"), bandwidth = 0.05, k = 18, truncate = 0.05, n_threads = 1, normalize = TRUE, log = TRUE, size_factors = NULL, pseudo_count = 1, output_assay_name = NULL )
input |
Input data, which can be provided as either a
|
assay_name |
For a |
spatial_coords |
Numeric matrix of spatial coordinates, assumed to contain x coordinates in first column and y coordinates in second column. Rows must correspond to expression columns in the same order. This argument is only used if the input is a numeric matrix. |
method |
Method used for smoothing. Options are |
bandwidth |
Bandwidth parameter for smoothing, expressed as proportion
of width or height (whichever is greater) of tissue area. Only used for
|
k |
Number of nearest other locations for |
truncate |
Truncation threshold parameter if |
n_threads |
Number of threads to use for nearest-neighbor searches. Default = 1. |
normalize |
Whether to perform library size normalization before spatial
smoothing. Expression values in each column are divided by a size factor
rescaled to have mean one. Default = |
log |
Whether to apply a log2 transformation after spatial smoothing.
The transformation is |
size_factors |
Optional positive numeric vector with one value per
spatial location, used for normalization when |
pseudo_count |
Positive numeric scalar added before log2 transformation
when |
output_assay_name |
Optional name for the output assay when |
The function returns processed expression values for downstream feature selection, dimensionality reduction, and clustering.
When normalize = TRUE, library size normalization is performed before
spatial smoothing. This ensures that neighboring locations contribute
according to the spatial weights rather than their sequencing depths. When
log = TRUE, log2 transformation is also performed after smoothing.
Size factors should be calculated from the desired reference set of genes
before filtering and supplied using size_factors.
Returns processed and spatially smoothed expression values for use in
downstream analyses. Results are returned either as a
SpatialExperiment object containing a new assay, or as a numeric
matrix-like object, depending on the input type.
set.seed(123) input <- matrix(rpois(60, lambda = 10), nrow = 10) spatial_coords <- cbind(rep(1:3, each = 2), rep(1:2, times = 3)) # normalize, smooth, and log-transform a numeric expression matrix out <- smoothclust(input, spatial_coords = spatial_coords, bandwidth = 0.6) dim(out) # supply size factors calculated before any gene filtering size_factors <- colSums(input) out_precomputed <- smoothclust(input, spatial_coords = spatial_coords, bandwidth = 0.6, size_factors = size_factors) dim(out_precomputed)set.seed(123) input <- matrix(rpois(60, lambda = 10), nrow = 10) spatial_coords <- cbind(rep(1:3, each = 2), rep(1:2, times = 3)) # normalize, smooth, and log-transform a numeric expression matrix out <- smoothclust(input, spatial_coords = spatial_coords, bandwidth = 0.6) dim(out) # supply size factors calculated before any gene filtering size_factors <- colSums(input) out_precomputed <- smoothclust(input, spatial_coords = spatial_coords, bandwidth = 0.6, size_factors = size_factors) dim(out_precomputed)