-
Notifications
You must be signed in to change notification settings - Fork 0
/
enrichment_analysis.R
55 lines (47 loc) · 3.09 KB
/
enrichment_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
### Function to visualize the enrichment results:
experiment_list = list.files("~/Desktop/enrichment_results/",pattern = ".bed")
setwd("~/Desktop/enrichment_results/")
#enrich_analysis_plot_list = list()
enrich_analysis_prep = function(experiment_list){
library(ggplot2)
experiment_name_list = gsub(".bed","",experiment_list)
enrich_analysis_list = list()
if(length(experiment_list) == 1){
enrich_analysis_list = read.delim(experiment_list,sep = "\t",header = T,stringsAsFactors = F)
enrich_analysis_list[,"experiment_name"] = experiment_name_list
enrich_analysis_list$annotation = as.factor(enrich_analysis_list$annotation)
enrich_analysis_list$star = ""
enrich_analysis_list$star[enrich_analysis_list$qvalue <= 0.05] = "*"
enrich_analysis_list$star[enrich_analysis_list$qvalue <= 0.01] = "**"
enrich_analysis_list$star[enrich_analysis_list$qvalue <= 0.001] = "***"
enrich_analysis_list$star[enrich_analysis_list$qvalue > 0.05] = "ns"
gplot = ggplot(enrich_analysis_list,aes(x = annotation,y = log2(fold))) +
geom_bar(aes(fill=annotation), stat="identity", width=0.5, colour="black") +
geom_text(aes(label=star), colour="red", vjust=0.8, size=7,hjust =1,position = position_stack(vjust = 0)) +
theme(strip.text.x = element_text(size=7,hjust = 0),panel.background = element_rect(fill="white", colour="black"), axis.text.y = element_text(hjust=0, size=10),panel.grid = element_blank(),legend.position = "none") +
labs(x = "",y = "log2(Fold Enrichment)") + coord_flip() + geom_hline(yintercept = 0,color="red",linetype="dotted",size=1.5)
}
else{
for(i in 1:length(experiment_list)){
enrich_analysis_list[[i]] = read.delim(experiment_list[i],sep = "\t",header = T,stringsAsFactors = F)
enrich_analysis_list[[i]][,"experiment_name"] = experiment_name_list[i]
}
enrich_analysis_combined = enrich_analysis_list[[1]]
for(i in 2:length(experiment_list)){
enrich_analysis_combined = rbind(enrich_analysis_combined,enrich_analysis_list[[i]])
}
enrich_analysis_combined$annotation = as.factor(enrich_analysis_combined$annotation)
enrich_analysis_combined$star = ""
enrich_analysis_combined$star[enrich_analysis_combined$qvalue <= 0.05] = "*"
enrich_analysis_combined$star[enrich_analysis_combined$qvalue <= 0.01] = "**"
enrich_analysis_combined$star[enrich_analysis_combined$qvalue <= 0.001] = "***"
enrich_analysis_combined$star[enrich_analysis_combined$qvalue > 0.05] = "ns"
gplot = ggplot(enrich_analysis_combined,aes(x = annotation,y = log2(fold))) +
geom_bar(aes(fill=annotation), stat="identity", width=0.8, colour="black") +
geom_text(aes(label=star), colour="red", vjust=0.8, size=2,hjust =1,position = position_stack(vjust = 0)) +
facet_wrap(~experiment_name, nrow=2) +
theme(strip.text.x = element_text(size=7,hjust = 0),panel.background = element_rect(fill="white", colour="black"), axis.text.y = element_text(hjust=0, size=7),panel.grid = element_blank(),legend.position = "none") +
labs(x = "",y = "log2(Fold Enrichment)") + coord_flip()
}
return(gplot)
}