Skip to content
This repository
Browse code

Implementing support values and fixing issue when external parenthese…

…s are missing.
  • Loading branch information...
commit b3d59354929bb4131e7fd77abeaf440c682864af 1 parent b7cd97a
Ben Morris bendmorris authored etal committed

Showing 1 changed file with 38 additions and 10 deletions. Show diff stats Hide diff stats

  1. +38 10 Bio/Phylo/NewickIO.py
48 Bio/Phylo/NewickIO.py
@@ -61,9 +61,10 @@ def from_string(cls, treetext):
61 61 handle = StringIO(treetext)
62 62 return cls(handle)
63 63
64   - def parse(self, values_are_confidence=False, rooted=False):
  64 + def parse(self, values_are_confidence=False, comments_are_confidence=True, rooted=False):
65 65 """Parse the text stream this object was initialized with."""
66 66 self.values_are_confidence = values_are_confidence
  67 + self.comments_are_confidence = comments_are_confidence
67 68 self.rooted = rooted
68 69 buf = ''
69 70 for line in self.handle:
@@ -77,26 +78,46 @@ def parse(self, values_are_confidence=False, rooted=False):
77 78
78 79 def _parse_tree(self, text):
79 80 """Parses the text representation into an Tree object."""
80   - text = text.lstrip()
  81 + text = text.strip()
  82 +
  83 + def make_new_clade(parent=None):
  84 + clade = Newick.Clade(name='')
  85 + if parent: clade.parent = parent
  86 + return clade
  87 +
  88 + root_clade = make_new_clade()
81 89
82 90 def process_clade(clade):
83 91 if not clade.name:
84 92 clade.name = None
85 93 if hasattr(clade, 'branch_length_string'):
86   - clade.branch_length = float(clade.branch_length_string)
  94 + if self.values_are_confidence:
  95 + clade.confidence = float(clade.branch_length_string)
  96 + else:
  97 + clade.branch_length = float(clade.branch_length_string)
87 98 del clade.branch_length_string
  99 + if self.comments_are_confidence and hasattr(clade, 'comment') and clade.comment:
  100 + try:
  101 + clade.confidence = int(clade.comment) / 100.
  102 + except ValueError, TypeError:
  103 + try:
  104 + clade.confidence = float(clade.comment)
  105 + except ValueError, TypeError:
  106 + pass
  107 + if hasattr(clade, 'confidence'):
  108 + try:
  109 + assert 0 <= clade.confidence <= 1
  110 + except AssertionError:
  111 + del clade.confidence
88 112 if hasattr(clade, 'parent'):
89 113 parent = clade.parent
90 114 parent.clades.append(clade)
91 115 del clade.parent
92 116 return parent
  117 + if clade is root_clade:
  118 + return root_clade
93 119
94   - def make_new_clade(parent=None):
95   - clade = Newick.Clade(name='')
96   - if parent: clade.parent = parent
97   - return clade
98   -
99   - current_clade = make_new_clade()
  120 + current_clade = root_clade
100 121 entering_quoted_string = False
101 122 escaped = False
102 123 entering_comment = False
@@ -126,6 +147,12 @@ def make_new_clade(parent=None):
126 147 entering_branch_length = False
127 148
128 149 elif char == ',':
  150 + # if the current clade is the root, then the external parentheses are missing
  151 + # and a new root should be created
  152 + if current_clade is root_clade:
  153 + root_clade = make_new_clade()
  154 + current_clade.parent = root_clade
  155 +
129 156 # start a new child clade at the same level as the current clade
130 157 parent = process_clade(current_clade)
131 158 current_clade = make_new_clade(parent)
@@ -166,8 +193,9 @@ def make_new_clade(parent=None):
166 193 escape = False
167 194
168 195 process_clade(current_clade)
  196 + process_clade(root_clade)
169 197
170   - return Newick.Tree(root=current_clade, rooted=self.rooted)
  198 + return Newick.Tree(root=root_clade, rooted=self.rooted)
171 199
172 200
173 201 # ---------------------------------------------------------

0 comments on commit b3d5935

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