def test_add_second_level(self):
        starting_point = "({A},{B}){A B};"
        expected_tree = "({A},({B},{C}){B C}){A B C};"

        phy_tree = PhyTree(loads(starting_point))
        phy_tree.add_to_group("C", "{B}")
        compare_nodes(self, phy_tree.get_newick()[0], loads(expected_tree)[0])
    def test_create_new_tree(self):
        starting_root = "{};"
        first_leaf = "{A};"

        phy_tree = PhyTree(loads(starting_root))
        phy_tree.add_to_group("A", "{}")
        compare_nodes(self, phy_tree.get_newick()[0], loads(first_leaf)[0])
    def test_add_leaf_append_group(self):
        old_tree = "(({A},{B}){A B},{C}){A B C};"
        new_tree = "(({A},{B},{X}){A B X},{C}){A B C X};"
        olf_phy, new_phy = PhyTree(), PhyTree()
        olf_phy.parse_string(old_tree)
        new_phy.parse_string(new_tree)

        olf_phy.add_to_group("X", "{A B}")

        old_newick, new_newick = olf_phy.get_newick(), new_phy.get_newick()
        compare_nodes(self, old_newick[0], new_newick[0])
Esempio n. 4
0
def random_tree(file_name: str, leaves_num: int = 0):
    if leaves_num == 0:
        print("Number of leaves:")
        leaves_num = get_integer()

    leaves_queue = string.ascii_uppercase[:leaves_num]

    tree = PhyTree()
    for l in leaves_queue:
        available_nodes = list(
            filter(lambda n: re.match(r"{[a-zA-Z0-9]*}", n), tree.get_nodes()))
        chosen_node = choice(available_nodes)
        tree.add_to_group(l, chosen_node)

    tree.save(file_name)
Esempio n. 5
0
def modify(file_name: str, tree: PhyTree):
    finished = False
    while not finished:
        print("Tree has following groups:")
        print(tree.get_nodes())
        print("Add leaf:")
        node = input()
        if re.match(r"[{} ]", node):
            print("Leaf name can not include {, }, and space")
            continue
        print(f"Add leaf '{node}' to group:")
        group = input()
        if not re.match(r'[{}]', group):
            print("Target group has to be enclosed by {} brackets")
            continue
        current_structure = dumps(tree.get_newick())
        try:
            tree.add_to_group(node, group)
        except ValueError as e:
            print(e)
            tree.parse_string(current_structure)
        finished = ask_if_finished()
    tree.save(file_name)
 def test_no_leaves_with_the_same_name(self):
     starting_point = "({A},({B},{D}){B D},{C}){A B C D};"
     phy_tree = PhyTree(loads(starting_point))
     with self.assertRaises(ValueError):
         phy_tree.add_to_group("D", "{A}")