Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CodeEntropy/config/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ class ArgSpec:
help="Type of neighbour search to use. Default is RAD; grid search is also "
"available.",
),
"conf_type": ArgSpec(
type=str,
default="ua_backbone",
help="Dihedral definition to use for conformational entropy."
"Default is ua_backbone.",
),
"parallel_frames": ArgSpec(
type=bool,
default=False,
Expand Down
5 changes: 5 additions & 0 deletions CodeEntropy/levels/dihedrals/conformational_state_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def build_conformational_states(
levels: dict[Any, list[str]],
groups: dict[int, list[Any]],
bin_width: float,
conf_type: str,
frame_selection: FrameSelection,
progress: _RichProgressSink | None = None,
chunk_size: int | None = None,
Expand Down Expand Up @@ -78,6 +79,7 @@ def build_conformational_states(
levels=levels,
groups=groups,
bin_width=bin_width,
conf_type=conf_type,
frame_selection=frame_selection,
chunk_size=chunk_size,
progress=progress,
Expand All @@ -89,6 +91,7 @@ def _build_conformational_states_serial_chunked(
levels: dict[Any, list[str]],
groups: dict[int, list[Any]],
bin_width: float,
conf_type: str,
frame_selection: FrameSelection,
chunk_size: int,
progress: _RichProgressSink | None = None,
Expand Down Expand Up @@ -156,6 +159,7 @@ def _build_conformational_states_serial_chunked(
group_id=group_id,
molecules=molecules,
level_list=level_list,
conf_type=conf_type,
)
tasks = self._build_conformation_chunk_tasks(
topologies=topologies,
Expand Down Expand Up @@ -188,6 +192,7 @@ def _build_conformational_states_serial_chunked(
level_list=level_list,
peaks_ua=peak_data.peaks_ua,
peaks_res=peak_data.peaks_res,
conf_type=conf_type,
)
for observable in observables
]
Expand Down
318 changes: 318 additions & 0 deletions CodeEntropy/levels/dihedrals/dihedral_finder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
"""Dihedral state assignment for conformational entropy.

This module converts selected-frame dihedral angle time series into discrete
conformational state labels. The resulting state labels are used downstream to
compute configurational entropy.

Frame-index contract:
- ``FrameSelection.analysis_indices`` are used for MDAnalysis trajectory access
in the active analysis universe.
- ``Dihedral(...).run(start, stop, step)`` uses frame bounds in the active
analysis-universe index space.
- ``dihedral_results.results.angles`` is always indexed locally from zero.
Never use an absolute/source frame index directly into that result array.
"""

from __future__ import annotations

import logging
from typing import Any

from CodeEntropy.levels.axes import AxesCalculator

logger = logging.getLogger(__name__)

UAKey = tuple[int, int]


class DihedralDefinitions:
"""Build conformational state labels from dihedral angles."""

def __init__(self) -> None:
"""Initializes the analysis helper."""
self._axes = AxesCalculator()

def method_res_bonds(self, data_container: Any, level: str) -> list[Any]:
"""Return dihedral AtomGroups for a container at a given level.
Using bonds between residues (1, 2) and (3,4) to identify 4 points
to cast as a dihedral group at the residue level.

Args:
data_container: MDAnalysis container.
level: Either ``"united_atom"`` or ``"residue"``.

Returns:
List of AtomGroups, each representing a dihedral definition.
"""
atom_groups: list[Any] = []

if level == "united_atom":
selected_indices = {int(index) for index in data_container.indices}

for dihedral in data_container.dihedrals:
dihedral_atoms = dihedral.atoms
dihedral_indices = {int(index) for index in dihedral_atoms.indices}

if len(dihedral_atoms) == 4 and dihedral_indices.issubset(
selected_indices
):
atom_groups.append(dihedral_atoms)

if level == "residue":
num_residues = len(data_container.residues)
if num_residues >= 4:
for residue in range(4, num_residues + 1):
residue1 = data_container.residues[residue - 4]
residue2 = data_container.residues[residue - 3]
residue3 = data_container.residues[residue - 2]
residue4 = data_container.residues[residue - 1]

atom1 = self._atoms_in_source_bonded_to_target(
residue1,
residue2,
)
atom2 = self._atoms_in_source_bonded_to_target(
residue2,
residue1,
)
atom3 = self._atoms_in_source_bonded_to_target(
residue3,
residue4,
)
atom4 = self._atoms_in_source_bonded_to_target(
residue4,
residue3,
)

dihedral_atoms = atom1 + atom2 + atom3 + atom4

if len(dihedral_atoms) == 4:
atom_groups.append(dihedral_atoms)
else:
logger.debug(
"Skipping residue-level dihedral for local residues "
"%s-%s-%s-%s because it produced %d atoms.",
residue - 4,
residue - 3,
residue - 2,
residue - 1,
len(dihedral_atoms),
)

return atom_groups

def method_res_pairs(self, data_container: Any, level: str) -> list[Any]:
"""Return dihedral AtomGroups for a container at a given level.
Using the bond between residues (1, 2) and the centre of geometry of
each residue to identify 4 points to cast as a dihedral group at the
residue level.

Args:
data_container: MDAnalysis container.
level: Either ``"united_atom"`` or ``"residue"``.

Returns:
List of AtomGroups, each representing a dihedral definition.
"""
atom_groups: list[Any] = []

if level == "united_atom":
selected_indices = {int(index) for index in data_container.indices}

for dihedral in data_container.dihedrals:
dihedral_atoms = dihedral.atoms
dihedral_indices = {int(index) for index in dihedral_atoms.indices}

if len(dihedral_atoms) == 4 and dihedral_indices.issubset(
selected_indices
):
atom_groups.append(dihedral_atoms)

if level == "residue":
num_residues = len(data_container.residues)
if num_residues >= 2:
for residue in range(2, num_residues + 1):
residue1 = data_container.residues[residue - 2]
index1 = data_container.residues.resindices[residue - 2]
residue2 = data_container.residues[residue - 1]
index2 = data_container.residues.resindices[residue - 1]

center = residue1.atoms.center_of_geometry()
atom_search = data_container.select_atoms(
f"point {center[0]} {center[1]} {center[2]} 1.5"
f" and resindex {index1}"
)
atom1 = atom_search[0]
atom2 = self._atoms_in_source_bonded_to_target(
residue1,
residue2,
)
atom3 = self._atoms_in_source_bonded_to_target(
residue2,
residue1,
)
center = residue2.atoms.center_of_geometry()
atom_search = data_container.select_atoms(
f"point {center[0]} {center[1]} {center[2]} 1.5"
f" and resindex {index2}"
)
atom4 = atom_search[0]

dihedral_atoms = atom1 + atom2 + atom3 + atom4

if len(dihedral_atoms) == 4:
atom_groups.append(dihedral_atoms)
else:
logger.debug(
"Skipping residue-level dihedral for local residues "
"%s-%s because it produced %d atoms.",
residue - 2,
residue - 1,
len(dihedral_atoms),
)

return atom_groups

def method_res_points(self, data_container: Any, level: str) -> list[Any]:
"""Return dihedral AtomGroups for a container at a given level.
At the residue level, for each residue a point is defined as the
atom nearest the average position of the edge atoms (bonded to an atom
in a different residue). These points are then used for residue level
dihedral groups.

Args:
data_container: MDAnalysis container (AtomGroup/Universe).
level: Either "united_atom" or "residue".

Returns:
List of AtomGroups (each representing a dihedral definition).
"""
atom_groups: list[Any] = []

if level == "united_atom":
selected_indices = {int(index) for index in data_container.indices}

for dihedral in data_container.dihedrals:
dihedral_atoms = dihedral.atoms
dihedral_indices = {int(index) for index in dihedral_atoms.indices}

if len(dihedral_atoms) == 4 and dihedral_indices.issubset(
selected_indices
):
atom_groups.append(dihedral_atoms)

if level == "residue":
num_residues = len(data_container.residues)
point: list[Any] = {}
if num_residues >= 4:
for residue in range(num_residues):
atom_search = []
index = data_container.residues.resindices[residue]
edges = data_container.select_atoms(
f"resindex {index} and bonded not resindex {index}"
)
if len(edges) > 1:
center = edges.center_of_geometry()
atom_search = data_container.select_atoms(
f"point {center[0]} {center[1]} {center[2]} 1.3"
f" and resindex {residue}"
)
if len(atom_search) == 0:
point[residue] = edges[0]
else:
point[residue] = atom_search[0]

for index in range(3, num_residues):
atom1 = point[index - 3]
atom2 = point[index - 2]
atom3 = point[index - 1]
atom4 = point[index]
atom_groups.append(atom1 + atom2 + atom3 + atom4)

return atom_groups

def method_ua_backbone(self, data_container: Any, level: str) -> list[Any]:
"""Return dihedral AtomGroups for a container at a given level.
For the residue level, use the heavy atoms from the backbone as the set
from which the dihedrals are found (gives secondary structure states).

Args:
data_container: MDAnalysis container (AtomGroup/Universe).
level: Either "united_atom" or "residue".

Returns:
List of AtomGroups (each representing a dihedral definition).
"""
atom_groups: list[Any] = []

if level == "united_atom":
selected_indices = {int(index) for index in data_container.indices}

for dihedral in data_container.dihedrals:
dihedral_atoms = dihedral.atoms
dihedral_indices = {int(index) for index in dihedral_atoms.indices}

if len(dihedral_atoms) == 4 and dihedral_indices.issubset(
selected_indices
):
atom_groups.append(dihedral_atoms)

if level == "residue":
num_residues = len(data_container.residues)
backbone = data_container.select_atoms("name H and not name H")
if num_residues >= 2:
for res_counter in range(num_residues):
residue = data_container.residues[res_counter]
index = data_container.residues.resindices[res_counter]
edges = data_container.select_atoms(
f"resindex {index} and bonded not resindex {index}"
)
if len(edges) == 1:
backbone += edges
elif len(edges) == 2:
chain = self._axes.get_chain(residue, edges[0], edges[1])
for atom in chain:
backbone += atom

logger.debug("backbone = %s", backbone)
dihedrals = backbone.dihedrals
for d in dihedrals:
atom_groups.append(d.atoms)

return atom_groups

@staticmethod
def _atoms_in_source_bonded_to_target(
source_residue: Any,
target_residue: Any,
) -> Any:
"""Return source-residue atoms bonded to atoms in a target residue.

This helper is used when constructing residue-level dihedral definitions
from lightweight molecule AtomGroups. It selects atoms from the source
residue that are bonded to any atom in the target residue without using
global ``resindex`` selection strings.

Args:
source_residue: Residue whose atoms should be tested for bonds.
target_residue: Adjacent residue providing the target bonded atoms.

Returns:
MDAnalysis AtomGroup containing atoms from ``source_residue`` that are
bonded to at least one atom in ``target_residue``. If no matching
atoms are found, an empty AtomGroup is returned.
"""
source_atoms = source_residue.atoms
target_indices = {int(index) for index in target_residue.atoms.indices}
selected_indices: list[int] = []

for atom in source_atoms:
bonded_atoms = getattr(atom, "bonded_atoms", None)
if bonded_atoms is None:
continue

bonded_indices = {int(index) for index in bonded_atoms.indices}
if bonded_indices.intersection(target_indices):
selected_indices.append(int(atom.index))

return source_atoms.universe.atoms[selected_indices]
3 changes: 3 additions & 0 deletions CodeEntropy/levels/dihedrals/peak_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ def _reduce_angle_observables_to_histograms(
target_res[dihedral_index] + counts[dihedral_index]
)

logger.debug("Histogram UA: %s", hist_ua)
logger.debug("Histogram Res: %s", hist_res)

return DihedralHistogramData(
num_residues=num_residues,
num_dihedrals_ua=num_dihedrals_ua,
Expand Down
Loading
Loading