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