Computational biophysics dihedral

From XuShuwenn/RedAct · View interactive version

Computational biophysics dihedral is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Calculate dihedral angles for protein backbone (N-CA-C-N) using torsion formula from atomic coordinates.

How to use this skill

Skill file

Protein Dihedral Angle Calculation

When to Use

New Section

Task-Protocol Override

Input Format

File /root/input.txt with one coordinate per line (x y z):

Calculation Method

Dihedral angle = angle between planes defined by (N, CA, C) and (CA, C, N')

Use the coordinates from /root/input.txt directly and compute the torsion from those four ordered points; do not infer the angle from a sketch or qualitative geometry alone.

Use torsion formula:

  1. Compute vectors
  2. Compute normals via cross products
  3. Calculate angle between normals using dot product
  4. Adjust sign based on direction

Use the full signed four-point torsion calculation directly for planes (N, CA, C) and (CA, C, N'); do not substitute a simpler bond-angle, unsigned angle-between-planes, or reordered-atom formula.

Use a signed atan2-based torsion formula as the default method so the result naturally lands in the required -180 to +180 range. Keep the computed angle at full precision during calculation; round only when formatting the final line for /root/output.txt.

Prefer a standard-library implementation (math) for vector arithmetic, cross products, dot products, norms, and atan2-based signed torsion unless the environment clearly provides numpy.

Do not assume external packages are installed for this task. If basic arithmetic is sufficient, use built-in Python only.

Prefer a single compact Python script that reads /root/input.txt, maps the points explicitly to N, CA, C, N', computes the signed torsion once with the standard atan2 workflow, writes /root/output.txt, and then verifies the written file. This reduces transcription mistakes in self-contained file tasks.

Before writing the result, perform a quick sanity check:

Output Format

Write to /root/output.txt:

Dihedral angle: XXX.XX degrees

Round to 2 decimal places. Range: -180 to +180 degrees.

Do not change formatting during finalization: keep the exact label Dihedral angle:, include a space before the numeric value and before degrees, and preserve two decimal places even for whole-number angles (for example 180.00).

New Section

Execution Checklist

New Section

Finalization Checklist

Tips

New Section

Minimal Example

from pathlib import Path
from math import atan2, degrees, sqrt


def sub(a, b):
    return [a[i] - b[i] for i in range(3)]


def dot(a, b):
    return sum(a[i] * b[i] for i in range(3))


def cross(a, b):
    return [
        a[1] * b[2] - a[2] * b[1],
        a[2] * b[0] - a[0] * b[2],
        a[0] * b[1] - a[1] * b[0],
    ]


def norm(v):
    return sqrt(dot(v, v))


def scale(v, s):
    return [x * s for x in v]


lines = [line.strip() for line in Path('/root/input.txt').read_text().splitlines() if line.strip()]
if len(lines) != 4:
    raise ValueError('Expected exactly 4 coordinate lines')
pts = [[float(x) for x in line.split()] for line in lines]
N, CA, C, N_next = pts
b0 = sub(CA, N)
b1 = sub(C, CA)
b2 = sub(N_next, C)
n0 = cross(b0, b1)
n1 = cross(b1, b2)
b1_norm = norm(b1)
if norm(n0) == 0 or norm(n1) == 0 or b1_norm == 0:
    raise ValueError('Colinear or repeated atoms; dihedral undefined')

n0u = scale(n0, 1.0 / norm(n0))
n1u = scale(n1, 1.0 / norm(n1))
b1u = scale(b1, 1.0 / b1_norm)
m1 = cross(n0u, b1u)
angle = degrees(atan2(dot(m1, n1u), dot(n0u, n1u)))
Path('/root/output.txt').write_text(f'Dihedral angle: {angle:.2f} degrees\n')

print(Path('/root/output.txt').read_text(), end='')  # verify persisted output
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.