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
|
#!/usr/bin/env python
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""Tests for pairwise2 module.
Put new test case here, the classes here will be imported and run
as TestCases in ``test_pairwise2.py`` and ``test_pairwise2_no_C.py``
with or without complementing C extensions.
"""
import pickle
import unittest
import warnings
from Bio import BiopythonWarning
from Bio import pairwise2
from Bio.Align import substitution_matrices
class TestPairwiseErrorConditions(unittest.TestCase):
"""Test several error conditions."""
def test_function_name(self):
"""Test for wrong function names."""
# Function name pattern must be globalXX or localXX
self.assertRaises(AttributeError, lambda: pairwise2.align.globalxxx)
self.assertRaises(AttributeError, lambda: pairwise2.align.localxxx)
self.assertRaises(AttributeError, lambda: pairwise2.align.glocalxx)
# First X must be from (x, m, d, c), second from (x, s, d, c)
self.assertRaises(AttributeError, lambda: pairwise2.align.globalax)
self.assertRaises(AttributeError, lambda: pairwise2.align.globalxa)
def test_function_parameters(self):
"""Test for number of parameters."""
# globalxx takes two parameters
self.assertRaises(TypeError, pairwise2.align.globalxx, "A")
# matrix_only is no keyword argument
self.assertRaises(
TypeError, pairwise2.align.globalxx, "A", "C", {"matrix_only": True}
)
# Both sequences must be either strings or lists
self.assertRaises(TypeError, pairwise2.align.globalxx, "A", ["C"])
# If both sequences are lists, gap_char must also be set as list
self.assertRaises(TypeError, pairwise2.align.globalxx, ["A"], ["C"])
# If one or both sequences are empty, there is no alignment
alignment = pairwise2.align.globalxx("A", "")
self.assertEqual(alignment, [])
# Gap scores must be negative
self.assertRaises(ValueError, pairwise2.align.globalxs, "A", "C", 5, -1)
self.assertRaises(ValueError, pairwise2.align.globalxs, "A", "C", -5, 1)
# Gap open penalty must be higher than gap extension penalty
self.assertRaises(ValueError, pairwise2.align.globalxs, "A", "C", -1, -5)
def test_param_names(self):
"""Test for unknown parameter in parameter names."""
a = pairwise2.align.alignment_function("globalxx")
a.param_names = ["Hello"]
self.assertRaises(ValueError, a.decode, "Bye")
def test_warnings(self):
"""Test for warnings."""
with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
# Trigger a warning.
pairwise2.align.localxx("GA", "CGA", penalize_end_gaps=True)
# Verify some things
self.assertEqual(len(w), 1)
self.assertEqual(w[-1].category, BiopythonWarning)
self.assertIn("should not", str(w[-1].message))
class TestPairwiseKeywordUsage(unittest.TestCase):
"""Tests for keyword usage."""
def test_keywords(self):
"""Test equality of calls with and without keywords."""
aligns = pairwise2.align.globalxx("GAACT", "GAT")
aligns_kw = pairwise2.align.globalxx(sequenceA="GAACT", sequenceB="GAT")
self.assertEqual(aligns, aligns_kw)
aligns = pairwise2.align.globalmx("GAACT", "GAT", 5, -4)
aligns_kw = pairwise2.align.globalmx(
sequenceA="GAACT", sequenceB="GAT", match=5, mismatch=-4
)
self.assertEqual(aligns, aligns_kw)
class TestPairwiseGlobal(unittest.TestCase):
"""Test some usual global alignments."""
def test_globalxx_simple(self):
"""Test globalxx."""
aligns = pairwise2.align.globalxx("GAACT", "GAT")
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GAACT
| | |
G-A-T
Score=3
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GAACT
|| |
GA--T
Score=3
""",
)
def test_globalxx_simple2(self):
"""Do the same test with sequence order reversed."""
aligns = pairwise2.align.globalxx("GAT", "GAACT")
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
G-A-T
| | |
GAACT
Score=3
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GA--T
|| |
GAACT
Score=3
""",
)
def test_one_alignment_only(self):
"""Test one_alignment_only parameter."""
aligns = pairwise2.align.globalxx("ACCGT", "ACG")
self.assertEqual(len(aligns), 2)
aligns = pairwise2.align.globalxx("ACCGT", "ACG", one_alignment_only=True)
self.assertEqual(len(aligns), 1)
def test_list_input(self):
"""Do a global alignment with sequences supplied as lists."""
aligns = pairwise2.align.globalxx(
["Gly", "Ala", "Thr"], ["Gly", "Ala", "Ala", "Cys", "Thr"], gap_char=["---"]
)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
self.assertEqual(score, 3)
self.assertEqual(seq1, ["Gly", "---", "Ala", "---", "Thr"])
self.assertEqual(seq2, ["Gly", "Ala", "Ala", "Cys", "Thr"])
class TestPairwiseLocal(unittest.TestCase):
"""Test some simple local alignments."""
def setUp(self):
self.blosum62 = substitution_matrices.load("BLOSUM62")
def test_localxs_1(self):
"""Test localxx."""
aligns = sorted(pairwise2.align.localxs("AxBx", "zABz", -0.1, 0))
# From Biopython 1.74 on this should only give one alignment, since
# we disallow leading and trailing 'zero-extensions'
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 AxB
| |
2 A-B
Score=1.9
""",
)
def test_localxs_2(self):
"""Test localxx with ``full_sequences=True``."""
aligns = sorted(pairwise2.align.localxs("AxBx", "zABz", -0.1, 0))
# From Biopython 1.74 on this should only give one alignment, since
# we disallow leading and trailing 'zero-extensions'
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(
seq1, seq2, score, begin, end, full_sequences=True
)
self.assertEqual(
alignment,
"""\
-AxBx
| |
zA-Bz
Score=1.9
""", # noqa: W291
)
def test_localds_zero_score_segments_symmetric(self):
"""Test if alignment is independent on direction of sequence."""
aligns1 = pairwise2.align.localds(
"CWHISLKM", "CWHGISGLKM", self.blosum62, -11, -1
)
aligns2 = pairwise2.align.localds(
"MKLSIHWC", "MKLGSIGHWC", self.blosum62, -11, -1
)
self.assertEqual(len(aligns1), len(aligns2))
def test_localxs_generic(self):
"""Test the generic method with local alignments."""
aligns = sorted(
pairwise2.align.localxs("AxBx", "zABz", -0.1, 0, force_generic=True)
)
# From Biopython 1.74 on this should only give one alignment, since
# we disallow leading and trailing 'zero-extensions'
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 AxB
| |
2 A-B
Score=1.9
""",
)
def test_localms(self):
"""Two different local alignments."""
aligns = sorted(
pairwise2.align.localms("xxxABCDxxx", "zzzABzzCDz", 1, -0.5, -3, -1)
)
alignment = pairwise2.format_alignment(*aligns[0])
self.assertEqual(
alignment,
"""\
6 CD
||
8 CD
Score=2
""",
)
alignment = pairwise2.format_alignment(*aligns[1])
self.assertEqual(
alignment,
"""\
4 AB
||
4 AB
Score=2
""",
)
def test_blosum62(self):
"""Test localds with blosum62."""
self.assertEqual(1, self.blosum62[("K", "Q")])
self.assertEqual(4, self.blosum62[("A", "A")])
self.assertEqual(8, self.blosum62[("H", "H")])
alignments = pairwise2.align.localds(
"VKAHGKKV", "FQAHCAGV", self.blosum62, -4, -4
)
for a in alignments:
self.assertEqual(
pairwise2.format_alignment(*a), "2 KAH\n .||\n2 QAH\n Score=13\n"
)
def test_empty_result(self):
"""Return no alignment."""
self.assertEqual(pairwise2.align.localxx("AT", "GC"), [])
class TestScoreOnly(unittest.TestCase):
"""Test parameter ``score_only``."""
def test_score_only_global(self):
"""Test ``score_only`` in a global alignment."""
aligns1 = pairwise2.align.globalxx("GAACT", "GAT")
aligns2 = pairwise2.align.globalxx("GAACT", "GAT", score_only=True)
self.assertEqual(aligns1[0][2], aligns2)
def test_score_only_local(self):
"""Test ``score_only`` in a local alignment."""
aligns1 = pairwise2.align.localms("xxxABCDxxx", "zzzABzzCDz", 1, -0.5, -3, -1)
aligns2 = pairwise2.align.localms(
"xxxABCDxxx", "zzzABzzCDz", 1, -0.5, -3, -1, score_only=True
)
self.assertEqual(aligns1[0][2], aligns2)
class TestPairwiseOpenPenalty(unittest.TestCase):
"""Alignments with gap-open penalty."""
def test_match_score_open_penalty1(self):
"""Test 1."""
aligns = pairwise2.align.globalms("AA", "A", 2.0, -1, -0.1, 0)
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
AA
|
-A
Score=1.9
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
AA
|
A-
Score=1.9
""", # noqa: W291
)
def test_match_score_open_penalty2(self):
"""Test 2."""
aligns = pairwise2.align.globalms("GAA", "GA", 1.5, 0, -0.1, 0)
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GAA
| |
G-A
Score=2.9
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GAA
||
GA-
Score=2.9
""", # noqa: W291
)
def test_match_score_open_penalty3(self):
"""Test 3."""
aligns = pairwise2.align.globalxs("GAACT", "GAT", -0.1, 0)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GAACT
|| |
GA--T
Score=2.9
""",
)
def test_match_score_open_penalty4(self):
"""Test 4."""
aligns = pairwise2.align.globalms("GCT", "GATA", 1, -2, -0.1, 0)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GC-T-
| |
G-ATA
Score=1.7
""", # noqa: W291
)
class TestPairwiseExtendPenalty(unittest.TestCase):
"""Alignments with gap-extend penalties."""
def test_extend_penalty1(self):
"""Test 1."""
aligns = pairwise2.align.globalxs("GACT", "GT", -0.5, -0.2)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
| |
G--T
Score=1.3
""",
)
def test_extend_penalty2(self):
"""Test 2."""
aligns = pairwise2.align.globalxs("GACT", "GT", -1.5, -0.2)
self.assertEqual(len(aligns), 1)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
| |
G--T
Score=0.3
""",
)
class TestPairwisePenalizeExtendWhenOpening(unittest.TestCase):
"""Alignment with ``penalize_extend_when_opening``."""
def test_penalize_extend_when_opening(self):
"""Add gap-extend penalty to gap-opening penalty."""
aligns = pairwise2.align.globalxs(
"GACT", "GT", -0.2, -1.5, penalize_extend_when_opening=1
)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
| |
G--T
Score=-1.2
""",
)
class TestPairwisePenalizeEndgaps(unittest.TestCase):
"""Alignments with end-gaps penalized or not."""
def test_penalize_end_gaps(self):
"""Turn off end-gap penalties."""
aligns = pairwise2.align.globalxs("GACT", "GT", -0.8, -0.2, penalize_end_gaps=0)
self.assertEqual(len(aligns), 3)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
.|
--GT
Score=1
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
| |
G--T
Score=1
""",
)
seq1, seq2, score, begin, end = aligns[2]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
|.
GT--
Score=1
""", # noqa: W291
)
def test_penalize_end_gaps2(self):
"""Do the same, but use the generic method (with the same result)."""
aligns = pairwise2.align.globalxs(
"GACT", "GT", -0.8, -0.2, penalize_end_gaps=0, force_generic=True
)
self.assertEqual(len(aligns), 3)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
.|
--GT
Score=1
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
| |
G--T
Score=1
""",
)
seq1, seq2, score, begin, end = aligns[2]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GACT
|.
GT--
Score=1
""", # noqa: W291
)
def test_separate_penalize_end_gaps(self):
"""Test alignment where end-gaps are differently penalized."""
align = pairwise2.align.globalms(
"AT", "AGG", 1.0, -0.5, -1.75, -0.25, penalize_end_gaps=(True, False)
)
self.assertEqual(align[0], ("A--T", "AGG-", -1.0, 0, 4))
class TestPairwiseSeparateGapPenalties(unittest.TestCase):
"""Alignments with separate gap-open penalties for both sequences."""
def test_separate_gap_penalties1(self):
"""Test 1."""
aligns = pairwise2.align.localxd("GAT", "GTCT", -0.3, 0, -0.8, 0)
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
G-AT
| .|
GTCT
Score=1.7
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
GA-T
|. |
GTCT
Score=1.7
""",
)
def test_separate_gap_penalties2(self):
"""Test 2."""
aligns = pairwise2.align.localxd("GAT", "GTCT", -0.5, 0, -0.2, 0)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 GAT
| |
1 G-T
Score=1.8
""",
)
class TestPairwiseSeparateGapPenaltiesWithExtension(unittest.TestCase):
"""Alignments with separate gap-extension penalties for both sequences."""
def test_separate_gap_penalties_with_extension(self):
"""Test separate gap-extension penalties and list input."""
aligns = pairwise2.align.localxd(
list("GAAT"), list("GTCCT"), -0.1, 0, -0.1, -0.1, gap_char=["-"]
)
self.assertEqual(len(aligns), 3)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
G - A A T
| . . |
G T C C T
Score=1.9
""", # noqa: W291
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
G A - A T
| . . |
G T C C T
Score=1.9
""", # noqa: W291
)
seq1, seq2, score, begin, end = aligns[2]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
G A A - T
| . . |
G T C C T
Score=1.9
""", # noqa: W291
)
class TestPairwiseMatchDictionary(unittest.TestCase):
"""Alignments with match dictionaries."""
match_dict = {("A", "A"): 1.5, ("A", "T"): 0.5, ("T", "T"): 1.0}
def test_match_dictionary1(self):
"""Test 1."""
aligns = pairwise2.align.localds("ATAT", "ATT", self.match_dict, -0.5, 0)
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
ATAT
|| |
AT-T
Score=3
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 ATA
||.
1 ATT
Score=3
""",
)
def test_match_dictionary2(self):
"""Test 2."""
aligns = pairwise2.align.localds("ATAT", "ATT", self.match_dict, -1, 0)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 ATA
||.
1 ATT
Score=3
""",
)
def test_match_dictionary3(self):
"""Test 3."""
aligns = pairwise2.align.localds("ATT", "ATAT", self.match_dict, -1, 0)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
1 ATT
||.
1 ATA
Score=3
""",
)
class TestPairwiseOneCharacter(unittest.TestCase):
"""Alignments where one sequence has length 1."""
def test_align_one_char1(self):
"""Test sequence with only one match."""
aligns = pairwise2.align.localxs("abcde", "c", -0.3, -0.1)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
3 c
|
1 c
Score=1
""",
)
def test_align_one_char2(self):
"""Test sequences with two possible match positions."""
aligns = pairwise2.align.localxs("abcce", "c", -0.3, -0.1)
self.assertEqual(len(aligns), 2)
aligns.sort()
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
4 c
|
1 c
Score=1
""",
)
seq1, seq2, score, begin, end = aligns[1]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
3 c
|
1 c
Score=1
""",
)
def test_align_one_char3(self):
"""Like test 1, but global alignment."""
aligns = pairwise2.align.globalxs("abcde", "c", -0.3, -0.1)
self.assertEqual(len(aligns), 1)
seq1, seq2, score, begin, end = aligns[0]
alignment = pairwise2.format_alignment(seq1, seq2, score, begin, end)
self.assertEqual(
alignment,
"""\
abcde
|
--c--
Score=0.2
""", # noqa: W291
)
class TestPersiteGapPenalties(unittest.TestCase):
"""Check gap penalty callbacks use correct gap opening position.
This tests that the gap penalty callbacks are really being used
with the correct gap opening position.
"""
def test_gap_here_only_1(self):
"""Open a gap in second sequence only."""
seq1 = "AAAABBBAAAACCCCCCCCCCCCCCAAAABBBAAAA"
seq2 = "AABBBAAAACCCCAAAABBBAA"
def no_gaps(x, y):
"""Very expensive to open a gap in seq1."""
return -2000 - y
def specific_gaps(x, y):
"""Very expensive to open a gap in seq2.
...unless it is in one of the allowed positions:
"""
breaks = [0, 11, len(seq2)]
return (-2 - y) if x in breaks else (-2000 - y)
alignments = pairwise2.align.globalmc(seq1, seq2, 1, -1, no_gaps, specific_gaps)
self.assertEqual(len(alignments), 1)
formatted = pairwise2.format_alignment(*alignments[0])
self.assertEqual(
formatted,
"""\
AAAABBBAAAACCCCCCCCCCCCCCAAAABBBAAAA
||||||||||| |||||||||||
--AABBBAAAACC----------CCAAAABBBAA--
Score=2
""", # noqa: W291
)
def test_gap_here_only_2(self):
"""Force a bad alignment.
Forces a bad alignment by having a very expensive gap penalty
where one would normally expect a gap, and a cheap gap penalty
in another place.
"""
seq1 = "AAAABBBAAAACCCCCCCCCCCCCCAAAABBBAAAA"
seq2 = "AABBBAAAACCCCAAAABBBAA"
def no_gaps(x, y):
"""Very expensive to open a gap in seq1."""
return -2000 - y
def specific_gaps(x, y):
"""Very expensive to open a gap in seq2.
...unless it is in one of the allowed positions:
"""
breaks = [0, 3, len(seq2)]
return (-2 - y) if x in breaks else (-2000 - y)
alignments = pairwise2.align.globalmc(seq1, seq2, 1, -1, no_gaps, specific_gaps)
self.assertEqual(len(alignments), 1)
formatted = pairwise2.format_alignment(*alignments[0])
self.assertEqual(
formatted,
"""\
AAAABBBAAAACCCCCCCCCCCCCCAAAABBBAAAA
||| ......|||||||||||||
--AAB----------BBAAAACCCCAAAABBBAA--
Score=-10
""", # noqa: W291
)
class TestOtherFunctions(unittest.TestCase):
"""Test remaining non-tested private methods."""
def test_clean_alignments(self):
"""``_clean_alignments`` removes redundant and wrong alignments."""
alns = [
("ACCGT", "AC-G-", 3.0, 0, 4),
("ACCGT", "AC-G-", 3.0, 1, 1),
("ACCGT", "A-CG-", 3.0, 0, 4),
("ACCGT", "AC-G-", 3.0, 0, 4),
("ACCGT", "A-CG-", 3.0, 0, 4),
]
expected = [("ACCGT", "AC-G-", 3.0, 0, 4), ("ACCGT", "A-CG-", 3.0, 0, 4)]
result = pairwise2._clean_alignments(alns)
self.assertEqual(expected, result)
def test_alignments_can_be_pickled(self):
alns = [("ACCGT", "AC-G-", 3.0, 0, 4)]
expected = [("ACCGT", "AC-G-", 3.0, 0, 4)]
result = pickle.loads(pickle.dumps(pairwise2._clean_alignments(alns)))
self.assertEqual(expected, result)
def test_print_matrix(self):
"""``print_matrix`` prints nested lists as nice matrices."""
import sys
from io import StringIO
out = StringIO()
sys.stdout = out
pairwise2.print_matrix(
[
[0.0, -1.0, -1.5, -2.0],
[-1.0, 4.0, 3.0, 2.5],
[-1.5, 3.0, 8.0, 7.0],
[-2.0, 2.5, 7.0, 6.0],
[-2.5, 2.0, 6.5, 11.0],
[-3.0, 1.5, 6.0, 10.0],
]
)
self.assertEqual(
out.getvalue(),
" 0.0 -1.0 -1.5 -2.0 \n"
"-1.0 4.0 3.0 2.5 \n"
"-1.5 3.0 8.0 7.0 \n"
"-2.0 2.5 7.0 6.0 \n"
"-2.5 2.0 6.5 11.0 \n"
"-3.0 1.5 6.0 10.0 \n",
)
sys.stdout = sys.__stdout__
def test_recover_alignments(self):
"""One possible start position in local alignment is not a match."""
self.assertEqual(len(pairwise2.align.localxx("AC", "GA")), 1)
if __name__ == "__main__":
if pairwise2.rint != pairwise2._python_rint:
# This uses the default C extensions, if import didn't fail.
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner, exit=False)
else:
print(
"Import of C functions failed. Only testing pure Python "
"fallback functions."
)
# Now, we switch explicitly to the fallback Python functions:
pairwise2._make_score_matrix_fast = pairwise2._python_make_score_matrix_fast
pairwise2.rint = pairwise2._python_rint
runner = unittest.TextTestRunner(verbosity=2)
unittest.main(testRunner=runner)
|