Package 'spatialLagGLM'

Title: Spatial Lag Regression for Spatial Transcriptomics
Description: Ranks genes in spatial transcriptomics data by the association between a spot's expression and the expression of its neighbours. For every gene, a quasi-Poisson generalized linear model is fitted with a library-size offset, user-defined design terms from the colData, and the spatial lag of the gene's own normalised counts as an additional covariate. The lag is a row-normalised, inverse-distance weighted mean over spots within a given radius, computed separately for each sample. The coefficient and test statistic of the lag, and the rank of the statistic across genes, are stored in the rowData of the input SpatialExperiment. No p-values are reported, because the null distribution of the statistic is not calibrated; the statistic is intended for ranking genes. Weights are computed in C++ with RcppArmadillo, and genes are fitted in parallel with BiocParallel. Plotting functions and a Shiny explorer are included.
Authors: Sahal Paladan [aut, cre] (ORCID: <https://orcid.org/0009-0005-0120-128X>)
Maintainer: Sahal Paladan <[email protected]>
License: MIT + file LICENSE
Version: 0.99.6
Built: 2026-12-02 04:49:07 UTC
Source: https://github.com/bioc/spatialLagGLM

Help Index


Generate Mock Spatial Transcriptomics Data

Description

Simulates a 30 x 30 grid of spots and 50 negative binomial genes. Spots within 7 units of the grid centre are labelled "TumorCore", the rest "Margin", and Gene1 is up-regulated in the core.

Usage

makeMockSpatial()

Value

A SpatialExperiment object with synthetic counts and coordinates.

Examples

se <- makeMockSpatial()

Summary Plot of Spatial Lag Results

Description

Plots the effect size against the absolute statistic for every gene and labels the top genes with the largest absolute statistic. The statistics are not calibrated (see spatialLagGLM), so no significance categories or thresholds are drawn: use the plot to rank genes.

Usage

plotLagSummary(se, top = 10L)

Arguments

se

A SpatialExperiment analysed with spatialLagGLM.

top

A single non-negative whole number: how many top-ranked genes to label.

Value

A ggplot2 object.

Examples

se <- makeMockSpatial()
analyzed <- spatialLagGLM(se, design = ~condition, radius = 2.5)
plotLagSummary(analyzed)

Plot a Gene on the Tissue Coordinates

Description

Plots either the raw counts of a gene or its spatial lag (the weighted mean of its neighbours' normalised counts, i.e. the covariate used by spatialLagGLM). With several samples, each is drawn in its own panel.

Usage

plotSpatialLag(
  se,
  gene_id,
  metric = c("expression", "spatial_lag"),
  assay.type = NULL
)

Arguments

se

A SpatialExperiment analysed with spatialLagGLM (needed only for "spatial_lag").

gene_id

A single gene name.

metric

"expression" for the raw counts, or "spatial_lag" for the spatial lag.

assay.type

A string naming the assay with raw counts. The default, NULL, uses the assay stored by spatialLagGLM, or "counts" if the object has not been analysed.

Value

A ggplot2 object.

Examples

se <- makeMockSpatial()
analyzed <- spatialLagGLM(se, design = ~condition, radius = 2.5)
plotSpatialLag(analyzed, gene_id = "Gene1", metric = "expression")
plotSpatialLag(analyzed, gene_id = "Gene1", metric = "spatial_lag")

Launch Interactive Spatial Lag Explorer

Description

Launch Interactive Spatial Lag Explorer

Usage

runSpatialLagExplorer(se)

Arguments

se

A SpatialExperiment analysed with spatialLagGLM.

Value

Called for its side effect of starting a Shiny app; returns nothing useful.

Examples

if (interactive()) {
    se <- makeMockSpatial()
    analyzed <- spatialLagGLM(se, design = ~condition, radius = 2.5)
    runSpatialLagExplorer(analyzed)
}

Quasi-Poisson Regression with a Spatially Lagged Covariate

Description

For every gene, fits a quasi-Poisson regression with a library-size offset, the terms of design, and the spatial lag of the gene's own size-factor-normalised counts as an extra covariate. The coefficient of the spatial lag, its test statistic and the rank of that statistic across genes are stored in rowData(se). No p-values are reported.

Usage

spatialLagGLM(
  se,
  design,
  radius = 2.5,
  assay.type = "counts",
  size.factors = NULL,
  BPPARAM = BiocParallel::bpparam()
)

Arguments

se

A SpatialExperiment object.

design

A formula using variables in colData(se), e.g. ~ condition.

radius

A positive number: the distance within which spots are neighbours, in the units of spatialCoords(se). For several samples, all samples should share the same units.

assay.type

A string naming the assay with raw counts.

size.factors

NULL (default) to compute size factors from the library sizes of assay.type, or a positive numeric vector with one value per spot. Supply this when se has been subsetted to a few genes, so that normalisation still reflects all genes.

BPPARAM

A BiocParallelParam object.

Details

For spot ii and a given gene, the model is

log⁡E[yi]=log⁡(si)+Xiβ+γ li,\log E[y_i] = \log(s_i) + X_i \beta + \gamma \, l_i,

where sis_i is the size factor (library size divided by its geometric mean), XiX_i the row of the design matrix, and li=∑jwijyj/sjl_i = \sum_j w_{ij} y_j / s_j the spatial lag. The weights wijw_{ij} are inverse distances between spots within radius, row-normalised, and are computed separately for each sample_id.

The question asked is whether γ=0\gamma = 0: given the offset and the design terms, is a spot's expression associated with the weighted mean of its neighbours' normalised expression?

Why no p-values. Because the lag is built from the same gene's counts, each pair of neighbouring spots enters the regression twice, once as the response and once inside a covariate. The usual quasi-Poisson standard error ignores this. Under no spatial structure, the variance of the statistic is larger than the model assumes by a factor of about 1+∑i≠jwijwji/∑ijwij21 + \sum_{i \neq j} w_{ij} w_{ji} / \sum_{ij} w_{ij}^2, which is close to 2 for symmetric weights. Nominal p-values are therefore far too small (about 18% of null genes fall below 0.05 in simulation). This factor depends on the weights, not on the gene, so it does not change how genes rank against each other. The statistic is therefore meant for ranking genes, not for testing against a significance threshold. For calibrated inference, compare the statistic with a permutation null (see the vignette).

This is not a spatial autoregressive model.

Genes whose fit fails or does not converge get NA results, and a warning reports how many.

The results are stored in rowData(se) as spatial_effect_size (γ^\hat\gamma, in units of normalised counts, so it depends on the expression level of the gene), spatial_statistic (γ^\hat\gamma divided by its quasi-Poisson standard error) and spatial_rank (rank of the absolute statistic, 1 = strongest). The radius, assay and size factors used are stored in metadata(se)$spatialLagGLM.

Value

se with spatial_effect_size, spatial_statistic and spatial_rank added to rowData.

Examples

se <- makeMockSpatial()
analyzed <- spatialLagGLM(se, design = ~condition, radius = 2.5)
head(SummarizedExperiment::rowData(analyzed))