The material in this course requires R version 3.2 and Bioconductor version 3.2

    getRversion() >= '3.2' && getRversion() < '3.3',
    BiocInstaller::biocVersion() == "3.2"

1 Background

Google Doc

For Review:

This lab is derived from: RNA-Seq workflow: gene-level exploratory analysis and differential expression, by Michael Love, Simon Anders, Wolfgang Huber; modified by Martin Morgan, October 2015.

This lab will walk you through an end-to-end RNA-Seq differential expression workflow, using DESeq2 along with other Bioconductor packages. The complete work flow starts from the FASTQ files, but we will start after reads have been aligned to a reference genome and reads overlapping known genes have been counted. We will perform exploratory data analysis (EDA), differential gene expression analysis with DESeq2, and visually explore the results.

A number of other Bioconductor packages are important in statistical inference of differential expression at the gene level, including Rsubread, edgeR, limma, BaySeq, and others.

2 Experimental data

The data used in this workflow is an RNA-Seq experiment of airway smooth muscle cells treated with dexamethasone, a synthetic glucocorticoid steroid with anti-inflammatory effects. Glucocorticoids are used, for example, in asthma patients to prevent or reduce inflammation of the airways. In the experiment, four primary human airway smooth muscle cell lines were treated with 1 micromolar dexamethasone for 18 hours. For each of the four cell lines, we have a treated and an untreated sample. The reference for the experiment is:

Himes BE, Jiang X, Wagner P, Hu R, Wang Q, Klanderman B, Whitaker RM, Duan Q, Lasky-Su J, Nikolos C, Jester W, Johnson M, Panettieri R Jr, Tantisira KG, Weiss ST, Lu Q. “RNA-Seq Transcriptome Profiling Identifies CRISPLD2 as a Glucocorticoid Responsive Gene that Modulates Cytokine Function in Airway Smooth Muscle Cells.” PLoS One. 2014 Jun 13;9(6):e99625. PMID: 24926665. GEO: GSE52778.

3 Preparing count matrices

As input, DESeq2 package expects count data as obtained, e.g., from RNA-Seq or another high-throughput sequencing experiment, in the form of a matrix of integer values. The value in the i-th row and the j-th column of the matrix tells how many reads have been mapped to gene i in sample j. Analogously, for other types of assays, the rows of the matrix might correspond e.g., to binding regions (with ChIP-Seq) or peptide sequences (with quantitative mass spectrometry).

The count values must be raw counts of sequencing reads. This is important for DESeq2’s statistical model to hold, as only the actual counts allow assessing the measurement precision correctly. Hence, please do not supply other quantities, such as (rounded) normalized counts, or counts of covered base pairs – this will only lead to nonsensical results.

We will discuss how to summarize data from BAM files to a count table later in ther course. Here we’ll ‘jump right in’ and start with a prepared SummarizedExperiment.

4 Starting from SummarizedExperiment

We now use R’s data() command to load a prepared SummarizedExperiment that was generated from the publicly available sequencing data files associated with the Himes et al. paper, described above. The steps we used to produce this object were equivalent to those you worked through in the previous sections, except that we used all the reads and all the genes. For more details on the exact steps used to create this object type vignette("airway") into your R session.

se <- airway

The information in a SummarizedExperiment object can be accessed with accessor functions. For example, to see the actual data, i.e., here, the read counts, we use the assay() function. (The head() function restricts the output to the first few lines.)

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

In this count matrix, each row represents an Ensembl gene, each column a sequenced RNA library, and the values give the raw numbers of sequencing reads that were mapped to the respective gene in each library. We also have metadata on each of the samples (the columns of the count matrix). If you’ve counted reads with some other software, you need to check at this step that the columns of the count matrix correspond to the rows of the column metadata.

We can quickly check the millions of fragments which uniquely aligned to the genes.

## SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516 SRR1039517 SRR1039520 SRR1039521 
##   20637971   18809481   25348649   15163415   24448408   30818215   19126151   21164133

Supposing we have constructed a SummarizedExperiment using one of the methods described in the previous section, we now need to make sure that the object contains all the necessary information about the samples, i.e., a table with metadata on the count matrix’s columns stored in the colData slot:

## DataFrame with 8 rows and 9 columns
##            SampleName     cell      dex    albut        Run avgLength Experiment    Sample
##              <factor> <factor> <factor> <factor>   <factor> <integer>   <factor>  <factor>
## SRR1039508 GSM1275862   N61311    untrt    untrt SRR1039508       126  SRX384345 SRS508568
## SRR1039509 GSM1275863   N61311      trt    untrt SRR1039509       126  SRX384346 SRS508567
## SRR1039512 GSM1275866  N052611    untrt    untrt SRR1039512       126  SRX384349 SRS508571
## SRR1039513 GSM1275867  N052611      trt    untrt SRR1039513        87  SRX384350 SRS508572
## SRR1039516 GSM1275870  N080611    untrt    untrt SRR1039516       120  SRX384353 SRS508575
## SRR1039517 GSM1275871  N080611      trt    untrt SRR1039517       126  SRX384354 SRS508576
## SRR1039520 GSM1275874  N061011    untrt    untrt SRR1039520       101  SRX384357 SRS508579
## SRR1039521 GSM1275875  N061011      trt    untrt SRR1039521        98  SRX384358 SRS508580
##               BioSample
##                <factor>
## SRR1039508 SAMN02422669
## SRR1039509 SAMN02422675
## SRR1039512 SAMN02422678
## SRR1039513 SAMN02422670
## SRR1039516 SAMN02422682
## SRR1039517 SAMN02422673
## SRR1039520 SAMN02422683
## SRR1039521 SAMN02422677

Here we see that this object already contains an informative colData slot – because we have already prepared it for you, as described in the airway vignette. However, when you work with your own data, you will have to add the pertinent sample / phenotypic information for the experiment at this stage. We highly recommend keeping this information in a comma-separated value (CSV) or tab-separated value (TSV) file, which can be exported from an Excel spreadsheet, and the assign this to the colData slot, making sure that the rows correspond to the columns of the SummarizedExperiment. We made sure of this correspondence by specifying the BAM files using a column of the sample table.

Check out the rowRanges() of the summarized experiment; these are the genomic ranges over which counting occurred.

## GRangesList object of length 64102:
## $ENSG00000000003 
## GRanges object with 17 ranges and 2 metadata columns:
##        seqnames               ranges strand   |   exon_id       exon_name
##           <Rle>            <IRanges>  <Rle>   | <integer>     <character>
##    [1]        X [99883667, 99884983]      -   |    667145 ENSE00001459322
##    [2]        X [99885756, 99885863]      -   |    667146 ENSE00000868868
##    [3]        X [99887482, 99887565]      -   |    667147 ENSE00000401072
##    [4]        X [99887538, 99887565]      -   |    667148 ENSE00001849132
##    [5]        X [99888402, 99888536]      -   |    667149 ENSE00003554016
##    ...      ...                  ...    ... ...       ...             ...
##   [13]        X [99890555, 99890743]      -   |    667156 ENSE00003512331
##   [14]        X [99891188, 99891686]      -   |    667158 ENSE00001886883
##   [15]        X [99891605, 99891803]      -   |    667159 ENSE00001855382
##   [16]        X [99891790, 99892101]      -   |    667160 ENSE00001863395
##   [17]        X [99894942, 99894988]      -   |    667161 ENSE00001828996
## ...
## <64101 more elements>
## -------
## seqinfo: 722 sequences (1 circular) from an unspecified genome

5 From SummarizedExperiment to DESeqDataSet

We will use the DESeq2 package for assessing differential expression. The package uses an extended version of the SummarizedExperiment calass, called DESeqDataSet. It’s easy to go from a SummarizedExperiment to DESeqDataSet:

dds <- DESeqDataSet(se, design = ~ cell + dex)

6 Visually exploring the dataset

6.0.1 The rlog transformation

Many common statistical methods for exploratory analysis of multidimensional data, especially methods for clustering and ordination (e.g., principal-component analysis and the like), work best for (at least approximately) homoskedastic data; this means that the variance of an observed quantity (here, the expression strength of a gene) does not depend on the mean. In RNA-Seq data, however, variance grows with the mean. For example, if one performs PCA (principal components analysis) directly on a matrix of normalized read counts, the result typically depends only on the few most strongly expressed genes because they show the largest absolute differences between samples. A simple and often used strategy to avoid this is to take the logarithm of the normalized count values plus a small pseudocount; however, now the genes with low counts tend to dominate the results because, due to the strong Poisson noise inherent to small count values, they show the strongest relative differences between samples.

As a solution, DESeq2 offers the regularized-logarithm transformation, or rlog() for short. For genes with high counts, the rlog transformation differs not much from an ordinary log2 transformation. For genes with lower counts, however, the values are shrunken towards the genes’ averages across all samples. Using an empirical Bayesian prior on inter-sample differences in the form of a ridge penalty, this is done such that the rlog-transformed data are approximately homoskedastic. See the help for ?rlog for more information and options. Another transformation, the variance stabilizing transformation (vsn()), is discussed alongside the rlog() in the DESeq2 vignette.

Note: the rlog transformation is provided for applications other than differential testing. For differential testing we recommend the DESeq() function applied to raw counts, as described later in this workflow, which also takes into account the dependence of the variance of counts on the mean value during the dispersion estimation step.

The function rlog() returns a SummarizedExperiment object which contains the rlog-transformed values in its assay() slot:

rld <- rlog(dds)
##                 SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516 SRR1039517 SRR1039520
## ENSG00000000003   9.399151   9.142478   9.501695   9.320796   9.757212   9.512183   9.617378
## ENSG00000000005   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000
## ENSG00000000419   8.901283   9.113976   9.032567   9.063925   8.981930   9.108531   8.894830
## ENSG00000000457   7.949897   7.882371   7.834273   7.916459   7.773819   7.886645   7.946411
## ENSG00000000460   5.849521   5.882363   5.486937   5.770334   5.940407   5.663847   6.107733
## ENSG00000000938  -1.638084  -1.637483  -1.558248  -1.636072  -1.597606  -1.639362  -1.637608
##                 SRR1039521
## ENSG00000000003   9.315309
## ENSG00000000005   0.000000
## ENSG00000000419   9.052303
## ENSG00000000457   7.908338
## ENSG00000000460   5.907824
## ENSG00000000938  -1.637724

To show the effect of the transformation, we plot the first sample against the second, first simply using the log2() function (after adding 1, to avoid taking the log of zero), and then using the rlog-transformed values. For the log2() method, we need estimate size factors to account for sequencing depth (this is done automatically for the rlog() method).

opar <- par( mfrow = c( 1, 2 ) )
dds <- estimateSizeFactors(dds)
plot( log2( 1 + counts(dds, normalized=TRUE)[ , 1:2] ),
     col=rgb(0,0,0,.2), pch=16, cex=0.3 )
plot( assay(rld)[ , 1:2],
     col=rgb(0,0,0,.2), pch=16, cex=0.3 )


Note that, in order to make it easier to see where several points are plotted on top of each other, we set the plotting color to a semi-transparent black and changed the points to solid circles (pch=16) with reduced size (cex=0.3).

We can see how genes with low counts seem to be excessively variable on the ordinary logarithmic scale, while the rlog transform compresses differences for genes for which the data cannot provide good information anyway.

6.0.2 Sample distances

A useful first step in an RNA-Seq analysis is often to assess overall similarity between samples: Which samples are similar to each other, which are different? Does this fit to the expectation from the experiment’s design?

We use the R function dist() to calculate the Euclidean distance between samples. To avoid that the distance measure is dominated by a few highly variable genes, and have a roughly equal contribution from all genes, we use it on the rlog-transformed data:

sampleDists <- dist( t( assay(rld) ) )
##            SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516 SRR1039517 SRR1039520
## SRR1039509   40.89060                                                                  
## SRR1039512   37.35231   50.07638                                                       
## SRR1039513   55.74569   41.49280   43.61052                                            
## SRR1039516   41.98797   53.58929   40.99513   57.10447                                 
## SRR1039517   57.69438   47.59326   53.52310   46.13742   42.10583                      
## SRR1039520   37.06633   51.80994   34.86653   52.54968   43.21786   57.13688           
## SRR1039521   56.04254   41.46514   51.90045   34.82975   58.40428   47.90244   44.78171

Note the use of the function t() to transpose the data matrix. We need this because dist() calculates distances between data rows and our samples constitute the columns.

We visualize the distances in a heatmap, using the function heatmap.2() from the gplots package.


We have to provide a hierarchical clustering hc to the heatmap.2() function based on the sample distances, or else the heatmap.2() function would calculate a clustering based on the distances between the rows/columns of the distance matrix.

sampleDistMatrix <- as.matrix( sampleDists )
rownames(sampleDistMatrix) <- paste( rld$dex, rld$cell, sep="-" )
colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)
hc <- hclust(sampleDists)
heatmap.2( sampleDistMatrix, Rowv=as.dendrogram(hc),
          symm=TRUE, trace="none", col=colors,
          margins=c(2,10), labCol=FALSE )

Note that we have changed the row names of the distance matrix to contain treatment type and patient number instead of sample ID, so that we have all this information in view when looking at the heatmap.

6.0.3 PCA plot

Another way to visualize sample-to-sample distances is a principal-components analysis (PCA). In this ordination method, the data points (i.e., here, the samples) are projected onto the 2D plane such that they spread out in the two directions which explain most of the differences in the data. The x-axis is the direction (or principal component) which separates the data points the most. The amount of the total variance which is contained in the direction is printed in the axis label.

plotPCA(rld, intgroup = c("dex", "cell"))

Here, we have used the function plotPCA() which comes with DESeq2. The two terms specified by intgroup are the interesting groups for labelling the samples; they tell the function to use them to choose colors. We can also build the PCA plot from scratch using ggplot2. This is done by asking the plotPCA() function to return the data used for plotting rather than building the plot. See the ggplot2 documentation for more details.

(data <- plotPCA(rld, intgroup = c( "dex", "cell"), returnData=TRUE))
##                   PC1        PC2           group   dex    cell       name
## SRR1039508 -14.331359  -4.208796  untrt : N61311 untrt  N61311 SRR1039508
## SRR1039509   6.754169  -2.245244    trt : N61311   trt  N61311 SRR1039509
## SRR1039512  -8.130393  -3.952904 untrt : N052611 untrt N052611 SRR1039512
## SRR1039513  14.505648  -2.941862   trt : N052611   trt N052611 SRR1039513
## SRR1039516 -11.891410  13.735002 untrt : N080611 untrt N080611 SRR1039516
## SRR1039517   8.373975  17.823844   trt : N080611   trt N080611 SRR1039517
## SRR1039520  -9.965898 -10.014674 untrt : N061011 untrt N061011 SRR1039520
## SRR1039521  14.685269  -8.195366   trt : N061011   trt N061011 SRR1039521
percentVar <- round(100 * attr(data, "percentVar"))

We can then use this data to build up the plot, specifying that the color of the points should reflect dexamethasone treatment and the shape should reflect the cell line.

qplot(PC1, PC2, color=dex, shape=cell, data=data) +
  xlab(paste0("PC1: ", percentVar[1], "% variance")) +
  ylab(paste0("PC2: ", percentVar[2], "% variance"))

From both visualizations, we see that the differences between cells are considerable, though not stronger than the differences due to treatment with dexamethasone. This shows why it will be important to account for this in differential testing by using a paired design (“paired”, because each dex treated sample is paired with one untreated sample from the same cell line). We are already set up for this by using the design formula ~ cell + dex when setting up the data object in the beginning.

7 Differential expression analysis

It will be convenient to make sure that untrt is the first level in the dex factor, so that the default log2 fold changes are calculated as treated over untreated (by default R will chose the first alphabetical level, remember: computers don’t know what to do unless you tell them). The function relevel() achieves this:

dds$dex <- relevel(dds$dex, "untrt")

In addition, if you have at any point subset the columns of the DESeqDataSet you should similarly call droplevels() on the factors if the subsetting has resulted in some levels having 0 samples.

7.1 Running the pipeline

Finally, we are ready to run the differential expression pipeline. With the data object prepared, the DESeq2 analysis can now be run with a single call to the function DESeq():

dds <- DESeq(dds)
## using pre-existing size factors
## estimating dispersions
## gene-wise dispersion estimates
## mean-dispersion relationship
## final dispersion estimates
## fitting model and testing

This function will print out a message for the various steps it performs. These are described in more detail in the manual page ?DESeq. Briefly these are: the estimation of size factors (which control for differences in the library size of the sequencing experiments), the estimation of dispersion for each gene, and fitting a generalized linear model.

A DESeqDataSet is returned which contains all the fitted information within it, and the following section describes how to extract out results tables of interest from this object.

7.2 Building the results table

Calling results() without any arguments will extract the estimated log2 fold changes and p values for the last variable in the design formula. If there are more than 2 levels for this variable, results() will extract the results table for a comparison of the last level over the first level.

(res <- results(dds))
## log2 fold change (MAP): dex trt vs untrt 
## Wald test p-value: dex trt vs untrt 
## DataFrame with 64102 rows and 6 columns
##                  baseMean log2FoldChange      lfcSE       stat       pvalue        padj
##                 <numeric>      <numeric>  <numeric>  <numeric>    <numeric>   <numeric>
## ENSG00000000003 708.60217    -0.37424998 0.09873107 -3.7906000 0.0001502838 0.001251416
## ENSG00000000005   0.00000             NA         NA         NA           NA          NA
## ENSG00000000419 520.29790     0.20215551 0.10929899  1.8495642 0.0643763851 0.192284345
## ENSG00000000457 237.16304     0.03624826 0.13684258  0.2648902 0.7910940556 0.910776144
## ENSG00000000460  57.93263    -0.08523371 0.24654402 -0.3457140 0.7295576905 0.878646940
## ...                   ...            ...        ...        ...          ...         ...
## LRG_94                  0             NA         NA         NA           NA          NA
## LRG_96                  0             NA         NA         NA           NA          NA
## LRG_97                  0             NA         NA         NA           NA          NA
## LRG_98                  0             NA         NA         NA           NA          NA
## LRG_99                  0             NA         NA         NA           NA          NA

As res is a DataFrame object, it carries metadata with information on the meaning of the columns:

mcols(res, use.names=TRUE)
## DataFrame with 6 rows and 2 columns
##                        type                               description
##                 <character>                               <character>
## baseMean       intermediate mean of normalized counts for all samples
## log2FoldChange      results  log2 fold change (MAP): dex trt vs untrt
## lfcSE               results          standard error: dex trt vs untrt
## stat                results          Wald statistic: dex trt vs untrt
## pvalue              results       Wald test p-value: dex trt vs untrt
## padj                results                      BH adjusted p-values

The first column, baseMean, is a just the average of the normalized count values, dividing by size factors, taken over all samples. The remaining four columns refer to a specific contrast, namely the comparison of the trt level over the untrt level for the factor variable dex. See the help page for results() (by typing ?results) for information on how to obtain other contrasts.

The column log2FoldChange is the effect size estimate. It tells us how much the gene’s expression seems to have changed due to treatment with dexamethasone in comparison to untreated samples. This value is reported on a logarithmic scale to base 2: for example, a log2 fold change of 1.5 means that the gene’s expression is increased by a multiplicative factor of \(2^{1.5} \approx 2.82\).

Of course, this estimate has an uncertainty associated with it, which is available in the column lfcSE, the standard error estimate for the log2 fold change estimate. We can also express the uncertainty of a particular effect size estimate as the result of a statistical test. The purpose of a test for differential expression is to test whether the data provides sufficient evidence to conclude that this value is really different from zero. DESeq2 performs for each gene a hypothesis test to see whether evidence is sufficient to decide against the null hypothesis that there is no effect of the treatment on the gene and that the observed difference between treatment and control was merely caused by experimental variability (i.e., the type of variability that you can just as well expect between different samples in the same treatment group). As usual in statistics, the result of this test is reported as a p value, and it is found in the column pvalue. (Remember that a p value indicates the probability that a fold change as strong as the observed one, or even stronger, would be seen under the situation described by the null hypothesis.)

We can also summarize the results with the following line of code, which reports some additional information.

## out of 33469 with nonzero total read count
## adjusted p-value < 0.1
## LFC > 0 (up)     : 2641, 7.9% 
## LFC < 0 (down)   : 2242, 6.7% 
## outliers [1]     : 0, 0% 
## low counts [2]   : 15441, 46% 
## (mean count < 5)
## [1] see 'cooksCutoff' argument of ?results
## [2] see 'independentFiltering' argument of ?results

Note that there are many genes with differential expression due to dexamethasone treatment at the FDR level of 10%. This makes sense, as the smooth muscle cells of the airway are known to react to glucocorticoid steroids. However, there are two ways to be more strict about which set of genes are considered significant:

Sometimes a subset of the p values in res will be NA (“not available”). This is DESeq()’s way of reporting that all counts for this gene were zero, and hence not test was applied. In addition, p values can be assigned NA if the gene was excluded from analysis because it contained an extreme count outlier. For more information, see the outlier detection section of the vignette.

7.3 Other comparisons

In general, the results for a comparison of any two levels of a variable can be extracted using the contrast argument to results(). The user should specify three values: the name of the variable, the name of the level in the numerator, and the name of the level in the denominator. Here we extract results for the log2 of the fold change of one cell line over another:

results(dds, contrast=c("cell", "N061011", "N61311"))
## log2 fold change (MAP): cell N061011 vs N61311 
## Wald test p-value: cell N061011 vs N61311 
## DataFrame with 64102 rows and 6 columns
##                  baseMean log2FoldChange     lfcSE        stat     pvalue      padj
##                 <numeric>      <numeric> <numeric>   <numeric>  <numeric> <numeric>
## ENSG00000000003 708.60217     0.29055775 0.1360076  2.13633388 0.03265221 0.2115083
## ENSG00000000005   0.00000             NA        NA          NA         NA        NA
## ENSG00000000419 520.29790    -0.05069642 0.1491735 -0.33984871 0.73397047 0.9339283
## ENSG00000000457 237.16304     0.01474463 0.1816382  0.08117584 0.93530211 0.9885943
## ENSG00000000460  57.93263     0.20247610 0.2807312  0.72124547 0.47075850 0.8333258
## ...                   ...            ...       ...         ...        ...       ...
## LRG_94                  0             NA        NA          NA         NA        NA
## LRG_96                  0             NA        NA          NA         NA        NA
## LRG_97                  0             NA        NA          NA         NA        NA
## LRG_98                  0             NA        NA          NA         NA        NA
## LRG_99                  0             NA        NA          NA         NA        NA

If results for an interaction term are desired, the name argument of results() should be used. Please see the help for the results() function for more details.

7.4 Multiple testing

Novices in high-throughput biology often assume that thresholding these p values at a low value, say 0.05, as is often done in other settings, would be appropriate – but it is not. We briefly explain why:

There are 5722 genes with a p value below 0.05 among the 33469 genes, for which the test succeeded in reporting a p value:

sum(res$pvalue < 0.05, na.rm=TRUE)
## [1] 5722
## [1] 33469

Now, assume for a moment that the null hypothesis is true for all genes, i.e., no gene is affected by the treatment with dexamethasone. Then, by the definition of p value, we expect up to 5% of the genes to have a p value below 0.05. This amounts to 1673 genes. If we just considered the list of genes with a p value below 0.05 as differentially expressed, this list should therefore be expected to contain up to 1673 / 5722 = 29% false positives.

DESeq2 uses the Benjamini-Hochberg (BH) adjustment as described in the base R p.adjust function; in brief, this method calculates for each gene an adjusted p value which answers the following question: if one called significant all genes with a p value less than or equal to this gene’s p value threshold, what would be the fraction of false positives (the false discovery rate, FDR) among them (in the sense of the calculation outlined above)? These values, called the BH-adjusted p values, are given in the column padj of the res object.

Hence, if we consider a fraction of 10% false positives acceptable, we can consider all genes with an adjusted p value below \(10% = 0.1\) as significant. How many such genes are there?

sum(res$padj < 0.1, na.rm=TRUE)
## [1] 4883

We subset the results table to these genes and then sort it by the log2 fold change estimate to get the significant genes with the strongest down-regulation.

resSig <- subset(res, padj < 0.1)
head(resSig[ order( resSig$log2FoldChange ), ])
## log2 fold change (MAP): dex trt vs untrt 
## Wald test p-value: dex trt vs untrt 
## DataFrame with 6 rows and 6 columns
##                  baseMean log2FoldChange     lfcSE       stat       pvalue         padj
##                 <numeric>      <numeric> <numeric>  <numeric>    <numeric>    <numeric>
## ENSG00000162692 508.17023      -3.453076 0.1764098 -19.574173 2.567708e-85 3.560819e-82
## ENSG00000146006  46.80760      -2.858867 0.3369900  -8.483538 2.184477e-17 1.096985e-15
## ENSG00000105989 333.21469      -2.851447 0.1754994 -16.247620 2.322038e-59 1.231226e-56
## ENSG00000214814 243.27698      -2.760339 0.2225604 -12.402652 2.528167e-35 4.220166e-33
## ENSG00000267339  26.23357      -2.746830 0.3515707  -7.813022 5.583283e-15 2.231828e-13
## ENSG00000013293 244.49733      -2.646722 0.1981697 -13.355834 1.095240e-40 2.295928e-38

…and with the strongest upregulation. The order() function gives the indices in increasing order, so a simple way to ask for decreasing order is to add a - sign. Alternatively, you can use the argument decreasing=TRUE.

head(resSig[ order( -resSig$log2FoldChange ), ])
## log2 fold change (MAP): dex trt vs untrt 
## Wald test p-value: dex trt vs untrt 
## DataFrame with 6 rows and 6 columns
##                  baseMean log2FoldChange     lfcSE      stat        pvalue          padj
##                 <numeric>      <numeric> <numeric> <numeric>     <numeric>     <numeric>
## ENSG00000179593  67.24305       4.884730 0.3312025  14.74847  3.147214e-49  1.031600e-46
## ENSG00000109906 385.07103       4.865889 0.3324560  14.63619  1.650703e-48  5.130840e-46
## ENSG00000152583 997.43977       4.316100 0.1724127  25.03354 2.637881e-138 4.755573e-134
## ENSG00000250978  56.31819       4.093661 0.3291519  12.43699  1.645805e-35  2.799110e-33
## ENSG00000163884 561.10717       4.079128 0.2103817  19.38917  9.525995e-84  1.073342e-80
## ENSG00000168309 159.52692       3.992788 0.2549099  15.66353  2.685734e-55  1.241498e-52

8 Diagnostic plots

A quick way to visualize the counts for a particular gene is to use the plotCounts() function, which takes as arguments the DESeqDataSet, a gene name, and the group over which to plot the counts.

topGene <- rownames(res)[which.min(res$padj)]
data <- plotCounts(dds, gene=topGene, intgroup=c("dex"), returnData=TRUE)

We can also make more customizable plots using the ggplot() function from the ggplot2 package:

ggplot(data, aes(x=dex, y=count, fill=dex)) +
  scale_y_log10() + 
  geom_dotplot(binaxis="y", stackdir="center")
## stat_bindot: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

An “MA-plot” provides a useful overview for an experiment with a two-group comparison. The log2 fold change for a particular comparison is plotted on the y-axis and the average of the counts normalized by size factor is shown on the x-axis (“M” for minus, because a log ratio is equal to log minus log, and “A” for average).

plotMA(res, ylim=c(-5,5))

Each gene is represented with a dot. Genes with an adjusted \(p\) value below a threshold (here 0.1, the default) are shown in red. The DESeq2 package incorporates a prior on log2 fold changes, resulting in moderated log2 fold changes from genes with low counts and highly variable counts, as can be seen by the narrowing of spread of points on the left side of the plot. This plot demonstrates that only genes with a large average normalized count contain sufficient information to yield a significant call.

We can label individual points on the MA plot as well. Here we use the with() R function to plot a circle and text for a selected row of the results object. Within the with() function, only the baseMean and log2FoldChange values for the selected rows of res are used.

plotMA(res, ylim=c(-5,5))
with(res[topGene, ], {
  points(baseMean, log2FoldChange, col="dodgerblue", cex=2, lwd=2)
  text(baseMean, log2FoldChange, topGene, pos=2, col="dodgerblue")

Whether a gene is called significant depends not only on its LFC but also on its within-group variability, which DESeq2 quantifies as the dispersion. For strongly expressed genes, the dispersion can be understood as a squared coefficient of variation: a dispersion value of 0.01 means that the gene’s expression tends to differ by typically \(\sqrt{0.01} = 10\%\) between samples of the same treatment group. For weak genes, the Poisson noise is an additional source of noise.

The function plotDispEsts() visualizes DESeq2’s dispersion estimates:


The black points are the dispersion estimates for each gene as obtained by considering the information from each gene separately. Unless one has many samples, these values fluctuate strongly around their true values. Therefore, we fit the red trend line, which shows the dispersions’ dependence on the mean, and then shrink each gene’s estimate towards the red line to obtain the final estimates (blue points) that are then used in the hypothesis test. The blue circles above the main “cloud” of points are genes which have high gene-wise dispersion estimates which are labelled as dispersion outliers. These estimates are therefore not shrunk toward the fitted trend line.

Another useful diagnostic plot is the histogram of the p values.

hist(res$pvalue, breaks=20, col="grey50", border="white")

This plot becomes a bit smoother by excluding genes with very small counts:

hist(res$pvalue[res$baseMean > 1], breaks=20, col="grey50", border="white")

9 Gene clustering

In the sample distance heatmap made previously, the dendrogram at the side shows us a hierarchical clustering of the samples. Such a clustering can also be performed for the genes. Since the clustering is only relevant for genes that actually carry signal, one usually carries it out only for a subset of most highly variable genes. Here, for demonstration, let us select the 35 genes with the highest variance across samples. We will work with the rlog() transformed counts:

topVarGenes <- head(order(-rowVars(assay(rld))),35)

The heatmap becomes more interesting if we do not look at absolute expression strength but rather at the amount by which each gene deviates in a specific sample from the gene’s average across all samples. Hence, we center each genes’ values across samples, and plot a heatmap. We provide the column side colors to help identify the treated samples (in blue) from the untreated samples (in grey).

colors <- colorRampPalette( rev(brewer.pal(9, "PuOr")) )(255)
sidecols <- c("grey","dodgerblue")[ rld$dex ]
mat <- assay(rld)[ topVarGenes, ]
mat <- mat - rowMeans(mat)
colnames(mat) <- paste0(rld$dex,"-",rld$cell)
heatmap.2(mat, trace="none", col=colors, ColSideColors=sidecols,
          labRow=FALSE, mar=c(10,2), scale="row")

We can now see blocks of genes which covary across patients. Note that a set of genes at the top of the heatmap are separating the N061011 cell line from the others. At the bottom of the heatmap, we see a set of genes for which the treated samples have higher gene expression.

10 Independent filtering

The MA plot highlights an important property of RNA-Seq data. For weakly expressed genes, we have no chance of seeing differential expression, because the low read counts suffer from so high Poisson noise that any biological effect is drowned in the uncertainties from the read counting. We can also show this by examining the ratio of small p values (say, less than, 0.01) for genes binned by mean normalized count:

# create bins using the quantile function
qs <- c(0, quantile(res$baseMean[res$baseMean > 0], 0:7/7))
# cut the genes into the bins
bins <- cut(res$baseMean, qs)
# rename the levels of the bins using the middle point
levels(bins) <- paste0("~",round(.5*qs[-1] + .5*qs[-length(qs)]))
# calculate the ratio of $p$ values less than .01 for each bin
ratios <- tapply(res$pvalue, bins, function(p) mean(p < .01, na.rm=TRUE))
# plot these ratios
barplot(ratios, xlab="mean normalized count", ylab="ratio of small p values")

At first sight, there may seem to be little benefit in filtering out these genes. After all, the test found them to be non-significant anyway. However, these genes have an influence on the multiple testing adjustment, whose performance improves if such genes are removed. By removing the weakly-expressed genes from the input to the FDR procedure, we can find more genes to be significant among those which we keep, and so improved the power of our test. This approach is known as independent filtering.

The term independent highlights an important caveat. Such filtering is permissible only if the filter criterion is independent of the actual test statistic. Otherwise, the filtering would invalidate the test and consequently the assumptions of the BH procedure. This is why we filtered on the average over all samples: this filter is blind to the assignment of samples to the treatment and control group and hence independent. The independent filtering software used inside DESeq2 comes from the genefilter package, which contains a reference to a paper describing the statistical foundation for independent filtering.

11 Annotation: adding gene names

Our result table only uses Ensembl gene IDs, but gene names may be more informative. Bioconductor’s annotation packages help with mapping various ID schemes to each other.

We load the AnnotationDbi package and the annotation package


This is the organism annotation package (“org”) for Homo sapiens (“Hs”), organized as an AnnotationDbi database package (“db”), using Entrez Gene IDs (“eg”) as primary key. To get a list of all available key types, use:

##  [7] "ENZYME"       "EVIDENCE"     "EVIDENCEALL"  "GENENAME"     "GO"           "GOALL"       
## [13] "IPI"          "MAP"          "OMIM"         "ONTOLOGY"     "ONTOLOGYALL"  "PATH"        
## [19] "PFAM"         "PMID"         "PROSITE"      "REFSEQ"       "SYMBOL"       "UCSCKG"      
## [25] "UNIGENE"      "UNIPROT"
res$hgnc_symbol <- 
    unname(mapIds(, rownames(res), "SYMBOL", "ENSEMBL"))
res$entrezgene <- 
    unname(mapIds(, rownames(res), "ENTREZID", "ENSEMBL"))

Now the results have the desired external gene ids:

resOrdered <- res[order(res$pvalue),]
## log2 fold change (MAP): dex trt vs untrt 
## Wald test p-value: dex trt vs untrt 
## DataFrame with 6 rows and 8 columns
##                   baseMean log2FoldChange     lfcSE      stat        pvalue          padj
##                  <numeric>      <numeric> <numeric> <numeric>     <numeric>     <numeric>
## ENSG00000152583   997.4398       4.316100 0.1724127  25.03354 2.637881e-138 4.755573e-134
## ENSG00000165995   495.0929       3.188698 0.1277441  24.96160 1.597973e-137 1.440413e-133
## ENSG00000101347 12703.3871       3.618232 0.1499441  24.13054 1.195378e-128 6.620010e-125
## ENSG00000120129  3409.0294       2.871326 0.1190334  24.12201 1.468829e-128 6.620010e-125
## ENSG00000189221  2341.7673       3.230629 0.1373644  23.51868 2.627083e-122 9.472210e-119
## ENSG00000211445 12285.6151       3.552999 0.1589971  22.34631 1.311440e-110 3.940441e-107
##                 hgnc_symbol  entrezgene
##                 <character> <character>
## ENSG00000152583     SPARCL1        8404
## ENSG00000165995      CACNB2         783
## ENSG00000101347      SAMHD1       25939
## ENSG00000120129       DUSP1        1843
## ENSG00000189221        MAOA        4128
## ENSG00000211445        GPX3        2878

12 Exporting results

You can easily save the results table in a CSV file, which you can then load with a spreadsheet program such as Excel. The call to is necessary to convert the DataFrame object (IRanges package) to a data.frame object which can be processed by write.csv.

write.csv(, file="results.csv")

13 Session information

As last part of this document, we call the function sessionInfo, which reports the version numbers of R and all the packages used in this session. It is good practice to always keep such a record as it will help to trace down what has happened in case that an R script ceases to work because the functions have been changed in a newer version of a package. The session information should also always be included in any emails to the Bioconductor support site along with all code used in the analysis.

## R version 3.2.2 (2015-08-14)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Debian GNU/Linux stretch/sid
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
##  [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
## attached base packages:
## [1] stats4    parallel  stats     graphics  grDevices utils     datasets  methods   base     
## other attached packages:
##  [1] genefilter_1.51.1                       RColorBrewer_1.1-2                     
##  [3] gplots_2.17.0                           GenomicFiles_1.5.8                     
##  [5] BiocParallel_1.3.54                     Homo.sapiens_1.3.1                     
##  [7] GO.db_3.2.2                             OrganismDbi_1.11.43                    
##  [9] biomaRt_2.25.3                          AnnotationHub_2.1.45                   
## [11] VariantAnnotation_1.15.34               RNAseqData.HNRNPC.bam.chr14_0.7.0      
## [13] GenomicAlignments_1.5.18                Rsamtools_1.21.21                      
## [15] ALL_1.11.0                                         
## [17] RSQLite_1.0.0                           DBI_0.3.1                              
## [19] ggplot2_1.0.1                           airway_0.103.1                         
## [21] limma_3.25.18                           DESeq2_1.9.51                          
## [23] RcppArmadillo_0.               Rcpp_0.12.1                            
## [25] BSgenome.Hsapiens.UCSC.hg19_1.4.0       BSgenome_1.37.6                        
## [27] rtracklayer_1.29.28                     TxDb.Hsapiens.UCSC.hg19.knownGene_3.2.2
## [29] GenomicFeatures_1.21.33                 AnnotationDbi_1.31.19                  
## [31] SummarizedExperiment_0.3.11             Biobase_2.29.1                         
## [33] GenomicRanges_1.21.32                   GenomeInfoDb_1.5.16                    
## [35] microbenchmark_1.4-2                    Biostrings_2.37.8                      
## [37] XVector_0.9.4                           IRanges_2.3.26                         
## [39] S4Vectors_0.7.23                        BiocGenerics_0.15.11                   
## [41] BiocStyle_1.7.9                        
## loaded via a namespace (and not attached):
##  [1] bitops_1.0-6                 httr_1.0.0                   tools_3.2.2                 
##  [4] R6_2.1.1                     KernSmooth_2.23-15           rpart_4.1-10                
##  [7] Hmisc_3.17-0                 colorspace_1.2-6             nnet_7.3-11                 
## [10] gridExtra_2.0.0              graph_1.47.2                 formatR_1.2.1               
## [13] sandwich_2.3-4               labeling_0.3                 caTools_1.17.1              
## [16] scales_0.3.0                 mvtnorm_1.0-3                RBGL_1.45.1                 
## [19] stringr_1.0.0                digest_0.6.8                 foreign_0.8-66              
## [22] rmarkdown_0.8.1              htmltools_0.2.6              BiocInstaller_1.19.14       
## [25] shiny_0.12.2                 zoo_1.7-12                   gtools_3.5.0                
## [28] acepack_1.3-3.3              RCurl_1.95-4.7               magrittr_1.5                
## [31] Formula_1.2-1                futile.logger_1.4.1          munsell_0.4.2               
## [34] proto_0.3-10                 stringi_0.5-5                multcomp_1.4-1              
## [37] yaml_2.1.13                  MASS_7.3-44                  zlibbioc_1.15.0             
## [40] plyr_1.8.3                   grid_3.2.2                   gdata_2.17.0                
## [43] lattice_0.20-33              splines_3.2.2                annotate_1.47.4             
## [46] locfit_1.5-9.1               knitr_1.11                   geneplotter_1.47.0          
## [49] reshape2_1.4.1               codetools_0.2-14             futile.options_1.0.0        
## [52] XML_3.98-1.3                 evaluate_0.8                 latticeExtra_0.6-26         
## [55] lambda.r_1.1.7               httpuv_1.3.3                 gtable_0.1.2                
## [58] mime_0.4                     xtable_1.7-4                 survival_2.38-3             
## [61] cluster_2.0.3                TH.data_1.0-6                interactiveDisplayBase_1.7.3

14 Resources