-
Notifications
You must be signed in to change notification settings - Fork 0
/
deseq_analysis.R
executable file
·492 lines (357 loc) · 17 KB
/
deseq_analysis.R
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
## Setting up working directory
rm(list=ls())
setwd("~/git_repositories/Tag_seq_javanica/Input_files")
#if (!requireNamespace("BiocManager", quietly = TRUE))
#install.packages("BiocManager")
BiocManager::install("vsn")
#install.packages("ggVennDiagram")
#if (!requireNamespace("BiocManager", quietly = TRUE))
#install.packages("BiocManager")
BiocManager::install("DESeq2")
install.packages("BiocManager")
#BiocManager::install("rrvgo")
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("EnhancedVolcano")
## Getting all the libraries that we need
library(DESeq2)
library(RColorBrewer)
library(EnhancedVolcano)
library(vsn)
library(ggplot2)
library(pheatmap)
library(tidyr)
library(gplots)
library(VennDiagram)
library(RColorBrewer)
#library(rrvgo)
library(topGO)
library(dplyr)
library(tibble)
# Getting the read count data
exp_data <- read.delim("only_javanica.txt", row.names = 1, sep = "\t")
#rownames(exp_data) <-gsub("transcript:", "",rownames(exp_data)) ## Removing the word transcript and replaces it with nothing
exp_data <- as.matrix(exp_data) #DESeq2 pipeline expects the count data as a matrix
head(exp_data)
dim(exp_data)
# Constructing metadata with rows corresponding to the columns of the count data (in this case exp_data)
treatment <- c("VW4","VW4","VW4","VW4","VW4","VW4","VW4_J2","VW4_J2", "VW4_J2", "VW5","VW5","VW5", "VW5", "VW5", "VW5", "VW5_J2", "VW5_J2", "VW5_J2" )
timepoint <- c(28, 28, 28, 7, 7, 7, 0, 0, 0, 28, 28, 28, 7, 7, 7, 0, 0, 0 )
group <- factor(paste0(treatment, ".", timepoint))
metadata <- data.frame("group" = group, row.names= colnames(exp_data))
metadata
table(metadata$group)
# Making DEseq data format
dds <- DESeqDataSetFromMatrix(exp_data, metadata, ~group)
#calculate the linear correction factors for each sample:
dds <- estimateSizeFactors(dds)
sizeFactors(dds)## gives the correction factors for each sample
normalized_dds <- counts(dds, normalized = TRUE)
# Data transformation and visualization
vsd <- vst(dds, blind = FALSE)
rld <- rlog(dds, blind = FALSE)
head(assay(vsd))
# PCA plot
pca_data <- plotPCA(vsd, intgroup = c("group"), returnData = TRUE)
percentVar <- round(100*attr(pca_data, "percentVar"))
ggplot(pca_data, aes(PC1, PC2, color = group, shape = group)) +
geom_point(size = 3) +
xlab(paste0("PC1: ", percentVar[1], "% variance")) +
ylab(paste0("PC1: ", percentVar[1], "% variance")) +
coord_fixed()
#Heat map
sampledistance <- dist(t(assay(vsd)))
SampledistanceMatrix <- as.matrix(sampledistance)
rownames(SampledistanceMatrix) <- paste(vsd$group)
colnames(SampledistanceMatrix) <- paste(vsd$group)
colors <- colorRampPalette(rev(brewer.pal(9, "Blues")) )(255)
pheatmap(SampledistanceMatrix,
clustering_distance_rows = sampledistance,
clustering_distance_cols = sampledistance,
col = colors)
## Differential expression analysis
dd1 <- DESeq(dds)
head(dd1)
dd1$group
########################################################################
## Comparing VW5_J2 against VW4_J2 (control)
res1 <- results(dd1, contrast = c("group","VW5_J2.0", "VW4_J2.0"), alpha = 0.05)
summary(res1)
#THERE ARE 134 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res1$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res1$pvalue))
EnhancedVolcano(res1,
lab = rownames(res1),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW5_J2 vs VW4_J2',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
#DESeq2 uses the so-called Benjamini-Hochberg (BH) adjustment; in brief, this method calculates for each gene an adjusted p value which answers the following question: if one called significant all genes with a p value less than or equal to this gene's p value threshold, what would be the fraction of false positives (the false discovery rate, FDR) among them (in the sense of the calculation outlined above)?
##These values, called the BH-adjusted p values, are given in the column padj of the results object.
##Hence, if we consider a fraction of 10% false positives acceptable, we can consider all genes with an adjusted p value below 10%=0.1 as significant. How many such genes are there?
sum(res1$padj<0.1, na.rm = TRUE) #20
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig1 <- res1[ which(res1$padj<0.1), ]
write.csv(resSig1, file = "DEgenes_VW5J2vsVW4J2.csv")
nrow(resSig1) #30 significant genes
#only down regulated genes
down_VW5_J2<- resSig1[order(resSig1$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW5_J2 <- subset(down_VW5_J2, log2FoldChange <=-1.5) #2 highly down regulated
head(down_VW5_J2)
#only up regulated genes
up_VW5_J2 <- resSig1[order(resSig1$log2FoldChange, decreasing = TRUE), ]
up_VW5_J2 <- subset(up_VW5_J2, log2FoldChange >=1.5) #8 highly up regulated
##############################################################################################################################################
#Comparing VW5_7days with VW4_J2
res2 <- results(dd1, contrast = c("group","VW5.7", "VW4_J2.0"), alpha = 0.05)
summary(res2)
#THERE ARE 575 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res2$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res2$pvalue))
EnhancedVolcano(res2,
lab = rownames(res2),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW5_7days vs VW4_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res1$padj<0.1, na.rm = TRUE) #20
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig2 <- res2[ which(res2$padj<0.1), ]
nrow(resSig2) #404 significant genes
write.csv(resSig2, file = "DEgenes_VW5_7vsVW4J2.csv")
#only down regulated genes
down_VW5_7<- resSig2[order(resSig2$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW5_7 <- subset(down_VW5_7, log2FoldChange <=-1.5) #166 highly down regulated
head(down_VW5_7)
#only up regulated genes
up_VW5_7 <- resSig2[order(resSig2$log2FoldChange, decreasing = TRUE), ]
up_VW5_7 <- subset(up_VW5_7, log2FoldChange >=1.5) #181 highly up regulated
###############################################################################################################################################
#Comparing VW5_28 days vs VW4_J2
res3 <- results(dd1, contrast = c("group","VW5.28", "VW4_J2.0"), alpha = 0.05)
summary(res3)
#THERE ARE 1148 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res3$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res3$pvalue))
EnhancedVolcano(res3,
lab = rownames(res3),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW5_28days vs VW4_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res3$padj<0.1, na.rm = TRUE) #1099
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig3 <- res3[ which(res3$padj<0.1), ]
nrow(resSig3) #404 significant genes
write.csv(resSig3, file = "DEgenes_VW5_28vsVW4J2.csv")
#only down regulated genes
down_VW5_28<- resSig3[order(resSig3$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW5_28 <- subset(down_VW5_28, log2FoldChange <=-1.5) #525 highly down regulated
nrow(down_VW5_28)
#only up regulated genes
up_VW5_28 <- resSig3[order(resSig3$log2FoldChange, decreasing = TRUE), ]
up_VW5_28 <- subset(up_VW5_28, log2FoldChange >=1.5) #164 highly up regulated
nrow(up_VW5_28)
##############################################################################################################################################################
#Comparing VW4_7days with VW4_J2
res4 <- results(dd1, contrast = c("group","VW4.7", "VW4_J2.0"), alpha = 0.05)
summary(res4)
#THERE ARE 527 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res4$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res4$pvalue))
EnhancedVolcano(res4,
lab = rownames(res4),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW4_7days vs VW4_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res4$padj<0.1, na.rm = TRUE) #366
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig4 <- res4[ which(res4$padj<0.1), ]
nrow(resSig4) # significant genes
write.csv(resSig4, file = "DEgenes_VW4_7vsVW4J2.csv")
#only down regulated genes
down_VW4_7<- resSig4[order(resSig4$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW4_7 <- subset(down_VW4_7, log2FoldChange <=-1.5) #145 highly down regulated
head(down_VW4_7)
nrow(down_VW4_7)
#only up regulated genes
up_VW4_7 <- resSig4[order(resSig4$log2FoldChange, decreasing = TRUE), ]
up_VW4_7 <- subset(up_VW4_7, log2FoldChange >=1.5) #162 highly up regulated
nrow(up_VW4_7)
#############################################################################################################################################################
res5 <- results(dd1, contrast = c("group","VW4.28", "VW4_J2.0"), alpha = 0.05)
summary(res4)
#THERE ARE 1128 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res5$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res5$pvalue))
EnhancedVolcano(res5,
lab = rownames(res5),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW4_28days vs VW4_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res5$padj<0.1, na.rm = TRUE) #1079 significant genes
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig5 <- res5[ which(res5$padj<0.1), ]
nrow(resSig5) # significant genes
write.csv(resSig5, file = "DEgenes_VW4_28vsVW4J2.csv")
#only down regulated genes
down_VW4_28<- resSig5[order(resSig5$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW4_28 <- subset(down_VW4_28, log2FoldChange <=-1.5) #518 highly down regulated
head(down_VW4_28)
nrow(down_VW4_28)
#only up regulated genes
up_VW4_28 <- resSig5[order(resSig5$log2FoldChange, decreasing = TRUE), ]
up_VW4_28 <- subset(up_VW4_28, log2FoldChange >=1.5) #169 highly up regulated
nrow(up_VW4_28)
##############################################################################################################################################################
res6 <- results(dd1, contrast = c("group","VW5.28", "VW5_J2.0"), alpha = 0.05)
summary(res6)
#THERE ARE 1106 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res6$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res6$pvalue))
EnhancedVolcano(res6,
lab = rownames(res6),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW5_28days vs VW5_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res6$padj<0.1, na.rm = TRUE) #1053 significant genes
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig6 <- res6[ which(res6$padj<0.1), ]
nrow(resSig6) #1053 significant genes
write.csv(resSig6, file = "DEgenes_VW5_28vsVW5J2.csv")
#only down regulated genes
down_VW5_28_VW5<- resSig6[order(resSig6$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW5_28_VW5 <- subset(down_VW5_28_VW5, log2FoldChange <=-1.5) #479 highly down regulated
head(down_VW5_28_VW5)
nrow(down_VW5_28_VW5)
#only up regulated genes
up_VW5_28_VW5 <- resSig6[order(resSig6$log2FoldChange, decreasing = TRUE), ]
up_VW5_28_VW5 <- subset(up_VW5_28_VW5, log2FoldChange >=1.5) #178 highly up regulated
nrow(up_VW5_28_VW5)
##############################################################################################################################################################
res7 <- results(dd1, contrast = c("group","VW5.7", "VW5_J2.0"), alpha = 0.05)
summary(res7)
#THERE ARE 561 GENES WITH pvalue below 0.05 among 7347 genes for which the test succeeded in reporting a p-value:
sum(res7$pvalue < 0.05 , na.rm = TRUE)
table(is.na(res7$pvalue))
EnhancedVolcano(res7,
lab = rownames(res7),
x = 'log2FoldChange',
y = 'pvalue',
xlim = c(-9, 9),
title = 'VW5_7days vs VW5_J2(control)',
subtitle = 'Differential Expression',
caption = 'FC cutoff, 1.5; p-value cutoff, 10e-5',
pCutoff = 10e-5,
FCcutoff = 1.5,
pointSize = 3.0,
labSize = 3.0)
sum(res7$padj<0.1, na.rm = TRUE) #416 significant genes
#Subseting the results table to these genes and then sort by log2 fold chage estimate to get the significant genes with the strongest down-regulation and up-regulation
resSig7 <- res7[ which(res7$padj<0.1), ]
nrow(resSig7) # 416 significant genes
write.csv(resSig7, file = "DEgenes_VW5_7daysvsVW5J2.csv")
#only down regulated genes
down_VW5_7_VW5<- resSig7[order(resSig7$log2FoldChange), ]
#sub-setting by log2foldchange <=-1.5
down_VW5_7_VW5 <- subset(down_VW5_7_VW5, log2FoldChange <=-1.5) #164 highly down regulated
head(down_VW5_7_VW5)
nrow(down_VW5_7_VW5)
#only up regulated genes
up_VW5_7_VW5<- resSig7[order(resSig7$log2FoldChange, decreasing = TRUE), ]
up_VW5_7_VW5 <- subset(up_VW5_7_VW5, log2FoldChange >=1.5) #185 highly up regulated
nrow(up_VW5_7_VW5)
##############################################################################################################################################################
# Helper function to display Venn diagram
display_venn <- function(x, ...){
library(VennDiagram)
grid.newpage()
venn_object <- venn.diagram(x, filename = NULL, ...)
grid.draw(venn_object)
}
## Venn Diagram to compare the differentially expressed genes
#sig_VW5J2 <- read.csv('DEgenes_VW5J2vsVW4J2.csv')
#sig_VW5_28 <- read.csv("DEgenes_VW5_28vsVW4J2.csv")
#sig_VW5_7 <- read.csv("DEgenes_VW5_7vsVW4J2.csv")
sig_VW4_28 <- read.csv("DEgenes_VW4_28vsVW4J2.csv")
nrow(sig_VW4_28)
sig_VW4_7 <- read.csv('DEgenes_VW4_7vsVW4J2.csv')
nrow(sig_VW4_7)
sig_VW5_7_VW5 <- read.csv("DEgenes_VW5_7daysvsVW5J2.csv")
nrow(sig_VW5_7_VW5)
sig_VW5_28_VW5 <- read.csv("DEgenes_VW5_28vsVW5J2.csv")
nrow(sig_VW5_28_VW5)
#set1 <-sig_VW5J2$X
#set2 <-sig_VW5_28$X
#set3<- sig_VW5_7$X
set4<- sig_VW4_28$X
set5<- sig_VW4_7$X
set6<- sig_VW5_28_VW5$X
set7<- sig_VW5_7_VW5$X
display.brewer.all(colorblindFriendly = TRUE)
brewer.pal(n =8, name ="Set2" )
display_venn(x = list(set5, set7), category.names = c("7dpi_VW4", "7dpi_VW5"),
# Circles
lwd = 2,
lty = 'blank',
fill = c("#FC8D62" ,"#8DA0CB"))
display_venn(x = list(set4, set6), category.names = c("28dpi_VW4", "28dpi_VW5"),
# Circles
lwd = 2,
lty = 'blank',
fill = c("#66C2A5" ,"#E78AC3"))
display_venn(x = list(set5, set4), category.names = c("7dpi_VW5", "28dpi_VW5"),
# Circles
lwd = 2,
lty = 'blank',
fill = c("#FC8D62" ,"#8DA0CB"))
display_venn(x = list(set7, set6), category.names = c("7dpi_VW4", "28dpi_VW4"),
# Circles
lwd = 2,
lty = 'blank',
fill = c("#FC8D62" ,"#8DA0CB"))