Aggregates single-cell spatial data into hexagonal bins and returns a
bin-level count matrix together with a matching sf polygon object,
ready to pass directly to blisa.default.
Usage
hexBinCells(
coords_df,
counts_matrix,
bin_size = 50,
min_cells = 1,
min_total_counts = 10,
group = NULL,
verbose = FALSE
)Arguments
- coords_df
Data frame or matrix with columns
x_centroidandy_centroid(e.g. the output ofSpatialExperiment::spatialCoords()). Row names must be cell IDs matching the column names ofcounts_matrix.- counts_matrix
Gene-by-cell count matrix (dense or sparse). Row names must be gene symbols; column names must be cell IDs present in
coords_df.- bin_size
Numeric. Approximate width of each hexagonal bin in coordinate units (e.g. microns). Analogous to
grid.length.xinsciderHex::gridDensity. Default50.- min_cells
Integer. Bins containing fewer than
min_cellscells are dropped from the output. Default1.- min_total_counts
Numeric. Bins whose total counts (summed over all genes) fall below this threshold are dropped from the output, alongside the
min_cellsfilter. Set to0to disable. Default10.- group
Factor or character vector of length
ncol(counts_matrix)giving the cell-type label of each cell. When supplied, a named list of per-cell-type gene-by-bin matrices is included in the output ascounts_by_group. DefaultNULL(not computed).- verbose
Logical. If
TRUE, print progress messages. DefaultFALSE(silent).
Value
A list with:
- counts_matrix
Gene-by-bin sparse count matrix (all cells combined). Column i corresponds to row i of
bins.- bins
An
sfobject of hexagonal bin polygons with ann_cellscolumn recording how many cells fall in each bin and atotal_countscolumn recording the summed counts per bin. Row order matches the columns ofcounts_matrix.- counts_by_group
(Only when
groupis supplied.) A named list of gene-by-bin sparse matrices, one per cell-type level, with the same bin order ascounts_matrix.
Examples
if (FALSE) { # \dontrun{
set.seed(42)
n <- 500
coords <- data.frame(
x_centroid = runif(n, 0, 1000),
y_centroid = runif(n, 0, 1000),
row.names = paste0("cell_", seq_len(n))
)
counts <- Matrix::Matrix(
matrix(rpois(20L * n, lambda = 5), nrow = 20L, ncol = n,
dimnames = list(paste0("gene_", 1:20), paste0("cell_", seq_len(n)))),
sparse = TRUE
)
group <- sample(c("TypeA", "TypeB"), n, replace = TRUE)
binned <- hexBinCells(coords, counts, bin_size = 100, group = group)
str(binned, max.level = 1)
} # }