| Title: | Spillover-Aware Interaction-Changed Genes at Contact, Paracrine and Niche Scale |
|---|---|
| Description: | Tests whether a receiver cell type changes a gene's expression with its exposure to a sender cell type, at three spatial scales: touching cells (contact), short-range neighbours (paracrine) and the regional neighbourhood (niche). Transcripts leaked from touching neighbours are modelled from the data and removed per cell before testing. Niche effects are screened with a calibrated marginal test (toroidal-shift null) and attributed to senders by exact fine-mapping. Counts are read in gene blocks through a provider, so whole sections run in bounded memory, in memory or from a disk-backed store, sequentially or on 'mirai' daemons. |
| Authors: | Ruben Dries [aut, cre] |
| Maintainer: | Ruben Dries <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-11-24 23:10:43 UTC |
| Source: | https://github.com/drieslab/edgeicg |
For every outline vertex, vertices of other cells within tau microns count as touching; the vertex
contributes half the length of its two adjacent outline segments. The shared length of (i, j) is the
mean of both directions. Used as the spillover graph (which neighbours can leak transcripts).
contact_from_polygons(poly, n, tau = 2, max_k = 6)contact_from_polygons(poly, n, tau = 2, max_k = 6)
poly |
data.frame with |
n |
number of cells. |
tau |
touching distance (microns). |
max_k |
neighbouring vertices searched per vertex. |
data.frame of directed edges recv, send, w (shared length, microns).
EdgeICG reads raw counts only in blocks. A provider is a list with
genes (character) and n_cells;
rows(lo, hi, gi): raw counts of cells lo:hi (in the provider's current cell order) for genes gi, as a
cells x genes matrix (sparse dgCMatrix preferred; dense is accepted);
order(ord): a provider whose cell i is this provider's cell ord[i]. The engine visits cells in
spatially ordered windows (see window_cells in edgeicg()) and asks the provider for that order once;
cleanup(): removes anything order() created;
narrow(gi): a provider for genes gi only (used by the gene prefilter);
split(list_of_gi): one provider per gene panel (windowed runs with more than panel_genes genes);
optionally slice(lo, hi, gi): the same block as a compact genes x cells matrix, for providers whose data
live in memory (the block is sent to the worker with its task);
read(gi): all cells for genes gi (cells x genes), kept for direct use.
counts_provider(counts) counts_provider_store(store, genes, cells)counts_provider(counts) counts_provider_store(store, genes, cells)
counts |
genes x cells matrix ( |
store |
a GiottoDisk expression store (genes x cells), e.g.
|
genes |
genes to analyse (store feature IDs). |
cells |
cell IDs in analysis order (store cell IDs). |
counts_provider() wraps an in-memory genes x cells matrix (sparse or dense). counts_provider_store() wraps a
GiottoDisk expression store, so whole sections never sit in memory: workers read their own windows from it.
For windowed runs, order() bakes the analysis genes into a temporary store in the engine's cell order
(GiottoDisk::storeWrite() of the view). The
expression store is sorted by cell, so a window and its halo are then a contiguous row range that prunes
parquet row groups, and only the analysis genes are scanned. The temporary store is removed by cleanup().
a provider list.
Tests, for every receiver type, sender type and gene, whether the receiver's expression changes with its exposure to the sender at contact, paracrine and niche scale, after removing transcripts leaked from touching neighbours. See the package description for the model.
edgeicg( counts, coords, types, lib = NULL, spill_edges = NULL, ex = NULL, block_genes = 500L, window_cells = NULL, panel_genes = 3000L, covariates = NULL, covariate_scope = c("all", "niche"), workers = 0L, backend = c("auto", "rust", "r"), threads = NULL, calibrate = TRUE, calib_cond = TRUE, attribute = TRUE, B = 30L, min_shift = 450, q_thr = 0.05, min_cells = 60L, min_det = 0.01, min_exposed = 30L, exposed_at = 0.1, drop_degenerate = TRUE, prefilter = TRUE, seed = 1L, verbose = TRUE, region = NULL, local = FALSE, patch_size = 1000, min_cells_region = 50L, min_exposed_region = 20L, genes = NULL, leak_genes = c("all", "tested"), leak = NULL, ... )edgeicg( counts, coords, types, lib = NULL, spill_edges = NULL, ex = NULL, block_genes = 500L, window_cells = NULL, panel_genes = 3000L, covariates = NULL, covariate_scope = c("all", "niche"), workers = 0L, backend = c("auto", "rust", "r"), threads = NULL, calibrate = TRUE, calib_cond = TRUE, attribute = TRUE, B = 30L, min_shift = 450, q_thr = 0.05, min_cells = 60L, min_det = 0.01, min_exposed = 30L, exposed_at = 0.1, drop_degenerate = TRUE, prefilter = TRUE, seed = 1L, verbose = TRUE, region = NULL, local = FALSE, patch_size = 1000, min_cells_region = 50L, min_exposed_region = 20L, genes = NULL, leak_genes = c("all", "tested"), leak = NULL, ... )
counts |
raw counts, genes x cells: a |
coords |
n x 2 cell centroids (microns), in the provider's cell order. |
types |
cell-type labels (character or factor), one per cell. |
lib |
library size per cell over all genes (default: column sums of |
spill_edges |
optional touching-cell edge list ( |
ex |
optional exposures from |
block_genes |
genes per block (memory: about cells of a type x block_genes x 8 bytes per copy). |
window_cells |
cells per window. With more cells than this, cells are visited in spatially ordered windows
(each with the halo of touching neighbours the leak correction needs) and every statistic is accumulated as a
sum over windows, so memory no longer grows with the section. |
panel_genes |
with several windows, genes are processed in panels of at most this many (each a separately baked store when counts are on disk), which bounds the memory of the partial sums; every count is still read once per pass. Default 3000. |
covariates |
optional numeric matrix (cells x q) of covariates added to every receiver type's design
and adjusted for but not tested, e.g. a zonation score or |
covariate_scope |
|
workers |
number of mirai daemons (0 = sequential). One window: tasks are gene blocks; several windows: tasks are bands of windows. Results are identical. |
backend |
|
threads |
threads per process for the Rust kernels (default: 1 with workers, all cores without). |
calibrate |
run the toroidal-shift calibration of the niche scale (recommended). |
calib_cond |
also calibrate the conditional (all-sender) niche z (secondary output). |
attribute |
attribute niche screen hits to senders. |
B, min_shift
|
number of shifts and minimum shift (microns) of the calibration. |
q_thr |
FDR threshold for the niche screen hits that are attributed. |
min_cells, min_det, min_exposed, exposed_at
|
testability: receiver types with at least |
drop_degenerate |
drop exposure columns without variance within a receiver type (their tests are NA). FALSE keeps them in the design. |
prefilter |
drop genes detected in fewer than |
seed |
seed of the calibration shifts. |
verbose |
progress messages. |
region |
optional region per cell (character or factor; NA = not in a region). The effect of every entry
is then also estimated within each region, with a test of whether it differs between regions; see
|
local |
experimental. TRUE: the same within square patches of |
patch_size |
patch side for |
min_cells_region, min_exposed_region
|
a region or patch is used for a receiver type with at least
|
genes |
genes to test (default: all genes of |
leak_genes |
genes the leak coefficients (lambda, mu per receiver-sender pair) are fitted on. |
leak |
optional leak coefficients to reuse instead of fitting them: an |
... |
further engine settings: |
an EdgeICG object; see edgeicg_results().
Flags hits whose gene marks another cell type, so leaked or mis-segmented transcripts can be told apart from candidate biology. Classes (first that applies):
edgeicg_classify( x, hits, markers = NULL, types_considered = NULL, spec_thr = 0.8, regional_n = 3 )edgeicg_classify( x, hits, markers = NULL, types_considered = NULL, spec_thr = 0.8, regional_n = 3 )
x |
an |
hits |
a table from |
markers |
optional named list (type -> character vector of marker genes). |
types_considered |
types used as "other" types (default: all fitted types). |
spec_thr, regional_n
|
thresholds. |
sender_marker: the gene marks the tested sender (sender specificity > spec_thr, or in markers).
other_marker: the gene marks a third type (not the receiver).
own_down: a gene of the receiver goes down next to the sender, where the receiver expresses it far more
than the sender (receiver specificity against the sender > spec_thr, or in markers of the receiver).
Next to that sender, mixed or mis-segmented cells and dilution by the sender's transcripts lower such
genes, so these hits need checking before they are read as regulation.
clean: none of the above.
Niche hits where the same sender and sign recur in at least regional_n receiver types are flagged
regional (shared tissue zone or a regional technical artefact).
Specificity of gene g for type k against type r is m_kg / (m_kg + m_rg) with m the mean
composition per type from the fit (x$type_mean).
hits with columns class and regional.
Every exposure is a share: for each cell, the fraction of its neighbourhood that belongs to each cell type (rows sum to 1), so local cell density does not enter.
edgeicg_exposures( coords, types, K = max(types), k = 8, sigma_p = 25, sigma_n = 150, bin = 10, max_k = 400 )edgeicg_exposures( coords, types, K = max(types), k = 8, sigma_p = 25, sigma_n = 150, bin = 10, max_k = 400 )
coords |
n x 2 matrix of cell centroids in microns. |
types |
integer cell-type codes 1..K. |
K |
number of types. |
k |
contact neighbours. |
sigma_p, sigma_n
|
paracrine and niche kernel widths (microns). |
bin |
niche grid bin (microns). |
max_k |
cap on paracrine neighbours per cell. |
contact (C): share of the k nearest neighbours' weight, w = exp(-d / median(d)).
paracrine (P): Gaussian (sigma_p) share among cells within 3 * sigma_p that are not contact
neighbours.
niche (N): Gaussian (sigma_n) share from a per-type cell-count grid (bin bin) blurred with a
separable filter, read back at each cell's bin. The grids are kept for the niche calibration.
list(contact = edge list (recv, send, w), C, P, N (n x K), fields (niche grids), settings).
Experimental: the Q test over patches is calibrated but has little power for smooth or half-plane patterns.
For a fit with local = TRUE: for every testable entry, whether its effect varies across patches of the
tissue (Cochran's Q over patches, BH within scale) and, for entries with q_het < q_thr, the effect per patch
with empirical-Bayes shrinkage towards the mean (effect_shrunk, sd_shrunk), to map where it is strongest.
edgeicg_local_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05 )edgeicg_local_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05 )
x |
an |
scale |
scales to report. |
q_thr |
threshold on |
list(entries = data.table(receiver, sender, gene, scale, global_effect, global_z, Q, df, tau2, q_het,
local_frac), patches = data.table(receiver, sender, gene, scale, patch, x, y, n_exposed, effect, se,
effect_shrunk, sd_shrunk)). local_frac: share of the exposed receivers that sit in patches whose shrunken
effect is more than 2 posterior sd from zero, with the sign of the strongest patch.
Assigns each cell to a square patch of a grid laid over the tissue. Used by edgeicg(local = TRUE).
edgeicg_patches(coords, size = 1000)edgeicg_patches(coords, size = 1000)
coords |
n x 2 cell centroids (microns). |
size |
patch side (microns). |
list(code = patch index per cell, labels, centers = data.frame(patch, x, y)).
For a fit with region =, the effect of every testable entry within each region, and a test of whether the
effect differs between regions.
edgeicg_region_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05, min_lfc = 0.25, all = FALSE )edgeicg_region_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05, min_lfc = 0.25, all = FALSE )
x |
an |
scale |
scales to report. |
q_thr |
threshold on the region q and on |
min_lfc |
effect-size floor for a region to count as significant. |
all |
FALSE (default): only entries that are heterogeneous or significant in at least one region. |
Within a region the effect is the slope of the receiver's expression (leak-corrected, adjusted for the other
senders and scales of the global model) on its exposure to the sender, with a region intercept. Between
regions, Cochran's Q (df = regions - 1) tests heterogeneity; q_het is its BH-adjusted p within scale.
pattern, for entries with q_het < q_thr: opposite (significant in a region, q < q_thr and
|lfc| >= min_lfc, and another region goes the other way with |z| >= 2), specific (otherwise significant in
one region only), variable (heterogeneous otherwise); shared when q_het >= q_thr.
data.table with one row per entry and region: receiver, sender, gene, scale, region, n_cells,
n_exposed, effect, lfc, se, z, q, global_z, global_q, Q, df, q_het, pattern, class. lfc uses the pair's
global exposure contrast (10th to 90th percentile), so it is comparable between regions. class is the
entry's hit class (edgeicg_classify()): a region-dependent sender_marker hit often reflects a sender
whose own expression differs between regions, not the receiver.
Results table
edgeicg_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05, min_lfc = 0.25 )edgeicg_results( x, scale = c("contact", "paracrine", "niche"), q_thr = 0.05, min_lfc = 0.25 )
x |
an |
scale |
one or more of |
q_thr |
keep entries with q below this (1 = all testable entries). |
min_lfc |
keep entries whose effect size, |
data.table: receiver, sender, gene, scale, effect, lfc, se, z, q, plus for niche the top attributed
sender (top_sender, alpha, cs_size).
Three cell types in smooth domains (cells about 10 microns apart). Ground truth the model never sees:
directed contact effects (multiplicative, receiver 1 <- sender 2 up on S1, receiver 2 <- sender 3 down
on S2), optional niche effects (receiver 1 genes S3 respond to the regional density of type 2), and
mass-conserving spillover between neighbours over a different graph than the model uses.
simulate_edgeicg( n = 4000, G = 200, seed = 1, kappa = 0.1, gamma = 1, niche_gamma = 0, spacing = 10, region_gamma = 0 )simulate_edgeicg( n = 4000, G = 200, seed = 1, kappa = 0.1, gamma = 1, niche_gamma = 0, spacing = 10, region_gamma = 0 )
n |
cells. |
G |
genes. |
seed |
seed. |
kappa |
mean leaked fraction. |
gamma |
contact log effect at full exposure. |
niche_gamma |
niche log effect. |
spacing |
cell spacing (microns). |
region_gamma |
contact log effect of region-dependent genes (0 = none). Regions are the left and right
halves of the tissue ( |
list(counts (genes x cells), coords, types, truth (data.table), lib, region (per cell); truth gains
a region column ("all", "right" or "opposite")).
A tensor-product natural-spline basis of the cell positions, to pass as covariates to edgeicg(): it absorbs
smooth spatial trends (tissue zonation, gradients of cell states within a type) so that they are not mistaken
for exposure effects. df per axis sets the scale of the trends removed: with a section of width W, trends
longer than about W / df are absorbed.
spatial_basis(coords, df = 4L)spatial_basis(coords, df = 4L)
coords |
n x 2 cell centroids. |
df |
degrees of freedom per axis (default 4: 4 + 4 + 16 = 24 columns). |
a numeric matrix (cells x (2 df + df^2)), columns centred.