-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_python.py
More file actions
306 lines (196 loc) · 7.67 KB
/
Copy pathtest_python.py
File metadata and controls
306 lines (196 loc) · 7.67 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from pathlib import Path
import numpy as np
import pytest
from finch import FinchError, Multisketch, Sketch, sketch_file
QUERY_FILE = Path(__file__).resolve().parent / "cli/tests/data/query.fa"
REFS_FILE = Path(__file__).resolve().parent / "cli/tests/data/refs.fa"
@pytest.fixture
def small_sketch():
# query.fa is small and so no_strict needs to be enabled otherwise the sequences will get
# rejected as "too few k-mers"
return sketch_file(QUERY_FILE.as_posix(), no_strict=True)
@pytest.fixture
def small_named_sketches():
sketches = []
for name in ("a", "b", "c"):
s = sketch_file(QUERY_FILE.as_posix(), no_strict=True)
s.name = name
sketches.append(s)
return sketches
@pytest.fixture
def sketch():
return sketch_file(REFS_FILE.as_posix(), filter=False)
#
## sketch_file tests
#
def test_sketch_file_no_strict():
s = sketch_file(QUERY_FILE.as_posix(), no_strict=True)
assert isinstance(s, Sketch)
assert len(s) > 0
assert s.seq_length > 0
assert s.num_valid_kmers > 0
assert s.name == QUERY_FILE.as_posix()
assert s.sketch_params["kmer_length"] == 21
@pytest.mark.parametrize(
"input_path,kmer_length,n_hashes",
[(QUERY_FILE, 11, 5), (REFS_FILE, 11, 5), (REFS_FILE, 21, 10)],
)
def test_sketch_file_n_hashes(input_path, kmer_length, n_hashes):
s = sketch_file(
input_path.as_posix(), n_hashes=n_hashes, kmer_length=kmer_length, filter=False
)
assert len(s) <= n_hashes
assert len(s.hashes) <= n_hashes
@pytest.mark.parametrize(
"input_path,kmer_length",
[(REFS_FILE, 11), (REFS_FILE, 21), (REFS_FILE, 31)],
)
def test_sketch_file_kmer_length(input_path, kmer_length):
s = sketch_file(input_path.as_posix(), kmer_length=kmer_length, filter=False)
assert s.sketch_params["kmer_length"] == kmer_length
@pytest.mark.parametrize(
"input_path,kmer_length",
[(QUERY_FILE, 11), (QUERY_FILE, 7), (QUERY_FILE, 17)],
)
def test_sketch_file_kmer_length_no_strict(input_path, kmer_length):
s = sketch_file(
input_path.as_posix(), kmer_length=kmer_length, filter=False, no_strict=True
)
assert s.sketch_params["kmer_length"] == kmer_length
def test_sketch_file_missing_file_raises():
with pytest.raises(FinchError):
sketch_file("/does/not/exist.fa")
def test_sketch_file_too_few_kmers_raises():
# With the default (strict) parameters the tiny test file is rejected.
with pytest.raises(FinchError):
sketch_file(QUERY_FILE.as_posix())
#
## Sketch struct tests
#
def test_new_sketch_is_empty():
s = Sketch("empty")
assert s.name == "empty"
assert len(s) == 0
assert s.seq_length == 0
assert s.num_valid_kmers == 0
assert s.hashes == []
def test_sketch_repr():
assert repr(Sketch("foo")) == '<Sketch "foo">'
def test_sketch_name_setter():
s = Sketch("foo")
s.name = "bar"
assert s.name == "bar"
def test_sketch_comment_setter():
s = Sketch("foo")
s.comment = "a helpful comment"
assert s.comment == "a helpful comment"
def test_sketch_params(small_sketch):
params = small_sketch.sketch_params
assert params["sketch_type"] == "mash"
assert params["kmer_length"] == 21
assert "hash_seed" in params
def test_sketch_copy(small_sketch):
clone = small_sketch.copy()
clone.name = "clone"
assert len(clone) == len(small_sketch)
assert small_sketch.name != "clone"
@pytest.mark.parametrize("sketch_fix", ["small_sketch", "sketch"])
def test_sketch_compare(sketch_fix, request):
sketch = request.getfixturevalue(sketch_fix)
containment, jaccard = sketch.compare(sketch)
assert containment == 1.0
assert jaccard == 1.0
def test_sketch_compare_bounds(small_named_sketches):
a, b, c = small_named_sketches
containment, jaccard = a.compare(b)
assert 0.0 <= containment <= 1.0
assert 0.0 <= jaccard <= 1.0
containment, jaccard = a.compare(c)
assert 0.0 <= containment <= 1.0
assert 0.0 <= jaccard <= 1.0
def test_sketch_compare_counts(small_sketch):
result = small_sketch.compare_counts(small_sketch)
assert len(result) == 8
common = result[0]
assert common == len(small_sketch)
@pytest.mark.parametrize("sketch_fix", ["small_sketch", "sketch"])
def test_compare_matrix_shape(sketch_fix, request):
sketch = request.getfixturevalue(sketch_fix)
other = sketch.copy()
matrix = sketch.compare_matrix(other, other)
assert isinstance(matrix, np.ndarray)
assert matrix.dtype == np.int32
# one row per query sketch, one column per reference hash
assert matrix.shape == (2, len(sketch))
def test_merge_identical_keeps_size(small_sketch):
other = small_sketch.copy()
n_before = len(small_sketch)
small_sketch.merge(other, None)
# merging identical sketches dedupes the hashes, so the size is unchanged
assert len(small_sketch) == n_before
def test_merge_sums_shared_counts(small_sketch):
before = small_sketch.counts.copy()
small_sketch.merge(small_sketch.copy(), None)
assert (small_sketch.counts == before * 2).all()
#
## Multisketch struct tests
#
def test_multisketch_from_sketches(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
assert len(ms) == 3
assert repr(ms) == "<Multisketch (3 sketches)>"
def test_multisketch_getitem_by_index(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
assert ms[0].name == "a"
assert ms[2].name == "c"
def test_multisketch_getitem_by_name(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
assert ms["b"].name == "b"
def test_multisketch_getitem_missing_name_raises(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
with pytest.raises(KeyError):
ms["does-not-exist"]
def test_multisketch_getitem_out_of_range_raises(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
with pytest.raises(IndexError):
ms[42]
def test_multisketch_contains(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
assert "a" in ms
assert "does-not-exist" not in ms
def test_multisketch_add(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches[:1])
ms.add(small_named_sketches[1])
assert len(ms) == 2
assert ms[0].name == "a"
assert ms[1].name == "b"
def test_multisketch_delitem(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
del ms[0]
assert len(ms) == 2
assert [s.name for s in ms] == ["b", "c"]
def test_multisketch_best_match(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
idx, match = ms.best_match(small_named_sketches[0])
assert isinstance(idx, int)
assert isinstance(match, Sketch)
def test_multisketch_filter_to_names(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
ms.filter_to_names(["a", "c"])
assert sorted(s.name for s in ms) == ["a", "c"]
def test_multisketch_filter_to_matches(small_named_sketches):
ms = Multisketch.from_sketches(small_named_sketches)
# identical sketches all match the query perfectly
ms.filter_to_matches(small_named_sketches[0], 1.0)
assert len(ms) == 3
def test_multisketch_save(small_named_sketches, tmp_path):
ms = Multisketch.from_sketches(small_named_sketches)
path = tmp_path / "sketches.bsk"
ms.save(str(path))
assert path.exists()
reopened = Multisketch.open(str(path))
assert len(reopened) == len(ms)
assert sorted(s.name for s in reopened) == ["a", "b", "c"]
def test_multisketch_open_missing_raises():
with pytest.raises(FinchError):
Multisketch.open("/does/not/exist.bsk")