Skip to content

Commit

Permalink
Merge branch 'main' into shared-object-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dwhswenson authored Jan 22, 2024
2 parents da9955e + a426dce commit 8489c24
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
13 changes: 13 additions & 0 deletions gufe/components/smallmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,16 @@ def _from_dict(cls, d: dict):
m.UpdatePropertyCache()

return cls(rdkit=m)

def copy_with_replacements(self, **replacements):
# this implementation first makes a copy with the name replaced
# only, then does any other replacements that are necessary
if 'name' in replacements:
name = replacements.pop('name')
dct = self._to_dict()
dct['molprops']['ofe-name'] = name
obj = self._from_dict(dct)
else:
obj = self

return super(SmallMoleculeComponent, obj).copy_with_replacements(**replacements)
3 changes: 2 additions & 1 deletion gufe/mapping/atom_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from collections.abc import Iterator
import gufe

from ..tokenization import GufeTokenizable
from .atom_mapping import AtomMapping


class AtomMapper(abc.ABC):
class AtomMapper(GufeTokenizable):
"""A class for manufacturing mappings"""
@abc.abstractmethod
def suggest_mappings(self, A: gufe.Component,
Expand Down
33 changes: 33 additions & 0 deletions gufe/tests/test_smallmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ def test_serialization_cycle_smiles(self, named_ethane):
assert named_ethane is not copy
assert named_ethane.smiles == copy.smiles

@pytest.mark.parametrize('replace', (
['name'],
['mol'],
['name', 'mol'],
))
def test_copy_with_replacements(self, named_ethane, replace):
replacements = {}
if 'name' in replace:
replacements['name'] = "foo"

if 'mol' in replace:
# it is a little weird to use copy_with_replacements to replace
# the whole molecule (possibly keeping the same name), but it
# should work if someone does! (could more easily imagine only
# using a new conformer)
rdmol = Chem.AddHs(Chem.MolFromSmiles("CO"))
Chem.AllChem.Compute2DCoords(rdmol)
mol = SmallMoleculeComponent.from_rdkit(rdmol)
dct = mol._to_dict()
for item in ['atoms', 'bonds', 'conformer']:
replacements[item] = dct[item]

new = named_ethane.copy_with_replacements(**replacements)
if 'name' in replace:
assert new.name == "foo"
else:
assert new.name == "ethane"

if 'mol' in replace:
assert new.smiles == "CO"
else:
assert new.smiles == "CC"


@pytest.mark.skipif(not HAS_OFFTK, reason="no openff toolkit available")
class TestSmallMoleculeComponentConversion:
Expand Down

0 comments on commit 8489c24

Please sign in to comment.