From 3fcfc6bbcfc12f5afc2ae2aa0b3a36f98333c9f1 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Mon, 5 Oct 2026 13:59:58 +0100 Subject: [PATCH 1/2] Add fix for issue-2236 --- .../openmm_septop/equil_septop_settings.py | 26 +++++++++++++++-- .../openmm_septop/test_septop_settings.py | 29 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/openfe/protocols/openmm_septop/equil_septop_settings.py b/src/openfe/protocols/openmm_septop/equil_septop_settings.py index 894a517e7..3952bf5f8 100644 --- a/src/openfe/protocols/openmm_septop/equil_septop_settings.py +++ b/src/openfe/protocols/openmm_septop/equil_septop_settings.py @@ -11,7 +11,8 @@ openfe.protocols.openmm_septop.SepTopProtocol """ -from typing import Optional +import copy +from typing import Any, Optional import numpy as np from gufe.settings import ( @@ -21,7 +22,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 +372,27 @@ 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 + ): + 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..b65147424 100644 --- a/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py +++ b/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py @@ -1,6 +1,9 @@ # This code is part of OpenFE and is licensed under the MIT license. # For details, see https://github.com/OpenFreeEnergy/openfe +import json + +import gufe import pytest from openfe import ChemicalSystem, SolventComponent @@ -8,6 +11,7 @@ SepTopProtocol, ) from openfe.protocols.openmm_septop.equil_septop_settings import SepTopSettings +from openfe.protocols.restraint_utils.settings import BoreschRestraintSettings @pytest.fixture() @@ -137,3 +141,28 @@ 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") + + 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) From 78c2c031eed9650c08e665a4ffb3128577c65cd7 Mon Sep 17 00:00:00 2001 From: IAlibay Date: Mon, 5 Oct 2026 18:08:15 +0100 Subject: [PATCH 2/2] Add FutureWarning --- .../protocols/openmm_septop/equil_septop_settings.py | 9 +++++++++ .../protocols/openmm_septop/test_septop_settings.py | 6 ++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/openfe/protocols/openmm_septop/equil_septop_settings.py b/src/openfe/protocols/openmm_septop/equil_septop_settings.py index 3952bf5f8..a40c5854c 100644 --- a/src/openfe/protocols/openmm_septop/equil_septop_settings.py +++ b/src/openfe/protocols/openmm_septop/equil_septop_settings.py @@ -12,6 +12,7 @@ """ import copy +import warnings from typing import Any, Optional import numpy as np @@ -387,6 +388,14 @@ def allow_legacy_complex_restraint_settings(cls, data: Any) -> Any: 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) 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 b65147424..eec83f191 100644 --- a/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py +++ b/src/openfe/tests/protocols/openmm_septop/test_septop_settings.py @@ -1,9 +1,6 @@ # This code is part of OpenFE and is licensed under the MIT license. # For details, see https://github.com/OpenFreeEnergy/openfe -import json - -import gufe import pytest from openfe import ChemicalSystem, SolventComponent @@ -151,7 +148,8 @@ def test_legacy_complex_restraint_settings(default_settings): legacy.pop("complex_restraint_settings_B") legacy["complex_restraint_settings"] = BoreschRestraintSettings(host_selection="name CA") - settings = SepTopSettings(**legacy) + 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"