Beispiel #1
0
def test_unit_cell_volume():
    from pyxmolpp2 import UnitCell, XYZ, Degrees

    cell = UnitCell(1, 1, 1, Degrees(90), Degrees(90), Degrees(90))
    assert cell.volume == 1
    cell.scale_by(2)
    assert cell.volume == 8

    cell.scale_to_volume(27)

    assert cell[0].distance(XYZ(3, 0, 0)) == pytest.approx(0)
    assert cell[1].distance(XYZ(0, 3, 0)) == pytest.approx(0)
    assert cell[2].distance(XYZ(0, 0, 3)) == pytest.approx(0)
Beispiel #2
0
def test_shorthands():
    import numpy as np
    from pyxmolpp2 import Rotation, Translation, XYZ, Degrees, Frame, calc_alignment

    frame = make_polyglycine([("A", 20)])
    for a in frame.atoms:
        a.r = XYZ(*np.random.random(3))
    frame2 = Frame(frame)

    assert frame2 != frame
    print(frame, frame2)
    asel = frame.atoms
    asel2 = frame2.atoms

    asel.coords.mean()
    # asel.mass_center([1.0] * asel.size) # todo: enable
    # asel.inertia_tensor([1.0] * asel.size) # todo: enable
    asel.coords.inertia_tensor()

    T = Translation(XYZ(1, 0, 0))
    asel2.coords.apply(T)
    print(asel.coords.rmsd(asel2.coords))
    assert np.isclose(asel.coords.rmsd(asel2.coords), 1.0)
    assert np.isclose(asel.coords.rmsd(asel2.coords), 1.0)
    asel2.coords.apply(T.inverted())
    assert np.isclose(asel.coords.rmsd(asel2.coords), 0.0)

    T = Translation(XYZ(1, 0, 0)) * Rotation(XYZ(1, 1, 1), Degrees(45))
    asel.coords.apply(asel.coords.alignment_to(asel2.coords))
    assert np.isclose(asel.coords.rmsd(asel2.coords), 0)

    asel2.coords.apply(T)
    asel.align_to(asel2)
    assert np.isclose(asel.coords.rmsd(asel2.coords), 0)

    asel2.coords.apply(T)
    asel2.guess_mass()
    asel.guess_mass()
    asel.align_to(asel2, weighted=True)
    assert np.isclose(asel.rmsd(asel2), 0)
    assert np.isclose(asel.rmsd(asel2, weighted=True), 0)

    T = Translation(XYZ(1, 0, 0)) * Rotation(XYZ(1, 1, 1), Degrees(45))
    asel2.coords.apply(T)

    assert np.allclose(
        T.matrix3d(),
        calc_alignment(asel2.coords.values, asel.coords.values).matrix3d())
    assert np.allclose(T.matrix3d(), asel.alignment_to(asel2).matrix3d())
Beispiel #3
0
def test_conversions():
    from pyxmolpp2 import Degrees, Radians, radians_to_degrees, degrees_to_radians
    deg = Degrees(10)
    rad = Radians(100)

    assert rad.radians == pytest.approx(100)
    assert deg.degrees == pytest.approx(10)

    assert rad.degrees == pytest.approx(radians_to_degrees(100))
    assert deg.radians == pytest.approx(degrees_to_radians(10))
Beispiel #4
0
def test_calc_alignment():
    from pyxmolpp2 import calc_alignment, XYZ, calc_rmsd, Rotation, Translation, Degrees

    a = np.array([(1, 2, 3), (1, 2, 5), (4, 2, 7), (8, 1, 4)])
    G = Rotation(XYZ(7, 6, 5), Degrees(12)) * Translation(XYZ(8, -9, 1))
    b = np.array([G.transform(XYZ(*x)).values for x in a])

    G2 = calc_alignment(a, b)
    assert calc_rmsd(a, b) > 1
    c = np.array([G2.transform(XYZ(*x)).values for x in b])
    assert calc_rmsd(a, c) == pytest.approx(0)
Beispiel #5
0
def test_composition():
    from pyxmolpp2 import XYZ, Rotation, Translation, Degrees, UniformScale

    R = Rotation(XYZ(1, 1, 1), Degrees(39))
    T = Translation(XYZ(7, 1, 2))
    S = UniformScale(1.5)
    G = R * T * S * R * T

    def check(G):
        r = XYZ(5, 6, 4)
        assert ((G * G.inverted()).transform(r) - r).len() == pytest.approx(0)

    check(R)
    check(T)
    check(S)
    check(G)

    for a in [R, T, S, G]:
        for b in [R, T, S, G]:
            check(a * b)
Beispiel #6
0
def test_transformation_3d():
    import numpy as np
    from pyxmolpp2 import XYZ, Rotation, Translation, Degrees

    R = Rotation(XYZ(1, 0, 0), Degrees(45))
    T = Translation(XYZ(1, 2, 5))

    G = T * R

    m = G.matrix3d()
    v = G.vector3d()

    assert pytest.approx(m[0, 0]) == 1
    assert pytest.approx(m[0, 1]) == 0
    assert pytest.approx(m[0, 2]) == 0
    assert pytest.approx(np.sqrt(2) / 2) == m[2, 1]

    assert v.x == 1
    assert v.y == 2
    assert v.z == 5
Beispiel #7
0
def test_rotation_decomposition():
    import numpy as np
    from pyxmolpp2 import XYZ, Rotation, Degrees

    for ax, theta in [
        (XYZ(0, 0, 1), Degrees(30)),
        (XYZ(0, 0, 1), Degrees(70)),
        (XYZ(0, 1, 0), Degrees(70)),
        (XYZ(1, 0, 0), Degrees(70)),
        (XYZ(1, 1, 0), Degrees(70)),
        (XYZ(1, 1, 1), Degrees(70)),
        (XYZ(1, 0, 1), Degrees(0)),
        (XYZ(1, 1, 1), Degrees(0)),
    ]:
        R = Rotation(ax, theta)

        ax1, theta1 = R.axis(), R.theta()

        R2 = Rotation(ax1, theta1)

        assert np.allclose(R.matrix3d(), R2.matrix3d()), ("\n".join(
            map(lambda x: "%25.18e" % x,
                (R.matrix3d() - R2.matrix3d()).flatten())))
Beispiel #8
0
def test_read_frame():
    from pyxmolpp2 import PdbFile, UnitCell, Degrees

    cells = [
        UnitCell(36.633, 36.633, 79.254, Degrees(90), Degrees(90),
                 Degrees(120.00)),
        UnitCell(50.840, 42.770, 28.950, Degrees(90), Degrees(90.00),
                 Degrees(90.00)),
        UnitCell(52.323, 79.498, 52.406, Degrees(90), Degrees(90.14),
                 Degrees(90.00)),
        UnitCell(25.037, 37.194, 49.217, Degrees(90), Degrees(90.00),
                 Degrees(90.00)),
    ]

    for pdb_code, cell in zip(['1PGB', '1UBQ', '5BMG', '5BMH'], cells):
        frames = PdbFile(os.environ["TEST_DATA_PATH"] +
                         f"/pdb/rcsb/{pdb_code}.pdb").frames()
        for frame in frames:
            for i in range(3):
                assert cell[i].distance(frame.cell[i]) == pytest.approx(0)
            assert frame.atoms.size > 0