Skip to content

Instantly share code, notes, and snippets.

@hypnopump
Last active February 29, 2024 20:53
Show Gist options
  • Save hypnopump/30d6bfdb8358d3a57d010c9a501fda56 to your computer and use it in GitHub Desktop.
Save hypnopump/30d6bfdb8358d3a57d010c9a501fda56 to your computer and use it in GitHub Desktop.
Dihedral angles from coordinates. Get dihedral angles from protein backbone coordinates. Calculate Phi, Psi from 3D coordinates of (N-term, C-alpha and C-term)
""" Dihedral angles from coordinates. Get dihedral angles from
protein backbone coordinates.
This script takes 4 vectors representing 4 points as input
and returns the dihedral angle between them.
The script was originally developed to calculate dihedral angles (phi,psi)
from protein backbone atoms' coordinates in 3D (N-term, C-alhpa, Cterm).
"""
import numpy as np
def calc_dihedral(u1, u2, u3, u4):
""" Calculate dihedral angle method. From bioPython.PDB
(adapted to np.array)
Calculate the dihedral angle between 4 vectors
representing 4 connected points. The angle is in
[-pi, pi].
"""
a1 = u2 - u1
a2 = u3 - u2
a3 = u4 - u3
v1 = np.cross(a1, a2)
v1 = v1 / (v1 * v1).sum(-1)**0.5
v2 = np.cross(a2, a3)
v2 = v2 / (v2 * v2).sum(-1)**0.5
porm = np.sign((v1 * a3).sum(-1))
rad = np.arccos((v1*v2).sum(-1) / ((v1**2).sum(-1) * (v2**2).sum(-1))**0.5)
if not porm == 0:
rad = rad * porm
return rad
a = np.array([1219.7, 4106.1, -7366.7])
b = np.array([1277.1, 4016.6, -7447.1])
c = np.array([1398.6, 3944.8, -7407.8])
d = np.array([1501.2, 3943.2, -7521.3])
alpha = calc_dihedral(a,b,c,d)
print("Dihedral angle in radians:", alpha, "and degrees:", alpha*180/np.pi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment