def test_check_group(self): tree = loads("({A},({B},{C}){B C},{D}){A B C D};") try: PhyTree._check_group(tree[0]) except ValueError: raise AssertionError()
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 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_consensus_two_trees(self): first = PhyTree(loads("({A},({B},({C},{D}){C D}){B C D}){A B C D};")) second = PhyTree(loads("(({A},{B}){A B},({C},{D}){C D}){A B C D};")) result = consensus([first, second], .5) compare_nodes(self, result.get_newick()[0], loads("({A},{B},({C},{D}){C D}){A B C D};")[0])
def test_check_group_fails(self): bad_groups = [ "({A},{A B}){A B};", "({A},{B},{C}){A B};", "({A},{B}){A B C};", "({A},{A},{B}){A B};" "{A};" ] for t in bad_groups: with self.assertRaises(ValueError): PhyTree._check_group(loads(t)[0])
def test_consensus_multiple_trees(self): trees = [ PhyTree(loads("({A},({B},({C},{D}){C D}){B C D}){A B C D};")), PhyTree(loads("({A},{B},({C},{D}){C D}){A B C D};")), PhyTree(loads("({A},{B},{C},{D}){A B C D};")), PhyTree(loads("(({A},{B}){A B},({C},{D}){C D}){A B C D};")), ] result = consensus(trees, .6) compare_nodes(self, result.get_newick()[0], loads("({A},{B},({C},{D}){C D}){A B C D};")[0])
def consensus_tree(): trees = [] add_trees = True while add_trees: try: phy_tree = PhyTree() path = input("Tree's path: ") if os.path.isdir(path): files = os.listdir(path) for f in files: p = PhyTree() p.load_file(path + f) trees.append(p) break else: phy_tree.load_file(path) except ValueError as e: print(e) continue trees.append(phy_tree) add_trees = not ask_if_finished() print("Threshold:") threshold = get_float() result = consensus(trees, threshold) path = input("Save as: ") result.save(path)
def test_correct_tree(self): tree = loads("({A},({B},({C},{D}){C D}){B C D},{E}){A B C D E};") try: PhyTree(tree) except ValueError: raise AssertionError()
def load_tree(file_name) -> None or List[Node]: try: newick_tree = load_from_file(file_name) phy_tree = PhyTree(newick_tree) return phy_tree except IOError: print("File not accessible") exit()
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])
def rf_distance_cmd(): print("First tree:") left = get_file_path() print("Second tree:") right = get_file_path() left_tree = PhyTree() left_tree.load_file(left) right_tree = PhyTree() right_tree.load_file(right) distance = rf_distance(left_tree, right_tree) print(f"Distance: {distance}")
def test_trivial_partition(self): tree = PhyTree(loads("(({A},{B}){A B},{C}){A B C}")) partitions = [ ({"B", "C"}, {"A"}), ({"A", "C"}, {"B"}), ({"A", "B"}, {"C"}), ] received_bipartitions: List[tuple] = bipartitions(tree) self.assertCountEqual(received_bipartitions, partitions)
def test_bad_tree(self): bad_trees = [ "(({B},({C},{D}){C D}){B C D},{E}){A B C D E};", "({A},({B},{C D}){B C D},{E}){A B C D E};", "({A},({B},({C},{D}){C D}){B C D},E){A B C};", "({A},({B},({C},{D}){C D}){B X D},{E}){A B C D E};", ] for t in bad_trees: with self.assertRaises(ValueError): PhyTree(loads(t))
def test_single_bipartition(self): tree = PhyTree(loads("(({A},{B}){A B},({C},{D}){C D}){A B C D}")) partitions = [ ({"C", "D"},{"A", "B"}), ({"B", "C", "D"}, {"A"}), ({"A", "C", "D"}, {"B"}), ({"A", "B", "D"}, {"C"}), ({"B", "C", "A"}, {"D"}), ] received_bipartitions: List[tuple] = bipartitions(tree) self.assertCountEqual(received_bipartitions, partitions)
def visualize_tree(root: Node): v, e = PhyTree._get_structure(root) graph = nx.DiGraph() graph.add_edges_from(e) positions = _position_vertices(root, 1) positions = {node: (x, y) for node, (x, y) in zip(v, positions)} nx.draw( graph, positions, font_size=6, with_labels=True) plt.show()
def cut(file_name: str): if not os.path.isfile(file_name): print("Given file has to exist.") return tree = PhyTree() tree.load_file(file_name) wanted_leaves = set() choose_leaves = True while choose_leaves: print(f"Chosen leaves: {wanted_leaves}") leaf = input("Add leaf: ") if leaf not in tree.leaves: print(f"{leaf} was not among the tree's leaves") continue if leaf in wanted_leaves: print(f"{leaf} is already among wanted leaves") continue wanted_leaves.add(leaf) choose_leaves = not ask_if_finished() cutter = TreeCutter() cutter.attach_tree(tree) cutter.choose_leaves(wanted_leaves) new_tree = cutter.cut() new_tree.save(file_name)
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)
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 update(file_name: str): tree = PhyTree() tree.load_file(file_name) modify(file_name, tree)
def create(file_name: str): tree = PhyTree() modify(file_name, tree)
def test_rf_distance_one_common(self): left_tree = PhyTree(loads("(({A},{B}){A B},({C},{D}){C D},{E}){A B C D E}")) right_tree = PhyTree(loads("(({A},{B}){A B},({C},{D},{E}){C D E}){A B C D E}")) distance: int = rf_distance(left_tree, right_tree) self.assertEqual(distance, 2)
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}")
def test_rf_distance_all_different_bipartitions(self): left_tree = PhyTree(loads("(({A},{B}){A B},({C},{D}){C D},{E}){A B C D E}")) right_tree = PhyTree(loads("(({A},{B},{C}){A B C},({D},{E}){D E}){A B C D E}")) distance: int = rf_distance(left_tree, right_tree) self.assertEqual(distance, 6)
def test_detect_group(self): group = "{Item Item}" self.assertTrue(PhyTree._is_group(group))
def setUp(self) -> None: self.tree = PhyTree(loads("({A},({B},{C}){B C},({D},({E},{F}){E F}){D E F}){A B C D E F};"))
def test_is_not_group(self): group = "{Item Item" self.assertFalse(PhyTree._is_group(group))
def test_get_leaves(self): tree = loads("(({A},{B}){A B},({C},{D}){C D}){A B C D};") leaves = {"{A}", "{B}", "{C}", "{D}"} received_leaves, _ = PhyTree._get_leaves_and_groups(tree) self.assertSetEqual(received_leaves, leaves)
def test_leaves_inside_group(self): group = "{A B C D}" leaves = ["{A}", "{B}", "{C}", "{D}"] self.assertListEqual(PhyTree._get_group_leaves(group), leaves)