| Title: | Spatial Domains with a Hidden Markov Random Field |
|---|---|
| Description: | Finds spatial domains by fitting the hidden Markov random field (HMRF) model of Zhu et al. (2018) <doi:10.1038/nbt.4260>: each cell's z-scored expression of spatial genes follows a multivariate Gaussian per domain, and a Potts prior favours domains shared with spatial neighbours. The model is fitted by expectation-maximization with iterated conditional modes over the colour classes of the spatial network, from a k-means start, as in the 'smfishHmrf' package. Colour classes are independent sets, so each update is a sparse matrix product; results equal 'smfishHmrf' on the same inputs. |
| Authors: | Qian Zhu [aut] (original HMRF method and smfishHmrf implementation), Guo-Cheng Yuan [aut], Ruben Dries [aut, cre] (ORCID: <https://orcid.org/0000-0001-7650-7754>) |
| Maintainer: | Ruben Dries <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-11-29 03:09:31 UTC |
| Source: | https://github.com/drieslab/spatialHMRF |
Fits the hidden Markov random field (HMRF) model of Zhu et al. (2018), as
in the smfishHmrf package: each cell's values follow a multivariate
Gaussian per domain, and a Potts prior of strength beta favours domains
shared with spatial neighbours. Fitted by expectation-maximization with
iterated conditional modes (ICM) over the colour classes of the network,
from a k-means start. Each colour class is an independent set, so its ICM
update and the neighbour counts are sparse matrix products; the results
equal smfishHmrf's on the same inputs.
If you use it, please cite the original paper (see References).
hmrf( y, network, k, beta = 2, zscore = c("cell", "feature", "none"), init = NULL, blocks = NULL, nstart = 200L, seed = 100L, subsample = 20000L, maxit = 50L, err = 1e-07, label_tol = 1e-04, n_icm = 10L, damp_tolerance = 1e-05, damp_factor = 1.05 )hmrf( y, network, k, beta = 2, zscore = c("cell", "feature", "none"), init = NULL, blocks = NULL, nstart = 200L, seed = 100L, subsample = 20000L, maxit = 50L, err = 1e-07, label_tol = 1e-04, n_icm = 10L, damp_tolerance = 1e-05, damp_factor = 1.05 )
y |
cells x features numeric matrix, cell IDs as row names (e.g. the scaled expression of spatial genes) |
network |
spatial network: a |
k |
integer, number of domains |
beta |
numeric, strength of the spatial prior (0: no prior) |
zscore |
|
init |
optional start from |
blocks |
optional colour classes from |
nstart, seed, subsample
|
k-means starts, seed and subsample size (used
when |
maxit |
integer, maximum EM iterations |
err |
numeric, relative change of means and covariances at which EM stops |
label_tol |
numeric, share of cells changing domain at which EM stops
(labels can flip between two states without the parameters converging);
0 stops on |
n_icm |
integer, ICM sweeps per EM iteration |
damp_tolerance, damp_factor
|
see |
A list: class (integer domain per cell), prob (cells x k
posterior probabilities), mu (features x k), sigma (features x
features x k), likelihood, iterations, init and blocks.
Zhu Q, Shah S, Dries R, Cai L, Yuan GC. Identification of spatially associated subpopulations by combining scRNAseq and sequential fluorescence in situ hybridization data. Nature Biotechnology 36, 1183–1190 (2018). doi:10.1038/nbt.4260
set.seed(1) xy <- matrix(runif(400), ncol = 2) * 10 dom <- 1L + (xy[, 1] > 5) y <- matrix(rnorm(200 * 4), 200) + c(-1.5, 1.5)[dom] rownames(y) <- paste0("c", 1:200) nn <- function(xy, k) { d <- as.matrix(stats::dist(xy)); diag(d) <- Inf t(apply(d, 1, order))[, seq_len(k)] } knn <- nn(xy, 5) e <- unique(t(apply(cbind(rep(1:200, 5), as.vector(knn)), 1, sort))) net <- data.frame(from = rownames(y)[e[, 1]], to = rownames(y)[e[, 2]]) fit <- hmrf(y, net, k = 2, beta = 2, zscore = "none", nstart = 10) table(fit$class, dom)set.seed(1) xy <- matrix(runif(400), ncol = 2) * 10 dom <- 1L + (xy[, 1] > 5) y <- matrix(rnorm(200 * 4), 200) + c(-1.5, 1.5)[dom] rownames(y) <- paste0("c", 1:200) nn <- function(xy, k) { d <- as.matrix(stats::dist(xy)); diag(d) <- Inf t(apply(d, 1, order))[, seq_len(k)] } knn <- nn(xy, 5) e <- unique(t(apply(cbind(rep(1:200, 5), as.vector(knn)), 1, sort))) net <- data.frame(from = rownames(y)[e[, 1]], to = rownames(y)[e[, 2]]) fit <- hmrf(y, net, k = 2, beta = 2, zscore = "none", nstart = 10) table(fit$class, dom)
hmrf_adjacency() turns an edge list of cell IDs into a
symmetric sparse matrix of neighbour counts (self edges dropped);
hmrf_blocks() colours it with igraph's DSatur heuristic and returns the
colour classes, which are independent sets.
hmrf_adjacency(network, ids) hmrf_blocks(adj)hmrf_adjacency(network, ids) hmrf_blocks(adj)
network |
|
ids |
cell IDs, the row order of |
adj |
a matrix from |
hmrf_adjacency(): a sparse n x n matrix. hmrf_blocks(): a
list of integer vectors (row positions) partitioning the cells.
k-means start, per-domain covariances and damping, as in
smfishHmrf: a domain whose covariance determinant is below
damp_tolerance gets the smallest diagonal term (searched in steps of
damp_factor from 1e-4) that brings it above. With more than
subsample cells, the nstart restarts run on a random subsample of
cells, k-means on all cells refines the 10 best, and the best refined
solution is kept, so the cost of the restarts does not grow with the
number of cells (smfishHmrf runs them on all cells).
hmrf_init( y, k, nstart = 200L, seed = 100L, subsample = 20000L, damp_tolerance = 1e-05, damp_factor = 1.05 )hmrf_init( y, k, nstart = 200L, seed = 100L, subsample = 20000L, damp_tolerance = 1e-05, damp_factor = 1.05 )
y |
cells x features matrix (already z-scored if wanted) |
k |
integer, number of domains |
nstart |
integer, k-means starts (smfishHmrf: 1000) |
seed |
integer, seed (0 or negative leaves the RNG alone) |
subsample |
integer, number of cells for the restarts; |
damp_tolerance, damp_factor
|
damping search settings |
A list with mu (features x k), sigma (features x features x
k) and damp (length k).