-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDE_Ruhland.R
More file actions
226 lines (152 loc) · 6.35 KB
/
Copy pathDE_Ruhland.R
File metadata and controls
226 lines (152 loc) · 6.35 KB
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
####################
# Ruhland dataset
###################
library(tidyverse)
library(DESeq2)
library(ggplot2)
library(pheatmap)
#BiocManager::install("apeglm")
library(apeglm)
df <- read.csv("../052_r_featureCounts_trimmed_Ruhland.counts.extraAttributes.txt", sep = "\t", skip = 1)
head(df)
names(df)
idx.num <- 14:19
sample_names <- c("EtOH_1", "EtOH_2", "EtOH_3", "TAM_1", "TAM_2", "TAM_3")
cbind(names(df[idx.num]), sample_names)
colnames(df)[idx.num] <- sample_names
names(df)
dim(df)
table(df$gene_biotype)
# Remove extra variance by subsetting for protein_coding
keep_gene <- which(df$gene_biotype == 'protein_coding')
df2 <- df[keep_gene,]
dim(df2)
# Went from 78348 to 21818
# 1. Set your filtering rules
GroupSize <- 3
minReads <- 10
# 2. Check each row of your raw counts data frame
# (This counts how many samples in each row have at least 10 reads)
keep <- rowSums(df2[,idx.num] >= minReads) >= GroupSize
# 3. Filter the data frame to keep only those rows
filtered_df <- df2[keep, ]
#note: levels let's us define the reference levels
treatment <- factor( c(rep("EtOH",3), rep("TAM",3)), levels=c("EtOH", "TAM") )
colData <- data.frame(treatment, row.names = colnames(filtered_df)[idx.num])
colData
numM <- filtered_df[,idx.num]
rownames(numM) <- filtered_df$Geneid
dds <- DESeqDataSetFromMatrix(
countData = numM, colData = colData,
design = ~ treatment)
dim(dds)
dds <- DESeq(dds)
vsd <- varianceStabilizingTransformation(dds)
pcaData <- plotPCA(vsd, intgroup=c("treatment"))
#png(filename = "docs/assets/images/DESeq2_mouseMT/mouseMT_pca1.png", units = "in", height = 4, width = 5, res = 600)
pcaData + geom_label(aes(x=PC1,y=PC2,label=name))
#dev.off()
res <- results(dds)
summary(res)
## What to do with outliers?
outlier <- which(is.na(res$padj))
pheatmap::pheatmap(assay(vsd)[outlier,],
show_rownames = TRUE,
#labels_row = paste0(filtered_df$Geneid[outlier], " - ", filtered_df$gene_symbol[outlier]),
#filename = '../05_figures/05_heatCheck_outliers_scaled.png' ,
#width = 7,
#height = 16,
scale = "row")
# Remove outliers
filtered_df_noOutliers <- filtered_df[-outlier, ]
numM <- filtered_df_noOutliers[,idx.num]
rownames(numM) <- filtered_df_noOutliers$Geneid
dds <- DESeqDataSetFromMatrix(
countData = numM, colData = colData,
design = ~ treatment)
dim(dds)
dds <- DESeq(dds)
vsd <- varianceStabilizingTransformation(dds)
pcaData <- plotPCA(vsd, intgroup=c("treatment"))
#png(filename = "docs/assets/images/DESeq2_mouseMT/mouseMT_pca1.png", units = "in", height = 4, width = 5, res = 600)
pcaData + geom_label(aes(x=PC1,y=PC2,label=name))
#dev.off()
res <- results(dds)
summary(res)
head(coef(dds)) # the second column corresponds to the difference between the 2 conditions
FDRthreshold = 0.05
logFCthreshold = 1
# add a column of NAs
res.lfc <- lfcShrink(dds, coef=2, res=res)
res.lfc$diffexpressed <- "NO"
# if log2Foldchange > 1 and pvalue < 0.01, set as "UP"
res.lfc$diffexpressed[res.lfc$log2FoldChange > logFCthreshold & res.lfc$padj < FDRthreshold] <- "UP"
# if log2Foldchange < 1 and pvalue < 0.01, set as "DOWN"
res.lfc$diffexpressed[res.lfc$log2FoldChange < -logFCthreshold & res.lfc$padj < FDRthreshold] <- "DOWN"
plot <- ggplot( data = data.frame( res.lfc ) , aes( x=log2FoldChange , y = -log10(padj) , col =diffexpressed ) ) +
geom_point() +
geom_vline(xintercept=c(-logFCthreshold, logFCthreshold), col="red") +
geom_hline(yintercept=-log10(FDRthreshold), col="red") +
scale_color_manual(values=c("blue", "grey", "red"))
plot
table(res.lfc$diffexpressed)
res$diffexpressed <- "NO"
# if log2Foldchange > 1 and pvalue < 0.01, set as "UP"
res$diffexpressed[res$log2FoldChange > logFCthreshold & res$padj < FDRthreshold] <- "UP"
# if log2Foldchange < 1 and pvalue < 0.01, set as "DOWN"
res$diffexpressed[res$log2FoldChange < -logFCthreshold & res$padj < FDRthreshold] <- "DOWN"
plot <- ggplot( data = data.frame( res ) , aes( x=log2FoldChange , y = -log10(padj) , col =diffexpressed ) ) +
geom_point() +
geom_vline(xintercept=c(-logFCthreshold, logFCthreshold), col="red") +
geom_hline(yintercept=-log10(FDRthreshold), col="red") +
scale_color_manual(values=c("blue", "grey", "red"))
plot
table(res$diffexpressed)
#ggsave(plot, filename = "docs/assets/images/DESeq2_mouseMT/mouseMT_volcano.png", dpi = 600)
vsd.counts <- assay(vsd)
topVarGenes <- head(order(rowVars(vsd.counts), decreasing = TRUE), 20)
mat <- vsd.counts[ topVarGenes, ] #scaled counts of the top genes
#png(filename = "docs/assets/images/DESeq2_mouseMT/mouseMT_heatmap.png", units = "in", height = 4, width = 6, res = 600)
pheatmap(mat,
scale = "row")
#dev.off()
master <- cbind(filtered_df_noOutliers, res)
write.csv(master, file = "051_r_Ruhland.DESeq2.results.csv", row.names = FALSE)
library(clusterProfiler)
library(org.Mm.eg.db)
allGenes <- master$Geneid
genes_universe <- bitr(allGenes, fromType = "ENSEMBL",
toType = c("ENTREZID", "SYMBOL"),
OrgDb = "org.Mm.eg.db")
head(genes_universe)
sig <- master[master$padj < 0.05, ]
sigGenes <- sig$Geneid
genes_DE <- bitr(sigGenes, fromType = "ENSEMBL",
toType = c("ENTREZID", "SYMBOL"),
OrgDb = "org.Mm.eg.db")
head(genes_DE)
# GO "biological process (BP)" enrichment
ego_bp <- enrichGO(gene = as.character(unique(genes_DE$ENTREZID)),
universe = as.character(unique(genes_universe$ENTREZID)),
OrgDb = org.Mm.eg.db,
ont = "BP",
pAdjustMethod = "BH",
pvalueCutoff = 0.01,
qvalueCutoff = 0.05,
readable = TRUE)
# couple of minutes to run
head(ego_bp)
dotplot(ego_bp)
# sample plot, but with adjusted p-value as x-axis
#dotplot(ego_bp, x = "p.adjust", showCategory = 20)
#BiocManager::install("ReactomePA")
library(ReactomePA)
# Reactome pathways enrichment
reactome.enrich <- enrichPathway(gene=as.character(unique(genes_DE$ENTREZID)),
organism = "mouse",
pAdjustMethod = "BH",
qvalueCutoff = 0.9,
readable=T,
universe = genes_universe$ENTREZID)
# <1 minute to run
dotplot(reactome.enrich, x = "p.adjust")