Package 'spatialLagGLM'

Title: Spatial Lag Regression for Spatial Transcriptomics
Description: Tests each gene in spatial transcriptomics data for 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, p-value and adjusted p-value of the lag are stored in the rowData of the input SpatialExperiment. Weights are computed in C++ with RcppArmadillo, and genes are fitted in parallel with BiocParallel. The p-values are intended for exploratory use. 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.4
Built: 2026-11-25 13:11:17 UTC
Source: https://github.com/BiocStaging/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()

Volcano Plot of Spatial Lag Results

Description

Volcano Plot of Spatial Lag Results

Usage

plotLagVolcano(se, alpha = 0.05, effect_cutoff = 0.5)

Arguments

se

A SpatialExperiment analysed with spatialLagGLM.

alpha

A single number in (0, 1]: adjusted p-value cutoff.

effect_cutoff

A single non-negative number: effect size cutoff.

Value

A ggplot2 object.

Examples

se <- makeMockSpatial()
analyzed <- spatialLagGLM(se, design = ~condition, radius = 2.5)
plotLagVolcano(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 = "counts"
)

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.

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 p-value and its Benjamini-Hochberg adjusted p-value are stored in rowData(se).

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 null hypothesis tested is γ=0\gamma = 0: given the offset and the design terms, a spot's expression is not associated with the weighted mean of its neighbours' normalised expression.

Because the lag is built from the same gene's counts, this is not a spatial autoregressive model, and the quasi-Poisson p-values have not been shown to be calibrated for this test. Treat them as exploratory and, where it matters, check them against a permutation of the spot coordinates.

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), p_value and adjusted_p_value. The radius, assay and size factors used are stored in metadata(se)$spatialLagGLM.

Value

se with spatial_effect_size, p_value and adjusted_p_value added to rowData.

Examples

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