Beispiel #1
0
def test_liveshell_do_load(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)
    dbbuddy.server_clients["uniprot"] = Db.UniProtRestClient(dbbuddy)
    dbbuddy.server_clients["uniprot"].http_errors_file.write("Hello!")

    db_session = "%s/mock_resources/test_databasebuddy_clients/dbbuddy_save.db" % hf.resource_path
    monkeypatch.setattr("builtins.input", lambda _: db_session)
    liveshell.do_load(None)
    out, err = capsys.readouterr()
    assert "Session loaded from file.\n\n" in out

    assert dbbuddy.server_clients["uniprot"].http_errors_file.read() == ""
    headings = liveshell.get_headings()
    for heading in ['ACCN', 'DB', 'Type', 'record', 'entry_name', 'length', 'organism-id', 'organism',
                    'protein_names', 'comments', 'gi_num', 'TaxId', 'status', 'name', 'biotype',
                    'object_type', 'strand', 'assembly_name', 'name']:
        assert heading in headings

    for heading in headings:
        assert heading in ['ACCN', 'DB', 'Type', 'record', 'entry_name', 'length', 'organism-id', 'organism',
                           'protein_names', 'comments', 'gi_num', 'TaxId', 'status', 'name', 'biotype',
                           'object_type', 'strand', 'assembly_name', 'name']

    monkeypatch.setattr("builtins.input", lambda _: "/no/file/here")
    liveshell.do_load(None)
    out, err = capsys.readouterr()
    assert "Error: Unable to read the provided file. Are you sure it's a saved DbBuddy live session?\n\n" in out
def test_uniprotrestclient_query_uniprot(capsys, monkeypatch):
    dbbuddy = Db.DbBuddy()
    client = Db.UniProtRestClient(dbbuddy)
    monkeypatch.setattr(Db, 'urlopen', mock_urlopen_handle_uniprot_ids)
    client.query_uniprot("inx15", {"format": "list"})

    assert client.results_file.read() == '''# Search: inx15
A8XEF9
O61786
A0A0H5SBJ0
//
'''
    # Also make sure request_params can come in as a list
    monkeypatch.setattr(Db, 'urlopen', mock_urlopen_handle_uniprot_ids)
    client.query_uniprot("inx15", [{"format": "list"}])

    # Errors
    monkeypatch.setattr(Db, 'urlopen', mock_raise_httperror)
    client.query_uniprot("inx15", [{"format": "list"}])
    assert client.http_errors_file.read() == "Uniprot search failed for 'inx15'\nHTTP Error 101: " \
                                             "Fake HTTPError from Mock\n//\n"

    monkeypatch.setattr(Db, 'urlopen', mock_raise_urlerror_8)
    client.query_uniprot("inx15", [{"format": "list"}])
    assert "Uniprot request failed, are you connected to the internet?" in client.http_errors_file.read()

    monkeypatch.setattr(Db, 'urlopen', mock_raise_urlerror)
    client.query_uniprot("inx15", [{"format": "list"}])
    assert "<urlopen error Fake URLError from Mock>" in client.http_errors_file.read()

    params = {"format": "tab", "columns": "id,entry name,length,organism-id,organism,protein names,comments"}
    client.query_uniprot("ABXEF9", params)
Beispiel #3
0
def test_uniprotrestclient_init():
    dbbuddy = Db.DbBuddy(", ".join(ACCNS[3:6]))
    client = Db.UniProtRestClient(dbbuddy)
    assert hash(dbbuddy) == hash(client.dbbuddy)
    assert client.server == 'http://www.uniprot.org/uniprot'
    assert type(client.http_errors_file) == br.TempFile
    assert type(client.results_file) == br.TempFile
    assert client.max_url == 1000
Beispiel #4
0
def test_liveshell_dump_session(monkeypatch):
    monkeypatch.setattr(Db.LiveShell, "cmdloop", mock_cmdloop)
    dbbuddy = Db.DbBuddy(_databases="uniprot")
    dbbuddy.server_clients["uniprot"] = Db.UniProtRestClient(dbbuddy)
    crash_file = br.TempFile(byte_mode=True)
    liveshell = Db.LiveShell(dbbuddy, crash_file)
    pre_dump = liveshell.crash_file.read()
    liveshell.dump_session()
    assert pre_dump == liveshell.crash_file.read()

    liveshell.dbbuddy.search_terms.append("Blahh")
    liveshell.dump_session()
    assert pre_dump != liveshell.crash_file.read()

    assert liveshell.dbbuddy.server_clients['uniprot'].lock
    assert not liveshell.dbbuddy.server_clients['ensembl']
    assert liveshell.undo
Beispiel #5
0
def test_uniprotrestclient_count_hits(monkeypatch):
    dbbuddy = Db.DbBuddy("inx15,inx16")
    client = Db.UniProtRestClient(dbbuddy)
    monkeypatch.setattr(Db, 'urlopen', mock_urlopen_uniprot_count_hits)
    assert client.count_hits() == 10

    for indx in range(10):
        client.dbbuddy.search_terms.append("a" * 110)
    assert client.count_hits() == 20

    monkeypatch.setattr(Db, 'urlopen', mock_raise_httperror)
    assert client.count_hits() == 0
    assert "d3b8e6bb4b9094117b7555b01dc85f64" in client.dbbuddy.failures

    with pytest.raises(ValueError) as err:
        client.dbbuddy.search_terms[0] = "a" * 1001
        client.count_hits()
    assert "Search term exceeds size limit of 1000 characters." in str(err)
Beispiel #6
0
def test_uniprotrestclient_fetch_proteins(monkeypatch, capsys, hf):
    def patch_query_uniprot_search(*args, **kwargs):
        print("patch_query_uniprot_search\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        client.results_file.write(
            '''# Search: inx15
A8XEF9	A8XEF9_CAEBR	381	6238	Caenorhabditis briggsae	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
O61786	O61786_CAEEL	382	6239	Caenorhabditis elegans	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
A0A0H5SBJ0	A0A0H5SBJ0_BRUMA	129	6279	Brugia malayi (Filarial nematode worm)	Innexin	Function (1); Sequence \
similarities (1); Subcellular location (1)
E3MGD6	E3MGD6_CAERE	384	31234	Caenorhabditis remanei (Caenorhabditis vulgaris)	Innexin	Function (1); \
Sequence similarities (1); Subcellular location (2)
//
# Search: inx16
O61787	INX16_CAEEL	372	6239	Caenorhabditis elegans	Innexin-16 (Protein opu-16)	Function (1); Sequence \
similarities (1); Subcellular location (1)
A0A0V1AZ11	A0A0V1AZ11_TRISP	406	6334	Trichinella spiralis (Trichina worm)	Innexin	Caution (1); Function (1); \
Sequence similarities (1); Subcellular location (2)
A8XEF8	A8XEF8_CAEBR	374	6238	Caenorhabditis briggsae	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
A0A0B2VB60	A0A0B2VB60_TOXCA	366	6265	Toxocara canis (Canine roundworm)	Innexin	Caution (2); Function (1); \
Sequence similarities (1); Subcellular location (1)
A0A0V0W5E2	A0A0V0W5E2_9BILA	410	92179	Trichinella sp. T6	Innexin	Caution (2); Function (1); Sequence \
similarities (1); Subcellular location (1)
//''', "w")
        return

    def patch_query_uniprot_fetch(*args, **kwargs):
        print("patch_query_uniprot_fetch\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        with open("%s/mock_resources/test_databasebuddy_clients/uniprot_fetch.txt" % hf.resource_path, "r") \
                as ifile:
            client.results_file.write(ifile.read(), "w")
        return

    def patch_query_uniprot_fetch_nothing(*args, **kwargs):
        print("patch_query_uniprot_fetch_nothing\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        client.results_file.write(
            "# Search: A8XEF9,O61786,A0A0H5SBJ0,E3MGD6,O61787,A0A0V1AZ11,A8XEF8,A0A0B2VB60,"
            "A0A0V0W5E2\n//\n//", "w")
        return

    monkeypatch.setattr(Db.UniProtRestClient, "query_uniprot", lambda _: True)
    dbbuddy = Db.DbBuddy("inx15,inx16")
    client = Db.UniProtRestClient(dbbuddy)
    client.fetch_proteins()

    out, err = capsys.readouterr()
    assert client.results_file.read() == ""
    assert "full records from UniProt..." not in err

    # Test a single call to query_uniprot
    monkeypatch.setattr(Db.UniProtRestClient, "query_uniprot",
                        patch_query_uniprot_search)
    client.search_proteins()
    monkeypatch.setattr(Db.UniProtRestClient, "query_uniprot",
                        patch_query_uniprot_fetch)
    client.fetch_proteins()
    out, err = capsys.readouterr()
    assert "Requesting 9 full records from UniProt..." in err

    # Test multicore call to query_uniprot
    monkeypatch.setattr(br, "run_multicore_function",
                        patch_query_uniprot_fetch)
    for accn, rec in client.dbbuddy.records.items():
        rec.record = None
    client.dbbuddy.records["a" * 999] = Db.Record("a" * 999,
                                                  _database="uniprot")
    client.fetch_proteins()
    out, err = capsys.readouterr()
    assert "Requesting 10 full records from UniProt..." in err
    seq = str(client.dbbuddy.records["A8XEF9"].record.seq)
    assert hf.string2hash(seq) == "04f13629336cf6cdd5859c8913b742a5"

    # Some edge cases
    monkeypatch.setattr(Db.UniProtRestClient, "query_uniprot",
                        patch_query_uniprot_fetch_nothing)
    client.http_errors_file.write("inx15\n%s\n//\n" %
                                  URLError("Fake URLError from Mock"))

    client.dbbuddy.records = OrderedDict([("a" * 999,
                                           Db.Record("a" * 999,
                                                     _database="uniprot"))])
    client.fetch_proteins()
    out, err = capsys.readouterr()
    assert "Requesting 1 full records from UniProt..." in err
    assert "No sequences returned\n\n" in err
    assert "The following errors were encountered while querying UniProt with fetch_proteins():" in err
    assert hf.string2hash(str(
        client.dbbuddy.records["a" *
                               999])) == "670bf9c6ae5832b42841798d882a7276"

    with pytest.raises(ValueError) as err:
        client.dbbuddy.records["a" * 1001] = Db.Record("a" * 1001,
                                                       _database="uniprot")
        client.fetch_proteins()
    assert "The provided accession or search term is too long (>1000)." in str(
        err)
Beispiel #7
0
def test_uniprotrestclient_search_proteins(monkeypatch, capsys):
    def patch_query_uniprot_multi(*args, **kwargs):
        print("patch_query_uniprot_multi\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        client1.results_file.write(
            '''# Search: inx15
A8XEF9	A8XEF9_CAEBR	381	6238	Caenorhabditis briggsae	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
O61786	O61786_CAEEL	382	6239	Caenorhabditis elegans	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
A0A0H5SBJ0	A0A0H5SBJ0_BRUMA	129	6279	Brugia malayi (Filarial nematode worm)	Innexin	Function (1); Sequence \
similarities (1); Subcellular location (1)
E3MGD6	E3MGD6_CAERE	384	31234	Caenorhabditis remanei (Caenorhabditis vulgaris)	Innexin	Function (1); Sequence \
similarities (1); Subcellular location (2)
//
# Search: inx16
O61787	INX16_CAEEL	372	6239	Caenorhabditis elegans	Innexin-16 (Protein opu-16)	Function (1); Sequence \
similarities (1); Subcellular location (1)
A0A0V1AZ11	A0A0V1AZ11_TRISP	406	6334	Trichinella spiralis (Trichina worm)	Innexin	Caution (1); Function (1); \
Sequence similarities (1); Subcellular location (2)
A8XEF8	A8XEF8_CAEBR	374	6238	Caenorhabditis briggsae	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
A0A0B2VB60	A0A0B2VB60_TOXCA	366	6265	Toxocara canis (Canine roundworm)	Innexin	Caution (2); Function (1); \
Sequence similarities (1); Subcellular location (1)
A0A0V0W5E2	A0A0V0W5E2_9BILA	410	92179	Trichinella sp. T6	Innexin	Caution (2); Function (1); Sequence \
similarities (1); Subcellular location (1)
//''', "w")
        return

    def patch_query_uniprot_single(*args, **kwargs):
        print("patch_query_uniprot_single\nargs: %s\nkwargs: %s" %
              (args, kwargs))
        client2.results_file.write(
            '''# Search: inx15
A8XEF9	A8XEF9_CAEBR	381	6238	Caenorhabditis briggsae	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
O61786	O61786_CAEEL	382	6239	Caenorhabditis elegans	Innexin	Function (1); Sequence similarities (1); \
Subcellular location (2)
A0A0H5SBJ0	A0A0H5SBJ0_BRUMA	129	6279	Brugia malayi (Filarial nematode worm)	Innexin
E3MGD6	E3MGD6_CAERE	384	31234	Caenorhabditis remanei (Caenorhabditis vulgaris)	Innexin
//''', "w")
        return

    monkeypatch.setattr(Db.UniProtRestClient, "count_hits", lambda _: 0)
    dbbuddy = Db.DbBuddy("inx15,inx16")
    client1 = Db.UniProtRestClient(dbbuddy)
    client1.search_proteins()
    out, err = capsys.readouterr()
    assert "Uniprot returned no results\n\n" in err

    monkeypatch.setattr(Db.UniProtRestClient, "count_hits", lambda _: 9)
    monkeypatch.setattr(br, "run_multicore_function",
                        patch_query_uniprot_multi)
    client1.search_proteins()
    out, err = capsys.readouterr()
    assert "Retrieving summary data for 9 records from UniProt\n" in err
    assert "Querying UniProt with 2 search terms (Ctrl+c to abort)\n" in err
    assert len(dbbuddy.records) == 9

    monkeypatch.setattr(Db.UniProtRestClient, "query_uniprot",
                        patch_query_uniprot_single)
    dbbuddy = Db.DbBuddy("inx15")
    client2 = Db.UniProtRestClient(dbbuddy)
    client2.search_proteins()
    out, err = capsys.readouterr()
    assert "Querying UniProt with the search term 'inx15'...\n" in err
    assert len(dbbuddy.records) == 4