Skip to content
This repository
Browse code

NewickIO: parse branch lengths in scientific notation (Issue #220)

Included a unit test for it, too.

Thanks to mathli for reporting this bug and offering a fix.
  • Loading branch information...
commit d9c78ca590fc79e53aad664aa2fc202117a906f9 1 parent b8b630b
Eric Talevich authored
17  Bio/Phylo/NewickIO.py
@@ -20,13 +20,13 @@
20 20
 class NewickError(Exception):
21 21
     """Exception raised when Newick object construction cannot continue."""
22 22
     pass
23  
-    
24  
-    
  23
+
  24
+
25 25
 tokens = [
26 26
     (r"\(",                         'open parens'),
27 27
     (r"\)",                         'close parens'),
28 28
     (r"[^\s\(\)\[\]\'\:\;\,]+",     'unquoted node label'),
29  
-    (r"\:[0-9]*\.?[0-9]+",          'edge length'),
  29
+    (r"\:[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)?", 'edge length'),
30 30
     (r"\,",                         'comma'),
31 31
     (r"\[(\\.|[^\]])*\]",           'comment'),
32 32
     (r"\'(\\.|[^\'])*\'",           'quoted node label'),
@@ -70,11 +70,11 @@ def _parse_confidence(text):
70 70
         # assert 0 <= current_clade.confidence <= 1
71 71
     except ValueError:
72 72
         return None
73  
-        
74  
-        
  73
+
  74
+
75 75
 def _format_comment(text):
76 76
     return '[%s]' % (text.replace('[', '\\[').replace(']', '\\]'))
77  
-    
  77
+
78 78
 def _get_comment(clade):
79 79
     if hasattr(clade, 'comment') and clade.comment:
80 80
         return _format_comment(str(clade.comment))
@@ -207,12 +207,13 @@ def new_clade(self, parent=None):
207 207
     def process_clade(self, clade):
208 208
         """Final processing of a parsed clade. Removes the node's parent and
209 209
         returns it."""
210  
-        if (clade.name and not (self.values_are_confidence or self.comments_are_confidence)
  210
+        if (clade.name and not (self.values_are_confidence or
  211
+                                self.comments_are_confidence)
211 212
             and clade.confidence is None):
212 213
             clade.confidence = _parse_confidence(clade.name)
213 214
             if not clade.confidence is None:
214 215
                 clade.name = None
215  
-            
  216
+
216 217
         if hasattr(clade, 'parent'):
217 218
             parent = clade.parent
218 219
             parent.clades.append(clade)
7  Tests/test_Phylo.py
@@ -67,6 +67,13 @@ def test_newick_write(self):
67 67
                 if c is not None)
68 68
         self.assertEqual(internal_names, set(('E', 'F')))
69 69
 
  70
+    def test_newick_read_scinot(self):
  71
+        """Parse Newick branch lengths in scientific notation."""
  72
+        tree = Phylo.read(StringIO("(foo:1e-1,bar:0.1)"), 'newick')
  73
+        clade_a = tree.clade[0]
  74
+        self.assertEqual(clade_a.name, 'foo')
  75
+        self.assertAlmostEqual(clade_a.branch_length, 0.1)
  76
+
70 77
     def test_format_branch_length(self):
71 78
         """Custom format string for Newick branch length serialization."""
72 79
         tree = Phylo.read(StringIO('A:0.1;'), 'newick')

0 notes on commit d9c78ca

Please sign in to comment.
Something went wrong with that request. Please try again.