switchde: inference of switch-like gene behaviour along single-cell trajectories

title: "switchde: inference of switch-like gene behaviour along single-cell trajectories" author: "Kieran Campbell" date: "r Sys.Date()" output: BiocStyle::html_document vignette: > %\VignetteIndexEntry{An overview of the switchde package} %\VignetteEngine{knitr::rmarkdown} \usepackage[utf8]{inputenc}

knitr::opts_chunk$set( cache = TRUE )


switchde is an R package for detecting switch-like differential expression along single-cell RNA-seq trajectories. It assumes genes follow a sigmoidal pattern of gene expression and tests for differential expression using a likelihood ratio test. It also returns maximum likelihood estimates (MLE) for the sigmoid parameters, which allows filtering of genes for up or down regulation as well as where along the trajectory the regulation occurs.

The parametric form of gene expression assumed is a sigmoid:


Governed by three parameters:


switchde can be installed from both Bioconductor and Github.

Example installation from Bioconductor:

if (!requireNamespace("BiocManager", quietly=TRUE))

Example installation from Github:


Pre-filtering of genes

Several inputs will cause issues in maximum likelihood and expecatation maximisation algorithms typically leading to error messages such as 'finite gradient required'. To avoid these, strict pre-filtering of genes is advised such as retaining genes with a certain mean expression and expressed in a certain proportion of cells. For example, if the matrix X represents logged expression data, we can retain only genes with mean expression greater than 1 and expressed in 20% of cells via

X_filtered <- X[rowMeans(X) > 0.1 & rowMeans(X > 0) > 0.2,]

By default, switchde also sets any expression less than 0.01 to 0. This can be controlled via the lower_threshold parameter.

Example usage

We provide a brief example on some synthetic single-cell data bundled with the package. synth_gex contains a 12-by-100 expression matrix of 12 genes, and ex_pseudotime contains a pseudotime vector of length 100. We can start by plotting the expression:


gex_cleaned <- as_data_frame(t(synth_gex)) %>% 
  dplyr::mutate(Pseudotime = ex_pseudotime) %>% 
  tidyr::gather(Gene, Expression, -Pseudotime)

ggplot(gex_cleaned, aes(x = Pseudotime, y = Expression)) +
  facet_wrap(~ Gene) + geom_point(shape = 21, fill = 'grey', color = 'black') +
  theme_bw() + stat_smooth(color = 'darkred', se = FALSE)

Non-zero inflated

Model fitting and differential expression testing is provided by a call to the switchde function:

sde <- switchde(synth_gex, ex_pseudotime)

This can equivalently be called using an SingleCellExperiment from the package SingleCellExperiment:

sce <- SingleCellExperiment(assays = list(exprs = synth_gex))
sde <- switchde(sce, ex_pseudotime)

This returns a data.frame with 6 columns:

We can use the function arrange from dplyr to order this by q-value:

dplyr::arrange(sde, qval)

We may then wish to plot the expression of a particular gene and the MLE model. This is acheived using the switchplot function, which takes three arguments:

We can easily extract the parameters using the extract_pars function and pass this to switchplot, which plots the maximum likelihood sigmoidal mean:

gene <- sde$gene[which.min(sde$qval)]
pars <- extract_pars(sde, gene)

switchplot(synth_gex[gene, ], ex_pseudotime, pars)

Note that switchplot returns a ggplot which can be further customised (e.g. using theme_xxx(), etc).


We can also model zero inflation in the data with a dropout probability proportional to the latent gene expression magnitude. To enable this set zero_inflation = TRUE. While this model is more appropriate for single-cell RNA-seq data, it requires use of the EM algorithm so takes typically 20x longer.

zde <- switchde(synth_gex, ex_pseudotime, zero_inflated = TRUE)

As before it returns a data_frame, this time with an additional parameter $\lambda$ corresponding to the dropout probability (see manuscript):

dplyr::arrange(zde, qval)

We can plot the gene with the largest dropout effect and compare it to the non-zero-inflated model:

gene <- zde$gene[which.min(zde$lambda)]
pars <- extract_pars(sde, gene)
zpars <- extract_pars(zde, gene)

switchplot(synth_gex[gene, ], ex_pseudotime, pars)
switchplot(synth_gex[gene, ], ex_pseudotime, zpars)

Controlling the EM algorithm

For zero-inflation the expectation-maximisation algorithm is used which will converge up to a user-supplied change in the log-likelihood after a given number of iterations. These are controlled by the parameters maxiter and log_lik_tol in the call to switchde. Most genes will converge after very few iterations, but some - particularly those with many zeros where a well defined 'step' can be fit - may take much longer. The default parameters are designed as a trade-off between accuracy and speed.

If any genes do not converge using the default parameters, the user is warned and should expect the EM_converged column of the output. In this case, three options are available:

  1. Trust that after 1000 EM iterations, the parameter estimates will be good enough
  2. Refit the genes for which EM_converged == FALSE with either increasing maxiter or increased log_lik_tol
  3. Discard those genes altogether

Use cases

Most pseudotime algorithms will infer something similar to principal component 1 or 2 of the data. Therefore, by definition, many genes will vary across pseudotime leading to a large proportion passing a strict FDR adjusted significance threshold. Genes designated as significant should therefore be treated with appropriate skepticism and ideally experimentally validated.

We further suggest some use cases that might be of interest to researchers:

  1. Take all genes passing some FDR threshold and perform GO analysis (using packages such as topGO or goseq) to find out what a particular pseudotime or principal component in their data corresponds to
  2. Select genes with interesting behaviour. For example, genes could be identified that most exhibit switch or step-like behaviour along the trajectory, using dplyr calls such as filter(sde, qval < 0.01, abs(k) > quantile(abs(k), 0.95))
  3. Find which genes govern different parts of the pseudotime trajectory. This can be accomplished by filtering for significant genes in different time points. For example, to find genes regulated in the first quarter of the trajectory, one could call
pst_quantiles <- quantile(pst, c(0, 0.25))
filter(sde, qval < 0.01, t0 > pst_quantiles[1], t0 < pst_quantiles[2])

Technical info


Try the switchde package in your browser

Any scripts or data that you put into this service are public.

switchde documentation built on Nov. 8, 2020, 5:19 p.m.