changeset 75:4d2c25baf5a3 py2.6

Fix rounding errors
author Jan Kanis <jan.code@jankanis.nl>
date Wed, 18 Jun 2014 17:50:32 +0200
parents 03e044b5bcc2
children 7d0d46168fd5
files blast2html.html.jinja blast2html.py
diffstat 2 files changed, 12 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/blast2html.html.jinja	Wed Jun 18 16:58:25 2014 +0200
+++ b/blast2html.html.jinja	Wed Jun 18 17:50:32 2014 +0200
@@ -597,9 +597,9 @@
                       <td>{{hsp['Hsp_bit-score']|fmt('.1f')}} bits({{hsp.Hsp_score}})</td>
                       <td>{{hsp.Hsp_evalue|fmt('.1f')}}</td>
                       <td>{{ hsp.Hsp_identity }}/{{ hsp|len }}({{
-                        (hsp.Hsp_identity/hsp|len) |fmt('.0%') }})</td>
+                        ((hsp.Hsp_identity|float)/hsp|len) | fmt('.0%') }})</td>
                       <td>{{ hsp.Hsp_gaps }}/{{ hsp|len
-                        }}({{ (hsp.Hsp_gaps / hsp|len) | fmt('.0%') }})</td>
+                        }}({{ ((hsp.Hsp_gaps|float) / hsp|len) | fmt('.0%') }})</td>
                       <td>{{ hsp['Hsp_query-frame']|asframe }}/{{ hsp['Hsp_hit-frame']|asframe }}</td>
                     </tr>
                   </table>
--- a/blast2html.py	Wed Jun 18 16:58:25 2014 +0200
+++ b/blast2html.py	Wed Jun 18 17:50:32 2014 +0200
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
-# Actually this program works with both python 2 and 3
+# Actually this program works with both python 2 and 3, tested against python 2.6
 
 # Copyright The Hyve B.V. 2014
 # License: GPL version 3 or (at your option) any higher version
@@ -12,6 +12,7 @@
 import math
 import warnings
 import six, codecs
+from six.moves import builtins
 from os import path
 from itertools import repeat
 import argparse
@@ -20,7 +21,7 @@
 
 
 
-_filters = dict()
+_filters = dict(int='int', float='float')
 def filter(func_or_name):
     "Decorator to register a function as filter in the current jinja environment"
     if isinstance(func_or_name, six.string_types):
@@ -241,7 +242,10 @@
             try:
                 environment.filters[filtername] = getattr(self, funcname)
             except AttributeError:
-                environment.filters[filtername] = globals()[funcname]
+                try:
+                    environment.filters[filtername] = globals()[funcname]
+                except KeyError:
+                    environment.filters[filtername] = getattr(builtins, funcname)
 
     def render(self, output):
         template = self.environment.get_template(self.templatename)
@@ -256,8 +260,6 @@
         result = template.render(blast=self.blast,
                                  iterations=self.blast.BlastOutput_iterations.Iteration,
                                  colors=self.colors,
-                                 # match_colors=self.match_colors(),
-                                 # hit_info=self.hit_info(),
                                  genelink=genelink,
                                  params=params)
         if six.PY2:
@@ -327,13 +329,14 @@
                 return (float(hsp[path]) for hsp in hsps)
 
             yield dict(hit = hit,
-                      title = firsttitle(hit),
+                       title = firsttitle(hit),
                        maxscore = "{0:.1f}".format(max(hsp_val('Hsp_bit-score'))),
                        totalscore = "{0:.1f}".format(sum(hsp_val('Hsp_bit-score'))),
                        cover = "{0:.0%}".format(cover_count / query_length),
                        e_value = "{0:.4g}".format(min(hsp_val('Hsp_evalue'))),
                        # FIXME: is this the correct formula vv?
-                       ident = "{0:.0%}".format(float(min(hsp.Hsp_identity / blastxml_len(hsp) for hsp in hsps))),
+                       # float(...) because non-rounding division doesn't work with lxml elements in python 2.6
+                       ident = "{0:.0%}".format(float(min(float(hsp.Hsp_identity) / blastxml_len(hsp) for hsp in hsps))),
                        accession = hit.Hit_accession)