def _add_leaf(node: Node, target: str, leaf: str): if PhyTree._is_group(node.name): if node.name == target: node.add_descendant(Node(f"{{{leaf}}}")) else: if PhyTree._is_group(target): expected_leaves = set(PhyTree._get_group_leaves(target)) else: expected_leaves = {target} for child in node.descendants: if PhyTree._is_group(child.name): child_leaves = set( PhyTree._get_group_leaves(child.name)) if expected_leaves <= child_leaves: PhyTree._add_leaf(child, target, leaf) break elif PhyTree._is_leaf(child.name): if child.name in expected_leaves: PhyTree._add_leaf(child, target, leaf) else: raise ValueError(f"Couldn't find {target}") elif PhyTree._is_leaf(node.name): if node.name == target: if node.name != '{}': node.add_descendant(Node(node.name)) node.add_descendant(Node(f"{{{leaf}}}")) else: node.name = f"{{{leaf}}}" return else: raise ValueError(f"Unexpected leaf: {node.name}") else: raise ValueError(f"Couldn't recognize {leaf} as a leaf or a group") node.name = node.name.replace("}", f" {leaf}}}")
def rename_none_node(node_to_rename: newick.Node, counter): """ Renaming node with no name to differ from other not named node. :param node_to_rename: node to be checked :param counter: int; counter for none nodes :return: (Node, int) """ if node_to_rename.name is None: node_to_rename.name = str(node_to_rename.name) + "_" + str(counter) counter += 1 return node_to_rename, counter
def test_node_parameters_changeability(self): test_obj = Node(name="A") self.assertEqual("A", test_obj.name) test_obj.name = "B" self.assertEqual("B", test_obj.name)
def test_node_parameters_changeability(self): test_obj = Node(name="A") self.assertEqual("A", test_obj.name) test_obj.name = "B" self.assertEqual("B", test_obj.name)