def bipartition_by_edge(self, e): """Prunes the subtree that attached to the head_node of edge e and returns them as a separate tree.""" t = self._tree nr = e.head_node assert e.tail_node is not None assert e.head_node is not None assert nr.parent_node is e.tail_node is_valid_tree(t) n = self.n_leaves potentially_deleted_nd = e.tail_node grandparent_nd = potentially_deleted_nd.parent_node e.tail_node.remove_child(nr, suppress_deg_two=True) nr.edge.length = None nr.parent_node = None convert_node_to_root_polytomy(nr) t1 = PhylogeneticTree(Tree(seed_node=nr)) n1 = t1.n_leaves # temp we could speed this up, by telling the Phylogenetic tree how many leaves it has if hasattr(e, "num_leaves_below"): if grandparent_nd is None: old_root = potentially_deleted_nd if old_root.edge: old_root.edge.num_leaves_below -= n1 else: if potentially_deleted_nd in grandparent_nd.child_nodes(): potentially_deleted_nd.edge.num_leaves_below -= n1 old_root = grandparent_nd if old_root.edge: old_root.edge.num_leaves_below -= n1 while old_root.parent_node: old_root = old_root.parent_node if old_root.edge: old_root.edge.num_leaves_below -= n1 else: old_root = grandparent_nd or potentially_deleted_nd while old_root.parent_node: old_root = old_root.parent_node t2 = PhylogeneticTree(Tree(seed_node=old_root)) is_valid_tree(t1._tree) is_valid_tree(t2._tree) return t1, t2