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
|
##############################################################################
# MDTraj: A Python Library for Loading, Saving, and Manipulating
# Molecular Dynamics Trajectories.
# Copyright 2012-2017 Stanford University and the Authors
#
# Authors: Robert McGibbon
# Contributors: Kyle A Beauchamp, Matthew Harrigan
#
# MDTraj is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 2.1
# of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with MDTraj. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
import functools
import sys
from collections import namedtuple
from pathlib import Path
import platform
import numpy as np
import pytest
import mdtraj as md
import mdtraj.core.trajectory
import mdtraj.formats
import mdtraj.utils
from mdtraj.core import element
from mdtraj.testing import eq
on_win = sys.platform == "win32"
on_win = sys.platform == "win32"
on_py3 = sys.version_info >= (3, 0)
on_32bit = (platform.architecture()[0] == '32bit' )
TrajObj = namedtuple("TrajObj", ["fobj", "fext", "fn"])
file_objs = [
(md.formats.NetCDFTrajectoryFile, "nc"),
(md.formats.HDF5TrajectoryFile, "h5"),
(md.formats.XTCTrajectoryFile, "xtc"),
(md.formats.TRRTrajectoryFile, "trr"),
(md.formats.DCDTrajectoryFile, "dcd"),
(md.formats.MDCRDTrajectoryFile, "mdcrd"),
(md.formats.DTRTrajectoryFile, "dtr"),
(md.formats.XYZTrajectoryFile, "xyz"),
(md.formats.XYZTrajectoryFile, "xyz.gz"),
(md.formats.LAMMPSTrajectoryFile, "lammpstrj"),
(md.formats.LH5TrajectoryFile, "lh5"),
(md.formats.PDBTrajectoryFile, "pdb"),
(md.formats.PDBTrajectoryFile, "pdb.gz"),
(md.formats.AmberNetCDFRestartFile, "ncrst"),
(md.formats.AmberRestartFile, "rst7"),
(md.formats.GroTrajectoryFile, "gro"),
]
@pytest.fixture(params=file_objs, ids=lambda x: x[1])
def ref_traj(request, monkeypatch):
fobj, fext = request.param
if on_win and on_py3:
if fext == "lh5":
pytest.skip("lh5 is not supported on Windows + Py3")
if fext in ["ncrst", "rst7"]:
pytest.xfail("These reference files don't exist yet")
return TrajObj(fobj, fext, f"frame0.{fext}")
@pytest.fixture(params=file_objs, ids=lambda x: x[1])
def write_traj(request, tmpdir):
fobj, fext = request.param
if on_win and on_py3:
if fext == "lh5":
pytest.skip("lh5 is not supported on Windows + Py3")
return TrajObj(fobj, fext, f"{tmpdir}/traj.{fext}")
@pytest.fixture
def write_traj_with_box(write_traj):
if write_traj.fext in ["xyz", "xyz.gz", "pdb", "pdb.gz", "lh5"]:
pytest.skip(f"{write_traj.fext} does not store box information")
else:
return write_traj
def test_mismatch(get_fn):
# loading a 22 atoms xtc with a topology that has 2,000 atoms
# some kind of error should happen
with pytest.raises(ValueError):
md.load(get_fn("frame0.xtc"), top=get_fn("4ZUO.pdb"))
def test_box(get_fn):
t = md.load(get_fn("native.pdb"))
assert eq(t.unitcell_vectors, None)
assert eq(t.unitcell_lengths, None)
assert eq(t.unitcell_angles, None)
assert eq(t.unitcell_volumes, None)
t.unitcell_vectors = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).reshape(1, 3, 3)
assert eq(np.array([1.0, 1.0, 1.0]), t.unitcell_lengths[0])
assert eq(np.array([90.0, 90.0, 90.0]), t.unitcell_angles[0])
assert eq(np.array([1.0]), t.unitcell_volumes)
def test_load_pdb_box(get_fn):
t = md.load(get_fn("native2.pdb"), no_boxchk=True)
assert eq(t.unitcell_lengths[0], np.array([0.1, 0.2, 0.3]))
assert eq(t.unitcell_angles[0], np.array([90.0, 90.0, 90.0]))
assert eq(t.unitcell_vectors[0], np.array([[0.1, 0, 0], [0, 0.2, 0], [0, 0, 0.3]]))
def test_load_pdb_gz(get_fn):
t = md.load(get_fn("1ncw.pdb.gz"))
assert eq(t.n_atoms, 3990)
def test_box_load_save(write_traj_with_box, get_fn):
t = md.load(get_fn("native2.pdb"), no_boxchk=True)
top = md.load_topology(get_fn("native.pdb"), no_boxchk=True)
# make sure than through a load/save
# cycle, the box information is preserved:
t.save(write_traj_with_box.fn)
t2 = md.load(write_traj_with_box.fn, top=top)
assert t.unitcell_vectors is not None
assert eq(t.xyz, t2.xyz, decimal=3)
assert eq(t.unitcell_vectors, t2.unitcell_vectors)
assert eq(t.unitcell_angles, t2.unitcell_angles)
assert eq(t.unitcell_lengths, t2.unitcell_lengths)
def test_slice(get_fn):
t = md.load(get_fn("traj.h5"))
# with copying
assert eq((t[0:5] + t[5:10]).xyz, t[0:10].xyz)
assert eq((t[0:5] + t[5:10]).time, t[0:10].time)
assert eq((t[0:5] + t[5:10]).unitcell_vectors, t[0:10].unitcell_vectors)
assert eq((t[0:5] + t[5:10]).unitcell_lengths, t[0:10].unitcell_lengths)
assert eq((t[0:5] + t[5:10]).unitcell_angles, t[0:10].unitcell_angles)
# without copying (in place)
assert eq(
(t.slice(key=range(5), copy=False) + t.slice(key=range(5, 10), copy=False)).xyz,
t.slice(key=range(10), copy=False).xyz,
)
assert eq(
(t.slice(key=range(5), copy=False) + t.slice(key=range(5, 10), copy=False)).time,
t.slice(key=range(10), copy=False).time,
)
assert eq(
(t.slice(key=range(5), copy=False) + t.slice(key=range(5, 10), copy=False)).unitcell_vectors,
t.slice(key=range(10), copy=False).unitcell_vectors,
)
assert eq(
(t.slice(key=range(5), copy=False) + t.slice(key=range(5, 10), copy=False)).unitcell_lengths,
t.slice(key=range(10), copy=False).unitcell_lengths,
)
assert eq(
(t.slice(key=range(5), copy=False) + t.slice(key=range(5, 10), copy=False)).unitcell_angles,
t.slice(key=range(10), copy=False).unitcell_angles,
)
def test_slice2(get_fn):
t = md.load(get_fn("traj.h5"))
# with copying
assert t[0] == t[[0, 1]][0]
# without copying (in place)
assert t.slice(key=0, copy=False) == t.slice(key=[0, 1], copy=True)[0]
def has_time_info(fext):
# Some formats don't save time information
return fext not in [
"dcd",
"pdb",
"pdb.gz",
"xyz",
"xyz.gz",
"lammpstrj",
"lh5",
"mdcrd",
]
def precision(fext):
if fext in ["xyz", "lammpstrj", "lh5"]:
return 3
else:
return 6
def precision2(fext1, fext2):
return min(precision(fext1), precision(fext2))
def test_read_path(ref_traj, get_fn, monkeypatch):
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
md.load(Path(get_fn(ref_traj.fn)), top=get_fn("native.pdb"))
md.load(Path(get_fn(ref_traj.fn)), top=get_fn("native.pdb"))
def test_write_path(write_traj, get_fn, monkeypatch):
if write_traj.fext in ("ncrst", "rst7"):
pytest.skip(f"{write_traj.fext} can only store 1 frame per file")
if write_traj.fext in ("mdcrd"):
pytest.skip(f"{write_traj.fext} can only store rectilinear boxes")
def test_base(write_traj, get_fn):
t = md.load(get_fn("traj.h5"))
if t.unitcell_vectors is None:
if write_traj.fext in ("dtr", "lammpstrj"):
pytest.skip(f"{write_traj.fext} needs to write unitcells")
t.save(Path(write_traj.fn))
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_read_write(ref_traj, write_traj, get_fn, monkeypatch):
if write_traj.fext in ("ncrst", "rst7"):
pytest.skip(f"{write_traj.fext} can only store 1 frame per file")
if write_traj.fext in ("mdcrd"):
pytest.skip(f"{write_traj.fext} can only store rectilinear boxes")
def test_base(ref_traj, write_traj, get_fn):
top = get_fn("native.pdb")
t = md.load(get_fn(ref_traj.fn), top=top)
if t.unitcell_vectors is None:
if write_traj.fext in ("dtr", "lammpstrj"):
pytest.skip(f"{write_traj.fext} needs to write unitcells")
t.save(write_traj.fn)
t2 = md.load(write_traj.fn, top=top)
eq(t.xyz, t2.xyz, decimal=precision2(ref_traj.fext, write_traj.fext))
if has_time_info(write_traj.fext):
eq(t.time, t2.time, decimal=3)
if write_traj.fext in ("nc") or ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, write_traj, get_fn)
test_base(ref_traj, write_traj, get_fn)
def test_load(ref_traj, get_fn, monkeypatch):
def test_base(ref_traj, get_fn):
nat = md.load(get_fn("native.pdb"))
num_block = 3
t0 = md.load(get_fn(ref_traj.fn), top=nat, discard_overlapping_frames=True)
t1 = md.load(get_fn(ref_traj.fn), top=nat, discard_overlapping_frames=False)
t2 = md.load(
[get_fn(ref_traj.fn) for _ in range(num_block)],
top=nat,
discard_overlapping_frames=False,
)
t3 = md.load(
[get_fn(ref_traj.fn) for _ in range(num_block)],
top=nat,
discard_overlapping_frames=True,
)
# these don't actually overlap, so discard_overlapping_frames should
# have no effect. the overlap is between the last frame of one and the
# first frame of the next.
assert eq(t0.n_frames, t1.n_frames)
assert eq(t0.n_frames * num_block, t2.n_frames)
assert eq(t3.n_frames, t2.n_frames)
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_hdf5(get_fn):
t = md.load(get_fn("traj.h5"))
t2 = md.load(get_fn("native.pdb"))
t3 = md.load(get_fn("traj.h5"), frame=8)
assert t.topology == t2.topology
assert eq(t.time, 0.002 * (1 + np.arange(100)))
assert eq(t.time, 0.002 * (1 + np.arange(100)))
assert eq(t[8].xyz, t3.xyz)
assert eq(t[8].time, t3.time)
assert eq(t[8].unitcell_vectors, t3.unitcell_vectors)
def test_center(get_fn):
traj = md.load(get_fn("traj.h5"))
traj.center_coordinates()
mu = traj.xyz.mean(1)
mu0 = np.zeros(mu.shape)
eq(mu0, mu)
for a in traj.top.atoms:
# Set all masses equal so we can compare against unweighted result
a.element = element.hydrogen
traj.center_coordinates(mass_weighted=True)
mu2 = traj.xyz.mean(1)
eq(mu0, mu2)
def test_center_aind(get_fn):
traj = md.load(get_fn("traj.h5"))
traj.restrict_atoms(np.arange(0, traj.n_atoms, 2))
traj.center_coordinates()
mu = traj.xyz.mean(1)
mu0 = np.zeros(mu.shape)
eq(mu0, mu)
for a in traj.top.atoms:
# Set all masses equal so we can compare against unweighted result
a.element = element.hydrogen
traj.center_coordinates(mass_weighted=True)
mu2 = traj.xyz.mean(1)
eq(mu0, mu2)
@pytest.mark.filterwarnings("ignore:atom_indices are not monotonically")
def test_float_atom_indices_exception(ref_traj, get_fn, monkeypatch):
def test_base(ref_traj, get_fn):
# Is an informative error message given when you supply floats for atom_indices?
top = md.load(get_fn("native.pdb")).topology
try:
md.load(get_fn(ref_traj.fn), atom_indices=[0.5, 1.3], top=top)
except ValueError as e:
assert e.args[0] == "indices must be of an integer type. float64 is not an integer type"
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_restrict_atoms(get_fn):
traj = md.load(get_fn("traj.h5"))
time_address = traj.time.ctypes.data
desired_atom_indices = [0, 1, 2, 5]
traj.restrict_atoms(desired_atom_indices)
atom_indices = [a.index for a in traj.top.atoms]
eq([0, 1, 2, 3], atom_indices)
eq(traj.xyz.shape[1], 4)
eq(traj.n_atoms, 4)
eq(traj.n_residues, 1)
eq(len(traj.top._bonds), 2)
eq(traj.n_residues, traj.topology._numResidues)
eq(traj.n_atoms, traj.topology._numAtoms)
eq(np.array([a.index for a in traj.topology.atoms]), np.arange(traj.n_atoms))
# assert that the time field was not copied
assert traj.time.ctypes.data == time_address
def test_restrict_atoms_not_inplace(get_fn):
traj = md.load(get_fn("traj.h5"))
traj_backup = md.load(get_fn("traj.h5"))
desired_atom_indices = [0, 1, 2, 5]
sliced = traj.restrict_atoms(desired_atom_indices, inplace=False)
# make sure the original one was not modified
eq(traj.xyz, traj_backup.xyz)
eq(traj.topology, traj_backup.topology)
eq(list(range(4)), [a.index for a in sliced.top.atoms])
eq(sliced.xyz.shape[1], 4)
eq(sliced.n_atoms, 4)
eq(sliced.n_residues, 1)
eq(len(sliced.top._bonds), 2)
eq(sliced.n_residues, sliced.topology._numResidues)
eq(sliced.n_atoms, sliced.topology._numAtoms)
eq(np.array([a.index for a in sliced.topology.atoms]), np.arange(sliced.n_atoms))
# make sure the two don't alias the same memory
assert traj.time.ctypes.data != sliced.time.ctypes.data
assert traj.unitcell_angles.ctypes.data != sliced.unitcell_angles.ctypes.data
assert traj.unitcell_lengths.ctypes.data != sliced.unitcell_lengths.ctypes.data
def test_array_vs_matrix(get_fn):
top = md.load(get_fn("native.pdb")).topology
xyz = np.random.randn(1, 22, 3)
xyz_mat = np.asarray(xyz)
t1 = md.Trajectory(xyz, top)
t2 = md.Trajectory(xyz_mat, top)
eq(t1.xyz, xyz)
eq(t2.xyz, xyz)
def test_pdb_unitcell_loadsave(tmpdir, get_fn):
# Make sure that nonstandard unitcell dimensions are saved and loaded
# correctly with PDB
tref = md.load(get_fn("native.pdb"))
tref.unitcell_lengths = 1 + 0.1 * np.random.randn(tref.n_frames, 3)
tref.unitcell_angles = 90 + 0.0 * np.random.randn(tref.n_frames, 3)
fn = f"{tmpdir}/x.pdb"
tref.save(fn)
tnew = md.load(fn)
eq(tref.unitcell_vectors, tnew.unitcell_vectors, decimal=3)
def test_load_combination(ref_traj, get_fn, monkeypatch):
# Test that the load function's stride and atom_indices work across
# all trajectory formats
def test_base(ref_traj, get_fn):
topology = md.load(get_fn("native.pdb")).topology
ainds = np.array([a.index for a in topology.atoms if a.element.symbol == "C"])
no_kwargs = md.load(get_fn(ref_traj.fn), top=topology)
strided3 = md.load(get_fn(ref_traj.fn), top=topology, stride=3)
subset = md.load(get_fn(ref_traj.fn), top=topology, atom_indices=ainds)
# test 1
t1 = no_kwargs
t2 = strided3
assert eq(t1.xyz[::3], t2.xyz)
assert eq(t1.time[::3], t2.time)
if t1.unitcell_vectors is not None:
assert eq(t1.unitcell_vectors[::3], t2.unitcell_vectors)
assert eq(t1.topology, t2.topology)
# test 2
t1 = no_kwargs
t2 = subset
assert eq(t1.xyz[:, ainds, :], t2.xyz)
assert eq(t1.time, t2.time)
if t1.unitcell_vectors is not None:
assert eq(t1.unitcell_vectors, t2.unitcell_vectors)
assert eq(t1.topology.subset(ainds), t2.topology)
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_no_topology():
# We can make trajectories without a topology
md.Trajectory(xyz=np.random.randn(10, 5, 3), topology=None)
def test_join():
xyz = np.random.rand(10, 5, 3)
# overlapping frames
t1 = md.Trajectory(xyz=xyz[:5], topology=None)
t2 = md.Trajectory(xyz=xyz[4:], topology=None)
t3 = t1.join(t2, discard_overlapping_frames=True)
t4 = t1.join(t2, discard_overlapping_frames=False)
eq(t3.xyz, xyz)
eq(len(t4.xyz), 11)
eq(t4.xyz, np.vstack((xyz[:5], xyz[4:])))
def test_md_join(get_fn):
fn = get_fn("traj.h5")
t_ref = md.load(get_fn("frame0.h5"))[:20]
loaded = md.load(fn, top=t_ref, stride=2)
iterloaded = md.join(md.iterload(fn, top=t_ref, stride=2, chunk=6))
eq(loaded.xyz, iterloaded.xyz)
eq(loaded.time, iterloaded.time)
eq(loaded.unitcell_angles, iterloaded.unitcell_angles)
eq(loaded.unitcell_lengths, iterloaded.unitcell_lengths)
def test_stack_1(get_fn):
t1 = md.load(get_fn("native.pdb"))
t2 = t1.stack(t1)
eq(t2.n_atoms, 2 * t1.n_atoms)
eq(t2.topology._numAtoms, 2 * t1.n_atoms)
eq(t1.xyz, t2.xyz[:, 0 : t1.n_atoms])
eq(t1.xyz, t2.xyz[:, t1.n_atoms :])
def test_stack_2():
t1 = md.Trajectory(xyz=np.random.rand(10, 5, 3), topology=None)
t2 = md.Trajectory(xyz=np.random.rand(10, 6, 3), topology=None)
t3 = t1.stack(t2)
eq(t3.xyz[:, :5], t1.xyz)
eq(t3.xyz[:, 5:], t2.xyz)
eq(t3.n_atoms, 11)
def test_seek_read_mode(ref_traj, get_fn, monkeypatch):
# Test the seek/tell capacity of the different TrajectoryFile objects in
# read mode. Basically, we just seek around the files and read different
# segments, keeping track of our location manually and checking with both
# tell() and by checking that the right coordinates are actually returned
fobj = ref_traj.fobj
fn = ref_traj.fn
if ref_traj.fobj is md.formats.PDBTrajectoryFile:
pytest.xfail("PDB Files don't support seeking")
if ref_traj.fext == "xyz.gz":
pytest.xfail("This is broken")
if ref_traj.fext == "gro":
pytest.xfail("This is broken")
def test_base(ref_traj, get_fn):
point = 0
xyz = md.load(get_fn(fn), top=get_fn("native.pdb")).xyz
length = len(xyz)
kwargs = {}
if fobj is md.formats.MDCRDTrajectoryFile:
kwargs = {"n_atoms": 22}
with fobj(get_fn(fn), **kwargs) as f:
for i in range(100):
r = np.random.rand()
if r < 0.25:
offset = np.random.randint(-5, 5)
if 0 < point + offset < length:
point += offset
f.seek(offset, 1)
else:
f.seek(0)
point = 0
if r < 0.5:
offset = np.random.randint(1, 10)
if point + offset < length:
read = f.read(offset)
if fobj not in [
md.formats.LH5TrajectoryFile,
md.formats.XYZTrajectoryFile,
]:
read = read[0]
readlength = len(read)
read = mdtraj.utils.in_units_of(read, f.distance_unit, "nanometers")
eq(xyz[point : point + offset], read)
point += readlength
elif r < 0.75:
offset = np.random.randint(low=-100, high=0)
try:
f.seek(offset, 2)
point = length + offset
except NotImplementedError:
# not all of the *TrajectoryFiles currently support
# seeking from the end, so we'll let this pass if they
# say that they dont implement this.
pass
else:
offset = np.random.randint(100)
f.seek(offset, 0)
point = offset
eq(f.tell(), point)
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_load_frame(ref_traj, get_fn, monkeypatch):
if ref_traj.fobj is md.formats.GroTrajectoryFile:
pytest.xfail("Gro doesn't implement seek")
def test_base(ref_traj, get_fn):
trajectory = md.load(get_fn(ref_traj.fn), top=get_fn("native.pdb"))
rand = np.random.randint(len(trajectory))
frame = md.load_frame(get_fn(ref_traj.fn), index=rand, top=get_fn("native.pdb"))
if ref_traj.fobj is md.formats.DTRTrajectoryFile:
pytest.xfail("DTR doesn't load a single frame properly")
eq(trajectory[rand].xyz, frame.xyz)
eq(trajectory[rand].unitcell_vectors, frame.unitcell_vectors)
if has_time_info(ref_traj.fext):
eq(trajectory[rand].time, frame.time)
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_load_frame_2eqq(get_fn):
t1 = md.load(get_fn("2EQQ.pdb"))
r = np.random.randint(len(t1))
t2 = md.load_frame(get_fn("2EQQ.pdb"), r)
eq(t1[r].xyz, t2.xyz)
def test_iterload(write_traj, get_fn, monkeypatch):
if write_traj.fext == "dtr":
pytest.xfail("This is broken with dtr")
def test_base(write_traj, get_fn):
t_ref = md.load(get_fn("frame0.h5"))[:20]
if write_traj.fext in ("ncrst", "rst7"):
pytest.skip("Only 1 frame per file format")
t_ref.save(write_traj.fn)
for stride in [1, 2, 3]:
loaded = md.load(write_traj.fn, top=t_ref, stride=stride)
iterloaded = functools.reduce(
lambda a, b: a.join(b),
md.iterload(write_traj.fn, top=t_ref, stride=stride, chunk=6),
)
eq(loaded.xyz, iterloaded.xyz)
eq(loaded.time, iterloaded.time)
eq(loaded.unitcell_angles, iterloaded.unitcell_angles)
eq(loaded.unitcell_lengths, iterloaded.unitcell_lengths)
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_iterload_skip(ref_traj, get_fn, monkeypatch):
if ref_traj.fobj is md.formats.PDBTrajectoryFile:
pytest.xfail("PDB Iterloads an extra frame!!")
if ref_traj.fobj is md.formats.GroTrajectoryFile:
pytest.xfail("Not implemented for some reason")
if ref_traj.fext in ("ncrst", "rst7"):
pytest.skip("Only 1 frame per file format")
def test_base(ref_traj, get_fn):
top = md.load(get_fn("native.pdb"))
t_ref = md.load(get_fn(ref_traj.fn), top=top)
for cs in [0, 1, 11, 100]:
for skip in [0, 1, 20, 101]:
t = functools.reduce(
lambda a, b: a.join(b),
md.iterload(get_fn(ref_traj.fn), skip=skip, top=top, chunk=cs),
)
eq(t_ref.xyz[skip:], t.xyz)
eq(t_ref.time[skip:], t.time)
eq(t_ref.topology, t.topology)
if ref_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(ref_traj, get_fn)
test_base(ref_traj, get_fn)
def test_iterload_chunk_dcd(get_fn):
# Makes sure that the actual chunk size yielded by iterload corresponds to the number of
# frames specified when calling it (for dcd files).
file = get_fn("alanine-dipeptide-explicit.dcd")
top = get_fn("alanine-dipeptide-explicit.pdb")
skip_frames = 3
frames_chunk = 2
full = md.load(file, top=top, stride=skip_frames)
chunks = []
for traj_chunk in md.iterload(
file,
top=top,
stride=skip_frames,
chunk=frames_chunk,
):
chunks.append(traj_chunk)
joined = md.join(chunks)
assert len(full) == len(joined)
assert eq(full.xyz, joined.xyz)
def test_save_load(write_traj, get_fn, monkeypatch):
# this cycles all the known formats you can save to, and then tries
# to reload, using just a single-frame file.
def test_base(write_traj, get_fn):
t_ref = md.load(get_fn("native.pdb"))
t_ref.unitcell_vectors = np.array([[[1, 0, 0], [0, 1, 0], [0, 0, 1]]])
t_ref.save(write_traj.fn)
t = md.load(write_traj.fn, top=t_ref.topology)
eq(t.xyz, t_ref.xyz)
eq(t.time, t_ref.time)
if t._have_unitcell:
eq(t.unitcell_angles, t_ref.unitcell_angles)
eq(t.unitcell_lengths, t_ref.unitcell_lengths)
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_force_overwrite(write_traj, get_fn, monkeypatch):
if write_traj.fext == "dtr":
pytest.xfail("This is broken with dtr")
def test_base(write_traj, get_fn):
t_ref = md.load(get_fn("native2.pdb"), no_boxchk=True)
open(write_traj.fn, "w").close()
t_ref.save(write_traj.fn, force_overwrite=True)
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_force_noverwrite(write_traj, get_fn, monkeypatch):
def test_base(write_traj, get_fn):
t_ref = md.load(get_fn("native2.pdb"), no_boxchk=True)
open(write_traj.fn, "w").close()
with pytest.raises(IOError):
t_ref.save(write_traj.fn, force_overwrite=False)
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_open_and_load(get_fn):
# These aren't tested in test_length because they don't support length!
files = ["frame0.mdcrd", "4waters.arc", "frame0.lammpstrj"]
for file in files:
if file.endswith(".mdcrd"):
md.open(get_fn(file), n_atoms=22)
else:
md.open(get_fn(file))
if file.endswith(".arc"):
md.load(get_fn(file), top=get_fn("4waters.pdb"))
else:
md.load(get_fn(file), top=get_fn("frame0.pdb"))
def test_length(get_fn):
files = [
"frame0.nc",
"frame0.h5",
"frame0.xtc",
"frame0.trr",
"frame0.dcd",
"2EQQ.pdb",
"frame0.xyz",
]
if not on_win:
files.append("frame0.lh5")
for file in files:
opened = md.open(get_fn(file))
if "." + file.rsplit(".", 1)[-1] in mdtraj.core.trajectory._TOPOLOGY_EXTS:
top = file
else:
top = "native.pdb"
loaded = md.load(get_fn(file), top=get_fn(top))
assert len(opened) == len(loaded)
def test_unitcell(write_traj, get_fn, monkeypatch):
# make sure that bogus unitcell vecotrs are not saved
if write_traj.fext in ["rst7", "ncrst", "lammpstrj", "dtr"]:
pytest.xfail(f"{write_traj.fext} seems to need unit vectors")
def test_base(write_traj, get_fn):
top = md.load(get_fn("native.pdb")).restrict_atoms(range(5)).topology
t = md.Trajectory(xyz=np.random.randn(100, 5, 3), topology=top)
t.save(write_traj.fn)
assert eq(md.load(write_traj.fn, top=top).unitcell_vectors, None)
if write_traj.fext in ("nc"):
# Running with scipy
with monkeypatch.context() as m:
m.setitem(sys.modules, "netCDF4", None)
test_base(write_traj, get_fn)
test_base(write_traj, get_fn)
def test_chunk0_iterload(get_fn):
filename = "frame0.h5"
trj0 = md.load(get_fn(filename))
for trj in md.iterload(get_fn(filename), chunk=0):
pass
eq(trj0.n_frames, trj.n_frames)
def test_hashing(get_fn):
frames = [
frame
for frame in md.iterload(
get_fn("frame0.xtc"),
chunk=1,
top=get_fn("native.pdb"),
)
]
hashes = [hash(frame) for frame in frames]
# check all frames have a unique hash value
assert len(hashes) == len(set(hashes))
# change topology and ensure hash changes too
top = frames[0].topology
top.add_bond(top.atom(0), top.atom(1))
last_frame_hash = hash(frames[0])
assert last_frame_hash != hashes[-1]
# test that trajectories without unitcell data can be hashed
t1 = md.load(get_fn("1bpi.pdb"))
t2 = md.load(get_fn("1bpi.pdb"))
assert hash(t1) == hash(t2)
def test_smooth(get_fn):
from scipy.signal import butter, filtfilt, lfilter, lfilter_zi
pad = 5
order = 3
b, a = butter(order, 2.0 / pad)
zi = lfilter_zi(b, a)
signal = np.sin(np.arange(100))
padded = np.r_[signal[pad - 1 : 0 : -1], signal, signal[-1:-pad:-1]]
z, _ = lfilter(b, a, padded, zi=zi * padded[0])
z2, _ = lfilter(b, a, z, zi=zi * z[0])
output = filtfilt(b, a, padded)
test = np.loadtxt(get_fn("smooth.txt"))
eq(output, test)
def test_make_whole(get_fn):
t1 = md.load(get_fn("water1_pbc.pdb"))
t2 = md.load(get_fn("water2_pbc.pdb"))
t1.make_molecules_whole(inplace=True)
t2.make_molecules_whole(inplace=True)
# Test that bond lengths are preserved
bonds1 = [(a1.index, a2.index) for a1, a2 in t1.topology._bonds]
bond_lenghts1 = [np.linalg.norm(t1.xyz[0, a1] - t1.xyz[0, a2]) for a1, a2 in bonds1]
bonds2 = [(a1.index, a2.index) for a1, a2 in t2.topology._bonds]
bond_lenghts2 = [np.linalg.norm(t2.xyz[0, a1] - t2.xyz[0, a2]) for a1, a2 in bonds2]
assert np.allclose(bond_lenghts1, bond_lenghts2)
@pytest.mark.skip(reason="Broken, maybe only on Python 3.11")
@pytest.mark.skipif(on_32bit, reason="image_molecule segfaults on 32-bit systems, see mdtraj Issue #1591")
def test_image_molecules(get_fn):
# Load trajectory with periodic box
t = md.load(
get_fn("alanine-dipeptide-explicit.dcd"),
top=get_fn("alanine-dipeptide-explicit.pdb"),
)
# Image to new trajectory
t_new = t.image_molecules(inplace=False)
# Test that t_new and t are not the same object (issue #1769)
assert t_new.xyz is not t.xyz
# Image inplace without making molecules whole
t.image_molecules(inplace=True, make_whole=False)
# Image inplace with making molecules whole
t.image_molecules(inplace=True, make_whole=True)
# Test coordinates in t are not corrupted to NaNs (issue #1813)
assert np.any(np.isnan(t.xyz)) is False
# Image with specified anchor molecules
molecules = t.topology.find_molecules()
anchor_molecules = molecules[0:3]
t.image_molecules(inplace=True, anchor_molecules=anchor_molecules)
def test_load_pdb_no_standard_names(get_fn):
# Minimal test. Standard_names=False will force load_pdb.py
# to NOT replace any non-standard atom or residue names in the topology
md.load(get_fn("native2.pdb"), standard_names=False, no_boxchk=True)
md.load_pdb(get_fn("native2.pdb"), standard_names=False, no_boxchk=True)
def test_load_with_atom_indices(get_fn):
t1 = md.load(get_fn("frame0.xtc"), top=get_fn("frame0.gro"), atom_indices=[5])
t2 = md.load(get_fn("frame0.xtc"), top=get_fn("frame0.gro"))
t2 = t2.atom_slice([5])
eq(t1.xyz, t2.xyz)
eq(t1.time, t2.time)
@pytest.mark.filterwarnings("ignore:atom_indices are not monotonically")
def test_atom_slicing(get_fn):
t = md.load(get_fn("frame0.xtc"), top=get_fn("frame0.gro"))
t1 = t.atom_slice([0, 1])
t2 = t.atom_slice([1, 0])
eq(t1.xyz, t2.xyz)
eq(t1.time, t2.time)
def test_load_with_frame(get_fn):
t1 = md.load(get_fn("frame0.xtc"), top=get_fn("frame0.pdb"), frame=3)
t2 = md.load(get_fn("frame0.xtc"), top=get_fn("frame0.pdb"))
t2 = t2.slice([3])
eq(t1.xyz, t2.xyz)
eq(t1.time, t2.time)
def test_add_remove_atoms(get_fn):
t = md.load(get_fn("aaqaa-wat.pdb"))
top = t.topology
old_atoms = list(top.atoms)[:]
# Add an atom 'MW' at the end of each water molecule
for r in list(top.residues)[::-1]:
if r.name != "HOH":
continue
atoms = list(r.atoms)
midx = atoms[-1].index + 1
top.insert_atom("MW", None, r, index=midx)
mwidx = [a.index for a in list(top.atoms) if a.name == "MW"]
# Check to see whether the 'MW' atoms have the correct index
assert mwidx == [183 + 4 * i for i in range(83)]
# Now delete the atoms again
for r in list(top.residues)[::-1]:
if r.name != "HOH":
continue
atoms = list(r.atoms)
top.delete_atom_by_index(atoms[-1].index)
roundtrip_atoms = list(top.atoms)[:]
# Ensure the atoms are the same after a round trip of adding / deleting
assert old_atoms == roundtrip_atoms
|