Skip to content
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

Globally set RDKit to include all properties when pickling an RDKit Mol #344

Merged
merged 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions gufe/components/explicitmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ def __init__(self, rdkit: RDKitMol, name: str = ""):
self._smiles: Optional[str] = None
self._name = name

def __getstate__(self):
# TODO: check that RDKit setting is set before issuing warning
if Chem.GetDefaultPickleProperties() != Chem.PropertyPickleOptions.AllProps:
warnings.warn(
"RDKit does not preserve Mol properties when pickled by default, which may drop e.g. atom charges; "
"consider setting `Chem.SetDefaultPickleProperties(Chem.PropertyPickleOptions.AllProps)`"
)

return self.__dict__

@classmethod
def _defaults(cls):
return super()._defaults()
Expand Down
15 changes: 15 additions & 0 deletions gufe/tests/test_explicitmoleculecomponent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pickle


class ExplicitMoleculeComponentMixin:

def test_pickle(self, instance):

pickled = pickle.dumps(instance)
unpickled = pickle.loads(pickled)

assert unpickled == instance

# it's currently the case that the flyweight pattern isn't respected by
# pickling/unpickling
assert not unpickled is instance
3 changes: 2 additions & 1 deletion gufe/tests/test_proteincomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from gufe import ProteinComponent

from .conftest import ALL_PDB_LOADERS
from .test_explicitmoleculecomponent import ExplicitMoleculeComponentMixin
from .test_tokenization import GufeTokenizableTestsMixin


Expand Down Expand Up @@ -81,7 +82,7 @@ def assert_topology_equal(ref_top, top):
assert ref_bond[1].index == bond[1].index


class TestProteinComponent(GufeTokenizableTestsMixin):
class TestProteinComponent(GufeTokenizableTestsMixin, ExplicitMoleculeComponentMixin):

cls = ProteinComponent
repr = "ProteinComponent(name=Steve)"
Expand Down
3 changes: 2 additions & 1 deletion gufe/tests/test_smallmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from gufe.components.explicitmoleculecomponent import _ensure_ofe_name
from gufe.tokenization import TOKENIZABLE_REGISTRY

from .test_explicitmoleculecomponent import ExplicitMoleculeComponentMixin
from .test_tokenization import GufeTokenizableTestsMixin


Expand Down Expand Up @@ -74,7 +75,7 @@ def test_ensure_ofe_name(internal, rdkit_name, name, expected, recwarn):
assert rdkit.GetProp("ofe-name") == out_name


class TestSmallMoleculeComponent(GufeTokenizableTestsMixin):
class TestSmallMoleculeComponent(GufeTokenizableTestsMixin, ExplicitMoleculeComponentMixin):

cls = SmallMoleculeComponent
repr = "SmallMoleculeComponent(name=ethane)"
Expand Down
23 changes: 23 additions & 0 deletions news/pickle-explicitmoleculecomponent.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added warning when pickling an ``ExplicitMoleculeComponent`` that RDKit mol properties not preserved by default.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
Loading