-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGE_Alina.Rmd
488 lines (342 loc) · 12.2 KB
/
DGE_Alina.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
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
---
title: "Differential Gene Expression Analysis Notebook - Alina's Data Analysis Project"
output:
pdf_document:
toc: yes
html_document:
toc: yes
number_sections: yes
---
The DGE analysis will be performed using the raw integer read counts for control and infected conditions. The goal here is to identify the differentially expressed genes under infected condition.
```{r message=F, warning=F, paged.print=TRUE}
library("DESeq2")
library("PoiClaClu")
library("pheatmap")
library("RColorBrewer")
library('tidyverse')
library("PoiClaClu")
library("vsn")
library('EnhancedVolcano')
```
```{r}
cm1 <-as.matrix(read.csv(
"C:/Users/kesha/Documents/R/Rtuts/Data/WG__Discussion_Alina's_EPEC_project/fc/counts.csv",
row.names = "X"
))
dim(cm1)
```
```{r}
head(cm1)
```
DESeq2 needs sample information (metadata) for performing DGE analysis. Let’s create the sample information (you can also import sample information if you have it in a file).
```{r}
sample_ID <- list(read.csv("C:/Users/kesha/Documents/R/Rtuts/Data/WG__Discussion_Alina's_EPEC_project/fc/samples.csv", row.names = "X"))
sample_ID
```
```{r}
condition = c("Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","Infected","control","control")
```
```{r}
coldata <- data.frame(sample_ID, condition)
colnames(coldata) <- c('sample','condition') # change name of one of the columns
coldata
```
```{r}
library(stringr)
coldata$sample = str_remove_all(coldata$sample, pattern = '.markdup.sorted') # tidying up the
coldata
```
```{r}
coldata$condition <- as.factor(coldata$condition)
coldata <- data.frame(coldata, row.names = 1) # convert column to row.names
coldata
```
It is essential to have the name of the columns in the count matrix in the same order as that in name of the samples (rownames in coldata)
```{r}
colnames(cm1) = str_remove_all(colnames(cm1), pattern = '.markdup.sorted') #tidying up these names again
colnames(cm1)
```
```{r}
all(rownames(coldata) %in% colnames(cm1))
```
Now, construct DESeqDataSet for DGE analysis:
```{r}
dds <- DESeqDataSetFromMatrix(countData = cm1,
colData = coldata,
design = ~ condition)
```
```{r}
dds
```
# Normalization
## Count Normalization via DESq2
```{r}
dds <- estimateSizeFactors(dds)
```
Can check the normalization factors using:
```{r}
sizeFactors(dds)
```
```{r}
normalized_counts <- counts(dds, normalized = TRUE)
```
Saving the normalized counts table.
```{r}
write.table(normalized_counts,
file="C:/Users/kesha/Documents/R/Rtuts/Data/WG__Discussion_Alina's_EPEC_project/results/normalized_counts.txt",
sep = "\t",
quote = FALSE,
col.names = NA)
```
ASK Q1!
# Quality Control
Assess overall similarity between samples and understand the following:
1. Which samples are similar to each other and which are different?
2. Does this fit to the expectation from experiment's design?
3. What are the major sources of variation in dataset?
## Count Data Tansformations - Extracting Transformed Values
### Transform normalised counts using rlog transformation for Visualization (only!)
```{r}
rld <- rlog(dds, blind = TRUE)
# blind flag turned on to keep the transformation unbiased to sample condition information
vsd <- vst(dds, blind = TRUE)
```
```{r}
head(assay(vsd))
```
### Effects of transformations on the variance
```{r}
# this gives log2(n + 1)
ntd <- normTransform(dds)
meanSdPlot(assay(ntd))
```
```{r}
meanSdPlot(assay(vsd))
```
```{r}
meanSdPlot(assay(rld))
```
## Data Quality assessment via sample clustering and visualization
### Heatmap of Count Matrix
```{r}
select <- order(rowMeans(counts(dds,normalized=TRUE)),
decreasing=TRUE)[1:21]
df <- as.data.frame(colData(dds))
```
```{r}
pheatmap(assay(ntd)[select,], cluster_rows=TRUE, show_rownames=TRUE,
cluster_cols=FALSE, annotation_col=df)
```
```{r}
pheatmap(assay(vsd)[select,], cluster_rows=TRUE, show_rownames=TRUE,
cluster_cols=FALSE, annotation_col=df)
```
### Heatmap of sample-to-sample distances using the variance stabilizing transformed values.
```{r}
sampleDists <- dist(t(assay(vsd)))
sampleDistMatrix <- as.matrix( sampleDists )
rownames(sampleDistMatrix) <- names(vsd$sizeFactor)
colnames(sampleDistMatrix) <- NULL
```
```{r}
colors <- colorRampPalette( rev(brewer.pal(9, "Spectral")) )(255)
pheatmap(sampleDistMatrix,
clustering_distance_rows = sampleDists,
clustering_distance_cols = sampleDists,
col= colors
)
```
### Plot PCA of Samples
```{r}
plotPCA(vsd, intgroup="condition")
```
### Hierarchical Clustering
```{r}
### Extract the rlog matrix from the object
rld_mat <- assay(rld) #assay() is function from the "SummarizedExperiment" package that was loaded when you loaded DESeq2
```
```{r}
### Compute pairwise correlation values
rld_cor <- cor(rld_mat) ## cor() is a base R function
head(rld_cor) ## check the output of cor(), make note of the rownames and colnames
```
```{r}
### Plot heatmap
pheatmap(rld_cor, border_color=NA,
fontsize = 8,
fontsize_row = 10,
height=50)
```
```{r}
heat.colors <- brewer.pal(6, "Spectral")
pheatmap(rld_cor, color = heat.colors, border_color=NA, fontsize = 7,
fontsize_row = 10, height=50)
```
# DGE Analysis
Pre-filter the genes which have low counts. Here, I will remove the genes which have < 10 reads (this can vary based on research goal) in total across all the samples. Pre-filtering helps to remove genes that have very few mapped reads, reduces memory, and increases the speed of the DESeq2 analysis.
```{r}
dds <- dds[rowSums(counts(dds)) >= 1,]
```
Now, select the reference level for condition comparisons. The reference level can set using ref parameter. The comparisons of other conditions will be compared against this reference i.e, the log2 fold changes will be calculated based on ref value (infected/control) . If this parameter is not set, comparisons will be based on alphabetical order of the levels.
```{r}
dds$condition <- relevel(dds$condition, ref = "control")
```
Perform Differntial Gene Expression Analysis:
```{r}
dds1 <- DESeq(dds)
```
```{r}
resultsNames(dds1)
```
### Result
```{r}
res <- results(dds1, independentFiltering=FALSE, name = "condition_Infected_vs_control")
res
```
Order gene expression table by adjusted p value (Benjamini-Hochberg FDR method)
```{r}
res_adj <- res[order(res$padj),]
res_adj
```
### Note on NA: some genes with p value set to NA.
1. This is due to all samples have zero counts for a gene ----->>> baseMean is 0 and all other values are NA.
2. There is a row with extreme outlier count for a gene (determined by cook's distance) ----->>> pvalue set to NA.
Cook's distance : DESeq function calculates, for every gene and for every sample, a diagnostic test for outliers called Cook’s distance. Cook’s distance is a measure of how much a single sample is influencing the fitted coefficients for a gene, and a large value of Cook’s distance is intended to indicate an outlier count. The Cook’s distances are stored as a matrix available in assays(dds)[["cooks"]].
3. If a row is filtered by automatic independent filtering, for having a low mean normalized count, then only the adjusted p value will be set to NA.
```{r}
write.csv(as.data.frame(res[order(res$padj),] ),
file="C:/Users/kesha/Documents/R/Rtuts/Data/WG__Discussion_Alina's_EPEC_project/results/condition_infected_vs_control_dge.csv")
```
```{r}
summary(results(dds1, alpha=0.05))
```
```{r}
r<- results(dds1, alpha=0.05, lfcThreshold =1)
r
```
```{r}
summary(results(dds1, alpha=0.05, lfcThreshold =1))
```
```{r}
ir <- results(dds1, alpha=0.05, lfcThreshold =1)
ir
```
## Results - with padj and LFC thresholds
```{r}
res.05 <- results(dds1, alpha = 0.05)
table(res.05$padj < 0.05)
```
```{r}
resLFC1 <- results(dds1, lfcThreshold=1)
table(resLFC1$padj < 0.05)
```
```{r}
new_res <- results(dds1, alpha = 0.05, lfcThreshold=1)
new_res
```
### Order gene expression table by adjusted p value (Benjamini-Hochberg FDR method)
```{r}
#
new_res_adj <- new_res[order(new_res$padj),]
new_res_adj
```
saving these results table :
```{r}
write.csv(as.data.frame(new_res_adj),
file="C:/Users/kesha/Documents/R/Rtuts/Data/WG__Discussion_Alina's_EPEC_project/results/condition_infected_vs_control_dge_withthresholdsimposed.csv")
```
# Exploratory Data analysis
```{r}
plotMA(new_res)
```
```{r}
plotDispEsts(dds1)
```
### Calculating sample distances is to use the Poisson Distance
This measure of dissimilarity between counts also takes the inherent 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.
```{r}
poisd <- PoissonDistance(t(counts(dds1)))
print(poisd)
```
```{r}
samplePoisDistMatrix <- as.matrix( poisd$dd )
dim(samplePoisDistMatrix)
#rownames(samplePoisDistMatrix) <- paste( dds1$dex, dds1$cell, sep=" - " )
#colnames(samplePoisDistMatrix) <- NULL
```
```{r}
pheatmap(samplePoisDistMatrix,
clustering_distance_rows = poisd$dd,
clustering_distance_cols = poisd$dd,
col = colors)
```
## Log fold change shrinkage for visualization and ranking
```{r}
resLFC <- lfcShrink(dds1, coef="condition_Infected_vs_control", type="apeglm")
resLFC
```
```{r}
plotMA(resLFC)
```
# Plotting Results
## Counts Plot
A quick way to visualize the counts for a particular gene is to use the plotCounts function that takes as arguments the DESeqDataSet, a gene name, and the group over which to plot the counts
```{r}
topGene <- rownames(res)[which.max(res_adj$padj)]
topGene
```
```{r}
plotCounts(dds1, gene = topGene, intgroup="condition")
```
### Normalized counts for a single gene over treatment group
```{r}
library("ggbeeswarm")
geneCounts <- plotCounts(dds1, gene = topGene,
intgroup="condition",
returnData = TRUE)
geneCounts
```
```{r}
ggplot(geneCounts, aes(x = condition, y = count, color = condition)) +
scale_y_log10() + geom_beeswarm(cex = 3)
```
# MDS Plot
# MA Plot
## Gene Clustering
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.For demonstration, let us select the 20 genes with the highest variance across samples.
```{r}
library("genefilter")
topVarGenes <- head(order(rowVars(assay(vsd)), decreasing = TRUE), 20)
topVarGenes
```
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 (figure below). We provide a data.frame that instructs the pheatmap function how to label the columns.
```{r}
mat <- assay(vsd)[ topVarGenes, ]
mat <- mat - rowMeans(mat)
print(mat)
anno <- as.data.frame(colData(vsd)[, "condition"])
```
```{r}
all(rownames(anno) %in% colnames(assay(vsd)))
# this needs to match in order for the genen clustering to take place
```
### Volcano Plots
```{r}
EnhancedVolcano(new_res,
lab = rownames(new_res),
x = 'log2FoldChange',
y = 'pvalue',
ylim = c(0, 40),
pCutoff = 0.05,
FCcutoff = 1.0,
pointSize = 3.0,
labSize = 3.0,
labCol = 'black',
labFace = 'bold',
boxedLabels = TRUE,
colAlpha = 1,
legendLabSize = 10,
legendIconSize = 4.0,
drawConnectors = TRUE,
widthConnectors = 0.75)
```