File: test_wrap.py

package info (click to toggle)
mdanalysis 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,696 kB
  • sloc: python: 92,135; ansic: 8,156; makefile: 215; sh: 138
file content (156 lines) | stat: -rw-r--r-- 5,139 bytes parent folder | download | duplicates (2)
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
# -*- 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.
#
# 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 numpy as np
import pytest
from numpy.testing import assert_array_almost_equal

import MDAnalysis as mda
from MDAnalysis.transformations import wrap, unwrap
from MDAnalysis.lib._cutil import make_whole
from MDAnalysisTests.datafiles import fullerene
from MDAnalysisTests.datafiles import GRO, TPR


@pytest.fixture()
def wrap_universes():
    # create the Universe objects for the tests
    # this universe is used for the unwrap testing cases
    reference = mda.Universe(fullerene)
    transformed = mda.Universe(fullerene)
    transformed.dimensions = np.asarray([10, 10, 10, 90, 90, 90], np.float32)
    transformed.atoms.wrap()

    return reference, transformed


@pytest.fixture()
def compound_wrap_universes():
    # create universe for the compound tests
    # this universe has multiple residues, segments and
    # fragments
    transformed = mda.Universe(TPR, GRO)
    reference = mda.Universe(TPR, GRO)
    # wrap the atoms back into the unit cell
    # in this coordinate file only the protein
    # is broken across PBC however the system
    # shape is not the same as the unit cell
    make_whole(reference.select_atoms("protein"))
    make_whole(transformed.select_atoms("protein"))

    return transformed, reference


@pytest.mark.parametrize(
    "ag",
    (
        [0, 1],
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
        np.array([[0], [1], [2]]),
        "thisisnotanag",
        1,
    ),
)
def test_wrap_bad_ag(wrap_universes, ag):
    # this universe has a box size zero
    ts = wrap_universes[0].trajectory.ts
    # what happens if something other than an AtomGroup is given?
    bad_ag = ag
    with pytest.raises(AttributeError):
        wrap(bad_ag)(ts)


def test_wrap_no_options(wrap_universes):
    # since were testing if the wrapping works
    # the reference and the transformed are switched
    trans, ref = wrap_universes
    trans.dimensions = ref.dimensions
    wrap(trans.atoms)(trans.trajectory.ts)
    assert_array_almost_equal(
        trans.trajectory.ts.positions, ref.trajectory.ts.positions, decimal=6
    )


@pytest.mark.parametrize(
    "compound", ("group", "residues", "segments", "fragments")
)
def test_wrap_with_compounds(compound_wrap_universes, compound):
    trans, ref = compound_wrap_universes
    ref.select_atoms("not resname SOL").wrap(compound=compound)
    wrap(trans.select_atoms("not resname SOL"), compound=compound)(
        trans.trajectory.ts
    )
    assert_array_almost_equal(
        trans.trajectory.ts.positions, ref.trajectory.ts.positions, decimal=6
    )


def test_wrap_api(wrap_universes):
    trans, ref = wrap_universes
    trans.dimensions = ref.dimensions
    trans.trajectory.add_transformations(wrap(trans.atoms))
    assert_array_almost_equal(
        trans.trajectory.ts.positions, ref.trajectory.ts.positions, decimal=6
    )


@pytest.mark.parametrize(
    "ag",
    (
        [0, 1],
        [0, 1, 2, 3, 4],
        np.array([0, 1]),
        np.array([0, 1, 2, 3, 4]),
        np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
        np.array([[0], [1], [2]]),
        "thisisnotanag",
        1,
    ),
)
def test_unwrap_bad_ag(wrap_universes, ag):
    # this universe has a box size zero
    ts = wrap_universes[0].trajectory.ts
    # what happens if something other than an AtomGroup is given?
    bad_ag = ag
    with pytest.raises(AttributeError):
        unwrap(bad_ag)(ts)


def test_unwrap(wrap_universes):
    ref, trans = wrap_universes
    # after rebuild the trans molecule it should match the reference
    unwrap(trans.atoms)(trans.trajectory.ts)
    assert_array_almost_equal(
        trans.trajectory.ts.positions, ref.trajectory.ts.positions, decimal=6
    )


def test_unwrap_api(wrap_universes):
    ref, trans = wrap_universes
    # after rebuild the trans molecule it should match the reference
    trans.trajectory.add_transformations(unwrap(trans.atoms))
    assert_array_almost_equal(
        trans.trajectory.ts.positions, ref.trajectory.ts.positions, decimal=6
    )