File: test_api.py

package info (click to toggle)
sourmash 4.9.4-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 54,688 kB
  • sloc: python: 59,380; ansic: 332; makefile: 277; sh: 6
file content (95 lines) | stat: -rw-r--r-- 2,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pytest
import sourmash

import sourmash_tst_utils as utils
from sourmash.sourmash_args import load_one_signature, SaveSignaturesToLocation


@utils.in_tempdir
def test_sourmash_signature_api(c):
    e = sourmash.MinHash(n=1, ksize=20)
    sig = sourmash.SourmashSignature(e)

    with SaveSignaturesToLocation(c.output("xxx.sig")) as save_sigs:
        save_sigs.add(sig)

    sig_x1 = load_one_signature(c.output("xxx.sig"))
    sig_x2 = list(sourmash.load_file_as_signatures(c.output("xxx.sig")))[0]

    assert sig_x1 == sig
    assert sig_x2 == sig


@utils.in_tempdir
def test_load_index_0_no_file(c):
    with pytest.raises(ValueError) as exc:
        sourmash.load_file_as_index(c.output("does-not-exist"))
    assert "Error while reading signatures from " in str(exc.value)


def test_load_index_1():
    testfile = utils.get_test_data("prot/protein.sbt.zip")
    idx = sourmash.load_file_as_index(testfile)

    sigs = list(idx.signatures())
    assert len(sigs) == 2


def test_load_index_2():
    testfile = utils.get_test_data("prot/protein.lca.json.gz")
    idx = sourmash.load_file_as_index(testfile)

    sigs = list(idx.signatures())
    assert len(sigs) == 2


def test_load_index_3():
    testfile = utils.get_test_data("prot/protein/")
    idx = sourmash.load_file_as_index(testfile)

    sigs = list(idx.signatures())
    assert len(sigs) == 2


def test_load_index_4():
    testfile = utils.get_test_data("prot/all.zip")
    idx = sourmash.load_file_as_index(testfile)

    sigs = list(idx.signatures())
    assert len(sigs) == 8


def test_load_index_4_b():
    testfile = utils.get_test_data("prot/protein.zip")
    idx = sourmash.load_file_as_index(testfile)

    sigs = list(idx.signatures())
    assert len(sigs) == 2


def test_load_fasta_as_signature():
    # try loading a fasta file - should fail with informative exception
    testfile = utils.get_test_data("short.fa")

    with pytest.raises(Exception) as exc:
        sourmash.load_file_as_index(testfile)

    print(exc.value)

    assert (
        f"Error while reading signatures from '{testfile}' - got sequences instead! Is this a FASTA/FASTQ file?"
        in str(exc.value)
    )


def test_load_and_search_sbt_api():
    treefile = utils.get_test_data("prot/protein.sbt.zip")
    queryfile = utils.get_test_data(
        "prot/protein/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig"
    )

    tree = sourmash.load_file_as_index(treefile)
    query = load_one_signature(queryfile)

    results = list(tree.search(query, threshold=0))
    assert len(results) == 2