changeset 67:19c48f2ec775

wrap alignments if they are too long
author Jan Kanis <jan.code@jankanis.nl>
date Tue, 17 Jun 2014 18:07:02 +0200
parents e3dd39906eef
children fa8a93bdefd7
files blast2html.html.jinja blast2html.py
diffstat 2 files changed, 32 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/blast2html.html.jinja	Wed Jun 04 16:28:45 2014 +0200
+++ b/blast2html.html.jinja	Tue Jun 17 18:07:02 2014 +0200
@@ -603,7 +603,9 @@
                     </tr>
                   </table>
 
-                  <pre class=alignmentgraphic>{{hsp|alignment_pre}}</pre>
+                  {% for alignment_line in hsp|alignment_pre %}
+                  <pre class=alignmentgraphic>{{alignment_line}}</pre>
+                  {% endfor %}
                 </div>
                 {% endfor %}
 
--- a/blast2html.py	Wed Jun 04 16:28:45 2014 +0200
+++ b/blast2html.py	Tue Jun 17 18:07:02 2014 +0200
@@ -83,11 +83,35 @@
 
 @filter
 def alignment_pre(hsp):
-    return (
-        "Query  {:>7s}  {}  {}\n".format(hsp['Hsp_query-from'].text, hsp.Hsp_qseq, hsp['Hsp_query-to']) +
-        "       {:7s}  {}\n".format('', hsp.Hsp_midline) +
-        "Subject{:>7s}  {}  {}".format(hsp['Hsp_hit-from'].text, hsp.Hsp_hseq, hsp['Hsp_hit-to'])
-    )
+    step = 60
+
+    def split(txt):
+        return [txt[i:i+step] for i in range(0, len(txt), step)]
+
+    qfrom = int(hsp['Hsp_query-from'])
+    qto = int(hsp['Hsp_query-to'])
+    hfrom = int(hsp['Hsp_hit-from'])
+    hto = int(hsp['Hsp_hit-to'])
+    qseq = hsp.Hsp_qseq.text
+    midline = hsp.Hsp_midline.text
+    hseq = hsp.Hsp_hseq.text
+    
+    offset = 0
+    for qs, mid, hs, offset in zip(split(qseq), split(midline), split(hseq), range(0, len(qseq), step)):
+        yield (
+            "Query  {:>7}  {}  {}\n".format(qfrom+offset, qs, qfrom+len(qs)-1) +
+            "       {:7}  {}\n".format('', mid) +
+            "Subject{:>7}  {}  {}".format(hfrom+offset, hs, hfrom+len(hs)-1)
+        )
+        
+    if qfrom+len(qseq)-1 != qto:
+        warnings.warn("Error in BlastXML input: Hsp node {} qseq length mismatch: from {} to {} length {}".format(
+            nodeid(hsp), qfrom, qto, len(qseq)))
+    if hfrom+len(hseq)-1 != hto:
+        warnings.warn("Error in BlastXML input: Hsp node {} hseq length mismatch: from {} to {} length {}".format(
+            nodeid(hsp), hfrom, hto, len(hseq)))
+
+    
 
 @filter('len')
 def blastxml_len(node):