-
Notifications
You must be signed in to change notification settings - Fork 1
/
RUN_ORA.R
218 lines (176 loc) · 7 KB
/
RUN_ORA.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
## Over-Representation Analysis with ClusterProfiler
# https://learn.gencore.bio.nyu.edu/rna-seq-analysis/over-representation-analysis/
##### Load Packages #####
source("FUN_Package_InstLoad.R")
PKG_Basic.set <- c("tidyverse","ggplot2","patchwork","DT")
PKG_BiocManager.set <- c("clusterProfiler","enrichplot","ggupset","limma")
FUN_Package_InstLoad(Basic.set = PKG_Basic.set, BiocManager.set = PKG_BiocManager.set)
# Annotations
# organism = "org.Dm.eg.db" ## Genome wide annotation for Fly
organism = "org.Hs.eg.db" ## Genome wide annotation for Human
# organism = "org.Mm.eg.db" ## Genome wide annotation for Mouse
library(organism, character.only = TRUE)
##### Function setting #####
## Call function
source("FUN_Beautify_ggplot.R")
##### Prepare Input #####
head(DEG_Extract.df)
## For the universe in clusterProfiler
# we want the log2 fold change
original_gene_list <- DEG_Extract.df$logFC
# name the vector
names(original_gene_list) <- DEG_Extract.df$Gene
# omit any NA values
ORA_GeneDiff_All <- na.omit(original_gene_list)
# sort the list in decreasing order (required for clusterProfiler)
ORA_GeneDiff_All = sort(ORA_GeneDiff_All, decreasing = TRUE)
rm(original_gene_list)
# ##---------------------------------------------##
# ## Use pipeline
# ORA_GeneDiff_All2 <- DEG_Extract.df %>% drop_na(.,3) %>% select(log2FoldChange) %>%
# unlist() %>% as.numeric()
# names(ORA_GeneDiff_All2) <- DEG_Extract.df %>% drop_na(.,3) %>% select(X)
# ORA_GeneDiff_All2 = sort(ORA_GeneDiff_All2, decreasing = TRUE)
# # Check
# sum(ORA_GeneDiff_All==ORA_GeneDiff_All2)
# ##---------------------------------------------##
## Gene list
# Exctract significant results (padj < 0.05)
# Sig_GeneExp.df = subset(DEG_Extract.df, FDR < 0.05)
Sig_GeneExp.df = subset(DEG_Extract.df, PValue < 0.05)
# From significant results, we want to filter on log2fold change
ORA_GeneList_Sig <- Sig_GeneExp.df$logFC
# Name the vector
names(ORA_GeneList_Sig) <- Sig_GeneExp.df$Gene
# omit NA values
ORA_GeneList_Sig <- na.omit(ORA_GeneList_Sig)
# filter on min log2fold change (log2FoldChange > 2)
ORA_GeneList_Sig <- names(ORA_GeneList_Sig)[abs(ORA_GeneList_Sig) > 1]
##### Create enrichGO object #####
## Create the object
Result_ORA_GO <- enrichGO(gene = ORA_GeneList_Sig,
universe = names(ORA_GeneDiff_All),
OrgDb = organism,
keyType = "SYMBOL", #'SYMBOL', #'ENSEMBL'
# http://bioconductor.org/help/course-materials/2014/useR2014/Integration.html
#readable = T,
ont = "BP",
pvalueCutoff = 0.05,
qvalueCutoff = 0.10)
# ##### Create enrichKEGG object #####
# ## Over-Representation Analysis with ClusterProfiler
# # https://learn.gencore.bio.nyu.edu/rna-seq-analysis/over-representation-analysis/
#
# ## Error
# ## Ref: https://www.biowolf.cn/Question/kegg_error.html
# install.packages("R.utils")
# R.utils::setOption("clusterProfiler.download.method","auto")
# library("clusterProfiler")
# kegg_organism = "hsa" # "hsa","mmu"
# ORA_KEGG_Result <- enrichKEGG(gene = ORA_GeneList_Sig,
# universe = names(ORA_GeneDiff_All),
# organism = kegg_organism,
# pvalueCutoff = 0.05,
# keyType = "ncbi-geneid")
#
# ##### Plot Chart #####
# ## Bar Chart
# barplot(ORA_KEGG_Result,
# showCategory = 10,
# title = "Enriched Pathways",
# font.size = 8)
# ## Dot Chart
# dotplot(ORA_KEGG_Result,
# showCategory = 10,
# title = "Enriched Pathways",
# font.size = 8)
# ## Category Netplot:
# # categorySize can be either 'pvalue' or 'geneNum'
# cnetplot(ORA_KEGG_Result, categorySize="pvalue", foldChange=ORA_GeneList_Sig)
#
#
# library(pathview)
#
# ## KEGG plot
# # Produce the native KEGG plot (PNG)
# mmu.p1 <- pathview(gene.data=ORA_GeneList_Sig, pathway.id="05235", species = kegg_organism, gene.idtype=gene.idtype.list[1])
#
# # Produce a different plot (PDF) (not displayed here)
# mmu.p2 <- pathview(gene.data=ORA_GeneList_Sig, pathway.id="05235", species = kegg_organism, gene.idtype=gene.idtype.list[1], kegg.native = F)
#
# knitr::include_graphics("mmu05235.pathview.png")
##### Outcome #####
## Upset Plot
GO_Upsetplot <- upsetplot(Result_ORA_GO)
GO_Upsetplot
# https://alanlee.fun/2022/01/08/introducing-upsetplot/
## Barplot
GO_Barplot <- barplot(Result_ORA_GO,
drop = TRUE,
showCategory = 10,
title = "GO Biological Pathways",
font.size = 8)
GO_Barplot
GO_Barplot <- GO_Barplot %>% FUN_BeautifyggPlot()
GO_Barplot
## Dotplot
GO_Dotplot <- dotplot(Result_ORA_GO)
GO_Dotplot
GO_Dotplot <- GO_Dotplot %>% FUN_BeautifyggPlot(LegPos = c(0.15, 0.65))
GO_Dotplot
## Encrichment map:
try({emapplot(Result_ORA_GO)})
#-----------------------------------------------------------------------------------------------#
## error
## https://github.com/YuLab-SMU/enrichplot/issues/79
## Solution 1
Result_ORA_GO <- pairwise_termsim(Result_ORA_GO)
GO_Emapplot <- emapplot(Result_ORA_GO)
GO_Emapplot
# ## Solution 2
# d <- GOSemSim::godata(organism, ont = "BP")
# compare_cluster_GO_emap <- enrichplot::pairwise_termsim(Result_ORA_GO, semData = d, method="Wang")
# emapplot(compare_cluster_GO_emap)
#-----------------------------------------------------------------------------------------------#
## Enriched GO induced graph:
GO_Goplot <- goplot(Result_ORA_GO, showCategory = 10)
GO_Goplot
## Category Netplot
# categorySize can be either 'pvalue' or 'geneNum'
GO_Cnetplot <- cnetplot(Result_ORA_GO, categorySize = "pvalue", foldChange = ORA_GeneDiff_All)
GO_Cnetplot
##### Export #####
ORA_Plot.lt <- list(Upsetplot=GO_Upsetplot, Barplot=GO_Barplot, Dotplot=GO_Dotplot,
Emapplot=GO_Emapplot, Goplot=GO_Goplot, Cnetplot=GO_Cnetplot)
## Export PDF file
pdf(
file = paste0(Save_Path,"/ORAResult_",SetExport_Name,".pdf"),
width = 10, height = 8
)
try({print(ORA_Plot.lt)})
dev.off()
## Export TIFF file
for (i in 1:length(ORA_Plot.lt)) {
try({
tiff(file = paste0(Save_Path,"/ORAResult_",names(ORA_Plot.lt)[i],"_",SetExport_Name,".tif"),
width = 27, height = 27, units = "cm", res = 200)
print(ORA_Plot.lt[i])
graphics.off()
})
}
rm(i)
rm(organism)
rm(GO_Upsetplot, GO_Barplot, GO_Dotplot, GO_Emapplot, GO_Goplot,GO_Cnetplot,
Sig_GeneExp.df)
# ##### Error part (to be corrected) #####
#
# ##### Wordcloud #####
# # install.packages("wordcloud")
# library(wordcloud)
#
# ## Wordcloud
# wcdf<-read.table(text=Result_ORA_GO$GeneRatio, sep = "/")[1]
# wcdf$term<-Result_ORA_GO[,2]
# wordcloud(words = wcdf$term, freq = wcdf$V1, scale=(c(4, .1)), colors=brewer.pal(8, "Dark2"), max.words = 25)
#
#