-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDGE.R
261 lines (188 loc) · 8.42 KB
/
DGE.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
#############################################################
# Rscript for Differential Gene Expression Analysis using #
# DESeq2, EdgeR and Limma #
# -Arindam Ghosh (16 November 2018) #
#############################################################
library(edgeR)
library(DESeq2)
library(geneplotter)
library(gplots)
library(ggplot2)
library(pheatmap)
library(UpSetR)
library(RColorBrewer)
#Read count table and extract counts for Protein coding genes
#$$$ Change file name here
counts = read.table("PRJNA230824_allCounts_noOver_noMulti.tsv", row.names=c(1), header=T)
sprintf ("Total Genes")
dim(counts)
#$$$ Change path here
protein_coding = as.matrix(read.table("/home/bioinfo/Documents/Work/reference_genome/Human_84/Annotation/gff/protein_coding"))
sprintf ("Total Protein Coding Genes")
dim(protein_coding)
counts_pc <- counts[protein_coding,]
dim(counts_pc)
# Filtering out genes which donot have 1 cpm atleast in two samples
keep <- rowSums(cpm(counts_pc)>1) >1
counts_pc_filt <- counts_pc[keep, ]
sprintf ("Number of genes after filtering low count genes")
dim(counts_pc_filt)
#$$$ Change number of samples here
condition = c(rep("T",4), rep("C",2))
coldata <- data.frame(row.names=colnames(counts_pc_filt), condition)
sprintf ("Coldata")
coldata
# DESEQ2 Analysis
dds = DESeqDataSetFromMatrix(countData=counts_pc_filt, colData=coldata, design=~condition)
dds = DESeq(dds)
res = results(dds)
sprintf ("DESeq2 results")
dim(res)
#$$$ Change file name here (3)
write.table(res, "DS5_DESeq_result.tsv", sep="\t", col.names=NA)
res_clean = na.exclude(as.data.frame(res))
sprintf ("Number of genes after NA exclude (DESeq)")
dim(res_clean)
upreg = res_clean[(res_clean$log2FoldChange>1 & res_clean$padj<0.01),]
write.table(upreg, "DS5_DESeq_upreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of upregulated genes (DESeq2)")
dim(upreg)
downreg = res_clean[(res_clean$log2FoldChange<(-1) & res_clean$padj<0.01),]
write.table(downreg, "DS5_DESeq_downreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of downregulated genes (DESeq2)")
dim(downreg)
# Volcano Plot
#$$$ Change file name here
png(filename="DS5_volcano.png")
with(res_clean, plot(log2FoldChange, -log10(padj), pch=20, main="Volcano Plot", col="grey", xlim=c(-10,10)))
with(subset(res_clean,padj<0.01 & log2FoldChange>1),points(log2FoldChange, -log10(padj), pch=20, col="green"))
with(subset(res_clean,padj<0.01 & log2FoldChange<(-1)),points(log2FoldChange, -log10(padj), pch=20, col="red"))
abline(h=2, lty=2)
abline(v=-1, lty=2)
abline(v=1, lty=2)
dev.off()
# ECDF Plot
#$$$ Change file name here
png(filename="DS5_ecdf.png", height=1020, width=1020, units='px')
multiecdf(counts(dds, normalized=TRUE)[,], xlab="MeanCounts", xlim=c(0,1000))
dev.off()
# Plot Density
#$$$ Change file name here
png(filename="DS5_density.png", height=1020, width=1020, units='px')
multidensity(counts(dds, normalized=TRUE)[,], xlab="MeanCounts", xlim=c(0,1000))
dev.off()
# PLOT SAMPLE TO SAMPLE DISTANCE
#$$$ Change file name here (2)
rld = rlogTransformation(dds, blind=T)
png(filename="DS5_sample_heatmap1.png", height=1020, width=1020, units='px')
distRL = dist(t(assay(rld)))
mat=as.matrix(distRL)
hmcol = colorRampPalette(brewer.pal(9,"GnBu"))(100)
heatmap.2(mat, trace="none", col=rev(hmcol), margin=c(13,13))
dev.off()
png(filename="DS5_sample_heatmap2.png", height=1020, width=1020, units='px')
pheatmap(mat, clustering_distance_rows=distRL, clustering_distance_cols=distRL, col=rev(hmcol))
dev.off()
# PCA
#$$$ Change file name here ()
png(filename="DS5_pca1.png")
pca = DESeq2::plotPCA(rld, intgroup=c("condition"), returnData=TRUE)
p<-ggplot(pca,aes(x=PC1,y=PC2,color=group, label=row.names(pca) ))
p<-p+geom_point()
p
dev.off()
png(filename="DS5_pca2.png")
plotPCA(rld, intgroup=c("condition"))
dev.off()
# PLOT DISPERSION ESTIMATE
#$$$ Change file name here
png(filename="DS5_Dispersion.png", height=1020, width=1020, units='px')
plotDispEsts(dds)
dev.off()
# MA Plot
#$$$ Change file name here
png(filename="DS5_MA.png", height=1020, width=1020, units='px')
plotMA(res)
dev.off()
# Box-plot of Normalization
#$$$ Change file name here (3)
png(filename="DS5_boxplot_normalized.png",width = 1500, height= 1020, units="px")
#$$$ Change sample size
colors = c(rep("green",4),rep("blue",2))
boxplot(log2(counts(dds, normalized=TRUE)+1), col=colors, outline = FALSE, main="Box-plot of Normalized counts", xlab="Samples", ylab="log transformed normalized counts")
legend("topright", inset=0, title="Sample type", c("Pluripotent","Non-pluripotent"), fill=c("green","blue"), cex=0.8)
dev.off()
png(filename="DS5_boxplot_unnormalized.png",width = 1500, height= 1020, units="px")
boxplot(log2(counts_pc_filt+1), col=colors, outline = FALSE, main="Box-plot of Un-normalized counts", xlab="Samples", ylab="log transformed normalized counts")
legend("topright", inset=0, title="Sample type", c("Pluripotent","Non-pluripotent"), fill=c("green","blue"), cex=0.8)
dev.off()
png(filename="DS5_boxplot_normalized_rld.png",width = 1500, height= 1020, units="px")
#$$$ Change sample size
colors = c(rep("green",4),rep("blue",2))
boxplot(assay(rld), col=colors, outline = FALSE, main="Box-plot of Normalized counts", xlab="Samples", ylab="log transformed normalized counts")
legend("topright", inset=0, title="Sample type", c("Pluripotent","Non-pluripotent"), fill=c("green","blue"), cex=0.8)
dev.off()
#$$$ Change file name here
save (dds, file = 'DS5_dds.rda')
# EDGER
#$$$ Change sample size
sample_info.edgeR <- factor(c(rep("T", 4), rep("C",2)))
sample_info.edgeR <- relevel(sample_info.edgeR, ref="C")
sprintf ("EdgeR sample info")
sample_info.edgeR
#$$$ Change file name here (4)
edgeR.DGElist <- DGEList(counts= counts_pc_filt, group=sample_info.edgeR)
sprintf ("Genes for edgeR")
dim(edgeR.DGElist$counts)
edgeR.DGElist <- calcNormFactors(edgeR.DGElist, method="TMM")
edgeR.DGElist$samples
design <- model.matrix(~sample_info.edgeR)
sprintf ("edgeR design")
design
edgeR.DGElist <- estimateDisp(edgeR.DGElist, design)
edger_fit <- glmFit(edgeR.DGElist, design)
edger_lrt <- glmLRT(edger_fit)
DGE.results_edgeR <- topTags(edger_lrt, n=Inf, sort.by = "none", adjust.method = "BH")
write.table(DGE.results_edgeR$table, "DS5_EdgeR_result.tsv", sep="\t", col.names=NA)
edger.upreg = DGE.results_edgeR$table[(DGE.results_edgeR$table$logFC>1 & DGE.results_edgeR$table$FDR<0.01),]
write.table(edger.upreg, "DS5_EdgeR_upreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of upregulated genes (edgeR)")
dim(edger.upreg)
edger.downreg = DGE.results_edgeR$table[(DGE.results_edgeR$table$logFC<(-1) & DGE.results_edgeR$table$FDR<0.01),]
write.table(edger.downreg, "DS5_EdgeR_downreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of downregulated genes (edgeR)")
dim(edger.downreg)
#$$$ Change file name here
save (edger_lrt, file = 'DS5_edgeLRT.rda')
# limma
rownames(design) <- colnames(edgeR.DGElist)
voomTransformed <- voom(edgeR.DGElist, design, plot=FALSE)
voom.fitted <- lmFit(voomTransformed, design = design)
voom.fitted <- eBayes(voom.fitted)
colnames(design)
#$$$ Change file name here (3)
DGE.results_limma <- topTable(voom.fitted, coef="sample_info.edgeRT", number=Inf, adjust.method="BH", sort.by="none")
write.table(DGE.results_limma, "DS5_limma_result.tsv", sep="\t", col.names=NA)
#$$$ Change file name here (2)
limma.upreg = DGE.results_limma[(DGE.results_limma$logFC>1 & DGE.results_limma$adj.P.Val<0.01),]
write.table(limma.upreg, "DS5_limma_upreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of upregulated genes (limma)")
dim(limma.upreg)
limma.downreg = DGE.results_limma[(DGE.results_limma$logFC<(-1) & DGE.results_limma$adj.P.Val<0.01),]
write.table(limma.downreg, "DS5_limma_downreg.tsv", sep="\t", col.names=NA)
sprintf ("Number of downregulated genes (limma)")
dim(limma.downreg)
#$$$ Change file name here
save (voom.fitted, file = 'DS5_VoomFitted.rda')
# Results comparision
#$$$ Change file name here (2)
upreg_list <- list(DESeq2 = rownames(upreg), edgeR = rownames(edger.upreg), limma = rownames(limma.upreg))
upreg_gns <- UpSetR::fromList(upreg_list)
png(filename="DS5_upreg_comparision.png", height=720, width=720, units='px')
UpSetR::upset(upreg_gns, order.by="freq", text.scale = 2)
dev.off()
downreg_list <- list(DESeq2.downreg =rownames(downreg), limma.downreg=rownames(limma.downreg), edger.downreg=rownames(edger.downreg))
downreg_gns = fromList(downreg_list)
png(filename="DS5_downreg_comparision.png", height=720, width=720, units='px')
upset(downreg_gns, order.by="freq", text.scale = 2)
dev.off()