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
|
#!/usr/bin/env python3
"""Create ROI overview report.
This report consists of plots of all metrics (y: metric, x: rank of value).
Each plot is written out as a PNG file and we also create one output HTML file
that shows all HTML files.
Plotting is done using the fine matplotlib.
"""
__author__ = 'Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>'
__copyright__ = 'Copyring 2013, Freie Universitaet Berlin'
__license__ = 'BSD 3-clause'
import sys
import os
import os.path
try:
import argparse
except ImportError:
import argparse26 as argparse
import Cheetah.Template
import matplotlib.pyplot as plt
import ngs_roi.io
import ngs_roi.app
import ngs_roi.argparse
# The HTML template to use for generating the HTML page.
REPORT_TPL = """
<html>
<head>
<title>ROI Report</title>
</head>
<body>
<h1>ROI Report</h1>
<h2>Table of Contents</h2>
<ul>
#for figure in $figures
<li><a href="#$figure.slug">$figure.title</a></li>
#end for
</ul>
<h2>Plots</h2>
#for figure in $figures
<h3 id="$figure.slug">$figure.title</h3>
<img src="$figure.file_name" title="$figure.title" />
#end for
</body>
</html>
"""
class ReportBuilder(ngs_roi.app.App):
"""This class is used for building the report."""
def __init__(self, args):
self.args = args
self.in_file = self.args.in_file
self.out_file = self.args.out_file
self.out_dir = self.args.out_dir
self.prepareOutDir()
def plotAndWrite(self, file_name, numbers, ylabel):
"""Create plot of numbers and write as PNG.
:param file_name:path to write plot to as image
:param numbers:list of numbers to plot
:param ylabel:label for y axis
"""
plt.figure()
plt.plot(numbers)
plt.ylabel(ylabel)
plt.savefig(file_name)
def run(self):
# Load ROI.
print('Loading ROI...', file=sys.stderr)
records = ngs_roi.io.load(self.in_file)
keys = records[0].data_keys
# Create ROI plots.
print('Creating plots...', file=sys.stderr)
METRICS = [('start position', 'start_pos', lambda x: x.start_pos),
('end position', 'end_pos', lambda x: x.end_pos),
('region length', 'region_length', lambda x: x.region_length),
('max count', 'max_count', lambda x: x.max_count)]
def getData(i):
def func(x):
try:
res = float(x.data[i])
except ValueError:
res = 0
return res
return func
for i, key in enumerate(keys):
slug = ''.join(x for x in key if x.isalnum())
METRICS.append((key, slug, getData(i)))
figure_infos = []
for title, slug, func in METRICS:
values = [func(x) for x in records]
file_name = 'report_%s.png' % slug
file_path = os.path.join(self.out_dir, file_name)
self.plotAndWrite(file_path, sorted(values), title)
figure_infos.append({'file_name': file_name, 'title': title, 'slug': slug})
# Create report HTML.
name_space = {'figures': figure_infos}
t = Cheetah.Template.Template(REPORT_TPL, searchList=name_space)
with open(os.path.join(self.out_dir, 'index.html'), 'wb') as f:
f.write(str(t))
with open(os.path.join(self.out_file), 'wb') as f:
f.write(str(t))
def main():
"""Program entry point."""
parser = argparse.ArgumentParser(description='Create ROI report.')
ngs_roi.argparse.addFileArguments(parser)
args = parser.parse_args()
ngs_roi.argparse.applyFileDefaults(args)
report_builder = ReportBuilder(args)
return report_builder.run()
if __name__ == '__main__':
sys.exit(main())
|