-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathtest_rms.py
More file actions
577 lines (496 loc) · 19.3 KB
/
test_rms.py
File metadata and controls
577 lines (496 loc) · 19.3 KB
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
# -*- 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 os
import MDAnalysis
import MDAnalysis as mda
from MDAnalysis.analysis import rms, align
from numpy.testing import assert_equal, assert_almost_equal
import numpy as np
import pytest
from MDAnalysis.exceptions import SelectionError, NoDataError
from MDAnalysisTests.datafiles import GRO, XTC, rmsfArray, PSF, DCD
class Testrmsd(object):
shape = (5, 3)
# vectors with length one
ones = np.ones(shape) / np.sqrt(3)
@pytest.fixture()
def a(self):
return self.ones * np.arange(1, 6)[:, np.newaxis]
@pytest.fixture()
def b(self, a):
return a + self.ones
@pytest.fixture()
def u(self):
u = mda.Universe(PSF, DCD)
return u
@pytest.fixture()
def u2(self):
u = mda.Universe(PSF, DCD)
return u
@pytest.fixture()
def p_first(self, u):
u.trajectory[0]
return u.select_atoms("protein")
@pytest.fixture()
def p_last(self, u2):
u2.trajectory[-1]
return u2.select_atoms("protein")
# internal test
def test_p_frames(self, p_first, p_last):
# check that these fixtures are really different
assert (
p_first.universe.trajectory.ts.frame
!= p_last.universe.trajectory.ts.frame
)
assert not np.allclose(p_first.positions, p_last.positions)
def test_no_center(self, a, b):
rmsd = rms.rmsd(a, b, center=False)
assert_almost_equal(rmsd, 1.0)
def test_center(self, a, b):
rmsd = rms.rmsd(a, b, center=True)
assert_almost_equal(rmsd, 0.0)
def test_list(self, a, b):
rmsd = rms.rmsd(a.tolist(), b.tolist(), center=False)
assert_almost_equal(rmsd, 1.0)
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_superposition(self, a, b, u, dtype):
bb = u.atoms.select_atoms("backbone")
a = bb.positions.copy()
u.trajectory[-1]
b = bb.positions.copy()
a, b = a.astype(dtype), b.astype(dtype)
rmsd = rms.rmsd(a, b, superposition=True)
assert_almost_equal(rmsd, 6.820321761927005)
def test_weights(self, a, b):
weights = np.zeros(len(a))
weights[0] = 1
weights[1] = 1
weighted = rms.rmsd(a, b, weights=weights)
firstCoords = rms.rmsd(a[:2], b[:2])
assert_almost_equal(weighted, firstCoords)
def test_weights_and_superposition_1(self, u):
weights = np.ones(len(u.trajectory[0]))
weighted = rms.rmsd(
u.trajectory[0],
u.trajectory[1],
weights=weights,
superposition=True,
)
firstCoords = rms.rmsd(
u.trajectory[0], u.trajectory[1], superposition=True
)
assert_almost_equal(weighted, firstCoords, decimal=5)
def test_weights_and_superposition_2(self, u):
weights = np.zeros(len(u.trajectory[0]))
weights[:100] = 1
weighted = rms.rmsd(
u.trajectory[0],
u.trajectory[-1],
weights=weights,
superposition=True,
)
firstCoords = rms.rmsd(
u.trajectory[0][:100], u.trajectory[-1][:100], superposition=True
)
# very close to zero, change significant decimal places to 5
assert_almost_equal(weighted, firstCoords, decimal=5)
def test_unequal_shape(self):
a = np.ones((4, 3))
b = np.ones((5, 3))
with pytest.raises(ValueError):
rms.rmsd(a, b)
def test_wrong_weights(self, a, b):
w = np.ones(2)
with pytest.raises(ValueError):
rms.rmsd(a, b, w)
def test_with_superposition_smaller(self, p_first, p_last):
A = p_first.positions
B = p_last.positions
rmsd = rms.rmsd(A, B)
rmsd_superposition = rms.rmsd(A, B, center=True, superposition=True)
# by design the super positioned rmsd is smaller
assert rmsd > rmsd_superposition
def test_with_superposition_equal(self, p_first, p_last):
align.alignto(p_first, p_last)
A = p_first.positions
B = p_last.positions
rmsd = rms.rmsd(A, B)
rmsd_superposition = rms.rmsd(A, B, center=True, superposition=True)
assert_almost_equal(rmsd, rmsd_superposition, decimal=6)
class TestRMSD(object):
@pytest.fixture()
def universe(self):
return MDAnalysis.Universe(PSF, DCD)
@pytest.fixture()
def outfile(self, tmpdir):
return os.path.join(str(tmpdir), "rmsd.txt")
@pytest.fixture()
def correct_values(self):
return [[0, 1, 0], [49, 50, 4.68953]]
@pytest.fixture()
def correct_values_mass(self):
return [[0, 1, 0], [49, 50, 4.74920]]
@pytest.fixture()
def correct_values_mass_add_ten(self):
return [[0, 1, 0.0632], [49, 50, 4.7710]]
@pytest.fixture()
def correct_values_group(self):
return [[0, 1, 0, 0, 0], [49, 50, 4.7857, 4.7048, 4.6924]]
@pytest.fixture()
def correct_values_backbone_group(self):
return [[0, 1, 0, 0, 0], [49, 50, 4.6997, 1.9154, 2.7139]]
def test_rmsd(self, universe, correct_values, client_RMSD):
# client_RMSD is defined in testsuite/analysis/conftest.py
# among with other testing fixtures. During testing, it will
# collect all possible backends and reasonable number of workers
# for a given AnalysisBase subclass, and extend the tests
# to run with all of them.
RMSD = MDAnalysis.analysis.rms.RMSD(universe, select="name CA")
RMSD.run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values,
4,
err_msg="error: rmsd profile should match" + "test values",
)
def test_rmsd_frames(self, universe, correct_values, client_RMSD):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, select="name CA")
RMSD.run(frames=[0, 49], **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values,
4,
err_msg="error: rmsd profile should match" + "test values",
)
def test_rmsd_unicode_selection(
self, universe, correct_values, client_RMSD
):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, select="name CA")
RMSD.run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values,
4,
err_msg="error: rmsd profile should match" + "test values",
)
def test_rmsd_atomgroup_selections(self, universe, client_RMSD):
# see Issue #1684
R1 = MDAnalysis.analysis.rms.RMSD(
universe.atoms, select="resid 1-30"
).run(**client_RMSD)
R2 = MDAnalysis.analysis.rms.RMSD(
universe.atoms.select_atoms("name CA"), select="resid 1-30"
).run(**client_RMSD)
assert not np.allclose(R1.results.rmsd[:, 2], R2.results.rmsd[:, 2])
def test_rmsd_single_frame(self, universe, client_RMSD):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
select="name CA",
).run(start=5, stop=6, **client_RMSD)
single_frame = [[5, 6, 0.91544906]]
assert_almost_equal(
RMSD.results.rmsd,
single_frame,
4,
err_msg="error: rmsd profile should match" + "test values",
)
def test_mass_weighted(self, universe, correct_values, client_RMSD):
# mass weighting the CA should give the same answer as weighing
# equally because all CA have the same mass
RMSD = MDAnalysis.analysis.rms.RMSD(
universe, select="name CA", weights="mass"
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values,
4,
err_msg="error: rmsd profile should match" "test values",
)
def test_custom_weighted(self, universe, correct_values_mass, client_RMSD):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, weights="mass").run(
step=49, **client_RMSD
)
assert_almost_equal(
RMSD.results.rmsd,
correct_values_mass,
4,
err_msg="error: rmsd profile should match" "test values",
)
def test_weights_mass_is_mass_weighted(self, universe, client_RMSD):
RMSD_mass = MDAnalysis.analysis.rms.RMSD(universe, weights="mass").run(
step=49, **client_RMSD
)
RMSD_cust = MDAnalysis.analysis.rms.RMSD(
universe, weights=universe.atoms.masses
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD_mass.results.rmsd,
RMSD_cust.results.rmsd,
4,
err_msg="error: rmsd profiles should match for 'mass' "
"and universe.atoms.masses",
)
def test_custom_weighted_list(
self, universe, correct_values_mass, client_RMSD
):
weights = universe.atoms.masses
RMSD = MDAnalysis.analysis.rms.RMSD(
universe, weights=list(weights)
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values_mass,
4,
err_msg="error: rmsd profile should match" + "test values",
)
def test_custom_groupselection_weights_applied_1D_array(
self, universe, client_RMSD
):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
select="backbone",
groupselections=["name CA and resid 1-5", "name CA and resid 1"],
weights=None,
weights_groupselections=[[1, 0, 0, 0, 0], None],
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd.T[3],
RMSD.results.rmsd.T[4],
4,
err_msg="error: rmsd profile should match "
"for applied weight array and selected resid",
)
def test_custom_groupselection_weights_applied_mass(
self, universe, correct_values_mass, client_RMSD
):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
select="backbone",
groupselections=["all", "all"],
weights=None,
weights_groupselections=["mass", universe.atoms.masses],
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd.T[3],
RMSD.results.rmsd.T[4],
4,
err_msg="error: rmsd profile should match "
"between applied mass and universe.atoms.masses",
)
def test_rmsd_scalar_weights_raises_ValueError(self, universe):
with pytest.raises(ValueError):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, weights=42)
def test_rmsd_string_weights_raises_ValueError(self, universe):
with pytest.raises(ValueError):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, weights="Jabberwock")
def test_rmsd_mismatched_weights_raises_ValueError(self, universe):
with pytest.raises(ValueError):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe, weights=universe.atoms.masses[:-1]
)
def test_rmsd_misuse_weights_for_groupselection_raises_TypeError(
self, universe
):
with pytest.raises(TypeError):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
groupselections=["all"],
weights=[universe.atoms.masses, universe.atoms.masses[:-1]],
)
def test_rmsd_mismatched_weights_in_groupselection_raises_ValueError(
self, universe
):
with pytest.raises(ValueError):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
groupselections=["all"],
weights=universe.atoms.masses,
weights_groupselections=[universe.atoms.masses[:-1]],
)
def test_rmsd_list_of_weights_wrong_length(self, universe):
with pytest.raises(ValueError):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
groupselections=["backbone", "name CA"],
weights="mass",
weights_groupselections=[None],
)
def test_rmsd_groupselections_with_mass_weights(
self, universe, client_RMSD
):
# Test to ensure line 694 coverage with weights="mass" in groupselections
# This tests the code path where weights="mass" is propagated to
# groupselections when weights_groupselections=False
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
select="backbone",
groupselections=["name CA", "backbone and resid 1:10"],
weights="mass",
weights_groupselections=False,
).run(step=49, **client_RMSD)
# Just verify it runs without error and produces valid results
assert RMSD.results.rmsd.shape[0] == 2 # 2 frames (0 and 49)
assert (
RMSD.results.rmsd.shape[1] == 5
) # frame, time, rmsd, group1, group2
def test_rmsd_group_selections(
self, universe, correct_values_group, client_RMSD
):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe, groupselections=["backbone", "name CA"]
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values_group,
4,
err_msg="error: rmsd profile should match" "test values",
)
def test_rmsd_backbone_and_group_selection(
self, universe, correct_values_backbone_group, client_RMSD
):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
reference=universe,
select="backbone",
groupselections=[
"backbone and resid 1:10",
"backbone and resid 10:20",
],
).run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values_backbone_group,
4,
err_msg="error: rmsd profile should match test values",
)
def test_ref_length_unequal_len(self, universe):
reference = MDAnalysis.Universe(PSF, DCD)
reference.atoms = reference.atoms[:-1]
with pytest.raises(SelectionError):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, reference=reference)
def test_mass_mismatches(self, universe):
reference = MDAnalysis.Universe(PSF, DCD)
reference.atoms.masses = 10
with pytest.raises(SelectionError):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, reference=reference)
def test_ref_mobile_mass_mismapped(
self, universe, correct_values_mass_add_ten, client_RMSD
):
reference = MDAnalysis.Universe(PSF, DCD)
universe.atoms.masses = universe.atoms.masses + 10
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
reference=reference,
select="all",
weights="mass",
tol_mass=100,
)
RMSD.run(step=49, **client_RMSD)
assert_almost_equal(
RMSD.results.rmsd,
correct_values_mass_add_ten,
4,
err_msg="error: rmsd profile should match "
"between true values and calculated values",
)
def test_group_selections_unequal_len(self, universe):
reference = MDAnalysis.Universe(PSF, DCD)
reference.atoms[0].residue.resname = "NOTMET"
with pytest.raises(SelectionError):
RMSD = MDAnalysis.analysis.rms.RMSD(
universe,
reference=reference,
groupselections=["resname MET", "type NH3"],
)
def test_rmsd_attr_warning(self, universe, client_RMSD):
RMSD = MDAnalysis.analysis.rms.RMSD(universe, select="name CA").run(
stop=2, **client_RMSD
)
wmsg = "The `rmsd` attribute was deprecated in MDAnalysis 2.0.0"
with pytest.warns(DeprecationWarning, match=wmsg):
assert_equal(RMSD.rmsd, RMSD.results.rmsd)
class TestRMSF(object):
@pytest.fixture()
def universe(self):
return mda.Universe(GRO, XTC)
def test_rmsf(self, universe, client_RMSF):
rmsfs = rms.RMSF(universe.select_atoms("name CA"))
rmsfs.run(**client_RMSF)
test_rmsfs = np.load(rmsfArray)
assert_almost_equal(
rmsfs.results.rmsf,
test_rmsfs,
5,
err_msg="error: rmsf profile should match test " "values",
)
def test_rmsf_single_frame(self, universe, client_RMSF):
rmsfs = rms.RMSF(universe.select_atoms("name CA")).run(
start=5, stop=6, **client_RMSF
)
assert_almost_equal(
rmsfs.results.rmsf, 0, 5, err_msg="error: rmsfs should all be zero"
)
def test_rmsf_identical_frames(self, universe, tmpdir, client_RMSF):
outfile = os.path.join(str(tmpdir), "rmsf.xtc")
# write a dummy trajectory of all the same frame
with mda.Writer(outfile, universe.atoms.n_atoms) as W:
for _ in range(universe.trajectory.n_frames):
W.write(universe)
universe = mda.Universe(GRO, outfile)
rmsfs = rms.RMSF(universe.select_atoms("name CA"))
rmsfs.run(**client_RMSF)
assert_almost_equal(
rmsfs.results.rmsf, 0, 5, err_msg="error: rmsfs should all be 0"
)
def test_rmsf_attr_warning(self, universe, client_RMSF):
rmsfs = rms.RMSF(universe.select_atoms("name CA")).run(
stop=2, **client_RMSF
)
wmsg = "The `rmsf` attribute was deprecated in MDAnalysis 2.0.0"
with pytest.warns(DeprecationWarning, match=wmsg):
assert_equal(rmsfs.rmsf, rmsfs.results.rmsf)
@pytest.mark.parametrize(
"classname,is_parallelizable",
[
(MDAnalysis.analysis.rms.RMSD, True),
(MDAnalysis.analysis.rms.RMSF, False),
],
)
def test_class_is_parallelizable(classname, is_parallelizable):
assert classname._analysis_algorithm_is_parallelizable == is_parallelizable
@pytest.mark.parametrize(
"classname,backends",
[
(
MDAnalysis.analysis.rms.RMSD,
(
"serial",
"multiprocessing",
"dask",
),
),
(MDAnalysis.analysis.rms.RMSF, ("serial",)),
],
)
def test_supported_backends(classname, backends):
assert classname.get_supported_backends() == backends