-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathPeak.summary.py
executable file
·246 lines (213 loc) · 11.6 KB
/
Peak.summary.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import division, with_statement
'''
Copyright 2015, 陈同 (chentong_biology@163.com).
===========================================================
'''
__author__ = 'chentong & ct586[9]'
__author_email__ = 'chentong_biology@163.com'
#=========================================================
desc = '''
Program description:
This is designed to summarize peak calling results.
Result files are combined with <samp_name> <type> <gene_attr>
Peak_file: <samp_name>_<type>/<samp_name>_<type>.peak.bed
BW file: <samp_name>_<type>/<samp_name>_<type>.log2ratio.bw
EnrichXLS: <summary_dir>/<type>.<gene_attr>.all.entrez.<samp_name>_<type>.<enrich_type>.scatterplot.dv.xls
EnrichPDF: <summary_dir>/<type>.<gene_attr>.all.entrez.<samp_name>_<type>.<enrich_type>.scatterplot.dv.pdf
EnrichPNG: <summary_dir>/<type>.<gene_attr>.all.entrez.<samp_name>_<type>.<enrich_type>.scatterplot.dv.png
'''
import sys
import os
from json import dumps as json_dumps
from time import localtime, strftime
timeformat = "%Y-%m-%d %H:%M:%S"
from optparse import OptionParser as OP
import re
from tools import *
#from multiprocessing.dummy import Pool as ThreadPool
#from bs4 import BeautifulSoup
reload(sys)
sys.setdefaultencoding('utf8')
debug = 0
def fprint(content):
"""
This is a Google style docs.
Args:
param1(str): this is the first param
param2(int, optional): this is a second param
Returns:
bool: This is a description of what is returned
Raises:
KeyError: raises an exception))
"""
print json_dumps(content,indent=1)
def cmdparameter(argv):
if len(argv) == 1:
global desc
print >>sys.stderr, desc
cmd = 'python ' + argv[0] + ' -h'
os.system(cmd)
sys.exit(1)
usages = "%prog -f file"
parser = OP(usage=usages)
parser.add_option("-s", "--sample-name", dest="samp_name",
metavar="FILEIN", help="The name of samples. Normally $(sampleL).")
parser.add_option("-t", "--type", dest="type",
help="Different types of modification. Normally $(typeL).")
parser.add_option("-g", "--gene-attr", dest="gene_attr",
default="peak_gene_promoter",
help="Different ways of finding target genes. Normally support <peak_gene_promoter>, <peak_gene_closest>.")
parser.add_option("-e", "--enrichmentType", dest="enrich_type",
default="BP_GO, MF_GO, CC_GO, KEGG",
help="Default <BP_GO, MF_GO, CC_GO, KEGG>. And only these 4 types of enrichment are supported. These strings will be used to get filenames.")
parser.add_option("-d", "--go-dir", dest="go_dir",
help="Directory containing GO enrichment results. Normally $(summary_dir).")
parser.add_option("-T", "--tag", dest="tag",
default='tag', help="A string containing only aphabets to use as program specific label. Normally this is unused unless this program is used multiple times in same pipeline. Default <tag>.")
parser.add_option("-r", "--report-dir", dest="report_dir",
default='report', help="Directory for report files. Default 'report'.")
parser.add_option("-R", "--report-sub-dir", dest="report_sub_dir",
default='5_peak_profile', help="Directory for saving report figures and tables. This dir will put under <report_dir>, so only dir name is needed. Default '5_peak_profile'.")
# parser.add_option("-d", "--doc-only", dest="doc_only",
# default=False, action="store_true", help="Specify to only generate doc.")
# parser.add_option("-n", "--number", dest="number", type="int",
# default=40, help="Set the maximum allowed samples for barplot. Default 40.\
# If more than this number of samples are given, heatmap will be used instead. ")
parser.add_option("-v", "--verbose", dest="verbose",
action="store_true", help="Show process information")
parser.add_option("-D", "--debug", dest="debug",
default=False, action="store_true", help="Debug the program")
(options, args) = parser.parse_args(argv[1:])
assert options.samp_name != None, "A filename needed for -s"
return (options, args)
#--------------------------------------------------------------------
def generateDoc(report_dir, report_sub_dir, tag, go_dir,
sample_nameL, typeL, gene_attrL, enrich_typeL):
dest_dir = report_dir+'/'+report_sub_dir+'/'
os.system('mkdir -p '+dest_dir)
print "\n# Peak鉴定和修饰图谱 {#peak-identification-modi-profile}\n"
knitr_read_txt(report_dir, "Peak_identification_profile")
count = 0
for type in typeL:
print "\n## " + type + ' profile\n\n'
for samp in sample_nameL:
count += 1
print "\n### {} {}\n\n".format(samp, type)
full_name = samp+'_'+type
peak = full_name+'/'+full_name+'.peak.bed'
bw = full_name+'/'+full_name+'.log2ratio.bw'
copy(dest_dir, peak, bw)
peak,bw = getRelativeDir([peak,bw], report_sub_dir)
peak_n, bw_n = getFileName([peak, bw])
print "* Peak file in bed format: [{}]({})\n".format(peak_n, peak)
#print "* Peak bound or modified gene list in text format: [{}]({})\n".format(gene_n, gene)
print "* Track file in bigWig format: [{}]({})\n".format(bw_n, bw)
if 'peak_gene_promoter' in gene_attrL:
middle = "peak_gene_promoter.all.entrez"
tag2 = tag + 'peak-gene-promoter'
print "\n### 启动子区存在修饰的基因\n"
gene = full_name+'/'+full_name+'.peak_gene_promoter.list'
copy(dest_dir, gene)
print "\n* 启动子区存在修饰的基因: [XLS]({})".format(getRelativeDir(gene, report_sub_dir))
print "\n#### 样品{}{}修饰或结合基因(启动子区)的功能富集分析\n".format(samp, type)
print '''
{} 修饰或结合的基因的筛选标准是:修饰或结合区域落在基因的启动子区域(转录起始位点上游1kb、下游 500 nt)。
'''.format(type)
go_prefix = '.'.join([go_dir+'/'+type, middle, full_name+'.'])
enrich_cnt = 0
for enrichType in enrich_typeL:
enrich_cnt += 1
go_prefix_enrich_xls = go_prefix+enrichType+'.xls'
go_prefix_enrich_pdf = go_prefix+enrichType+'.scatterplot.dv.pdf'
copy(dest_dir, go_prefix_enrich_xls)
copypdf(dest_dir, go_prefix_enrich_pdf)
go_prefix_enrich_xls,go_prefix_enrich_pdf = \
getRelativeDir([go_prefix_enrich_xls,go_prefix_enrich_pdf],
report_sub_dir)
go_prefix_enrich_xls_name,go_prefix_enrich_pdf_name = \
getFileName([go_prefix_enrich_xls,go_prefix_enrich_pdf])
print '''
(ref:enrich-{tag}-{count}-{enrich_cnt}) Top 30 gene ontology or KEGG enrichment terms for genes regulated by {type} in {samp}. 缺少的图表示对应样品中没有富集的条目。 Full lists of GO or KEGG enrichment terms can be downloaded ([{go_prefix_enrich_xls_name}]({go_prefix_enrich_xls})) ([{go_prefix_enrich_pdf_name}]({go_prefix_enrich_pdf})).
```{{r enrich-{tag}-{count}-{enrich_cnt}, out.width="90%", fig.cap="(ref:enrich-{tag}-{count}-{enrich_cnt})"}}
knitr::include_graphics(c("{go_prefix_enrich_png}"))
```
'''.format(tag=tag2, count=count, enrich_cnt=enrich_cnt, type=type, samp=samp,
go_prefix_enrich_xls_name=go_prefix_enrich_xls_name,
go_prefix_enrich_pdf_name=go_prefix_enrich_pdf_name,
go_prefix_enrich_xls=go_prefix_enrich_xls,
go_prefix_enrich_pdf=go_prefix_enrich_pdf,
go_prefix_enrich_png=go_prefix_enrich_pdf.replace('pdf', 'png'))
if 'peak_gene_closest' in gene_attrL:
middle = "peak_gene_closest.all.entrez"
tag2 = tag + 'peak-gene-closest'
print "\n### 修饰区邻近的基因\n"
gene = full_name+'/'+full_name+'.peak_gene_closest.list'
copy(dest_dir, gene)
print "\n* 修饰区临近基因列表: [XLS]({})\n".format(getRelativeDir(gene, report_sub_dir))
print "\n#### 样品{}{}修饰或结合基因(启动子区)的功能富集分析\n".format(samp, type)
print '''
{} 修饰区临近基因的筛选标准是:修饰或结合区域最近的基因 (上游优先, 距离记载在最后一列)。
'''.format(type)
go_prefix = '.'.join([go_dir+'/'+type, middle, full_name+'.'])
enrich_cnt = 0
for enrichType in enrich_typeL:
enrich_cnt += 1
go_prefix_enrich_xls = go_prefix+enrichType+'.xls'
go_prefix_enrich_pdf = go_prefix+enrichType+'.scatterplot.dv.pdf'
copy(dest_dir, go_prefix_enrich_xls)
copypdf(dest_dir, go_prefix_enrich_pdf)
go_prefix_enrich_xls,go_prefix_enrich_pdf = \
getRelativeDir([go_prefix_enrich_xls,go_prefix_enrich_pdf],
report_sub_dir)
go_prefix_enrich_xls_name,go_prefix_enrich_pdf_name = \
getFileName([go_prefix_enrich_xls,go_prefix_enrich_pdf])
print '''
(ref:enrich-{tag}-{count}-{enrich_cnt}) Top 30 gene ontology or KEGG enrichment terms for genes regulated by {type} in {samp}. 缺少的图表示对应样品中没有富集的条目。 Full lists of GO or KEGG enrichment terms can be downloaded ([{go_prefix_enrich_xls_name}]({go_prefix_enrich_xls})) ([{go_prefix_enrich_pdf_name}]({go_prefix_enrich_pdf})).
```{{r enrich-{tag}-{count}-{enrich_cnt}, out.width="90%", fig.cap="(ref:enrich-{tag}-{count}-{enrich_cnt})"}}
knitr::include_graphics(c("{go_prefix_enrich_png}"))
```
'''.format(tag=tag2, count=count, enrich_cnt=enrich_cnt, type=type, samp=samp,
go_prefix_enrich_xls_name=go_prefix_enrich_xls_name,
go_prefix_enrich_pdf_name=go_prefix_enrich_pdf_name,
go_prefix_enrich_xls=go_prefix_enrich_xls,
go_prefix_enrich_pdf=go_prefix_enrich_pdf,
go_prefix_enrich_png=go_prefix_enrich_pdf.replace('pdf', 'png'))
#-------------------------------
#-------------------------------
def main():
options, args = cmdparameter(sys.argv)
#-----------------------------------
go_dir = options.go_dir
sample_nameL = re.split(r'[, ]*', options.samp_name.strip())
typeL = re.split(r'[, ]*', options.type.strip())
gene_attrL = re.split(r'[, ]*', options.gene_attr.strip())
enrich_type = options.enrich_type
enrich_typeL = re.split(r'[, ]*', enrich_type.strip())
report_dir = options.report_dir
report_sub_dir = options.report_sub_dir
verbose = options.verbose
tag = options.tag
global debug
debug = options.debug
#-----------------------------------
aDict = {}
generateDoc(report_dir, report_sub_dir, tag, go_dir, sample_nameL, typeL, gene_attrL, enrich_typeL)
if __name__ == '__main__':
startTime = strftime(timeformat, localtime())
main()
endTime = strftime(timeformat, localtime())
fh = open('python.log', 'a')
print >>fh, "%s\n\tRun time : %s - %s " % \
(' '.join(sys.argv), startTime, endTime)
fh.close()
###---------profile the program---------
#import profile
#profile_output = sys.argv[0]+".prof.txt")
#profile.run("main()", profile_output)
#import pstats
#p = pstats.Stats(profile_output)
#p.sort_stats("time").print_stats()
###---------profile the program---------