Examples » Torsion Angles

TorsionAngle represents a tuple of 4 atoms (a-b-c-d) that correspond to dihedral angle. In addition it might be able to rotate part of structure around b-c bond (where a is static, d is moving).

For standard protein residues angles can be constructed using TorsionAngleFactory:

from pyxmolpp2 import TorsionAngleFactory
from pyxmolpp2 import Degrees

residue48 = frame.molecules[0][48]
print(residue48)

psi_48 = TorsionAngleFactory.psi(residue48)
print(psi_48)
print(psi_48.value().degrees)
A.LYS-48
<pyxmolpp2._core.TorsionAngle object at 0x7fee1758f530>
142.71298452616637
# first residue don't have omega angle
print(TorsionAngleFactory.omega(frame.residues[0]))
None

Let's rotate psi_48 angle:

# Residues 49-76 are affected by this rotation
psi_48.rotate_to(Degrees(150))
print(psi_48.value().degrees)
149.99999999999983
Torsion angle constructor allow two forms:
  1. Read-only torsion angle
  2. Read-write torsion angle
from pyxmolpp2 import TorsionAngle, Atom

r1 = frame.residues[1]
r2 = frame.residues[2]

# Let's create a read-only phi of residue 2
phi_2_ro = TorsionAngle(r1["C"], r2["N"], r2["CA"], r2["C"])

# Check against factory angle:
assert phi_2_ro.value().degrees == TorsionAngleFactory.phi(r2).value().degrees

Attempt to set read-only angle will lead to GeomError:

phi_2_ro.rotate_to(Degrees(-130))
Traceback (most recent call last):
  File "<string>", line 1, in <module>
pyxmolpp2._core.GeomError: TorsionAngle: affected AtomSelection are not set

To make TorsionAngle writeable one need to pass to constructor a selector function which returns a selection of affected atoms by our torsion angle

def affected_phi_atoms(a: Atom, b: Atom, c: Atom, d: Atom):
    from pyxmolpp2 import rId
    return a.molecule.residues.filter(rId > a.residue.id).atoms

phi_2_rw = TorsionAngle(r1["C"], r2["N"], r2["CA"], r2["C"],
                        affected_phi_atoms)

phi_2_rw.rotate_to(Degrees(-130))

print(phi_2_ro.value().degrees)
print(phi_2_rw.value().degrees)
-130.0000000000001
-130.0000000000001