Michael Love [1], Simon Anders [2], Wolfgang Huber [3]
[1] Dept. of Biostatistics, Dana-Farber Cancer Institute & Harvard T.H.Chan School of Public Health, Boston, US;
[2] Institute for Molecular Medicine Finland (FIMM), Helsinki, Finland;
[3] European Molecular Biology Laboratory (EMBL), Heidelberg, Germany.
This workflow is an abridged/modified version of the longer RNA-seq workflow available under Workflows on the Bioconductor website.
Here we will cover basic steps in RNA-seq analysis using a variety of Bioconductor packages:
This lab will walk you through an RNA-Seq differential expression workflow, using DESeq2 along with other Bioconductor packages. We note that a number of other Bioconductor packages can also be used for statistical inference of differential expression at the gene level including edgeR, BaySeq, DSS and limma.
Remember, if you have a question about a particular function, for example, DESeq()
, you can read about the arguments and details with:
?DESeq
All Bioconductor packages also come with descriptive vignettes. The DESeq2 vignette can be accessed with:
browseVignettes("DESeq2")
If you have questions about this workflow, please post them to the Bioconductor support site. If the questions concern a specific package, you can tag the post with the name of the package, or for general questions about the workflow, tag the post with deseq2
. Note the posting guide for crafting an optimal question for the support site.
The data used in this workflow consists of RNA-seq samples of human 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 this experiment, we have two samples each from four cell lines: one sample was untreated, while another sample was treated with 1 micromolar dexamethasone for 18 hours. The publication 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.
Our aim in this workflow is to identify differences in gene expression due to treatment, compared to control samples, while controlling for the differences across cell line.
As input, the 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 (or fragments in a paired-end experiment) can be uniquely assigned to gene i in sample j. 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.
For more details on why we work with counts, or the statistical model underlying the methods, see the DESeq2 paper.
We will assume we have already aligned the reads to the genome and have BAM files for each of the samples. In order to see the code which was used for this experiment to align the paired-end reads to the genome, see the full workflow or the vignette for the airway data package.
First we will need to define the genes in which we will count the RNA-seq fragments. Our initial goal is to construct a TxDb object, which contains all the information about the exons, transcripts and genes of an organism. From this we can pull out the exons associated with each gene, which will be used for counting.
We have different options for how we read in the gene model information. One option is to read the gene model from a GTF file, using makeTxDbFromGFF from the GenomicFeatures package. GTF files can be downloaded from Ensembl’s FTP site or other gene model repositories.
We first need to load our data package, which contains the GTF file (and small example BAM files and count matrix).
library("airway")
The R function system.file can be used to find out where on your computer the files from a package have been installed. Here we ask for the full path to the extdata
directory, which is part of the airway package.
Note that this GTF file only contains a subset of all the genes from the 75th Ensembl release.
dir <- system.file("extdata", package="airway", mustWork=TRUE)
gtffile <- file.path(dir, "Homo_sapiens.GRCh37.75_subset.gtf")
Now we can create the TxDb.
library("GenomicFeatures")
txdb <- makeTxDbFromGFF(gtffile, format="gtf")
## Warning in matchCircularity(seqlevels(gr), circ_seqs): None of the strings
## in your circ_seqs argument match your seqnames.
Now we can produce a GRangesList of all the exons grouped by gene. Each element of the list defines all the exons associated with a given gene.
ebg <- exonsBy(txdb, by="gene")
ebg[[1]]
## GRanges object with 18 ranges and 2 metadata columns:
## seqnames ranges strand | exon_id exon_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## [1] 1 [11086580, 11087705] - | 98 ENSE00000818830
## [2] 1 [11090233, 11090307] - | 99 ENSE00000472123
## [3] 1 [11090805, 11090939] - | 100 ENSE00000743084
## [4] 1 [11094885, 11094963] - | 101 ENSE00000743085
## [5] 1 [11097750, 11097868] - | 103 ENSE00003520086
## ... ... ... ... ... ... ...
## [14] 1 [11106948, 11107176] - | 111 ENSE00003467404
## [15] 1 [11106948, 11107176] - | 112 ENSE00003489217
## [16] 1 [11107260, 11107280] - | 113 ENSE00001833377
## [17] 1 [11107260, 11107284] - | 114 ENSE00001472289
## [18] 1 [11107260, 11107290] - | 115 ENSE00001881401
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
An even faster option for creating a TxDb, compared to reading in GTF files, is to pull down the pre-processed GTF file using AnnotationHub. Below we show an example of how you could construct a TxDb for Saccharomyces cerevisiae from the Ensembl resource. Browse the AnnotationHub vignettes for more details on usage.
library("AnnotationHub")
ah <- AnnotationHub()
qu <- query(ah, c("Ensembl","gtf","Saccharomyces cerevisiae","release-80"))
stopifnot(length(qu) == 1) # check there was only one match
gtf <- qu[[1]] # this actually downloads the gene model
## using guess work to populate seqinfo
txdbFromAHub <- makeTxDbFromGRanges(gtf)
There are still other options for obtaining a TxDb. For the known genes track from the UCSC Genome Browser, one can use the pre-built Bioconductor package: TxDb.Hsapiens.UCSC.hg19.knownGene. The makeTxDbFromBiomart and makeTxDbFromUCSC functions can be used to automatically pull gene models from Biomart and UCSC, respectively. See the GenomicFeatures package for more details.
We will now show how to count reads/fragments which can be uniquely assigned to the exons of a gene, across all the genes and all the samples. Besides the main count matrix, which we will use later, the airway package also contains a small subset of reads from eight BAM files (the reads which overlap the genes in our subset GTF file).
We will use these BAM files to demonstrate how a count matrix can be constructed. Afterwards, we will load the full count matrix corresponding to all samples and all data, which is already provided in the same package, and will continue the analysis with that full table.
In our directory specified earlier, we find the eight BAM files (and some other files):
list.files(dir)
## [1] "GSE52778_series_matrix.txt"
## [2] "Homo_sapiens.GRCh37.75_subset.gtf"
## [3] "SRR1039508_subset.bam"
## [4] "SRR1039508_subset.bam.bai"
## [5] "SRR1039509_subset.bam"
## [6] "SRR1039512_subset.bam"
## [7] "SRR1039513_subset.bam"
## [8] "SRR1039516_subset.bam"
## [9] "SRR1039517_subset.bam"
## [10] "SRR1039520_subset.bam"
## [11] "SRR1039521_subset.bam"
## [12] "SraRunInfo_SRP033351.csv"
## [13] "sample_table.csv"
Typically, we have a sample table with experimental metadata for our samples (which samples were in treatment vs control, which were in batch 1 vs batch 2, etc.). For your own project, you might create such a comma-separated value (CSV) file using a text editor or spreadsheet software such as Excel.= We can load such a file with the function read.csv.
(The parentheses around the last line are used to print the result in addition to storing it to the sampleTable
object.)
csvfile <- file.path(dir,"sample_table.csv")
( sampleTable <- read.csv(csvfile,row.names=1) )
## SampleName cell dex albut Run avgLength Experiment
## SRR1039508 GSM1275862 N61311 untrt untrt SRR1039508 126 SRX384345
## SRR1039509 GSM1275863 N61311 trt untrt SRR1039509 126 SRX384346
## SRR1039512 GSM1275866 N052611 untrt untrt SRR1039512 126 SRX384349
## SRR1039513 GSM1275867 N052611 trt untrt SRR1039513 87 SRX384350
## SRR1039516 GSM1275870 N080611 untrt untrt SRR1039516 120 SRX384353
## SRR1039517 GSM1275871 N080611 trt untrt SRR1039517 126 SRX384354
## SRR1039520 GSM1275874 N061011 untrt untrt SRR1039520 101 SRX384357
## SRR1039521 GSM1275875 N061011 trt untrt SRR1039521 98 SRX384358
## Sample BioSample
## SRR1039508 SRS508568 SAMN02422669
## SRR1039509 SRS508567 SAMN02422675
## SRR1039512 SRS508571 SAMN02422678
## SRR1039513 SRS508572 SAMN02422670
## SRR1039516 SRS508575 SAMN02422682
## SRR1039517 SRS508576 SAMN02422673
## SRR1039520 SRS508579 SAMN02422683
## SRR1039521 SRS508580 SAMN02422677
Using the Run
column in the sample table, we construct the full paths to the files we want to perform the counting operation on:
filenames <- file.path(dir, paste0(sampleTable$Run, "_subset.bam"))
We indicate in Bioconductor that these files are BAM files using the BamFileList function. Here we can optionally also specify details about how the BAM files should be treated, e.g., only process 2 million reads at a time.
library("Rsamtools")
bamfiles <- BamFileList(filenames, yieldSize=2000000)
Note: make sure that the chromosome names of the genomic features in the annotation you use are consistent with the chromosome names of the reference used for read alignment. Otherwise, the scripts might fail to count any reads to features due to the mismatching names. We can check the chromosome names in the alignment files like so:
seqinfo(bamfiles[1])
## Seqinfo object with 84 sequences from an unspecified genome:
## seqnames seqlengths isCircular genome
## 1 249250621 <NA> <NA>
## 10 135534747 <NA> <NA>
## 11 135006516 <NA> <NA>
## 12 133851895 <NA> <NA>
## 13 115169878 <NA> <NA>
## ... ... ... ...
## GL000210.1 27682 <NA> <NA>
## GL000231.1 27386 <NA> <NA>
## GL000229.1 19913 <NA> <NA>
## GL000226.1 15008 <NA> <NA>
## GL000207.1 4262 <NA> <NA>
After these preparations, the actual counting is easy. The function summarizeOverlaps from the GenomicAlignments package will do this. This produces a SummarizedExperiment object, which contains a variety of information about an experiment, and will be described in more detail below.
Note: If it is desired to perform counting using multiple cores, one can use the register and MulticoreParam functions from the BiocParallel package before the counting call below.
library("GenomicAlignments")
The result of the counting call will be a SummarizedExperiment object, explained in a section below.
se <- summarizeOverlaps(features=ebg, reads=bamfiles,
mode="Union",
singleEnd=FALSE,
ignore.strand=TRUE,
fragments=TRUE )
We specify a number of arguments besides the features
and the reads
. The mode
argument describes what kind of read overlaps will be counted as a hit. These modes are shown in Figure 1 of the “Counting reads with summarizeOverlaps” vignette for the GenomicAlignments package. Setting singleEnd
to FALSE
indicates that the experiment produced paired-end reads, and we want to count a pair of reads only once toward the read count for a gene.
In order to produce correct counts, it is important to know if the RNA-Seq experiment was strand-specific or not. This experiment was not strand-specific so we set ignore.strand
to TRUE
. The fragments
argument can be used when singleEnd=FALSE
to specify if unpaired reads should be counted (yes if fragments=TRUE
).
Here we show the component parts of a SummarizedExperiment object, and also its subclasses, such as the DESeqDataSet which is explained in the next section. The assay(s)
(pink block) contains the matrix (or matrices) of summarized values, the rowRanges
(blue block) contains information about the genomic ranges, and the colData
(green block) contains information about the samples or experiments. The highlighted line in each block represents the first row (note that the first row of colData
lines up with the first column of the assay
.
The example code above actually only counts a small subset of reads from the original experiment. Nevertheless, we can still investigate the resulting SummarizedExperiment by looking at the counts in the assay
slot, the phenotypic data about the samples in colData
slot (in this case an empty DataFrame), and the data about the genes in the rowRanges
slot.
se
## class: RangedSummarizedExperiment
## dim: 20 8
## metadata(0):
## assays(1): counts
## rownames(20): ENSG00000009724 ENSG00000116649 ... ENSG00000271794
## ENSG00000271895
## rowRanges metadata column names(0):
## colnames(8): SRR1039508_subset.bam SRR1039509_subset.bam ...
## SRR1039520_subset.bam SRR1039521_subset.bam
## colData names(0):
head(assay(se))
## SRR1039508_subset.bam SRR1039509_subset.bam
## ENSG00000009724 38 28
## ENSG00000116649 1004 1255
## ENSG00000120942 218 256
## ENSG00000120948 2751 2080
## ENSG00000171819 4 50
## ENSG00000171824 869 1075
## SRR1039512_subset.bam SRR1039513_subset.bam
## ENSG00000009724 66 24
## ENSG00000116649 1122 1313
## ENSG00000120942 233 252
## ENSG00000120948 3353 1614
## ENSG00000171819 19 543
## ENSG00000171824 1115 1051
## SRR1039516_subset.bam SRR1039517_subset.bam
## ENSG00000009724 42 41
## ENSG00000116649 1100 1879
## ENSG00000120942 269 465
## ENSG00000120948 3519 3716
## ENSG00000171819 1 10
## ENSG00000171824 944 1405
## SRR1039520_subset.bam SRR1039521_subset.bam
## ENSG00000009724 47 36
## ENSG00000116649 745 1536
## ENSG00000120942 207 400
## ENSG00000120948 2220 1990
## ENSG00000171819 14 1067
## ENSG00000171824 748 1590
colSums(assay(se))
## SRR1039508_subset.bam SRR1039509_subset.bam SRR1039512_subset.bam
## 6478 6501 7699
## SRR1039513_subset.bam SRR1039516_subset.bam SRR1039517_subset.bam
## 6801 8009 10849
## SRR1039520_subset.bam SRR1039521_subset.bam
## 5254 9168
colData(se)
## DataFrame with 8 rows and 0 columns
rowRanges(se)
## GRangesList object of length 20:
## $ENSG00000009724
## GRanges object with 18 ranges and 2 metadata columns:
## seqnames ranges strand | exon_id exon_name
## <Rle> <IRanges> <Rle> | <integer> <character>
## [1] 1 [11086580, 11087705] - | 98 ENSE00000818830
## [2] 1 [11090233, 11090307] - | 99 ENSE00000472123
## [3] 1 [11090805, 11090939] - | 100 ENSE00000743084
## [4] 1 [11094885, 11094963] - | 101 ENSE00000743085
## [5] 1 [11097750, 11097868] - | 103 ENSE00003520086
## ... ... ... ... ... ... ...
## [14] 1 [11106948, 11107176] - | 111 ENSE00003467404
## [15] 1 [11106948, 11107176] - | 112 ENSE00003489217
## [16] 1 [11107260, 11107280] - | 113 ENSE00001833377
## [17] 1 [11107260, 11107284] - | 114 ENSE00001472289
## [18] 1 [11107260, 11107290] - | 115 ENSE00001881401
##
## ...
## <19 more elements>
## -------
## seqinfo: 1 sequence from an unspecified genome; no seqlengths
Note that the rowRanges
slot is a GRangesList, which contains all the information about the exons for each gene, i.e., for each row of the count matrix. It also contains metadata about the construction of the gene model in the metadata
slot.
str(metadata(rowRanges(se)))
## List of 1
## $ genomeInfo:List of 15
## ..$ Db type : chr "TxDb"
## ..$ Supporting package : chr "GenomicFeatures"
## ..$ Data source : chr "/home/ubuntu/R-libs/airway/extdata/Homo_sapiens.GRCh37.75_subset.gtf"
## ..$ Organism : chr NA
## ..$ Taxonomy ID : chr NA
## ..$ miRBase build ID : chr NA
## ..$ Genome : chr NA
## ..$ transcript_nrow : chr "65"
## ..$ exon_nrow : chr "279"
## ..$ cds_nrow : chr "158"
## ..$ Db created by : chr "GenomicFeatures package from Bioconductor"
## ..$ Creation time : chr "2015-07-17 17:20:54 +0000 (Fri, 17 Jul 2015)"
## ..$ GenomicFeatures version at creation time: chr "1.21.13"
## ..$ RSQLite version at creation time : chr "1.0.0"
## ..$ DBSCHEMAVERSION : chr "1.1"
The colData
slot, so far empty, should contain all the metadata. We hence assign our sample table to it:
colData(se) <- DataFrame(sampleTable)
We could have also counted the fragments which align to each gene using the featureCounts function from the Rsubread package. Note: this package only exists for Linux and Mac machines, not for Windows machines. Here we show an example of counting, using only the first file from the vector of 8 files. This function produces a list with the count matrix called counts.
library("Rsubread")
fc <- featureCounts(filenames[1], annot.ext=gtffile,
isGTFAnnotationFile=TRUE, isPairedEnd=TRUE)
##
## ========== _____ _ _ ____ _____ ______ _____
## ===== / ____| | | | _ \| __ \| ____| /\ | __ \
## ===== | (___ | | | | |_) | |__) | |__ / \ | | | |
## ==== \___ \| | | | _ <| _ /| __| / /\ \ | | | |
## ==== ____) | |__| | |_) | | \ \| |____ / ____ \| |__| |
## ========== |_____/ \____/|____/|_| \_\______/_/ \_\_____/
## Rsubread 1.19.2
##
## //========================== featureCounts setting ===========================\\
## || ||
## || Input files : 1 BAM file ||
## || P /home/ubuntu/R-libs/airway/extdata/SRR1039 ... ||
## || ||
## || Output file : ./.Rsubread_featureCounts_pid13752 ||
## || Annotations : /home/ubuntu/R-libs/airway/extdata/Homo_sapi ... ||
## || ||
## || Threads : 1 ||
## || Level : meta-feature level ||
## || Paired-end : yes ||
## || Strand specific : no ||
## || Multimapping reads : not counted ||
## || Multi-overlapping reads : not counted ||
## || ||
## || Chimeric reads : counted ||
## || Both ends mapped : not required ||
## || ||
## \\===================== http://subread.sourceforge.net/ ======================//
##
## //================================= Running ==================================\\
## || ||
## || Load annotation file /home/ubuntu/R-libs/airway/extdata/Homo_sapiens.G ... ||
## || Features : 406 ||
## || Meta-features : 20 ||
## || Chromosomes/contigs : 1 ||
## || ||
## || Process BAM file /home/ubuntu/R-libs/airway/extdata/SRR1039508_subset. ... ||
## || Paired-end reads are included. ||
## || Assign fragments (read pairs) to features... ||
## || Found reads that are not properly paired. ||
## || (missing mate or the mate is not the next read) ||
## || Below are the two reads that are not properly paired: ||
## || SRR1039508.16177033 99 1 11072708 255 63M = 11072763 ||
## || 1090 N # NH:i:1 HI:i:1 ||
## || SRR1039508.34326 163 1 11072719 255 63M = 11072745 ||
## || 1061 N # NH:i:1 HI:i:1 ||
## || 2 reads have missing mates. ||
## || Input was converted to a format accepted by featureCounts. ||
## || Total fragments : 7142 ||
## || Successfully assigned fragments : 6649 (93.1%) ||
## || Running time : 0.11 minutes ||
## || ||
## || Read assignment finished. ||
## || ||
## \\===================== http://subread.sourceforge.net/ ======================//
names(fc)
## [1] "counts" "annotation" "targets" "stat"
head(fc$counts)
## X.home.ubuntu.R.libs.airway.extdata.SRR1039508_subset.bam
## ENSG00000175262 0
## ENSG00000215785 0
## ENSG00000264181 0
## ENSG00000207213 0
## ENSG00000120948 2673
## ENSG00000009724 38
At this point, we have counted the reads which overlap the genes in the gene model we specified. This is a branching point where we could use a variety of Bioconductor packages for exploration and differential expression of the counts, including edgeR, BaySeq, DSS and limma. We will continue, using DESeq2. The SummarizedExperiment object (or count matrix and sample table) is all we need to start our analysis. In the following section we will show how to use it to create the data object used by DESeq2.
Bioconductor software packages often define and use a custom class for their data object, which makes sure that all the needed data slots are consistently provided and fulfill the requirements. In addition, Bioconductor has general data classes (such as the SummarizedExperiment) that can be used to move data between packages. In DESeq2, the custom class is called DESeqDataSet. It is built on top of the SummarizedExperiment class (in technical terms, DESeqDataSet is a subclass), and it is easy to convert SummarizedExperiment instances into DESeqDataSet and vice versa. One of the main differences is that the assay
slot is instead accessed using the count accessor, and the class enforces that the values in this matrix are non-negative integers.
A second difference is that the DESeqDataSet has an associated design formula. The experimental design is specified at the beginning of the analysis, as it will inform many of the DESeq2 functions how to treat the samples in the analysis (one exception is the size factor estimation, i.e., the adjustment for differing library sizes, which does not depend on the design formula). The design formula tells which variables in the column metadata (colData
) specify the experimental design and how these factors should be used in the analysis. The column metadata is where the information about the samples (columns of the count matrix) are stored.
The simplest design formula for differential expression would be ~ condition
, where condition
is a column in colData(dds)
which specifies which of two (or more groups) the samples belong to. For the airway experiment, we will specify ~ cell + dex
, which means that we want to test for the effect of dexamethasone (the last factor), controlling for the effect of different cell line (the first factor).
You can use R’s formula notation to express any experimental design that can be described within an ANOVA-like framework. Note that DESeq2 uses the same formula notation as, for instance, the linear model (lm) function of base R. If the question of interest is whether a fold change due to treatment is different across groups, interaction terms can be included using models such as ~ group + treatment + group:treatment
. See the manual page for ?results
for examples of extracting contrasts from more complex designs such as these.
In the following sections, we will demonstrate the construction of the DESeqDataSet from two starting points:
For a full example of using the HTSeq Python package for read counting, please see the pasilla vignette. For an example of generating the DESeqDataSet from files produced by htseq-count, please see the DESeq2 vignette.
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 browseVignettes("airway")
into your R session.
data("airway")
se <- airway
We can quickly check the millions of fragments which uniquely aligned to the genes (the second argument of round tells how many decimal points to keep).
round( colSums(assay(se)) / 1e6, 1 )
## SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516 SRR1039517
## 20.6 18.8 25.3 15.2 24.4 30.8
## SRR1039520 SRR1039521
## 19.1 21.2
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 information on the count matrix’s columns stored in the colData
slot:
colData(se)
## DataFrame with 8 rows and 9 columns
## SampleName cell dex albut Run avgLength
## <factor> <factor> <factor> <factor> <factor> <integer>
## SRR1039508 GSM1275862 N61311 untrt untrt SRR1039508 126
## SRR1039509 GSM1275863 N61311 trt untrt SRR1039509 126
## SRR1039512 GSM1275866 N052611 untrt untrt SRR1039512 126
## SRR1039513 GSM1275867 N052611 trt untrt SRR1039513 87
## SRR1039516 GSM1275870 N080611 untrt untrt SRR1039516 120
## SRR1039517 GSM1275871 N080611 trt untrt SRR1039517 126
## SRR1039520 GSM1275874 N061011 untrt untrt SRR1039520 101
## SRR1039521 GSM1275875 N061011 trt untrt SRR1039521 98
## Experiment Sample BioSample
## <factor> <factor> <factor>
## SRR1039508 SRX384345 SRS508568 SAMN02422669
## SRR1039509 SRX384346 SRS508567 SAMN02422675
## SRR1039512 SRX384349 SRS508571 SAMN02422678
## SRR1039513 SRX384350 SRS508572 SAMN02422670
## SRR1039516 SRX384353 SRS508575 SAMN02422682
## SRR1039517 SRX384354 SRS508576 SAMN02422673
## SRR1039520 SRX384357 SRS508579 SAMN02422683
## SRR1039521 SRX384358 SRS508580 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.
Once we have our fully annotated SummarizedExperiment object, we can construct a DESeqDataSet object from it, which will then form the starting point of the actual DESeq2 package, described in the following sections. We add an appropriate design for the analysis.
library("DESeq2")
dds <- DESeqDataSet(se, design = ~ cell + dex)
If we only wanted to perform transformations and exploratory data analysis we could use a ~ 1
for the design, but be careful, because a true experimental design, e.g. ~ condition
would need to be added later before differential expression (or else we would only be testing the intercept).
Note that there are two alternative functions, DESeqDataSetFromMatrix and DESeqDataSetFromHTSeq, which allow you to get started in case you have your data not in the form of a SummarizedExperiment object, but either as a simple matrix of count values or as output files from the htseq-count script from the HTSeq Python package.
Below we demonstrate using DESeqDataSetFromMatrix.
In this section, we will show how to build an DESeqDataSet supposing we only have a count matrix and a table of sample information.
Note: if you have prepared a SummarizedExperiment you should skip this section. While the previous section would be used to contruct a DESeqDataSet from a SummarizedExperiment, here we first extract the individual object (count matrix and sample info) from the SummarizedExperiment in order to build it back up into a new object – only for demonstration purposes. In practice, the count matrix would either be read in from a file or perhaps generated by an R function like featureCounts from the Rsubread package.
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.)
countdata <- assay(se)
head(countdata)
## 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
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 information 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.
coldata <- colData(se)
We now have all the ingredients to prepare our data object in a form that is suitable for analysis, namely:
countdata
: a table with the read countscoldata
: a table with information about the samplesTo now construct the data object from the matrix of counts and the sample information table, we use:
(ddsMat <- DESeqDataSetFromMatrix(countData = countdata,
colData = coldata,
design = ~ cell + dex))
## class: DESeqDataSet
## dim: 64102 8
## metadata(0):
## assays(1): counts
## rownames(64102): ENSG00000000003 ENSG00000000005 ... LRG_98 LRG_99
## rowRanges metadata column names(0):
## colnames(8): SRR1039508 SRR1039509 ... SRR1039520 SRR1039521
## colData names(9): SampleName cell ... Sample BioSample
We will continue with the object generated from the SummarizedExperiment section.
There are two separate paths in this workflow, the one we will see first involves transformations of the counts in order to visualizally explore sample relationships. In the second part, we will go back to the orignal raw counts for statistical testing. This is critical because the statistical testing methods rely on original data (not scaled or transformed) for calculating the precision of measurements.
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, 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)
head(assay(rld))
## SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516
## ENSG00000000003 9.399151 9.142478 9.501695 9.320796 9.757212
## ENSG00000000005 0.000000 0.000000 0.000000 0.000000 0.000000
## ENSG00000000419 8.901283 9.113976 9.032567 9.063925 8.981930
## ENSG00000000457 7.949897 7.882371 7.834273 7.916459 7.773819
## ENSG00000000460 5.849521 5.882363 5.486937 5.770334 5.940407
## ENSG00000000938 -1.638084 -1.637483 -1.558248 -1.636072 -1.597606
## SRR1039517 SRR1039520 SRR1039521
## ENSG00000000003 9.512183 9.617378 9.315309
## ENSG00000000005 0.000000 0.000000 0.000000
## ENSG00000000419 9.108531 8.894830 9.052303
## ENSG00000000457 7.886645 7.946411 7.908338
## ENSG00000000460 5.663847 6.107733 5.907824
## ENSG00000000938 -1.639362 -1.637608 -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).
par( mfrow = c( 1, 2 ) )
dds <- estimateSizeFactors(dds)
plot(log2( 1 + counts(dds, normalized=TRUE)[ , 1:2] ),
pch=16, cex=0.3)
plot(assay(rld)[ , 1: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.
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) ) )
sampleDists
## SRR1039508 SRR1039509 SRR1039512 SRR1039513 SRR1039516
## 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
## SRR1039521 56.04254 41.46514 51.90045 34.82975 58.40428
## SRR1039517 SRR1039520
## SRR1039509
## SRR1039512
## SRR1039513
## SRR1039516
## SRR1039517
## SRR1039520 57.13688
## SRR1039521 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 pheatmap from the pheatmap package.
library("pheatmap")
library("RColorBrewer")
In order to plot the sample distance matrix with the rows/columns arranged by those distances in the matrix, we manually provide the sampleDists
to the clustering_distance
argument of the pheatmap function. Otherwise the pheatmap function would assume that the matrix contains the data values themselves, and would calculate distances between the rows/columns of the distance matrix, which is not desired.
sampleDistMatrix <- as.matrix( sampleDists )
rownames(sampleDistMatrix) <- paste( rld$dex, rld$cell, sep="-" )
colnames(sampleDistMatrix) <- NULL
colors <- colorRampPalette( rev(brewer.pal(9, "Blues")) )(255)
pheatmap(sampleDistMatrix,
clustering_distance_rows=sampleDists,
clustering_distance_cols=sampleDists,
col=colors)
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.
Another option for calculating sample distances is to use the Poisson Distance, implemented in the CRAN package PoiClaClu. Similar to the transformations offered in DESeq2, this measure of dissimilarity also takes the variance structure of counts into consideration when calculating the distances between samples. The PoissonDistance function takes the original count matrix (not normalized) with samples as rows instead of columns, so we need to transpose the counts in dds
.
library("PoiClaClu")
poisd <- PoissonDistance(t(counts(dds)))
We can plot the heatmap as before:
samplePoisDistMatrix <- as.matrix( poisd$dd )
rownames(samplePoisDistMatrix) <- paste( rld$dex, rld$cell, sep="-" )
colnames(samplePoisDistMatrix) <- NULL
pheatmap(samplePoisDistMatrix,
clustering_distance_rows=poisd$dd,
clustering_distance_cols=poisd$dd,
col=colors)
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 labeling 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 on using ggplot.
(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.
library("ggplot2")
ggplot(data, aes(PC1, PC2, color=dex, shape=cell)) + geom_point(size=3) +
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.
Another plot, very similar to the PCA plot, can be made using the multidimensional scaling (MDS) function in base R. This is useful when we don’t have the original data, but only a matrix of distances. Here we have the MDS plot for the distances calculated from the rlog transformed counts:
mds <- data.frame(cmdscale(sampleDistMatrix))
mds <- cbind(mds, as.data.frame(colData(rld)))
ggplot(mds, aes(X1,X2,color=dex,shape=cell)) + geom_point(size=3)
And here from the PoissonDistance:
mds <- data.frame(cmdscale(samplePoisDistMatrix))
mds <- cbind(mds, as.data.frame(colData(dds)))
ggplot(mds, aes(X1,X2,color=dex,shape=cell)) + geom_point(size=3)
Now, suppose we want to do statistical testing on this dataset to investigate which, if any, genes show evidence of differential expression due to treatment, and controlling for cell line.
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")
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 for DESeq, which can be accessed by typing ?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.
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
## <numeric> <numeric> <numeric> <numeric>
## ENSG00000000003 708.60217 -0.37424998 0.09873107 -3.7906000
## ENSG00000000005 0.00000 NA NA NA
## ENSG00000000419 520.29790 0.20215551 0.10929899 1.8495642
## ENSG00000000457 237.16304 0.03624826 0.13684258 0.2648902
## ENSG00000000460 57.93263 -0.08523371 0.24654402 -0.3457140
## ... ... ... ... ...
## LRG_94 0 NA NA NA
## LRG_96 0 NA NA NA
## LRG_97 0 NA NA NA
## LRG_98 0 NA NA NA
## LRG_99 0 NA NA NA
## pvalue padj
## <numeric> <numeric>
## ENSG00000000003 0.0001502838 0.001251416
## ENSG00000000005 NA NA
## ENSG00000000419 0.0643763851 0.192284345
## ENSG00000000457 0.7910940556 0.910776144
## ENSG00000000460 0.7295576905 0.878646940
## ... ... ...
## LRG_94 NA NA
## LRG_96 NA NA
## LRG_97 NA NA
## LRG_98 NA NA
## LRG_99 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 or approximately 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, which will be covered in later sections.
summary(res)
##
## 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:
padj
in the results table)lfcThreshold
argument of results. See the DESeq2 vignette for a demonstration of the use of this argument.If we lower the false discovery rate threshold, we should also tell this value to results()
, so that the function will use an alternative threshold for the optimal independent filtering step:
res.05 <- results(dds, alpha=.05)
table(res.05$padj < .05)
##
## FALSE TRUE
## 12736 4057
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.
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:
( res.cell <- 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
## <numeric> <numeric> <numeric> <numeric> <numeric>
## ENSG00000000003 708.60217 0.29055775 0.1360076 2.13633388 0.03265221
## ENSG00000000005 0.00000 NA NA NA NA
## ENSG00000000419 520.29790 -0.05069642 0.1491735 -0.33984871 0.73397047
## ENSG00000000457 237.16304 0.01474463 0.1816382 0.08117584 0.93530211
## ENSG00000000460 57.93263 0.20247610 0.2807312 0.72124547 0.47075850
## ... ... ... ... ... ...
## LRG_94 0 NA NA NA NA
## LRG_96 0 NA NA NA NA
## LRG_97 0 NA NA NA NA
## LRG_98 0 NA NA NA NA
## LRG_99 0 NA NA NA NA
## padj
## <numeric>
## ENSG00000000003 0.2115083
## ENSG00000000005 NA
## ENSG00000000419 0.9339283
## ENSG00000000457 0.9885943
## ENSG00000000460 0.8333258
## ... ...
## LRG_94 NA
## LRG_96 NA
## LRG_97 NA
## LRG_98 NA
## LRG_99 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.
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)]
plotCounts(dds, gene=topGene, intgroup=c("dex"))
We can also make more customizable plots using the ggplot function from the ggplot2 package:
data <- plotCounts(dds, gene=topGene, intgroup=c("dex","cell"), returnData=TRUE)
ggplot(data, aes(x=dex, y=count, color=cell)) +
scale_y_log10() +
geom_point(position=position_jitter(width=.1,height=0), size=3)
Here we use a more structural arrangement instead of random jitter, and color by the treatment.
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.
Note that the DESeq test actually takes into account the cell line effect, so a more detailed plot would also show the cell lines.
ggplot(data, aes(x=dex, y=count, color=cell, group=cell)) +
scale_y_log10() + geom_point(size=3) + geom_line()
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")
})
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:
library("genefilter")
topVarGenes <- head(order(-rowVars(assay(rld))),20)
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).
mat <- assay(rld)[ topVarGenes, ]
mat <- mat - rowMeans(mat)
df <- as.data.frame(colData(rld)[,c("cell","dex")])
pheatmap(mat, annotation_col=df)
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.
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 org.Hs.eg.db:
library("AnnotationDbi")
library("org.Hs.eg.db")
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:
columns(org.Hs.eg.db)
## [1] "ACCNUM" "ALIAS" "ENSEMBL" "ENSEMBLPROT"
## [5] "ENSEMBLTRANS" "ENTREZID" "ENZYME" "EVIDENCE"
## [9] "EVIDENCEALL" "GENENAME" "GO" "GOALL"
## [13] "IPI" "MAP" "OMIM" "ONTOLOGY"
## [17] "ONTOLOGYALL" "PATH" "PFAM" "PMID"
## [21] "PROSITE" "REFSEQ" "SYMBOL" "UCSCKG"
## [25] "UNIGENE" "UNIPROT"
We can use the mapIds function to add invidual columns to our results table. To add the gene symbol and Entrez ID, we call mapIds twice:
res$symbol <- mapIds(org.Hs.eg.db,
keys=row.names(res),
column="SYMBOL",
keytype="ENSEMBL",
multiVals="first")
res$entrez <- mapIds(org.Hs.eg.db,
keys=row.names(res),
column="ENTREZID",
keytype="ENSEMBL",
multiVals="first")
Now the results have the desired external gene ids:
resOrdered <- res[order(res$padj),]
head(resOrdered)
## 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
## <numeric> <numeric> <numeric> <numeric>
## ENSG00000152583 997.4398 4.316100 0.1724127 25.03354
## ENSG00000165995 495.0929 3.188698 0.1277441 24.96160
## ENSG00000101347 12703.3871 3.618232 0.1499441 24.13054
## ENSG00000120129 3409.0294 2.871326 0.1190334 24.12201
## ENSG00000189221 2341.7673 3.230629 0.1373644 23.51868
## ENSG00000211445 12285.6151 3.552999 0.1589971 22.34631
## pvalue padj symbol entrez
## <numeric> <numeric> <character> <character>
## ENSG00000152583 2.637881e-138 4.755573e-134 SPARCL1 8404
## ENSG00000165995 1.597973e-137 1.440413e-133 CACNB2 783
## ENSG00000101347 1.195378e-128 6.620010e-125 SAMHD1 25939
## ENSG00000120129 1.468829e-128 6.620010e-125 DUSP1 1843
## ENSG00000189221 2.627083e-122 9.472210e-119 MAOA 4128
## ENSG00000211445 1.311440e-110 3.940441e-107 GPX3 2878
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 as.data.frame is necessary to convert the DataFrame object (IRanges package) to a data.frame object which can be processed by write.csv.
write.csv(as.data.frame(resOrdered), file="results.csv")
Another more sophisticated package for exporting results from various Bioconductor analysis packages is the ReportingTools package. ReportingTools will automatically generate dynamic HTML documents, including links to external databases using gene identifiers and boxplots summarizing the normalized counts across groups.
library("ReportingTools")
The most basic report is just a nicer HTML interface to the results table in R. We convert our results object into a dataframe and pass this to the ReportingTools functions. Here we will take just the top 1000 genes:
resTop <- head(resOrdered,1000)
resTop <- as(resTop, "data.frame")
resTop <- resTop[,c("padj","baseMean","log2FoldChange","pvalue",
"symbol","entrez")]
resTop$ensembl <- rownames(resTop)
There are three steps: (1) initializing a report, (2) publishing (pushing) the results to the report, and (3) finishing the report. The report will be written in a directory reports
created in the current working directory.
getwd()
htmlRep <- HTMLReport(shortName = "airway_DE_report",
title = "Airway differential expression analysis 06/2015",
reportDirectory = "./reports")
publish(resTop, htmlRep)
url <- finish(htmlRep)
browseURL(url)
We can now open in a web browser the file ./reports/airway_DE_report.html
. This gives a readable report with pagination, as well as sortable, filterable numeric columns, and a search function.
We can also use some of the built-in functionality of ReportingTools for DESeq2 objects (there are similar functions for edgeR and limma objects).
By including the DESeqDataSet along with a factor
indicating the condition of the samples, the output will include boxplots of normalized counts. Remember, though that our statistical test was not just of the differences across dexamethasone treatment, but the differences of the pairs (taking account of the cell line information).
Additionally, if we change the primary ID of our dataset to be Entrez IDs, then ReportingTools will also include the gene symbol, name and a link to an external database. In order to see what this looks like, let’s create a subset of the results table and DESeqDataSet in the same order: the top 50 genes by adjusted p-value. We then add the Entrez ID as the rownames of both objects.
resReport <- head(resOrdered, 50)
ddsReport <- dds[ rownames(resReport), ]
rownames(resReport) <- resReport$entrez
rownames(ddsReport) <- rownames(resReport)
Now we can pass these objects to ReportingTools:
htmlRep <- HTMLReport(shortName = "airway_DE_report",
title = "Airway differential expression analysis 06/2015",
reportDirectory = "./reports")
publish(resReport, htmlRep, DataSet=ddsReport,
annotation.db="org.Hs.eg.db",
n=50, factor=dds$dex)
url <- finish(htmlRep)
browseURL(url)
Here we will show some basic techniques for examining RNA-seq alignments in R, using the GenomicAlignments and the Gviz packages. First we will define a GRange which covers the subset of genes we loaded in the beginning of this workflow. In preparation for loading the alignments into R, we also need to create an index file for the BAM file we will be reading alignments from.
total.range <- GRanges("1", IRanges(11e6, 11.4e6))
indexBam(filenames[1])
## /home/ubuntu/R-libs/airway/extdata/SRR1039508_subset.bam
## "/home/ubuntu/R-libs/airway/extdata/SRR1039508_subset.bam.bai"
Now we can read in the alignments from the BAM file, and we can specify that we only want to read in the alignments from a specific region using ScamBamParam
.
ga <- readGAlignmentPairs(filenames[1], param=ScanBamParam(which=total.range))
ga
## GAlignmentPairs object with 7140 pairs, strandMode=1, and 0 metadata columns:
## seqnames strand : ranges -- ranges
## <Rle> <Rle> : <IRanges> -- <IRanges>
## [1] 1 + : [11053773, 11053835] -- [11053840, 11053902]
## [2] 1 - : [11067931, 11067993] -- [11067866, 11067928]
## [3] 1 + : [11072708, 11072770] -- [11072763, 11073797]
## [4] 1 + : [11072730, 11072792] -- [11073826, 11073888]
## [5] 1 + : [11072740, 11072801] -- [11073971, 11076911]
## ... ... ... ... ... ... ...
## [7136] 1 + : [11359177, 11359239] -- [11359278, 11359340]
## [7137] 1 + : [11362260, 11362322] -- [11362290, 11362352]
## [7138] 1 + : [11364685, 11364707] -- [11364733, 11364795]
## [7139] 1 - : [11365987, 11366049] -- [11365866, 11365928]
## [7140] 1 + : [11386063, 11386123] -- [11386132, 11386194]
## -------
## seqinfo: 84 sequences from an unspecified genome
Note that without specifying a region, or without setting yieldSize
while defining a BamFile
, this would start to load all the alignments into your R session, which would likely take up too much memory.
There are now a couple of methods defined for GAlignmentPairs objects, which can be found on the help page ?GAlignmentPairs
. One of these is to compute the genomic coverage. We can then look at the coverage over a small area, convert this to a numeric vector and plot it using base R graphics.
cov <- coverage(ga)
zoom <- GRanges("1", IRanges(11.07e6, 11.09e6))
cov[zoom]
## RleList of length 1
## $`1`
## integer-Rle of length 20001 with 3187 runs
## Lengths: 2708 11 11 1 4 5 ... 2 2 1 7 3 5
## Values : 0 1 2 3 4 5 ... 5 6 5 4 2 1
cov.numeric <- as.numeric(cov[zoom][[1]])
plot(cov.numeric, type="h")
Now we will use the Gviz package to make a more sophisticated version of this plot. See the vignette and help files for Gviz for more details on each function and its arguments.
library("Gviz")
options(ucscChromosomeNames=FALSE)
grtrack <- GeneRegionTrack(txdb)
gtrack <- GenomeAxisTrack()
altrack <- AlignmentsTrack(filenames[1])
plotTracks(list(gtrack, grtrack, altrack),
from=11.07e6, to=11.09e6, chromosome="1")
The above plot includes diagrams for each alignment. We can simplify this visualization by just showing the coverage and the exon junction coverage, similar to the sashimi plot which can be viewed in IGV.
altrack <- AlignmentsTrack(filenames[1], type=c("coverage","sashimi"))
plotTracks(list(gtrack, grtrack, altrack),
from=11.07e6, to=11.09e6, chromosome="1")
Suppose now we want to know, of the transcripts which can be viewed in this window, for which are most of the RNA-seq fragments compatible? Here, compatible means that the junctions in the aligned reads pairs must be compatible with the exon-exon junctions of the transcript.
ebt <- exonsBy(txdb, by="tx")
ebt.sub <- ebt[ebt %over% zoom]
fco <- findCompatibleOverlaps(ga, ebt.sub)
fco
## Hits object with 7276 hits and 0 metadata columns:
## queryHits subjectHits
## <integer> <integer>
## [1] 3 2
## [2] 4 2
## [3] 4 3
## [4] 4 4
## [5] 7 2
## ... ... ...
## [7272] 2942 11
## [7273] 2943 11
## [7274] 2944 11
## [7275] 2945 11
## [7276] 2946 11
## -------
## queryLength: 7140
## subjectLength: 12
We can investigate this Hits object, and tally the number of fragments comptabible with each transcript. We then save the top transcript, by fragment count.
(tab <- table(subjectHits(fco)))
##
## 1 2 3 4 5 6 7 8 9 10 11 12
## 438 2454 354 542 1018 1450 818 36 51 29 55 31
ebt.top <- ebt.sub[names(tab)[which.max(tab)]]
Now we can use GeneRegionTrack again, this time providing a GRangesList with the single top transcript. The Gviz package has many options for visualization and different data input.
grtrack2 <- GeneRegionTrack(ebt.top)
plotTracks(list(gtrack, grtrack2, altrack),
from=11.07e6, to=11.09e6, chromosome="1")
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.
sessionInfo()
## R version 3.2.1 (2015-06-18)
## Platform: x86_64-unknown-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.2 LTS
##
## 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] grid stats4 parallel stats graphics grDevices utils
## [8] datasets methods base
##
## other attached packages:
## [1] Gviz_1.13.3 ReportingTools_2.9.1
## [3] org.Hs.eg.db_3.1.2 RSQLite_1.0.0
## [5] DBI_0.3.1 genefilter_1.51.0
## [7] ggplot2_1.0.1 PoiClaClu_1.0.2
## [9] RColorBrewer_1.1-2 pheatmap_1.0.7
## [11] DESeq2_1.9.23 RcppArmadillo_0.5.200.1.0
## [13] Rcpp_0.11.6 Rsubread_1.19.2
## [15] GenomicAlignments_1.5.11 Rsamtools_1.21.14
## [17] Biostrings_2.37.2 XVector_0.9.1
## [19] AnnotationHub_2.1.30 GenomicFeatures_1.21.13
## [21] AnnotationDbi_1.31.17 airway_0.103.1
## [23] SummarizedExperiment_0.3.2 Biobase_2.29.1
## [25] GenomicRanges_1.21.16 GenomeInfoDb_1.5.8
## [27] IRanges_2.3.14 S4Vectors_0.7.10
## [29] BiocGenerics_0.15.3 BiocStyle_1.7.4
## [31] knitr_1.10.5
##
## loaded via a namespace (and not attached):
## [1] Category_2.35.1 matrixStats_0.14.2
## [3] bitops_1.0-6 httr_1.0.0
## [5] tools_3.2.1 R6_2.1.0
## [7] rpart_4.1-10 Hmisc_3.16-0
## [9] colorspace_1.2-6 nnet_7.3-10
## [11] GGally_0.5.0 gridExtra_2.0.0
## [13] curl_0.9.1 graph_1.47.2
## [15] formatR_1.2 ggbio_1.17.2
## [17] rtracklayer_1.29.12 labeling_0.3
## [19] scales_0.2.5 RBGL_1.45.1
## [21] stringr_1.0.0 digest_0.6.8
## [23] foreign_0.8-65 R.utils_2.1.0
## [25] rmarkdown_0.7 AnnotationForge_1.11.12
## [27] dichromat_2.0-0 htmltools_0.2.6
## [29] BSgenome_1.37.3 limma_3.25.13
## [31] PFAM.db_3.1.2 BiocInstaller_1.19.8
## [33] shiny_0.12.1 GOstats_2.35.1
## [35] hwriter_1.3.2 BiocParallel_1.3.34
## [37] R.oo_1.19.0 acepack_1.3-3.3
## [39] VariantAnnotation_1.15.20 RCurl_1.95-4.7
## [41] magrittr_1.5 GO.db_3.1.2
## [43] Formula_1.2-1 futile.logger_1.4.1
## [45] Matrix_1.2-2 munsell_0.4.2
## [47] proto_0.3-10 R.methodsS3_1.7.0
## [49] stringi_0.5-5 yaml_2.1.13
## [51] edgeR_3.11.2 MASS_7.3-43
## [53] zlibbioc_1.15.0 plyr_1.8.3
## [55] lattice_0.20-33 splines_3.2.1
## [57] annotate_1.47.1 locfit_1.5-9.1
## [59] geneplotter_1.47.0 reshape2_1.4.1
## [61] biomaRt_2.25.1 futile.options_1.0.0
## [63] XML_3.98-1.3 evaluate_0.7
## [65] biovizBase_1.17.1 latticeExtra_0.6-26
## [67] lambda.r_1.1.7 httpuv_1.3.2
## [69] gtable_0.1.2 reshape_0.8.5
## [71] mime_0.3 xtable_1.7-4
## [73] survival_2.38-3 OrganismDbi_1.11.42
## [75] cluster_2.0.2 interactiveDisplayBase_1.7.0
## [77] GSEABase_1.31.3