1 Introduction

smoothclust is a method for segmentation of spatial domains and spatially-aware clustering in spatial transcriptomics data. The method generates spatial domains with smooth boundaries by smoothing gene expression profiles across neighboring spatial locations, followed by unsupervised clustering. Spatial domains consisting of consistent mixtures of cell types may then be further investigated by applying cell type compositional analyses or differential analyses.

2 Installation

The smoothclust package can be installed from Bioconductor as follows (using R version 4.4 and Bioconductor version 3.19, available from approximately 2024-05-01 onwards). Additional details are shown on the Bioconductor package landing page.

install.packages("BiocManager")
BiocManager::install("smoothclust")

The latest development version of the package can be installed from the devel version of Bioconductor or from GitHub.

3 Input data format

Input data can be provided either as a SpatialExperiment object within the Bioconductor framework, or as numeric matrices of expression values and spatial coordinates. See help file (?smoothclust) for details.

In the example workflow below, we assume the input is in SpatialExperiment format.

4 Tutorial

The example workflow in this section demonstrates how to run smoothclust to generate spatial domains with smooth boundaries for a dataset from the 10x Genomics Visium platform.

library(smoothclust)
library(STexampleData)
library(scuttle)
library(scran)
library(scater)
library(ggspavis)

Load dataset from STexampleData package:

# load data
spe <- Visium_humanDLPFC()
## see ?STexampleData and browseVignettes('STexampleData') for documentation
## loading from cache
# keep spots over tissue
spe <- spe[, colData(spe)$in_tissue == 1]

dim(spe)
## [1] 33538  3639
assayNames(spe)
## [1] "counts"

Run smoothclust using default parameter settings, which have been selected to be appropriate for Visium data from human tissue.

The method for smoothing can be specified by providing the method argument, with available options uniform, kernel, and knn. Additional arguments can be used to set parameter values, including bandwidth, k, and truncate, depending on the choice of method. For more details, see the function documentation (?smoothclust) or the paper (in preparation).

# run smoothclust
spe <- smoothclust(spe)

The smoothed expression counts are stored in a new assay named counts_smooth:

# check output object
assayNames(spe)
## [1] "counts"        "counts_smooth"

Calculate log-transformed normalized counts (logcounts) on the smoothed expression counts. Here, the argument assay.type = "counts_smooth" specifies that we want to calculate logcounts using the smoothed counts from smoothclust.

# calculate logcounts
spe <- logNormCounts(spe, assay.type = "counts_smooth")

assayNames(spe)
## [1] "counts"        "counts_smooth" "logcounts"

Run clustering. We use a standard clustering workflow from single-cell data, consisting of k-means clustering on the top principal components (PCs) calculated on the set of top highly variable genes (HVGs) with logcounts as the input.

We use a relatively small number of clusters for demonstration purposes in this example:

# preprocessing steps for clustering

# remove mitochondrial genes
is_mito <- grepl("(^mt-)", rowData(spe)$gene_name, ignore.case = TRUE)
table(is_mito)
## is_mito
## FALSE  TRUE 
## 33525    13
spe <- spe[!is_mito, ]
dim(spe)
## [1] 33525  3639
# select top highly variable genes (HVGs)
dec <- modelGeneVar(spe)
top_hvgs <- getTopHVGs(dec, prop = 0.1)
length(top_hvgs)
## [1] 986
spe <- spe[top_hvgs, ]
dim(spe)
## [1]  986 3639
# dimensionality reduction

# compute PCA on top HVGs
set.seed(123)
spe <- runPCA(spe)
# run k-means clustering
set.seed(123)
k <- 5
clust <- kmeans(reducedDim(spe, "PCA"), centers = k)$cluster
table(clust)
## clust
##    1    2    3    4    5 
##  967 1574  492  272  334
colLabels(spe) <- factor(clust)

Plot clusters / spatial domains generated by the smoothclust workflow:

# color palettes
pal8 <- "libd_layer_colors"
pal36 <- unname(palette.colors(36, "Polychrome 36"))

# plot clusters / spatial domains
plotSpots(spe, annotate = "label", pal = pal8)