-
Notifications
You must be signed in to change notification settings - Fork 25
/
de-tutorial.Rmd
333 lines (212 loc) · 7.39 KB
/
de-tutorial.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
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
---
title: "Differential Expression Tutorial"
author: "Mark Dunning; mark 'dot' dunning 'at' cruk.cam.ac.uk, Oscar Rueda; oscar 'dot' rueda 'at' cruk.cam.ac.uk"
date: '`r format(Sys.time(), "Last modified: %d %b %Y")`'
output: html_document
---
# Importing the data
The data from this experiment comprises nine paired tumor/normal colon tissues on Illumina HT12\_v3 gene expression Beadchips. These data were generated to inform a comparison of technologies for microRNA profiling. However, we will only use the mRNA data here.
```{r echo=FALSE,message=FALSE}
library(GEOquery)
library(limma)
```
```{r cache=TRUE}
library(GEOquery)
library(limma)
url <- "ftp://ftp.ncbi.nih.gov/pub/geo/DATA/SeriesMatrix/GSE33126/GSE33126_series_matrix.txt.gz"
filenm <- "data/GSE33126_series_matrix.txt.gz"
if(!file.exists(filenm)) download.file(url, destfile=filenm)
gse <- getGEO(filename=filenm)
gse
```
```{r}
head(exprs(gse))
```
******
##Q Do the data look to be normalised?
******
```{r}
exprs(gse) <- log2(exprs(gse))
boxplot(exprs(gse),outline=FALSE)
```
Inspect the clinical variables
******
##Q What column has the information about whether is sample is a tumour or normal?
##Q How many do we have in each group?
******
```{r echo=FALSE}
## Your answer here ##
```
******
##Q Create a design matrix for the experiment, on paper if you like
******
# Simple t-tests
For completeness, we note that t-statistics and corresponding p-values can be generated quickly using the `genefilter` package.
```{r}
pd <- pData(gse)
SampleGroup <- pd$source_name_ch1
library(genefilter)
destats <- rowttests(exprs(gse),fac=SampleGroup)
head(destats)
```
- Can we remember the drawbacks of this approach?
A useful function is the `model.matrix`, which understands the linear model syntax of R (i.e. using the tilde `~` symbol)
```{r}
design <- model.matrix(~0+SampleGroup)
colnames(design) <- c("Normal","Tumour")
```
By-hand, we would do;
```{r}
design <- matrix(nrow=18,ncol=2)
design[,1] <- c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1)
design[,2] <- c(1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0)
colnames(design) <- c("Normal","Tumour")
```
or a shortcut in this would use the `rep` function.
```{r}
design[,1] <- rep(c(0,1),by=9)
design[,2] <- rep(c(1,0),by=9)
```
Prior to the differential expression analysis, we will *filter* the data so only the top 50% most-variable genes get analysed, which [has been shown](http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2906865/) to increase our power to detect differential expression. We can do this using the `varFilter` function in the `genefilter` package.
```{r}
library(genefilter)
gse.expFilt <- varFilter(gse)
gse.expFilt
```
The `lmFit` funcion is used to fit the model to the data. The output has various components, most of which you probably don't need to know about.
```{r}
fit <- lmFit(exprs(gse.expFilt), design)
names(fit)
dim(fit$coefficients)
head(fit$coefficients)
```
******
##Q. What is the interpretation of the `coeff` item in the output
******
Now we define the contrast
```{r}
contrasts <- makeContrasts(Tumour - Normal, levels=design)
fit2 <- contrasts.fit(fit, contrasts)
head(fit2$coeff)
```
Finally, apply the *empirical Bayes'* step
```{r}
fit2 <- eBayes(fit2)
fit2
```
We usually get our first look at the results by using the `topTable` command
```{r}
topTable(fit2)
```
The rows in this table are ordered according the *B-statistic*, or log-odds from the empirical Bayes' analysis. You should be cautious when interpreting these values. Although a B-statistic of 0 corresponds to a 50/50 chance of a gene being DE, this is contingent on a certain percentage of genes in the experiment being DE. In experiments such as the one we are looked at here, there are a large number of DE genes. In this case genes with B-statistic < 0 could still be DE. Thus, you should use the B-statistic as a ranking criteria and not attempt to compare the values between experiments.
# Including annotation
```{r}
anno <- fData(gse.expFilt)
head(anno)[,1:5]
anno <- anno[,c("Symbol","Entrez_Gene_ID","Chromosome","Cytoband")]
fit2$genes <- anno
topTable(fit2)
```
# Diagnostic checks
At this point it is useful to take stock and do some diagnostic checks
```{r}
dotchart(exprs(gse)["ILMN_1704294",])
```
```{r}
boxplot(exprs(gse)["ILMN_1704294",]~SampleGroup)
```
To find how many genes are DE we can use the `decideTests` function, which also applies multiple-test correction.
*Make a note of how many genes are DE, you will need this later*
```{r}
decideTests(fit2)
table(decideTests(fit2))
sum(abs(decideTests(fit2))==1)
```
The "*Volcano Plot*" function is a common way of visualising the results of a DE analysis. The $x$ axis shows the log-fold change and the $y$ axis is some measure of statistical significance, which in this case is the log-odds, or "B" statistic. A characteristic "volcano" shape should be seen.
```{r}
volcanoplot(fit2)
```
The function also has options to highlight the postions of the top N genes from the test.
```{r}
volcanoplot(fit2,highlight=10,names = fit2$genes$"Symbol")
```
# Exporting the results
```{r}
write.fit(fit2, file = "de-results.txt",adjust="BH")
```
# Getting the results for a particular gene
The `topTable` function by default only prints the top 10 DE genes. However, by changing the arguments we can make it print the statistics for all the genes that we test.
```{r}
testResults <- topTable(fit2, number=nrow(fit2))
```
This gives us the opportunity to look-up the results for particular genes of interest. Lets say we're interested in "OCIAD2" (for no particular reason...)
```{r}
testResults[which(testResults$Symbol == "OCIAD2"),]
```
If we have a list of genes, the `%in%` function can be used.
```{r}
mylist <- c("LOC441066","ARF3","FMNL3","CSF1R","XLKD1","TTRAP","DMWD","SYNPO2L","PILRB","LAMP3")
testResults[which(testResults$Symbol %in% mylist),]
```
# Further filtering and ordering
******
- Q. Create a data frame containing genes with i) adjusted p-value < 0.05 ii) log-fold change > 1. How many genes does this contain?
- Q. Now order this data frame according to chromosome
******
```{r echo=FALSE}
## Your Answer here ##
```
# Improved analysis: Doing a paired analysis
******
- Q. What would the design matrix look like to incorporate patient ID into the model
- Q. Create this model using the `model.matrix` function
******
```{r echo=FALSE}
## Your answer here ##
```
******
- Q. Proceed to fit your model to the data
- Q. How many genes are DE? How does this compare to the previous model?
******
```{r}
## Your answer here ##
```
# The estrogen dataset
Recall the estrogen dataset from yesterday
```{r}
library(affy)
targetsFile <- "estrogen/estrogen.txt"
pd <- read.AnnotatedDataFrame(targetsFile,header=TRUE,sep="",row.names=1)
pData(pd)
ER <- pData(pd)$estrogen
Time <- factor(pData(pd)$time.h)
design <- model.matrix(~ER+Time)
design
design2 <- model.matrix(~ER*Time)
design2
```
# Reading and normalising the data
We read the data and then apply rma normalisation. What are the steps?
```{r}
raw <-ReadAffy(celfile.path = "estrogen", filenames=rownames(pData(pd)),phenoData = pd)
raw
eset <- rma(raw)
eset
```
# Fitting the models
Fit the model with and without interaction.
```{r}
library(limma)
fit1 <- lmFit(eset, design)
fit1 <- eBayes(fit1)
topTable(fit1, coef=2)
fit2 <- lmFit(eset, design2)
fit2 <- eBayes(fit2)
topTable(fit2, coef=2)
```
******
## Q. Which one do you prefer? Why?
******
```{r echo=FALSE}
table(decideTests(fit2)[,4])
```