/
test_lca_functions.py
428 lines (305 loc) · 13.9 KB
/
test_lca_functions.py
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Tests for functions in lca submodule.
"""
import pytest
from sourmash.lca import lca_utils
from sourmash.lca.lca_utils import (LineagePair, build_tree, find_lca,
taxlist, count_lca_for_assignments,
zip_lineage, display_lineage,
make_lineage, is_lineage_match,
pop_to_rank)
class FakeLCA_Database(object):
def __init__(self):
self._assignments = {}
def _set_lineage_assignment(self, hashval, assignment):
self._assignments[hashval] = assignment
def get_lineage_assignments(self, hashval):
if hashval in self._assignments:
return self._assignments[hashval]
else:
return None
def test_taxlist_1():
assert list(taxlist()) == ['superkingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species', 'strain']
def test_taxlist_2():
assert list(taxlist(include_strain=False)) == ['superkingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species']
def test_zip_lineage_1():
x = [ LineagePair('superkingdom', 'a'), LineagePair('phylum', 'b') ]
assert zip_lineage(x) == ['a', 'b', '', '', '', '', '', '']
def test_zip_lineage_2():
x = [ LineagePair('superkingdom', 'a'), LineagePair('phylum', 'b') ]
assert zip_lineage(x, truncate_empty=True) == ['a', 'b']
def test_zip_lineage_3():
x = [ LineagePair('superkingdom', 'a'), LineagePair(None, ''), LineagePair('class', 'c') ]
assert zip_lineage(x) == ['a', '', 'c', '', '', '', '', '']
def test_zip_lineage_3_truncate():
x = [ LineagePair('superkingdom', 'a'), LineagePair(None, ''), LineagePair('class', 'c') ]
assert zip_lineage(x, truncate_empty=True) == ['a', '', 'c']
def test_zip_lineage_4():
x = [ LineagePair('superkingdom', 'a'), LineagePair('class', 'c') ]
with pytest.raises(ValueError) as e:
zip_lineage(x)
assert 'incomplete lineage at phylum - is class instead' in str(e.value)
def test_display_lineage_1():
x = [ LineagePair('superkingdom', 'a'), LineagePair('phylum', 'b') ]
assert display_lineage(x) == "a;b", display_lineage(x)
def test_display_lineage_2():
x = [ LineagePair('superkingdom', 'a'), LineagePair(None, ''), LineagePair('class', 'c') ]
assert display_lineage(x) == "a;;c", display_lineage(x)
def test_build_tree():
tree = build_tree([[LineagePair('rank1', 'name1'),
LineagePair('rank2', 'name2')]])
assert tree == { LineagePair('rank1', 'name1'):
{ LineagePair('rank2', 'name2') : {}} }
def test_build_tree_2():
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2a')],
[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2b')],
])
assert tree == { LineagePair('rank1', 'name1'): { LineagePair('rank2', 'name2a') : {},
LineagePair('rank2', 'name2b') : {}} }
def test_build_tree_3(): # empty 'rank2' name
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', '')]])
assert tree == { LineagePair('rank1', 'name1'): {} }
def test_build_tree_4():
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2a')],
])
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2b')],
], tree)
assert tree == { LineagePair('rank1', 'name1'): { LineagePair('rank2', 'name2a') : {},
LineagePair('rank2', 'name2b') : {}} }
def test_build_tree_5():
with pytest.raises(ValueError):
tree = build_tree([])
def test_find_lca():
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2')]])
lca = find_lca(tree)
assert lca == ((LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2'),), 0)
def test_find_lca_2():
tree = build_tree([[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2a')],
[LineagePair('rank1', 'name1'), LineagePair('rank2', 'name2b')],
])
lca = find_lca(tree)
assert lca == ((LineagePair('rank1', 'name1'),), 2)
def test_find_lca_3():
lin1 = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b')
tree = build_tree([lin1, lin2])
lca, reason = find_lca(tree)
assert lca == lin1 # find most specific leaf node
def test_gather_assignments_1():
# test basic mechanics of gather_assignments function
hashval = 12345678
lin = lca_utils.make_lineage('a;b;c')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin ]))
assignments = lca_utils.gather_assignments([hashval], [db])
print(assignments)
assert assignments[hashval] == set([ lin ])
def test_gather_assignments_2():
# test basic mechanics of gather_assignments function with two lineages
hashval = 12345678
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
assignments = lca_utils.gather_assignments([hashval], [db])
print(assignments)
assert assignments[hashval] == set([ lin, lin2 ])
def test_gather_assignments_3():
# test basic mechanics of gather_assignments function with two lineages
# and two hashvals
hashval = 12345678
hashval2 = 87654321
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
db._set_lineage_assignment(hashval2, set([ lin ]))
assignments = lca_utils.gather_assignments([hashval, hashval2], [db])
print(assignments)
assert assignments[hashval] == set([ lin, lin2 ])
assert assignments[hashval2] == set([ lin ])
def test_count_lca_for_assignments_1():
# test basic mechanics of gather_assignments function
hashval = 12345678
lin = lca_utils.make_lineage('a;b;c')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin ]))
assignments = lca_utils.gather_assignments([hashval], [db])
counts = count_lca_for_assignments(assignments)
print(counts)
assert len(counts) == 1
assert counts[lin] == 1
def test_count_lca_for_assignments_2():
# test basic mechanics of gather_assignments function with two lineages
hashval = 12345678
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
assignments = lca_utils.gather_assignments([hashval], [db])
counts = count_lca_for_assignments(assignments)
print(counts)
assert counts[lin] == 0
assert counts[lin2] == 0
assert len(counts) == 1
lca_lin = lca_utils.make_lineage('a;b')
assert counts[lca_lin] == 1
def test_count_lca_for_assignments_3():
# test basic mechanics of gather_assignments function with two lineages
# and two hashvals
hashval = 12345678
hashval2 = 87654321
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
db._set_lineage_assignment(hashval2, set([ lin ]))
assignments = lca_utils.gather_assignments([hashval, hashval2], [db])
counts = count_lca_for_assignments(assignments)
print(counts)
assert len(counts) == 2
assert counts[lin] == 1
assert counts[lin2] == 0
lca_lin = lca_utils.make_lineage('a;b')
assert counts[lca_lin] == 1
def test_count_lca_for_assignments_abund_1():
# test basic mechanics of gather_assignments function
hashval = 12345678
hashval_counts = dict()
hashval_counts[hashval] = 3
lin = lca_utils.make_lineage('a;b;c')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin ]))
assignments = lca_utils.gather_assignments(hashval_counts.keys(), [db])
counts = count_lca_for_assignments(assignments, hashval_counts)
print(counts)
assert len(counts) == 1
assert counts[lin] == 3
def test_count_lca_for_assignments_abund_2():
# test basic mechanics of gather_assignments function with two lineages
hashval = 12345678
hashval_counts = dict()
hashval_counts[hashval] = 3
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
assignments = lca_utils.gather_assignments(hashval_counts, [db])
counts = count_lca_for_assignments(assignments, hashval_counts)
print(counts)
assert counts[lin] == 0
assert counts[lin2] == 0
assert len(counts) == 1
lca_lin = lca_utils.make_lineage('a;b')
assert counts[lca_lin] == 3 # yes!
def test_count_lca_for_assignments_abund_3():
# test basic mechanics of gather_assignments function with two lineages
# and two hashvals
hashval = 12345678
hashval2 = 87654321
hashval_counts = dict()
hashval_counts[hashval] = 2
hashval_counts[hashval2] = 5
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
db._set_lineage_assignment(hashval2, set([ lin ]))
assignments = lca_utils.gather_assignments(hashval_counts, [db])
counts = count_lca_for_assignments(assignments, hashval_counts)
print(counts)
assert len(counts) == 2
assert counts[lin] == 5 # makes sense
assert counts[lin2] == 0 # makes sense
lca_lin = lca_utils.make_lineage('a;b')
assert counts[lca_lin] == 2 # yes!
def test_count_lca_for_assignments_abund_4():
# test basic mechanics of gather_assignments function with three lineages
# and three hashvals
hashval = 12345678
hashval2 = 87654321
hashval3 = 34567891
hashval_counts = dict()
hashval_counts[hashval] = 2
hashval_counts[hashval2] = 5
hashval_counts[hashval3] = 3
lin = lca_utils.make_lineage('a;b;c')
lin2 = lca_utils.make_lineage('a;b;d')
lin3 = lca_utils.make_lineage('a;b;d;e')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ])) # lca: a;b
db._set_lineage_assignment(hashval2, set([ lin ])) # lca: a;b;c
db._set_lineage_assignment(hashval3, set([ lin2, lin3 ])) # a;b;d;e
assignments = lca_utils.gather_assignments(hashval_counts, [db])
counts = count_lca_for_assignments(assignments, hashval_counts)
print(counts)
assert len(counts) == 3
assert counts[lin] == 5 # makes sense b/c hashval2
assert counts[lin2] == 0 # a;b;d (lin2) + a;b;d;e (lin3) -->a;b;d;e (lin3) only
assert counts[lin3] == 3 # hashval3
lca_lin = lca_utils.make_lineage('a;b')
assert counts[lca_lin] == 2 # yes, b/c hashval
def test_count_lca_for_assignments_abund_5():
# test basic mechanics of gather_assignments function with two lineages
# and two hashvals when linages match but one has lower taxo detail
hashval = 12345678
hashval2 = 87654321
hashval_counts = dict()
hashval_counts[hashval] = 2
hashval_counts[hashval2] = 5
lin = lca_utils.make_lineage('a;b;d')
lin2 = lca_utils.make_lineage('a;b;d;e')
db = FakeLCA_Database()
db._set_lineage_assignment(hashval, set([ lin, lin2 ]))
db._set_lineage_assignment(hashval2, set([ lin ]))
assignments = lca_utils.gather_assignments(hashval_counts, [db])
counts = count_lca_for_assignments(assignments, hashval_counts)
print(counts)
assert len(counts) == 2
assert counts[lin] == 5 # makes sense
assert counts[lin2] == 2 # lin+lin2 yield just lin2
def test_is_lineage_match_1():
# basic behavior: match at order and above, but not at family or below.
lin1 = make_lineage('d__a;p__b;c__c;o__d;f__e')
lin2 = make_lineage('d__a;p__b;c__c;o__d;f__f')
assert is_lineage_match(lin1, lin2, 'superkingdom')
assert is_lineage_match(lin1, lin2, 'phylum')
assert is_lineage_match(lin1, lin2, 'class')
assert is_lineage_match(lin1, lin2, 'order')
assert not is_lineage_match(lin1, lin2, 'family')
assert not is_lineage_match(lin1, lin2, 'genus')
assert not is_lineage_match(lin1, lin2, 'species')
def test_is_lineage_match_2():
# match at family, and above, levels; no genus or species to match
lin1 = make_lineage('d__a;p__b;c__c;o__d;f__f')
lin2 = make_lineage('d__a;p__b;c__c;o__d;f__f')
assert is_lineage_match(lin1, lin2, 'superkingdom')
assert is_lineage_match(lin1, lin2, 'phylum')
assert is_lineage_match(lin1, lin2, 'class')
assert is_lineage_match(lin1, lin2, 'order')
assert is_lineage_match(lin1, lin2, 'family')
assert not is_lineage_match(lin1, lin2, 'genus')
assert not is_lineage_match(lin1, lin2, 'species')
def test_is_lineage_match_3():
# one lineage is empty
lin1 = make_lineage('')
lin2 = make_lineage('d__a;p__b;c__c;o__d;f__f')
assert not is_lineage_match(lin1, lin2, 'superkingdom')
assert not is_lineage_match(lin1, lin2, 'family')
assert not is_lineage_match(lin1, lin2, 'order')
assert not is_lineage_match(lin1, lin2, 'class')
assert not is_lineage_match(lin1, lin2, 'phylum')
assert not is_lineage_match(lin1, lin2, 'genus')
assert not is_lineage_match(lin1, lin2, 'species')
def test_pop_to_rank_1():
# basic behavior - pop to order?
lin1 = make_lineage('d__a;p__b;c__c;o__d')
lin2 = make_lineage('d__a;p__b;c__c;o__d;f__f')
print(lin1)
print(pop_to_rank(lin2, 'order'))
assert pop_to_rank(lin2, 'order') == lin1
def test_pop_to_rank_2():
# what if we're already above rank?
lin2 = make_lineage('d__a;p__b;c__c;o__d;f__f')
print(pop_to_rank(lin2, 'species'))
assert pop_to_rank(lin2, 'species') == lin2