File: EDASeq.Rmd

package info (click to toggle)
r-bioc-edaseq 2.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 408 kB
  • sloc: sh: 13; makefile: 2
file content (549 lines) | stat: -rw-r--r-- 18,969 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
---
title: "EDASeq: Exploratory Data Analysis and Normalization for RNA-Seq"
author: "Davide Risso"
date: "Last modified: May 17, 2018; Compiled: `r format(Sys.time(), '%B %d, %Y')`"
bibliography: biblio.bib
output:
  BiocStyle::html_document:
    toc: true
vignette: >
  %\VignetteEncoding{UTF-8}
---

<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{EDASeq Vignette}
-->

# Introduction

In this document, we show how to conduct Exploratory Data Analysis
(EDA) and normalization for a typical RNA-Seq experiment using the
package `EDASeq`.

One can think of EDA for RNA-Seq as a two-step process: "read-level"
EDA helps in discovering lanes with low sequencing depths, quality
issues, and unusual nucleotide frequencies, while ``gene-level'' EDA
can capture mislabeled lanes, issues with distributional assumptions
(e.g., over-dispersion), and GC-content bias.

The package also implements both "within-lane" and "between-lane"
normalization procedures, to account, respectively, for within-lane
gene-specific (and possibly lane-specific) effects on read counts
(e.g., related to gene length or GC-content) and for between-lane
distributional differences in read counts (e.g., sequencing depths).

To illustrate the functionality of the `EDASeq` package, we
make use of the _Saccharomyces cerevisiae_ RNA-Seq data from
[@lee2008novel]. Briefly, a wild-type strain and three mutant
strains were sequenced using the Solexa 1G Genome Analyzer.  For each
strain, there are four technical replicate lanes from the same library
preparation. The reads were aligned using `Bowtie`
[@langmead2009ultrafast], with unique mapping and allowing up to
two mismatches.

The `leeBamViews` package provides a subset of the aligned
reads in BAM format. In particular, only the reads mapped between
bases 800,000 and 900,000 of chromosome XIII are considered. We use
these reads to illustrate read-level EDA.

The `yeastRNASeq` package contains gene-level read counts for
four lanes: two replicates of the wild-type strain ("wt") and
two replicates of one of the mutant strains ("mut").  We use
these data to illustrate gene-level EDA.

```{r setup, echo=FALSE}
library(knitr)
opts_chunk$set(cache=FALSE, message=FALSE, echo=TRUE, results="markup")
options(width=60)
```

```{r data}
library(EDASeq)
library(yeastRNASeq)
library(leeBamViews)
```

# Reading in unaligned and aligned read data {#secRead}

## Unaligned reads

Unaligned (unmapped) reads stored in FASTQ format may be managed via
the class `FastqFileList` imported from `ShortRead`.
Information related to the libraries sequenced in each lane can be
stored in the `elementMetadata` slot of the
`FastqFileList` object.

```{r import-raw}
files <- list.files(file.path(system.file(package = "yeastRNASeq"),
                              "reads"), pattern = "fastq", full.names = TRUE)
names(files) <- gsub("\\.fastq.*", "", basename(files))
met <- DataFrame(conditions=c(rep("mut",2), rep("wt",2)),
                 row.names=names(files))
fastq <- FastqFileList(files)
elementMetadata(fastq) <- met
fastq
```

## Aligned reads

The package can deal with aligned (mapped) reads in BAM format, using
the class `BamFileList` from `Rsamtools`.  Again, the
`elementMetadata` slot can be used to store lane-level sample
information.

```{r import-aligned}
files <- list.files(file.path(system.file(package = "leeBamViews"), "bam"),
                    pattern = "bam$", full.names = TRUE)
names(files) <- gsub("\\.bam", "", basename(files))

gt <- gsub(".*/", "", files)
gt <- gsub("_.*", "", gt)
lane <- gsub(".*(.)$", "\\1", gt)
geno <- gsub(".$", "", gt)

pd <- DataFrame(geno=geno, lane=lane,
                row.names=paste(geno,lane,sep="."))

bfs <- BamFileList(files)
elementMetadata(bfs) <- pd
bfs
```


# Read-level EDA


## Numbers of unaligned and aligned reads

One important check for quality control is to look at the total number
of reads produced in each lane, the number and the percentage of reads mapped to a
reference genome. A low total
number of reads might be a symptom of low quality of the input RNA,
while a low mapping percentage might indicate poor quality of the
reads (low complexity), problems with the reference genome, or
mislabeled lanes.

```{r plot-total}
colors <- c(rep(rgb(1,0,0,alpha=0.7),2),
            rep(rgb(0,0,1,alpha=0.7),2),
            rep(rgb(0,1,0,alpha=0.7),2),
            rep(rgb(0,1,1,alpha=0.7),2))
barplot(bfs,las=2,col=colors)
```

The figure, produced using the `barplot` method for the
`BamFileList` class, displays the number of mapped reads for
the subset of the yeast dataset included in the package
`leeBamViews`.  Unfortunately, `leeBamViews` does
not provide unaligned reads, but barplots of the total number of reads
can be obtained using the `barplot` method for the
`FastqFileList` class. Analogously, one can plot the percentage
of mapped reads with the `plot` method with signature
`c(x="BamFileList", y="FastqFileList")`. See the manual pages for
details.

## Read quality scores

As an additional quality check, one can plot the mean per-base (i.e.,
per-cycle) quality of the unmapped or mapped reads in every lane.

```{r plot-total-2}
plotQuality(bfs,col=colors,lty=1)
legend("topright",unique(elementMetadata(bfs)[,1]), fill=unique(colors))
```

## Individual lane summaries

If one is interested in looking more thoroughly at one lane, it is
possible to display the per-base distribution of quality scores for
each lane and the number of mapped reads
stratified by chromosome or strand. As expected,
all the reads are mapped to chromosome XIII.

```{r plot-qual}
plotQuality(bfs[[1]],cex.axis=.8)
barplot(bfs[[1]],las=2)
```

## Read nucleotide distributions

A potential source of bias is related to the sequence composition of
the reads. The function `plotNtFrequency` plots the
per-base nucleotide frequencies for all the reads in a given
lane.

```{r plot-nt}
plotNtFrequency(bfs[[1]])
```


# Gene-level EDA

Examining statistics and quality metrics at a read level can help in
discovering problematic libraries or systematic biases in one or more
lanes. Nevertheless, some biases can be difficult to detect at this
scale and gene-level EDA is equally important.

## Classes and methods for gene-level counts

There are several Bioconductor packages for aggregating reads over
genes (or other genomic regions, such as, transcripts and exons) given
a particular genome annotation, e.g., `IRanges`,
`ShortRead`, `Genominator`, `Rsubread`. See
their respective vignettes for details.

Here, we consider this step done and load the object
`geneLevelData` from `yeastRNASeq`, which provides
gene-level counts for 2 wild-type and 2 mutant lanes from the yeast
dataset of `lee2008novel` (see the `Genominator`
vignette for an example on the same dataset).

```{r load-gene-level}
data(geneLevelData)
head(geneLevelData)
```

Since it is useful to explore biases related to length and GC-content,
the `EDASeq` package provides, for illustration purposes,
length and GC-content for _S. cerevisiae_ genes (based on SGD
annotation, version r64 [@sgd]).

Functionality for automated retrieval of gene length and GC-content
is introduced in the last section of the vignette.


```{r load-lgc}
data(yeastGC)
head(yeastGC)
data(yeastLength)
head(yeastLength)
```

First, we filter the non-expressed genes, i.e., we consider only the
genes with an average read count greater than 10 across the four lanes
and for which we have length and GC-content information.

```{r filter}
filter <- apply(geneLevelData,1,function(x) mean(x)>10)
table(filter)
common <- intersect(names(yeastGC),
                    rownames(geneLevelData[filter,]))
length(common)
```

This leaves us with `r length(common)` genes.

The `EDASeq` package provides the `SeqExpressionSet`
class to store gene counts, (lane-level) information on the sequenced
libraries, and (gene-level) feature information.  We use the data
frame `met` created in Section `secRead` for the
lane-level data.  As for the feature data, we use gene length and
GC-content.

```{r create-object}
feature <- data.frame(gc=yeastGC,length=yeastLength)
data <- newSeqExpressionSet(counts=as.matrix(geneLevelData[common,]),
                            featureData=feature[common,],
                            phenoData=data.frame(
                              conditions=factor(c(rep("mut",2),rep("wt",2))),
                              row.names=colnames(geneLevelData)))
data
```

Note that the row names of `counts` and `featureData`
must be the same; likewise for the row names of `phenoData`
and the column names of `counts`.  The expression values can be accessed with `counts`, the lane information with `pData`,
and the feature information with `fData`.

```{r show-data}
head(counts(data))
pData(data)
head(fData(data))
```

The `SeqExpressionSet` class has two additional slots:
`normalizedCounts` and `offset` (matrices of the same dimension as `counts`),
which may be used to store a matrix of normalized counts and of
normalization offsets, respectively, to be used for subsequent analyses (see Section
\ref{secDE} and the `edgeR` vignette for details on the
role of offsets). If not specified, the offset is initialized as a matrix
of zeros.

```{r show-offset}
head(offst(data))
```

## Between-lane distribution of gene-level counts

One of the main considerations when dealing with gene-level counts is
the difference in count distributions between lanes. The
`boxplot` method provides an easy way to produce boxplots of
the logarithms of the gene counts in each lane.

```{r boxplot-genelevel}
boxplot(data,col=colors[1:4])
```

The `MDPlot` method produces a mean-difference plot (MD-plot)
of read counts for two lanes.

```{r md-plot}
MDPlot(data,c(1,3))
```

## Over-dispersion

Although the Poisson distribution is a natural and simple way to model
count data, it has the limitation of assuming equality of the mean and
variance. For this reason, the negative binomial distribution has been
proposed as an alternative when the data show over-dispersion. The
function `meanVarPlot` can be used to check whether the
count data are over-dispersed (for the Poisson distribution, one would
expect the points in the following Figures to be evenly
scattered around the black line).

```{r plot-mean-var}
meanVarPlot(data[,1:2], log=TRUE, ylim=c(0,16))
meanVarPlot(data, log=TRUE, ylim=c(0,16))
```

Note that the mean-variance relationship should be examined within
replicate lanes only (i.e., conditional on variables expected to
contribute to differential expression). For the yeast dataset, it is
not surprising to see no evidence of over-dispersion for the two
mutant technical replicate lanes; likewise for
the two wild-type lanes. However, one expects over-dispersion in the
presence of biological variability, when considering at once all four mutant and wild-type lanes
[@anders2010differential,@bullard2010evaluation,@robinson2010edger].


## Gene-specific effects on read counts

Several authors have reported selection biases related to sequence
features such as gene length, GC-content, and mappability
[@bullard2010evaluation,@hansen2011removing,@oshlack2009transcript,@risso2011gc].

In the following figure, obtained using `biasPlot`, one can
see the dependence of gene-level counts on GC-content. The same plot
could be created for gene length or mappability instead of GC-content.

```{r plot-gc}
biasPlot(data, "gc", log=TRUE, ylim=c(1,5))
```

To show that GC-content dependence can bias differential expression
analysis, one can produce stratified boxplots of the log-fold-change
of read counts from two lanes using the `biasBoxplot` method.
Again, the same type of plots can be created
for gene length or mappability.

```{r boxplot-gc}
lfc <- log(counts(data)[,3]+0.1) - log(counts(data)[,1]+0.1)
biasBoxplot(lfc, fData(data)$gc)
```


# Normalization

Following [@risso2011gc], we consider two main types of effects
on gene-level counts: (1) within-lane gene-specific (and possibly
lane-specific) effects, e.g., related to gene length or GC-content,
and (2) effects related to between-lane distributional differences,
e.g., sequencing depth. Accordingly,
`withinLaneNormalization` and
`betweenLaneNormalization` adjust for the first and second
type of effects, respectively.  We recommend to normalize for
within-lane effects prior to between-lane normalization.

We implemented four within-lane normalization methods, namely: loess
robust local regression of read counts (log) on a gene feature such as
GC-content (`loess`), global-scaling between feature strata
using the median (`median`), global-scaling between feature
strata using the upper-quartile (`upper`), and full-quantile
normalization between feature strata (`full`). For a discussion
of these methods in context of GC-content normalization see
[@risso2011gc].

```{r normalization}
dataWithin <- withinLaneNormalization(data,"gc", which="full")
dataNorm <- betweenLaneNormalization(dataWithin, which="full")
```

Regarding between-lane normalization, the package implements three of
the methods introduced in [@bullard2010evaluation]:
global-scaling using the median (`median`), global-scaling using
the upper-quartile (`upper`), and full-quantile normalization
(`full`).

The next figure shows how after full-quantile within- and
between-lane normalization, the GC-content bias is reduced and the
distribution of the counts is the same in each lane.

```{r plot-gc-norm}
biasPlot(dataNorm, "gc", log=TRUE, ylim=c(1,5))
boxplot(dataNorm, col=colors)
```

## Offset 

Some authors have argued that it is better to leave the count data
unchanged to preserve their sampling properties and instead use an
offset for normalization purposes in the statistical model for read
counts
[@anders2010differential,@hansen2011removing,@robinson2010edger]. This
can be achieved easily using the argument `offset` in both
normalization functions.

```{r norm-offset}
dataOffset <- withinLaneNormalization(data,"gc",
                                      which="full",offset=TRUE)
dataOffset <- betweenLaneNormalization(dataOffset,
                                       which="full",offset=TRUE)
```

Note that the `dataOffset` object will have both normalized
counts and offset stored in their respective slots.


# Differential expression analysis

One of the main applications of RNA-Seq is differential expression
analysis.  The normalized counts (or the original counts and the
offset) obtained using the `EDASeq` package can be supplied
to packages such as `edgeR` [@robinson2010edger] or
`DESeq2` [@deseq2] to find differentially
expressed genes. This section should be considered only as an
illustration of the compatibility of the results of `EDASeq`
with two of the most widely used packages for differential expression;
we refer ther reader to the edgeR's user guide and to the DESeq2 vignettes for more details on the methods implemented there.

## edgeR

We can perform a differential expression analysis with
`edgeR` based on the original counts by passing an offset to
the generalized linear model.  See the `edgeR` vignette for details
about how to perform a differential expression analysis with more complex designs or more robust approaches.

```{r edger}
library(edgeR)
design <- model.matrix(~conditions, data=pData(dataOffset))

y <- DGEList(counts=counts(dataOffset),
             group=pData(dataOffset)$conditions)
y$offset <- -offst(dataOffset)
y <- estimateDisp(y, design)

fit <- glmFit(y, design)
lrt <- glmLRT(fit, coef=2)
topTags(lrt)
```

## DESeq2

We can perform the same differential expression analysis with
`DESeq2`.

```{r deseq}
library(DESeq2)
dds <- DESeqDataSetFromMatrix(countData = counts(dataOffset),
                              colData = pData(dataOffset),
                              design = ~ conditions)

normFactors <- exp(-1 * offst(dataOffset))
normFactors <- normFactors / exp(rowMeans(log(normFactors)))
normalizationFactors(dds) <- normFactors

dds <- DESeq(dds)
res <- results(dds)
res
```

# Definitions and conventions

## Rounding

After either within-lane or between-lane normalization, the expression
values are not counts
anymore. However, their distribution still shows some typical features
of counts distribution (e.g., the variance depends on the mean).
Hence, for most applications, it is useful to round the normalized
values to recover count-like values, which we refer to as "pseudo-counts".

By default, both
`withinLaneNormalization` and
`betweenLaneNormalization` round the normalized values to
the closest integer. This behavior can be changed by specifying
`round=FALSE`. This gives the user more flexibility and assures
that rounding approximations do not affect subsequent computations
(e.g., recovering the offset from the normalized counts).

## Zero counts

To avoid problems in the computation of logarithms (e.g. in log-fold-changes), we add a small positive constant (namely $0.1$) to the
counts. For instance, the log-fold-change between $y_1$ and $y_2$ is
defined as
\begin{equation*}
  \frac{\log(y_1 + 0.1)}{\log(y_2 + 0.1)}.
\end{equation*}

## Offset
We define an offset in the normalization as
\begin{equation*}
  o = \log(y_{norm} + 0.1) - \log(y_{raw} + 0.1),
\end{equation*}
where $y_{norm}$ and $y_{raw}$ are the normalized and raw counts, respectively.

One can easily recover the normalized data from the raw counts and
offset, as shown here:

```{r unrounded}
dataNorm <- betweenLaneNormalization(data, round=FALSE, offset=TRUE)

norm1 <- normCounts(dataNorm)
norm2 <- exp(log(counts(dataNorm) + 0.1 ) + offst(dataNorm)) - 0.1

head(norm1 - norm2)
```

Note that the small constant added in the definition of offset does
not matter when pseudo-counts are considered, i.e.,

```{r rounded}
head(round(normCounts(dataNorm)) - round(counts(dataNorm) * exp(offst(dataNorm))))
```

We defined the offset as the log-ratio between normalized and raw
counts. However, the `edgeR` functions expect as offset
argument the log-ratio between raw and normalized counts. One must use
`-offst(offsetData)` as the offset argument of `edgeR`.


# Retrieving gene length and GC-content

Two essential features the gene-level EDA normalizes for are gene length and
GC-content. As users might wish to automatically retrieve this information, we
provide the function `getGeneLengthAndGCContent`. Given selected
ENTREZ or ENSEMBL gene IDs and the organism under investigation, this can be
done either based on BioMart (default) or using BioC annotation utilities.

```{r getLengthAndGC, eval=FALSE}
getGeneLengthAndGCContent(id=c("ENSG00000012048", "ENSG00000139618"), org="hsa")
```

Accordingly, we can retrieve the precalculated yeast data that has been used
throughout the vignette via

```{r getLengthAndGC-full, eval=FALSE}
fData(data) <- getGeneLengthAndGCContent(featureNames(data),
                                              org="sacCer3", mode="org.db")
```

# SessionInfo

```{r sessionInfo}
sessionInfo()
```

# References