diff --git a/src/openfe/protocols/openmm_septop/equil_septop_settings.py b/src/openfe/protocols/openmm_septop/equil_septop_settings.py index 894a517e7..a40c5854c 100644 --- a/src/openfe/protocols/openmm_septop/equil_septop_settings.py +++ b/src/openfe/protocols/openmm_septop/equil_septop_settings.py @@ -11,7 +11,9 @@ openfe.protocols.openmm_septop.SepTopProtocol """ -from typing import Optional +import copy +import warnings +from typing import Any, Optional import numpy as np from gufe.settings import ( @@ -21,7 +23,7 @@ ) from gufe.settings.typing import PicosecondQuantity from openff.units import unit as offunit -from pydantic import Field, field_validator +from pydantic import Field, field_validator, model_validator from openfe.protocols.openmm_afe.equil_afe_settings import ( AlchemicalSettings, @@ -371,6 +373,35 @@ def must_be_positive(cls, v): """ Settings for the Boresch restraint applied to ligand B in the complex. """ + + @model_validator(mode="before") + @classmethod + def allow_legacy_complex_restraint_settings(cls, data: Any) -> Any: + """ + Allow settings with ``complex_restraint_settings`` (pre openfe v1.13) + to be loaded by converting it to ``complex_restraint_settings_A`` + and ``complex_restraint_settings_B``. + """ + if ( + isinstance(data, dict) + and "complex_restraint_settings" in data + and "complex_restraint_settings_A" not in data + and "complex_restraint_settings_B" not in data + ): + wmsg = ( + "Loading SepTop settings from `openfe<=1.12` " + "which uses ``complex_restraint_settings``. " + "The values of ``complex_restraint_settings`` have been copied over to " + "``complex_restraint_settings_A`` and ``complex_restraint_settings_B``. " + "This will no longer be supported in openfe v1.14." + ) + warnings.warn(wmsg, FutureWarning, stacklevel=2) + data = dict(data) + legacy = data.pop("complex_restraint_settings") + data["complex_restraint_settings_A"] = copy.deepcopy(legacy) + data["complex_restraint_settings_B"] = copy.deepcopy(legacy) + return data + analysis_settings: MultiStateAnalysisSettings = Field( default_factory=MultiStateAnalysisSettings ) diff --git a/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py b/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py index 8acd6b769..eec83f191 100644 --- a/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py +++ b/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py @@ -8,6 +8,7 @@ SepTopProtocol, ) from openfe.protocols.openmm_septop.equil_septop_settings import SepTopSettings +from openfe.protocols.restraint_utils.settings import BoreschRestraintSettings @pytest.fixture() @@ -137,3 +138,29 @@ def test_adaptive_settings_with_protein_membrane(a2a_protein_membrane_component, assert isinstance(settings, SepTopSettings) # Barostat should have been updated assert settings.complex_integrator_settings.barostat == "MonteCarloMembraneBarostat" + + +def test_legacy_complex_restraint_settings(default_settings): + # Settings created before per-ligand complex restraints were introduced (openfe v1.13) + # used a single `complex_restraint_settings` field + legacy = dict(default_settings) + legacy.pop("complex_restraint_settings_A") + legacy.pop("complex_restraint_settings_B") + legacy["complex_restraint_settings"] = BoreschRestraintSettings(host_selection="name CA") + + with pytest.warns(FutureWarning, match="complex_restraint_settings"): + settings = SepTopSettings(**legacy) + + assert settings.complex_restraint_settings_A.host_selection == "name CA" + assert settings.complex_restraint_settings_B.host_selection == "name CA" + # A and B must not share the same object + assert settings.complex_restraint_settings_A is not settings.complex_restraint_settings_B + + +def test_legacy_complex_restraint_settings_mixed(default_settings): + # Mixing the legacy field with the new ones is not allowed + mixed = dict(default_settings) + mixed["complex_restraint_settings"] = BoreschRestraintSettings() + + with pytest.raises(ValueError, match="complex_restraint_settings"): + SepTopSettings(**mixed)