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
|
#!/usr/bin/env python3
"""Generation of detailed ROI reports with larger plots.
This report generation works for hundred of ROIs.
"""
try:
import argparse
except ImportError:
import argparse26 as argparse
import math
import os.path
import sys
import Cheetah.Template
import matplotlib.pyplot as plt
import ngs_roi.app
import ngs_roi.argparse
import ngs_roi.io
PAGE_TPL = """
<html>
<head>
<title>ROI Table</title>
<style type="text/css">
div.plot
{
float: left;
padding: 4px;
margin: 2px;
width: 420px;
}
.plot h2 { margin-top: 3px; margin-bottom: 3px; text-align: center; }
.plot img { display: block; margin: 0 auto; }
</style>
</head>
<body>
<h1>Detailed ROI Report</h1>
#for i, roi in enumerate($records)
<div class="plot">
<h2>${roi.ref}:${roi.start_pos + 1}-${roi.end_pos+1}</h2>
<a href="${href($roi)}" target="dead"><img src="plot_${i}.png" /></a>
<p>
<b>chr:start-end</b> <a href="${href($roi)}" target="dead">${roi.ref}:${roi.start_pos}-${roi.end_pos} ${roi.strand}</a>;
<b>region name</b> ${roi.region_name};
<b>region length</b> ${roi.region_length};
</p>
#if $roi.data
<p>#for j, key in enumerate($data_keys)#<b>$key:</b> ${roi.data[$j]}; #end for#</p>
#end if
</div>
#end for
<iframe name="dead" height="0" width="0"></iframe>
<div><code>$args</code></div>
</body>
</html>
"""
class DetailedRoiGenerator(ngs_roi.app.App):
"""Generate detailed ROI report.
:ivar args:Arguments from the comment line.
"""
def __init__(self, args):
self.args = args
def run(self):
"""Run report generation, return status code.
:return: integer with the result.
"""
print('Loading ROI', file=sys.stderr)
records = ngs_roi.io.load(self.args.in_file, self.args.max_rois)
keys = records[0].data_keys
self.writeHtml(keys, records)
self.writePlots(records)
return 0
def writePlots(self, records):
COLOR = 'blue'
LINE_WIDTH = .5
LINE_STYLE = '-'
TICK_FONT_SIZE = 8
LABEL_FONT_SIZE = 10
for i, roi in enumerate(records):
file_name = 'plot_%d.png' % i
file_name = os.path.join(self.args.out_dir, file_name)
print('Writing plot %s' % file_name, file=sys.stderr)
plt.figure(figsize=(4, 2.5))
plt.gcf().subplots_adjust(bottom=0.16, left=0.15)
plt.plot(roi.points, color=COLOR, linewidth=LINE_WIDTH, linestyle=LINE_STYLE)
plt.ylim(ymin=0)
if self.args.max_value:
plt.ylim(ymax=self.args.max_value)
plt.tick_params(labelsize=TICK_FONT_SIZE)
plt.ylabel('coverage', fontsize=LABEL_FONT_SIZE, weight='semibold')
plt.xlabel('ROI beginPos', fontsize=LABEL_FONT_SIZE, weight='semibold')
plt.savefig(file_name)
def writeHtml(self, keys, records):
file_name = self.args.out_file
print('Writing HTML file %s' % file_name, file=sys.stderr)
vals = {'args': self.args, 'records': records, 'data_keys': keys,
'href': lambda x: self.buildHref(x.ref, x.start_pos, x.end_pos)}
t = Cheetah.Template.Template(PAGE_TPL, searchList=vals)
with open(file_name, 'wb') as f:
f.write(str(t))
def main():
parser = argparse.ArgumentParser(description='Plot ROI file.')
ngs_roi.argparse.addFileArguments(parser)
ngs_roi.argparse.addPlotGridArguments(parser)
ngs_roi.argparse.addLinkArguments(parser)
args = parser.parse_args()
ngs_roi.argparse.applyFileDefaults(args)
app = DetailedRoiGenerator(args)
return app.run()
if __name__ == '__main__':
sys.exit(main())
|