def test_screw_formats_inplace_ui(capsys, pb_odd_resources): temp_file = br.TempFile() with open(pb_odd_resources["compare"], "r") as ifile: temp_file.write(ifile.read()) test_in_args = deepcopy(in_args) test_in_args.screw_formats = "nexus" test_in_args.in_place = True test_in_args.trees[0] = temp_file.path tester = Pb.PhyloBuddy(temp_file.path) Pb.command_line_ui(test_in_args, tester, skip_exit=True) out, err = capsys.readouterr() assert "File overwritten at:" in err check_file = os.path.isfile("%s.nex" % temp_file.path) assert check_file test_in_args.trees[0] = "%s.nex" % temp_file.path test_in_args.screw_formats = "newick" tester = Pb.PhyloBuddy("%s.nex" % temp_file.path) Pb.command_line_ui(test_in_args, tester, skip_exit=True) out, err = capsys.readouterr() assert "File overwritten at:" in err check_file = os.path.isfile("%s.nwk" % temp_file.path) assert check_file
def test_show_unique(pb_odd_resources, pb_resources, hf): tester = Pb.PhyloBuddy(pb_odd_resources['compare']) Pb.show_unique(tester) assert hf.buddy2hash(tester) == "396e27e3c7c5aa126ec07f31307a288e" with pytest.raises(AssertionError): # If only a single tree is present tester = Pb.PhyloBuddy(pb_resources.get_one("m k")) Pb.show_unique(tester)
def test_show_unique(pb_odd_resources, pb_resources, hf): tester = Pb.PhyloBuddy(pb_odd_resources['compare']) Pb.show_unique(tester) assert hf.buddy2hash(tester) == "ea5b0d1fcd7f39cb556c0f5df96281cf" with pytest.raises(AssertionError): # If only a single tree is present tester = Pb.PhyloBuddy(pb_resources.get_one("m k")) Pb.show_unique(tester)
def test_collapse_polytomies_ui(capsys, pb_odd_resources, hf): test_in_args = deepcopy(in_args) test_in_args.collapse_polytomies = [[20]] Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(pb_odd_resources['support']), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "1b0979265205b17ca7f34abbd02f6e26" test_in_args.collapse_polytomies = [[0.1, 'length']] Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(pb_odd_resources['support']), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "252572f7b9566c62df24d57065412240"
def test_split_polytomies_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.split_polytomies = True Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(Pb.PhyloBuddy('(A,(B,C,D));')), skip_exit=True) out, err = capsys.readouterr() assert out in ['(A:1.0,(B:1.0,(C:1.0,D:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(B:1.0,(D:1.0,C:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(C:1.0,(B:1.0,D:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(C:1.0,(D:1.0,B:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(D:1.0,(B:1.0,C:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(D:1.0,(C:1.0,B:1.0):1e-06):1.0):1.0;\n']
def test_collapse_polytomies(pb_odd_resources, hf): tester = Pb.PhyloBuddy(pb_odd_resources['support']) tester = Pb.collapse_polytomies(tester, 20) assert hf.buddy2hash(tester) == "1b0979265205b17ca7f34abbd02f6e26" tester = Pb.PhyloBuddy(pb_odd_resources['support']) tester = Pb.collapse_polytomies(tester, threshold=0.1, mode='length') assert hf.buddy2hash(tester) == "252572f7b9566c62df24d57065412240" with pytest.raises(NameError) as err: Pb.collapse_polytomies(tester, threshold=0.1, mode='foo') assert "Mode must be 'support' or 'length'" in str(err)
def __init__(self): base_dict_structure = {'single': {}, 'multi': {}} self.resources = deepcopy(base_dict_structure) self.resources['single'] = {file_format: name.format(path=RESOURCE_PATH) for file_format, name in [ ("newick", "{path}single_tree.newick"), ("nexus", "{path}single_tree.nex"), ("nexml", "{path}single_tree.xml")]} self.resources['multi'] = {file_format: name.format(path=RESOURCE_PATH) for file_format, name in [ ("newick", "{path}multi_tree.newick"), ("nexus", "{path}multi_tree.nex"), ("nexml", "{path}multi_tree.xml")]} # Create new PhyloBuddy objects for each resrouce file self.pb_objs = deepcopy(base_dict_structure) for num in self.resources: for file_format in self.resources[num]: self.pb_objs[num][file_format] = Pb.PhyloBuddy(self.resources[num][file_format]) self.code_dict = {"num_trees": {"o": "single", "m": "multi"}, "format": {"k": "newick", "n": "nexus", "l": "nexml"}} self.single_letter_codes = {"o": "single", "m": "multi", "k": "newick", "n": "nexus", "l": "nexml"}
def test_unroot_ui(capsys, pb_odd_resources, hf): test_in_args = deepcopy(in_args) test_in_args.unroot = True Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(pb_odd_resources["figtree"]), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "10e9024301b3178cdaed0b57ba33f615"
def test_show_unique_unrooted(monkeypatch, pb_odd_resources, hf): def mock_treeerror(*args): raise TreeError(args) tester = Pb.PhyloBuddy(pb_odd_resources['compare']) Pb.unroot(tester) Pb.show_unique(tester) assert hf.buddy2hash(tester) == "50f1d86072989ac61dcf95ee5fc19f7e"
def test_split_polytomies(): tester = Pb.PhyloBuddy('(A,(B,C,D));') Pb.split_polytomies(tester) assert str(tester) in ['(A:1.0,(B:1.0,(C:1.0,D:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(B:1.0,(D:1.0,C:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(C:1.0,(B:1.0,D:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(C:1.0,(D:1.0,B:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(D:1.0,(B:1.0,C:1.0):1e-06):1.0):1.0;\n', '(A:1.0,(D:1.0,(C:1.0,B:1.0):1e-06):1.0):1.0;\n']
def test_show_unique_ui(capsys, pb_resources, hf, pb_odd_resources): test_in_args = deepcopy(in_args) test_in_args.show_unique = True Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(pb_odd_resources["compare"]), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "ea5b0d1fcd7f39cb556c0f5df96281cf" with pytest.raises(AssertionError) as err: Pb.command_line_ui(test_in_args, pb_resources.get_one("m k"), pass_through=True) assert "PhyloBuddy object should have exactly 2 trees." in str(err)
def test_error(monkeypatch, capsys, pb_resources, pb_odd_resources): test_in_args = deepcopy(in_args) test_in_args.show_unique = [True] Pb.command_line_ui(test_in_args, pb_resources.get_one("o k"), skip_exit=True) out, err = capsys.readouterr() assert "PhyloBuddy object should have exactly 2 trees." in err monkeypatch.setattr(Pb, "_convert_to_ete", mock_assertionerror) with pytest.raises(AssertionError): Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(pb_odd_resources["compare"]), skip_exit=True)
def test_show_unique_unrooted(monkeypatch, pb_odd_resources, hf): def mock_treeerror(*args): raise TreeError(args) tester = Pb.PhyloBuddy(pb_odd_resources['compare']) Pb.unroot(tester) Pb.show_unique(tester) assert hf.buddy2hash(tester) == "2bba16e2c77102ba150adecc352407a9" monkeypatch.setattr(Pb.ete3.TreeNode, 'robinson_foulds', mock_treeerror) with pytest.raises(TreeError): Pb.show_unique(tester)
def test_hash_ids_edges(monkeypatch, pb_resources, hf, pb_odd_resources): class MockRandom(object): def __init__(self): self.values = [ "YCva8wcKxN", "r5iHz5XItJ", "b0dfUBWelI", "Tv0XxZziIQ", "hkwU50UQfq", "Jbn4JWgxXy", "OeGgev9Mdu", "pQRpDbjomQ", "z9JvFAa1vZ", "vHwNvExi6A", "i6tluyP88x", "mXvIO8GYAg", "clDGuGrku8", "atmvoy1uN6", "0R59tE6LgP", "eOUHJPbVaf", "2U0FarU2Lv", "4tITUmacIV", "UND58iKTiH", "aqQWMJnjeu", "rOFRXh4ftJ", "HeAcnkJ8LV", "bdWtyB4OP4", "L0aFqF3oMc", "4HN9dP8uqL", "TtdeGIxxY6", "s4S1RfO9sO", "mIScdjmMzq", "ohGB7fpJGq", "CtLTzCg6RB", "QufRP2p8jN", "NA98oE9g9u", "6IbtWWrdDr", "ZqnxsFXwpT", "hZd46Bc15s", "iL936ARfV9", "iOcjJIWv7Y", "yVkDeDhFpK", "d2amahGHzt", "H9VzjZZdbW", "kxFL0xMfFx", "glhRhmReup", "kxFL0xMfFx" ] self.current_value = self.values.pop() def choice(self, *args): if not self.current_value: self.current_value = self.values.pop() next_char = self.current_value[0] self.current_value = self.current_value[1:] return next_char with pytest.raises(TypeError) as e: Pb.hash_ids(Pb.PhyloBuddy, hash_length="foo") assert "Hash length argument must be an integer, not <class 'str'>" in str( e) with pytest.raises(ValueError) as e: Pb.hash_ids(Pb.PhyloBuddy, hash_length=0) assert "Hash length must be greater than 0" in str(e) with pytest.raises(ValueError) as e: Pb.hash_ids(pb_resources.get_one("m n"), hash_length=1) assert "Insufficient number of hashes available to cover all sequences." in str( e) tester = Pb.PhyloBuddy(pb_odd_resources['node_lables']) test_hash = hf.buddy2hash(tester) tester = Pb.hash_ids(tester, hash_length=5, nodes=True) assert hf.buddy2hash(tester) != test_hash monkeypatch.setattr(Pb.random, "Random", MockRandom) tester = Pb.hash_ids(pb_resources.get_one("o n")) assert hf.buddy2hash(tester) == "48b1b2b0e1f7012ea1a964269300ac6b"
def test_list_ids_ui(capsys, pb_resources, hf): test_in_args = deepcopy(in_args) test_in_args.list_ids = [True] Pb.command_line_ui(test_in_args, pb_resources.get_one("m k"), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "4f0857e0211ff0cd058d0cf7cbaf64d5" test_in_args.list_ids = [4] Pb.command_line_ui(test_in_args, pb_resources.get_one("m k"), skip_exit=True) out, err = capsys.readouterr() assert hf.string2hash(out) == "bf2fbfe1bd52e9b27ae21f5c06e7763a" test_in_args.list_ids = [4] Pb.command_line_ui(test_in_args, Pb.PhyloBuddy("(, );"), skip_exit=True) out, err = capsys.readouterr() assert out == "#### tree_1 ####\nNone\n\n"
def test_unroot(pb_odd_resources, hf): tester = Pb.PhyloBuddy(pb_odd_resources['figtree']) Pb.unroot(tester) assert hf.buddy2hash(tester) == "10e9024301b3178cdaed0b57ba33f615"
def test_rename_nodes(pb_odd_resources, hf): tester = Pb.PhyloBuddy(pb_odd_resources["node_lables"]) Pb.rename(tester, "Equus|Ruminantiamorpha|Canis", "Family") assert hf.buddy2hash(tester) == "46ba6ce0f3a1859b4c7326a6f5e69263"