def choose_action(tree: str, action_name: str, optional: str = None):
    if action_name in ["--update", "-u"]:
        update(tree)
    elif action_name in ["--show", "-sh"]:
        phy_tree: PhyTree
        if re.match(r'[\S]*\.newick$', tree):
            phy_tree = load_tree(tree)
        elif re.match(r'\([\S ]+\}\;$', tree):
            phy_tree = PhyTree()
            try:
                phy_tree.parse_string(tree)
            except ValueError as e:
                print(e)
                exit()
        else:
            print("Post tree as a filename or string")
            exit()
        visualize_tree(phy_tree.get_newick()[0])
    elif action_name in ["--create", "-c"]:
        create(tree)
    elif action_name in ["--random-tree", "-r"]:
        if optional is None:
            random_tree(tree)
        else:
            random_tree(tree, int(optional))
    elif action_name in ["--cut"]:
        cut(tree)
    else:
        print("Wrong name function")
    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])
Example #3
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)