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

SmallMoleculeComponent round trip does not save atom hybridization #407

Open
jthorton opened this issue Nov 14, 2024 · 2 comments · May be fixed by #408
Open

SmallMoleculeComponent round trip does not save atom hybridization #407

jthorton opened this issue Nov 14, 2024 · 2 comments · May be fixed by #408
Assignees

Comments

@jthorton
Copy link
Contributor

jthorton commented Nov 14, 2024

I noticed the hybridization of each atom was set as UNSPECIFIED when working on a ligand from a graphml object I think this is due to the to_dict method not saving the hybridization of the atom. See this simplified example

from rdkit import Chem
from gufe import SmallMoleculeComponent

# generate an rdkit molecule
ethane = Chem.MolFromSmiles("CC")
ethane = Chem.AddHs(methane)
Chem.rdDistGeom.EmbedMolecule(methane, useRandomCoords=False, randomSeed=0)
# check the state of each carbon
for atom in methane.GetAtoms():
    if atom.GetAtomicNum() == 6:
        print(atom.GetHybridization())

# create the SMC
smc = SmallMoleculeComponent(methane, name="methane")
# round trip to dict
smc_dict = smc.to_dict()
# remove the original object
del smc
smc_2 = SmallMoleculeComponent.from_dict(smc_dict)
# check the state again
for atom in smc_2.to_rdkit().GetAtoms():
    if atom.GetAtomicNum() == 6:
        print(atom.GetHybridization())

SP3
SP3
UNSPECIFIED
UNSPECIFIED

@jthorton jthorton self-assigned this Nov 14, 2024
@jthorton
Copy link
Contributor Author

My initial idea of changing to use the rdkit internal JSON representation in the to_dict method doesn't seem to fix this as it also loses the hybridization info see extended example:

from rdkit import Chem
from gufe import SmallMoleculeComponent
settings = Chem.rdMolInterchange.JSONWriteParameters()
settings.useRDKitExtensions = True
# generate an rdkit molecule
ethane = Chem.MolFromSmiles("CC")
ethane = Chem.AddHs(methane)
Chem.rdDistGeom.EmbedMolecule(methane, useRandomCoords=False, randomSeed=0)
# check the state of each carbon
for atom in methane.GetAtoms():
    if atom.GetAtomicNum() == 6:
        print(atom.GetHybridization())

# create the SMC
smc = SmallMoleculeComponent(methane, name="methane")
# round trip to dict
smc_dict = smc.to_dict()
# remove the original object
del smc
smc_2 = SmallMoleculeComponent.from_dict(smc_dict)
# check the state again
for atom in smc_2.to_rdkit().GetAtoms():
    if atom.GetAtomicNum() == 6:
        print(atom.GetHybridization())

json_str = Chem.MolToJSON(smc_2.to_rdkit(), settings)
print(json_str)
json_mol = Chem.JSONToMols(json_str)[0]
for atom in json_mol.GetAtoms():
    if atom.GetAtomicNum() == 6:
        print(atom.GetHybridization())

SP3
SP3
UNSPECIFIED
UNSPECIFIED
{"rdkitjson":{"version":11},"defaults":{"atom":{"z":6,"impHs":0,"chg":0,"nRad":0,"isotope":0,"stereo":"unspecified"},"bond":{"bo":1,"stereo":"unspecified"}},"molecules":[{"atoms":[{},{},{"z":1},{"z":1},{"z":1},{"z":1},{"z":1},{"z":1}],"bonds":[{"atoms":[0,1]},{"atoms":[0,2]},{"atoms":[0,3]},{"atoms":[0,4]},{"atoms":[1,5]},{"atoms":[1,6]},{"atoms":[1,7]}],"conformers":[{"dim":3,"coords":[[-0.7455,0.0414,0.0117],[0.7473,0.0028,0.0012],[-1.1297,-0.6374,0.8144],[-1.1848,1.0255,0.1996],[-1.1998,-0.3346,-0.9388],[1.0841,-0.7365,-0.7731],[1.2266,0.9617,-0.268],[1.2018,-0.323,0.9531]]}],"properties":{"ofe-name":"methane"},"extensions":[{"name":"rdkitRepresentation","formatVersion":2,"toolkitVersion":"2023.09.4"}]}]}
UNSPECIFIED
UNSPECIFIED

So I think I just need to add this property to the to and from dict method.

@dotsdl
Copy link
Member

dotsdl commented Nov 14, 2024

Oh shoot, I'm glad we tried at least! Does rdkit have some kind of global settings similar to the pickle ones to tell it to include everything in its JSON representation?

@jthorton jthorton linked a pull request Nov 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants