Example #1
0
def test_alignment_edges(monkeypatch, sb_resources):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy(
        "{0}{1}mock_resources{1}test_muscle{1}result".format(
            RES_PATH, os.path.sep), "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # Weird binary given, but muscle found
    tester = sb_resources.get_one("d f")
    with pytest.raises(br.GuessError) as err:
        tester = Alb.generate_msa(tester, "my_mucsle")
    assert "Could not determine format from raw input" in str(err)

    with pytest.raises(AttributeError) as err:
        Alb.generate_msa(tester, "foo")
    assert "foo is not a supported alignment tool." in str(err)

    monkeypatch.setattr(br, "ask", lambda *_: False)
    tmp_dir.subdir("keep_files")
    with pytest.raises(SystemExit):
        Alb.generate_msa(tester,
                         "mafft",
                         keep_temp="%s%skeep_files" %
                         (tmp_dir.path, os.path.sep))
Example #2
0
def test_muscle(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy("{0}mock_resources{1}test_muscle{1}result".format(RES_PATH, os.path.sep),
                "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'muscle')
    assert hf.buddy2hash(tester) == "5ec18f3e0c9f5cf96944a1abb130232f", tester.write("temp.del")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'muscle', quiet=True)
    assert hf.buddy2hash(tester) == "5ec18f3e0c9f5cf96944a1abb130232f", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester, 'muscle', 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", encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "bd85c34b11261aef2d38c7a6a9d20bf9"
Example #3
0
def test_generate_tree_edges(alb_resources, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    for root, dirs, files in os.walk("%smock_resources%stest_fasttree_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)

    with pytest.raises(RuntimeError) as err:
        Pb.generate_tree(alb_resources.get_one("o d n"), 'fasttree', "--nonsense")
    assert "fasttree threw an error. Scroll up for more info." in str(err)

    with pytest.raises(FileExistsError) as err:
        Pb.generate_tree(alb_resources.get_one("o d n"), 'raxml', keep_temp=tmp_dir.path)
    assert "Execution of raxml was halted to prevent files in" in str(err)

    with pytest.raises(FileNotFoundError) as err:
        Pb.generate_tree(alb_resources.get_one("o d n"), 'Foo')
    assert 'Foo failed to generate a tree' in str(err)

    monkeypatch.setattr(Pb.shutil, "which", lambda *_: None)
    with pytest.raises(ProcessLookupError) as err:
        Pb.generate_tree(alb_resources.get_one("o d n"), 'Foo')
    assert "ProcessLookupError: #### Could not find Foo on your system. ####" in str(err)

    monkeypatch.setattr(Pb.Popen, "communicate", lambda *_: ["".encode(), "".encode()])
    with pytest.raises(AttributeError) as err:
        Pb.generate_tree(alb_resources.get_one("o d n"), 'Foo')
    assert "Foo is not a recognized tree inference tool. Please check your spelling (case sensitive)" in str(err)
Example #4
0
def test_mafft(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    tmp_dir.subdir("keep_files")
    shutil.copy("{0}{1}mock_resources{1}test_mafft{1}result".format(RES_PATH, os.path.sep),
                "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'mafft')
    assert hf.buddy2hash(tester) == "f94e0fd591dad83bd94201f0af038904", tester.write("temp.del")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'mafft', quiet=True)
    assert hf.buddy2hash(tester) == "f94e0fd591dad83bd94201f0af038904", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    monkeypatch.setattr(br, "ask", lambda *_: True)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester, 'mafft', 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", encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "6eaa7e087dd42a8d3ffe86d216b917d8"
Example #5
0
def test_fasttree(alb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    for root, dirs, files in os.walk("%smock_resources%stest_fasttree_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(Pb, "check_output", mock_check_output)
    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, 'fasttree')
    assert hf.buddy2hash(
        tester) == "4b2ab8c39f27b9871f9a370ca7d0c4b3", tester.write("temp.del")

    # quiet
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'fasttree', quiet=True)
    assert hf.buddy2hash(
        tester) == "4b2ab8c39f27b9871f9a370ca7d0c4b3", tester.write("temp.del")

    # params
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'fasttree', "-pseudo", quiet=True)
    assert hf.buddy2hash(
        tester) == "4b2ab8c39f27b9871f9a370ca7d0c4b3", tester.write("temp.del")

    # slight edges
    tester = alb_resources.get_one("m p s")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'fasttree-foo', quiet=True)
    assert hf.buddy2hash(
        tester) == "7692ee7edb8df4f91d2bdca6c3767796", tester.write("temp.del")

    # keep
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    Pb.generate_tree(tester,
                     'fasttree',
                     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) == "bcd034f0db63a7b41f4b3b6661200ef3"
    else:
        assert hf.string2hash(
            kept_output) == "10df99cd1696b002f841aa18b78477ca"
Example #6
0
def test_phyml(alb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    for root, dirs, files in os.walk("%smock_resources%stest_phyml_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, 'phyml')
    assert hf.buddy2hash(
        tester) == "9fbcfe1d565b9fd23e2c5fca86019f8e", tester.write("temp.del")

    # quiet
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'phyml', quiet=True)
    assert hf.buddy2hash(
        tester) == "9fbcfe1d565b9fd23e2c5fca86019f8e", tester.write("temp.del")

    # params
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'phyml', "--sequential", quiet=True)
    assert hf.buddy2hash(
        tester) == "9fbcfe1d565b9fd23e2c5fca86019f8e", tester.write("temp.del")

    # slight edges
    tester = alb_resources.get_one("o p n")
    tester.hash_map = HASH_MAP
    tester = Pb.generate_tree(tester, 'phyml-foo', tmp_dir.path, quiet=True)
    assert hf.buddy2hash(
        tester) == "9fbcfe1d565b9fd23e2c5fca86019f8e", tester.write("temp.del")

    # keep
    tester = alb_resources.get_one("o d n")
    tester.hash_map = HASH_MAP
    Pb.generate_tree(tester,
                     'phyml',
                     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) == "a795da6869c5e3a34962a52ec35006ed"
    else:
        assert hf.string2hash(
            kept_output) == "5a3559c264cb4c4779f15a515aaf2286"
Example #7
0
def test_clustalomega(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy(
        "{1}mock_resources{0}test_clustalo{0}result".format(
            os.path.sep, RES_PATH), "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'clustalomega')
    assert hf.buddy2hash(
        tester) == "f5afdc7c76ab822bdc95230329766aba", tester.write("temp.del")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'clustalomega', quiet=True)
    assert hf.buddy2hash(
        tester) == "f5afdc7c76ab822bdc95230329766aba", tester.write("temp.del")

    # params
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester,
                              'clustalomega',
                              "--outfmt=nexus",
                              quiet=True)
    assert hf.buddy2hash(
        tester) == "23d7c9fa33454ed551a5896e532cf552", tester.write("temp.del")

    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester,
                              'clustalomega',
                              "--outfmt=foobar",
                              quiet=True)
    assert hf.buddy2hash(
        tester) == "f5afdc7c76ab822bdc95230329766aba", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester,
                     'clustalomega',
                     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",
                  encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "bad3a345e769d32672d39ee51df295f5"
Example #8
0
def test_clustalw2(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy(
        "{1}mock_resources{0}test_clustalw2{0}result".format(
            os.path.sep, hf.resource_path),
        "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'clustalw2')
    assert hf.buddy2hash(
        tester) == "955440b5139c8e6d7d3843b7acab8446", tester.write("temp.del")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'clustalw2', quiet=True)
    assert hf.buddy2hash(
        tester) == "955440b5139c8e6d7d3843b7acab8446", tester.write("temp.del")

    # params
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'clustalw2', "-output=nexus", quiet=True)
    assert hf.buddy2hash(
        tester) == "f4a61a8c2d08a1d84a736231a4035e2e", tester.write("temp.del")

    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester,
                              'clustalw2',
                              "-output=foobar",
                              quiet=True)
    assert hf.buddy2hash(
        tester) == "955440b5139c8e6d7d3843b7acab8446", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester,
                     'clustalw2',
                     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",
                  encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "7c03d671198d4e6c4bb56c8fb5619fff"
Example #9
0
def test_pagan(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy(
        "{0}{1}mock_resources{1}test_pagan{1}result.fas".format(
            RES_PATH, os.path.sep), "%s%s" % (mock_tmp_dir.path, os.path.sep))
    open("warnings", "w").close()
    assert os.path.isfile("warnings")
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'pagan')
    assert hf.buddy2hash(tester) == "da1c6bb365e2da8cb4e7fad32d7dafdb"
    assert not os.path.isfile("warnings")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'pagan', quiet=True)
    assert hf.buddy2hash(tester) == "da1c6bb365e2da8cb4e7fad32d7dafdb"

    # params
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'pagan', "-f nexus", quiet=True)
    assert hf.buddy2hash(
        tester) == "f93607e234441a2577fa7d8a387ef7ec", tester.write("temp.del")

    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'pagan', "-f foobar", quiet=True)
    assert hf.buddy2hash(
        tester) == "da1c6bb365e2da8cb4e7fad32d7dafdb", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester,
                     'pagan',
                     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",
                  encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "864112dafb4896ba25ece7929ca0a818"
Example #10
0
def test_fasttree_inputs(alb_resources, hf):
    temp_dir = br.TempDir()
    # Nucleotide
    alignbuddy = alb_resources.get_one("o d n")

    tester = Pb.generate_tree(Alb.make_copy(alignbuddy), 'FastTree',
                              '-seed 12345')
    assert hf.buddy2hash(tester) in [
        'd7f505182dd1a1744b45cc326096f70c', 'da8a67cae6f3f70668f7cf04060b7cd8',
        '732c5e9a978cebb1cfce6af6d64950c2'
    ]

    tester = Pb.generate_tree(alignbuddy,
                              'fasttree',
                              '-seed 12345',
                              quiet=True)
    assert hf.buddy2hash(tester) in [
        'd7f505182dd1a1744b45cc326096f70c', 'da8a67cae6f3f70668f7cf04060b7cd8',
        '732c5e9a978cebb1cfce6af6d64950c2'
    ]

    alignbuddy = alb_resources.get_one("o p n")
    tester = Pb.generate_tree(alignbuddy,
                              'fasttree',
                              '-seed 12345',
                              keep_temp="%s%snew_dir" %
                              (temp_dir.path, os.sep))
    assert hf.buddy2hash(tester) in [
        '57eace9bdd2074297cbd2692c1f4cd38', '82d5a9d4f44fbedf29565686a7cdfcaa',
        '682210ef16beedee0e9f43c05edac112'
    ]
Example #11
0
def test_liveshell_append_slash_if_dir():
    tmp_dir = br.TempDir()
    tmp_dir.subfile("test.txt")
    assert Db.LiveShell._append_slash_if_dir(
        tmp_dir.path) == "%s%s" % (tmp_dir.path, os.sep)
    assert Db.LiveShell._append_slash_if_dir(
        tmp_dir.subfiles[0]) == tmp_dir.subfiles[0]
Example #12
0
def test_generate_alignment_keep_temp(monkeypatch, sb_resources):
    tester = sb_resources.get_one("d f")
    temp_dir = br.TempDir()
    temp_dir.subdir("ga_temp_files")

    def ask_false(*ask_args):
        if ask_args:
            pass
        return False

    def ask_true(*ask_args):
        if ask_args:
            pass
        return True

    monkeypatch.setattr(br, "ask", ask_false)
    with pytest.raises(SystemExit):
        Alb.generate_msa(tester,
                         clustalo_bin,
                         keep_temp="%s%sga_temp_files" %
                         (temp_dir.path, os.sep))

    monkeypatch.setattr(br, "ask", ask_true)
    Alb.generate_msa(tester,
                     clustalo_bin,
                     keep_temp="%s%sga_temp_files" % (temp_dir.path, os.sep))
    assert os.path.isfile("{0}{1}ga_temp_files{1}result".format(
        temp_dir.path, os.sep))
    assert os.path.isfile("{0}{1}ga_temp_files{1}tmp.fa".format(
        temp_dir.path, os.sep))
Example #13
0
def test_walklevel():
    tmp_dir = br.TempDir()
    tmp_dir.subdir("mydir")
    tmp_dir.subdir("mydir{0}subdir".format(os.path.sep))
    tmp_dir.subdir("mydir{0}subdir{0}subsubdir".format(os.path.sep))

    tmp_dir.subfile("myfile.txt")
    tmp_dir.subfile("mydir{0}subfile.txt".format(os.path.sep))
    tmp_dir.subfile("mydir{0}subdir{0}subsubfile.txt".format(os.path.sep))

    walker = br.walklevel(tmp_dir.path)
    root, dirs, files = next(walker)
    assert root == tmp_dir.path
    assert dirs == ["mydir"]
    assert files == ["myfile.txt"]

    root, dirs, files = next(walker)
    assert root == "%s%smydir" % (tmp_dir.path, os.path.sep)
    assert dirs == ["subdir"]
    assert files == ["subfile.txt"]

    with pytest.raises(StopIteration):
        next(walker)

    walker = br.walklevel(tmp_dir.path)
    counter = 0
    for _ in walker:
        counter += 1
    assert counter == 2

    walker = br.walklevel(tmp_dir.path, level=2)
    counter = 0
    for _ in walker:
        counter += 1
    assert counter == 3
Example #14
0
def test_tempdir_save():
    save_dir = br.TempDir()
    save_dir.subfile("testfile")
    assert save_dir.save("%s/fakedir" % save_dir.path)
    assert os.path.exists("%s/fakedir" % save_dir.path)
    assert os.path.exists("%s/fakedir/testfile" % save_dir.path)
    assert not save_dir.save("%s/fakedir" % save_dir.path)
Example #15
0
def test_liveshell_complete_load_save_write(monkeypatch):
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    dbbuddy = Db.DbBuddy()
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    tmpdir = br.TempDir()
    os.chdir(tmpdir.path)
    tmpdir.subfile("file.txt")
    tmpdir.subdir("extra_dir")

    # Load
    assert liveshell.complete_load("load fi ", "load fi ", 5,
                                   7) == ['file.txt']
    assert liveshell.complete_load(
        "load ", "load ", 5, 5) == ['extra_dir%s' % os.path.sep, 'file.txt']
    assert not liveshell.complete_load("load ", "load ", 4, 5)

    # Save
    assert liveshell.complete_save("save fi ", "save fi ", 5,
                                   7) == ['file.txt']
    assert liveshell.complete_save(
        "save ", "save ", 5, 5) == ['extra_dir%s' % os.path.sep, 'file.txt']
    assert not liveshell.complete_save("save ", "save ", 4, 5)

    # Save
    assert liveshell.complete_write("write fi ", "write fi ", 6,
                                    8) == ['file.txt']
    assert liveshell.complete_write(
        "write ", "write ", 6, 6) == ['extra_dir%s' % os.path.sep, 'file.txt']
    assert not liveshell.complete_write("write ", "write ", 4, 5)
Example #16
0
def test_liveshell_do_bash(monkeypatch, capsys):
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    dbbuddy = Db.DbBuddy()
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    capsys.readouterr()
    tmp_file = br.TempFile()
    liveshell.do_bash("echo 'hello from bash' > %s" % tmp_file.path)
    assert "hello from bash" in tmp_file.read()

    monkeypatch.setattr(
        "builtins.input",
        lambda _: "echo 'Line from input' > %s" % tmp_file.path)
    liveshell.do_bash(None)
    assert "Line from input" in tmp_file.read()

    liveshell.do_bash("cd /this/path/doesnt/exist")
    out, err = capsys.readouterr()
    assert "-sh: cd: /this/path/doesnt/exist: No such file or directory\n" in out

    tmp_dir = br.TempDir()
    tmp_dir.subfile("foo.txt")
    cwd = os.getcwd()
    liveshell.do_bash("cd %s" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert tmp_dir.path in out
    assert os.path.isfile("foo.txt")
    os.chdir(cwd)
Example #17
0
def test_tempdir_make_dir_generator():
    test_dir = br.TempDir()
    dir_path = test_dir.path
    for _ in test_dir._make_dir():
        pass
    assert not os.path.isdir(dir_path)
    os.makedirs(dir_path)
Example #18
0
def test_prank(sb_resources, hf, monkeypatch):
    mock_tmp_dir = br.TempDir()
    tmp_dir = br.TempDir()
    shutil.copy(
        "{0}{1}mock_resources{1}test_prank{1}result.best.fas".format(
            RES_PATH, os.path.sep), "%s%s" % (mock_tmp_dir.path, os.path.sep))
    monkeypatch.setattr(Alb, "which", lambda *_: True)
    monkeypatch.setattr(Alb, "Popen", MockPopen)
    monkeypatch.setattr(br, "TempDir", lambda: mock_tmp_dir)

    # basic
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'prank')
    assert hf.buddy2hash(
        tester) == "eff3e6728b5126e285a422863567294f", tester.write("temp.del")

    # quiet
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'prank', quiet=True)
    assert hf.buddy2hash(
        tester) == "eff3e6728b5126e285a422863567294f", tester.write("temp.del")

    # params
    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'prank', "-f=nexus", quiet=True)
    assert hf.buddy2hash(
        tester) == "4dcaa948e109487ee12512b6ac02183c", tester.write("temp.del")

    tester = sb_resources.get_one("d f")
    tester = Alb.generate_msa(tester, 'prank', "-f=foobar", quiet=True)
    assert hf.buddy2hash(
        tester) == "eff3e6728b5126e285a422863567294f", tester.write("temp.del")

    # keep
    monkeypatch.setattr(Sb, "hash_ids", mock_hash_ids)
    tester = sb_resources.get_one("d f")
    Alb.generate_msa(tester,
                     'prank',
                     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",
                  encoding="utf-8") as ifile:
            kept_output += ifile.read()
    assert hf.string2hash(kept_output) == "d0564931e20a61b441fc60549f5560a0"
Example #19
0
def test_liveshell_do_write(monkeypatch, capsys, hf):
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    monkeypatch.setattr(Db.LiveShell, "dump_session", lambda _: True)
    dbbuddy = Db.DbBuddy()
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)

    load_file = "%s/mock_resources/test_databasebuddy_clients/dbbuddy_save.db" % hf.resource_path
    liveshell.do_load(load_file)
    capsys.readouterr()
    tmp_dir = br.TempDir()

    # write a summary
    monkeypatch.setattr("builtins.input", lambda _: "%s/save1" % tmp_dir.path)
    liveshell.do_write(None)
    assert os.path.isfile("%s/save1" % tmp_dir.path)
    with open("%s/save1" % tmp_dir.path, "r") as ifile:
        assert len(ifile.read()) == 249980
    out, err = capsys.readouterr()
    assert re.search("1407 summary records.*written to.*save1", out)

    # write ids/accns
    dbbuddy.out_format = "ids"
    monkeypatch.setattr(br, "ask", lambda _: True)
    dbbuddy.records['O14727'].record = Db.Record('O14727', _record=True)
    liveshell.do_write("%s/save2" % tmp_dir.path)
    assert os.path.isfile("%s/save2" % tmp_dir.path)
    with open("%s/save2" % tmp_dir.path, "r") as ifile:
        assert len(ifile.read()) == 18661
    out, err = capsys.readouterr()
    assert re.search("1407 accessions.*written to.*save2", out)

    # Abort summary
    monkeypatch.setattr(br, "ask", lambda _: False)
    liveshell.do_write("%s/save3" % tmp_dir.path)
    assert not os.path.isfile("%s/save3" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Abort..." in out

    # Permission error
    dbbuddy.out_format = "fasta"
    monkeypatch.setattr("builtins.open", OpenPermissionError)
    liveshell.do_write("%s/save4" % tmp_dir.path)
    assert not os.path.isfile("%s/save4" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Error: You do not have write privileges in the specified directory.\n\n" in out

    # File exists
    monkeypatch.setattr(br, "ask", lambda _: False)
    liveshell.do_write("%s/save2" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Abort..." in out
    assert "written" not in out

    # Not a directory
    liveshell.do_write("%s/ghostdir/save5" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "The specified directory does not exist. Please create it before continuing" in out
    assert "written" not in out
Example #20
0
def test_write(sb_resources, hf):
    temp_dir = br.TempDir()
    tester = sb_resources.get_one("d g")
    tester.write("%s/sequences.gb" % temp_dir.path)
    with open("%s/sequences.gb" % temp_dir.path, encoding="utf-8") as ifile:
        assert hf.string2hash(ifile.read()) == "2e02a8e079267bd9add3c39f759b252c"

    tester.write("%s/sequences.fa" % temp_dir.path, out_format="fasta")
    with open("%s/sequences.fa" % temp_dir.path, encoding="utf-8") as ifile:
        assert hf.string2hash(ifile.read()) == "25073539df4a982b7f99c72dd280bb8f"
Example #21
0
def test_setup(capsys, monkeypatch):
    tmp_dir = br.TempDir()
    __init__ = tmp_dir.subfile("__init__")
    monkeypatch.setattr(Bs.buddysuite, "__file__", __init__)
    monkeypatch.setattr(br, "ask", lambda *_: True)
    monkeypatch.setattr("builtins.input", lambda *_: "")
    sys.argv = ['BuddySuite.py', "-s"]
    Bs.main()
    assert os.path.isdir("%s/buddy_data" % tmp_dir.path)
    assert os.path.isfile("%s/buddy_data/config.ini" % tmp_dir.path)
    assert os.path.isfile("%s/buddy_data/buddysuite_usage.json" % tmp_dir.path)
    assert os.path.isfile("%s/buddy_data/cmd_history" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert out == """\
Welcome to BuddySuite!
Let's configure your installation:

Providing a valid email address is recommended if accessing public databases with BuddySuite.
The maintainers of those resources may attempt to contact you before blocking your IP if you are not adhering to their usage limitations.

BuddySuite is able to automatically send anonymized usage statistics and crash reports to the developers as part of the software improvement program.

Success! You're all set.
    Email address:     [email protected]
    Send diagnostics:  True

These choices can be changed at any time by re-running setup.
Enjoy the BuddySuite!

"""
    monkeypatch.setattr(br, "ask", lambda *_: False)
    monkeypatch.setattr("builtins.input", lambda *_: "*****@*****.**")
    Bs.main()
    out, err = capsys.readouterr()
    assert out == """\
Welcome to BuddySuite!
Let's configure your installation:

Providing a valid email address is recommended if accessing public databases with BuddySuite.
The maintainers of those resources may attempt to contact you before blocking your IP if you are not adhering to their usage limitations.

BuddySuite is able to automatically send anonymized usage statistics and crash reports to the developers as part of the software improvement program.

Success! You're all set.
    Email address:     [email protected]
    Send diagnostics:  False

These choices can be changed at any time by re-running setup.
Enjoy the BuddySuite!

"""
    monkeypatch.setattr("builtins.input", lambda *_: "not an email address")
    Bs.main()
    out, err = capsys.readouterr()
    assert out == """\
Example #22
0
def test_write(sb_resources, hf):
    temp_dir = br.TempDir()
    tester = sb_resources.get_one("d g")
    tester.write("%s/sequences.gb" % temp_dir.path)
    with open("%s/sequences.gb" % temp_dir.path, encoding="utf-8") as ifile:
        assert hf.string2hash(
            ifile.read()) == "2e02a8e079267bd9add3c39f759b252c"

    tester.write("%s/sequences.fa" % temp_dir.path, out_format="fasta")
    with open("%s/sequences.fa" % temp_dir.path, encoding="utf-8") as ifile:
        data = ifile.read()
        assert hf.string2hash(data) == "6a9b3b554aa9ddb90ea62967bd26d5b7"
Example #23
0
def test_tempdir_subdirs():
    test_dir = br.TempDir()
    subdir = test_dir.subdir("test")
    assert os.path.exists(subdir)
    assert os.path.exists("{0}/test".format(test_dir.path))

    with pytest.raises(ValueError):
        test_dir.del_subdir("dasdfs")

    test_dir.del_subdir("test")
    assert not os.path.exists(subdir)
    assert not os.path.exists("{0}/test".format(test_dir.path))

    subdir = test_dir.subdir()
    assert os.path.exists(subdir)
    test_dir.del_subdir(subdir)
Example #24
0
def test_liveshell_do_save(monkeypatch, capsys):
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    dbbuddy = Db.DbBuddy()
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    dbbuddy.records["foo"] = "bar"

    # Standard, no problems
    tmp_dir = br.TempDir()
    monkeypatch.setattr("builtins.input",
                        lambda _: "%s/save_dir/save_file1" % tmp_dir.path)
    liveshell.do_save(None)
    out, err = capsys.readouterr()
    assert "Live session saved\n\n" in out
    assert os.path.isfile("%s/save_dir/save_file1.db" % tmp_dir.path)
    with open("%s/save_dir/save_file1.db" % tmp_dir.path, "rb") as ifile:
        assert len(ifile.read()) in [
            279, 281
        ]  # Different versions of python give different file sizes

    # File exists, abort
    monkeypatch.setattr(br, "ask", lambda _, **kwargs: False)
    liveshell.do_save("%s/save_dir/save_file1" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Abort...\n\n" in out

    # PermissionError
    monkeypatch.setattr("builtins.open", OpenPermissionError)
    liveshell.do_save("%s/save_dir/save_file2" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Error: You do not have write privileges to create a file in the specified directory.\n\n" in out
    assert not os.path.isfile("%s/save_dir/save_file2.db" % tmp_dir.path)

    def makedirs_permissionerror(*args, **kwargs):
        print("makedirs_permissionerror\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        raise PermissionError

    monkeypatch.setattr(os, "makedirs", makedirs_permissionerror)
    liveshell.do_save("%s/save_dir/deeper_dir/save_file2" % tmp_dir.path)
    out, err = capsys.readouterr()
    assert "Error: You do not have write privileges to create a directory in the specified path.\n\n" in out
    assert not os.path.isfile(
        "%s/save_dir/deeper_dir/save_file2.db" % tmp_dir.path)
Example #25
0
def test_liveshell_init(monkeypatch, capsys, hf):
    # Default instantiate
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    dbbuddy = Db.DbBuddy()
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    assert type(liveshell.tmpdir) == br.TempDir
    assert liveshell.terminal_default == "\033[m\033[40m\033[97m"
    assert liveshell.prompt == 'DbBuddy> '
    assert hf.string2hash(
        liveshell.doc_leader) == "e71aa4976437bdb0c22eeaacfaea6f9f"
    assert hash(liveshell.dbbuddy) == hash(dbbuddy)
    assert liveshell.crash_file == crash_file
    assert os.path.split(liveshell.history_path)[-1] == "cmd_history"
    assert not liveshell.undo
    assert not liveshell.hash
    assert liveshell.shell_execs == []
    assert type(liveshell.usage) == br.Usage
    out, err = capsys.readouterr()
    assert "Your session is currently unpopulated. Use 'search' to retrieve records." in out

    # Set cmd history path
    tmp_dir = br.TempDir()
    monkeypatch.setitem(Db.CONFIG, "data_dir", tmp_dir.path)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    assert liveshell.history_path == "%s%scmd_history" % (tmp_dir.path, os.sep)

    # Permission error
    os.chmod("%s%scmd_history" % (tmp_dir.path, os.sep), 0o333)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    # Windows does not actually change the permissions with os.chmod...
    if os.name != "nt":
        assert liveshell.history_path == "%s%scmd_history" % (
            liveshell.tmpdir.path, os.sep)

    # Run initial search
    monkeypatch.setattr(Db, "retrieve_summary", lambda _: True)
    dbbuddy = Db.DbBuddy("Inx15")
    Db.LiveShell(dbbuddy, crash_file)
    assert not dbbuddy.records

    Db.LiveShell(dbbuddy, crash_file)
    dbbuddy = Db.DbBuddy("ENSAMEG00000011912")
    assert len(dbbuddy.records) == 1
Example #26
0
def test_inplace(capsys, alb_resources, hf):
    tmp_dir = br.TempDir()
    tester = alb_resources.get_one("o d f")
    tester.write("%s/align" % tmp_dir.path)

    test_in_args = deepcopy(in_args)
    test_in_args.transcribe = True
    test_in_args.in_place = True
    test_in_args.alignments = ["%s/align" % tmp_dir.path]

    Alb.command_line_ui(test_in_args, tester, skip_exit=True)
    out, err = capsys.readouterr()
    tester = Alb.AlignBuddy("%s/align" % tmp_dir.path)
    assert "File over-written at:" in err
    assert hf.buddy2hash(tester) == "8f78e0c99e2d6d7d9b89b8d854e02bcd", tester.write("temp.del")

    test_in_args.alignments = ["I/do/not/exist"]
    Alb.command_line_ui(test_in_args, alb_resources.get_one("o d f"), skip_exit=True)
    out, err = capsys.readouterr()
    assert "Warning: The -i flag was passed in, but the positional argument doesn't seem to be a file." in err
Example #27
0
def test_uninstall(monkeypatch):
    tmp_dir = br.TempDir()
    tmp_dir.subdir("main_site")
    tmp_dir.subdir("main_site/buddysuite")
    __init__ = tmp_dir.subfile("main_site/buddysuite/__init__")
    monkeypatch.setattr(Bs.buddysuite, "__file__", __init__)

    tmp_dir.subfile("seqbuddy")
    tmp_dir.subfile("alignbuddy")
    tmp_dir.subfile("databasebuddy")
    tmp_dir.subfile("phylobuddy")
    tmp_dir.subfile("sb")
    tmp_dir.subfile("something_else")

    monkeypatch.setattr(br, "ask", lambda *_, **__: True)
    monkeypatch.setattr(
        br, "config_values",
        lambda *_: {"shortcuts": ["/not/a/link",
                                  "%s/sb" % tmp_dir.path]})
    monkeypatch.setattr(shutil, "which", lambda buddy: "%s/%s" %
                        (tmp_dir.path, buddy))
    sys.argv = ['BuddySuite.py', "-u"]

    root, dirs, files = next(br.walklevel(tmp_dir.path))
    assert dirs == ["main_site"]
    assert sorted(files) == [
        'alignbuddy', 'databasebuddy', 'phylobuddy', 'sb', 'seqbuddy',
        'something_else'
    ]

    Bs.main()
    root, dirs, files = next(br.walklevel(tmp_dir.path))
    assert dirs == []
    assert files == ["something_else"]

    # Run one more time to ensure that the rmtree FileNotFoundError raises
    Bs.main()
    root, dirs, files = next(br.walklevel(tmp_dir.path))
    assert dirs == []
    assert files == ["something_else"]
Example #28
0
def test_tempdir_subfiles():
    test_dir = br.TempDir()
    subfile = test_dir.subfile("testfile")
    assert os.path.exists(subfile)
    assert os.path.exists("{0}/testfile".format(test_dir.path))
    with open(subfile, 'w') as file_to_write:
        file_to_write.write("hello world")
    with open(subfile, 'r') as file_to_write:
        assert file_to_write.read() == "hello world"
    with open("{0}/testfile".format(test_dir.path), 'r') as file_to_write:
        assert file_to_write.read() == "hello world"

    with pytest.raises(ValueError):
        test_dir.del_subfile("dasdfs")

    test_dir.del_subfile("testfile")
    assert not os.path.exists(subfile)
    assert not os.path.exists("{0}/testfile".format(test_dir.path))

    subfile = test_dir.subfile()
    assert os.path.exists(subfile)
    test_dir.del_subfile(subfile)
Example #29
0
def test_tempdir_init():
    test_dir = br.TempDir()
    assert os.path.exists(test_dir.path)
    assert not len(test_dir.subdirs)
    assert not len(test_dir.subfiles)
Example #30
0
import urllib.request
import argparse
import json
from hashlib import md5
from time import sleep
import datetime
from unittest import mock
import AlignBuddy as Alb
import buddy_resources as br
from pkg_resources import DistributionNotFound
from configparser import ConfigParser
if os.name == "nt":
    import msvcrt

# Globals
TEMP_DIR = br.TempDir()
RESOURCE_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             '..{0}unit_test_resources{0}'.format(os.path.sep))


# Mock resources
class MockLocation(object):
    def __init__(self):
        self.start = 0
        self.end = 1


def mock_valueerror(*args, **kwargs):
    raise ValueError(args, kwargs)