/
test_tax.py
1566 lines (1190 loc) · 72.1 KB
/
test_tax.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 'sourmash tax' command line and high level API.
"""
import os
import csv
import pytest
import sourmash_tst_utils as utils
from sourmash.tax import tax_utils
from sourmash_tst_utils import SourmashCommandFailed
## command line tests
def test_run_sourmash_tax():
status, out, err = utils.runscript('sourmash', ['tax'], fail_ok=True)
assert status != 0 # no args provided, ok ;)
def test_metagenome_stdout_0(runtmp):
# test basic metagenome
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
assert 'test1,superkingdom,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,phylum,0.116,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,phylum,0.088,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,phylum,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,class,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,class,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,class,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,order,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,order,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,order,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,family,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,family,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,family,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,genus,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella,md5,test1.sig,0.057,444000' in c.last_result.out
assert 'test1,genus,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,genus,0.028,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola,md5,test1.sig,0.016,138000' in c.last_result.out
assert 'test1,genus,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000' in c.last_result.out
assert 'test1,species,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia;s__Escherichia coli,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,species,0.028,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola;s__Phocaeicola vulgatus,md5,test1.sig,0.016,138000' in c.last_result.out
assert 'test1,species,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
def test_metagenome_stdout_0_db(runtmp):
# test basic metagenome with sqlite database
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.db')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
assert 'test1,superkingdom,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,phylum,0.116,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,phylum,0.088,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,phylum,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,class,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,class,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,class,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,order,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,order,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,order,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,family,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,family,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,family,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,genus,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella,md5,test1.sig,0.057,444000' in c.last_result.out
assert 'test1,genus,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,genus,0.028,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola,md5,test1.sig,0.016,138000' in c.last_result.out
assert 'test1,genus,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000' in c.last_result.out
assert 'test1,species,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia;s__Escherichia coli,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,species,0.028,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola;s__Phocaeicola vulgatus,md5,test1.sig,0.016,138000' in c.last_result.out
assert 'test1,species,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
def test_metagenome_summary_csv_out(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
sum_csv = csv_base + ".summarized.csv"
csvout = runtmp.output(sum_csv)
outdir = os.path.dirname(csvout)
runtmp.run_sourmash('tax', 'metagenome', '--gather-csv', g_csv, '--taxonomy-csv', tax, '-o', csv_base, '--output-dir', outdir)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status == 0
assert os.path.exists(csvout)
sum_gather_results = [x.rstrip() for x in open(csvout)]
assert f"saving `csv_summary` output to {csvout}" in runtmp.last_result.err
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in sum_gather_results[0]
assert 'test1,superkingdom,0.2042281611487834,d__Bacteria,md5,test1.sig,0.13080306238801107,1024000' in sum_gather_results[1]
assert 'test1,superkingdom,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[2]
assert 'test1,phylum,0.11607499002792182,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.07265026877341586,582000' in sum_gather_results[3]
assert 'test1,phylum,0.08815317112086159,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[4]
assert 'test1,phylum,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[5]
assert 'test1,class,0.11607499002792182,d__Bacteria;p__Bacteroidota;c__Bacteroidia,md5,test1.sig,0.07265026877341586,582000' in sum_gather_results[6]
assert 'test1,class,0.08815317112086159,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[7]
assert 'test1,class,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[8]
assert 'test1,order,0.11607499002792182,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales,md5,test1.sig,0.07265026877341586,582000' in sum_gather_results[9]
assert 'test1,order,0.08815317112086159,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[10]
assert 'test1,order,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[11]
assert 'test1,family,0.11607499002792182,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae,md5,test1.sig,0.07265026877341586,582000' in sum_gather_results[12]
assert 'test1,family,0.08815317112086159,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[13]
assert 'test1,family,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[14]
assert 'test1,genus,0.0885520542481053,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella,md5,test1.sig,0.05701254275940707,444000' in sum_gather_results[15]
assert 'test1,genus,0.08815317112086159,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[16]
assert 'test1,genus,0.027522935779816515,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola,md5,test1.sig,0.015637726014008795,138000' in sum_gather_results[17]
assert 'test1,genus,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[18]
assert 'test1,species,0.0885520542481053,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.05701254275940707,444000' in sum_gather_results[19]
assert 'test1,species,0.08815317112086159,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia;s__Escherichia coli,md5,test1.sig,0.05815279361459521,442000' in sum_gather_results[20]
assert 'test1,species,0.027522935779816515,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola;s__Phocaeicola vulgatus,md5,test1.sig,0.015637726014008795,138000' in sum_gather_results[21]
assert 'test1,species,0.7957718388512166,unclassified,md5,test1.sig,0.8691969376119889,3990000' in sum_gather_results[22]
def test_metagenome_krona_tsv_out(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
kr_csv = csv_base + ".krona.tsv"
csvout = runtmp.output(kr_csv)
outdir = os.path.dirname(csvout)
print("csvout: ", csvout)
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax, '-o', csv_base,
'--output-format', 'krona', '--rank', 'genus', '--output-dir', outdir)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status == 0
assert os.path.exists(csvout)
assert f"saving `krona` output to {csvout}" in runtmp.last_result.err
gn_krona_results = [x.rstrip().split('\t') for x in open(csvout)]
print("species krona results: \n", gn_krona_results)
assert ['fraction', 'superkingdom', 'phylum', 'class', 'order', 'family', 'genus'] == gn_krona_results[0]
assert ['0.0885520542481053', 'd__Bacteria', 'p__Bacteroidota', 'c__Bacteroidia', 'o__Bacteroidales', 'f__Bacteroidaceae', 'g__Prevotella'] == gn_krona_results[1]
assert ['0.08815317112086159', 'd__Bacteria', 'p__Proteobacteria', 'c__Gammaproteobacteria', 'o__Enterobacterales', 'f__Enterobacteriaceae', 'g__Escherichia'] == gn_krona_results[2]
assert ['0.027522935779816515', 'd__Bacteria', 'p__Bacteroidota', 'c__Bacteroidia', 'o__Bacteroidales', 'f__Bacteroidaceae', 'g__Phocaeicola'] == gn_krona_results[3]
assert ['0.7957718388512166', 'unclassified', 'unclassified', 'unclassified', 'unclassified', 'unclassified', 'unclassified'] == gn_krona_results[4]
def test_metagenome_lineage_summary_out(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
lin_csv = csv_base + ".lineage_summary.tsv"
csvout = runtmp.output(lin_csv)
outdir = os.path.dirname(csvout)
print("csvout: ", csvout)
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax,
'-o', csv_base, '--output-format', 'lineage_summary', '--rank',
'genus', '--output-dir', outdir)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status == 0
assert os.path.exists(csvout)
assert f"saving `lineage_summary` output to {csvout}" in runtmp.last_result.err
gn_lineage_summary = [x.rstrip().split('\t') for x in open(csvout)]
print("species lineage summary results: \n", gn_lineage_summary)
assert ['lineage', 'test1'] == gn_lineage_summary[0]
assert ['d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Phocaeicola', '0.027522935779816515'] == gn_lineage_summary[1]
assert ['d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella', '0.0885520542481053'] == gn_lineage_summary[2]
assert ['d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria;o__Enterobacterales;f__Enterobacteriaceae;g__Escherichia', '0.08815317112086159'] == gn_lineage_summary[3]
assert ['unclassified', '0.7957718388512166'] == gn_lineage_summary[4]
def test_metagenome_no_taxonomy_fail(runtmp):
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'metagenome', '-g', g_csv)
assert "error: the following arguments are required: -t/--taxonomy-csv" in str(exc.value)
def test_metagenome_no_rank_lineage_summary(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax, '-o', csv_base, '--output-format', 'lineage_summary')
assert "Rank (--rank) is required for krona and lineage_summary output formats." in str(exc.value)
def test_metagenome_no_rank_krona(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax, '-o', csv_base, '--output-format', 'krona')
assert "Rank (--rank) is required for krona and lineage_summary output formats." in str(exc.value)
def test_genome_no_rank_krona(runtmp):
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'genome', '-g', g_csv, '--taxonomy-csv', tax, '-o', csv_base, '--output-format', 'krona')
assert "Rank (--rank) is required for krona output format." in str(exc.value)
def test_metagenome_rank_not_available(runtmp):
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax,
'--rank', 'strain')
print(str(exc.value))
assert c.last_result.status == -1
assert "No taxonomic information provided for rank strain: cannot summarize at this rank" in str(exc.value)
def test_metagenome_duplicated_taxonomy_fail(runtmp):
c = runtmp
# write temp taxonomy with duplicates
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
duplicated_csv = runtmp.output("duplicated_taxonomy.csv")
with open(duplicated_csv, 'w') as dup:
tax = [x.rstrip() for x in open(taxonomy_csv, 'r')]
tax.append(tax[1] + 'FOO') # add first tax_assign again
dup.write("\n".join(tax))
g_csv = utils.get_test_data('tax/test1.gather.csv')
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', duplicated_csv)
assert "cannot read taxonomy" in str(exc.value)
assert "multiple lineages for identifier GCF_001881345" in str(exc.value)
def test_metagenome_duplicated_taxonomy_force(runtmp):
c = runtmp
# write temp taxonomy with duplicates
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
duplicated_csv = runtmp.output("duplicated_taxonomy.csv")
with open(duplicated_csv, 'w') as dup:
tax = [x.rstrip() for x in open(taxonomy_csv, 'r')]
tax.append(tax[1]) # add first tax_assign again
dup.write("\n".join(tax))
g_csv = utils.get_test_data('tax/test1.gather.csv')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', duplicated_csv, '--force')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
# same as stdout test - just check the first few lines
assert c.last_result.status == 0
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
assert 'test1,superkingdom,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,phylum,0.116,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,phylum,0.088,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,phylum,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
def test_metagenome_missing_taxonomy(runtmp):
c = runtmp
# write temp taxonomy with missing entry
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
subset_csv = runtmp.output("subset_taxonomy.csv")
with open(subset_csv, 'w') as subset:
tax = [x.rstrip() for x in open(taxonomy_csv, 'r')]
subset.write("\n".join(tax[:4]))
g_csv = utils.get_test_data('tax/test1.gather.csv')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', subset_csv)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert "The following are missing from the taxonomy information: GCF_003471795" in c.last_result.err
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.193,d__Bacteria,md5,test1.sig,0.124,970000'in c.last_result.out
assert 'test1,superkingdom,0.807,unclassified,md5,test1.sig,0.876,4044000' in c.last_result.out
assert 'test1,phylum,0.105,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.066,528000' in c.last_result.out
assert 'test1,phylum,0.088,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,phylum,0.807,unclassified,md5,test1.sig,0.876,4044000' in c.last_result.out
assert 'test1,class,0.105,d__Bacteria;p__Bacteroidota;c__Bacteroidia,md5,test1.sig,0.066,528000' in c.last_result.out
def test_metagenome_missing_fail_taxonomy(runtmp):
c = runtmp
# write temp taxonomy with missing entry
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
subset_csv = runtmp.output("subset_taxonomy.csv")
with open(subset_csv, 'w') as subset:
tax = [x.rstrip() for x in open(taxonomy_csv, 'r')]
subset.write("\n".join(tax[:4]))
g_csv = utils.get_test_data('tax/test1.gather.csv')
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', subset_csv, '--fail-on-missing-taxonomy')
print(str(exc.value))
assert "The following are missing from the taxonomy information: GCF_003471795" in str(exc.value)
assert "Failing on missing taxonomy, as requested via --fail-on-missing-taxonomy." in str(exc.value)
assert c.last_result.status == -1
def test_metagenome_multiple_taxonomy_files_missing(runtmp):
c = runtmp
# write temp taxonomy with duplicates
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
# gather against mult databases
g_csv = utils.get_test_data('tax/test1_x_gtdbrs202_genbank_euks.gather.csv')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', taxonomy_csv, '--force')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert "of 6, missed 2 lineage assignments." in c.last_result.err
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'multtest,superkingdom,0.204,d__Bacteria,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.131,1024000' in c.last_result.out
assert 'multtest,superkingdom,0.796,unclassified,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.869,3990000' in c.last_result.out
assert 'multtest,phylum,0.116,d__Bacteria;p__Bacteroidota,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.073,582000' in c.last_result.out
assert 'multtest,phylum,0.088,d__Bacteria;p__Proteobacteria,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.058,442000' in c.last_result.out
assert 'multtest,phylum,0.796,unclassified,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.869,3990000' in c.last_result.out
assert 'multtest,class,0.116,d__Bacteria;p__Bacteroidota;c__Bacteroidia,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.073,582000' in c.last_result.out
assert 'multtest,class,0.088,d__Bacteria;p__Proteobacteria;c__Gammaproteobacteria,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.058,442000' in c.last_result.out
assert 'multtest,class,0.796,unclassified,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.869,3990000' in c.last_result.out
def test_metagenome_multiple_taxonomy_files(runtmp):
c = runtmp
# write temp taxonomy with duplicates
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
protozoa_genbank = utils.get_test_data('tax/protozoa_genbank_lineage.csv')
bacteria_refseq = utils.get_test_data('tax/bacteria_refseq_lineage.csv')
# gather against mult databases
g_csv = utils.get_test_data('tax/test1_x_gtdbrs202_genbank_euks.gather.csv')
c.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', taxonomy_csv, protozoa_genbank, bacteria_refseq)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert "of 6, missed 0 lineage assignments." in c.last_result.err
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'multtest,superkingdom,0.204,Bacteria,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.131,1024000' in c.last_result.out
assert 'multtest,superkingdom,0.051,Eukaryota,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.245,258000' in c.last_result.out
assert 'multtest,superkingdom,0.744,unclassified,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.624,3732000' in c.last_result.out
assert 'multtest,phylum,0.116,Bacteria;Bacteroidetes,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.073,582000' in c.last_result.out
assert 'multtest,phylum,0.088,Bacteria;Proteobacteria,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.058,442000' in c.last_result.out
assert 'multtest,phylum,0.051,Eukaryota;Apicomplexa,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.245,258000' in c.last_result.out
assert 'multtest,phylum,0.744,unclassified,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.624,3732000' in c.last_result.out
assert 'multtest,class,0.116,Bacteria;Bacteroidetes;Bacteroidia,9687eeed,outputs/abundtrim/HSMA33MX.abundtrim.fq.gz,0.073,582000' in c.last_result.out
def test_metagenome_empty_gather_results(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
#creates empty gather result
g_csv = runtmp.output('g.csv')
with open(g_csv, "w") as fp:
fp.write("")
print("g_csv: ", g_csv)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax)
assert f'Cannot read gather results from {g_csv}. Is file empty?' in str(exc.value)
assert runtmp.last_result.status == -1
def test_metagenome_bad_gather_header(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
bad_g_csv = runtmp.output('g.csv')
#creates bad gather result
bad_g = [x.replace("name", "nope") for x in open(g_csv, 'r')]
with open(bad_g_csv, 'w') as fp:
for line in bad_g:
fp.write(line)
print("bad_gather_results: \n", bad_g)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'metagenome', '-g', bad_g_csv, '--taxonomy-csv', tax)
assert f'Not all required gather columns are present in {bad_g_csv}.' in str(exc.value)
assert runtmp.last_result.status == -1
def test_metagenome_empty_tax_lineage_input(runtmp):
tax_empty = runtmp.output('t.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
with open(tax_empty, "w") as fp:
fp.write("")
print("t_csv: ", tax_empty)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'metagenome', '-g', g_csv, '--taxonomy-csv', tax_empty)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status != 0
assert "cannot read taxonomy assignments from" in str(exc.value)
def test_metagenome_perfect_match_warning(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
perfect_g_csv = runtmp.output('g.csv')
#create a perfect gather result
with open(g_csv, 'r') as fp:
r = csv.DictReader(fp, delimiter=',')
header = r.fieldnames
print(header)
with open(perfect_g_csv, 'w') as out_fp:
w = csv.DictWriter(out_fp, header)
w.writeheader()
for n, row in enumerate(r):
if n == 0:
# make a perfect match
row["f_unique_to_query"] = 1.0
else:
# set the rest to 0
row["f_unique_to_query"] = 0.0
w.writerow(row)
print(row)
runtmp.run_sourmash('tax', 'metagenome', '-g', perfect_g_csv, '--taxonomy-csv', tax)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status == 0
assert 'WARNING: 100% match! Is query "test1" identical to its database match, GCF_001881345' in runtmp.last_result.err
def test_metagenome_over100percent_error(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
perfect_g_csv = runtmp.output('g.csv')
#create a perfect gather result
with open(g_csv, 'r') as fp:
r = csv.DictReader(fp, delimiter=',')
header = r.fieldnames
print(header)
with open(perfect_g_csv, 'w') as out_fp:
w = csv.DictWriter(out_fp, header)
w.writeheader()
for n, row in enumerate(r):
if n == 0:
row["f_unique_to_query"] = 1.0
# let the rest stay as they are (should be > 100% match now)
w.writerow(row)
print(row)
with pytest.raises(SourmashCommandFailed):
runtmp.run_sourmash('tax', 'metagenome', '-g', perfect_g_csv, '--taxonomy-csv', tax)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status == -1
assert "ERROR: The tax summary of query 'test1' is 1.1160749900279219, which is > 100% of the query!!" in runtmp.last_result.err
def test_metagenome_gather_duplicate_query(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
# different filename, contents identical to test1
g_res2 = runtmp.output("test2.gather.csv")
with open(g_res2, 'w') as fp:
for line in open(g_res, 'r'):
fp.write(line)
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'metagenome', '--gather-csv', g_res, g_res2,
'--taxonomy-csv', taxonomy_csv)
assert c.last_result.status == -1
print(str(exc.value))
assert "Gather query test1 was found in more than one CSV. Cannot load from " in str(exc.value)
def test_metagenome_gather_duplicate_query_force(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
# different filename, contents identical to test1
g_res2 = runtmp.output("test2.gather.csv")
with open(g_res2, 'w') as fp:
for line in open(g_res, 'r'):
fp.write(line)
c.run_sourmash('tax', 'metagenome', '--gather-csv', g_res, g_res2,
'--taxonomy-csv', taxonomy_csv, '--force')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert '--force is set, ignoring duplicate query.' in c.last_result.err
assert 'No gather results loaded from ' in c.last_result.err
assert 'loaded results from 1 gather CSVs' in c.last_result.err
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
assert 'test1,superkingdom,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
assert 'test1,phylum,0.116,d__Bacteria;p__Bacteroidota,md5,test1.sig,0.073,582000' in c.last_result.out
assert 'test1,phylum,0.088,d__Bacteria;p__Proteobacteria,md5,test1.sig,0.058,442000' in c.last_result.out
assert 'test1,phylum,0.796,unclassified,md5,test1.sig,0.869,3990000' in c.last_result.out
def test_metagenome_gather_duplicate_filename(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
c.run_sourmash('tax', 'metagenome', '--gather-csv', g_res, g_res, '--taxonomy-csv', taxonomy_csv)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert f'ignoring duplicated reference to file: {g_res}'
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
def test_metagenome_gather_duplicate_filename_from_file(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
f_csv.write(f"{g_res}\n")
c.run_sourmash('tax', 'metagenome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert f'ignoring duplicated reference to file: {g_res}'
assert 'query_name,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,superkingdom,0.204,d__Bacteria,md5,test1.sig,0.131,1024000' in c.last_result.out
def test_genome_empty_gather_results(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
#creates empty gather result
g_csv = runtmp.output('g.csv')
with open(g_csv, "w") as fp:
fp.write("")
print("g_csv: ", g_csv)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'genome', '-g', g_csv, '--taxonomy-csv', tax)
assert f'Cannot read gather results from {g_csv}. Is file empty?' in str(exc.value)
assert runtmp.last_result.status == -1
def test_genome_bad_gather_header(runtmp):
tax = utils.get_test_data('tax/test.taxonomy.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
bad_g_csv = runtmp.output('g.csv')
#creates bad gather result
bad_g = [x.replace("f_unique_to_query", "nope") for x in open(g_csv, 'r')]
with open(bad_g_csv, 'w') as fp:
for line in bad_g:
fp.write(line)
print("bad_gather_results: \n", bad_g)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'genome', '-g', bad_g_csv, '--taxonomy-csv', tax)
assert f'Not all required gather columns are present in {bad_g_csv}.' in str(exc.value)
assert runtmp.last_result.status == -1
def test_genome_empty_tax_lineage_input(runtmp):
tax_empty = runtmp.output('t.csv')
g_csv = utils.get_test_data('tax/test1.gather.csv')
with open(tax_empty, "w") as fp:
fp.write("")
print("t_csv: ", tax_empty)
with pytest.raises(SourmashCommandFailed) as exc:
runtmp.run_sourmash('tax', 'genome', '-g', g_csv, '--taxonomy-csv', tax_empty)
print(runtmp.last_result.status)
print(runtmp.last_result.out)
print(runtmp.last_result.err)
assert runtmp.last_result.status != 0
assert "cannot read taxonomy assignments from" in str(exc.value)
def test_genome_rank_stdout_0(runtmp):
# test basic genome
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
c.run_sourmash('tax', 'genome', '--gather-csv', g_csv, '--taxonomy-csv', tax,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_rank_stdout_0_db(runtmp):
# test basic genome with sqlite database
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.db')
c.run_sourmash('tax', 'genome', '--gather-csv', g_csv, '--taxonomy-csv',
tax, '--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_rank_csv_0(runtmp):
# test basic genome - output csv
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
cl_csv = csv_base + ".classifications.csv"
csvout = runtmp.output(cl_csv)
outdir = os.path.dirname(csvout)
print("csvout: ", csvout)
c.run_sourmash('tax', 'genome', '-g', g_csv, '--taxonomy-csv', tax,
'--rank', 'species', '-o', csv_base, '--containment-threshold', '0',
'--output-dir', outdir)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert f"saving `classification` output to {csvout}" in runtmp.last_result.err
assert c.last_result.status == 0
cl_results = [x.rstrip() for x in open(csvout)]
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in cl_results[0]
assert 'test1,match,species,0.0885520542481053,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.05701254275940707,444000.0' in cl_results[1]
def test_genome_rank_krona(runtmp):
# test basic genome - output csv
c = runtmp
g_csv = utils.get_test_data('tax/test1.gather.csv')
tax = utils.get_test_data('tax/test.taxonomy.csv')
csv_base = "out"
cl_csv = csv_base + ".krona.tsv"
csvout = runtmp.output(cl_csv)
outdir = os.path.dirname(csvout)
print("csvout: ", csvout)
c.run_sourmash('tax', 'genome', '-g', g_csv, '--taxonomy-csv', tax,
'--rank', 'species', '-o', csv_base, '--containment-threshold', '0',
'--output-format', 'krona', '--output-dir', outdir)
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert f"saving `krona` output to {csvout}" in runtmp.last_result.err
assert c.last_result.status == 0
kr_results = [x.rstrip().split('\t') for x in open(csvout)]
print(kr_results)
assert ['fraction', 'superkingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species'] == kr_results[0]
assert ['0.0885520542481053', 'd__Bacteria', 'p__Bacteroidota', 'c__Bacteroidia', 'o__Bacteroidales', 'f__Bacteroidaceae', 'g__Prevotella', 's__Prevotella copri'] == kr_results[1]
def test_genome_gather_from_file_rank(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
c.run_sourmash('tax', 'genome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_two_files(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
# make test2 results (identical to test1 except query_name and filename)
g_res2 = runtmp.output("test2.gather.csv")
test2_results = [x.replace("test1", "test2") for x in open(g_res, 'r')]
with open(g_res2, 'w') as fp:
for line in test2_results:
fp.write(line)
c.run_sourmash('tax', 'genome', '-g', g_res, g_res2, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
assert 'test2,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test2.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_duplicate_filename(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
c.run_sourmash('tax', 'genome', '--gather-csv', g_res, g_res, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert f'ignoring duplicated reference to file: {g_res}'
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_from_file_duplicate_filename(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
f_csv.write(f"{g_res}\n")
c.run_sourmash('tax', 'genome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert f'ignoring duplicated reference to file: {g_res}'
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_from_file_duplicate_query(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
# different filename, contents identical to test1
g_res2 = runtmp.output("test2.gather.csv")
with open(g_res2, 'w') as fp:
for line in open(g_res, 'r'):
fp.write(line)
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
f_csv.write(f"{g_res2}\n")
with pytest.raises(SourmashCommandFailed) as exc:
c.run_sourmash('tax', 'genome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
assert c.last_result.status == -1
print(str(exc.value))
assert "Gather query test1 was found in more than one CSV. Cannot load from " in str(exc.value)
def test_genome_gather_from_file_duplicate_query_force(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
# different filename, contents identical to test1
g_res2 = runtmp.output("test2.gather.csv")
with open(g_res2, 'w') as fp:
for line in open(g_res, 'r'):
fp.write(line)
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
f_csv.write(f"{g_res2}\n")
c.run_sourmash('tax', 'genome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0', '--force')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
assert '--force is set, ignoring duplicate query.' in c.last_result.err
assert 'No gather results loaded from ' in c.last_result.err
assert 'loaded results from 1 gather CSVs' in c.last_result.err
def test_genome_gather_cli_and_from_file(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
# make test2 results (identical to test1 except query_name)
g_res2 = runtmp.output("test2.gather.csv")
test2_results = [x.replace("test1", "test2") for x in open(g_res, 'r')]
with open(g_res2, 'w') as fp:
for line in test2_results:
fp.write(line)
# write test2 csv to a text file for input
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res2}\n")
c.run_sourmash('tax', 'genome', '-g', g_res, '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
assert 'test2,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test2.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_cli_and_from_file_duplicate_filename(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
# also write test1 csv to a text file for input
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
c.run_sourmash('tax', 'genome', '-g', g_res, '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--rank', 'species', '--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert f'ignoring duplicated reference to file: {g_res}' in c.last_result.err
assert 'query_name,status,rank,fraction,lineage,query_md5,query_filename,f_weighted_at_rank,bp_match_at_rank' in c.last_result.out
assert 'test1,match,species,0.089,d__Bacteria;p__Bacteroidota;c__Bacteroidia;o__Bacteroidales;f__Bacteroidaceae;g__Prevotella;s__Prevotella copri,md5,test1.sig,0.057,444000.0' in c.last_result.out
def test_genome_gather_from_file_below_threshold(runtmp):
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/test1.gather.csv')
g_from_file = runtmp.output("tmp-from-file.txt")
with open(g_from_file, 'w') as f_csv:
f_csv.write(f"{g_res}\n")
c.run_sourmash('tax', 'genome', '--from-file', g_from_file, '--taxonomy-csv', taxonomy_csv,
'--containment-threshold', '1')
print(c.last_result.status)
print(c.last_result.out)
print(c.last_result.err)
assert c.last_result.status == 0
assert "query_name,status,rank,fraction,lineage" in c.last_result.out
assert "test1,below_threshold,,0.000," in c.last_result.out
def test_genome_gather_two_queries(runtmp):
'''
This checks for initial bug where classification
would only happen for one genome per rank when
doing --containment-threshold classification
'''
c = runtmp
taxonomy_csv = utils.get_test_data('tax/test.taxonomy.csv')
g_res = utils.get_test_data('tax/47+63_x_gtdb-rs202.gather.csv')
# split 47+63 into two fake queries: q47, q63
g_res2 = runtmp.output("two-queries.gather.csv")
q2_results = [x for x in open(g_res, 'r')]
# rename queries
q2_results[1] = q2_results[1].replace('47+63', 'q47')
q2_results[2] = q2_results[2].replace('47+63', 'q63')
with open(g_res2, 'w') as fp:
for line in q2_results:
print(line)
fp.write(line)
c.run_sourmash('tax', 'genome', '-g', g_res2, '--taxonomy-csv', taxonomy_csv,
'--containment-threshold', '0')
print(c.last_result.status)
print(c.last_result.out)