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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
|
# This file is part of khmer, https://github.com/dib-lab/khmer/, and is
# Copyright (C) 2010-2015, Michigan State University.
# Copyright (C) 2015-2016, The Regents of the University of California.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * Neither the name of the Michigan State University nor the names
# of its contributors may be used to endorse or promote products
# derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Contact: khmer-project@idyll.org
# pylint: disable=missing-docstring,protected-access,no-member,invalid-name
import khmer
from khmer import Nodegraph, Countgraph
from khmer import ReadParser
from khmer import reverse_complement as revcomp
from khmer.khmer_args import create_matching_nodegraph
import screed
import pytest
from . import khmer_tst_utils as utils
def teardown_module():
utils.cleanup()
@pytest.mark.huge
def test_toobig():
try:
khmer.Nodegraph(32, 1e13, 1)
assert 0, "This should fail"
except MemoryError as err:
print(str(err))
def test_add_tag():
nodegraph = khmer.Nodegraph(6, 1, 1)
assert nodegraph.n_tags == 0
nodegraph.add_tag('AATAAG')
assert nodegraph.n_tags == 1
print(nodegraph.get_tagset())
assert nodegraph.get_tagset() == ['AATAAG']
def test_add_tag():
nodegraph = khmer.Nodegraph(6, 1, 1)
assert nodegraph.n_tags == 0
nodegraph.add_tag('AATAAG')
assert nodegraph.n_tags == 1
print(nodegraph.get_tagset())
assert nodegraph.get_tagset() == ['AATAAG']
def test_get_tag_sequences():
nodegraph = khmer.Nodegraph(6, 1, 1)
assert nodegraph.n_tags == 0
kmer = nodegraph.hash('AATAAG')
nodegraph.add_tag(kmer)
assert nodegraph.n_tags == 1
tagset = nodegraph.get_tags_for_sequence('GGGAATAAGGGG')
tagset = list(tagset)
assert len(tagset) == 1
assert nodegraph.reverse_hash(tagset[0]) == 'AATAAG'
def test_add_stop_tag():
nodegraph = khmer.Nodegraph(6, 1, 1)
nodegraph.add_stop_tag('AATAAG')
print(nodegraph.get_stop_tags())
assert nodegraph.get_stop_tags() == ['AATAAG']
def test_add_stop_tag_hashval():
nodegraph = khmer.Nodegraph(6, 1, 1)
kmer = nodegraph.hash('AATAAG')
nodegraph.add_stop_tag(kmer)
print(nodegraph.get_stop_tags())
assert nodegraph.get_stop_tags() == ['AATAAG']
def test__get_set_tag_density():
nodegraph = khmer.Nodegraph(32, 1, 1,)
orig = nodegraph.tag_density
assert orig != 2
nodegraph.tag_density = 2
assert nodegraph.tag_density == 2
def test_update_from():
nodegraph = khmer.Nodegraph(5, 1000, 4)
other_nodegraph = khmer.Nodegraph(5, 1000, 4)
assert nodegraph.get('AAAAA') == 0
assert nodegraph.get('GCGCG') == 0
assert nodegraph.n_occupied() == 0
assert other_nodegraph.get('AAAAA') == 0
assert other_nodegraph.get('GCGCG') == 0
assert other_nodegraph.n_occupied() == 0
other_nodegraph.count('AAAAA')
assert nodegraph.get('AAAAA') == 0
assert nodegraph.get('GCGCG') == 0
assert nodegraph.n_occupied() == 0
assert other_nodegraph.get('AAAAA') == 1
assert other_nodegraph.get('GCGCG') == 0
assert other_nodegraph.n_occupied() == 1
nodegraph.count('GCGCG')
assert nodegraph.get('AAAAA') == 0
assert nodegraph.get('GCGCG') == 1
assert nodegraph.n_occupied() == 1
assert other_nodegraph.get('AAAAA') == 1
assert other_nodegraph.get('GCGCG') == 0
assert other_nodegraph.n_occupied() == 1
nodegraph.update(other_nodegraph)
assert nodegraph.get('AAAAA') == 1
assert nodegraph.get('GCGCG') == 1
assert nodegraph.n_occupied() == 2
assert other_nodegraph.get('AAAAA') == 1
assert other_nodegraph.get('GCGCG') == 0
assert other_nodegraph.n_occupied() == 1
def test_update_from_2():
ng1 = khmer.Nodegraph(20, 1000, 4)
ng2 = khmer.Nodegraph(20, 1000, 4)
filename = utils.get_test_data('random-20-a.fa')
ng1.consume_seqfile(filename)
ng2.consume_seqfile(filename)
assert ng1.n_occupied() == ng2.n_occupied()
ng1.update(ng2)
assert ng1.n_occupied() == ng2.n_occupied()
def test_update_from_diff_ksize_2():
nodegraph = khmer.Nodegraph(5, 1000, 4)
other_nodegraph = khmer.Nodegraph(4, 1000, 4)
try:
nodegraph.update(other_nodegraph)
assert 0, "should not be reached"
except ValueError as err:
print(str(err))
try:
other_nodegraph.update(nodegraph)
assert 0, "should not be reached"
except ValueError as err:
print(str(err))
def test_update_from_diff_tablesize():
nodegraph = khmer.Nodegraph(5, 100, 4)
other_nodegraph = khmer.Nodegraph(5, 1000, 4)
try:
nodegraph.update(other_nodegraph)
assert 0, "should not be reached"
except ValueError as err:
print(str(err))
def test_update_from_diff_num_tables():
nodegraph = khmer.Nodegraph(5, 1000, 3)
other_nodegraph = khmer.Nodegraph(5, 1000, 4)
try:
nodegraph.update(other_nodegraph)
assert 0, "should not be reached"
except ValueError as err:
print(str(err))
def test_n_occupied_1():
filename = utils.get_test_data('random-20-a.fa')
ksize = 20 # size of kmer
htable_size = 100000 # size of hashtable
num_nodegraphs = 1 # number of hashtables
# test modified c++ n_occupied code
nodegraph = khmer.Nodegraph(ksize, htable_size, num_nodegraphs)
for _, record in enumerate(screed.open(filename)):
nodegraph.consume(record.sequence)
# this number calculated independently
assert nodegraph.n_occupied() == 3884, nodegraph.n_occupied()
def test_bloom_python_1():
# test python code to count unique kmers using bloom filter
filename = utils.get_test_data('random-20-a.fa')
ksize = 20 # size of kmer
htable_size = 100000 # size of hashtable
num_nodegraphs = 3 # number of hashtables
nodegraph = khmer.Nodegraph(ksize, htable_size, num_nodegraphs)
n_unique = 0
for _, record in enumerate(screed.open(filename)):
sequence = record.sequence
seq_len = len(sequence)
for num in range(0, seq_len + 1 - ksize):
kmer = sequence[num:num + ksize]
if not nodegraph.get(kmer):
n_unique += 1
nodegraph.count(kmer)
assert n_unique == 3960
assert nodegraph.n_occupied() == 3884, nodegraph.n_occupied()
# this number equals n_unique
assert nodegraph.n_unique_kmers() == 3960, nodegraph.n_unique_kmers()
def test_bloom_c_1():
# test c++ code to count unique kmers using bloom filter
filename = utils.get_test_data('random-20-a.fa')
ksize = 20 # size of kmer
htable_size = 100000 # size of hashtable
num_nodegraphs = 3 # number of hashtables
nodegraph = khmer.Nodegraph(ksize, htable_size, num_nodegraphs)
for _, record in enumerate(screed.open(filename)):
nodegraph.consume(record.sequence)
assert nodegraph.n_occupied() == 3884
assert nodegraph.n_unique_kmers() == 3960
def test_n_occupied_2(): # simple one
ksize = 4
nodegraph = khmer.Nodegraph(ksize, 1, 1, primes=[11])
nodegraph.count('AAAA') # 00 00 00 00 = 0
assert nodegraph.n_occupied() == 1
nodegraph.count('ACTG') # 00 10 01 11 =
assert nodegraph.n_occupied() == 2
nodegraph.count('AACG') # 00 00 10 11 = 11 # collision 1
assert nodegraph.n_occupied() == 2
nodegraph.count('AGAC') # 00 11 00 10 # collision 2
assert nodegraph.n_occupied() == 2, nodegraph.n_occupied()
def test_n_occupied_2_add_is_count(): # 'add' synonym for 'count'
ksize = 4
nodegraph = khmer.Nodegraph(ksize, 1, 1, primes=[11])
nodegraph.add('AAAA') # 00 00 00 00 = 0
assert nodegraph.n_occupied() == 1
nodegraph.add('ACTG') # 00 10 01 11 =
assert nodegraph.n_occupied() == 2
nodegraph.add('AACG') # 00 00 10 11 = 11 # collision 1
assert nodegraph.n_occupied() == 2
nodegraph.add('AGAC') # 00 11 00 10 # collision 2
assert nodegraph.n_occupied() == 2, nodegraph.n_occupied()
def test_bloom_c_2(): # simple one
ksize = 4
# use only 1 hashtable, no bloom filter
nodegraph = khmer.Nodegraph(ksize, 1, 1, primes=[11])
nodegraph.count('AAAA') # 00 00 00 00 = 0
nodegraph.count('ACTG') # 00 10 01 11 =
assert nodegraph.n_unique_kmers() == 2
nodegraph.count('AACG') # 00 00 10 11 = 11 # collision with 1st kmer
assert nodegraph.n_unique_kmers() == 2
nodegraph.count('AGAC') # 00 11 00 10 # collision with 2nd kmer
assert nodegraph.n_unique_kmers() == 2
# use two hashtables with 11,13
other_nodegraph = khmer.Nodegraph(ksize, 1, 1, primes=[11, 13])
other_nodegraph.count('AAAA') # 00 00 00 00 = 0
other_nodegraph.count('ACTG') # 00 10 01 11 = 2*16 +4 +3 = 39
assert other_nodegraph.n_unique_kmers() == 2
# 00 00 10 11 = 11 # collision with only 1st kmer
other_nodegraph.count('AACG')
assert other_nodegraph.n_unique_kmers() == 3
other_nodegraph.count('AGAC')
# 00 11 00 10 3*16 +2 = 50
# collision with both 2nd and 3rd kmers
assert other_nodegraph.n_unique_kmers() == 3
def test_combine_pe():
inpfile = utils.get_test_data('combine_parts_1.fa')
nodegraph = khmer.Nodegraph(32, 1, 1)
nodegraph.consume_partitioned_fasta(inpfile)
assert nodegraph.count_partitions() == (2, 0)
first_seq = "CATGCAGAAGTTCCGCAACCATACCGTTCAGT"
pid1 = nodegraph.get_partition_id(first_seq)
second_seq = "CAAATGTACATGCACTTAAAATCATCCAGCCG"
pid2 = nodegraph.get_partition_id(second_seq)
assert pid1 == 2
assert pid2 == 80293
nodegraph.join_partitions(pid1, pid2)
pid1 = nodegraph.get_partition_id(first_seq)
pid2 = nodegraph.get_partition_id(second_seq)
assert pid1 == pid2
assert nodegraph.count_partitions() == (1, 0)
def test_load_partitioned():
inpfile = utils.get_test_data('combine_parts_1.fa')
nodegraph = khmer.Nodegraph(32, 1, 1)
nodegraph.consume_partitioned_fasta(inpfile)
assert nodegraph.count_partitions() == (2, 0)
first_seq = "CATGCAGAAGTTCCGCAACCATACCGTTCAGT"
assert nodegraph.get(first_seq)
second_seq = "CAAATGTACATGCACTTAAAATCATCCAGCCG"
assert nodegraph.get(second_seq)
third_s = "CATGCAGAAGTTCCGCAACCATACCGTTCAGTTCCTGGTGGCTA"[-32:]
assert nodegraph.get(third_s)
def test_consume_partitioned_fail():
inpfile = utils.get_test_data('test-reads.fa')
nodegraph = khmer.Nodegraph(32, 1, 1)
with pytest.raises(ValueError):
nodegraph.consume_partitioned_fasta(inpfile)
def test_count_within_radius_simple():
inpfile = utils.get_test_data('all-A.fa')
nodegraph = khmer.Nodegraph(4, 1, 1, primes=[3, 5])
print(nodegraph.consume_seqfile(inpfile))
n = nodegraph.count_kmers_within_radius('AAAA', 1)
assert n == 1
n = nodegraph.count_kmers_within_radius('AAAA', 10)
assert n == 1
def test_count_within_radius_big():
inpfile = utils.get_test_data('random-20-a.fa')
nodegraph = khmer.Nodegraph(20, 1e5, 4)
nodegraph.consume_seqfile(inpfile)
n = nodegraph.count_kmers_within_radius('CGCAGGCTGGATTCTAGAGG', int(1e6))
assert n == 3961, n
nodegraph = khmer.Nodegraph(21, 1e5, 4)
nodegraph.consume_seqfile(inpfile)
n = nodegraph.count_kmers_within_radius('CGCAGGCTGGATTCTAGAGGC', int(1e6))
assert n == 39
def test_count_kmer_degree():
inpfile = utils.get_test_data('all-A.fa')
nodegraph = khmer.Nodegraph(4, 1, 1, primes=[3, 5])
nodegraph.consume_seqfile(inpfile)
assert nodegraph.kmer_degree('AAAA') == 2
assert nodegraph.kmer_degree('AAAT') == 1
assert nodegraph.kmer_degree('AATA') == 0
assert nodegraph.kmer_degree('TAAA') == 1
def test_kmer_neighbors():
inpfile = utils.get_test_data('all-A.fa')
nodegraph = khmer.Nodegraph(4, 100, 1)
nodegraph.consume_seqfile(inpfile)
def n_to_str(x):
return [str(i) for i in x]
h = nodegraph.hash('AAAA')
print(type('AAAA'))
assert n_to_str(nodegraph.neighbors(
h)) == ['AAAA', 'AAAA'] # AAAA on both sides
assert n_to_str(nodegraph.neighbors('AAAA')) == [
'AAAA', 'AAAA'] # AAAA on both sides
h = nodegraph.hash('AAAT')
assert n_to_str(nodegraph.neighbors(h)) == ['AAAA'] # AAAA on 1 side
assert n_to_str(nodegraph.neighbors('AAAT')) == ['AAAA'] # AAAA on 1 side
h = nodegraph.hash('AATA')
assert nodegraph.neighbors(h) == [] # no neighbors
assert n_to_str(nodegraph.neighbors('AATA')) == [] # AAAA on one side
h = nodegraph.hash('TAAA')
assert n_to_str(nodegraph.neighbors(h)) == ['AAAA'] # AAAA on both
assert n_to_str(nodegraph.neighbors('TAAA')) == ['AAAA'] # AAAA on both
def test_kmer_neighbors_wrong_ksize():
inpfile = utils.get_test_data('all-A.fa')
nodegraph = khmer.Nodegraph(4, 1, 1, primes=[3, 5])
nodegraph.consume_seqfile(inpfile)
try:
nodegraph.neighbors('AAAAA')
assert 0, "neighbors() should fail with too long string"
except ValueError:
pass
try:
nodegraph.neighbors(b'AAAAA')
assert 0, "neighbors() should fail with too long string"
except ValueError:
pass
try:
nodegraph.neighbors({})
assert 0, "neighbors() should fail with non hash/str arg"
except TypeError:
pass
def test_save_load_tagset():
nodegraph = khmer.Nodegraph(32, 1, 1)
outfile = utils.get_temp_filename('tagset')
nodegraph.add_tag('A' * 32)
nodegraph.save_tagset(outfile)
nodegraph.add_tag('G' * 32)
nodegraph.load_tagset(outfile) # implicitly => clear_tags=True
nodegraph.save_tagset(outfile)
# if tags have been cleared, then the new tagfile will be larger (34 bytes)
# else smaller (26 bytes).
fp = open(outfile, 'rb')
data = fp.read()
fp.close()
assert len(data) == 30, len(data)
def test_save_load_tagset_noclear():
nodegraph = khmer.Nodegraph(32, 1, 1)
outfile = utils.get_temp_filename('tagset')
nodegraph.add_tag('A' * 32)
nodegraph.save_tagset(outfile)
nodegraph.add_tag('G' * 32)
nodegraph.load_tagset(outfile, False) # set clear_tags => False; zero tags
nodegraph.save_tagset(outfile)
# if tags have been cleared, then the new tagfile will be large (34 bytes);
# else small (26 bytes).
fp = open(outfile, 'rb')
data = fp.read()
fp.close()
assert len(data) == 38, len(data)
def test_stop_traverse():
filename = utils.get_test_data('random-20-a.fa')
ksize = 20 # size of kmer
htable_size = 1e4 # size of hashtable
num_nodegraphs = 3 # number of hashtables
nodegraph = khmer.Nodegraph(ksize, htable_size, num_nodegraphs)
# without tagging/joining across consume, this breaks into two partition;
# with, it is one partition.
nodegraph.add_stop_tag('TTGCATACGTTGAGCCAGCG')
# DO NOT join reads across stoptags
nodegraph.consume_seqfile_and_tag(filename)
subset = nodegraph.do_subset_partition(0, 0, True)
nodegraph.merge_subset(subset)
n, _ = nodegraph.count_partitions()
assert n == 2, n
def test_get_ksize():
kh = khmer.Nodegraph(22, 1, 1)
assert kh.ksize() == 22
def test_get_hashsizes():
kh = khmer.Nodegraph(22, 100, 4)
# Py2/3 hack, longify converts to long in py2, remove once py2 isn't
# supported any longer.
expected = utils.longify([97, 89, 83, 79])
assert kh.hashsizes() == expected, kh.hashsizes()
def test_extract_unique_paths_0():
kh = khmer.Nodegraph(10, 1, 1, primes=[5, 7, 11, 13])
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
assert x == ['ATGGAGAGACACAGATAGACAGGAGTGGCGATG']
kh.consume('ATGGAGAGACACAGATAGACAGGAGTGGCGATG')
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
assert not x
def test_extract_unique_paths_1():
kh = khmer.Nodegraph(10, 1, 1, primes=[5, 7, 11, 13])
kh.consume('AGTGGCGATG')
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
print(x)
assert x == ['ATGGAGAGACACAGATAGACAGGAGTGGCGAT'] # all but the last k-mer
def test_extract_unique_paths_2():
kh = khmer.Nodegraph(10, 1, 1, primes=[5, 7, 11, 13])
kh.consume('ATGGAGAGAC')
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
print(x)
assert x == ['TGGAGAGACACAGATAGACAGGAGTGGCGATG'] # all but the 1st k-mer
def test_extract_unique_paths_3():
kh = khmer.Nodegraph(10, 1, 1, primes=[5, 7, 11, 13])
kh.consume('ATGGAGAGAC')
kh.consume('AGTGGCGATG')
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
print(x)
# all but the 1st/last k-mer
assert x == ['TGGAGAGACACAGATAGACAGGAGTGGCGAT']
def test_extract_unique_paths_4():
kh = khmer.Nodegraph(10, 1e6, 4)
kh.consume('ATGGAGAGAC')
kh.consume('AGTGGCGATG')
kh.consume('ATAGACAGGA')
x = kh.extract_unique_paths('ATGGAGAGACACAGATAGACAGGAGTGGCGATG', 10, 1)
print(x)
assert x == ['TGGAGAGACACAGATAGACAGG', 'TAGACAGGAGTGGCGAT']
def test_get_raw_tables():
kh = khmer.Nodegraph(10, 1e6, 4)
kh.consume('ATGGAGAGAC')
kh.consume('AGTGGCGATG')
kh.consume('ATAGACAGGA')
tables = kh.get_raw_tables()
for size, table in zip(kh.hashsizes(), tables):
assert isinstance(table, memoryview)
assert size == len(table)
def test_simple_median():
hi = khmer.Nodegraph(6, 1e5, 2)
(median, average, stddev) = hi.get_median_count("AAAAAA")
print(median, average, stddev)
assert median == 0
assert average == 0.0
assert stddev == 0.0
hi.consume("AAAAAA")
(median, average, stddev) = hi.get_median_count("AAAAAA")
print(median, average, stddev)
assert median == 1
assert average == 1.0
assert stddev == 0.0
def test_badget():
hbts = khmer.Nodegraph(6, 1e6, 1)
dna = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAG"
hbts.consume(dna)
assert hbts.get("AGCTTT") == 1
assert hbts.get("GATGAG") == 0
try:
hbts.get(b"AGCTT")
assert 0, "this should fail"
except ValueError as err:
print(str(err))
try:
hbts.get(u"AGCTT")
assert 0, "this should fail"
except ValueError as err:
print(str(err))
#
def test_load_notexist_should_fail():
savepath = utils.get_temp_filename('tempnodegraphsave0.htable')
try:
hi = Countgraph.load(savepath)
assert 0, "load should fail"
except OSError:
pass
def test_load_truncated_should_fail():
inpath = utils.get_test_data('random-20-a.fa')
savepath = utils.get_temp_filename('tempnodegraphsave0.ct')
hi = khmer.Countgraph(12, 1000, 2)
hi.consume_seqfile(inpath)
hi.save(savepath)
fp = open(savepath, 'rb')
data = fp.read()
fp.close()
fp = open(savepath, 'wb')
fp.write(data[:1000])
fp.close()
try:
hi = Countgraph.load(savepath)
assert 0, "load should fail"
except OSError as e:
print(str(e))
def test_save_load_tagset_notexist():
nodegraph = khmer.Nodegraph(32, 1, 1)
outfile = utils.get_temp_filename('tagset')
try:
nodegraph.load_tagset(outfile)
assert 0, "this test should fail"
except OSError as e:
print(str(e))
def test_save_load_tagset_trunc():
nodegraph = khmer.Nodegraph(32, 1, 1)
outfile = utils.get_temp_filename('tagset')
nodegraph.add_tag('A' * 32)
nodegraph.add_tag('G' * 32)
nodegraph.save_tagset(outfile)
# truncate tagset file...
fp = open(outfile, 'rb')
data = fp.read()
fp.close()
for i in range(len(data)):
fp = open(outfile, 'wb')
fp.write(data[:i])
fp.close()
# try loading it...
try:
nodegraph.load_tagset(outfile)
assert 0, "this test should fail"
except OSError as err:
print(str(err), i)
# try loading it...
try:
nodegraph.load_tagset(outfile)
assert 0, "this test should fail"
except OSError:
pass
# to build the test files used below, add 'test' to this function
# and then look in /tmp. You will need to tweak the version info in
# khmer.hh in order to create "bad" versions, of course. -CTB
def _build_testfiles():
# nodegraph file
inpath = utils.get_test_data('random-20-a.fa')
hi = khmer.Nodegraph(12, 1, 1, primes=[2])
hi.consume_seqfile(inpath)
hi.save('/tmp/goodversion-k12.htable')
# tagset file
nodegraph = khmer.Nodegraph(32, 1, 1)
nodegraph.add_tag('A' * 32)
nodegraph.add_tag('G' * 32)
nodegraph.save_tagset('/tmp/goodversion-k32.tagset')
# stoptags file
fakelump_fa = utils.get_test_data('fakelump.fa')
nodegraph = khmer.Nodegraph(32, 100000, 4)
nodegraph.consume_seqfile_and_tag(fakelump_fa)
subset = nodegraph.do_subset_partition(0, 0)
nodegraph.merge_subset(subset)
EXCURSION_DISTANCE = 40
EXCURSION_KMER_THRESHOLD = 82
EXCURSION_KMER_COUNT_THRESHOLD = 1
counting = khmer.Countgraph(32, 100000, 4)
nodegraph.repartition_largest_partition(counting,
EXCURSION_DISTANCE,
EXCURSION_KMER_THRESHOLD,
EXCURSION_KMER_COUNT_THRESHOLD)
nodegraph.save_stop_tags('/tmp/goodversion-k32.stoptags')
def test_hashbits_file_version_check():
inpath = utils.get_test_data('badversion-k12.htable')
try:
nodegraph = Nodegraph.load(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_nodegraph_file_type_check():
kh = khmer.Countgraph(12, 1, 1)
savepath = utils.get_temp_filename('tempcountingsave0.ct')
kh.save(savepath)
try:
nodegraph = Nodegraph.load(savepath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_stoptags_file_version_check():
nodegraph = khmer.Nodegraph(32, 1, 1,)
inpath = utils.get_test_data('badversion-k32.stoptags')
try:
nodegraph.load_stop_tags(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_stoptags_ksize_check():
nodegraph = khmer.Nodegraph(31, 1, 1)
inpath = utils.get_test_data('goodversion-k32.stoptags')
try:
nodegraph.load_stop_tags(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_stop_tags_filetype_check():
nodegraph = khmer.Nodegraph(31, 1, 1)
inpath = utils.get_test_data('goodversion-k32.tagset')
try:
nodegraph.load_stop_tags(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_tagset_file_version_check():
nodegraph = khmer.Nodegraph(32, 1, 1)
inpath = utils.get_test_data('badversion-k32.tagset')
try:
nodegraph.load_tagset(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_stop_tags_truncate_check():
nodegraph = khmer.Nodegraph(32, 1, 1)
inpath = utils.get_test_data('goodversion-k32.tagset')
data = open(inpath, 'rb').read()
truncpath = utils.get_temp_filename('zzz')
for i in range(len(data)):
fp = open(truncpath, 'wb')
fp.write(data[:i])
fp.close()
try:
nodegraph.load_stop_tags(truncpath)
assert 0, "expect failure of previous command"
except OSError as e:
print(i, str(e))
def test_tagset_ksize_check():
nodegraph = khmer.Nodegraph(31, 1, 1)
inpath = utils.get_test_data('goodversion-k32.tagset')
try:
nodegraph.load_tagset(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_tagset_filetype_check():
nodegraph = khmer.Nodegraph(31, 1, 1)
inpath = utils.get_test_data('goodversion-k32.stoptags')
try:
nodegraph.load_tagset(inpath)
assert 0, "this should fail"
except OSError as e:
print(str(e))
def test_bad_primes_list():
try:
khmer.Nodegraph(31, 1, 1, primes=["a", "b", "c"])
assert 0, "Bad primes list should fail"
except TypeError as e:
print(str(e))
def test_consume_absentfasta():
nodegraph = khmer.Nodegraph(31, 1, 1)
try:
nodegraph.consume_seqfile()
assert 0, "this should fail"
except TypeError as err:
print(str(err))
try:
readparser = ReadParser(utils.get_test_data('empty-file'))
nodegraph.consume_seqfile(readparser)
assert 0, "this should fail"
except OSError as err:
print(str(err))
except ValueError as err:
print(str(err))
def test_bad_primes():
try:
Nodegraph(6, 1, 1, primes=["a", "b", "c"])
assert 0, "this should fail"
except TypeError as e:
print(str(e))
def test_consume_seqfile_and_tag_with_badreads_parser():
nodegraph = khmer.Nodegraph(6, 1e6, 2)
try:
readsparser = khmer.ReadParser(utils.get_test_data("test-empty.fa"))
nodegraph.consume_seqfile_and_tag(readsparser)
assert 0, "this should fail"
except OSError as e:
print(str(e))
except ValueError as e:
print(str(e))
def test_n_occupied_save_load():
filename = utils.get_test_data('random-20-a.fa')
nodegraph = khmer.Nodegraph(20, 100000, 3)
for _, record in enumerate(screed.open(filename)):
nodegraph.consume(record.sequence)
assert nodegraph.n_occupied() == 3884
assert nodegraph.n_unique_kmers() == 3960
savefile = utils.get_temp_filename('out')
nodegraph.save(savefile)
ng2 = Nodegraph.load(savefile)
assert ng2.n_occupied() == 3884, ng2.n_occupied()
assert ng2.n_unique_kmers() == 0 # this is intended behavior, sigh.
def test_n_occupied_vs_countgraph():
filename = utils.get_test_data('random-20-a.fa')
nodegraph = khmer.Nodegraph(20, 100000, 3)
countgraph = khmer.Countgraph(20, 100000, 3)
assert nodegraph.n_occupied() == 0, nodegraph.n_occupied()
assert countgraph.n_occupied() == 0, countgraph.n_occupied()
assert nodegraph.n_unique_kmers() == 0, nodegraph.n_unique_kmers()
assert countgraph.n_unique_kmers() == 0, countgraph.n_unique_kmers()
for _, record in enumerate(screed.open(filename)):
nodegraph.consume(record.sequence)
countgraph.consume(record.sequence)
assert nodegraph.hashsizes() == nodegraph.hashsizes()
# these are all the same -- good :).
assert nodegraph.n_occupied() == 3884, nodegraph.n_occupied()
assert countgraph.n_occupied() == 3884, countgraph.n_occupied()
assert nodegraph.n_unique_kmers() == 3960, nodegraph.n_unique_kmers()
assert countgraph.n_unique_kmers() == 3960, countgraph.n_unique_kmers()
def test_n_occupied_vs_countgraph_another_size():
filename = utils.get_test_data('random-20-a.fa')
nodegraph = khmer.Nodegraph(20, 10000, 3)
countgraph = khmer.Countgraph(20, 10000, 3)
assert nodegraph.n_occupied() == 0, nodegraph.n_occupied()
assert countgraph.n_occupied() == 0, countgraph.n_occupied()
assert nodegraph.n_unique_kmers() == 0, nodegraph.n_unique_kmers()
assert countgraph.n_unique_kmers() == 0, countgraph.n_unique_kmers()
for _, record in enumerate(screed.open(filename)):
nodegraph.consume(record.sequence)
countgraph.consume(record.sequence)
assert nodegraph.hashsizes() == nodegraph.hashsizes()
# these are all the same -- good :).
assert nodegraph.n_occupied() == 3269, nodegraph.n_occupied()
assert countgraph.n_occupied() == 3269, countgraph.n_occupied()
assert nodegraph.n_unique_kmers() == 3916, nodegraph.n_unique_kmers()
assert countgraph.n_unique_kmers() == 3916, countgraph.n_unique_kmers()
def test_traverse_linear_path():
contigfile = utils.get_test_data('simple-genome.fa')
contig = list(screed.open(contigfile))[0].sequence
K = 21
nodegraph = khmer.Nodegraph(K, 1e5, 4)
stopgraph = khmer.Nodegraph(K, 1e5, 4)
nodegraph.consume(contig)
degree_nodes = khmer.HashSet(K)
size, conns, visited = nodegraph.traverse_linear_path(contig[:K],
degree_nodes,
stopgraph)
assert size == 980
assert len(conns) == 0
assert len(visited) == 980
def test_find_high_degree_nodes():
contigfile = utils.get_test_data('simple-genome.fa')
contig = list(screed.open(contigfile))[0].sequence
K = 21
nodegraph = khmer.Nodegraph(K, 1e5, 4)
stopgraph = khmer.Nodegraph(K, 1e5, 4)
nodegraph.consume(contig)
degree_nodes = nodegraph.find_high_degree_nodes(contig)
assert len(degree_nodes) == 0
def test_find_high_degree_nodes_2():
contigfile = utils.get_test_data('simple-genome.fa')
contig = list(screed.open(contigfile))[0].sequence
K = 21
nodegraph = khmer.Nodegraph(K, 1e5, 4)
nodegraph.consume(contig)
nodegraph.count(contig[2:22] + 'G') # will add another neighbor to 1:22
print(nodegraph.neighbors(contig[1:22]))
degree_nodes = nodegraph.find_high_degree_nodes(contig)
assert len(degree_nodes) == 1
assert nodegraph.hash(contig[1:22]) in degree_nodes
def test_traverse_linear_path_2():
contigfile = utils.get_test_data('simple-genome.fa')
contig = list(screed.open(contigfile))[0].sequence
print('contig len', len(contig))
K = 21
nodegraph = khmer.Nodegraph(K, 1e5, 4)
stopgraph = khmer.Nodegraph(K, 1e5, 4)
nodegraph.consume(contig)
nodegraph.count(contig[101:121] + 'G') # will add another neighbor
print(nodegraph.neighbors(contig[101:122]))
degree_nodes = nodegraph.find_high_degree_nodes(contig)
assert len(degree_nodes) == 1
assert nodegraph.hash(contig[100:121]) in degree_nodes
# traverse from start, should end at node 100:121
size, conns, visited = nodegraph.traverse_linear_path(contig[0:21],
degree_nodes,
stopgraph)
print(size, list(conns), list(visited))
assert size == 100
assert len(visited) == 100
assert nodegraph.hash(contig[100:121]) in conns
assert len(conns) == 1
for k in conns: # everything in connections => stop
assert stopgraph.get(k)
for k in visited: # nothing in visited => stop
assert not stopgraph.get(k)
# traverse from immediately after 100:121, should end at the end
size, conns, visited = nodegraph.traverse_linear_path(contig[101:122],
degree_nodes,
stopgraph)
print(size, list(conns), list(visited))
assert size == 879
assert len(visited) == 879
assert nodegraph.hash(contig[100:121]) in conns
assert len(conns) == 1
for k in conns: # everything in connections => stop
assert stopgraph.get(k)
for k in visited: # nothing in visited => stop
assert not stopgraph.get(k)
# traverse from end, should end at 100:121
size, conns, visited = nodegraph.traverse_linear_path(contig[-21:],
degree_nodes,
stopgraph)
print(size, list(conns), len(visited))
assert size == 879
assert len(visited) == 879
assert nodegraph.hash(contig[100:121]) in conns
assert len(conns) == 1
for k in conns: # everything in connections => stop
assert stopgraph.get(k)
for k in visited: # nothing in visited => stop
assert not stopgraph.get(k)
def test_traverse_linear_path_3_stopgraph():
contigfile = utils.get_test_data('simple-genome.fa')
contig = list(screed.open(contigfile))[0].sequence
print('contig len', len(contig))
K = 21
nodegraph = khmer.Nodegraph(K, 1e5, 4)
stopgraph = khmer.Nodegraph(K, 1e5, 4)
nodegraph.consume(contig)
nodegraph.count(contig[101:121] + 'G') # will add another neighbor
print(nodegraph.neighbors(contig[101:122]))
degree_nodes = nodegraph.find_high_degree_nodes(contig)
assert len(degree_nodes) == 1
assert nodegraph.hash(contig[100:121]) in degree_nodes
stopgraph.count(contig[101:122]) # stop traversal - only adj to start
size, conns, visited = nodegraph.traverse_linear_path(contig[101:122],
degree_nodes,
stopgraph)
print(size, list(conns), len(visited))
assert size == 0
assert len(visited) == 0
assert len(conns) == 0
@pytest.mark.parametrize('ntables,targetsize', [
(4, 1e5),
(6, 1e5),
(8, 1e5),
(5, 1e6),
(7, 1e6),
(9, 1e6),
])
def test_create_matching_nodegraph(ntables, targetsize):
cg = khmer.Countgraph(31, targetsize, ntables)
ng = create_matching_nodegraph(cg)
assert cg.hashsizes() == ng.hashsizes()
|