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
|
from io import StringIO
from collections import defaultdict
from numpy.testing import (
assert_equal,
assert_array_equal,
)
import pytest
import MDAnalysis
from MDAnalysis.analysis.hydrogenbonds.wbridge_analysis import (
WaterBridgeAnalysis,
)
class TestWaterBridgeAnalysis(object):
@staticmethod
@pytest.fixture(scope="class")
def universe_empty():
"""A universe with no hydrogen bonds"""
grofile = """Test gro file
5
1ALA N 1 0.000 0.000 0.000
1ALA H 2 0.100 0.000 0.000
2SOL OW 3 3.000 0.000 0.000
4ALA H 4 0.500 0.000 0.000
4ALA N 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_DA():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen bond
donor"""
grofile = """Test gro file
3
1ALA N 1 0.000 0.000 0.000
1ALA H 2 0.100 0.000 0.000
4ALA O 3 0.300 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_DA_PBC():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen bond
donor but in a PBC condition"""
grofile = """Test gro file
3
1ALA N 1 0.800 0.000 0.000
1ALA H 2 0.900 0.000 0.000
4ALA O 3 0.100 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AD():
"""A universe with one hydrogen bond donor bonding to a hydrogen bond
acceptor"""
grofile = """Test gro file
3
1ALA O 1 0.000 0.000 0.000
4ALA H 2 0.200 0.000 0.000
4ALA N 3 0.300 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_loop():
"""A universe with one hydrogen bond acceptor bonding to a water which
bonds back to the first hydrogen bond acceptor and thus form a loop"""
grofile = """Test gro file
5
1ALA O 1 0.000 0.001 0.000
2SOL OW 2 0.300 0.001 0.000
2SOL HW1 3 0.200 0.002 0.000
2SOL HW2 4 0.200 0.000 0.000
4ALA O 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_DWA():
"""A universe with one hydrogen bond donor bonding to a hydrogen bond
acceptor through a water"""
grofile = """Test gro file
5
1ALA N 1 0.000 0.000 0.000
1ALA H 2 0.100 0.000 0.000
2SOL OW 3 0.300 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
4ALA O 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_DWD():
"""A universe with one hydrogen bond donor bonding to a hydrogen bond
donor through a water"""
grofile = """Test gro file
5
1ALA N 1 0.000 0.000 0.000
1ALA H 2 0.100 0.000 0.000
2SOL OW 3 0.300 0.000 0.000
4ALA H 4 0.500 0.000 0.000
4ALA N 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWA():
"""A universe with two hydrogen bond acceptor are joined by a water"""
grofile = """Test gro file
5
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
4ALA O 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWD():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen
bond donor through a water"""
grofile = """Test gro file
5
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
4ALA H 4 0.500 0.000 0.000
4ALA N 5 0.600 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWWA():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen bond
acceptor through two waters"""
grofile = """Test gro file
7
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
3SOL OW 5 0.600 0.000 0.000
3SOL HW1 6 0.700 0.000 0.000
4ALA O 7 0.900 0.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWWWA():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen bond
acceptor through three waters"""
grofile = """Test gro file
9
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
3SOL OW 5 0.600 0.000 0.000
3SOL HW1 6 0.700 0.000 0.000
4SOL OW 7 0.900 0.000 0.000
4SOL HW1 8 1.000 0.000 0.000
5ALA O 9 1.200 0.000 0.000
10.0 10.0 10.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWWWWA():
"""A universe with one hydrogen bond acceptor bonding to a hydrogen bond
acceptor through three waters"""
grofile = """Test gro file
11
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
3SOL OW 5 0.600 0.000 0.000
3SOL HW1 6 0.700 0.000 0.000
4SOL OW 7 0.900 0.000 0.000
4SOL HW1 8 1.000 0.000 0.000
5SOL OW 9 1.200 0.000 0.000
5SOL HW1 10 1.300 0.000 0.000
6ALA O 11 1.400 0.000 0.000
10.0 10.0 10.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_branch():
"""A universe with one hydrogen bond acceptor bonding to two hydrogen
bond acceptor in selection 2"""
grofile = """Test gro file
9
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
3SOL OW 5 0.600 0.000 0.000
3SOL HW1 6 0.700 0.000 0.000
3SOL HW2 7 0.600 0.100 0.000
4ALA O 8 0.900 0.000 0.000
5ALA O 9 0.600 0.300 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def universe_AWA_AWWA():
"""A universe with one hydrogen bond acceptors are bonded through one or
two water"""
grofile = """Test gro file
12
1ALA O 1 0.000 0.000 0.000
2SOL OW 2 0.300 0.000 0.000
2SOL HW1 3 0.200 0.000 0.000
2SOL HW2 4 0.400 0.000 0.000
4ALA O 5 0.600 0.000 0.000
5ALA O 6 0.000 1.000 0.000
6SOL OW 7 0.300 1.000 0.000
6SOL HW1 8 0.200 1.000 0.000
6SOL HW2 9 0.400 1.000 0.000
7SOL OW 10 0.600 1.000 0.000
7SOL HW1 11 0.700 1.000 0.000
8ALA O 12 0.900 1.000 0.000
1.0 1.0 1.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
return u
@staticmethod
@pytest.fixture(scope="class")
def wb_multiframe():
"""A water bridge object with multipley frames"""
grofile = """Test gro file
13
1ALA O 1 0.000 0.000 0.000
1ALA H 2 0.000 0.000 0.000
2SOL OW 3 0.300 0.000 0.000
2SOL HW1 4 0.200 0.000 0.000
2SOL HW2 5 0.400 0.000 0.000
3SOL OW 6 0.600 0.000 0.000
3SOL HW1 7 0.700 0.000 0.000
4SOL OW 8 0.900 0.000 0.000
4SOL HW1 9 1.000 0.000 0.000
5SOL OW 10 1.200 0.000 0.000
5SOL HW1 11 1.300 0.000 0.000
6ALA H 12 1.400 0.000 0.000
6ALA O 13 1.400 0.000 0.000
10.0 10.0 10.0"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
wb = WaterBridgeAnalysis(
u, "protein and (resid 1)", "protein and (resid 4)", order=4
)
# Build an dummy WaterBridgeAnalysis object for testing
wb.results.network = []
wb.results.network.append({(1, 0, 12, None, 2.0, 180.0): None})
wb.results.network.append({(0, None, 12, 13, 2.0, 180.0): None})
wb.results.network.append(
{(1, 0, 3, None, 2.0, 180.0): {(4, 2, 12, None, 2.0, 180.0): None}}
)
wb.results.network.append(
{
(0, None, 3, 2, 2.0, 180.0): {
(4, 2, 5, None, 2.0, 180.0): {
(5, None, 11, 12, 2.0, 180.0): None
}
}
}
)
wb.timesteps = range(len(wb.results.network))
return wb
def test_nodata(self, universe_DA):
"""Test if the funtions can run when there is no data.
This is achieved by not runing the run() first."""
wb = WaterBridgeAnalysis(
universe_DA,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
)
wb.generate_table()
assert_equal(wb.timesteps_by_type(), None)
assert_equal(wb.count_by_time(), None)
assert_equal(wb.count_by_type(), None)
def test_selection_type_error(self, universe_DA):
"""Test the case when the wrong selection1_type is given"""
try:
wb = WaterBridgeAnalysis(
universe_DA,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
selection1_type="aaa",
)
except ValueError:
pass
else:
raise pytest.fail("selection_type aaa should rasie error")
def test_distance_type_error(self, universe_DA):
"""Test the case when the wrong selection1_type is given"""
with pytest.raises(
ValueError,
match="Only 'hydrogen' and 'heavy' are allowed for option `distance_type'",
):
WaterBridgeAnalysis(
universe_DA,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
selection1_type="aaa",
distance_type="aaa",
)
def test_selection2_type_error(self, universe_DA):
"""Test the case when the wrong selection1_type is given"""
with pytest.raises(
ValueError, match="`selection2_type` is not a keyword argument."
):
WaterBridgeAnalysis(
universe_DA,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
selection1_type="aaa",
selection2_type="aaa",
)
def test_empty_selection(self, universe_DA):
"""Test the case when selection yields empty result"""
wb = WaterBridgeAnalysis(
universe_DA,
"protein and (resid 9)",
"protein and (resid 10)",
order=0,
)
wb.run()
assert wb.results.network == [{}]
def test_loop(self, universe_loop):
"""Test if loop can be handled correctly"""
wb = WaterBridgeAnalysis(
universe_loop,
"protein and (resid 1)",
"protein and (resid 1 or resid 4)",
)
wb.run()
assert_equal(len(wb.results.network[0].keys()), 2)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_donor_accepter(self, universe_DA, distance_type):
"""Test zeroth order donor to acceptor hydrogen bonding"""
wb = WaterBridgeAnalysis(
universe_DA,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
update_selection=True,
debug=True,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (1, 0, 2, None))
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_donor_accepter_pbc(self, universe_DA_PBC, distance_type):
"""Test zeroth order donor to acceptor hydrogen bonding in PBC conditions"""
wb = WaterBridgeAnalysis(
universe_DA_PBC,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
pbc=True,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (1, 0, 2, None))
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_accepter_donor(self, universe_AD, distance_type):
"""Test zeroth order acceptor to donor hydrogen bonding"""
wb = WaterBridgeAnalysis(
universe_AD,
"protein and (resid 1)",
"protein and (resid 4)",
order=0,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 1, 2))
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_water_accepter(self, universe_AWA, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form
water bridge with hydrogen bond acceptor from selection 2"""
wb = WaterBridgeAnalysis(
universe_AWA,
"protein and (resid 1)",
"protein and (resid 4)",
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
assert_equal(second[list(second.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_donor_water_accepter(self, universe_DWA, distance_type):
"""Test case where the hydrogen bond donor from selection 1 form
water bridge with hydrogen bond acceptor from selection 2"""
wb = WaterBridgeAnalysis(
universe_DWA,
"protein and (resid 1)",
"protein and (resid 4)",
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (1, 0, 2, None))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 2, 4, None))
assert_equal(second[list(second.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_water_donor(self, universe_AWD, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form
water bridge with hydrogen bond donor from selection 2"""
wb = WaterBridgeAnalysis(
universe_AWD,
"protein and (resid 1)",
"protein and (resid 4)",
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (1, None, 3, 4))
assert_equal(second[list(second.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_donor_water_donor(self, universe_DWD, distance_type):
"""Test case where the hydrogen bond donor from selection 1 form
water bridge with hydrogen bond donor from selection 2"""
wb = WaterBridgeAnalysis(
universe_DWD,
"protein and (resid 1)",
"protein and (resid 4)",
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (1, 0, 2, None))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (2, None, 3, 4))
assert_equal(second[list(second.keys())[0]], None)
def test_empty(self, universe_empty):
"""Test case where no water bridge exists"""
wb = WaterBridgeAnalysis(universe_empty, "protein", "protein")
wb.run(verbose=False)
assert_equal(wb.results.network[0], defaultdict(dict))
def test_same_selection(self, universe_DWA):
"""
This test tests that if the selection 1 and selection 2 are both protein.
However, the protein only forms one hydrogen bond with the water.
This entry won't be included.
"""
wb = WaterBridgeAnalysis(
universe_DWA, "protein and resid 1", "protein and resid 1"
)
wb.run(verbose=False)
assert_equal(wb.results.network[0], defaultdict(dict))
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_2water_accepter(self, universe_AWWA, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form second order
water bridge with hydrogen bond acceptor from selection 2"""
# test first order
wb = WaterBridgeAnalysis(
universe_AWWA,
"protein and (resid 1)",
"protein and (resid 4)",
distance_type=distance_type,
)
wb.run(verbose=False)
assert_equal(wb.results.network[0], defaultdict(dict))
# test second order
wb = WaterBridgeAnalysis(
universe_AWWA,
"protein and (resid 1)",
"protein and (resid 4)",
order=2,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
assert_equal(third[list(third.keys())[0]], None)
# test third order
wb = WaterBridgeAnalysis(
universe_AWWA,
"protein and (resid 1)",
"protein and (resid 4)",
order=3,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
assert_equal(third[list(third.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_3water_accepter(self, universe_AWWWA, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form third order
water bridge with hydrogen bond acceptor from selection 2"""
wb = WaterBridgeAnalysis(
universe_AWWWA,
"protein and (resid 1)",
"protein and (resid 5)",
order=2,
distance_type=distance_type,
)
wb.run(verbose=False)
assert_equal(wb.results.network[0], defaultdict(dict))
wb = WaterBridgeAnalysis(
universe_AWWWA,
"protein and (resid 1)",
"protein and (resid 5)",
order=3,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
fourth = third[list(third.keys())[0]]
assert_equal(list(fourth.keys())[0][:4], (7, 6, 8, None))
assert_equal(fourth[list(fourth.keys())[0]], None)
wb = WaterBridgeAnalysis(
universe_AWWWA,
"protein and (resid 1)",
"protein and (resid 5)",
order=4,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
fourth = third[list(third.keys())[0]]
assert_equal(list(fourth.keys())[0][:4], (7, 6, 8, None))
assert_equal(fourth[list(fourth.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_4water_accepter(self, universe_AWWWWA, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form fourth order
water bridge with hydrogen bond acceptor from selection 2"""
wb = WaterBridgeAnalysis(
universe_AWWWWA,
"protein and (resid 1)",
"protein and (resid 6)",
order=3,
distance_type=distance_type,
)
wb.run(verbose=False)
assert_equal(wb.results.network[0], defaultdict(dict))
wb = WaterBridgeAnalysis(
universe_AWWWWA,
"protein and (resid 1)",
"protein and (resid 6)",
order=4,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
fourth = third[list(third.keys())[0]]
assert_equal(list(fourth.keys())[0][:4], (7, 6, 8, None))
fifth = fourth[list(fourth.keys())[0]]
assert_equal(list(fifth.keys())[0][:4], (9, 8, 10, None))
assert_equal(fifth[list(fifth.keys())[0]], None)
wb = WaterBridgeAnalysis(
universe_AWWWWA,
"protein and (resid 1)",
"protein and (resid 6)",
order=5,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(list(third.keys())[0][:4], (5, 4, 6, None))
fourth = third[list(third.keys())[0]]
assert_equal(list(fourth.keys())[0][:4], (7, 6, 8, None))
fifth = fourth[list(fourth.keys())[0]]
assert_equal(list(fifth.keys())[0][:4], (9, 8, 10, None))
assert_equal(fifth[list(fifth.keys())[0]], None)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_22water_accepter(self, universe_branch, distance_type):
"""Test case where the hydrogen bond acceptor from selection 1 form a second order
water bridge with hydrogen bond acceptor from selection 2
and the last water is linked to two residues in selection 2"""
wb = WaterBridgeAnalysis(
universe_branch,
"protein and (resid 1)",
"protein and (resid 4 or resid 5)",
order=2,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
third = second[list(second.keys())[0]]
assert_equal(
[(5, 4, 7, None), (6, 4, 8, None)],
sorted([key[:4] for key in list(third.keys())]),
)
def test_timeseries_wba(self, universe_branch):
"""Test if the time series data is correctly generated in water bridge analysis format"""
wb = WaterBridgeAnalysis(
universe_branch,
"protein and (resid 1)",
"protein and (resid 4 or resid 5)",
order=2,
)
wb.output_format = "sele1_sele2"
wb.run(verbose=False)
timeseries = sorted(wb.results.timeseries[0])
assert_equal(
timeseries[0][:4], (0, 2, ("ALA", 1, "O"), ("SOL", 2, "HW1"))
)
assert_equal(
timeseries[1][:4], (3, 4, ("SOL", 2, "HW2"), ("SOL", 3, "OW"))
)
assert_equal(
timeseries[2][:4], (5, 7, ("SOL", 3, "HW1"), ("ALA", 4, "O"))
)
assert_equal(
timeseries[3][:4], (6, 8, ("SOL", 3, "HW2"), ("ALA", 5, "O"))
)
def test_timeseries_hba(self, universe_branch):
"""Test if the time series data is correctly generated in hydrogen bond analysis format"""
wb = WaterBridgeAnalysis(
universe_branch,
"protein and (resid 1)",
"protein and (resid 4 or resid 5)",
order=2,
)
wb.output_format = "donor_acceptor"
wb.run(verbose=False)
timeseries = sorted(wb.results.timeseries[0])
assert_equal(
timeseries[0][:4], (2, 0, ("SOL", 2, "HW1"), ("ALA", 1, "O"))
)
assert_equal(
timeseries[1][:4], (3, 4, ("SOL", 2, "HW2"), ("SOL", 3, "OW"))
)
assert_equal(
timeseries[2][:4], (5, 7, ("SOL", 3, "HW1"), ("ALA", 4, "O"))
)
assert_equal(
timeseries[3][:4], (6, 8, ("SOL", 3, "HW2"), ("ALA", 5, "O"))
)
@pytest.mark.parametrize("distance_type", ["hydrogen", "heavy"])
def test_acceptor_12water_accepter(self, universe_AWA_AWWA, distance_type):
"""Test of independent first order and second can be recognised correctely"""
wb = WaterBridgeAnalysis(
universe_AWA_AWWA,
"protein and (resid 1 or resid 5)",
"protein and (resid 4 or resid 8)",
order=1,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(list(network.keys())[0][:4], (0, None, 2, 1))
second = network[list(network.keys())[0]]
assert_equal(list(second.keys())[0][:4], (3, 1, 4, None))
assert_equal(second[list(second.keys())[0]], None)
network = wb.results.network[0]
wb = WaterBridgeAnalysis(
universe_AWA_AWWA,
"protein and (resid 1 or resid 5)",
"protein and (resid 4 or resid 8)",
order=2,
distance_type=distance_type,
)
wb.run(verbose=False)
network = wb.results.network[0]
assert_equal(
[(0, None, 2, 1), (5, None, 7, 6)],
sorted([key[:4] for key in list(network.keys())]),
)
def test_count_by_type_single_link(self, universe_DWA):
"""
This test tests the simplest water bridge to see if count_by_type() works.
"""
wb = WaterBridgeAnalysis(
universe_DWA, "protein and (resid 1)", "protein and (resid 4)"
)
wb.run(verbose=False)
assert_equal(
wb.count_by_type(), [(1, 4, "ALA", 1, "H", "ALA", 4, "O", 1.0)]
)
def test_count_by_type_multiple_link(self, universe_AWA_AWWA):
"""
This test tests if count_by_type() can give the correct result for more than 1 links.
"""
wb = WaterBridgeAnalysis(
universe_AWA_AWWA,
"protein and (resid 1 or resid 5)",
"protein and (resid 4 or resid 8)",
order=2,
)
wb.run(verbose=False)
assert_equal(
sorted(wb.count_by_type()),
[
[0, 4, "ALA", 1, "O", "ALA", 4, "O", 1.0],
[5, 11, "ALA", 5, "O", "ALA", 8, "O", 1.0],
],
)
def test_count_by_type_multiple_frame(self, wb_multiframe):
"""
This test tests if count_by_type() works in multiply situations.
:return:
"""
result = [
[0, 11, "ALA", 1, "O", "ALA", 6, "H", 0.25],
[0, 12, "ALA", 1, "O", "ALA", 6, "O", 0.25],
[1, 12, "ALA", 1, "H", "ALA", 6, "O", 0.5],
]
assert_equal(sorted(wb_multiframe.count_by_type()), result)
def test_count_by_type_filter(self, wb_multiframe):
"""
This test tests if modifying analysis_func
allows some results to be filtered out in count_by_type().
:return:
"""
def analysis(current, output, u):
sele1_index, sele1_heavy_index, atom2, heavy_atom2, dist, angle = (
current[0]
)
atom1, heavy_atom1, sele2_index, sele2_heavy_index, dist, angle = (
current[-1]
)
sele1 = u.atoms[sele1_index]
sele2 = u.atoms[sele2_index]
(s1_resname, s1_resid, s1_name) = (
sele1.resname,
sele1.resid,
sele1.name,
)
(s2_resname, s2_resid, s2_name) = (
sele2.resname,
sele2.resid,
sele2.name,
)
key = (
sele1_index,
sele2_index,
s1_resname,
s1_resid,
s1_name,
s2_resname,
s2_resid,
s2_name,
)
if s2_name == "H":
output[key] += 1
result = [((0, 11, "ALA", 1, "O", "ALA", 6, "H"), 0.25)]
assert_equal(
sorted(wb_multiframe.count_by_type(analysis_func=analysis)), result
)
def test_count_by_type_merge(self, wb_multiframe):
"""
This test tests if modifying analysis_func
allows some same residue to be merged in count_by_type().
"""
def analysis(current, output, u):
sele1_index, sele1_heavy_index, atom2, heavy_atom2, dist, angle = (
current[0]
)
atom1, heavy_atom1, sele2_index, sele2_heavy_index, dist, angle = (
current[-1]
)
sele1 = u.atoms[sele1_index]
sele2 = u.atoms[sele2_index]
(s1_resname, s1_resid, s1_name) = (
sele1.resname,
sele1.resid,
sele1.name,
)
(s2_resname, s2_resid, s2_name) = (
sele2.resname,
sele2.resid,
sele2.name,
)
key = (s1_resname, s1_resid, s2_resname, s2_resid)
output[key] = 1
result = [(("ALA", 1, "ALA", 6), 1.0)]
assert_equal(
sorted(wb_multiframe.count_by_type(analysis_func=analysis)), result
)
def test_count_by_type_order(self, wb_multiframe):
"""
This test tests if modifying analysis_func
allows the order of water bridge to be separated in count_by_type().
:return:
"""
def analysis(current, output, u):
sele1_index, sele1_heavy_index, atom2, heavy_atom2, dist, angle = (
current[0]
)
atom1, heavy_atom1, sele2_index, sele2_heavy_index, dist, angle = (
current[-1]
)
sele1 = u.atoms[sele1_index]
sele2 = u.atoms[sele2_index]
(s1_resname, s1_resid, s1_name) = (
sele1.resname,
sele1.resid,
sele1.name,
)
(s2_resname, s2_resid, s2_name) = (
sele2.resname,
sele2.resid,
sele2.name,
)
key = (
s1_resname,
s1_resid,
s2_resname,
s2_resid,
len(current) - 1,
)
output[key] = 1
result = [
(("ALA", 1, "ALA", 6, 0), 0.5),
(("ALA", 1, "ALA", 6, 1), 0.25),
(("ALA", 1, "ALA", 6, 2), 0.25),
]
assert_equal(
sorted(wb_multiframe.count_by_type(analysis_func=analysis)), result
)
def test_count_by_time(self, wb_multiframe):
"""
This test tests if count_by_times() works.
:return:
"""
assert_equal(
wb_multiframe.count_by_time(), [(0, 1), (1, 1), (2, 1), (3, 1)]
)
def test_count_by_time_weight(self, universe_AWA_AWWA):
"""
This test tests if modyfing the analysis_func allows the weight to be changed
in count_by_type().
:return:
"""
wb = WaterBridgeAnalysis(
universe_AWA_AWWA,
"protein and (resid 1 or resid 5)",
"protein and (resid 4 or resid 8)",
order=2,
)
wb.run(verbose=False)
def analysis(current, output, u):
sele1_index, sele1_heavy_index, atom2, heavy_atom2, dist, angle = (
current[0]
)
atom1, heavy_atom1, sele2_index, sele2_heavy_index, dist, angle = (
current[-1]
)
sele1 = u.atoms[sele1_index]
sele2 = u.atoms[sele2_index]
(s1_resname, s1_resid, s1_name) = (
sele1.resname,
sele1.resid,
sele1.name,
)
(s2_resname, s2_resid, s2_name) = (
sele2.resname,
sele2.resid,
sele2.name,
)
key = (s1_resname, s1_resid, s2_resname, s2_resid)
output[key] += len(current) - 1
assert_equal(
wb.count_by_time(analysis_func=analysis),
[
(0, 3),
],
)
def test_count_by_time_empty(self, universe_AWA_AWWA):
"""
See if count_by_time() can handle zero well.
:return:
"""
wb = WaterBridgeAnalysis(
universe_AWA_AWWA,
"protein and (resid 1 or resid 5)",
"protein and (resid 4 or resid 8)",
order=2,
)
wb.run(verbose=False)
def analysis(current, output, u):
pass
assert_equal(
wb.count_by_time(analysis_func=analysis),
[
(0, 0),
],
)
def test_generate_table_hba(self, wb_multiframe):
"""Test generate table using hydrogen bond analysis format"""
table = wb_multiframe.generate_table(output_format="donor_acceptor")
assert_array_equal(
sorted(table.donor_resid),
[1, 1, 2, 2, 2, 6, 6],
)
def test_generate_table_s1s2(self, wb_multiframe):
"""Test generate table using hydrogen bond analysis format"""
table = wb_multiframe.generate_table(output_format="sele1_sele2")
assert_array_equal(
sorted(table.sele1_resid),
[1, 1, 1, 1, 2, 2, 3],
)
def test_timesteps_by_type(self, wb_multiframe):
"""Test the timesteps_by_type function"""
timesteps = sorted(wb_multiframe.timesteps_by_type())
assert_array_equal(
timesteps[3], [1, 12, "ALA", 1, "H", "ALA", 6, "O", 0, 2]
)
def test_duplicate_water(self):
"""A case #3119 where
Acceptor···H−O···H-Donor
|
H···O-H
will be recognised as 3rd order water bridge.
"""
grofile = """Test gro file
7
1LEU O 1 1.876 0.810 1.354
117SOL HW1 2 1.853 0.831 1.162
117SOL OW 3 1.877 0.890 1.081
117SOL HW2 4 1.908 0.828 1.007
135SOL OW 5 1.924 0.713 0.845
1LEU H 6 1.997 0.991 1.194
1LEU N 7 2.041 1.030 1.274
2.22092 2.22092 2.22092"""
u = MDAnalysis.Universe(StringIO(grofile), format="gro")
wb = WaterBridgeAnalysis(
u, "resname LEU and name O", "resname LEU and name N H", order=4
)
wb.run()
assert len(wb.results.timeseries[0]) == 2
def test_warn_results_deprecated(self, universe_DA):
wb = WaterBridgeAnalysis(
universe_DA,
"protein and (resid 9)",
"protein and (resid 10)",
order=0,
)
wb.run()
wmsg = "The `network` attribute was deprecated in MDAnalysis 2.0.0"
with pytest.warns(DeprecationWarning, match=wmsg):
assert_equal(wb.network, wb.results.network)
wmsg = "The `timeseries` attribute was deprecated in MDAnalysis 2.0.0"
with pytest.warns(DeprecationWarning, match=wmsg):
assert_equal(wb.timeseries, wb.results.timeseries)
|