Repository navigation
Introduce Axes for Branched Residues #502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6cb2049
fdfb2a9
8be9be4
a9edb43
1be9bb6
089125e
2332298
be9f2f0
16921ae
54d53b8
5b0cf1f
86d92b9
3140aa6
464f3ff
4c18941
c42f9f2
e4c0eda
3dd12d3
d17cb50
d2980e5
a51587f
ff946bd
5b9d88e
2378654
0ed64d3
385c462
3dd380a
33d69de
8cdf1ff
d1bf09c
04a9b35
20d87ca
b16c137
b42c219
e31a6c9
f523292
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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}") | ||
|
|
@@ -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) | ||
|
|
@@ -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): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
@@ -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). | ||
|
|
@@ -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}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_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, | ||
|
|
@@ -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, | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we just use
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, []) | ||
|
|
@@ -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, | ||
|
|
@@ -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) | ||
|
|
@@ -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, | ||
|
|
@@ -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. | ||
|
|
@@ -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( | ||
|
|
@@ -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": | ||
|
|
||
There was a problem hiding this comment.
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_bondsstill usesresindex ± 1, andget_residue_axes_from_topology/get_UA_axes_from_topologydon't use the edge logic at all, so I think--axes topologyand--axes localwill 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?There was a problem hiding this comment.
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.