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
|
# -*- Mode: python; tab-width: 4; indent-tabs-mode:nil; coding:utf-8 -*-
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 fileencoding=utf-8
#
# MDAnalysis --- https://www.mdanalysis.org
# Copyright (c) 2006-2017 The MDAnalysis Development Team and contributors
# (see the file AUTHORS for the full list of names)
#
# Released under the Lesser GNU Public Licence, v2.1 or any higher version
#
# Please cite your use of MDAnalysis in published work:
#
# R. J. Gowers, M. Linke, J. Barnoud, T. J. E. Reddy, M. N. Melo, S. L. Seyler,
# D. L. Dotson, J. Domanski, S. Buchoux, I. M. Kenney, and O. Beckstein.
# MDAnalysis: A Python package for the rapid analysis of molecular dynamics
# simulations. In S. Benthall and S. Rostrup editors, Proceedings of the 15th
# Python in Science Conference, pages 102-109, Austin, TX, 2016. SciPy.
# doi: 10.25080/majora-629e541a-00e
#
# N. Michaud-Agrawal, E. J. Denning, T. B. Woolf, and O. Beckstein.
# MDAnalysis: A Toolkit for the Analysis of Molecular Dynamics Simulations.
# J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787
#
import MDAnalysis as mda
import numpy as np
import pytest
from MDAnalysis.coordinates.TNG import HAS_PYTNG
from MDAnalysis.lib.mdamath import triclinic_box
from numpy.testing import assert_allclose, assert_equal
from MDAnalysisTests.coordinates.base import (
BaseReference,
MultiframeReaderTest,
)
from MDAnalysisTests.datafiles import (
COORDINATES_TNG,
COORDINATES_TOPOLOGY,
TNG_traj,
TNG_traj_gro,
TNG_traj_uneven_blocks,
TNG_traj_vels_forces,
)
@pytest.mark.skipif(HAS_PYTNG, reason="pytng present")
def test_pytng_not_present_raises():
with pytest.raises(ImportError, match="please install pytng"):
_ = mda.Universe(TNG_traj_gro, TNG_traj)
@pytest.mark.skipif(HAS_PYTNG, reason="pytng present")
def test_parse_n_atoms_no_pytng():
with pytest.raises(ImportError, match="please install pytng"):
mda.coordinates.TNG.TNGReader.parse_n_atoms(TNG_traj)
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
class TNGReference(BaseReference):
"""Reference synthetic trajectory that was
copied from test_xdr.TRReference"""
def __init__(self):
super(TNGReference, self).__init__()
self.trajectory = COORDINATES_TNG
self.topology = COORDINATES_TOPOLOGY
self.reader = mda.coordinates.TNG.TNGReader
self.writer = mda.coordinates.TNG.TNGReader.Writer
self.ext = "tng"
self.changing_dimensions = True
self.prec = 4
self.first_frame.velocities = self.first_frame.positions / 10
self.first_frame.forces = self.first_frame.positions / 100
self.second_frame.velocities = self.second_frame.positions / 10
self.second_frame.forces = self.second_frame.positions / 100
self.last_frame.velocities = self.last_frame.positions / 10
self.last_frame.forces = self.last_frame.positions / 100
self.jump_to_frame.velocities = self.jump_to_frame.positions / 10
self.jump_to_frame.forces = self.jump_to_frame.positions / 100
def iter_ts(self, i):
ts = self.first_frame.copy()
ts.positions = 2**i * self.first_frame.positions
ts.velocities = ts.positions / 10
ts.forces = ts.positions / 100
ts.time = i
ts.frame = i
return ts
@pytest.mark.filterwarnings("ignore:Empty string")
@pytest.mark.filterwarnings("ignore:Stride of block")
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
class TestTNGCoordinatesTraj(MultiframeReaderTest):
@staticmethod
@pytest.fixture()
def ref():
return TNGReference()
def test_get_writer_1(self, reader):
with pytest.raises(
NotImplementedError,
match="There is currently no writer for TNG files",
):
reader.Writer()
def test_get_writer_2(self, reader):
with pytest.raises(
NotImplementedError,
match="There is currently no writer for TNG files",
):
reader.Writer()
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
def test_tng_traj_uneven_blocks():
with pytest.raises(IOError, match="Strides of TNG special blocks"):
_ = mda.Universe(TNG_traj_gro, TNG_traj_uneven_blocks)
@pytest.mark.filterwarnings("ignore:Failed read for block")
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
class TestTNGTraj(object):
_n_atoms = 1000
_n_frames = 101
_stride = 5000
prec = 5
# these values all taken from GMX dump / 10 to get MDA units
_pos_frame_0_first_3_atoms = (
np.array(
[
[2.53300e00, 1.24400e00, 3.50600e00],
[8.30000e-01, 2.54400e00, 3.44800e00],
[1.09100e00, 1.10000e-01, 3.12900e00],
]
)
* 10
)
_pos_frame_100_first_3_atoms = (
np.array(
[
[4.40000e-01, 3.89000e-01, 1.37400e00],
[1.43200e00, 1.64900e00, 2.93900e00],
[2.01500e00, 2.10300e00, 2.65700e00],
]
)
* 10
)
_box_frame_0 = (
np.array(
[
3.60140e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.60140e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.60140e00,
]
).reshape(3, 3)
* 10
)
_box_frame_100 = (
np.array(
[
3.60140e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.60140e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.60140e00,
]
).reshape(3, 3)
* 10
)
_box_frame_100 = (
np.array(
[
3.58965e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.58965e00,
0.00000e00,
0.00000e00,
0.00000e00,
3.58965e00,
]
).reshape(3, 3)
* 10
)
@pytest.fixture(scope="class")
def universe(self):
return mda.Universe(TNG_traj_gro, TNG_traj)
def test_n_atoms(self, universe):
assert_equal(universe.trajectory.n_atoms, self._n_atoms)
def test_n_frames(self, universe):
assert_equal(
universe.trajectory.n_frames,
self._n_frames,
"wrong number of frames in TNG file",
)
def test_block_names(self, universe):
"""Check the block names in the file"""
assert "TNG_TRAJ_BOX_SHAPE" in universe.trajectory.blocks
assert "TNG_TRAJ_POSITIONS" in universe.trajectory.blocks
assert "TNG_GMX_LAMBDA" in universe.trajectory.blocks
def test_special_blocks(self, universe):
"""Check the position and box special blocks are present"""
assert "TNG_TRAJ_BOX_SHAPE" in universe.trajectory.special_blocks
assert "TNG_TRAJ_POSITIONS" in universe.trajectory.special_blocks
def test_additional_blocks(self, universe):
"""Check the lambda special block is present"""
assert "TNG_GMX_LAMBDA" in universe.trajectory.additional_blocks
def check_strides(self, universe):
"""Check the stride of trajectory frames in integrator steps"""
assert universe.trajectory._global_stride == self._stride
def test_initial_frame_is_0(self, universe):
assert_equal(
universe.trajectory.ts.frame,
0,
"initial frame is not 0 but {0}".format(
universe.trajectory.ts.frame
),
)
def test_starts_with_first_frame(self, universe):
"""Test that coordinate arrays are filled as soon as the trajectory
has been opened."""
assert np.any(universe.atoms.positions > 0)
def test_rewind(self, universe):
trj = universe.trajectory
trj.next()
trj.next() # for readers that do not support indexing
assert_equal(
trj.ts.frame, 2, "failed to forward to frame 2 (frameindex 2)"
)
trj.rewind()
assert_equal(trj.ts.frame, 0, "failed to rewind to first frame")
assert np.any(universe.atoms.positions > 0)
def test_full_slice(self, universe):
trj_iter = universe.trajectory[:]
frames = [ts.frame for ts in trj_iter]
assert_equal(frames, np.arange(universe.trajectory.n_frames))
def test_random_access(self, universe):
pos1 = universe.atoms[0].position
universe.trajectory.next()
universe.trajectory.next()
pos3 = universe.atoms[0].position
universe.trajectory[0]
assert_equal(universe.atoms[0].position, pos1)
universe.trajectory[2]
assert_equal(universe.atoms[0].position, pos3)
def test_frame_overrun(self, universe):
with pytest.raises(IndexError, match="exceeds length of trajectory"):
universe.trajectory[101]
def test_positions_first_frame(self, universe):
pos = universe.trajectory[0].positions
assert_allclose(
pos[0:3, :], self._pos_frame_0_first_3_atoms, rtol=10**-self.prec
)
def test_box_first_frame(self, universe):
dims = universe.trajectory[0].dimensions
assert_allclose(
dims, triclinic_box(*self._box_frame_0), rtol=10**-self.prec
)
def test_positions_last_frame(self, universe):
pos = universe.trajectory[100].positions
assert_allclose(
pos[0:3, :],
self._pos_frame_100_first_3_atoms,
rtol=10**-self.prec,
)
def test_box_last_frame(self, universe):
dims = universe.trajectory[100].dimensions
assert_allclose(
dims, triclinic_box(*self._box_frame_100), rtol=10**-self.prec
)
@pytest.mark.parametrize("frame", [0, 20, 50, 100])
def test_step(self, universe, frame):
ts = universe.trajectory[frame]
step = ts.data["step"]
assert step == ts.frame * universe.trajectory._global_stride
def test_lambda_in_ts(self, universe):
ts = universe.trajectory[10]
assert "TNG_GMX_LAMBDA" in ts.data.keys()
assert isinstance(ts.data["TNG_GMX_LAMBDA"], np.ndarray)
assert_equal(
ts.data["TNG_GMX_LAMBDA"], np.asarray([[0]], dtype=np.float32)
)
def test_read_box_fail_strange_step(self, universe):
stepnum = 123 # step number with no data
iterator_step = universe.trajectory._file_iterator.read_step(stepnum)
with pytest.raises(IOError, match="Failed to read box from TNG file"):
universe.trajectory._frame_to_ts(
iterator_step, universe.trajectory.ts
)
def test_read_pos_fail_strange_step(self, universe):
stepnum = 123 # step number with no data
iterator_step = universe.trajectory._file_iterator.read_step(stepnum)
# set _has_box to False to trigger position reading error
universe.trajectory._has_box = False
with pytest.raises(
IOError, match="Failed to read positions from TNG file"
):
universe.trajectory._frame_to_ts(
iterator_step, universe.trajectory.ts
)
def test_additional_block_read_fails(self, universe):
stepnum = 123 # step number with no data
iterator_step = universe.trajectory._file_iterator.read_step(stepnum)
# set has_box, has_pos, to false to trigger GMX_LAMBDA reading error
universe.trajectory._has_box = False
universe.trajectory._has_positions = False
# doesn't have velocities or forces
with pytest.raises(
IOError, match="Failed to read additional block TNG_GMX_LAMBDA"
):
universe.trajectory._frame_to_ts(
iterator_step, universe.trajectory.ts
)
def test_parse_n_atoms(self, universe):
assert universe.trajectory.parse_n_atoms(TNG_traj) == self._n_atoms
@pytest.mark.filterwarnings("ignore:Off stride read for block")
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
class TestTNGTraj_vels_forces(object):
_n_atoms = 1000
_n_frames = 51
_stride = 10
prec = 5
@pytest.fixture(scope="class")
def universe(self):
return mda.Universe(TNG_traj_gro, TNG_traj_vels_forces)
def test_n_atoms(self, universe):
assert_equal(universe.trajectory.n_atoms, self._n_atoms)
def test_n_frames(self, universe):
assert_equal(
universe.trajectory.n_frames,
self._n_frames,
"wrong number of frames in TNG file",
)
def test_block_names(self, universe):
"""Check the block names in the file"""
assert "TNG_TRAJ_BOX_SHAPE" in universe.trajectory.blocks
assert "TNG_TRAJ_POSITIONS" in universe.trajectory.blocks
assert "TNG_TRAJ_VELOCITIES" in universe.trajectory.blocks
assert "TNG_TRAJ_FORCES" in universe.trajectory.blocks
assert "TNG_GMX_LAMBDA" in universe.trajectory.blocks
def test_special_blocks(self, universe):
"""Check the position and box special blocks are present"""
assert "TNG_TRAJ_BOX_SHAPE" in universe.trajectory.special_blocks
assert "TNG_TRAJ_POSITIONS" in universe.trajectory.special_blocks
assert "TNG_TRAJ_VELOCITIES" in universe.trajectory.special_blocks
assert "TNG_TRAJ_FORCES" in universe.trajectory.special_blocks
def check_strides(self, universe):
"""Check the stride of trajectory frames in integrator steps"""
assert universe.trajectory._global_stride == self._stride
def test_read_vels_fail_strange_step(self, universe):
stepnum = 123 # step number with no data
iterator_step = universe.trajectory._file_iterator.read_step(stepnum)
# set _has_* attrs to False to trigger velocities reading error
universe.trajectory._has_box = False
universe.trajectory._has_positions = False
with pytest.raises(
IOError, match="Failed to read velocities from TNG file"
):
universe.trajectory._frame_to_ts(
iterator_step, universe.trajectory.ts
)
def test_read_force_fail_strange_step(self, universe):
stepnum = 123 # step number with no data
iterator_step = universe.trajectory._file_iterator.read_step(stepnum)
# set _has_* attrs to False to trigger forces reading error
universe.trajectory._has_box = False
universe.trajectory._has_positions = False
universe.trajectory._has_velocities = False
with pytest.raises(
IOError, match="Failed to read forces from TNG file"
):
universe.trajectory._frame_to_ts(
iterator_step, universe.trajectory.ts
)
@pytest.mark.skipif(not HAS_PYTNG, reason="pytng not installed")
def test_writer_raises_notimpl():
u = mda.Universe(TNG_traj_gro, TNG_traj)
with pytest.raises(
NotImplementedError,
match="There is currently no writer for TNG files",
):
u.trajectory.Writer()
|