Skip to content

Map expression and compare cluster fits

Expression clusters group spots with similar transcript profiles. Their labels do not identify cells, anatomical layers, or independent samples. This example fits the expression graph without tissue coordinates and then returns the labels to the image, so physical adjacency remains a separate observation. One mouse supplies the section; its cluster labels do not change that experimental unit.

The two fits share their input counts, retained spots, variable gene identifiers, normalization, component count, and requested neighbor count. Seurat selects the variable genes with vst before either spatial statistic is calculated. Python reads that exported feature list instead of selecting a different set. Normalization precedes feature exclusion, so both fits use the complete spot totals as their denominators.

Setting Seurat Scanpy
Eligible features Genes detected in at least three spots The same eligibility rule
Selected features 2,000 genes from Seurat vst The exported Seurat identifiers
Transformation log1p counts per 10,000 The same transformation
Scaling Centered and scaled without clipping Centered and scaled without clipping
PCA 30 components with IRLBA 30 components with ARPACK
Expression graph Annoy neighbors and a pruned shared-neighbor graph Euclidean neighbors and UMAP connectivity weights
Requested neighbors 15 15
Community fit Leiden modularity through R igraph Leiden modularity through Python igraph
Resolution and seed 0.5 and 20261031 0.5 and 20261031

The Seurat clustering documentation and Scanpy Leiden documentation describe their graph and community choices. The same numeric settings do not make their neighbor graphs identical. The comparison therefore reports an adjusted Rand index and a complete overlap table rather than requiring matching cluster numbers.

source("/shared/spatial_utils.R")
suppressPackageStartupMessages(library(Seurat))
data <- ReadSection()
counts <- data$counts
eligible <- rowSums(counts > 0) >= 3
object <- CreateSeuratObject(counts, min.cells = 0, min.features = 0)
object <- NormalizeData(object, scale.factor = 10000, verbose = FALSE)
object <- subset(object, features = rownames(counts)[eligible])
object <- FindVariableFeatures(object, selection.method = "vst",
nfeatures = 2000, verbose = FALSE)
features <- VariableFeatures(object)
stopifnot(length(features) == 2000, !anyDuplicated(features))
object <- ScaleData(object, features = features, scale.max = Inf,
verbose = FALSE)
object <- RunPCA(object, features = features, npcs = 30,
seed.use = 20261031, verbose = FALSE)
object <- FindNeighbors(object, dims = 1:30, k.param = 15,
nn.method = "annoy", n.trees = 50,
prune.SNN = 1 / 15, verbose = FALSE)
object <- FindClusters(object, algorithm = 4, leiden_method = "igraph",
leiden_objective_function = "modularity",
resolution = 0.5, n.iter = 10,
random.seed = 20261031, verbose = FALSE)
clusters <- as.character(Idents(object))
stopifnot(identical(names(Idents(object)), data$barcodes))
dir.create("outputs", showWarnings = FALSE)
write.table(data.frame(feature_id = features),
"outputs/variable_features.tsv", sep = "\t", quote = FALSE,
row.names = FALSE)
write.table(data.frame(barcode = data$barcodes, cluster = clusters),
"outputs/clusters_r.tsv", sep = "\t", quote = FALSE,
row.names = FALSE)
normalized <- NormalizeCounts(counts)
panel <- PanelIndices(data$features)
png("outputs/expression_r.png", width = 1800, height = 650, res = 150)
par(mfrow = c(1, 3), mar = c(4, 4, 3, 1))
DrawSpatial(data, as.numeric(normalized[panel[1], ]), "Slc17a7 expression")
DrawSpatial(data, as.numeric(normalized[panel[4], ]), "Mbp expression")
DrawSpatial(data, as.numeric(clusters), "Seurat expression clusters",
categorical = TRUE)
invisible(dev.off())
WriteMetrics(list(spots = ncol(counts), features = length(features),
components = 30, clusters = length(unique(clusters))))
cat(sprintf("Seurat fits %d expression clusters across %d spots.\n",
length(unique(clusters)), ncol(counts)))
cat(sprintf("Both fits use the same %d selected feature identifiers.\n",
length(features)))
Seurat fits 15 expression clusters across 2695 spots.
Both fits use the same 2000 selected feature identifiers.

The R figure maps two fixed genes and the independently fitted Seurat expression clusters.

Gene colors show log1p counts per 10,000 UMIs. Cluster colors represent expression groups within this section and carry no cell type assignment.

The tissue images are supplied by 10x Genomics under CC BY 4.0. These figures add expression and cluster overlays. No archived provider cluster assignments enter either fresh fit.

Spatially coherent clusters can reflect anatomy, cell composition, technical variation, or a combination of these factors. The maps do not distinguish those explanations by themselves. The graph lesson defines physical neighbors independently of this expression fit.

Hao and colleagues, 2023 describe the Seurat methods for integrative, multimodal, and scalable single-cell analysis.

Wolf, Angerer, and Theis, 2018 describe Scanpy, the Python toolkit for large-scale single-cell expression analysis.

The Scanpy 1.11 Leiden reference documents resolution, partition options, and the random state. The man/FindClusters.Rd reference in the Seurat 5.6.0 source package describes its Leiden option and clustering objective. Compare those definitions with the method table before interpreting agreement between the two fits.