|
0
|
1 #!/usr/bin/env python
|
|
|
2 """Generation of detailed ROI reports with larger plots.
|
|
|
3
|
|
|
4 This report generation works for hundred of ROIs.
|
|
|
5 """
|
|
|
6
|
|
5
|
7 try:
|
|
|
8 import argparse
|
|
|
9 except ImportError:
|
|
|
10 import argparse26 as argparse
|
|
0
|
11 import math
|
|
|
12 import os.path
|
|
|
13 import sys
|
|
|
14
|
|
|
15 import Cheetah.Template
|
|
|
16 import matplotlib.pyplot as plt
|
|
|
17
|
|
1
|
18 import ngs_roi.app
|
|
|
19 import ngs_roi.argparse
|
|
|
20 import ngs_roi.io
|
|
0
|
21
|
|
|
22 PAGE_TPL = """
|
|
|
23 <html>
|
|
1
|
24 <head>
|
|
|
25 <title>ROI Table</title>
|
|
|
26 <style type="text/css">
|
|
|
27 div.plot
|
|
|
28 {
|
|
|
29 float: left;
|
|
|
30 padding: 4px;
|
|
|
31 margin: 2px;
|
|
|
32 width: 420px;
|
|
|
33 }
|
|
|
34
|
|
|
35 .plot h2 { margin-top: 3px; margin-bottom: 3px; text-align: center; }
|
|
|
36 .plot img { display: block; margin: 0 auto; }
|
|
|
37 </style>
|
|
|
38 </head>
|
|
0
|
39 <body>
|
|
|
40 <h1>Detailed ROI Report</h1>
|
|
1
|
41
|
|
0
|
42 #for i, roi in enumerate($records)
|
|
1
|
43 <div class="plot">
|
|
|
44 <h2>${roi.ref}:${roi.start_pos + 1}-${roi.end_pos+1}</h2>
|
|
|
45 <a href="${href($roi)}" target="dead"><img src="plot_${i}.png" /></a>
|
|
|
46 <p>
|
|
|
47 <b>chr:start-end</b> <a href="${href($roi)}" target="dead">${roi.ref}:${roi.start_pos}-${roi.end_pos} ${roi.strand}</a>;
|
|
|
48 <b>region name</b> ${roi.region_name};
|
|
|
49 <b>region length</b> ${roi.region_length};
|
|
|
50 </p>
|
|
|
51 #if $roi.data
|
|
|
52 <p>#for j, key in enumerate($data_keys)#<b>$key:</b> ${roi.data[$j]}; #end for#</p>
|
|
|
53 #end if
|
|
|
54 </div>
|
|
0
|
55 #end for
|
|
1
|
56 <iframe name="dead" height="0" width="0"></iframe>
|
|
|
57 <div><code>$args</code></div>
|
|
0
|
58 </body>
|
|
|
59 </html>
|
|
|
60 """
|
|
|
61
|
|
1
|
62 class DetailedRoiGenerator(ngs_roi.app.App):
|
|
0
|
63 """Generate detailed ROI report.
|
|
|
64
|
|
|
65 :ivar args:Arguments from the comment line.
|
|
|
66 """
|
|
|
67
|
|
|
68 def __init__(self, args):
|
|
|
69 self.args = args
|
|
|
70
|
|
|
71 def run(self):
|
|
|
72 """Run report generation, return status code.
|
|
|
73
|
|
|
74 :return: integer with the result.
|
|
|
75 """
|
|
|
76 print >>sys.stderr, 'Loading ROI'
|
|
1
|
77 records = ngs_roi.io.load(self.args.in_file, self.args.max_rois)
|
|
|
78 keys = records[0].data_keys
|
|
0
|
79
|
|
|
80 self.writeHtml(keys, records)
|
|
|
81 self.writePlots(records)
|
|
|
82 return 0
|
|
|
83
|
|
|
84 def writePlots(self, records):
|
|
1
|
85 COLOR = 'blue'
|
|
|
86 LINE_WIDTH = .5
|
|
|
87 LINE_STYLE = '-'
|
|
|
88 TICK_FONT_SIZE = 8
|
|
|
89 LABEL_FONT_SIZE = 10
|
|
0
|
90 for i, roi in enumerate(records):
|
|
1
|
91 file_name = 'plot_%d.png' % i
|
|
|
92 file_name = os.path.join(self.args.out_dir, file_name)
|
|
0
|
93 print >>sys.stderr, 'Writing plot %s' % file_name
|
|
1
|
94 plt.figure(figsize=(4, 2.5))
|
|
|
95 plt.gcf().subplots_adjust(bottom=0.16, left=0.15)
|
|
|
96 plt.plot(roi.points, color=COLOR, linewidth=LINE_WIDTH, linestyle=LINE_STYLE)
|
|
|
97 plt.ylim(ymin=0)
|
|
|
98 if self.args.max_value:
|
|
|
99 plt.ylim(ymax=self.args.max_value)
|
|
|
100 plt.tick_params(labelsize=TICK_FONT_SIZE)
|
|
|
101 plt.ylabel('coverage', fontsize=LABEL_FONT_SIZE, weight='semibold')
|
|
|
102 plt.xlabel('ROI beginPos', fontsize=LABEL_FONT_SIZE, weight='semibold')
|
|
0
|
103 plt.savefig(file_name)
|
|
|
104
|
|
|
105 def writeHtml(self, keys, records):
|
|
|
106 file_name = self.args.out_file
|
|
|
107 print >>sys.stderr, 'Writing HTML file %s' % file_name
|
|
|
108
|
|
1
|
109 vals = {'args': self.args, 'records': records, 'data_keys': keys,
|
|
|
110 'href': lambda x: self.buildHref(x.ref, x.start_pos, x.end_pos)}
|
|
0
|
111 t = Cheetah.Template.Template(PAGE_TPL, searchList=vals)
|
|
|
112
|
|
|
113 with open(file_name, 'wb') as f:
|
|
|
114 f.write(str(t))
|
|
|
115
|
|
|
116
|
|
|
117 def main():
|
|
|
118 parser = argparse.ArgumentParser(description='Plot ROI file.')
|
|
1
|
119 ngs_roi.argparse.addFileArguments(parser)
|
|
|
120 ngs_roi.argparse.addPlotGridArguments(parser)
|
|
|
121 ngs_roi.argparse.addLinkArguments(parser)
|
|
|
122 args = parser.parse_args()
|
|
|
123 ngs_roi.argparse.applyFileDefaults(args)
|
|
0
|
124
|
|
|
125 app = DetailedRoiGenerator(args)
|
|
|
126 return app.run()
|
|
|
127
|
|
|
128
|
|
|
129 if __name__ == '__main__':
|
|
|
130 sys.exit(main())
|