def test_root_ui_mrca(capsys): test_in_args = deepcopy(in_args) test_in_args.root = [["firSA25a", "penSH31b"]] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "757489907bd5c82882d084ffcb22cfba"
def test_root_ui_leaf(capsys): test_in_args = deepcopy(in_args) test_in_args.root = [["firSA25a"]] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "f32bdc34bfe127bb0453a80cf7b01302"
def test_screw_formats_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.screw_formats = "nexus" Pb.command_line_ui(test_in_args, pb_resources.get_one("m k"), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "543d2fc90ca1f391312d6b8fe896c59c"
def test_screw_formats_inplace_ui(capsys): temp_file = MyFuncs.TempFile() with open(resource("compare_trees.newick"), "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 over-written 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 over-written at:" in err check_file = os.path.isfile("%s.nwk" % temp_file.path) assert check_file
def test_screw_formats_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.screw_formats = "nexus" Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "543d2fc90ca1f391312d6b8fe896c59c"
def test_root_ui_midpoint(capsys): test_in_args = deepcopy(in_args) test_in_args.root = [[]] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "25ea14c2e89530a0fb48163c0ef2a102"
def test_rename_ids_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.rename_ids = ['Mle', 'Phylo'] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "6843a620b725a3a0e0940d4352f2036f"
def test_unroot_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.unroot = True Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(resource("figtree.nexus")), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "10e9024301b3178cdaed0b57ba33f615"
def test_num_tips_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.num_tips = True Pb.command_line_ui(test_in_args, pb_resources.get_one("m k"), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "f43920f4df66e76fbacae4af178eeebb"
def test_print_trees_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.print_trees = True Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "fe340117cb8f573100c00fc897e6c8ce"
def test_stdout(capsys): Pb._stdout("Hello std_out", quiet=False) out, err = capsys.readouterr() assert out == "Hello std_out" Pb._stdout("Hello std_out", quiet=True) out, err = capsys.readouterr() assert out == ""
def test_stderr(capsys): Pb._stderr("Hello std_err", quiet=False) out, err = capsys.readouterr() assert err == "Hello std_err" Pb._stderr("Hello std_err", quiet=True) out, err = capsys.readouterr() assert err == ""
def test_show_unique(): tester = Pb.PhyloBuddy(resource("compare_trees.newick")) Pb.show_unique(tester) assert phylo_to_hash(tester) == "ea5b0d1fcd7f39cb556c0f5df96281cf" with pytest.raises(AssertionError): # If only a single tree is present tester = Pb.PhyloBuddy(Pb.make_copy(pb_objects[0])) Pb.show_unique(tester)
def test_print_trees_internal_ui(capsys): # Most of this function is covered repeatedly below, so just test the -t flag test_in_args = deepcopy(in_args) test_in_args.test = True test_in_args.unroot = True Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert err == "*** Test passed ***\n"
def test_generate_tree_ui2(): test_in_args = deepcopy(in_args) test_in_args.in_format, test_in_args.out_format = "nexus", "newick" test_in_args.trees = [resource("Mnemiopsis_cds.nex")] test_in_args.generate_tree = [["fasttree", "-s 12345"]] # Command doesn't exist in fasttree with pytest.raises(SystemExit): Pb.command_line_ui(test_in_args, [])
def test_generate_tree_ui3(): test_in_args = deepcopy(in_args) test_in_args.in_format, test_in_args.out_format = "nexus", "newick" test_in_args.trees = [resource("Mnemiopsis_cds.nex")] test_in_args.generate_tree = [["foo"]] with pytest.raises(SystemExit): Pb.command_line_ui(test_in_args, [])
def test_phyml_inputs(): # Nucleotide tester = Alb.AlignBuddy(resource("Mnemiopsis_cds.nex")) tester = Pb.generate_tree(tester, 'phyml', '-m GTR --r_seed 12345') assert phylo_to_hash(tester) == 'd3a4e7601998885f333ddd714ca764db' # Peptide tester = Alb.AlignBuddy(resource("Mnemiopsis_pep.nex")) tester = Pb.generate_tree(tester, 'phyml', '-m Blosum62 --r_seed 12345') assert phylo_to_hash(tester) == '52c7d028341b250bcc867d57a68c794c'
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_in_place_ui(capsys): # Some of this function is covered below, so just test an edge test_in_args = deepcopy(in_args) test_in_args.in_place = True test_in_args.trees = [Pb.make_copy(pb_objects[0])] test_in_args.screw_formats = "nexus" Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert "Warning: The -i flag was passed in, but the positional" in err
def test_generate_tree_ui1(capsys): test_in_args = deepcopy(in_args) test_in_args.in_format, test_in_args.out_format = "nexus", "newick" test_in_args.trees = [resource("Mnemiopsis_cds.nex")] test_in_args.generate_tree = [["fasttree", "-seed 12345"]] Pb.command_line_ui(test_in_args, [], skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "d7f505182dd1a1744b45cc326096f70c"
def test_raxml_inputs(): # Nucleotide tester = Alb.AlignBuddy(resource("Mnemiopsis_cds.nex")) tester = Pb.generate_tree(tester, 'raxml') assert phylo_to_hash(tester) == '706ba436f8657ef3aee7875217dd07c0' # Peptide tester = Alb.AlignBuddy(resource("Mnemiopsis_pep.nex")) tester = Pb.generate_tree(tester, 'raxml') assert phylo_to_hash(tester) == 'fc35569091eeba49ac4dcec7fc6890bf'
def test_argparse_init(capsys): sys.argv = ['PhyloBuddy.py', resource("compare_trees.newick")] temp_in_args, phylobuddy = Pb.argparse_init() assert string2hash(str(phylobuddy)) == "d8e14a2bfc8e9c0ac3c524f5fb478c67" sys.argv += ["-f", "foo"] with pytest.raises(SystemExit): Pb.argparse_init() out, err = capsys.readouterr() assert "Error: The format 'foo' passed in with the -f flag is not recognized." in err
def test_fasttree_inputs(): temp_dir = MyFuncs.TempDir() # Nucleotide alignbuddy = Alb.AlignBuddy(resource("Mnemiopsis_cds.nex")) tester = Pb.generate_tree(alignbuddy, 'fasttree', '-seed 12345') assert phylo_to_hash(tester) == 'd7f505182dd1a1744b45cc326096f70c' alignbuddy = Alb.AlignBuddy(resource("Mnemiopsis_pep.nex")) tester = Pb.generate_tree(alignbuddy, 'fasttree', '-seed 12345', keep_temp="%s/new_dir" % temp_dir.path) assert phylo_to_hash(tester) == '57eace9bdd2074297cbd2692c1f4cd38'
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_show_unique_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.show_unique = True Pb.command_line_ui(test_in_args, Pb.PhyloBuddy(resource("compare_trees.newick")), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "ea5b0d1fcd7f39cb556c0f5df96281cf" with pytest.raises(SystemExit): Pb.command_line_ui(test_in_args, pb_resources.get_one("m k")) out, err = capsys.readouterr() assert err == "AssertionError: PhyloBuddy object should have exactly 2 trees.\n"
def test_display_trees_ui(capsys, monkeypatch): test_in_args = deepcopy(in_args) test_in_args.display_trees = True show = mock.Mock(return_value=True) monkeypatch.setattr(ete3.TreeNode, "show", show) Pb.command_line_ui(test_in_args, pb_resources.get_one("o k"), skip_exit=True) # noinspection PyUnresolvedReferences with mock.patch.dict('os.environ'): del os.environ['DISPLAY'] Pb.command_line_ui(test_in_args, pb_resources.get_one("o k"), skip_exit=True) out, err = capsys.readouterr() assert "Error: Your system is non-graphical, so display_trees can not work." in err
def get(self, code="", mode="objs"): """ Returns copies of PhyloBuddy objects, the :param code: :param mode: {"objs", "paths"} :return: OrderedDict {key: resource} """ files = self._parse_code(code) output = OrderedDict() key = ["", ""] for num_aligns in files["num_trees"]: key[0] = num_aligns n = self.code_dict["num_trees"][num_aligns] for _format in files["format"]: key[1] = _format f = self.code_dict["format"][_format] try: assert not " ".join(key) in output if mode == "objs": output[" ".join(key)] = Pb.make_copy(self.pb_objs[n][f]) elif mode == "paths": output[" ".join(key)] = self.res_paths[n][f] else: raise ValueError("The 'mode' parameter only accepts 'objs' or 'paths' as input.") except KeyError: pass return output
def test_generate_trees_edge_cases(): temp_file = MyFuncs.TempFile() tester = Alb.AlignBuddy(resource("Mnemiopsis_cds.nex")) with pytest.raises(FileExistsError): Pb.generate_tree(tester, "raxml", keep_temp=temp_file.path) with pytest.raises(AttributeError): Pb.generate_tree(tester, "foo") with pytest.raises(FileNotFoundError): Pb.generate_tree(tester, "raxml", "-m BINCATLG") with pytest.raises(RuntimeError): Pb.generate_tree(tester, "fasttree", "-s 12345") # noinspection PyUnresolvedReferences with mock.patch.dict(os.environ, {"PATH": ""}): with pytest.raises(ProcessLookupError): Pb.generate_tree(tester, "raxml")
def test_distance_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.distance = [False] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "7a9f9902ef30ca3d7ac97b63cfdd0b2e" test_in_args.distance = ["uwrf"] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "6e520f6911aabdf91dfd075d1545bc1e" test_in_args.distance = ["ed"] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "f0acdd9c751bce77fe14355cc77b69cc"
def test_consensus_tree_ui(capsys): test_in_args = deepcopy(in_args) test_in_args.consensus_tree = [False] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "f20cbd5aae5971cce8efbda15e4e0b7e" test_in_args.consensus_tree = [0.9] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "447862ed1ed6e98f2fb535ecce70218b" test_in_args.consensus_tree = [1.5] Pb.command_line_ui(test_in_args, Pb.make_copy(pb_objects[0]), skip_exit=True) out, err = capsys.readouterr() assert string2hash(out) == "f20cbd5aae5971cce8efbda15e4e0b7e"
def test_root_leaf(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) tester = Pb.root(tester, "firSA25a") assert hf.buddy2hash(tester) == next_hash
def test_consensus_tree(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) tester = Pb.consensus_tree(tester) assert hf.buddy2hash(tester) == next_hash, tester.write("error_files%s%s" % (next_hash, os.path.sep))
def test_distance_ed(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) output = str(Pb.distance(tester, method='ed')) assert hf.string2hash(output) == next_hash, print(output)
def test_raxml(alb_resources, hf, monkeypatch): mock_tmp_dir = br.TempDir() tmp_dir = br.TempDir() root, dirs, files = next(os.walk("%smock_resources%stest_raxml_inputs" % (RES_PATH, os.path.sep))) for _file in files: shutil.copyfile("%s%s%s" % (root, os.path.sep, _file), "%s%s%s" % (mock_tmp_dir.path, os.path.sep, _file)) monkeypatch.setattr(Pb.shutil, "which", lambda *_: True) monkeypatch.setattr(Pb, "Popen", MockPopen) monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir) # basic tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml') assert hf.buddy2hash(tester) == "1cede6c576bb88125e2387d850f813ab", tester.write("temp.del") # quiet tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml', quiet=True) assert hf.buddy2hash(tester) == "1cede6c576bb88125e2387d850f813ab", tester.write("temp.del") # params tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml', "-w path{0}to{0}nowhere".format(os.path.sep), quiet=True) assert hf.buddy2hash(tester) == "1cede6c576bb88125e2387d850f813ab", tester.write("temp.del") # slight edges tester = alb_resources.get_one("o p n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml-HPC', tmp_dir.path, quiet=True) assert hf.buddy2hash(tester) == "1cede6c576bb88125e2387d850f813ab", tester.write("temp.del") # bootstraps tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml', "-b 1234 -N 4", quiet=True) assert hf.buddy2hash(tester) == "b8a3b6068aa06bd8a70fcc9bda0efad9", tester.write("temp.del") # keep tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP Pb.generate_tree(tester, 'raxml', keep_temp="%s%skeep_files" % (tmp_dir.path, os.path.sep)) root, dirs, files = next(os.walk("%s%skeep_files" % (tmp_dir.path, os.path.sep))) kept_output = "" for file in sorted(files): with open("%s%s%s" % (root, os.path.sep, file), "r") as ifile: kept_output += ifile.read() if os.name == "nt": assert hf.string2hash(kept_output) == "7f2fdfe55dbe805bd994f3f56c79bb1b" else: assert hf.string2hash(kept_output) == "393353fb47861460aecaefa69a6ec55c" # multi-run os.remove("%s%sRAxML_bestTree.result" % (mock_tmp_dir.path, os.path.sep)) tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml', "-N 3", quiet=True) assert hf.buddy2hash(tester) == "e8ce19bba744f0188df2ebb4ffced4d8", tester.write("temp.del") # bipartitions shutil.copy("{0}mock_resources{1}test_raxml_inputs{1}bipartitions{1}RAxML_bipartitions.result".format(RES_PATH, os.path.sep), "%s%s" % (mock_tmp_dir.path, os.path.sep)) tester = alb_resources.get_one("o d n") tester.hash_map = HASH_MAP tester = Pb.generate_tree(tester, 'raxml', "-f b -z {0}{1}RAxML_bootstrap.result " "-t {0}{1}RAxML_bestTree.result".format(tmp_dir.path, os.path.sep)) assert hf.buddy2hash(tester) == "457533ada8e987fd0c50a41aabe1700b"
def test_hash_ids(pb_resources, hf): for phylobuddy in pb_resources.get_list("m o k n l"): orig_hash = hf.buddy2hash(phylobuddy) Pb.hash_ids(phylobuddy) assert hf.buddy2hash(phylobuddy) != orig_hash
def test_list_ids(key, next_hash, pb_resources, hf): tester = str(Pb.list_ids(pb_resources.get_one(key))) assert hf.string2hash(tester) == next_hash
def test_prune_taxa(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) Pb.prune_taxa(tester, 'fir') assert hf.buddy2hash(tester) == next_hash
def test_rename_ids(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) tester = Pb.rename(tester, 'Mle', 'Phylo') assert hf.buddy2hash(tester) == next_hash
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"
def test_root_middle(key, next_hashes, pb_resources, hf): tester = pb_resources.get_one(key) tester = Pb.root(tester) assert hf.buddy2hash(tester) in next_hashes
def test_root_mrca1(key, next_hash, pb_resources, hf): tester = pb_resources.get_one(key) tester = Pb.root(tester, "ovi47[ab]", "penIT11b") assert hf.buddy2hash(tester) == next_hash