Contents

1 Introduction

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:

example_sigmoid()

Governed by three parameters:

2 Installation

switchde can be installed from both Bioconductor and Github.

Example installation from Bioconductor:

source("https://bioconductor.org/biocLite.R")
biocLite("switchde")

Example installation from Github:

devtools::install_github("kieranrcampbell/switchde")

3 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.

4 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:

data(synth_gex)
data(ex_pseudotime)

gex_cleaned <- as_data_frame(t(synth_gex)) %>% 
  mutate(Pseudotime = ex_pseudotime) %>% 
  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)
## `geom_smooth()` using method = 'loess'

4.1 Non-zero inflated

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

sde <- switchde(synth_gex, ex_pseudotime)
## Input gene-by-cell matrix: 12 genes and  100 cells

This can equivalently be called using an SCESet from the package scater:

sce <- newSCESet(synth_gex)
## Warning in newSCESet(synth_gex): 'logExprsOffset' should be set manually
## for non-count data
## Warning in newSCESet(synth_gex): 'lowerDetectionLimit' should be set
## manually for log-expression values
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:

arrange(sde, qval)
## # A tibble: 12 × 6
##      gene         pval         qval       mu0         k         t0
##     <chr>        <dbl>        <dbl>     <dbl>     <dbl>      <dbl>
## 1   Gene8 6.511826e-21 7.814191e-20  5.320604 -9.217995  0.1675885
## 2   Gene2 1.030532e-12 6.183192e-12  3.445171  6.792040  0.7013710
## 3   Gene9 3.389230e-12 1.355692e-11  5.525775 -5.048873  0.3183684
## 4   Gene1 9.478795e-12 2.843639e-11  3.864267  7.506369  0.5669419
## 5   Gene5 1.544662e-11 3.707188e-11  3.514618 10.348479  0.4752514
## 6   Gene4 4.831243e-10 9.662485e-10 12.843280 -3.224137 -0.2114432
## 7   Gene3 2.196190e-08 3.764897e-08  4.202233 -4.998180  0.5578453
## 8  Gene10 4.473107e-08 6.709660e-08  3.176998  8.471475  0.3797383
## 9   Gene7 4.273169e-06 5.697559e-06  3.778711  7.417856  0.3024221
## 10 Gene12 3.362652e-04 4.035183e-04  4.858423 -4.047125  0.7791013
## 11 Gene11 1.024067e-03 1.117164e-03  2.689331 -7.272166  0.8318200
## 12  Gene6 1.913351e-02 1.913351e-02  1.758552 -8.871320  0.8700276

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)
print(pars)
##        mu0          k         t0 
##  5.3206036 -9.2179954  0.1675885
switchplot(synth_gex[gene, ], ex_pseudotime, pars)

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

4.2 Zero-inflation

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)
## Input gene-by-cell matrix: 12 genes and  100 cells

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

arrange(zde, qval)
## # A tibble: 12 × 8
##      gene         pval         qval       mu0         k         t0
##     <chr>        <dbl>        <dbl>     <dbl>     <dbl>      <dbl>
## 1   Gene8 5.625294e-21 6.750353e-20  5.317191 -9.207447  0.1679975
## 2   Gene2 6.213000e-13 3.727800e-12  3.459876  6.781590  0.7018734
## 3   Gene9 2.316019e-12 9.264078e-12  5.516667 -5.062499  0.3202179
## 4   Gene1 3.656618e-12 1.096985e-11  3.892672  7.442629  0.5676791
## 5   Gene5 1.005704e-11 2.413690e-11  3.522190 10.359586  0.4749875
## 6   Gene4 1.553175e-10 3.106349e-10 12.585511 -3.239785 -0.1981837
## 7   Gene3 1.453212e-08 2.491220e-08  4.225005 -4.976907  0.5567603
## 8  Gene10 2.261518e-08 3.392277e-08  3.190127  8.430649  0.3768249
## 9   Gene7 3.833282e-06 5.111042e-06  3.784151  7.402635  0.3026021
## 10 Gene12 2.961844e-04 3.554212e-04  4.903057 -3.965264  0.7756024
## 11 Gene11 9.377884e-04 1.023042e-03  2.699964 -7.178971  0.8324670
## 12  Gene6 1.430175e-02 1.430175e-02  1.778996 -8.672376  0.8692932
## # ... with 2 more variables: lambda <dbl>, EM_converged <lgl>

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)

4.2.1 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

5 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])

6 Technical info

sessionInfo()
## R version 3.4.0 (2017-04-21)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 16.04.2 LTS
## 
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.5-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.5-bioc/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] parallel  stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] switchde_1.2.0      tidyr_0.6.1         dplyr_0.5.0        
## [4] scater_1.4.0        ggplot2_2.2.1       Biobase_2.36.0     
## [7] BiocGenerics_0.22.0 BiocStyle_2.4.0    
## 
## loaded via a namespace (and not attached):
##  [1] tximport_1.4.0       beeswarm_0.2.3       locfit_1.5-9.1      
##  [4] reshape2_1.4.2       lattice_0.20-35      rhdf5_2.20.0        
##  [7] colorspace_1.3-2     htmltools_0.3.5      stats4_3.4.0        
## [10] viridisLite_0.2.0    yaml_2.1.14          XML_3.98-1.6        
## [13] DBI_0.6-1            matrixStats_0.52.2   plyr_1.8.4          
## [16] zlibbioc_1.22.0      stringr_1.2.0        munsell_0.4.3       
## [19] gtable_0.2.0         codetools_0.2-15     evaluate_0.10       
## [22] memoise_1.1.0        labeling_0.3         knitr_1.15.1        
## [25] IRanges_2.10.0       biomaRt_2.32.0       httpuv_1.3.3        
## [28] vipor_0.4.5          AnnotationDbi_1.38.0 Rcpp_0.12.10        
## [31] xtable_1.8-2         edgeR_3.18.0         scales_0.4.1        
## [34] backports_1.0.5      limma_3.32.0         S4Vectors_0.14.0    
## [37] mime_0.5             gridExtra_2.2.1      rjson_0.2.15        
## [40] digest_0.6.12        stringi_1.1.5        shiny_1.0.2         
## [43] grid_3.4.0           rprojroot_1.2        tools_3.4.0         
## [46] bitops_1.0-6         magrittr_1.5         lazyeval_0.2.0      
## [49] RCurl_1.95-4.8       tibble_1.3.0         RSQLite_1.1-2       
## [52] Matrix_1.2-9         data.table_1.10.4    ggbeeswarm_0.5.3    
## [55] shinydashboard_0.5.3 assertthat_0.2.0     rmarkdown_1.4       
## [58] viridis_0.4.0        R6_2.2.0             compiler_3.4.0