rdkit: MolPickler doesn't preserve atom properties

from rdkit import Chem
mol = Chem.RWMol(Chem.MolFromSmiles('CCC'))
mol.GetAtomWithIdx(0).SetProp('label', 'X')
atom = mol.GetAtomWithIdx(0)
print(atom.HasProp('label'), atom.GetProp('label'))
mol = Chem.Mol(mol.ToBinary())
atom = mol.GetAtomWithIdx(0)
print(atom.HasProp('label'))

gives

1 X
0

It could be that there is a flag to preserve the atom properties. If that is the case, an improvement of the docs would be required. PropertyMol also doesn’t preserve the atom properties.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

This was resolved by #1277 : it’s now possible to configure the RDKit to pickle molecular, atomic, and bond properties:

In [14]: m.SetProp('foo','bar')

In [16]: m.GetAtomWithIdx(0).SetIntProp('hello',4)

In [17]: Chem.SetDefaultPickleProperties(Chem.PropertyPickleOptions.AllProps)

In [18]: import pickle

In [19]: m2 = pickle.loads(pickle.dumps(m))

In [20]: m2.GetPropsAsDict()
Out[20]: {'foo': 'bar', 'numArom': 0}

In [21]: m2.GetAtomWithIdx(0).GetPropsAsDict()
Out[21]: {'_CIPRank': 0, 'hello': 4}

In [22]: m2 = Chem.Mol(m.ToBinary())

In [23]: m2.GetPropsAsDict()
Out[23]: {'foo': 'bar', 'numArom': 0}

In [24]: m2.GetAtomWithIdx(0).GetPropsAsDict()
Out[24]: {'_CIPRank': 0, 'hello': 4}

Note that the default behavior is the same as before, you do need to set the option in order to have properties pickled with the molecule.