|
0
|
1 #!/usr/bin/env python
|
|
|
2 """Create ROI overview report.
|
|
|
3
|
|
|
4 This report consists of plots of all metrics (y: metric, x: rank of value).
|
|
|
5 Each plot is written out as a PNG file and we also create one output HTML file
|
|
|
6 that shows all HTML files.
|
|
|
7
|
|
|
8 Plotting is done using the fine matplotlib.
|
|
|
9 """
|
|
|
10
|
|
|
11 from __future__ import print_function
|
|
|
12
|
|
|
13 __author__ = 'Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>'
|
|
|
14 __copyright__ = 'Copyring 2013, Freie Universitaet Berlin'
|
|
|
15 __license__ = 'BSD 3-clause'
|
|
|
16
|
|
|
17
|
|
|
18 import sys
|
|
|
19 import os
|
|
|
20 import os.path
|
|
|
21 import argparse
|
|
|
22
|
|
|
23 import Cheetah.Template
|
|
|
24 import matplotlib.pyplot as plt
|
|
|
25
|
|
|
26 import ngs_roi.io
|
|
|
27 import ngs_roi.app
|
|
|
28 import ngs_roi.argparse
|
|
|
29
|
|
|
30
|
|
|
31 # The HTML template to use for generating the HTML page.
|
|
|
32 REPORT_TPL = """
|
|
|
33 <html>
|
|
|
34 <head>
|
|
|
35 <title>ROI Report</title>
|
|
|
36 </head>
|
|
|
37 <body>
|
|
|
38 <h1>ROI Report</h1>
|
|
|
39 <h2>Table of Contents</h2>
|
|
|
40 <ul>
|
|
|
41 #for figure in $figures
|
|
|
42 <li><a href="#$figure.slug">$figure.title</a></li>
|
|
|
43 #end for
|
|
|
44 </ul>
|
|
|
45 <h2>Plots</h2>
|
|
|
46 #for figure in $figures
|
|
|
47 <h3 id="$figure.slug">$figure.title</h3>
|
|
|
48 <img src="$figure.file_name" title="$figure.title" />
|
|
|
49 #end for
|
|
|
50 </body>
|
|
|
51 </html>
|
|
|
52 """
|
|
|
53
|
|
|
54
|
|
|
55 class ReportBuilder(ngs_roi.app.App):
|
|
|
56 """This class is used for building the report."""
|
|
|
57
|
|
|
58 def __init__(self, args):
|
|
|
59 self.args = args
|
|
|
60 self.in_file = self.args.in_file
|
|
|
61 self.out_file = self.args.out_file
|
|
|
62 self.out_dir = self.args.out_dir
|
|
|
63 self.prepareOutDir()
|
|
|
64
|
|
|
65 def plotAndWrite(self, file_name, numbers, ylabel):
|
|
|
66 """Create plot of numbers and write as PNG.
|
|
|
67
|
|
|
68 :param file_name:path to write plot to as image
|
|
|
69 :param numbers:list of numbers to plot
|
|
|
70 :param ylabel:label for y axis
|
|
|
71 """
|
|
|
72 plt.figure()
|
|
|
73 plt.plot(numbers)
|
|
|
74 plt.ylabel(ylabel)
|
|
|
75 plt.savefig(file_name)
|
|
|
76
|
|
|
77 def run(self):
|
|
|
78 # Load ROI.
|
|
|
79 print('Loading ROI...', file=sys.stderr)
|
|
|
80 records = ngs_roi.io.load(self.in_file)
|
|
|
81 keys = records[0].data_keys
|
|
|
82 # Create ROI plots.
|
|
|
83 print('Creating plots...', file=sys.stderr)
|
|
|
84 METRICS = [('start position', 'start_pos', lambda x: x.start_pos),
|
|
|
85 ('end position', 'end_pos', lambda x: x.end_pos),
|
|
|
86 ('region length', 'region_length', lambda x: x.region_length),
|
|
|
87 ('max count', 'max_count', lambda x: x.max_count)]
|
|
|
88 def getData(i):
|
|
|
89 def func(x):
|
|
|
90 try:
|
|
|
91 res = float(x.data[i])
|
|
|
92 except ValueError:
|
|
|
93 res = 0
|
|
|
94 return res
|
|
|
95 return func
|
|
|
96 for i, key in enumerate(keys):
|
|
|
97 slug = ''.join(x for x in key if x.isalnum())
|
|
|
98 METRICS.append((key, slug, getData(i)))
|
|
|
99 figure_infos = []
|
|
|
100 for title, slug, func in METRICS:
|
|
|
101 values = [func(x) for x in records]
|
|
|
102 file_name = 'report_%s.png' % slug
|
|
|
103 file_path = os.path.join(self.out_dir, file_name)
|
|
|
104 self.plotAndWrite(file_path, sorted(values), title)
|
|
|
105 figure_infos.append({'file_name': file_name, 'title': title, 'slug': slug})
|
|
|
106 # Create report HTML.
|
|
|
107 name_space = {'figures': figure_infos}
|
|
|
108 t = Cheetah.Template.Template(REPORT_TPL, searchList=name_space)
|
|
|
109 with open(os.path.join(self.out_dir, 'index.html'), 'wb') as f:
|
|
|
110 f.write(str(t))
|
|
|
111 with open(os.path.join(self.out_file), 'wb') as f:
|
|
|
112 f.write(str(t))
|
|
|
113
|
|
|
114 def main():
|
|
|
115 """Program entry point."""
|
|
|
116
|
|
|
117 parser = argparse.ArgumentParser(description='Create ROI report.')
|
|
|
118
|
|
|
119 ngs_roi.argparse.addFileArguments(parser)
|
|
|
120 args = parser.parse_args()
|
|
|
121 ngs_roi.argparse.applyFileDefaults(args)
|
|
|
122
|
|
|
123 report_builder = ReportBuilder(args)
|
|
|
124 return report_builder.run()
|
|
|
125
|
|
|
126
|
|
|
127 if __name__ == '__main__':
|
|
|
128 sys.exit(main())
|