diff --git a/CodeEntropy/levels/axes.py b/CodeEntropy/levels/axes.py index 87d8832a..95dedf4c 100644 --- a/CodeEntropy/levels/axes.py +++ b/CodeEntropy/levels/axes.py @@ -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): """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 + res_index (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}") + 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 diff --git a/CodeEntropy/levels/nodes/covariance.py b/CodeEntropy/levels/nodes/covariance.py index 8e203b56..96994aac 100644 --- a/CodeEntropy/levels/nodes/covariance.py +++ b/CodeEntropy/levels/nodes/covariance.py @@ -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 + 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,7 +426,6 @@ 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. @@ -457,6 +433,8 @@ def _build_ua_vectors( 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": diff --git a/config.yaml b/config.yaml new file mode 100644 index 00000000..83d26a3d --- /dev/null +++ b/config.yaml @@ -0,0 +1,12 @@ +--- + +run1: + top_traj_file: + - "../thioredoxin.prmtop" + - "../thioredoxin_1_imaged.nc" + selection_string: "resid 29-34" + start: 0 + end: 100 + step: 1 + combined_forcetorque: False + axes: "local" diff --git a/docs/source/science.rst b/docs/source/science.rst index b42af0ac..7a786967 100644 --- a/docs/source/science.rst +++ b/docs/source/science.rst @@ -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 diff --git a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py index 302b0e79..ac18473c 100644 --- a/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py +++ b/tests/unit/CodeEntropy/levels/nodes/test_covariance_node.py @@ -12,11 +12,12 @@ class FakeAtomGroup: """Small AtomGroup-like object for covariance-node tests.""" - def __init__(self, name="ag", *, length=1): + def __init__(self, name="ag", *, length=1, select_map=None): self.name = name self._length = length self.indices = np.arange(length) self.atoms = self + self._select_map = dict(select_map or {}) def __len__(self): return self._length @@ -27,6 +28,9 @@ def principal_axes(self): def center_of_mass(self, unwrap=False): return np.array([1.0, 2.0, 3.0]) + def select_atoms(self, query: str): + return self._select_map.get(query, FakeAtomGroup([])) + class FakeResidue: """Small residue-like object.""" @@ -167,7 +171,7 @@ def test_process_united_atom_updates_outputs_and_molcount(): out_force = {"ua": {}, "res": {}, "poly": {}} out_torque = {"ua": {}, "res": {}, "poly": {}} molcount = {} - + mol.residues[0].resindex = 0 node._process_united_atom( u=universe, mol=mol, @@ -195,7 +199,7 @@ def test_process_united_atom_returns_when_no_beads_or_empty_atom_groups(): mol = FakeMolecule(n_residues=1) out_force = {"ua": {}, "res": {}, "poly": {}} out_torque = {"ua": {}, "res": {}, "poly": {}} - + mol.residues[0].resindex = 7 node._process_united_atom( u=FakeUniverse([mol]), mol=mol, @@ -434,6 +438,7 @@ def test_build_ua_vectors_uses_local_axes(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -442,7 +447,6 @@ def test_build_ua_vectors_uses_local_axes(): force_partitioning=0.5, axes="local", is_highest=True, - res_position=None, ) assert make_whole.call_count == 2 @@ -457,6 +461,15 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): u = FakeUniverse([]) ua_topology = object() axes_topology = SimpleNamespace(ua={(3, 4, 0): ua_topology}) + residue_group = FakeAtomGroup("res") + residue = FakeResidue() + residue.atoms.name = residue_group.name + + def _select_atoms(q): + if q == "resindex 7": + return residue + + residue_group.select_atoms = _select_atoms axes_manager.get_UA_axes_from_topology.return_value = ( np.eye(3), @@ -471,15 +484,15 @@ def test_build_ua_vectors_uses_cached_axes_topology_when_available(): u=u, mol_id=3, local_res_i=4, + relative_res_i=3, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeAtomGroup("res"), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=axes_topology, box=None, force_partitioning=0.5, axes="topology", is_highest=True, - res_position=None, ) assert len(force_vecs) == 1 @@ -510,6 +523,7 @@ def test_build_ua_vectors_uses_principal_axes(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -518,7 +532,6 @@ def test_build_ua_vectors_uses_principal_axes(): force_partitioning=0.5, axes="principal", is_highest=False, - res_position=None, ) assert make_whole.call_count == 2 @@ -772,21 +785,28 @@ def test_build_ua_vectors_multiple_residues_first_residue(): ) node._ft.get_weighted_forces = MagicMock(return_value=np.array([1.0, 0.0, 0.0])) node._ft.get_weighted_torques = MagicMock(return_value=np.array([0.0, 1.0, 0.0])) + residue = FakeResidue() + + def _select_atoms(q): + if q == "resindex 0": + return residue + residue_group = FakeMolecule(n_residues=2) + residue_group.select_atoms = _select_atoms with patch("CodeEntropy.levels.nodes.covariance.make_whole") as make_whole: force_vecs, torque_vecs = node._build_ua_vectors( u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeMolecule(n_residues=2), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=None, box=None, force_partitioning=0.5, axes="local", is_highest=True, - res_position=-1, ) assert make_whole.call_count == 2 @@ -804,21 +824,29 @@ def test_build_ua_vectors_multiple_residues_middle_residue(): ) node._ft.get_weighted_forces = MagicMock(return_value=np.array([1.0, 0.0, 0.0])) node._ft.get_weighted_torques = MagicMock(return_value=np.array([0.0, 1.0, 0.0])) + residue = FakeResidue() + + def _select_atoms(q): + if q == "resindex 1": + return residue + + residue_group = FakeMolecule(n_residues=3) + residue_group.select_atoms = _select_atoms with patch("CodeEntropy.levels.nodes.covariance.make_whole") as make_whole: force_vecs, torque_vecs = node._build_ua_vectors( u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=1, bead_groups=[FakeAtomGroup("ua")], - residue_group=FakeMolecule(n_residues=3), + residue_group=residue_group, axes_manager=axes_manager, axes_topology=None, box=None, force_partitioning=0.5, axes="local", is_highest=True, - res_position=0, ) assert make_whole.call_count == 2 @@ -842,6 +870,7 @@ def test_build_ua_vectors_topology_falls_back_to_local_when_key_missing(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=axes_manager, @@ -850,7 +879,6 @@ def test_build_ua_vectors_topology_falls_back_to_local_when_key_missing(): force_partitioning=0.5, axes="topology", is_highest=True, - res_position=None, ) axes_manager.get_UA_axes.assert_called_once() @@ -865,6 +893,7 @@ def test_build_ua_vectors_raises_for_unknown_axes(): u=FakeUniverse([]), mol_id=0, local_res_i=0, + relative_res_i=0, bead_groups=[FakeAtomGroup("ua")], residue_group=FakeAtomGroup("res"), axes_manager=MagicMock(), @@ -873,7 +902,6 @@ def test_build_ua_vectors_raises_for_unknown_axes(): force_partitioning=0.5, axes="bogus", is_highest=True, - res_position=None, ) diff --git a/tests/unit/CodeEntropy/levels/test_axes.py b/tests/unit/CodeEntropy/levels/test_axes.py index a58b1d49..b0acf066 100644 --- a/tests/unit/CodeEntropy/levels/test_axes.py +++ b/tests/unit/CodeEntropy/levels/test_axes.py @@ -182,7 +182,7 @@ def _sel(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 2.0, 3.0])), ) - trans, rot, center, moi = ax.get_UA_axes(u, index=0, res_position=None) + trans, rot, center, moi = ax.get_UA_axes(u, index=0, resindex=3) assert np.allclose(trans, np.eye(3)) assert np.allclose(rot, np.eye(3)) @@ -216,7 +216,7 @@ def _sel(q): monkeypatch.setattr(ax, "get_bonded_axes", lambda **kwargs: (None, None)) with pytest.raises(ValueError): - ax.get_UA_axes(u, index=5, res_position=None) + ax.get_UA_axes(u, index=5, resindex=0) def test_get_bonded_vector_axes_degenerate_axis1_raises(): @@ -749,7 +749,6 @@ def test_get_UA_axes_multiple_heavy_atoms_uses_custom_principal_axes(monkeypatch ], positions=np.array([[0, 0, 0], [1, 0, 0]], dtype=float), ) - system_atom = _atom(index=0, mass=12.0, pos=[0, 0, 0]) heavy_atom_selection = _FakeAtomGroup( [system_atom], positions=np.array([[0, 0, 0]], dtype=float) @@ -770,7 +769,7 @@ def select_atoms(self, q): data_container = MagicMock() data_container.atoms = _Atoms() - data_container.residues.__len__.return_value = 1 + data_container.atoms.residues = [heavy_atoms] data_container.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90], dtype=float) _FakeAtomGroup.atoms = heavy_atom_selection @@ -790,7 +789,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, center, moi = ax.get_UA_axes( - data_container, index=0, res_position=None + data_container, index=0, resindex=0 ) assert trans_axes.shape == (3, 3) @@ -1360,13 +1359,30 @@ def test_get_residue_bonded_axes_1_heavy_atom_backbone_2neighbours(monkeypatch): ], positions=np.array([[1.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=float), ) + backbone_atom = _atom(index=9, mass=12.0, pos=[0, 1, 0]) + neighbours = _FakeAtomGroup( + [ + _atom(index=7, mass=14.0, pos=[1, 1, 1]), + _atom(index=11, mass=12.0, pos=[2, 2, 2]), + ], + positions=np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], dtype=float), + ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.endswith("(bonded resindex 0 or resindex 2)"): - return edge_atom_set + if q == "resindex 1": + return residue + if q.startswith("(not resindex 1"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [edge_atom_set[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [edge_atom_set[1]] u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -1409,12 +1425,28 @@ def test_get_residue_bonded_axes_multiple_heavy_atoms_backbone_2neighbours(monke _atom(index=9, mass=12.0, pos=[0, 1, 0]), _atom(index=10, mass=14.0, pos=[0, 2, 0]), ] + neighbours = _FakeAtomGroup( + [ + _atom(index=7, mass=14.0, pos=[1, 1, 1]), + _atom(index=13, mass=12.0, pos=[2, 2, 2]), + ], + positions=np.array([[1.0, 1.0, 1.0], [2.0, 2.0, 2.0]], dtype=float), + ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.endswith("(bonded resindex 0 or resindex 2)"): - return edge_atom_set + if q == "resindex 1": + return residue + if q.startswith("(not resindex 1"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [edge_atom_set[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [edge_atom_set[1]] u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -1449,6 +1481,7 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) residue = u.select_atoms("resindex 0") residue.__len__.return_value = 3 + uas = _FakeAtomGroup( [ _atom(index=0, mass=12.0, pos=[1, -1, 0]), @@ -1456,18 +1489,30 @@ def test_get_residue_bonded_axes_terminal_resid(monkeypatch): _atom(index=2, mass=12.0, pos=[0, 0, 0]), ] ) + neighbour = _FakeAtomGroup( + [ + _atom(index=3, mass=12.0, pos=[-1, -1, 0]), + ] + ) + edge = uas[2] + neighbour[0].resindex = 1 def _select_atoms(q): if q == "mass 2 to 999": return uas + if q == "resindex 0": + return residue + if q.startswith("(not resindex 0"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": + return [edge] if q.startswith("(mass 2 to 999) and bonded"): return [uas[1]] - if q.startswith("resindex 0 and (bonded resindex"): - return [uas[2]] monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms @@ -1494,37 +1539,48 @@ def test_get_ua_axes_bonded_axes_2neighbours_1_heavy_atom_backbone(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 3 - residue = residue_group.residues[1] + residue = residue_group.select_atoms("resindex 1") + residue.__len__.return_value = 5 heavy_atoms = _FakeAtomGroup( [ + _atom(index=2, mass=12.0, pos=(1, 1, 1)), _atom(index=3, mass=12.0, pos=(1, 0, 0)), _atom(index=5, mass=12.0, pos=(0, 1, 0)), _atom(index=7, mass=12.0, pos=(0, 0, 1)), + _atom(index=8, mass=12.0, pos=(2, 2, 2)), ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float), ) - - edge_atoms = _FakeAtomGroup( + neighbours = _FakeAtomGroup( [ - _atom(index=3, mass=12.0, pos=(1, 0, 0)), - _atom(index=7, mass=12.0, pos=(0, 0, 1)), + _atom(index=1, mass=12.0, pos=(0, -1, 0)), + _atom(index=9, mass=12.0, pos=(2, 3, 2)), ], - positions=np.array([[1, 0, 0], [0, 0, 1]], dtype=float), + positions=np.array([[0, -1, 0], [2, 3, 2]], dtype=float), ) def _select_atoms(q): + if q == "resindex 1": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("resindex "): - return edge_atoms if q.startswith("index "): return [heavy_atoms[1]] + if q == ("not resindex 1"): + return neighbours + if q == ("(bonded resindex 0) and (resindex 1)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 2) and (resindex 1)"): + return [heavy_atoms[3]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: [heavy_atoms[1]]) monkeypatch.setattr( @@ -1534,7 +1590,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=1, res_position=0 + data_container=residue_group, index=1, resindex=1 ) assert trans_axes.shape == (3, 3) @@ -1547,7 +1603,7 @@ def test_get_ua_axes_bonded_axes_2neighbours_multiple_heavy_atoms_backbone(monke ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 3 - residue = residue_group.residues[1] + residue = residue_group.select_atoms("resindex 2") heavy_atoms = _FakeAtomGroup( [ @@ -1567,16 +1623,34 @@ def test_get_ua_axes_bonded_axes_2neighbours_multiple_heavy_atoms_backbone(monke positions=np.array([[1, 0, 0], [0, 1, 1]], dtype=float), ) + neighbours = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(0, -1, 0)), + _atom(index=10, mass=12.0, pos=(2, 3, 2)), + ], + positions=np.array([[0, -1, 0], [2, 3, 2]], dtype=float), + ) + def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("resindex "): - return edge_atoms - if q.startswith("index "): + if q == "resindex 2": + return residue + if q == ("not resindex 2"): + return neighbours + if q == ("(bonded resindex 1) and (resindex 2)"): + return [edge_atoms[0]] + if q == ("(bonded resindex 3) and (resindex 2)"): + return [edge_atoms[1]] + if q == ("index 5"): return [heavy_atoms[1]] + neighbours[0].resindex = 1 + neighbours[1].resindex = 3 + residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, "get_chain", lambda residue, first, last: [heavy_atoms[1], heavy_atoms[2]] @@ -1588,7 +1662,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=1, res_position=0 + data_container=residue_group, index=1, resindex=2 ) assert trans_axes.shape == (3, 3) @@ -1610,23 +1684,34 @@ def test_get_ua_axes_bonded_axes_first_resid(monkeypatch): ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=3, mass=12.0, pos=(1, 1, 1)), + ], + ) + neighbour[0].resindex = 1 + residue.atoms[0] = heavy_atoms[0] residue.atoms[0].position = heavy_atoms[0].position - edge_atom_set = [heavy_atoms[2]] bonded_atoms = [heavy_atoms[1]] def _select_atoms(q): + if q == "resindex 0": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == ("index 0"): return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms + if q == "not resindex 0": + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": + return [heavy_atoms[2]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1635,7 +1720,7 @@ def _select_atoms(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 1.0, 1.0])), ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=-1 + data_container=residue_group, index=0, resindex=0 ) assert trans_axes.shape == (3, 3) @@ -1648,32 +1733,43 @@ def test_get_ua_axes_bonded_axes_last_resid(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() residue_group.__len__ = 2 - residue = residue_group.residues[0] + residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + _atom(index=3, mass=12.0, pos=(0, 0, 1)), ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + ], + ) + neighbour[0].resindex = 1 + residue.atoms[0] = heavy_atoms[0] residue.atoms[0].position = heavy_atoms[0].position - edge_atom_set = [heavy_atoms[0]] bonded_atoms = [heavy_atoms[1]] def _select_atoms(q): + if q == "resindex 2": + return residue if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == ("index 1"): return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms + if q == "not resindex 2": + return neighbour + if q == "(bonded resindex 1) and (resindex 2)": + return [heavy_atoms[0]] residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1682,7 +1778,7 @@ def _select_atoms(q): lambda system, atom, dimensions: (np.eye(3), np.array([1.0, 1.0, 1.0])), ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=1 + data_container=residue_group, index=0, resindex=2 ) assert trans_axes.shape == (3, 3) @@ -1734,7 +1830,7 @@ def test_get_UA_axes_raises_when_only_rot_axes_fail(monkeypatch): _atom(index=0, mass=12.0, pos=(1, 0, 0)), _atom(index=1, mass=12.0, pos=(0, 1, 0)), ] - u.residues = [heavy_atoms] + u.atoms.residues = [heavy_atoms] def _sel(q): if q == "mass 2 to 999": @@ -1749,36 +1845,48 @@ def _sel(q): monkeypatch.setattr(ax, "get_bonded_axes", lambda **kwargs: (None, None)) with pytest.raises(ValueError): - ax.get_UA_axes(u, index=0, res_position=None) + ax.get_UA_axes(u, index=0, resindex=0) def test_get_ua_axes_bonded_terminal_2_points(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() - residue_group.__len__ = 2 - residue = residue_group.residues[1] + residue = residue_group.atoms.residues[1] + residue_group.atoms.residues.__len__.return_value = 2 heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - ], + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + ] ) - edge_atom_set = [heavy_atoms[0]] + bonded_atoms = [heavy_atoms[1]] + neighbour = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + ], + ) + neighbour[0].resindex = 1 def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == "index 1": + return [heavy_atoms[0]] + if q == "resindex 2": + return residue + if q == "not resindex 2": + return neighbour + if q == "(bonded resindex 1) and (resindex 2)": return [heavy_atoms[0]] - if q.startswith("resindex "): - return edge_atom_set if q.startswith("(mass 2 to 999) and bonded index "): return bonded_atoms residue_group.select_atoms.side_effect = _select_atoms + residue_group.atoms.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = _select_atoms monkeypatch.setattr( ax, @@ -1791,7 +1899,7 @@ def _select_atoms(q): ) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=1 + data_container=residue_group, index=0, resindex=2 ) assert np.allclose(trans_axes, 2 * np.eye(3)) @@ -1803,24 +1911,39 @@ def _select_atoms(q): def test_get_ua_axes_non_terminal_2_atoms(monkeypatch): ax = AxesCalculator() residue_group = MagicMock() - residue_group.__len__ = 3 + residue_group.atoms.residues.__len__.return_value = 3 residue = residue_group.residues[1] heavy_atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + ], + ) + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=3, mass=12.0, pos=(3, 3, 4)), ], ) + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): if q == "mass 2 to 999": # return heavy atoms group return heavy_atoms - if q.startswith("index"): + if q == "resindex 1": + return residue + if q == "index 1": return [heavy_atoms[0]] - if q.startswith("resindex "): - return heavy_atoms + if q == "not resindex 1": + return neighbours + if q == ("(bonded resindex 0) and (resindex 1)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 2) and (resindex 1)"): + return [heavy_atoms[1]] + residue_group.atoms.select_atoms.side_effect = _select_atoms residue_group.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms monkeypatch.setattr( @@ -1833,7 +1956,7 @@ def _select_atoms(q): ) monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: []) trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=0 + data_container=residue_group, index=0, resindex=1 ) assert np.allclose(trans_axes, 2 * np.eye(3)) @@ -1849,22 +1972,41 @@ def test_get_residue_axes_non_terminal_2_atoms(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) residue = u.select_atoms("resindex 5") residue.__len__.return_value = 2 + u.atoms.residues.__len__.return_value = 3 monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + uas = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), ], ) + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=3, mass=12.0, pos=(3, 3, 4)), + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + def _select_atoms(q): if q == "mass 2 to 999": + # return heavy atoms group return uas - if q.startswith("resindex 5 and (bonded resindex"): - return uas + if q == "resindex 5": + return residue + if q.startswith("(not resindex 5)"): + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = residue monkeypatch.setattr(ax, "get_chain", lambda residue, first, last: []) @@ -1902,22 +2044,34 @@ def test_get_residue_axes_terminal_2_atoms(monkeypatch): [ _atom(index=0, mass=12.0, pos=(1, 0, 0)), _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), ], ) monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) + neighbour = _FakeAtomGroup( + [ + _atom(index=2, mass=12.0, pos=(2, 1, 1)), + ], + ) + neighbour[0].resindex = 1 + bonded_atoms = [uas[0]] + def _select_atoms(q): if q == "mass 2 to 999": + # return heavy atoms group return uas - if q.startswith("resindex 0 and (bonded resindex"): - # the edge atom - return [uas[2]] + if q == "resindex 0": + return residue + if q.startswith("(not resindex 0)"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": + return [uas[1]] if q.startswith("(mass 2 to 999) and bonded index "): - return uas[0:2] + return bonded_atoms + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms @@ -1938,7 +2092,7 @@ def _select_atoms(q): ) assert np.allclose(trans_axes, np.eye(3)) assert np.allclose(rot_axes, 2 * np.eye(3)) - assert np.allclose(rot_center, [0, 0, 1]) + assert np.allclose(rot_center, [0, 1, 0]) assert np.allclose(moi, [1, 1, 1]) @@ -1954,6 +2108,12 @@ def test_get_res_axes_terminal_1_atom(monkeypatch): _atom(index=0, mass=12.0, pos=(1, 0, 0)), ], ) + neighbour = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(2, 1, 1)), + ], + ) + neighbour[0].resindex = 1 monkeypatch.setattr( ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) ) @@ -1961,11 +2121,16 @@ def test_get_res_axes_terminal_1_atom(monkeypatch): def _select_atoms(q): if q == "mass 2 to 999": return uas + if q == "resindex 0": + return residue if q.startswith("(mass 2 to 999) and bonded index "): return [] - if q.startswith("resindex "): + if q.startswith("(not resindex 0)"): + return neighbour + if q == "(bonded resindex 1) and (resindex 0)": return [uas[0]] + u.select_atoms.side_effect = _select_atoms u.atoms.select_atoms.side_effect = _select_atoms residue.select_atoms.side_effect = _select_atoms residue.atoms.select_atoms.side_effect = _select_atoms @@ -1988,57 +2153,6 @@ def _select_atoms(q): assert np.allclose(moi, [1, 1, 1]) -def test_get_UA_axes_terminal_1_atom(monkeypatch): - ax = AxesCalculator() - residue_group = MagicMock() - residue_group.__len__ = 2 - residue = residue_group.residues[1] - heavy_atoms = _FakeAtomGroup( - [ - _atom(index=0, mass=12.0, pos=(1, 1, 1)), - _atom(index=1, mass=12.0, pos=(3, 3, 3)), - ], - ) - - def _select_atoms_data_container(q): - if q == "mass 2 to 999": - # return heavy atoms group - return heavy_atoms - if q.startswith("resindex "): - return [heavy_atoms[0]] - - def _select_atoms_residue(q): - if q == "mass 2 to 999": - # return heavy atoms group - return [heavy_atoms[0]] - if q.startswith("index"): - return [heavy_atoms[0]] - if q.startswith("(mass 2 to 999) and bonded "): - return [] - - residue_group.select_atoms.side_effect = _select_atoms_data_container - residue_group.atoms.select_atoms.side_effect = _select_atoms_data_container - residue.select_atoms.side_effect = _select_atoms_residue - residue.atoms.select_atoms.side_effect = _select_atoms_residue - monkeypatch.setattr( - ax, - "get_bonded_axes", - lambda system, atom, dimensions: (np.eye(3), 3 * np.eye(3)), - ) - monkeypatch.setattr( - ax, "get_principal_axes_from_group", lambda *_a, **_k: 2 * np.eye(3) - ) - - trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( - data_container=residue_group, index=0, res_position=-1 - ) - - assert np.allclose(trans_axes, 2 * np.eye(3)) - assert np.allclose(rot_axes, np.eye(3)) - assert np.allclose(rot_center, [1, 1, 1]) - assert np.allclose(moi, 3 * np.eye(3)) - - def test_get_terminal_axes_1point(monkeypatch): ax = AxesCalculator() residue = MagicMock() @@ -2188,22 +2302,37 @@ def test_get_residue_axes_all_atom_MOI(monkeypatch): monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) atoms = _FakeAtomGroup( [ - _atom(index=0, mass=12.0, pos=(1, 0, 0)), - _atom(index=1, mass=12.0, pos=(0, 1, 0)), - _atom(index=2, mass=12.0, pos=(0, 0, 1)), - _atom(index=3, mass=1.0, pos=(0, 0, 0)), - _atom(index=4, mass=1.0, pos=(1, 1, 1)), + _atom(index=1, mass=12.0, pos=(1, 0, 0)), + _atom(index=2, mass=12.0, pos=(0, 1, 0)), + _atom(index=3, mass=12.0, pos=(0, 0, 1)), + _atom(index=4, mass=1.0, pos=(0, 0, 0)), + _atom(index=5, mass=1.0, pos=(1, 1, 1)), ], positions=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [1, 1, 1]]), ) + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(1, 2, 1)), + _atom(index=6, mass=12.0, pos=(3, 3, 4)), + ], + ) residue.atoms.positions = atoms.positions residue.atoms.masses = atoms.masses + neighbours[0].resindex = 0 + neighbours[1].resindex = 2 def _select_atoms(q): - if q.startswith("resindex"): - return atoms[0:2] + if q == ("resindex 1"): + return residue + if q.startswith("(not resindex 1)"): + return neighbours + if q == "(bonded resindex 0) and (resindex 1)": + return [atoms[0]] + if q == "(bonded resindex 2) and (resindex 1)": + return [atoms[2]] u.atoms.select_atoms.side_effect = _select_atoms + u.select_atoms.side_effect = _select_atoms monkeypatch.setattr(ax, "get_principal_axes_from_group", lambda _ag: 2 * np.eye(3)) monkeypatch.setattr( ax, @@ -2218,3 +2347,167 @@ def _select_atoms(q): assert np.allclose(rot_axes, np.eye(3)) assert np.allclose(rot_center, [1, 0, 0]) assert np.allclose(moi, [26, 38, 38]) + + +def test_get_residue_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + monkeypatch.setattr( + ax, "get_principal_axes_from_group", lambda *_a, **_k: np.eye(3) + ) + + uas = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass=16.0, pos=(4, 4, 4)), + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index=6, mass=16.0, pos=(4, 5, 4)), + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return uas + if q == "resindex 5": + return residue + if q.startswith("(not resindex 5)"): + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [uas[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [uas[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [uas[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = residue + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(3)) + ) + monkeypatch.setattr( + ax, + "get_custom_residue_moment_of_inertia", + lambda center_of_mass, positions, masses, custom_rot_axes, dimensions: np.array( + [1, 1, 1] + ), + ) + + trans_axes, rot_axes, rot_center, moi = ax.get_residue_axes( + data_container=u, + index=5, + relative_index=0, + ) + + assert np.allclose(trans_axes, np.eye(3)) + assert np.allclose(rot_axes, 2 * np.eye(3)) + assert np.allclose(rot_center, [2, 2, 2]) + assert np.allclose(moi, [1, 1, 1]) + + +def test_get_ua_axes_branched(monkeypatch): + ax = AxesCalculator() + u = MagicMock() + u.dimensions = np.array([10.0, 10.0, 10.0, 90, 90, 90]) + monkeypatch.setattr("CodeEntropy.levels.axes.make_whole", lambda _ag: None) + residue = u.select_atoms("resindex 5") + residue.__len__.return_value = 3 + u.atoms.residues.__len__.return_value = 4 + + heavy_atoms = _FakeAtomGroup( + [ + _atom(index=1, mass=12.0, pos=(1, 1, 1)), + _atom(index=2, mass=12.0, pos=(3, 3, 3)), + _atom(index=3, mass=16.0, pos=(4, 4, 4)), + ], + ) + + neighbours = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=4, mass=12.0, pos=(3, 3, 4)), + _atom(index=6, mass=16.0, pos=(4, 5, 4)), + ], + ) + neighbours[0].resindex = 4 + neighbours[1].resindex = 6 + neighbours[2].resindex = 8 + + def _select_atoms(q): + if q == "mass 2 to 999": + # return heavy atoms group + return heavy_atoms + if q == "resindex 5": + return residue + if q == "index 3": + return [heavy_atoms[2]] + if q == "not resindex 5": + return neighbours + if q == ("(bonded resindex 4) and (resindex 5)"): + return [heavy_atoms[0]] + if q == ("(bonded resindex 6) and (resindex 5)"): + return [heavy_atoms[1]] + if q == ("(bonded resindex 8) and (resindex 5)"): + return [heavy_atoms[2]] + + u.select_atoms.side_effect = _select_atoms + u.atoms.select_atoms.side_effect = _select_atoms + residue.select_atoms.side_effect = _select_atoms + residue.atoms.select_atoms.side_effect = _select_atoms + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([2, 2, 2], 2 * np.eye(3)) + ) + monkeypatch.setattr( + ax, + "get_bonded_axes", + lambda system, atom, dimensions: (3 * np.eye(3), [12, 12, 12]), + ) + + trans_axes, rot_axes, rot_center, moi = ax.get_UA_axes( + data_container=u, + index=2, + resindex=5, + ) + + assert np.allclose(trans_axes, 2 * np.eye(3)) + assert np.allclose(rot_axes, 3 * np.eye(3)) + assert np.allclose(rot_center, [4, 4, 4]) + assert np.allclose(moi, [12, 12, 12]) + + +def test_get_non_terminal_axes_branched(monkeypatch): + ax = AxesCalculator() + residue = MagicMock() + residue.__len__ = 4 + edges = _FakeAtomGroup( + [ + _atom(index=0, mass=12.0, pos=(0, 0, 0)), + _atom(index=2, mass=14.0, pos=(3, 0, 0)), + _atom(index=3, mass=12.0, pos=(1, 1, 0)), + ], + ) + monkeypatch.setattr( + ax, "get_residue_custom_axes", lambda edges, center: ([1, 0, 0], 3 * np.eye(3)) + ) + centre, axes = ax.get_non_terminal_axes( + residue=residue, edges=edges, dimensions=np.array([1, 1, 1]) + ) + + assert np.allclose(centre, [1, 0, 0]) + assert np.allclose(axes, 3 * np.eye(3))