-
Notifications
You must be signed in to change notification settings - Fork 1
/
sestrin_pathway_figures [v3].Rmd
195 lines (171 loc) · 8.02 KB
/
sestrin_pathway_figures [v3].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
---
title: "Sestrin Pathway Figures"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(ggpubr)
library(dplyr)
library(tibble)
library(enrichR)
library(Hmisc)
```
```{r}
setwd("C:/Users/NOBEL/Box/Lab Notes/Lab Notes Benjamin/Sestrins/Mouse RNA-Seq")
```
```{r}
up_tab = read.table("./nosva_v4/d0_sig0.01_up_l2fc1.csv", sep=",", header = TRUE, stringsAsFactors = FALSE)
down_tab = read.table("./nosva_v4/d0_sig0.01_down_l2fc1.csv", sep=",", header = TRUE, stringsAsFactors = FALSE)
head(up_tab)
```
```{r}
up_enrich = enrichr(up_tab$Symbol[1:100], c("GO_Molecular_Function_2018", "GO_Cellular_Component_2018", "GO_Biological_Process_2018", "KEGG_2019_Mouse"))
up_enrich_kegg = subset(up_enrich[["KEGG_2019_Mouse"]], subset=P.value<0.05)
up_enrich_kegg$log10pval = -log10(up_enrich_kegg$P.value)
up_enrich_cs = up_enrich_kegg[order(as.numeric(up_enrich_kegg$Combined.Score), decreasing=TRUE),]
up_enrich_pval = up_enrich_kegg[order(as.numeric(up_enrich_kegg$P.value), decreasing=FALSE),]
up_enrich_cs
up_enrich_pval
```
```{r}
down_enrich = enrichr(down_tab$Symbol[1:100], c("GO_Molecular_Function_2018", "GO_Cellular_Component_2018", "GO_Biological_Process_2018", "KEGG_2019_Mouse"))
down_enrich_kegg = subset(down_enrich[["KEGG_2019_Mouse"]], subset=P.value<0.05)
down_enrich_kegg$log10pval = -log10(down_enrich_kegg$P.value)
down_enrich_cs = down_enrich_kegg[order(as.numeric(down_enrich_kegg$Combined.Score), decreasing=TRUE),]
down_enrich_pval = down_enrich_kegg[order(as.numeric(down_enrich_kegg$P.value), decreasing=FALSE),]
down_enrich_cs
down_enrich_pval
```
```{r}
saveRDS(down_enrich, "./nosva_v4/RDS/down_enrichR_d0_lfc1.RDS")
saveRDS(up_enrich, "./nosva_v4/RDS/up_enrichR_d0_lfc1.RDS")
```
```{r}
# all genes in enriched KEGG terms
up_kegg_genes = unique(unlist(lapply(up_enrich_pval$Genes, function(x) strsplit(x, split=";"))))
down_kegg_genes = unique(unlist(lapply(down_enrich_pval$Genes, function(x) strsplit(x, split=";"))))
# create empty contingency table
up_kegg_gene_matrix = matrix(ncol=length(up_kegg_genes), nrow=length(up_enrich_cs$Term))
down_kegg_gene_matrix = matrix(ncol=length(down_kegg_genes), nrow=length(down_enrich_cs$Term))
# fill contingency tables and melt
for(gene in 1:length(up_kegg_genes)){
up_kegg_gene_matrix[grep(up_kegg_genes[gene], up_enrich_cs$Genes, fixed=TRUE), gene] = 1
}
rownames(up_kegg_gene_matrix) = up_enrich_cs$Term
colnames(up_kegg_gene_matrix) = up_kegg_genes
up_kegg_gene_matrix = melt(up_kegg_gene_matrix)
head(up_kegg_gene_matrix)
for(gene in 1:length(down_kegg_genes)){
down_kegg_gene_matrix[grep(down_kegg_genes[gene], down_enrich_cs$Genes, fixed=TRUE), gene] = 1
}
rownames(down_kegg_gene_matrix) = down_enrich_cs$Term
colnames(down_kegg_gene_matrix) = down_kegg_genes
down_kegg_gene_matrix = melt(down_kegg_gene_matrix)
head(down_kegg_gene_matrix)
```
```{r}
ggplot(up_kegg_gene_matrix, aes(x = Var2, y = Var1)) +
geom_raster(aes(fill=value)) +
scale_fill_gradient(low="grey90", high="red") +
labs(x="letters", y="LETTERS", title="Matrix") +
theme_bw() + theme(axis.text.x=element_text(size=9, angle=90, vjust=0.3),
axis.text.y=element_text(size=9),
plot.title=element_text(size=11))
```
```{r}
up_kegg_gene_pval = up_tab[match(capitalize(tolower(up_kegg_genes)), up_tab$Symbol),"padj"] # get pvalues for genes in kegg table
names(up_kegg_gene_pval) = up_tab$Symbol[match(capitalize(tolower(up_kegg_genes)), up_tab$Symbol)] # assign gene symbols as names
up_kegg_gene_l2fc = up_tab[match(capitalize(tolower(up_kegg_genes)), up_tab$Symbol),"log2FoldChange"] # repeat for log2foldchanges
names(up_kegg_gene_l2fc) = up_tab$Symbol[match(capitalize(tolower(up_kegg_genes)), up_tab$Symbol)]
up_kegg_gene_matrix$pval = up_kegg_gene_pval[capitalize(tolower(up_kegg_gene_matrix$Var2))] # assign pvalues to kegg table
up_kegg_gene_matrix$l2fc = up_kegg_gene_l2fc[capitalize(tolower(up_kegg_gene_matrix$Var2))]
up_kegg_gene_matrix$log10padj = -log10(up_kegg_gene_matrix$pval) * up_kegg_gene_matrix$value # only keep values for genes in pathway
up_kegg_gene_matrix$l2fc = up_kegg_gene_matrix$l2fc * up_kegg_gene_matrix$value
up_kegg_gene_matrix
down_kegg_gene_pval = down_tab[match(capitalize(tolower(down_kegg_genes)), down_tab$Symbol),"padj"]
names(down_kegg_gene_pval) = down_tab$Symbol[match(capitalize(tolower(down_kegg_genes)), down_tab$Symbol)]
down_kegg_gene_l2fc = down_tab[match(capitalize(tolower(down_kegg_genes)), down_tab$Symbol),"log2FoldChange"]
names(down_kegg_gene_l2fc) = down_tab$Symbol[match(capitalize(tolower(down_kegg_genes)), down_tab$Symbol)]
down_kegg_gene_matrix$pval = down_kegg_gene_pval[capitalize(tolower(down_kegg_gene_matrix$Var2))]
down_kegg_gene_matrix$l2fc = down_kegg_gene_l2fc[capitalize(tolower(down_kegg_gene_matrix$Var2))]
down_kegg_gene_matrix$log10padj = -log10(down_kegg_gene_matrix$pval) * down_kegg_gene_matrix$value # only keep values for genes in pathway
down_kegg_gene_matrix$l2fc = down_kegg_gene_matrix$l2fc * down_kegg_gene_matrix$value
down_kegg_gene_matrix
```
```{r}
up_kegg_plot = ggplot(up_kegg_gene_matrix, aes(Var2, Var1)) +
geom_point(aes(size=l2fc, color=log10padj)) +
theme_bw() +
labs_pubr() +
theme(axis.text.x=element_text(size=9, angle=90, hjust=1),
axis.text.y=element_text(size=11),
plot.title=element_text(size=11)) +
scale_colour_gradient(low="gray50", high="blue") +
xlab("") +
ylab("") +
guides(color = guide_colorbar(order=1),
size = guide_legend(order=2))
up_kegg_plot
ggsave(up_kegg_plot, filename="./nosva_v4/Figures/up_kegg_dotplot.tiff", units="in", width=8, height=3.5, dpi=320)
```
```{r}
down_kegg_plot = ggplot(down_kegg_gene_matrix, aes(Var2, Var1)) +
geom_point(aes(size=-l2fc, color=log10padj)) +
theme_bw() +
labs_pubr() +
theme(axis.text.x=element_text(size=9, angle=90, hjust=1),
axis.text.y=element_text(size=11),
plot.title=element_text(size=11)) +
scale_colour_gradient(low="gray50", high="red") +
xlab("") +
ylab("") +
guides(color = guide_colorbar(order=1),
size = guide_legend(order=2))
ggsave(down_kegg_plot, filename="./nosva_v4/Figures/down_kegg_dotplot.tiff", units="in", width=8.5, height=3.75, dpi=320)
```
```{r}
#$color=ifelse(l2fc>0, "red", "blue")
total_kegg_gene_matrix = rbind(up_kegg_gene_matrix, down_kegg_gene_matrix)
total_kegg_plot = ggplot(total_kegg_gene_matrix, aes(Var2, Var1)) +
geom_point(aes(color=ifelse(l2fc>0, "red", "blue"), group=l2fc, size=log10padj, alpha=0.8)) +
theme_bw() +
labs_pubr() +
theme(axis.text.x=element_text(size=9, angle=90, hjust=1),
axis.text.y=element_text(size=11),
plot.title=element_text(size=11)) +
xlab("") +
ylab("") +
guides(color = guide_colorbar(order=1),
size = guide_legend(order=2))
total_kegg_plot
ggsave(total_kegg_plot, filename="./nosva_v4/Figures/total_kegg_plot.tiff", units="in", width=12, height=6, dpi=320)
```
```{r}
sum_kegg_tab = rbind(up_enrich_pval, down_enrich_pval)
sum_kegg_tab$reg = factor(c(rep("Up",nrow(up_enrich_pval)), rep("Down",nrow(down_enrich_pval))), levels = c("Down","Up"))
sum_kegg_tab$count = unlist(lapply(unlist(sum_kegg_tab$Genes), function(x) length(unlist(strsplit(unlist(x),";")))))
sum_kegg_tab
sum_kegg_dot = ggdotchart(sum_kegg_tab, x = "Term", y="log10pval",
size="count", group="reg", color="reg",
rotate=TRUE, sorting="descending",
palette="lancet", y.text.col = TRUE)
sum_kegg_dot = ggpar(sum_kegg_dot,
ylab = "-log10(padj)", xlab = "KEGG Term",
font.y = c(12, "bold"), font.x = c(12, "bold"),
font.ytickslab = c(11, "bold"),
font.xtickslab = c(11, "bold"),
legend = "right",
legend.title = c("Regulation"), font.legend = c(12, "bold"))
sum_kegg_dot
ggsave(sum_kegg_dot, filename="./nosva_v4/Figures/d0_lfc1_dot_kegg_enrichR.tiff", units="in", width=8, height=5, dpi=320)
```
# GO Term Clustering
```{r}
down_enrich_gobp = down_enrich[["GO_Biological_Process_2018"]]
up_enrich_gobp = up_enrich[["GO_Biological_Process_2018"]]
up_enrich_gobp
down_enrich_gobp
```