def simplify_tree(self, tree): root_label = self._simplify_tree(tree) if tree.label in ['Arg1', 'Arg2', 'Conn', 'none']: tree.children = self.get_leave_node(tree) return for i, c in enumerate(tree.children): if self.deeperthan1(c): self.simplify_tree(c) else: n = TreeNode() n.children = [c] n.label = c.label tree.children[i] = n
def coalesce_paths(self, child_paths, t0): """ Create a new TreeNode and assign a given list of child nodes and its host node. :param child_paths: A list of TreeNodes in the pathogen tree. :param t0: Time of pathogen coalescence as height :return: A tuple containing: 1. TreeNode object for the new pathogen lineage. 2. updated extant list """ assert len(child_paths ) == 2, 'Can only coalesce 2 pathogen lineages at a time' p1, p2 = child_paths assert p1 in self.extant_p and p2 in self.extant_p, 'Both pathogen lineages must be extant' assert p1.host == p2.host, 'Can only coalesce pathogen lineages in the same host' host = p1.host assert p1.height < t0 and p2.height < t0, \ 'Pathogen lineage heights %f %f cannot exceed coalescent event %f' % (p1.height, p2.height, t0) # create new pathogen lineage new_path = TreeNode(name='_'.join([x.name for x in child_paths]), dist=0) new_path.add_features(host=host, height=t0) # cast child_paths as a List because ete3.Tree.children requires it new_path.children = list(child_paths) self.extant_p.append(new_path) # coalesced pathogen lineages are no longer extant for node in child_paths: node.up = new_path node.dist = t0 - node.height # when node was created, we stored the height self.extant_p.remove(node) self.not_extant_p.append(node) return new_path