Package rdkit :: Package Chem :: Module PropertyMol :: Class PropertyMol
[hide private]
[frames] | no frames]

Class PropertyMol

source code

Mol --+
      |
     PropertyMol

allows rdkit molecules to be pickled with their properties saved.

>>> from rdkit.six.moves import cPickle
>>> m = Chem.MolFromMolFile('test_data/benzene.mol')
>>> m.GetProp('_Name')
'benzene.mol'

by default pickling removes properties:
>>> m2 = cPickle.loads(cPickle.dumps(m))
>>> m2.HasProp('_Name')
0

Property mols solve this:
>>> pm = PropertyMol(m)
>>> pm.GetProp('_Name')
'benzene.mol'
>>> pm.SetProp('MyProp','foo')
>>> pm.HasProp('MyProp')
1

>>> pm2 = cPickle.loads(cPickle.dumps(pm))
>>> Chem.MolToSmiles(pm2)
'c1ccccc1'
>>> pm2.GetProp('_Name')
'benzene.mol'
>>> pm2.HasProp('MyProp')
1
>>> pm2.GetProp('MyProp')
'foo'
>>> pm2.HasProp('MissingProp')
0

Property mols are a bit more permissive about the types
of property values:
>>> pm.SetProp('IntVal',1)

That wouldn't work with a standard mol

but the Property mols still convert all values to strings before storing:
>>> pm.GetProp('IntVal')
'1'

This is a test for sf.net issue 2880943: make sure properties end up in SD files:
>>> import tempfile,os
>>> fn = tempfile.mktemp('.sdf')
>>> w = Chem.SDWriter(fn)
>>> w.write(pm)
>>> w=None
>>> txt = open(fn,'r').read()
>>> '<IntVal>' in txt
True
>>> try:
...   os.unlink(fn)
... except Exception:
...   pass

The next level of that bug: does writing a *depickled* propertymol
to an SD file include properties:
>>> fn = tempfile.mktemp('.sdf')
>>> w = Chem.SDWriter(fn)
>>> pm = cPickle.loads(cPickle.dumps(pm))
>>> w.write(pm)
>>> w=None
>>> txt = open(fn,'r').read()
>>> '<IntVal>' in txt
True
>>> try:
...   os.unlink(fn)
... except Exception:
...   pass

Instance Methods [hide private]
 
__init__(self, mol) source code
 
SetProp(self, nm, val) source code
 
__getstate__(self) source code
 
__setstate__(self, stateD) source code
Class Variables [hide private]
  __getstate_manages_dict__ = True