-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenrichment_plot.py
More file actions
277 lines (227 loc) · 9.12 KB
/
enrichment_plot.py
File metadata and controls
277 lines (227 loc) · 9.12 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
# enrichment_plot.py
# Verson: 1.1
# Author: Juse
# Use R to plot enrichment dotplot.
# Usage: See github.
import argparse
import os
import time
def get_args():
parser = argparse.ArgumentParser(description = "This is used to plot enrichment dotplot.")
parser.add_argument("-gef", "--genefore", type = str, help = "The gene list as foregroud.")
parser.add_argument("-geb", "--geneback", type = str, help = "The gene list and annotation as backgroud.")
parser.add_argument("-go", "--golist", type = str, help = "The go_term.list file.")
parser.add_argument("-w", "--width", default = '8', type = str, help = "Width of plot, default = 8.")
parser.add_argument("-l", "--height", default = '8', type = str, help = "Height of plot, default = 8.")
parser.add_argument("-y", "--yaxis", default = "Description", choices = ['Description','ID'], type = str, help = "The yaxis of plot, use GO Description as default.")
parser.add_argument("-o", "--output", type = str, required = True, help = "The output directory, required.")
parser.add_argument("-t", "--title", default = 'NULL', type = str, help = "The title of plot, default = NULL.")
parser.add_argument("-c", "--csv", default = 'NULL', type = str, help = "Just plot with file given.")
args = parser.parse_args()
return args
def get_path(args):
if args.csv == 'NULL':
paths = {
'fore_gene': os.path.abspath(args.genefore),
'back_gene': os.path.abspath(args.geneback),
'go_list': os.path.abspath(args.golist),
'output_dir': os.path.abspath(args.output),
}
else:
paths = {
'output_dir': os.path.abspath(args.output),
'csv_dir': os.path.abspath(args.csv)
}
return paths
def R_code(args, paths, run_time):
rcode = '''
# 加载包
library("clusterProfiler")
library("ggplot2")
library("DOSE")
# 加载各个参数
goterm = "%juse%1"
backgene = "%juse%2"
foregene = "%juse%3"
wid = %juse%4
hei = %juse%5
title = "%juse%7"
# 生成背景注释集
go_class <- read.delim(goterm, header=FALSE, stringsAsFactors =FALSE)
names(go_class) <- c('ID','Description','Ontology')
go_anno <- read.delim(backgene, header=FALSE, stringsAsFactors =FALSE)
names(go_anno) <- c('gene_id','ID')
go_anno <-merge(go_anno, go_class, by = 'ID', all.x = TRUE)
# 生成前景基因集
gene_list <- read.delim(foregene,header=FALSE)
names(gene_list) <- c('gene_id')
gene_select <- gene_list$gene_id
# 进行富集分析
go_rich <- enricher(gene = gene_select,
TERM2GENE = go_anno[c('ID','gene_id')],
TERM2NAME = go_anno[c('ID','Description')],
pvalueCutoff = 0.05,
pAdjustMethod = 'BH',
qvalueCutoff = 0.05,
maxGSSize = 200)
# 提取富集分析结果制成表格
plot_data = cbind(go_rich$ID, go_rich$Description, go_rich$GeneRatio, go_rich$BgRatio, go_rich$Count, go_rich$p.adjust, go_rich$ID, go_rich$geneID)
colnames(plot_data) = c("ID", "Description", "GeneRatio", "BgRatio", "Count","qvalue", "Ontology", "GeneID")
plot_data = data.frame(plot_data)
for(i in 1:length(plot_data$Ontology)){
if(is.na(plot_data$Description[i])){
plot_data$Ontology[i] = 'NA'
}else{
plot_data$Ontology[i] = go_class$Ontology[go_class$ID == plot_data$ID[i]]
}
}
# 处理数据
plot_data_noNA = plot_data[complete.cases(plot_data$Description),]
plot_data_noNA = transform(plot_data_noNA, Count = as.numeric(Count),
qvalue = as.numeric(qvalue))
plot_data_noNA = plot_data_noNA[order(plot_data_noNA$qvalue,-plot_data_noNA$Count),]
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "cellular_component"] = "Cellular Component"
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "biological_process"] = "Biological Process"
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "molecular_function"] = "Molecular Function"
Top15 = c()
CC = 0
BP = 0
MF = 0
for(i in 1:length(plot_data_noNA$Ontology)){
if(plot_data_noNA$Ontology[i] == "Cellular Component"){
CC = CC + 1
if(CC <= 5){
Top15 = append(Top15,(i))
}
}
if(plot_data_noNA$Ontology[i] == "Biological Process"){
BP = BP + 1
if(BP <= 5){
Top15 = append(Top15,(i))
}
}
if(plot_data_noNA$Ontology[i] == "Molecular Function"){
MF = MF + 1
if(MF <= 5){
Top15 = append(Top15,(i))
}
}
}
# 进行气泡图绘制
enrichment_plot = ggplot(plot_data_noNA[Top15,],aes(x = parse_ratio(GeneRatio),y = reorder(%juse%6, Count))) + geom_point(aes(size=Count,color=qvalue,)) +
scale_color_gradient(low = "red", high = "blue") + theme_bw() + ylab(NULL) + xlab("GeneRatio") +
facet_wrap(~Ontology, scale="free",ncol = 1,strip.position = "right") +
theme(text = element_text(size = 15))
enrichment_plot_top10 = ggplot(plot_data_noNA[1:10,],aes(x = parse_ratio(GeneRatio),y = reorder(%juse%6, Count))) + geom_point(aes(size=Count,color=qvalue,)) +
scale_color_gradient(low = "red", high = "blue") + theme_bw() + ylab(NULL) + xlab("GeneRatio") +
theme(text = element_text(size = 15))
if(title != "NULL"){
enrichment_plot = enrichment_plot + ggtitle(title)
enrichment_plot_top10 = enrichment_plot_top10 + ggtitle(title)
}
# 保存图及富集分析表格
pdf("output%1%",width = wid,height = hei)
enrichment_plot
dev.off()
pdf("output%2%",width = wid,height = hei)
enrichment_plot_top10
dev.off()
write.csv(plot_data,file="output%3%")
'''
r_code_use = rcode.replace("%juse%1", paths['go_list'])\
.replace("%juse%2", paths['back_gene'])\
.replace("%juse%3", paths['fore_gene'])\
.replace("%juse%4", args.width)\
.replace("%juse%5", args.height)\
.replace("%juse%6", args.yaxis)\
.replace("%juse%7", args.title)\
.replace("output%1%", f"{paths['output_dir']}/{run_time}/enrichment_plot.pdf")\
.replace("output%2%", f"{paths['output_dir']}/{run_time}/enrichment_plot_top10.pdf")\
.replace("output%3%", f"{paths['output_dir']}/{run_time}/enrichment_analysis.csv")\
return r_code_use
def plot_R(args, paths, run_time):
rcode = '''
# 加载包
library("DOSE")
library("ggplot2")
# 加载数据集
plot_data_noNA = read.csv("%juse%1", header=TRUE)
wid = %juse%4
hei = %juse%5
title = "%juse%7"
# 处理数据
plot_data_noNA = plot_data_noNA[complete.cases(plot_data_noNA$Description),]
plot_data_noNA = transform(plot_data_noNA, Count = as.numeric(Count),
qvalue = as.numeric(qvalue))
plot_data_noNA = plot_data_noNA[order(plot_data_noNA$qvalue,-plot_data_noNA$Count),]
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "cellular_component"] = "Cellular Component"
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "biological_process"] = "Biological Process"
plot_data_noNA$Ontology[plot_data_noNA$Ontology == "molecular_function"] = "Molecular Function"
Top15 = c()
CC = 0
BP = 0
MF = 0
for(i in 1:length(plot_data_noNA$Ontology)){
if(plot_data_noNA$Ontology[i] == "Cellular Component"){
CC = CC + 1
if(CC <= 5){
Top15 = append(Top15,(i))
}
}
if(plot_data_noNA$Ontology[i] == "Biological Process"){
BP = BP + 1
if(BP <= 5){
Top15 = append(Top15,(i))
}
}
if(plot_data_noNA$Ontology[i] == "Molecular Function"){
MF = MF + 1
if(MF <= 5){
Top15 = append(Top15,(i))
}
}
}
# 进行气泡图绘制
enrichment_plot = ggplot(plot_data_noNA[Top15,],aes(x = parse_ratio(GeneRatio),y = reorder(%juse%6, Count))) + geom_point(aes(size=Count,color=qvalue,)) +
scale_color_gradient(low = "red", high = "blue") + theme_bw() + ylab(NULL) + xlab("GeneRatio") +
facet_wrap(~Ontology, scale="free",ncol = 1,strip.position = "right") +
theme(text = element_text(size = 15))
enrichment_plot_top10 = ggplot(plot_data_noNA[1:10,],aes(x = parse_ratio(GeneRatio),y = reorder(%juse%6, Count))) + geom_point(aes(size=Count,color=qvalue,)) +
scale_color_gradient(low = "red", high = "blue") + theme_bw() + ylab(NULL) + xlab("GeneRatio") +
theme(text = element_text(size = 15))
if(title != "NULL"){
enrichment_plot = enrichment_plot + ggtitle(title)
enrichment_plot_top10 = enrichment_plot_top10 + ggtitle(title)
}
# 保存图及富集分析表格
pdf("output%1%",width = wid,height = hei)
enrichment_plot
dev.off()
pdf("output%2%",width = wid,height = hei)
enrichment_plot_top10
dev.off()
'''
r_code_use = rcode.replace("%juse%1", paths['csv_dir'])\
.replace("%juse%4", args.width)\
.replace("%juse%5", args.height)\
.replace("%juse%6", args.yaxis)\
.replace("%juse%7", args.title)\
.replace("output%1%", f"{paths['output_dir']}/{run_time}/enrichment_plot.pdf")\
.replace("output%2%", f"{paths['output_dir']}/{run_time}/enrichment_plot_top10.pdf")\
return r_code_use
def run_r_script(r_code, paths, run_time):
os.makedirs(f"{paths['output_dir']}/{run_time}", exist_ok=True)
script_path = f"{paths['output_dir']}/{run_time}/enrichment_plot.R"
with open(script_path, 'w') as f:
f.write(r_code)
os.system(f'Rscript {script_path}')
def main():
run_time = time.strftime('%Y%m%d-%H%M%S')
args = get_args()
paths = get_path(args)
r_code = R_code(args, paths, run_time) if args.csv == 'NULL' else plot_R(args, paths, run_time)
run_r_script(r_code, paths, run_time)
print(f'Finished! See output at {paths["output_dir"]}')
if __name__ == "__main__":
main()