-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathslide_dge.Rmd
267 lines (189 loc) · 6.76 KB
/
slide_dge.Rmd
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
---
title: "Differential Gene Expression"
subtitle: "Workshop on RNA-Seq"
author: "`r paste0('<b>Julie Lorent</b> | ',format(Sys.time(), '%d-%b-%Y'))`"
institute: NBIS, SciLifeLab
keywords: bioinformatics, course, scilifelab, nbis
output:
xaringan::moon_reader:
encoding: 'UTF-8'
self_contained: false
chakra: 'assets/remark-latest.min.js'
css: 'assets/slide.css'
lib_dir: libs
nature:
ratio: '4:3'
highlightLanguage: r
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%/%total%"
include: NULL
---
exclude: true
count: false
```{r,echo=FALSE,child="assets/header-slide.Rmd"}
```
<!-- ------------ Only edit title, subtitle & author above this ------------ -->
```{r,include=FALSE}
# load the packages you need
library(dplyr)
library(tidyr)
#library(stringr)
library(ggplot2)
#library(plotly)
library(pheatmap)
library(DESeq2)
library(edgeR)
library(gridExtra)
source("https://raw.githubusercontent.com/NBISweden/workshop-RNAseq/master/assets/scripts.R")
```
```{r,eval=TRUE,include=FALSE}
download_data("data/counts_filtered.csv")
cf <- read.csv("data/counts_filtered.csv",header=TRUE,stringsAsFactors=FALSE,row.names=1)
download_data("data/metadata_raw.csv")
mr <- read.csv("data/metadata_raw.csv",header=TRUE,stringsAsFactors=FALSE,row.names=1)
```
---
name: intro
## What I'll talk about in this lecture
- Compare gene expression between 2 groups of samples
- while accounting for differences in sequencing depth
- by testing for differences in means with appropriate estimation of count data variability
- Calculate log fold changes
- Step by step description of DESeq2 analysis
## What we will discuss in the next lecture
- More information on count data distributions
- Batch effects
- Advanced designs
---
name: intro2
## Couldn’t we just use a Student’s t test for each gene?
```{r, echo=FALSE, out.width="30%", fig.align='center'}
knitr::include_graphics("data/boxplot.png")
```
- May have few replicates -> "borrow" information from other genes
- Multiple testing issues -> Correction
- Distribution is not normal -> negative binomial methods
---
name: prep
## Preparation
- DEseq2 (and edgeR) take as input raw counts and metadata.
- Create the DESeq2 object
```{r}
library(DESeq2)
mr$Group <- factor(mr$Group)
d <- DESeqDataSetFromMatrix(countData=cf,colData=mr,design=~Group)
d
```
- Categorical variables must be factors
- Building GLM models: `~var`, `~covar+var`
???
- The model `~var` asks DESeq2 to find DEGs between the levels under the variable *var*.
- The model `~covar+var` asks DESeq2 to find DEGs between the levels under the variable *var* while controlling for the covariate *covar*.
---
name: dge-sf
## Size factors
- Objective of the differential gene expression: compare concentration of cDNA fragments from each gene between conditions/samples.
- Data we have: read counts which depend on these concentration, but also on sequencing depth
- Total count can be influenced by a few highly variable genes
- For this reason, DESeq2 uses size factors (median-of-ratios) instead of total count as normalization factors to account for differences in sequencing depth
- Normalisation factors are computed as follows:
```{r}
d <- DESeq2::estimateSizeFactors(d,type="ratio")
sizeFactors(d)
```
---
name: neg-binom
## Negative binomial distribution
- RNAseq data is not normally distributed neither as raw counts nor using simple transformations
- DESeq2 and edgeR instead assume negative binomial distributions.
- Given this assumption, to test for differential expression, one need to get a good estimate of the dispersion (variability given the mean).
---
name: dge-dispersion-1
## Dispersion
- Dispersion is a measure of spread or variability in the data.
- Variance is a classical measure of dispersion which is usually not used for negative binomial distributions because of its relationship to the mean
- The DESeq2 dispersion approximates the coefficient of variation for genes with moderate to high count values and is corrected for genes with low count values
```{r,fig.height=4,fig.width=8,echo=FALSE}
cv <- function(x) sd(x)/mean(x)
rowCv <- function(x) apply(x,1,cv)
{
par(mfrow=c(1,2))
plot(x=log10(rowMeans(cf)+1),y=log10(rowVars(as.matrix(cf))),xlab="Log10 Mean counts",ylab="Log10 Mean Variance")
plot(x=log10(rowMeans(cf)+1),y=log10(rowCv(as.matrix(cf))),xlab="Log10 Mean counts",ylab="Log10 Mean CV")
par(mfrow=c(1,1))
}
```
---
name: dge-dispersion-2
## Dispersion
- RNAseq experiments typically have few replicates
- To improve the dispersion estimation in this case, we "borrow" information from other genes with similar mean values
```{r,fig.height=3,fig.width=3}
d <- DESeq2::estimateDispersions(d)
```
![](data/deseq_dispersion.png)
---
name: dge-test
## Testing
- Log2 fold changes changes are computed after GLM fitting `FC = counts group B / counts group A`
```{r}
dg <- nbinomWaldTest(d)
resultsNames(dg)
```
--
- Use `results()` to customise/return results
- Set coefficients using `contrast` or `name`
- Filtering results by fold change using `lfcThreshold`
- `cooksCutoff` removes outliers
- `independentFiltering` removes low count genes
- `pAdjustMethod` sets method for multiple testing correction
- `alpha` set the significance threshold
---
name: dge-test-2
## Testing
```{r}
res <- results(dg,name="Group_day07_vs_day00",alpha=0.05)
summary(res)
```
- Alternative way to specify contrast
```{r,eval=FALSE}
results(dg,contrast=c("Group","day07","day00"),alpha=0.05)
```
---
name: dge-test-3
## Testing
```{r}
head(res)
```
---
name: dge-test-4
## Testing
- Use `lfcShrink()` to correct fold changes for genes with high dispersion or low counts
- Does not change number of DE genes
![](data/lfc_shrink.png)
---
name: acknowl
## Acknowledgements
- RNA-seq analysis [Bioconductor vignette](http://master.bioconductor.org/packages/release/workflows/vignettes/rnaseqGene/inst/doc/rnaseqGene.html)
- [DGE Workshop](https://github.com/hbctraining/DGE_workshop/tree/master/lessons) by HBC training
<!-- --------------------- Do not edit this and below --------------------- -->
---
name: acknowl
## Acknowledgements
- RNA-seq analysis [Bioconductor vignette](http://master.bioconductor.org/packages/release/workflows/vignettes/rnaseqGene/inst/doc/rnaseqGene.html)
- [DGE Workshop](https://github.com/hbctraining/DGE_workshop/tree/master/lessons) by HBC training
---
name: end_slide
class: end-slide, middle
count: false
# Thank you. Questions?
```{r,echo=FALSE,child="assets/footer-slide.Rmd"}
```
```{r,include=FALSE,eval=FALSE}
# manually run this to render this document to HTML
rmarkdown::render("slide_dge.Rmd")
# manually run this to convert HTML to PDF
#pagedown::chrome_print("presentation_dge.html",output="presentation_dge.pdf")
```