Typical RNA-seq call from DESeq2

Note: the typical RNA-seq workflow for users would be to call apeglm estimation from within the lfcShrink function from the DESeq2 package. The unevaluated code chunk shows how to obtain apeglm shrinkage estimates after running DESeq. See the DESeq2 vignette for more details. The lfcShrink wrapper function takes care of many details below, and unifies the interface for multiple shrinkage estimators. The coefficient to shrink can be specified either by name or by number (following the order in resultsNames(dds)). Be aware that DESeq2’s lfcShrink interface provides LFCs on the log2 scale, while apeglm provides coefficients on the natural log scale.

Example RNA-seq analysis

Here we show example code which mimics what will happen inside the lfcShrink function when using the apeglm method (Zhu, Ibrahim, and Love 2018).

Load a prepared SummarizedExperiment:

##                 SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516
## ENSG00000000003        679        448        873        408       1138
## ENSG00000000005          0          0          0          0          0
## ENSG00000000419        467        515        621        365        587
## ENSG00000000457        260        211        263        164        245
## ENSG00000000460         60         55         40         35         78
## ENSG00000000938          0          0          2          0          1
##                 SRR1039517 SRR1039520 SRR1039521
## ENSG00000000003       1047        770        572
## ENSG00000000005          0          0          0
## ENSG00000000419        799        417        508
## ENSG00000000457        331        233        229
## ENSG00000000460         63         76         60
## ENSG00000000938          0          0          0

For demonstration, we will use 5000 genes of the airway dataset, the first from those genes with at least 10 counts across all samples.

First run a DESeq2 differential expression analysis (Love, Huber, and Anders 2014) (size factors, and dispersion estimates could similarly be estimated using edgeR):

Defining data and parameter objects necessary for apeglm. We must multiply the coefficients from DESeq2 by a factor, because apeglm provides natural log coefficients. Again, this would be handled inside of lfcShrink in DESeq2 for a typical RNA-seq analysis.

Running apeglm

Here apeglm on 5000 genes takes less than a minute on a laptop. It scales with number of genes, the number of samples and the number of variables in the design formula, where here we have 5 coefficients (one for the four cell cultures and one for the difference due to dexamethasone treatment).

We provide apeglm with the SummarizedExperiment although the function can also run on a matrix of counts or other observed data. We specify a coef as well as a threshold which we discuss below. Note that we multiple the threshold by log(2) to convert from log2 scale to natural log scale.

##    user  system elapsed 
##  25.072   0.004  25.095
## List of 7
##  $ no.shrink            : int [1:4] 1 2 3 4
##  $ prior.mean           : num 0
##  $ prior.scale          : num 0.299
##  $ prior.df             : num 1
##  $ prior.no.shrink.mean : num 0
##  $ prior.no.shrink.scale: num 15
##  $ prior.var            : num 0.0894

There are faster implementations of apeglm specifically for negative binomial likelihoods. The version nbinomR is ~5 times faster than the default general.

##    user  system elapsed 
##   5.708   0.000   5.711

The version nbinomCR is ~10 times faster than the default general.

##    user  system elapsed 
##   2.900   0.000   2.903

The version nbinomC returns only the MAP coefficients and can be ~50-100 times faster than the default general. The MAP coefficients are the same as returned by nbinomCR above, we just skip the calculation of posterior SD. A variant of nbinomC is nbinomC* which includes random starts.

##    user  system elapsed 
##   0.188   0.000   0.188

Among other output, we have the estimated coefficients attached to the ranges of the SummarizedExperiment used as input:

## [1] "CompressedGRangesList"
## attr(,"package")
## [1] "GenomicRanges"
## DataFrame with 5000 rows and 5 columns
##                       X.Intercept.         cellN061011         cellN080611
##                          <numeric>           <numeric>           <numeric>
## ENSG00000000003   6.64565685883533  0.0607881068454851   0.229672330995948
## ENSG00000000419   6.23452224381129 -0.0849990629106306 -0.0151414731625982
## ENSG00000000457   5.45295046335278  0.0601670302480185 -0.0556310344837282
## ENSG00000000460   3.77047988584789   0.545203896896547   0.261356100282538
## ENSG00000000938 -0.314689550017607   -4.17748981931399  -0.539678670119333
## ...                            ...                 ...                 ...
## ENSG00000122756  -3.62985171822994    4.03910910319155   -2.00591343153508
## ENSG00000122778   5.23905962019222  0.0800614479132497   0.785718241416109
## ENSG00000122779    5.6686092652971 -0.0569747440556084  0.0501292974551586
## ENSG00000122783   6.01534721389999  0.0368918236875201  -0.157828298844088
## ENSG00000122786   9.78251386174189  -0.182349284400608  0.0942307382675965
##                          cellN61311                dextrt
##                           <numeric>             <numeric>
## ENSG00000000003  -0.152039407728874     -0.26470148487765
## ENSG00000000419 -0.0476622243538475      0.11513214059328
## ENSG00000000457  0.0484622685322348   0.00907000392879693
## ENSG00000000460   0.349429848473466   -0.0425238102966019
## ENSG00000000938   -4.20084641406842  -0.00836574242389753
## ...                             ...                   ...
## ENSG00000122756    2.95799390281032 -0.000701743026964077
## ENSG00000122778   0.240098604814203    -0.285457153642263
## ENSG00000122779 -0.0441315171478886    -0.109945276882168
## ENSG00000122783  -0.297399861534912    0.0690120376642595
## ENSG00000122786    -0.1202800288012     0.523514860344816

We can compare the coefficients from apeglm with those from DESeq2. apeglm provides coefficients on the natural log scale, so we must convert to log2 scale by multiplying by log2(exp(1)). Note that DESeq2’s lfcShrink function converts apeglm coefficients to the log2 scale internally.

##    user  system elapsed 
##   1.812   0.000   1.810

Here we plot apeglm estimators against DESeq2:

Here we plot MLE, DESeq2 and apeglm estimators against the mean of normalized counts: