Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6cb2049
added axes_topology as None
ioanaapapa Sep 8, 2026
fdfb2a9
disabled axes topology
ioanaapapa Sep 15, 2026
8be9be4
residue group formation and identification of edge atoms is now indep…
ioanaapapa Nov 10, 2026
a9edb43
topology axes are not disabled anymore
ioanaapapa Nov 11, 2026
1be9bb6
changed calling get_custom_axes (old function name) to get_bonded_vec…
ioanaapapa Nov 11, 2026
089125e
updated unit tests for new residue group (residue of interest + neigh…
ioanaapapa Nov 11, 2026
2332298
updated unit tests
ioanaapapa Nov 12, 2026
be9f2f0
fixed merge conflicts with main post adding axes argument
ioanaapapa Nov 12, 2026
16921ae
updated test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours
ioanaapapa Nov 17, 2026
54d53b8
updated test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours
ioanaapapa Nov 17, 2026
5b0cf1f
fixed terminal residue unit test
ioanaapapa Nov 18, 2026
86d92b9
fixed terminal residue unit test
ioanaapapa Nov 18, 2026
3140aa6
fixed terminal residue unit test
ioanaapapa Nov 19, 2026
464f3ff
updated covariance node unit tests
ioanaapapa Nov 19, 2026
4c18941
updated axes unit tests
ioanaapapa Nov 27, 2026
c42f9f2
Merge branch 'main' into 425-branched-resids-all-atom-MOI-update
ioanaapapa Nov 27, 2026
e4c0eda
merged with updates from main
ioanaapapa Nov 27, 2026
3dd12d3
updated covariance node tests
ioanaapapa Nov 27, 2026
d17cb50
covariance node tests
ioanaapapa Nov 27, 2026
d2980e5
changed documentation to introduce branched residues match labels use…
ioanaapapa Nov 27, 2026
a51587f
new tests for branched residues
ioanaapapa Nov 27, 2026
ff946bd
new tests for branched residues
ioanaapapa Nov 27, 2026
5b9d88e
unit tests
ioanaapapa Nov 27, 2026
2378654
unit tests
ioanaapapa Nov 27, 2026
0ed64d3
get_residue_axes for branched residues unit test
ioanaapapa Nov 27, 2026
385c462
get_residue_axes for branched residues unit test with correct name
ioanaapapa Nov 27, 2026
3dd380a
get_residue_axes for branched residues unit test with correct name
ioanaapapa Nov 27, 2026
33d69de
get_residue_axes for branched residues fixed get_residue_custom_axes …
ioanaapapa Nov 27, 2026
8cdf1ff
get_residue_axes for branched residues fixed get_residue_custom_axes …
ioanaapapa Nov 27, 2026
d1bf09c
test get_ua_axes for branched residues fixed
ioanaapapa Nov 27, 2026
04a9b35
test get_ua_axes for branched residues fixed
ioanaapapa Nov 27, 2026
20d87ca
test get_non_terminal_axes for residue with 3 edges
ioanaapapa Nov 27, 2026
b16c137
Delete example config.yaml added by mistake
ioanaapapa Nov 29, 2026
b42c219
chore(test): restrict coverage measurement to `CodeEntropy` package
harryswift01 Nov 30, 2026
e31a6c9
Merge remote-tracking branch 'origin/main' into 425-branched-resids-a…
harryswift01 Nov 30, 2026
f523292
fixed docstrings for consistent resindex name
ioanaapapa Nov 30, 2026
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
114 changes: 53 additions & 61 deletions CodeEntropy/levels/axes.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just had a look at the topology caching and I'm not sure it's picking up these changes. _has_neighbor_bonds still uses resindex ± 1, and get_residue_axes_from_topology / get_UA_axes_from_topology don't use the edge logic at all, so I think --axes topology and --axes local will give different results for branched residues. Is updating the cached path in scope for this one, or should we pick it up as a follow-up?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't changed the topology caching -- axes topology and --axes local already give different results as the topology caching still uses the principal axes of a residue so we could follow up when we match those, as well. They should however match for UA rotation axes and that's where there might now be an issue that means we need to update it now. I'll have a look.

Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def get_residue_axes(
ValueError:
If the residue selection is empty.
"""
# TODO refine selection so that it will work for branched polymers

index_prev = index + relative_index - 1
index_next = index + relative_index + 1

if residue is None:
residue = data_container.select_atoms(f"resindex {index + relative_index}")
Expand All @@ -126,12 +122,17 @@ def get_residue_axes(
raise ValueError(
f"Empty residue selection for resindex={index + relative_index}"
)

edge_atom_set = data_container.atoms.select_atoms(
f"resindex {index + relative_index} and "
f"(bonded resindex {index_prev} or "
f"resindex {index_next})"
neighbours = data_container.select_atoms(
f"(not resindex {index + relative_index}) and "
f"(bonded resindex {index + relative_index})"
)
edge_atom_set = []
for neighbour in neighbours:
edge = data_container.select_atoms(
f"(bonded resindex {neighbour.resindex}) and "
f"(resindex {index + relative_index})"
)
edge_atom_set.append(edge[0])

if len(edge_atom_set) == 0:
rot_axes, moment_of_inertia = self.get_molecule_axes(residue)
Expand Down Expand Up @@ -203,7 +204,7 @@ def get_residue_axes_from_topology(

return trans_axes, rot_axes, center, moment_of_inertia

def get_UA_axes(self, data_container, index: int, res_position):
def get_UA_axes(self, data_container, index: int, resindex: int):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since get_UA_axes is called for every UA bead in every frame, we're recalculating the same edges for each bead in a residue. Could we work them out once per residue and pass them in? Also, the docstring calls this parameter res_index but it's resindex.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be good. Would it then be best to find the edges in the covariance node, then pass them as an argument to get_res/UA_axes?

"""Compute united-atom-level translational and rotational axes.

The translational and rotational axes at the united-atom level.
Expand Down Expand Up @@ -231,8 +232,8 @@ def get_UA_axes(self, data_container, index: int, res_position):
Molecule and trajectory data.
index (int):
Bead index (ordinal among heavy atoms).
res_position: where the residue of interest is
in data_container
resindex (int):
Index of the residue the UA is in.
Returns:
Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
- trans_axes: Translational axes (3, 3).
Expand All @@ -248,34 +249,26 @@ def get_UA_axes(self, data_container, index: int, res_position):
"""

index = int(index) # UA bead index
heavy_atoms = data_container.select_atoms("mass 2 to 999")
heavy_atoms = data_container.atoms.select_atoms("mass 2 to 999")
# use the same trans axes as the residue level
if len(heavy_atoms) > 1:
if len(data_container.residues) == 1:
if len(data_container.atoms.residues) == 1:
# only the one residue => use principal axes
residue = data_container
trans_center = data_container.atoms.center_of_mass(unwrap=True)
trans_axes = self.get_principal_axes_from_group(data_container.atoms)
else:
# residue of interest has at least one neighbour
if res_position == -1 or res_position == 1:
# look at a terminal residue
if res_position == -1:
# first residue
residue = data_container.residues[0]
resindex = residue.resindex
resindex_next = resindex + 1
edge_atom_set = data_container.select_atoms(
f"resindex {resindex} and bonded resindex {resindex_next}"
)
else:
# last residue
residue = data_container.residues[1]
resindex = residue.resindex
resindex_prev = resindex - 1
edge_atom_set = data_container.select_atoms(
f"resindex {resindex} and bonded resindex {resindex_prev}"
)
residue = data_container.select_atoms(f"resindex {resindex}")
neighbours = data_container.select_atoms(f"not resindex {resindex}")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the edge atoms only depend on the bonds, could we work them out once in the axes topology node and cache them (e.g. edge_indices on ResidueAxesTopology)? That would save redoing the selections every frame and keep the local and topology paths in sync.

edge_atom_set = []
for neighbour in neighbours:
edge = data_container.select_atoms(
f"(bonded resindex {neighbour.resindex}) "
f"and (resindex {resindex})"
)
edge_atom_set.append(edge[0])
if len(edge_atom_set) == 1:
edge_atom = edge_atom_set[0]
trans_center, trans_axes = self.get_terminal_axes(
residue=residue,
Expand All @@ -284,15 +277,6 @@ def get_UA_axes(self, data_container, index: int, res_position):
)
else:
# between 2 residues
residue = data_container.residues[1]
resindex = residue.resindex
resindex_next = resindex + 1
resindex_prev = resindex - 1
edge_atom_set = data_container.select_atoms(
f"resindex {resindex} and "
f"(bonded resindex {resindex_prev} or "
f"resindex {resindex_next})"
)
trans_center, trans_axes = self.get_non_terminal_axes(
residue=residue,
edges=edge_atom_set,
Expand Down Expand Up @@ -608,11 +592,14 @@ def get_terminal_axes(self, residue, edge, dimensions):
def get_non_terminal_axes(self, residue, edges, dimensions):
"""
Compute rotation axes at the residue level/ translation axes at
the UA level for the non-terminal residues in a linear polymer, given the
edge atoms (i.e. heavy atoms bonded to neighbour residues) and
residue of interest. Find the shortest chain between edge atoms: the backbone.
Edges + backbone average position determine the residue rotational axes.
(see get_residue_custom_axes). If the two edge heavy atoms
the UA level for the non-terminal residues, given the edge
atoms (i.e. heavy atoms bonded to neighbour residues) and
residue of interest. If there are two edge atoms, find the shortest chain
between them: the backbone. Edges + backbone average position determine the
residue rotational axes. If there are at least three edge atoms, first two
edge atoms act the same as the edges in the linear case and the average
position of the other edges atoms acts as the backbone centre in the linear
case. (see get_residue_custom_axes). If the two edge heavy atoms
are bonded to each other (i.e. there is no backbone), x-axis is set
along the vector between the edge atom and average position of bonded
atoms, y-axis is arbitrary and z-axis is paralel to the two. This is the
Expand All @@ -626,22 +613,27 @@ def get_non_terminal_axes(self, residue, edges, dimensions):
rot_center: (3,) rotation centre,
rot_axes: (3,3) rotation axes of residue
"""
backbone = self.get_chain(residue, edges[0], edges[1])
backbone_center = np.zeros(3)
if len(backbone) > 0:
for heavy_atom in backbone:
backbone_center += heavy_atom.position
backbone_center /= len(backbone)
rot_center, rot_axes = self.get_residue_custom_axes(
edges.positions, backbone_center
)
if len(edges) == 2:
backbone = self.get_chain(residue, edges[0], edges[1])
backbone_center = np.zeros(3)
if len(backbone) > 0:
for heavy_atom in backbone:
backbone_center += heavy_atom.position
backbone_center /= len(backbone)
rot_center, rot_axes = self.get_residue_custom_axes(
[edges[0].position, edges[1].position], backbone_center
)
else:
rot_center = (edges[0].position + edges[1].position) / 2
rot_axes = self.get_bonded_vector_axes(
a=rot_center,
b_list=[edges[0].position],
c=np.zeros(3),
dimensions=dimensions,
)
else:
rot_center = (edges[0].position + edges[1].position) / 2
rot_axes = self.get_bonded_vector_axes(
a=rot_center,
b_list=[edges[0].position],
c=np.zeros(3),
dimensions=dimensions,
rot_center, rot_axes = self.get_residue_custom_axes(
[edges[0].position, edges[1].position], edges[2].position
)
return rot_center, rot_axes

Expand Down
60 changes: 18 additions & 42 deletions CodeEntropy/levels/nodes/covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,10 @@ def _process_united_atom(
molcount: dict[tuple[int, int], int],
) -> None:
"""Compute united-atom second moments for one molecule.
If there are multiple residues in the molecule, build
residue group and attribute res_position:
- First residue: residue_group contains first two
residues. res_position is -1.
- Last residue: residue_group contains last two
residues. res_position is 1.
- Non-terminal residue: residue_group contains
residue of interest + two neighbours. res_position
is 0.
If there are multiple residues in the molecule,
residue group and retain residue_id for residue of
interest. Each residue group contains the residue of
interest + bonded heavy atoms in neighbouring residues.

Args:
u: Universe-like object used to resolve bead atom indices.
Expand All @@ -211,32 +206,13 @@ def _process_united_atom(
out_torque: Frame-local torque second-moment accumulator, mutated in place.
molcount: Per-residue group sample counters, mutated in place.
"""
relative_res_i = mol.residues[0].resindex
for local_res_i, res in enumerate(mol.residues):
if len(mol.residues) > 1:
if local_res_i == 0:
relative_id = res.resindex
# index relative to first in mda universe
res_position = -1
residue_group = mol.select_atoms(
f"resindex {local_res_i + relative_id} "
f"or resindex {local_res_i + 1 + relative_id}"
)
elif local_res_i == len(mol.residues) - 1:
res_position = 1
residue_group = mol.select_atoms(
f"resindex {local_res_i - 1 + relative_id} "
f"or resindex {local_res_i + relative_id}"
)
else:
res_position = 0
residue_group = mol.select_atoms(
f"resindex {local_res_i - 1 + relative_id} "
f"or resindex {local_res_i + relative_id} "
f"or resindex {local_res_i + 1 + relative_id}"
)
resindex = local_res_i + relative_res_i

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use res.resindex here instead of local_res_i + relative_res_i? It avoids assuming the residues are numbered consecutively, and we wouldn't need to pass relative_res_i through.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember this leading to an issue for systems with multiple molecules but I'll check again

residue_group = mol.select_atoms(f"(bonded resindex {resindex})")

else:
res_position = None
residue_group = res.atoms
bead_key = (mol_id, "united_atom", local_res_i)
bead_idx_list = beads.get(bead_key, [])
Expand All @@ -251,6 +227,7 @@ def _process_united_atom(
u=u,
mol_id=mol_id,
local_res_i=local_res_i,
relative_res_i=relative_res_i,
residue_group=residue_group,
bead_groups=bead_groups,
axes_manager=axes_manager,
Expand All @@ -259,7 +236,6 @@ def _process_united_atom(
force_partitioning=force_partitioning,
axes=axes,
is_highest=is_highest,
res_position=res_position,
)

F, T = self._ft.compute_frame_covariance(force_vecs, torque_vecs)
Expand Down Expand Up @@ -441,6 +417,7 @@ def _build_ua_vectors(
u: Any,
mol_id: int,
local_res_i: int,
relative_res_i: int,
bead_groups: list[Any],
residue_group: Any,
axes_manager: Any,
Expand All @@ -449,14 +426,15 @@ def _build_ua_vectors(
force_partitioning: float,
axes: str,
is_highest: bool,
res_position: int,
) -> tuple[list[np.ndarray], list[np.ndarray]]:
"""Build force and torque vectors for united-atom beads.

Args:
u: Universe-like object used to resolve cached atom indices.
mol_id: Molecule index used in axes-topology lookup keys.
local_res_i: Local residue index used in axes-topology lookup keys.
relative_res_i: Residue index of first residue within ``mol``. This
might differ from residue index of first residue in topology.
bead_groups: Atom groups representing UA beads in a residue.
residue_group: Atom group for the parent residue group.
axes_manager: Axes helper used to select axes, centres, and moments.
Expand All @@ -472,17 +450,15 @@ def _build_ua_vectors(
"""
force_vecs: list[np.ndarray] = []
torque_vecs: list[np.ndarray] = []
for ua_i, bead in enumerate(bead_groups):
if res_position == -1:
residue = residue_group.residues[0]
elif res_position == 0 or res_position == 1:
residue = residue_group.residues[1]
else:
residue = residue_group

resindex = local_res_i + relative_res_i
residue = residue_group.select_atoms(f"resindex {resindex}")

for ua_i, bead in enumerate(bead_groups):
ua_topology = None
if axes == "topology":
ua_topology = axes_topology.ua.get((mol_id, local_res_i, ua_i))

if ua_topology is not None:
trans_axes, rot_axes, center, moi = (
axes_manager.get_UA_axes_from_topology(
Expand All @@ -497,14 +473,14 @@ def _build_ua_vectors(
make_whole(residue_group)
make_whole(bead)
trans_axes, rot_axes, center, moi = axes_manager.get_UA_axes(
residue_group, ua_i, res_position
residue_group, ua_i, resindex
)
elif axes == "local":
# we don't use the topology
make_whole(residue_group)
make_whole(bead)
trans_axes, rot_axes, center, moi = axes_manager.get_UA_axes(
residue_group, ua_i, res_position
residue_group, ua_i, resindex
)

elif axes == "principal":
Expand Down
15 changes: 8 additions & 7 deletions docs/source/science.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ The axes for this transformation are calculated for each bead in each time step.

For the polymer level, the translational and rotational axes are defined as the principal axes of the molecule.

For the residue level, there are three situations.
When the residue is not bonded to any other residues, the translational and rotational axes are the principal axes of the molecule.
When the residue is part of a larger polymer and is not a terminus of that polymer, the translational axes are the principal axes of the polymer, and the rotational axes are defined from the two heavy atoms bonded to neighbour residues (E1,E2) and the average position of all other backbone atoms in the residue (C). The backbone of a residue is defined as the shortest path between the two edge atoms of the residue, i.e.the two heavy atoms bonded to neighbour residues.The centre of rotation (O) is located at the point where the perpendicular from C meets the E1-E2 vector.
When the residue is part of a larger polymer and is a terminus of that polymer, the translational axes are the principal axes of the polymer, and the rotational axes are defined from the heavy atom bonded to a
neighbour residue (E1), the average position of all heavy atoms bonded to E1 (C) and the average position of all other heavy atoms in the residue (E2). The centre of rotation (O) is defined the same as above forthe non-terminal residue case.

For the united atom level, the translational axes are defined as the residue rotational axes and the rotational axes are defined from the average position of the bonds to neighbouring heavy atoms.
For the residue level, translational axes correspond to the principal axes of the molecule. For rotational axes, there are four situations.
1. When the residue is not bonded to any other residues, rotational axes are the same as translational axes.
2. When the residue is part of a polymer and is a terminus of that polymer, rotational axes are defined from the heavy atom bonded to a
neighbour residue (A1), the average position of all heavy atoms bonded to A1 (A3) and the average position of all other heavy atoms in the residue (A2). The centre of rotation (O) is located at the point where the perpendicular from A3 meets the A1-A2 vector. The x axis is then defined along the O-A1 vector, the y axis is defined along the O-A3 vector, and the z axis is defined as perpendicular to the x and y axes.
3. When the residue is part of a larger polymer and is bonded to two other residues within that polymer, rotational axes are defined from the two heavy atoms bonded to neighbour residues (A1,A2) and the average position of all other backbone atoms in the residue (A3). The backbone of a residue is defined as the shortest path between A1 and A2. The axes and centre of rotation are then defined in the same manner as in the terminal residue case.
4. When the residue is part of a larger polymer and is bonded to three or more residues within that polymer, rotational axes are defined from three of the heavy atom bonded to neighbour residues (E1,E2,E3). The axes and centre of rotation are then defined in the same manner as in the cases above.

For the united atom level, the translational axes correspond to the residue rotational axes and the rotational axes are defined based on bonds to neighbouring heavy atoms.
If there are no bonds to other heavy atoms, the principal axes of the molecule are used.

Conformational Entropy
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ markers = [
"slow: long-running regression tests (20-30+ minutes)",
]

[tool.coverage.run]
source = ["CodeEntropy"]

[tool.ruff]
line-length = 88
target-version = "py311"
Expand Down
Loading
Loading