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

Save atom hybridization #408

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
17 changes: 16 additions & 1 deletion gufe/components/smallmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@
5: Chem.rdchem.BondStereo.STEREOTRANS}
_BONDSTEREO_TO_INT = {v: k for k, v in _INT_TO_BONDSTEREO.items()}

# following the numbering in rdkit
_INT_TO_HYBRIDIZATION = {
0: Chem.rdchem.HybridizationType.UNSPECIFIED,
1: Chem.rdchem.HybridizationType.S,
2: Chem.rdchem.HybridizationType.SP,
3: Chem.rdchem.HybridizationType.SP2,
4: Chem.rdchem.HybridizationType.SP3,
5: Chem.rdchem.HybridizationType.SP2D,
6: Chem.rdchem.HybridizationType.SP3D,
7: Chem.rdchem.HybridizationType.SP3D2,
8: Chem.rdchem.HybridizationType.OTHER
}
_HYBRIDIZATION_TO_INT = {v: k for k, v in _INT_TO_HYBRIDIZATION.items()}


def _setprops(obj, d: dict) -> None:
# add props onto rdkit "obj" (atom/bond/mol/conformer)
Expand Down Expand Up @@ -213,7 +227,7 @@ def _to_dict(self) -> dict:
atoms.append((
atom.GetAtomicNum(), atom.GetIsotope(), atom.GetFormalCharge(), atom.GetIsAromatic(),
_ATOMCHIRAL_TO_INT[atom.GetChiralTag()], atom.GetAtomMapNum(),
atom.GetPropsAsDict(includePrivate=False),
atom.GetPropsAsDict(includePrivate=False), _HYBRIDIZATION_TO_INT[atom.GetHybridization()]
))
output['atoms'] = atoms

Expand Down Expand Up @@ -247,6 +261,7 @@ def _from_dict(cls, d: dict):
a.SetChiralTag(_INT_TO_ATOMCHIRAL[atom[4]])
a.SetAtomMapNum(atom[5])
_setprops(a, atom[6])
a.SetHybridization(_INT_TO_HYBRIDIZATION[atom[7]])
jthorton marked this conversation as resolved.
Show resolved Hide resolved
em.AddAtom(a)

for bond in d['bonds']:
Expand Down
12 changes: 12 additions & 0 deletions gufe/tests/test_smallmoleculecomponent.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,18 @@ def test_to_dict(self, phenol):

assert isinstance(d, dict)

def test_to_dict_hybridization(self, phenol):
"""
Make sure dict round trip saves the hybridization
<https://github.com/OpenFreeEnergy/gufe/issues/407>
"""
phenol_dict = phenol.to_dict()
TOKENIZABLE_REGISTRY.clear()
new_phenol = SmallMoleculeComponent.from_dict(phenol_dict)
for atom in new_phenol.to_rdkit().GetAtoms():
if atom.GetAtomicNum() == 6:
assert atom.GetHybridization() == Chem.rdchem.HybridizationType.SP2

@pytest.mark.skipif(not HAS_OFFTK, reason="no openff toolkit available")
def test_deserialize_roundtrip(self, toluene, phenol):
roundtrip = SmallMoleculeComponent.from_dict(phenol.to_dict())
Expand Down
Loading