Skip to content

Commit

Permalink
Minor bug fix.
Browse files Browse the repository at this point in the history
Traverse tree optionally doesn't reset params.
Fix tip parsing bug.
  • Loading branch information
evogytis committed Aug 21, 2020
1 parent 3125964 commit 3425837
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions baltic/baltic.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def subtree(self,k=None,traverse_condition=None,stem=True):
Returns a new baltic tree instance.
Note - custom traversal functions can result in multitype trees.
If this is undesired call singleType() on the resulting subtree afterwards. """
subtree=copy.deepcopy(self.traverse_tree(k,include_condition=lambda k:True,traverse_condition=traverse_condition))
subtree=copy.deepcopy(self.traverse_tree(k,include_condition=lambda k:True,traverse_condition=traverse_condition,reset_branches=False))

if subtree is None or len([k for k in subtree if k.branchType=='leaf'])==0:
return None
Expand Down Expand Up @@ -256,14 +256,15 @@ def treeStats(self):

print('\nNumbers of objects in tree: %d (%d nodes and %d leaves)\n'%(len(obs),len(nodes),len(self.getExternal()))) ## report numbers of different objects in the tree

def traverse_tree(self,cur_node=None,include_condition=lambda k:k.branchType=='leaf',traverse_condition=lambda k:True,collect=None,verbose=False):
if cur_node==None: ## if no starting point defined - start from root
def traverse_tree(self,cur_node=None,include_condition=lambda k:k.branchType=='leaf',traverse_condition=lambda k:True,collect=None,reset_branches=True,verbose=False):
if reset_branches:
for k in self.Objects: ## reset various parameters
if k.branchType=='node':
k.leaves=set()
k.childHeight=None
k.height=None

if cur_node==None: ## if no starting point defined - start from root
if verbose==True: print('Initiated traversal from root')
cur_node=self.root

Expand All @@ -286,7 +287,7 @@ def traverse_tree(self,cur_node=None,include_condition=lambda k:k.branchType=='l
elif cur_node.branchType=='node': ## cur_node is node
for child in filter(traverse_condition,cur_node.children): ## only traverse through children we're interested
if verbose==True: print('visiting child %s'%(child.index))
self.traverse_tree(cur_node=child,include_condition=include_condition,traverse_condition=traverse_condition,verbose=verbose,collect=collect) ## recurse through children
self.traverse_tree(cur_node=child,include_condition=include_condition,traverse_condition=traverse_condition,verbose=verbose,collect=collect,reset_branches=False) ## recurse through children
if verbose==True: print('child %s done'%(child.index))
assert len(cur_node.children)>0, 'Tried traversing through hanging node without children. Index: %s'%(cur_node.index)
cur_node.childHeight=max([child.childHeight if child.branchType=='node' else child.height for child in cur_node.children])
Expand Down Expand Up @@ -982,7 +983,7 @@ def make_tree(data,ll=None,verbose=False):
"""
patterns = {
'beast_tip': r'(\(|,)([0-9]+)(\[|\:)',
'non_beast_tip': r'(\(|,)(\'|\")*([^\(\):\[]+)(\'|\"|)(\[)*'
'non_beast_tip': r'(\(|,)(\'|\")*([^\(\):\[\'\"]+)(\'|\"|)*(\[)*'
}
if isinstance(data,str)==False: ## tree string is not an instance of string (could be unicode) - convert
data=str(data)
Expand Down

0 comments on commit 3425837

Please sign in to comment.