Spillover-aware interaction-changed genes with edgeicg

edgeicg() asks, for every receiver cell type, sender cell type and gene, whether the receiver’s expression changes with its exposure to the sender at three scales:

  • contact: touching cells;
  • paracrine: short-range neighbours, Gaussian kernel with σ = 25 µm;
  • niche: the regional neighbourhood, σ = 150 µm.

In imaging-based data, a touching cell’s transcripts leak into its neighbour. That leak looks like a contact effect: the receiver appears to express the sender’s markers. edgeicg models the leak from the data, removes it per cell, and only then tests.

A simulated tissue

library(edgeicg)
sim <- simulate_edgeicg(n = 2500, G = 100, seed = 7, gamma = 1.2)
dim(sim$counts)
#> [1]  100 2500
table(sim$types)
#> 
#>   t1   t2   t3 
#> 1106  863  531

sim$truth lists the planted effects.

Fit

On real data, pass:

  • the library size over all genes as lib;
  • the touching-cell graph from the cell outlines as spill_edges (see contact_from_polygons()).

Without spill_edges, the kNN contact graph is used.

fit <- edgeicg(sim$counts, sim$coords, sim$types, min_shift = 150, B = 8, verbose = FALSE)
fit
#> EdgeICG: 2500 cells, 3 types, 100 genes, 6 pairs (pass1 0 s, pass2 0 s, pass3 0 s)
#>   leak fitted on: all (100 genes)
#>   hits: q < 0.05 and |log2 FC| >= 0.25 (q < 0.05 alone)
#>   contact   18 (20)
#>   paracrine 1 (1)
#>   niche     0 (0), screen

min_shift and B set the spatial calibration of the niche scale: B toroidal shifts of at least min_shift µm. The defaults (450 µm, 30 shifts) are meant for whole sections. This simulated tissue is small, so the example uses smaller values.

Results

edgeicg_results() returns one row per test with q below a threshold. edgeicg_classify() then sorts the hits into interpretable classes, for example whether the gene is a marker of the sender, a sign of leftover leakage.

res <- edgeicg_results(fit, q_thr = 0.05)
head(res)
#>    receiver sender   gene   scale   effect       lfc        se        z
#>      <char> <char> <char>  <char>    <num>     <num>     <num>    <num>
#> 1:       t1     t2   g047 contact 2.183266 0.4248408 0.2276113 9.592081
#> 2:       t1     t2   g088 contact 2.232350 0.5543982 0.2421968 9.217089
#> 3:       t1     t2   g095 contact 2.894435 0.4480876 0.3152320 9.181920
#> 4:       t1     t2   g035 contact 2.419436 0.4053591 0.2653434 9.118129
#> 5:       t1     t2   g058 contact 1.845994 0.6085121 0.2406478 7.670936
#> 6:       t1     t2   g021 contact 1.885799 0.4729807 0.2489097 7.576240
#>               q
#>           <num>
#> 1: 5.179571e-19
#> 2: 8.469457e-18
#> 3: 8.469457e-18
#> 4: 1.146484e-17
#> 5: 2.048956e-12
#> 6: 3.557133e-12
table(res$scale)
#> 
#>   contact paracrine 
#>        18         1
hits <- edgeicg_classify(fit, res)
table(hits$class)
#> 
#> clean 
#>    19

The niche scale runs as a calibrated screen followed by attribution. For each screen hit, fit$attribution gives every candidate sender’s posterior probability (pip), its share alpha among single-sender models, and whether it belongs to the credible set.

nrow(fit$attribution)            # this small simulation has no niche-screen hits
#> [1] 0
head(fit$attribution[order(-pip)])
#> Empty data.table (0 rows and 11 cols): receiver,sender,gene,z_marg,q_marg,z_cond...

Large data

Every statistic in the model is a sum over cells, so the engine computes partial sums and adds them up. Several arguments control how:

  • Gene blocks (block_genes) bound memory for in-memory counts.
  • Workers (workers): mirai daemons. Results are identical with and without them.
  • Cell windows (window_cells): the cells are visited in spatially ordered windows, so memory no longer grows with the section.
  • Counts on disk: pass a GiottoDisk expression store as counts, or wrap it with counts_provider_store(). Each worker then reads its own windows from a copy baked once in window order.
  • Rust kernels: with the edgeicgrs package installed, backend = "auto" computes the matrix products in Rust.

The windowed run below gives the same results as the single-window fit above:

fit_w <- edgeicg(sim$counts, sim$coords, sim$types, min_shift = 150, B = 8, window_cells = 700, verbose = FALSE)
fit_w$n_windows
#> [1] 4
all.equal(fit_w$scale_fits$contact$z, fit$scale_fits$contact$z)
#> [1] TRUE

From Giotto, the same analysis is findInteractionChangedFeats(g, cluster_column = ..., method = "edgeicg").