/
test_index_protocol.py
1278 lines (947 loc) · 37.9 KB
/
test_index_protocol.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests for the 'Index' class and protocol. All Index classes should support
this functionality.
"""
import pytest
import glob
import sourmash
from sourmash import SourmashSignature
from sourmash.index import (
LinearIndex,
ZipFileLinearIndex,
LazyLinearIndex,
MultiIndex,
StandaloneManifestIndex,
IndexSearchResult,
)
from sourmash.index import CounterGather
from sourmash.index.sqlite_index import SqliteIndex
from sourmash.index.revindex import RevIndex
from sourmash.sbt import SBT, GraphFactory
from sourmash.manifest import CollectionManifest, BaseCollectionManifest
from sourmash.lca.lca_db import LCA_Database, load_single_database
from sourmash.minhash import flatten_and_intersect_scaled, flatten_and_downsample_scaled
import sourmash_tst_utils as utils
def _load_three_sigs():
# utility function - load & return these three sigs.
sig2 = utils.get_test_data("2.fa.sig")
sig47 = utils.get_test_data("47.fa.sig")
sig63 = utils.get_test_data("63.fa.sig")
ss2 = sourmash.load_one_signature(sig2, ksize=31)
ss47 = sourmash.load_one_signature(sig47)
ss63 = sourmash.load_one_signature(sig63)
return [ss2, ss47, ss63]
def build_linear_index(runtmp):
ss2, ss47, ss63 = _load_three_sigs()
lidx = LinearIndex()
lidx.insert(ss2)
lidx.insert(ss47)
lidx.insert(ss63)
return lidx
def build_lazy_linear_index(runtmp):
lidx = build_linear_index(runtmp)
return LazyLinearIndex(lidx)
def build_sbt_index(runtmp):
ss2, ss47, ss63 = _load_three_sigs()
factory = GraphFactory(5, 100, 3)
root = SBT(factory, d=2)
root.insert(ss2)
root.insert(ss47)
root.insert(ss63)
return root
def build_sbt_index_save_load(runtmp):
root = build_sbt_index(runtmp)
out = runtmp.output("xyz.sbt.zip")
root.save(out)
return sourmash.load_file_as_index(out)
def build_zipfile_index(runtmp):
from sourmash.save_load import SaveSignatures_ZipFile
location = runtmp.output("index.zip")
with SaveSignatures_ZipFile(location) as save_sigs:
for ss in _load_three_sigs():
save_sigs.add(ss)
idx = ZipFileLinearIndex.load(location)
return idx
def build_multi_index(runtmp):
siglist = _load_three_sigs()
lidx = LinearIndex(siglist)
mi = MultiIndex.load([lidx], [None], None)
return mi
def build_standalone_manifest_index(runtmp):
sig2 = utils.get_test_data("2.fa.sig")
sig47 = utils.get_test_data("47.fa.sig")
sig63 = utils.get_test_data("63.fa.sig")
ss2 = sourmash.load_one_signature(sig2, ksize=31)
ss47 = sourmash.load_one_signature(sig47)
ss63 = sourmash.load_one_signature(sig63)
siglist = [(ss2, sig2), (ss47, sig47), (ss63, sig63)]
rows = []
rows.extend((CollectionManifest.make_manifest_row(ss, loc) for ss, loc in siglist))
mf = CollectionManifest(rows)
mf_filename = runtmp.output("mf.csv")
mf.write_to_filename(mf_filename)
idx = StandaloneManifestIndex.load(mf_filename)
return idx
def build_lca_index(runtmp):
siglist = _load_three_sigs()
db = LCA_Database(31, 1000, "DNA")
for ss in siglist:
db.insert(ss)
return db
def build_lca_index_save_load(runtmp):
db = build_lca_index(runtmp)
outfile = runtmp.output("db.lca.json")
db.save(outfile)
return sourmash.load_file_as_index(outfile)
def build_sqlite_index(runtmp):
filename = runtmp.output("idx.sqldb")
db = SqliteIndex.create(filename)
siglist = _load_three_sigs()
for ss in siglist:
db.insert(ss)
return db
def build_revindex(runtmp):
ss2, ss47, ss63 = _load_three_sigs()
lidx = RevIndex(template=ss2.minhash)
lidx.insert(ss2)
lidx.insert(ss47)
lidx.insert(ss63)
return lidx
def build_lca_index_save_load_sql(runtmp):
db = build_lca_index(runtmp)
outfile = runtmp.output("db.lca.json")
db.save(outfile, format="sql")
x = load_single_database(outfile)
db_load = x[0]
return db_load
#
# create a fixture 'index_obj' that is parameterized by all of these
# building functions.
#
@pytest.fixture(
params=[
build_linear_index,
build_lazy_linear_index,
build_sbt_index,
build_zipfile_index,
build_multi_index,
build_standalone_manifest_index,
build_lca_index,
build_sbt_index_save_load,
build_lca_index_save_load,
build_sqlite_index,
build_lca_index_save_load_sql,
# build_revindex,
]
)
def index_obj(request, runtmp):
build_fn = request.param
# build on demand
return build_fn(runtmp)
###
### generic Index tests go here
###
def test_index_search_exact_match(index_obj):
# search for an exact match
ss2, ss47, ss63 = _load_three_sigs()
sr = index_obj.search(ss2, threshold=1.0)
print([s[1].name for s in sr])
assert len(sr) == 1
assert sr[0].signature.minhash == ss2.minhash
assert sr[0].score == 1.0
def test_index_search_lower_threshold(index_obj):
# search at a lower threshold/multiple results with ss47
ss2, ss47, ss63 = _load_three_sigs()
sr = index_obj.search(ss47, threshold=0.1)
print([s[1].name for s in sr])
assert len(sr) == 2
sr.sort(key=lambda x: -x[0])
assert sr[0].signature.minhash == ss47.minhash
assert sr[0].score == 1.0
assert sr[1].signature.minhash == ss63.minhash
assert round(sr[1].score, 2) == 0.32
def test_index_search_lower_threshold_2(index_obj):
# search at a lower threshold/multiple results with ss63
ss2, ss47, ss63 = _load_three_sigs()
sr = index_obj.search(ss63, threshold=0.1)
print([s[1].name for s in sr])
assert len(sr) == 2
sr.sort(key=lambda x: -x[0])
assert sr[0].signature.minhash == ss63.minhash
assert sr[0].score == 1.0
assert sr[1].signature.minhash == ss47.minhash
assert round(sr[1].score, 2) == 0.32
def test_index_search_higher_threshold_2(index_obj):
# search at a higher threshold/one match
ss2, ss47, ss63 = _load_three_sigs()
# search for sig63 with high threshold => 1 match
sr = index_obj.search(ss63, threshold=0.8)
print([s[1].name for s in sr])
assert len(sr) == 1
sr.sort(key=lambda x: -x[0])
assert sr[0].signature.minhash == ss63.minhash
assert sr[0].score == 1.0
def test_index_search_containment(index_obj):
# search for containment at a low threshold/multiple results with ss63
ss2, ss47, ss63 = _load_three_sigs()
sr = index_obj.search(ss63, do_containment=True, threshold=0.1)
print([s[1].name for s in sr])
assert len(sr) == 2
sr.sort(key=lambda x: -x[0])
assert sr[0].signature.minhash == ss63.minhash
assert sr[0].score == 1.0
assert sr[1].signature.minhash == ss47.minhash
assert round(sr[1].score, 2) == 0.48
def test_index_signatures(index_obj):
# signatures works?
siglist = list(index_obj.signatures())
ss2, ss47, ss63 = _load_three_sigs()
assert len(siglist) == 3
# check md5sums, since 'in' doesn't always work
md5s = set(ss.md5sum() for ss in siglist)
assert ss2.md5sum() in md5s
assert ss47.md5sum() in md5s
assert ss63.md5sum() in md5s
def test_index_signatures_with_location(index_obj):
# signatures_with_location works?
siglist = list(index_obj.signatures_with_location())
ss2, ss47, ss63 = _load_three_sigs()
assert len(siglist) == 3
# check md5sums, since 'in' doesn't always work
md5s = set((ss.md5sum() for ss, loc in siglist))
assert ss2.md5sum() in md5s
assert ss47.md5sum() in md5s
assert ss63.md5sum() in md5s
def test_index_len(index_obj):
# len works?
assert len(index_obj) == 3
def test_index_bool(index_obj):
# bool works?
assert bool(index_obj)
def test_index_location(index_obj):
# location works?
assert str(index_obj.location)
def test_index_manifest(index_obj):
# manifest is either None or a BaseCollectionManifest
manifest = index_obj.manifest
if manifest is not None:
assert isinstance(manifest, BaseCollectionManifest)
def test_index_select_basic(index_obj):
# select does the basic thing ok
idx = index_obj.select(
ksize=31,
moltype="DNA",
abund=False,
containment=True,
scaled=1000,
num=0,
picklist=None,
)
assert len(idx) == 3
siglist = list(idx.signatures())
assert len(siglist) == 3
# check md5sums, since 'in' doesn't always work
md5s = set(ss.md5sum() for ss in siglist)
ss2, ss47, ss63 = _load_three_sigs()
assert ss2.md5sum() in md5s
assert ss47.md5sum() in md5s
assert ss63.md5sum() in md5s
def test_index_select_nada(index_obj):
# select works ok when nothing matches!
# CTB: currently this EITHER raises a ValueError OR returns an empty
# Index object, depending on implementation. :think:
# See: https://github.com/sourmash-bio/sourmash/issues/1940
try:
idx = index_obj.select(ksize=21)
except ValueError:
idx = LinearIndex([])
assert len(idx) == 0
siglist = list(idx.signatures())
assert len(siglist) == 0
def test_index_prefetch(index_obj):
# test basic prefetch
ss2, ss47, ss63 = _load_three_sigs()
# search for ss2
results = []
for result in index_obj.prefetch(ss2, threshold_bp=0):
results.append(result)
assert len(results) == 1
assert results[0].signature.minhash == ss2.minhash
# search for ss47 - expect two results
results = []
for result in index_obj.prefetch(ss47, threshold_bp=0):
results.append(result)
assert len(results) == 2
assert results[0].signature.minhash == ss47.minhash
assert results[1].signature.minhash == ss63.minhash
def test_index_best_containment(index_obj):
# test basic containment search
ss2, ss47, ss63 = _load_three_sigs()
match = index_obj.best_containment(ss2)
assert match
assert match.score == 1.0
assert match.signature.minhash == ss2.minhash
match = index_obj.best_containment(ss47)
assert match
assert match.score == 1.0
assert match.signature.minhash == ss47.minhash
def test_index_best_containment_threshold_1(index_obj):
# test best_containment() method, in some detail
ss2, ss47, ss63 = _load_three_sigs()
# now construct query signatures with specific numbers of hashes --
# note, these signatures all have scaled=1000.
mins = list(sorted(ss2.minhash.hashes))
new_mh = ss2.minhash.copy_and_clear()
# query with empty hashes
assert not new_mh
with pytest.raises(ValueError):
index_obj.best_containment(SourmashSignature(new_mh))
# add one hash
new_mh.add_hash(mins.pop())
assert len(new_mh) == 1
result = index_obj.best_containment(SourmashSignature(new_mh))
assert result
containment, match_sig, name = result
assert containment == 1.0
assert match_sig.minhash == ss2.minhash
# check with a threshold -> should be no results.
with pytest.raises(ValueError):
index_obj.best_containment(SourmashSignature(new_mh), threshold_bp=5000)
# add three more hashes => length of 4
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
assert len(new_mh) == 4
result = index_obj.best_containment(SourmashSignature(new_mh))
assert result
containment, match_sig, name = result
assert containment == 1.0
assert match_sig.minhash == ss2.minhash
# check with a too-high threshold -> should be no results.
with pytest.raises(ValueError):
index_obj.best_containment(SourmashSignature(new_mh), threshold_bp=5000)
def test_best_containment_threshold_5(index_obj):
# test gather() method, in some detail
ss2, ss47, ss63 = _load_three_sigs()
# now construct query signatures with specific numbers of hashes --
# note, these signatures all have scaled=1000.
mins = list(sorted(ss2.minhash.hashes.keys()))
new_mh = ss2.minhash.copy_and_clear()
# add five hashes
for i in range(5):
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
new_mh.add_hash(mins.pop())
# should get a result with no threshold (any match at all is returned)
result = index_obj.best_containment(SourmashSignature(new_mh))
assert result
containment, match_sig, name = result
assert containment == 1.0
assert match_sig.minhash == ss2.minhash
# now, check with a threshold_bp that should be meet-able.
result = index_obj.best_containment(SourmashSignature(new_mh), threshold_bp=5000)
assert result
containment, match_sig, name = result
assert containment == 1.0
assert match_sig.minhash == ss2.minhash
###
### CounterGather tests
###
def create_basic_counter_gather(runtmp):
"Construct a CounterGather class."
return CounterGather
class CounterGather_LinearIndex:
"""
Provides an (inefficient) CounterGather-style class, for
protocol testing purposes.
"""
def __init__(self, orig_query):
"Constructor - take a SourmashSignature that is the original query."
orig_query_mh = orig_query.minhash
if orig_query_mh.scaled == 0:
raise ValueError
# Index object used to actually track matches.
self.idx = LinearIndex()
self.orig_query_mh = orig_query_mh.copy().flatten()
self.query_started = 0
self.scaled = orig_query_mh.scaled
self.locations = {}
def add(self, ss, *, location=None, require_overlap=True):
"Insert potential match."
if self.query_started:
raise ValueError("cannot add more signatures to counter after peek/consume")
# skip duplicates
md5 = ss.md5sum()
if md5 in self.locations:
return
# confirm that this match has an overlap...
add_mh = ss.minhash.flatten()
overlap = self.orig_query_mh.count_common(add_mh, downsample=True)
# ...figure out what scaled we are operating at now...
if overlap:
self.downsample(add_mh.scaled)
elif require_overlap:
raise ValueError("no overlap between query and signature!?")
# ...and add to the Index, while also tracking location!
self.idx.insert(ss)
self.locations[md5] = location
def signatures(self):
"Yield all signatures"
return self.idx.signatures()
def downsample(self, scaled):
"Track highest scaled across all possible matches."
if scaled > self.scaled:
self.scaled = scaled
return self.scaled
def peek(self, cur_query_mh, *, threshold_bp=0):
"""
Find best match to current query within this CounterGather object.
"""
self.query_started = 1
scaled = self.downsample(cur_query_mh.scaled)
cur_query_mh = flatten_and_downsample_scaled(cur_query_mh, scaled)
# no hashes remaining? exit.
if not self.orig_query_mh or not cur_query_mh:
return []
# verify current query is a subset of the original.
if cur_query_mh.contained_by(self.orig_query_mh, downsample=True) < 1:
raise ValueError("current query not a subset of original query")
# did we get a match?
res = self.idx.peek(cur_query_mh, threshold_bp=threshold_bp)
if not res:
return []
sr, intersect_mh = res
# got match - replace location & return.
match = sr.signature
md5 = match.md5sum()
location = self.locations[md5]
return IndexSearchResult(sr.score, match, location), intersect_mh
def consume(self, *args, **kwargs):
self.query_started = 1
return self.idx.consume(*args, **kwargs)
class CounterGather_LCA:
"""
Provides an alternative implementation of a CounterGather-style class,
based on LCA_Database. This is currently just for protocol
and API testing purposes.
"""
def __init__(self, query):
from sourmash.lca.lca_db import LCA_Database
query_mh = query.minhash
if query_mh.scaled == 0:
raise ValueError("must use scaled MinHash")
self.orig_query_mh = query_mh
lca_db = LCA_Database(query_mh.ksize, query_mh.scaled, query_mh.moltype)
self.db = lca_db
self.siglist = {}
self.locations = {}
self.query_started = 0
def add(self, ss, *, location=None, require_overlap=True):
"Add this signature into the counter."
if self.query_started:
raise ValueError("cannot add more signatures to counter after peek/consume")
overlap = self.orig_query_mh.count_common(ss.minhash, True)
if overlap:
self.downsample(ss.minhash.scaled)
elif require_overlap:
raise ValueError("no overlap between query and signature!?")
self.db.insert(ss)
md5 = ss.md5sum()
self.siglist[md5] = ss
self.locations[md5] = location
def signatures(self):
"Yield all signatures."
yield from self.siglist.values()
def downsample(self, scaled):
"Track highest scaled across all possible matches."
if scaled > self.db.scaled:
self.db.downsample_scaled(scaled)
return self.db.scaled
def peek(self, query_mh, *, threshold_bp=0):
"Return next possible match."
from sourmash import SourmashSignature
self.query_started = 1
scaled = self.downsample(query_mh.scaled)
query_mh = query_mh.downsample(scaled=scaled)
if not self.orig_query_mh or not query_mh:
return []
if query_mh.contained_by(self.orig_query_mh, downsample=True) < 1:
raise ValueError("current query not a subset of original query")
query_ss = SourmashSignature(query_mh)
# returns search_result, intersect_mh
try:
result = self.db.best_containment(query_ss, threshold_bp=threshold_bp)
except ValueError:
result = None
if not result:
return []
cont = result.score
match = result.signature
intersect_mh = flatten_and_intersect_scaled(result.signature.minhash, query_mh)
md5 = result.signature.md5sum()
location = self.locations[md5]
new_sr = IndexSearchResult(cont, match, location)
return [new_sr, intersect_mh]
def consume(self, intersect_mh):
self.query_started = 1
@pytest.fixture(
params=[
CounterGather,
CounterGather_LinearIndex,
CounterGather_LCA,
]
)
def counter_gather_constructor(request):
build_fn = request.param
# build on demand
return build_fn
def test_counter_get_signatures(counter_gather_constructor):
# test .signatures() method
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear()
match_mh_2.add_many(range(10, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear()
match_mh_3.add_many(range(15, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
siglist = list(counter.signatures())
assert len(siglist) == 3
assert match_ss_1 in siglist
assert match_ss_2 in siglist
assert match_ss_3 in siglist
def _consume_all(query_mh, counter, threshold_bp=0):
results = []
query_mh = query_mh.to_mutable()
last_intersect_size = None
while 1:
result = counter.peek(query_mh, threshold_bp=threshold_bp)
if not result:
break
sr, intersect_mh = result
print(sr.signature.name, len(intersect_mh))
if last_intersect_size:
assert len(intersect_mh) <= last_intersect_size
last_intersect_size = len(intersect_mh)
counter.consume(intersect_mh)
query_mh.remove_many(intersect_mh.hashes)
results.append((sr, len(intersect_mh)))
return results
def test_counter_gather_1(counter_gather_constructor):
# check a contrived set of non-overlapping gather results,
# generated via CounterGather
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear()
match_mh_2.add_many(range(10, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear()
match_mh_3.add_many(range(15, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
results = _consume_all(query_ss.minhash, counter)
expected = (
["match1", 10],
["match2", 5],
["match3", 2],
)
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_b(counter_gather_constructor):
# check a contrived set of somewhat-overlapping gather results,
# generated via CounterGather. Here the overlaps are structured
# so that the gather results are the same as those in
# test_counter_gather_1(), even though the overlaps themselves are
# larger.
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear()
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear()
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
results = _consume_all(query_ss.minhash, counter)
expected = (
["match1", 10],
["match2", 5],
["match3", 2],
)
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_c_with_threshold(counter_gather_constructor):
# check a contrived set of somewhat-overlapping gather results,
# generated via CounterGather. Here the overlaps are structured
# so that the gather results are the same as those in
# test_counter_gather_1(), even though the overlaps themselves are
# larger.
# use a threshold, here.
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear()
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear()
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
results = _consume_all(query_ss.minhash, counter, threshold_bp=3)
expected = (["match1", 10], ["match2", 5])
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_d_diff_scaled(counter_gather_constructor):
# test as above, but with different scaled.
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear().downsample(scaled=10)
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear().downsample(scaled=20)
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear().downsample(scaled=30)
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
results = _consume_all(query_ss.minhash, counter)
expected = (
["match1", 10],
["match2", 5],
["match3", 2],
)
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_d_diff_scaled_query(counter_gather_constructor):
# test as above, but with different scaled for QUERY.
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1)
query_mh.add_many(range(0, 20))
match_mh_1 = query_mh.copy_and_clear().downsample(scaled=10)
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear().downsample(scaled=20)
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear().downsample(scaled=30)
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# downsample query now -
query_ss = SourmashSignature(query_mh.downsample(scaled=100), name="query")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
results = _consume_all(query_ss.minhash, counter)
expected = (
["match1", 10],
["match2", 5],
["match3", 2],
)
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_e_abund_query(counter_gather_constructor):
# test as above, but abund query
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1, track_abundance=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh, name="query")
match_mh_1 = query_mh.copy_and_clear().flatten()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear().flatten()
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear().flatten()
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter
counter = counter_gather_constructor(query_ss)
counter.add(match_ss_1)
counter.add(match_ss_2)
counter.add(match_ss_3)
# must flatten before peek!
results = _consume_all(query_ss.minhash.flatten(), counter)
expected = (
["match1", 10],
["match2", 5],
["match3", 2],
)
assert len(results) == len(expected), results
for (sr, size), (exp_name, exp_size) in zip(results, expected):
sr_name = sr.signature.name.split()[0]
assert sr_name == exp_name
assert size == exp_size
def test_counter_gather_1_f_abund_match(counter_gather_constructor):
# test as above, but abund query
query_mh = sourmash.MinHash(n=0, ksize=31, scaled=1, track_abundance=1)
query_mh.add_many(range(0, 20))
query_ss = SourmashSignature(query_mh.flatten(), name="query")
match_mh_1 = query_mh.copy_and_clear()
match_mh_1.add_many(range(0, 10))
match_ss_1 = SourmashSignature(match_mh_1, name="match1")
match_mh_2 = query_mh.copy_and_clear()
match_mh_2.add_many(range(7, 15))
match_ss_2 = SourmashSignature(match_mh_2, name="match2")
match_mh_3 = query_mh.copy_and_clear()
match_mh_3.add_many(range(13, 17))
match_ss_3 = SourmashSignature(match_mh_3, name="match3")
# load up the counter