0
|
1 #!/usr/bin/env python
|
|
2 """ROI Table Generator
|
|
3
|
|
4 Generates a HTML page with a table of ROI record details. Besides showing the
|
|
5 numeric ROI information, it also gives little roi plots in a column.
|
|
6
|
|
7 For the little ROI plots, it calls the program roi_plot_thumbnails that has to
|
|
8 be in the PATH.
|
|
9 """
|
|
10
|
|
11 __author__ = 'Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>'
|
|
12 __copyright__ = 'Copyring 2013, Freie Universitaet Berlin'
|
|
13 __license__ = 'BSD 3-clause'
|
|
14
|
|
15
|
|
16 # TODO(holtgrew): Actually call roi_plot_thumbnails
|
|
17 # TODO(holtgrew): from __future__ use print_function
|
|
18
|
|
19
|
|
20 import argparse
|
|
21 import math
|
|
22 import os.path
|
|
23 import sys
|
|
24
|
|
25 import Cheetah.Template
|
|
26
|
|
27 import ngs_roi.app
|
|
28 import ngs_roi.argparse
|
|
29 import ngs_roi.io
|
|
30
|
|
31
|
|
32 # Main template.
|
|
33 PAGE_TPL = """
|
|
34 <html>
|
|
35 <head><title>ROI Table</title></head>
|
|
36 <body>
|
|
37 <h1>ROI Table</h1>
|
|
38 $table
|
1
|
39 <div><code>$args</code></div>
|
0
|
40 </body>
|
|
41 </html>
|
|
42 """
|
|
43
|
|
44 # Template for a table.
|
|
45 TABLE_TPL = """
|
|
46 <table border="1">
|
|
47 <tr>
|
|
48 <th>plot</th>
|
|
49 <th>chr</th>
|
|
50 <th>start</th>
|
|
51 <th>end</th>
|
|
52 <th>name</th>
|
|
53 <th>length</th>
|
|
54 <th>strand</th>
|
|
55 <th>max_count</th>
|
|
56 #for i, key in enumerate($data_keys)
|
|
57 <th>$key</th>
|
|
58 #end for
|
|
59 </tr>
|
|
60 #for id, roi in enumerate($records)
|
|
61 <tr>
|
|
62 <td><div style="width:${args.plot_width}; margin:2px; height:${args.plot_height+1}; background:url(thumbnail_${imgId($id)}.png) -${imgX($id)} -${imgY($id)};"></div></td>
|
|
63 <td>$roi.ref</td>
|
|
64 <td style="text-align:right;">$fmtPos($roi.start_pos + 1)</td>
|
|
65 <td style="text-align:right;">$fmtPos($roi.end_pos)</td>
|
|
66 <td><a href="$href($roi)">$roi.region_name</a></td>
|
|
67 <td style="text-align:right;">$fmtPos($roi.region_length)</td>
|
|
68 <td style="text-align:center;">$roi.strand</td>
|
|
69 <td style="text-align:right;">$roi.max_count</td>
|
|
70 #for i, key in enumerate($data_keys)
|
|
71 <td>$roi.data[$i]</td>
|
|
72 #end for
|
|
73 </tr>
|
|
74 #end for
|
|
75 </table>
|
|
76 """
|
|
77
|
|
78
|
|
79 class RoiTable(object):
|
|
80 """A table of ROI records with small plots."""
|
|
81
|
|
82 def __init__(self, args, keys, records, app):
|
|
83 self.args = args
|
|
84 self.keys = keys
|
|
85 self.records = records
|
|
86 self.app = app
|
|
87
|
|
88 def tplFuncs(self):
|
|
89 def intWithCommas(x):
|
|
90 if type(x) not in [type(0), type(0L)]:
|
|
91 raise TypeError("Parameter must be an integer.")
|
|
92 if x < 0:
|
|
93 return '-' + intWithCommas(-x)
|
|
94 result = ''
|
|
95 while x >= 1000:
|
|
96 x, r = divmod(x, 1000)
|
|
97 result = ",%03d%s" % (r, result)
|
|
98 return "%d%s" % (x, result)
|
|
99
|
|
100 def imgId(idx):
|
|
101 """Image id from roi record id."""
|
|
102 return idx / (self.args.num_rows * self.args.num_cols)
|
|
103
|
|
104 def imgX(idx):
|
|
105 """x position in image from record id."""
|
|
106 x = idx % self.args.num_cols
|
|
107 res = x * self.args.plot_width
|
|
108 if x > 0:
|
|
109 res += (x - 1) * 2
|
|
110 return res
|
|
111
|
|
112 def imgY(idx):
|
|
113 """y position in image from record id."""
|
|
114 y = idx / self.args.num_cols
|
|
115 res = y * self.args.plot_height
|
|
116 res += y * 2
|
|
117 return res
|
|
118
|
|
119 return {'fmtPos': intWithCommas, 'imgId': imgId, 'imgX': imgX, 'imgY': imgY}
|
|
120
|
|
121 def render(self):
|
|
122 """Returns string with rendered table."""
|
|
123 vals = {'data_keys': self.keys, 'records': self.records, 'args': self.args,
|
|
124 'href': lambda x:self.app.buildHref(x.ref, x.start_pos, x.end_pos)}
|
|
125 vals.update(self.tplFuncs())
|
|
126 t = Cheetah.Template.Template(TABLE_TPL, searchList=vals)
|
|
127 return str(t)
|
|
128
|
|
129
|
|
130 class TableApp(ngs_roi.app.App):
|
|
131 def __init__(self, args):
|
|
132 # Call parent's constructor and create output directory.
|
|
133 ngs_roi.app.App.__init__(self, args)
|
|
134 self.prepareOutDir()
|
|
135
|
|
136 def run(self):
|
|
137 # Load ROI records.
|
|
138 print >>sys.stderr, 'Loading ROI'
|
|
139 records = ngs_roi.io.load(self.args.in_file, self.args.max_rois)
|
|
140 keys = []
|
|
141 if records:
|
|
142 keys = records[0].data_keys
|
|
143
|
|
144 # Create plots.
|
|
145 print >>sys.stderr, 'Creating plots...'
|
|
146 runner = ngs_roi.app.PlotThumbnailsRunner(self.args)
|
|
147 runner.run()
|
|
148
|
|
149 # Create table.
|
|
150 print >>sys.stderr, 'Creating table...'
|
|
151 self.createHtml(self.args.out_file, keys, records)
|
|
152 return 0
|
|
153
|
|
154 def createHtml(self, file_name, keys, records):
|
|
155 print >>sys.stderr, 'Writing %s' % self.args.out_file
|
|
156 vals = {'table': RoiTable(self.args, keys, records, self).render()}
|
|
157 t = Cheetah.Template.Template(PAGE_TPL, searchList=vals)
|
|
158 with open(self.args.out_file, 'wb') as f:
|
|
159 f.write(str(t))
|
|
160
|
|
161
|
|
162 def main():
|
|
163 parser = argparse.ArgumentParser(description='Plot ROI file.')
|
|
164
|
|
165 ngs_roi.argparse.addFileArguments(parser)
|
|
166 ngs_roi.argparse.addPlotGridArguments(parser, default_plot_height=60,
|
|
167 default_plot_width=90)
|
|
168 ngs_roi.argparse.addLinkArguments(parser)
|
|
169 args = parser.parse_args()
|
|
170 ngs_roi.argparse.applyFileDefaults(args)
|
|
171
|
|
172 app = TableApp(args)
|
|
173 return app.run()
|
|
174
|
|
175
|
|
176 if __name__ == '__main__':
|
|
177 sys.exit(main())
|