Permalink
Browse files

NewickIO: parse confidence values less stringently.

Moved confidence parsing to a separate function. Don't require confidences to be
in [0.0, 1.0] and don't treat integer confidences as percentages (i.e. dividing
by 100). These choices are left in the comments for future consideration.
  • Loading branch information...
1 parent 87e6f2c commit 0aee549e72fe5dcf9bcea239d29780706500922a @etal etal committed Feb 11, 2013
Showing with 16 additions and 14 deletions.
  1. +15 −13 Bio/Phylo/NewickIO.py
  2. +1 −1 Tests/test_Phylo.py
View
@@ -58,6 +58,19 @@ def write(trees, handle, plain=False, **kwargs):
# ---------------------------------------------------------
# Input
+def _parse_confidence(text):
+ if text.isdigit():
+ return int(text)
+ # NB: Could make this more consistent by treating as a percentage
+ # return int(text) / 100.
+ try:
+ return float(text)
+ # NB: This should be in [0.0, 1.0], but who knows what people will do
+ # assert 0 <= current_clade.confidence <= 1
+ except ValueError:
+ return None
+
+
class Parser(object):
"""Parse a Newick tree given a file handle.
@@ -110,19 +123,8 @@ def _parse_tree(self, text):
# comment
current_clade.comment = token[1:-1]
if self.comments_are_confidence:
- # check to see if this comment contains a support value
- try:
- current_clade.confidence = int(current_clade.comment) / 100.
- except:
- try:
- current_clade.confidence = float(current_clade.comment)
- except:
- pass
- if hasattr(current_clade, 'confidence') and current_clade.confidence:
- try:
- assert 0 <= current_clade.confidence <= 1
- except AssertionError:
- del current_clade.confidence
+ # Try to use this comment as a numeric support value
+ current_clade.confidence = _parse_confidence(current_clade.comment)
elif token == '(':
# start a new clade, which is a child of the current clade
View
@@ -37,7 +37,7 @@ def test_newick_read_single(self):
self.assertEqual(len(tree.get_terminals()), 33)
self.assertEqual(tree.find_any('Homo sapiens').comment, 'modern human')
self.assertEqual(tree.find_any('Equus caballus').comment, "wild horse; also 'Equus ferus caballus'")
- self.assertEqual(tree.root.confidence, 1.0)
+ self.assertEqual(tree.root.confidence, 100)
tree = Phylo.read(EX_NEXUS2, 'nexus')
self.assertEqual(len(tree.get_terminals()), 658)

0 comments on commit 0aee549

Please sign in to comment.