File: test_SVDSuperimposer.py

package info (click to toggle)
python-biopython 1.78%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 65,756 kB
  • sloc: python: 221,141; xml: 178,777; ansic: 13,369; sql: 1,208; makefile: 131; sh: 70
file content (133 lines) | stat: -rw-r--r-- 4,354 bytes parent folder | download
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
# Copyright 2017 by Maximilian Greil.  All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license.  Please see the LICENSE file that should have been included
# as part of this package.

"""Tests for SVDSuperimposer module."""

import unittest

try:
    from numpy import array
    from numpy import dot  # missing in old PyPy's micronumpy
    from numpy import array_equal
    from numpy import around
    from numpy.linalg import svd, det  # Missing in PyPy 2.0 numpypy
except ImportError:
    from Bio import MissingPythonDependencyError

    raise MissingPythonDependencyError(
        "Install NumPy if you want to use Bio.SVDSuperimposer."
    ) from None

from Bio.SVDSuperimposer import SVDSuperimposer


class SVDSuperimposerTest(unittest.TestCase):
    def setUp(self):
        self.x = array(
            [
                [51.65, -1.90, 50.07],
                [50.40, -1.23, 50.65],
                [50.68, -0.04, 51.54],
                [50.22, -0.02, 52.85],
            ]
        )

        self.y = array(
            [
                [51.30, -2.99, 46.54],
                [51.09, -1.88, 47.58],
                [52.36, -1.20, 48.03],
                [52.71, -1.18, 49.38],
            ]
        )

        self.sup = SVDSuperimposer()
        self.sup.set(self.x, self.y)

    def test_get_init_rms(self):
        x = array([[1.19, 1.28, 1.37], [1.46, 1.55, 1.64], [1.73, 1.82, 1.91]])
        y = array([[1.91, 1.82, 1.73], [1.64, 1.55, 1.46], [1.37, 1.28, 1.19]])
        self.sup.set(x, y)
        self.assertIsNone(self.sup.init_rms)
        init_rms = 0.8049844719
        self.assertTrue(
            float("%.3f" % self.sup.get_init_rms()), float("%.3f" % init_rms)
        )

    def test_oldTest(self):
        self.assertTrue(
            array_equal(
                around(self.sup.reference_coords, decimals=3),
                around(self.x, decimals=3),
            )
        )
        self.assertTrue(
            array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))
        )
        self.assertIsNone(self.sup.rot)
        self.assertIsNone(self.sup.tran)
        self.assertIsNone(self.sup.rms)
        self.assertIsNone(self.sup.init_rms)

        self.sup.run()
        self.assertTrue(
            array_equal(
                around(self.sup.reference_coords, decimals=3),
                around(self.x, decimals=3),
            )
        )
        self.assertTrue(
            array_equal(around(self.sup.coords, decimals=3), around(self.y, decimals=3))
        )
        rot = array(
            [
                [0.68304983, 0.53664371, 0.49543563],
                [-0.52277295, 0.83293229, -0.18147242],
                [-0.51005037, -0.13504564, 0.84947707],
            ]
        )
        tran = array([38.78608157, -20.65451334, -15.42227366])
        self.assertTrue(
            array_equal(around(self.sup.rot, decimals=3), around(rot, decimals=3))
        )
        self.assertTrue(
            array_equal(around(self.sup.tran, decimals=3), around(tran, decimals=3))
        )
        self.assertIsNone(self.sup.rms)
        self.assertIsNone(self.sup.init_rms)

        rms = 0.00304266526014
        self.assertEqual(float("%.3f" % self.sup.get_rms()), float("%.3f" % rms))

        rot_get, tran_get = self.sup.get_rotran()
        self.assertTrue(
            array_equal(around(rot_get, decimals=3), around(rot, decimals=3))
        )
        self.assertTrue(
            array_equal(around(tran_get, decimals=3), around(tran, decimals=3))
        )

        y_on_x1 = dot(self.y, rot) + tran
        y_x_solution = array(
            [
                [5.16518846e01, -1.90018270e00, 5.00708397e01],
                [5.03977138e01, -1.22877050e00, 5.06488200e01],
                [5.06801788e01, -4.16095666e-02, 5.15368866e01],
                [5.02202228e01, -1.94372374e-02, 5.28534537e01],
            ]
        )
        self.assertTrue(
            array_equal(around(y_on_x1, decimals=3), around(y_x_solution, decimals=3))
        )

        y_on_x2 = self.sup.get_transformed()
        self.assertTrue(
            array_equal(around(y_on_x2, decimals=3), around(y_x_solution, decimals=3))
        )


if __name__ == "__main__":
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)