allpy
changeset 1010:32aed6c153d4
Added util markup_to_html.py that outputs simple HTML table with marked up alignment
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Tue, 06 Mar 2012 16:07:36 +0400 |
parents | 72a99edab6eb |
children | 5dc4f805b000 |
files | utils/markup_to_html.html utils/markup_to_html.py |
diffstat | 2 files changed, 85 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/utils/markup_to_html.html Tue Mar 06 16:07:36 2012 +0400 1.3 @@ -0,0 +1,37 @@ 1.4 +<html> 1.5 + <head> 1.6 + <title>Alignment with markup</title> 1.7 + <style> 1.8 + * { font-family: fixed; font-size: 8pt; } 1.9 + .code1 { display: block; font-size: 12pt; width: 2em; } 1.10 + .code3 { display: none; } 1.11 + .number { display: none; } 1.12 + .popup { display: none; background: #ffc; padding: .2em; border: solid thin black; white-space: nowrap; } 1.13 + span.code1:hover { background: #fcc; } 1.14 + span.code1:hover .popup { display: block; position: absolute; margin-left: 1em; } 1.15 + </style> 1.16 + </head> 1.17 + <body> 1.18 + <table> 1.19 + {% for row in rows %} 1.20 + <tr> 1.21 + {% for monomer in row %} 1.22 + <td> 1.23 + {% if monomer %} 1.24 + <span class='code1'>{{ monomer.code1 }} 1.25 + <div class='popup'>{{ monomer.description.replace("\n", "<br>") }}</div> 1.26 + </span> 1.27 + <span class='code3'>{{ monomer.code3 }}</span> 1.28 + {% for name, markup in row.sequence.markups.items() %} 1.29 + <span class='{{ name }}'>{{ markup.get(monomer, "") }}</span> 1.30 + {% endfor %} 1.31 + {% else %} 1.32 + <span class='code1'>-</span> 1.33 + {% endif %} 1.34 + </td> 1.35 + {% endfor %} 1.36 + </tr> 1.37 + {% endfor %} 1.38 + </table> 1.39 + </body> 1.40 +</html>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/utils/markup_to_html.py Tue Mar 06 16:07:36 2012 +0400 2.3 @@ -0,0 +1,48 @@ 2.4 +#!/usr/bin/python 2.5 +"""Represent markup as HTML file. 2.6 + 2.7 +Input defaults to stdin. 2.8 +""" 2.9 +import sys 2.10 +import optparse 2.11 +from os.path import dirname, join 2.12 +from allpy import protein 2.13 +from jinja2 import Template 2.14 + 2.15 +template_prefix = dirname(sys.modules[__name__].__file__) 2.16 +template_file = join(template_prefix, "markup_to_html.html") 2.17 + 2.18 +parser = optparse.OptionParser(description=__doc__, usage="%prog [options] [infile]") 2.19 +parser.add_option("-o", "--outfile", 2.20 + help="Output file name, default: stdout") 2.21 +parser.add_option("-m", "--import-module", 2.22 + help="Import this module before anything, useful for custom markup classes") 2.23 +options, args = parser.parse_args() 2.24 + 2.25 +outfile = options.outfile and open(options.outfile, "w") or sys.stdout 2.26 +infile = len(args) == 1 and open(args[0]) or sys.stdin 2.27 +if len(args) > 1: 2.28 + parser.error("Too many arguments on the command line") 2.29 + 2.30 +if options.import_module: 2.31 + sys.path.append(".") 2.32 + __import__(options.import_module) 2.33 + 2.34 +aln = protein.Alignment().append_file(infile, format="markup") 2.35 + 2.36 +def repr_items(items): 2.37 + return ["{0}: {1}".format(k, v) for k, v in sorted(items)] 2.38 + 2.39 +for sequence in aln.sequences: 2.40 + sequence.add_markup('number') 2.41 + for monomer in sequence: 2.42 + items = repr_items((k,v) for k, v in vars(monomer).items() if k != 'input_code1') 2.43 + items += [""] 2.44 + items += repr_items([('code3', monomer.code3), ('name', monomer.name)]) 2.45 + items += [""] 2.46 + items += repr_items([('sequence ' + k, v) for k, v in vars(sequence).items() if k != 'markups']) 2.47 + monomer.description = "\n".join(items) 2.48 + 2.49 +rows = aln.rows_as_lists() 2.50 +template = Template(open(template_file).read()) 2.51 +outfile.write(template.render(vars()))