Exemplo n.º 1
0
def test_index_functions():
    """
    The `index_xxx()` functions should give the same result as the
    corresponding `xxx` functions.
    """
    stack = strucio.load_structure(join(data_dir, "1l2y.mmtf"))
    array = stack[0]
    # Test for atom array, stack and raw coordinates
    samples = (array, stack, struc.coord(array), struc.coord(stack))
    # Generate random indices
    random.seed(42)
    indices = random.randint(array.array_length(), size=(100, 4), dtype=int)
    for sample in samples:
        if isinstance(sample, np.ndarray):
            atoms1 = sample[..., indices[:, 0], :]
            atoms2 = sample[..., indices[:, 1], :]
            atoms3 = sample[..., indices[:, 2], :]
            atoms4 = sample[..., indices[:, 3], :]
        else:
            atoms1 = sample[..., indices[:, 0]]
            atoms2 = sample[..., indices[:, 1]]
            atoms3 = sample[..., indices[:, 2]]
            atoms4 = sample[..., indices[:, 3]]
        assert np.allclose(struc.displacement(atoms1, atoms2),
                           struc.index_displacement(sample, indices[:, :2]),
                           atol=1e-5)
        assert np.allclose(struc.distance(atoms1, atoms2),
                           struc.index_distance(sample, indices[:, :2]),
                           atol=1e-5)
        assert np.allclose(struc.angle(atoms1, atoms2, atoms3),
                           struc.index_angle(sample, indices[:, :3]),
                           atol=1e-5)
        assert np.allclose(struc.dihedral(atoms1, atoms2, atoms3, atoms4),
                           struc.index_dihedral(sample, indices[:, :4]),
                           atol=1e-5)
Exemplo n.º 2
0
def test_rotate_measure(axis, random_seed):
    """
    Rotate and measure resulting angle that should be equal to the input
    angle.
    """
    np.random.seed(random_seed)
    # Maximum rotation is only 180 degrees,
    # as with higher angles the measured angle would decrease again
    ref_angle = np.random.rand() * np.pi
    angles = np.zeros(3)
    angles[axis] = ref_angle

    # The measured angle is only equal to the input angle,
    # if the input coordinates have no component on the rotation axis 
    input_coord = np.ones(3)
    input_coord[axis] = 0

    rotated = struc.rotate(input_coord, angles)
    test_angle = struc.angle(rotated, 0, input_coord)
    
    # Vector length should be unchanged
    assert np.linalg.norm(rotated) \
        == pytest.approx(np.linalg.norm(input_coord))
    assert test_angle == pytest.approx(ref_angle)
Exemplo n.º 3
0
def test_angle():
    coord1 = struc.coord([0, 0, 1])
    coord2 = struc.coord([0, 0, 0])
    coord3 = struc.coord([0, 1, 1])
    assert struc.angle(coord1, coord2, coord3) == pytest.approx(0.25 * np.pi)
Exemplo n.º 4
0
print(struc.distance(stack, stack[0, 0]).shape)
# And finally distances between two adjacent CA in the first frame
array = stack[0]
print("Adjacent CA distances")
print(struc.distance(array[:-1], array[1:]))

########################################################################
# Like some other functions in :mod:`biotite.structure`, we are able to
# pick any combination of an atom, atom array or stack. Alternatively
# :class:`ndarray` objects containing the coordinates can be provided.
#
# Furthermore, we can measure bond angles and dihedral angles.

# Calculate angle between first 3 CA atoms in first frame
# (in radians)
print("Angle:", struc.angle(array[0], array[1], array[2]))
# Calculate dihedral angle between first 4 CA atoms in first frame
# (in radians)
print("Dihedral angle:", struc.dihedral(array[0], array[1], array[2],
                                        array[4]))

########################################################################
# .. note:: The :func:`distance()`, :func:`angle()` and
#    :func:`dihedral()` functions all have their :func:`pair_...()`
#    counterparts, that take can atom array (stack) and
#    pairs/triplets/quadruplets of atoms, of which distance/angle should
#    be calculated.
#    Both variants can be setup to consider periodic boundary conditions
#    by setting the `box` or `periodic` parameter, respectively.
#
# In some cases one is interested in the dihedral angles of the peptide