/
test_sketchcomparison.py
864 lines (719 loc) · 35.9 KB
/
test_sketchcomparison.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
"""
Tests for the 'SketchComparison' classes.
"""
import numpy as np
import pytest
from sourmash import load_one_signature
from sourmash.minhash import MinHash
from sourmash.sketchcomparison import FracMinHashComparison, NumMinHashComparison
import sourmash_tst_utils as utils
# can we parameterize scaled too (so don't need separate downsample tests?)
def test_FracMinHashComparison(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == False
assert cmp.cmp_scaled == 1
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.mh1_containment == a.contained_by(b)
assert cmp.mh2_containment == b.contained_by(a)
assert cmp.avg_containment == a.avg_containment(b)
assert cmp.max_containment == a.max_containment(b)
assert cmp.jaccard == a.jaccard(b) == b.jaccard(a)
intersect_mh = a.flatten().intersection(b.flatten())
assert cmp.intersect_mh == intersect_mh == b.flatten().intersection(a.flatten())
assert cmp.total_unique_intersect_hashes == 4
assert cmp.pass_threshold # default threshold is 0; this should pass
if track_abundance:
assert cmp.angular_similarity == a.angular_similarity(b) == b.angular_similarity(a)
assert cmp.cosine_similarity == a.angular_similarity(b) == b.angular_similarity(a)
assert cmp.weighted_intersection(from_mh=cmp.mh1).hashes == intersect_mh.inflate(a).hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2).hashes == intersect_mh.inflate(b).hashes
assert cmp.weighted_intersection(from_abundD=a_values).hashes == intersect_mh.inflate(a).hashes
assert cmp.weighted_intersection(from_abundD=b_values).hashes == intersect_mh.inflate(b).hashes
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
assert cmp.weighted_intersection(from_mh=cmp.mh1).hashes == intersect_mh.hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2).hashes == intersect_mh.hashes
def test_FracMinHashComparison_downsample(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp_scaled = 2
ds_a = a.downsample(scaled=cmp_scaled)
ds_b = b.downsample(scaled=cmp_scaled)
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b, cmp_scaled = cmp_scaled)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.mh1_cmp == ds_a
assert cmp.mh2_cmp == ds_b
assert cmp.ignore_abundance == False
assert cmp.cmp_scaled == cmp_scaled
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.mh1_containment == ds_a.contained_by(ds_b)
assert cmp.mh2_containment == ds_b.contained_by(ds_a)
assert cmp.avg_containment == ds_a.avg_containment(ds_b)
assert cmp.max_containment == ds_a.max_containment(ds_b)
assert cmp.jaccard == ds_a.jaccard(ds_b) == ds_b.jaccard(ds_a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
assert cmp.total_unique_intersect_hashes == 8
assert cmp.pass_threshold # default threshold is 0; this should pass
if track_abundance:
assert cmp.angular_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.cosine_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.weighted_intersection(from_mh=cmp.mh1_cmp).hashes == intersect_mh.inflate(ds_a).hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2_cmp).hashes == intersect_mh.inflate(ds_b).hashes
assert cmp.weighted_intersection(from_abundD=cmp.mh1_cmp.hashes).hashes == intersect_mh.inflate(ds_a).hashes
assert cmp.weighted_intersection(from_abundD=cmp.mh2_cmp.hashes).hashes == intersect_mh.inflate(ds_b).hashes
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
assert cmp.weighted_intersection(from_mh=cmp.mh1_cmp).hashes == intersect_mh.hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2_cmp).hashes == intersect_mh.hashes
def test_FracMinHashComparison_autodownsample(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=2, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp_scaled = 2
ds_a = a.downsample(scaled=cmp_scaled)
ds_b = b.downsample(scaled=cmp_scaled)
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.mh1_cmp == ds_a
assert cmp.mh2_cmp == ds_b
assert cmp.ignore_abundance == False
assert cmp.cmp_scaled == cmp_scaled
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.mh1_containment == ds_a.contained_by(ds_b)
assert cmp.mh2_containment == ds_b.contained_by(ds_a)
assert cmp.avg_containment == ds_a.avg_containment(ds_b)
assert cmp.max_containment == ds_a.max_containment(ds_b)
assert cmp.jaccard == ds_a.jaccard(ds_b) == ds_b.jaccard(ds_a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
assert cmp.total_unique_intersect_hashes == 8
assert cmp.pass_threshold # default threshold is 0; this should pass
if track_abundance:
assert cmp.angular_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.cosine_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.weighted_intersection(from_mh=cmp.mh1_cmp).hashes == intersect_mh.inflate(ds_a).hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2_cmp).hashes == intersect_mh.inflate(ds_b).hashes
assert cmp.weighted_intersection(from_abundD=a_values).hashes == intersect_mh.inflate(a).hashes
assert cmp.weighted_intersection(from_abundD=b_values).hashes == intersect_mh.inflate(b).hashes
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
assert cmp.weighted_intersection(from_mh=cmp.mh1_cmp).hashes == intersect_mh.hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2_cmp).hashes == intersect_mh.hashes
def test_FracMinHashComparison_ignore_abundance(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
intersection_w_abund = {1:8, 3:5, 5:3, 8:3}
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp_scaled = 2
ds_a = a.flatten().downsample(scaled=cmp_scaled)
ds_b = b.flatten().downsample(scaled=cmp_scaled)
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b, cmp_scaled = cmp_scaled, ignore_abundance=True)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == True
assert cmp.cmp_scaled == cmp_scaled
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.mh1_containment == a.contained_by(b)
assert cmp.mh2_containment == b.contained_by(a)
assert cmp.avg_containment == b.avg_containment(a)
assert cmp.max_containment == a.max_containment(b)
assert cmp.jaccard == a.jaccard(b) == b.jaccard(a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
assert cmp.total_unique_intersect_hashes == 8
assert cmp.pass_threshold # default threshold is 0; this should pass
# with ignore_abundance = True, all of these should not be usable. Do we want errors, or ""/None?
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
assert not cmp.mh1_cmp.track_abundance
assert not cmp.mh2_cmp.track_abundance
assert cmp.weighted_intersection(from_mh=cmp.mh1_cmp).hashes == intersect_mh.hashes
assert cmp.weighted_intersection(from_mh=cmp.mh2_cmp).hashes == intersect_mh.hashes
def test_FracMinHashComparison_fail_threshold(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=1, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp_scaled = 2
ds_a = a.flatten().downsample(scaled=cmp_scaled)
ds_b = b.flatten().downsample(scaled=cmp_scaled)
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b, cmp_scaled = cmp_scaled, threshold_bp=40)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == False
assert cmp.cmp_scaled == cmp_scaled
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.mh1_containment == a.contained_by(b)
assert cmp.mh2_containment == b.contained_by(a)
assert cmp.avg_containment == a.avg_containment(b)
assert cmp.max_containment == a.max_containment(b)
assert cmp.jaccard == a.jaccard(b) == b.jaccard(a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
assert cmp.total_unique_intersect_hashes == 8
assert not cmp.pass_threshold # threshold is 40; this should fail
def test_FracMinHashComparison_potential_false_negative():
f1 = utils.get_test_data('scaled100/GCF_000005845.2_ASM584v2_genomic.fna.gz.sig.gz')
f2 = utils.get_test_data('scaled100/GCF_000006945.1_ASM694v1_genomic.fna.gz.sig.gz')
f3 = utils.get_test_data('scaled100/GCF_000783305.1_ASM78330v1_genomic.fna.gz.sig.gz')
a = load_one_signature(f1, ksize=21).minhash
b = load_one_signature(f2).minhash
c = load_one_signature(f3).minhash
assert a.size_is_accurate() == True
assert b.size_is_accurate() == True
assert c.size_is_accurate() == True
# build FracMinHashComparison
cmp = FracMinHashComparison(a, b)
# check ani, potential false negative
cmp.estimate_jaccard_ani()
assert cmp.jaccard_ani == a.jaccard_ani(b).ani == b.jaccard_ani(a).ani
print(cmp.jaccard_ani)
assert cmp.potential_false_negative == a.jaccard_ani(b).p_exceeds_threshold == b.jaccard_ani(a).p_exceeds_threshold
assert cmp.potential_false_negative == False
assert cmp.jaccard_ani_untrustworthy == a.jaccard_ani(b).je_exceeds_threshold == b.jaccard_ani(a).je_exceeds_threshold
cmp.estimate_mh1_containment_ani()
a_cont_ani_manual = a.containment_ani(b)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
print(a_cont_ani_manual.p_exceeds_threshold)
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
assert cmp.potential_false_negative == False
cmp.estimate_mh2_containment_ani()
b_cont_ani_manual = b.containment_ani(a)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
assert cmp.potential_false_negative == False
cmp.estimate_max_containment_ani()
mc_ani_manual = a.max_containment_ani(b)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
assert cmp.avg_containment_ani == np.mean([a.containment_ani(b).ani, b.containment_ani(a).ani])
assert cmp.potential_false_negative == False
#downsample to where it becomes a potential false negative
cmp = FracMinHashComparison(a, b, cmp_scaled=16000)
cmp.estimate_mh1_containment_ani()
assert cmp.potential_false_negative == True
def test_FracMinHashComparison_incompatible_ksize(track_abundance):
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 21, scaled=2, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(TypeError) as exc:
FracMinHashComparison(a, b)
print(str(exc))
assert "Error: Cannot compare incompatible sketches." in str(exc)
def test_FracMinHashComparison_incompatible_moltype(track_abundance):
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 31, scaled=2, is_protein=True, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(TypeError) as exc:
FracMinHashComparison(a, b)
print(str(exc))
assert "Error: Cannot compare incompatible sketches." in str(exc)
def test_FracMinHashComparison_incompatible_sketchtype(track_abundance):
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(10, 31, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(TypeError) as exc:
FracMinHashComparison(a, b)
print(str(exc))
assert "Error: Both sketches must be 'num' or 'scaled'." in str(exc)
def test_FracMinHashComparison_incompatible_cmp_scaled(track_abundance):
# pass in too low of a cmp_scaled value
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 31, scaled=10, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(ValueError) as exc:
FracMinHashComparison(a, b, cmp_scaled = 1)
print(str(exc))
assert "new scaled 1 is lower than current sample scaled 10" in str(exc)
def test_FracMinHashComparison_redownsample_without_scaled(track_abundance):
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(0, 31, scaled=10, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp = FracMinHashComparison(a, b)
assert cmp.cmp_scaled == 10
with pytest.raises(ValueError) as exc:
# try to redownsample without passing in cmp_scaled
cmp.downsample_and_handle_ignore_abundance()
print(str(exc))
assert "Error: must pass in a comparison scaled or num value." in str(exc)
def test_NumMinHashComparison(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(10, 21, scaled=0, track_abundance=track_abundance)
b = MinHash(10, 21, scaled=0, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
assert a.num and b.num and not a.scaled and not b.scaled
# build NumMinHashComparison
cmp = NumMinHashComparison(a, b)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == False
assert cmp.cmp_num == 10
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.jaccard == a.jaccard(b) == b.jaccard(a)
intersect_mh = a.flatten().intersection(b.flatten())
assert cmp.intersect_mh == intersect_mh == b.flatten().intersection(a.flatten())
if track_abundance:
assert cmp.angular_similarity == a.angular_similarity(b) == b.angular_similarity(a)
assert cmp.cosine_similarity == a.angular_similarity(b) == b.angular_similarity(a)
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
def test_NumMinHashComparison_downsample(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(10, 21, scaled=0, track_abundance=track_abundance)
b = MinHash(10, 21, scaled=0, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
assert a.num and b.num and not a.scaled and not b.scaled
cmp_num = 5
ds_a = a.downsample(num=cmp_num)
ds_b = b.downsample(num=cmp_num)
# build NumMinHashComparison
cmp = NumMinHashComparison(a, b, cmp_num = cmp_num)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == False
assert cmp.cmp_num == cmp_num
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.jaccard == ds_a.jaccard(ds_b) == ds_b.jaccard(ds_a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
if track_abundance:
assert cmp.angular_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.cosine_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
def test_NumMinHashComparison_autodownsample(track_abundance):
# build FracMinHash Comparison and check values
a = MinHash(10, 21, scaled=0, track_abundance=track_abundance)
b = MinHash(5, 21, scaled=0, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
assert a.num and b.num and not a.scaled and not b.scaled
cmp_num = 5
ds_a = a.downsample(num=cmp_num)
ds_b = b.downsample(num=cmp_num)
# build NumMinHashComparison
cmp = NumMinHashComparison(a, b)
assert cmp.mh1 == a
assert cmp.mh2 == b
assert cmp.ignore_abundance == False
assert cmp.cmp_num == cmp_num
assert cmp.ksize == 21
assert cmp.moltype == "DNA"
assert cmp.jaccard == ds_a.jaccard(ds_b) == ds_b.jaccard(ds_a)
intersect_mh = ds_a.flatten().intersection(ds_b.flatten())
assert cmp.intersect_mh == intersect_mh == ds_b.flatten().intersection(ds_a.flatten())
if track_abundance:
assert cmp.angular_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
assert cmp.cosine_similarity == ds_a.angular_similarity(ds_b) == ds_b.angular_similarity(ds_a)
else:
with pytest.raises(TypeError) as exc:
cmp.angular_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
with pytest.raises(TypeError) as exc:
cmp.cosine_similarity
print(str(exc))
assert "Error: Angular (cosine) similarity requires both sketches to track hash abundance." in str(exc)
def test_NumMinHashComparison_incompatible_ksize(track_abundance):
a_num = MinHash(20, 31, track_abundance=track_abundance)
b_num = MinHash(10, 21, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a_num.set_abundances(a_values)
b_num.set_abundances(b_values)
else:
a_num.add_many(a_values.keys())
b_num.add_many(b_values.keys())
# build NumMinHashComparison
with pytest.raises(TypeError) as exc:
NumMinHashComparison(a_num, b_num)
print(str(exc))
assert "Error: Cannot compare incompatible sketches." in str(exc)
def test_NumMinHashComparison_incompatible_moltype(track_abundance):
a_num = MinHash(20, 31, track_abundance=track_abundance)
b_num = MinHash(10, 31, is_protein=True, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a_num.set_abundances(a_values)
b_num.set_abundances(b_values)
else:
a_num.add_many(a_values.keys())
b_num.add_many(b_values.keys())
with pytest.raises(TypeError) as exc:
NumMinHashComparison(a_num, b_num)
print(str(exc))
assert "Error: Cannot compare incompatible sketches." in str(exc)
def test_NumMinHashComparison_incompatible_sketchtype(track_abundance):
a = MinHash(0, 31, scaled=1, track_abundance=track_abundance)
b = MinHash(10, 31, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(TypeError) as exc:
NumMinHashComparison(a, b)
print(str(exc))
assert "Error: Both sketches must be 'num' or 'scaled'." in str(exc)
def test_NumMinHashComparison_redownsample_without_num(track_abundance):
a = MinHash(10, 31, track_abundance=track_abundance)
b = MinHash(5, 31, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
cmp = NumMinHashComparison(a, b)
with pytest.raises(ValueError) as exc:
# try to redownsample without passing in cmp_num
cmp.downsample_and_handle_ignore_abundance()
print(str(exc))
assert "Error: must pass in a comparison scaled or num value." in str(exc)
def test_NumMinHashComparison_incompatible_cmp_num(track_abundance):
# pass in too high of a cmp_num value
a = MinHash(200, 31, track_abundance=track_abundance)
b = MinHash(100, 31, track_abundance=track_abundance)
a_values = { 1:5, 3:3, 5:2, 8:2}
b_values = { 1:3, 3:2, 5:1, 6:1, 8:1, 10:1 }
if track_abundance:
a.set_abundances(a_values)
b.set_abundances(b_values)
else:
a.add_many(a_values.keys())
b.add_many(b_values.keys())
with pytest.raises(ValueError) as exc:
NumMinHashComparison(a, b, cmp_num = 150)
print(str(exc))
assert "new sample num is higher than current sample num" in str(exc)
def test_FracMinHashComparison_ANI(track_abundance):
# need real mh here, small test data fails
if track_abundance:
f1 = utils.get_test_data('track_abund/47.fa.sig')
f2 = utils.get_test_data('track_abund/63.fa.sig')
else:
f1 = utils.get_test_data('47.fa.sig')
f2 = utils.get_test_data('63.fa.sig')
a = load_one_signature(f1, ksize=31).minhash
b = load_one_signature(f2, ksize=31).minhash
cmp = FracMinHashComparison(a, b)
# check jaccard ani
cmp.estimate_jaccard_ani()
assert cmp.jaccard_ani == a.jaccard_ani(b).ani == b.jaccard_ani(a).ani
assert cmp.potential_false_negative == a.jaccard_ani(b).p_exceeds_threshold == b.jaccard_ani(a).p_exceeds_threshold
assert cmp.jaccard_ani_untrustworthy == a.jaccard_ani(b).je_exceeds_threshold == b.jaccard_ani(a).je_exceeds_threshold
cmp.estimate_mh1_containment_ani()
a_cont_ani_manual = a.containment_ani(b)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
# assert cmp.mh1_containment_ani_low is None
# assert cmp.mh1_containment_ani_high is None
cmp.estimate_mh2_containment_ani()
b_cont_ani_manual = b.containment_ani(a)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
# assert cmp.mh2_containment_ani_low is None
# assert cmp.mh2_containment_ani_high is None
cmp.estimate_max_containment_ani()
mc_ani_manual = a.max_containment_ani(b)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
# assert cmp.max_containment_ani_low is None
# assert cmp.max_containment_ani_high is None
assert cmp.avg_containment_ani == np.mean([a.containment_ani(b).ani, b.containment_ani(a).ani])
def test_FracMinHashComparison_ANI_provide_similarity(track_abundance):
# need real mh here, small test data fails
if track_abundance:
f1 = utils.get_test_data('track_abund/47.fa.sig')
f2 = utils.get_test_data('track_abund/63.fa.sig')
else:
f1 = utils.get_test_data('47.fa.sig')
f2 = utils.get_test_data('63.fa.sig')
a = load_one_signature(f1, ksize=31).minhash
b = load_one_signature(f2, ksize=31).minhash
cmp = FracMinHashComparison(a, b)
# check jaccard ani
jaccard = a.jaccard(b)
cmp.estimate_jaccard_ani(jaccard=jaccard)
assert cmp.jaccard_ani == a.jaccard_ani(b).ani == b.jaccard_ani(a).ani
assert cmp.potential_false_negative == a.jaccard_ani(b).p_exceeds_threshold == b.jaccard_ani(a).p_exceeds_threshold
assert cmp.jaccard_ani_untrustworthy == a.jaccard_ani(b).je_exceeds_threshold == b.jaccard_ani(a).je_exceeds_threshold
a_cont = a.contained_by(b)
b_cont = b.contained_by(a)
mc = a.max_containment(b)
cmp.estimate_mh1_containment_ani(containment=a_cont)
a_cont_ani_manual = a.containment_ani(b)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
cmp.estimate_mh2_containment_ani(containment=b_cont)
b_cont_ani_manual = b.containment_ani(a)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
cmp.estimate_max_containment_ani(max_containment=mc)
mc_ani_manual = a.max_containment_ani(b)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
assert cmp.avg_containment_ani == np.mean([a.containment_ani(b).ani, b.containment_ani(a).ani])
def test_FracMinHashComparison_ANI_estimate_CI(track_abundance):
# need real mh here, small test data fails
if track_abundance:
f1 = utils.get_test_data('track_abund/47.fa.sig')
f2 = utils.get_test_data('track_abund/63.fa.sig')
else:
f1 = utils.get_test_data('47.fa.sig')
f2 = utils.get_test_data('63.fa.sig')
a = load_one_signature(f1, ksize=31).minhash
b = load_one_signature(f2, ksize=31).minhash
cmp = FracMinHashComparison(a, b, estimate_ani_ci=True)
cmp.estimate_jaccard_ani()
assert cmp.jaccard_ani == a.jaccard_ani(b).ani == b.jaccard_ani(a).ani
assert cmp.potential_false_negative == a.jaccard_ani(b).p_exceeds_threshold == b.jaccard_ani(a).p_exceeds_threshold
assert cmp.jaccard_ani_untrustworthy == a.jaccard_ani(b).je_exceeds_threshold == b.jaccard_ani(a).je_exceeds_threshold
cmp.estimate_mh1_containment_ani()
a_cont_ani_manual = a.containment_ani(b, estimate_ci=True)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
assert cmp.mh1_containment_ani_low == a_cont_ani_manual.ani_low
assert cmp.mh1_containment_ani_high == a_cont_ani_manual.ani_high
cmp.estimate_mh2_containment_ani()
b_cont_ani_manual = b.containment_ani(a, estimate_ci=True)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
assert cmp.mh2_containment_ani_low == b_cont_ani_manual.ani_low
assert cmp.mh2_containment_ani_high == b_cont_ani_manual.ani_high
cmp.estimate_max_containment_ani()
mc_ani_manual = a.max_containment_ani(b, estimate_ci=True)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
assert cmp.max_containment_ani_low == mc_ani_manual.ani_low
assert cmp.max_containment_ani_high ==mc_ani_manual.ani_high
def test_FracMinHashComparison_ANI_estimate_CI_ci99(track_abundance):
# need real mh here, small test data fails
if track_abundance:
f1 = utils.get_test_data('track_abund/47.fa.sig')
f2 = utils.get_test_data('track_abund/63.fa.sig')
else:
f1 = utils.get_test_data('47.fa.sig')
f2 = utils.get_test_data('63.fa.sig')
a = load_one_signature(f1, ksize=31).minhash
b = load_one_signature(f2, ksize=31).minhash
cmp = FracMinHashComparison(a, b, estimate_ani_ci=True, ani_confidence=0.99)
# check containment ani
cmp.estimate_mh1_containment_ani()
a_cont_ani_manual = a.containment_ani(b, estimate_ci=True, confidence=0.99)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
assert cmp.mh1_containment_ani_low == a_cont_ani_manual.ani_low
assert cmp.mh1_containment_ani_high == a_cont_ani_manual.ani_high
cmp.estimate_mh2_containment_ani()
b_cont_ani_manual = b.containment_ani(a, estimate_ci=True, confidence=0.99)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
assert cmp.mh2_containment_ani_low == b_cont_ani_manual.ani_low
assert cmp.mh2_containment_ani_high == b_cont_ani_manual.ani_high
cmp.estimate_max_containment_ani()
mc_ani_manual = a.max_containment_ani(b, estimate_ci=True, confidence=0.99)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
assert cmp.max_containment_ani_low == mc_ani_manual.ani_low
assert cmp.max_containment_ani_high ==mc_ani_manual.ani_high
def test_FracMinHashComparison_ANI_downsample(track_abundance):
# need real mh here, small test data fails
if track_abundance:
f1 = utils.get_test_data('track_abund/47.fa.sig')
f2 = utils.get_test_data('track_abund/63.fa.sig')
else:
f1 = utils.get_test_data('47.fa.sig')
f2 = utils.get_test_data('63.fa.sig')
a = load_one_signature(f1, ksize=31).minhash
b = load_one_signature(f2, ksize=31).minhash
cmp = FracMinHashComparison(a, b, cmp_scaled=1100, estimate_ani_ci=True)
# now manually downsample
a = a.downsample(scaled=1100)
b = b.downsample(scaled=1100)
# check jaccard ani
cmp.estimate_jaccard_ani()
assert cmp.jaccard_ani == a.jaccard_ani(b).ani == b.jaccard_ani(a).ani
assert cmp.potential_false_negative == a.jaccard_ani(b).p_exceeds_threshold == b.jaccard_ani(a).p_exceeds_threshold
assert cmp.jaccard_ani_untrustworthy == a.jaccard_ani(b).je_exceeds_threshold == b.jaccard_ani(a).je_exceeds_threshold
cmp.estimate_mh1_containment_ani()
a_cont_ani_manual = a.containment_ani(b, estimate_ci=True)
assert cmp.mh1_containment_ani == a_cont_ani_manual.ani
assert cmp.potential_false_negative == a_cont_ani_manual.p_exceeds_threshold
assert cmp.mh1_containment_ani_low == a_cont_ani_manual.ani_low
assert cmp.mh1_containment_ani_high == a_cont_ani_manual.ani_high
cmp.estimate_mh2_containment_ani()
b_cont_ani_manual = b.containment_ani(a, estimate_ci=True)
assert cmp.mh2_containment_ani == b_cont_ani_manual.ani
assert cmp.potential_false_negative == b_cont_ani_manual.p_exceeds_threshold
assert cmp.mh2_containment_ani_low == b_cont_ani_manual.ani_low
assert cmp.mh2_containment_ani_high == b_cont_ani_manual.ani_high
cmp.estimate_max_containment_ani()
mc_ani_manual = a.max_containment_ani(b, estimate_ci=True)
assert cmp.max_containment_ani == max(a.containment_ani(b).ani, b.containment_ani(a).ani) == mc_ani_manual.ani
assert cmp.potential_false_negative == mc_ani_manual.p_exceeds_threshold
assert cmp.max_containment_ani_low == mc_ani_manual.ani_low
assert cmp.max_containment_ani_high ==mc_ani_manual.ani_high