diff --git a/src/BioSimSpace/Sandpit/Exscientia/_SireWrappers/_molecule.py b/src/BioSimSpace/Sandpit/Exscientia/_SireWrappers/_molecule.py index 654be76bf..e421d0bd6 100644 --- a/src/BioSimSpace/Sandpit/Exscientia/_SireWrappers/_molecule.py +++ b/src/BioSimSpace/Sandpit/Exscientia/_SireWrappers/_molecule.py @@ -1472,9 +1472,7 @@ def translate(self, vector, property_map={}): if self._sire_object.has_property(coord_prop): _property_map["coordinates"] = coord_prop mol = ( - self._sire_object.move() - .translate(_SireMaths.Vector(vec), _property_map) - .commit() + mol.move().translate(_SireMaths.Vector(vec), _property_map).commit() ) else: diff --git a/src/BioSimSpace/_SireWrappers/_molecule.py b/src/BioSimSpace/_SireWrappers/_molecule.py index 75880eddb..662cb252a 100644 --- a/src/BioSimSpace/_SireWrappers/_molecule.py +++ b/src/BioSimSpace/_SireWrappers/_molecule.py @@ -1412,9 +1412,7 @@ def translate(self, vector, property_map={}): if self._sire_object.has_property(coord_prop): _property_map["coordinates"] = coord_prop mol = ( - self._sire_object.move() - .translate(_SireMaths.Vector(vec), _property_map) - .commit() + mol.move().translate(_SireMaths.Vector(vec), _property_map).commit() ) else: diff --git a/tests/Sandpit/Exscientia/_SireWrappers/test_molecule.py b/tests/Sandpit/Exscientia/_SireWrappers/test_molecule.py index 89ae620a4..eef91f38e 100644 --- a/tests/Sandpit/Exscientia/_SireWrappers/test_molecule.py +++ b/tests/Sandpit/Exscientia/_SireWrappers/test_molecule.py @@ -173,3 +173,54 @@ def test_makeCompatibleWith_regression(): # Make sure the energies are approximately equal. assert nrg_compatible == pytest.approx(nrg_leap, rel=1e-5) + + +def test_translate_custom_coordinates_property(perturbable_system): + """ + Regresson test to ensure that a molecule with a custom + coordinates property gets translated correctly. + """ + + from sire.maths import Vector + + # Extract a copy of the perturbable molecule. + mol = perturbable_system.getPerturbableMolecules()[0].copy() + + # Copy the "coordinates0" property to a custom property installed + # "test" + cursor = mol._sire_object.cursor() + cursor["test"] = cursor["coordinates0"] + mol._sire_object = cursor.commit() + + # Store the existing coordinates. + coords0 = mol._sire_object.property("coordinates0").to_vector() + coords1 = mol._sire_object.property("coordinates1").to_vector() + coords_test = mol._sire_object.property("test").to_vector() + + # Translate the molecule. + mol.translate(3 * [BSS.Units.Length.angstrom], property_map={"coordinates": "test"}) + + # Get the new coordinates. + new_coords0 = mol._sire_object.property("coordinates0").to_vector() + new_coords1 = mol._sire_object.property("coordinates1").to_vector() + new_coords_test = mol._sire_object.property("test").to_vector() + + # Create a vector of the displacement. + v_ref = Vector(1.0, 1.0, 1.0) + + # Check that the coordinates have been translated correctly. + for c_new, c_old in zip(new_coords0, coords0): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value() + for c_new, c_old in zip(new_coords1, coords1): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value() + for c_new, c_old in zip(new_coords_test, coords_test): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value() diff --git a/tests/Sandpit/Exscientia/conftest.py b/tests/Sandpit/Exscientia/conftest.py index cc4cebb38..82268b3b3 100644 --- a/tests/Sandpit/Exscientia/conftest.py +++ b/tests/Sandpit/Exscientia/conftest.py @@ -169,3 +169,14 @@ def merged_benzene_pyrrole(benzene, pyrrole, mapping_benzene_pyrrole): allow_ring_breaking=True, allow_ring_size_change=True, ) + + +@pytest.fixture(scope="module") +def perturbable_system(): + """A vacuum perturbable system.""" + return BSS.IO.readPerturbableSystem( + f"{url}/perturbable_system0.prm7", + f"{url}/perturbable_system0.rst7", + f"{url}/perturbable_system1.prm7", + f"{url}/perturbable_system1.rst7", + ) diff --git a/tests/_SireWrappers/test_molecule.py b/tests/_SireWrappers/test_molecule.py index bb447ec6e..2b4dbf16e 100644 --- a/tests/_SireWrappers/test_molecule.py +++ b/tests/_SireWrappers/test_molecule.py @@ -145,3 +145,54 @@ def test_makeCompatibleWith_regression(): # Make sure the energies are approximately equal. assert nrg_compatible == pytest.approx(nrg_leap, rel=1e-5) + + +def test_translate_custom_coordinates_property(perturbable_system): + """ + Regresson test to ensure that a molecule with a custom + coordinates property gets translated correctly. + """ + + from sire.maths import Vector + + # Extract a copy of the perturbable molecule. + mol = perturbable_system.getPerturbableMolecules()[0].copy() + + # Copy the "coordinates0" property to a custom property installed + # "test" + cursor = mol._sire_object.cursor() + cursor["test"] = cursor["coordinates0"] + mol._sire_object = cursor.commit() + + # Store the existing coordinates. + coords0 = mol._sire_object.property("coordinates0").to_vector() + coords1 = mol._sire_object.property("coordinates1").to_vector() + coords_test = mol._sire_object.property("test").to_vector() + + # Translate the molecule. + mol.translate(3 * [BSS.Units.Length.angstrom], property_map={"coordinates": "test"}) + + # Get the new coordinates. + new_coords0 = mol._sire_object.property("coordinates0").to_vector() + new_coords1 = mol._sire_object.property("coordinates1").to_vector() + new_coords_test = mol._sire_object.property("test").to_vector() + + # Create a vector of the displacement. + v_ref = Vector(1.0, 1.0, 1.0) + + # Check that the coordinates have been translated correctly. + for c_new, c_old in zip(new_coords0, coords0): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value() + for c_new, c_old in zip(new_coords1, coords1): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value() + for c_new, c_old in zip(new_coords_test, coords_test): + v = c_new - c_old + assert pytest.approx(v.x().value(), abs=1e-5) == v_ref.x().value() + assert pytest.approx(v.y().value(), abs=1e-5) == v_ref.y().value() + assert pytest.approx(v.z().value(), abs=1e-5) == v_ref.z().value()