Package 'SpaNorm'

Title: Spatially-aware normalisation for spatial transcriptomics data
Description: This package implements the spatially aware library size normalisation algorithm, SpaNorm. SpaNorm normalises out library size effects while retaining biology through the modelling of smooth functions for each effect. Normalisation is performed in a gene- and cell-/spot- specific manner, yielding library size adjusted data.
Authors: Dharmesh D. Bhuva [aut, cre] , Agus Salim [aut] , Ahmed Mohamed [aut]
Maintainer: Dharmesh D. Bhuva <[email protected]>
License: GPL (>= 3)
Version: 1.1.0
Built: 2024-11-30 04:53:00 UTC
Source: https://github.com/bioc/SpaNorm

Help Index


Filter genes based on expression

Description

This function computes the size factors using a fast but inaccurate approach. Size factors are computed using the direct estimate of library sizes (sum of all counts). Though fast, this approach does not cater for compositional biases in the data and therefore is less accurate than scran-based estimates.

Usage

fastSizeFactors(spe)

## S4 method for signature 'SpatialExperiment'
fastSizeFactors(spe)

Arguments

spe

a SpatialExperiment, Seurat, or SpatialFeatureExperiment object containing count data.

Value

a SpatialExperiment, Seurat, or SpatialFeatureExperiment, containing size factors in the 'sizeFactor' column of the column annotation.

Examples

data(HumanDLPFC)
HumanDLPFC <- fastSizeFactors(HumanDLPFC)
head(HumanDLPFC$sizeFactor)

Filter genes based on expression

Description

This function removes genes that are very lowly expressed.

Usage

filterGenes(spe, prop = 0.1)

## S4 method for signature 'SpatialExperiment'
filterGenes(spe, prop = 0.1)

## S4 method for signature 'Seurat'
filterGenes(spe, prop = 0.1)

Arguments

spe

a SpatialExperiment, Seurat, or SpatialFeatureExperiment object containing count data.

prop

a numeric, indicating the proportion of loci/cells where the gene should be expressed (default is 0.1, i.e., genes should be expressed in at least 10% of the loci/cells).

Value

a logical vector encoding which genes should be kept for further analysis.

Examples

data(HumanDLPFC)
keep <- filterGenes(HumanDLPFC)
table(keep)

Human dorsolateral prefrontal cortex (DLPFC) visium sample

Description

Human DLPFC sample profiled using the 10x Visium platform. Lowly expressed genes were filtered out using the filterGenes function to retain genes expressed in at least 10% of samples. This version was obtained from the SpatialLIBD R/Bioconductor package.

Usage

data(HumanDLPFC)

Format

A SpatialExperiment containing region annotations in the colData column 'AnnotatedCluster'.

Source

SpatialLIBD R/Bioconductor package.

References

Maynard KR, Collado-Torres L, Weber LM, Uytingco C, Barry BK, Williams SR, Catallini JL, Tran MN, Besich Z, Tippani M, Chew J. Transcriptome-scale spatial gene expression in the human dorsolateral prefrontal cortex. Nature neuroscience. 2021 Mar;24(3):425-36.


Plot spatial transcriptomic annotations per spot

Description

Plot spatial transcriptomic annotations per spot

Usage

plotSpatial(
  spe,
  what = c("annotation", "expression", "reduceddim"),
  ...,
  assay = SummarizedExperiment::assayNames(spe),
  dimred = SingleCellExperiment::reducedDimNames(spe),
  img = FALSE,
  crop = FALSE,
  imgAlpha = 1,
  rl = 1,
  circles = FALSE
)

Arguments

spe

a SpatialExperiment object.

what

a character, specifying what aspect should be plot, "annotation", "expression", or reduced dimension ("reduceddim").

...

additional aesthetic mappings or fixed parameters (e.g., shape = ".").

assay

a character or numeric, specifying the assay to plot (default is the first assay).

dimred

a character or numeric, specifying the reduced dimension to plot (default is the first reduced dimension).

img

a logical, indicating whether the tissue image (if present) should be plot (default = FALSE).

crop

a logical, indicating whether the image should be cropped to the spatial coordinates (default = FALSE).

imgAlpha

a numeric, specifying the alpha value for the image (default = 1).

rl

a numeric, specifying the relative size of the text (default = 1).

circles

a logical, indicating whether the spots should be plotted as circles (default = FALSE). This can be slower for large datasets.

Value

a ggplot2 object

Examples

library(SpatialExperiment)
library(ggplot2)

# load data
data(HumanDLPFC)

# plot spatial region annotations
p1 <- plotSpatial(HumanDLPFC, colour = AnnotatedCluster)
p1

# change colour scale
p1 + scale_colour_brewer(palette = "Paired")

# plot spatial expression
plotSpatial(HumanDLPFC, what = "expression", colour = ENSG00000075624) +
 scale_colour_viridis_c(option = "F")

# plot logcounts
logcounts(HumanDLPFC) <- log2(counts(HumanDLPFC) + 1)
plotSpatial(HumanDLPFC, what = "expression", colour = ENSG00000075624, assay = "logcounts") +
 scale_colour_viridis_c(option = "F")

# change point shape
plotSpatial(HumanDLPFC, what = "expression", colour = ENSG00000075624, assay = "logcounts", shape = 18) +
 scale_colour_viridis_c(option = "F")

Spatially-dependent normalisation for spatial transcriptomics data

Description

Performs normalisation of spatial transcriptomics data using spatially-dependent spot- and gene- specific size factors.

Usage

SpaNorm(
  spe,
  sample.p = 0.25,
  gene.model = c("nb"),
  adj.method = c("auto", "logpac", "pearson", "medbio", "meanbio"),
  scale.factor = 1,
  df.tps = 6,
  lambda.a = 1e-04,
  batch = NULL,
  tol = 1e-04,
  step.factor = 0.5,
  maxit.nb = 50,
  maxit.psi = 25,
  maxn.psi = 500,
  overwrite = FALSE,
  verbose = TRUE,
  ...
)

## S4 method for signature 'SpatialExperiment'
SpaNorm(
  spe,
  sample.p = 0.25,
  gene.model = c("nb"),
  adj.method = c("auto", "logpac", "pearson", "medbio", "meanbio"),
  scale.factor = 1,
  df.tps = 6,
  lambda.a = 1e-04,
  batch = NULL,
  tol = 1e-04,
  step.factor = 0.5,
  maxit.nb = 50,
  maxit.psi = 25,
  maxn.psi = 500,
  overwrite = FALSE,
  verbose = TRUE,
  ...
)

Arguments

spe

a SpatialExperiment or Seurat object, with the count data stored in 'counts' or 'data' assays respectively.

sample.p

a numeric, specifying the (maximum) proportion of cells/spots to sample for model fitting (default is 0.25).

gene.model

a character, specifying the model to use for gene/protein abundances (default 'nb'). This should be 'nb' for count based datasets.

adj.method

a character, specifying the method to use to adjust the data (default 'auto', see details)

scale.factor

a numeric, specifying the sample-specific scaling factor to scale the adjusted count.

df.tps

a numeric, specifying the degrees of freedom for the thin-plate spline (default is 6).

lambda.a

a numeric, specifying the smoothing parameter for regularizing regression coefficients (default is 0.0001). Actual lambda.a used is lambda.a * ncol(spe).

batch

a vector or numeric matrix, specifying the batch design to regress out (default NULL, representing no batch effects). See details for more information on how to define this variable.

tol

a numeric, specifying the tolerance for convergence (default is 1e-4).

step.factor

a numeric, specifying the multiplicative factor to decrease IRLS step by when log-likelihood diverges (default is 0.5).

maxit.nb

a numeric, specifying the maximum number of IRLS iteration for estimating NB mean parameters for a given dispersion parameter (default is 50).

maxit.psi

a numeric, specifying the maximum number of IRLS iterations to estimate the dispersion parameter (default is 25).

maxn.psi

a numeric, specifying the maximum number of cells/spots to sample for dispersion estimation (default is 500).

overwrite

a logical, specifying whether to force recomputation and overwrite an existing fit (default FALSE). Note that if df.tps, batch, lambda.a, or gene.model are changed, the model is recomputed and overwritten.

verbose

a logical, specifying whether to show update messages (default TRUE).

...

other parameters fitting parameters.

Details

SpaNorm works by first fitting a spatial regression model for library size to the data. Normalised data can then be computed using various adjustment approaches. When a negative binomial gene-model is used, the data can be adjusted using the following approaches: 'logpac', 'pearson', 'medbio', and 'meanbio'.

Batch effects can be specified using the batch parameter. If this parameter is a vector, a design matrix will be created within the function using model.matrix. If a custom design is provided in the form of a numeric matrix, this should ideally be created using model.matrix. The batch matrix should be created with an intercept term. The SpaNorm function will automatically detect the intercept term and remove the relevant column. Alternatively, users can subset the model matrix to remove this column manually. Please note that the model formula should include the intercept term and that the intercept column should be subset out after.

Value

a SpatialExperiment or Seurat object with the adjusted data stored in 'logcounts' or 'data', respectively.

Examples

data(HumanDLPFC)

SpaNorm(HumanDLPFC, sample.p = 0.05, df.tps = 2, tol = 1e-2)

An S4 class to store a SpaNorm model fit

Description

An S4 class to store a SpaNorm model fit

Usage

## S4 method for signature 'SpaNormFit'
x$name

Arguments

x

an object of class SpaNormFit.

name

a character, specifying the name of the slot to retrieve.

Value

Return value varies depending on method.

Slots

ngenes

a numeric, specifying the number of genes in the dataset.

ncells

a numeric, specifying the number of cells/spots in the dataset.

gene.model

a character, specifying the gene-specific model to used (see getGeneModels()).

df.tps

an integer, specifying the degrees of freedom to used for the thin plate spline.

sample.p

a numeric, specifying the proportion of samples used to approximated the model.

lambda.a

a numeric, specifying the shinkage parameter used.

batch

a vector or matrix, specifying the batch design used (if any).

W

a matrix, specifying the covariate matrix of the linear model.

alpha

a matrix, specifying the coefficients of the linear model.

gmean

a numeric, specifying the mean estimate for each gene in the linear model.

psi

a numeric, specifying the over-dispersion parameter for each gene if a negative binomial model was used (or a vector of NAs if another gene model is used).

wtype

a factor, specifying the covariate types of columns in the covariate matrix, W. These could be "biology", "ls", or "batch".

loglik

a numeric, specifying the log-likelihood of the model at each external iteration.

Examples

example(SpaNorm)