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

Class PropertyMol

source code

 object --+        
          |        
??.instance --+    
              |    
     rdchem.Mol --+
                  |
                 PropertyMol

allows rdkit molecules to be pickled with their properties saved.

>>> import os
>>> from rdkit.six.moves import cPickle
>>> from rdkit import RDConfig
>>> m = Chem.MolFromMolFile(os.path.join(RDConfig.RDCodeDir, 'Chem', '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)
__init__( (object)arg1) -> None : Constructor, takes no arguments
source code
 
SetProp(self, nm, val)
SetProp( (Mol)self, (str)key, (str)val [, (bool)computed=False]) -> None : Sets a molecular property
source code
 
__getstate__(self) source code
 
__setstate__(self, stateD) source code

Inherited from rdchem.Mol: AddConformer, ClearComputedProps, ClearProp, Compute2DCoords, ComputeGasteigerCharges, Debug, GetAromaticAtoms, GetAtomWithIdx, GetAtoms, GetAtomsMatchingQuery, GetBondBetweenAtoms, GetBondWithIdx, GetBonds, GetBoolProp, GetConformer, GetConformers, GetDoubleProp, GetIntProp, GetNumAtoms, GetNumBonds, GetNumConformers, GetNumHeavyAtoms, GetProp, GetPropNames, GetPropsAsDict, GetRingInfo, GetSubstructMatch, GetSubstructMatches, GetUnsignedProp, HasProp, HasSubstructMatch, NeedsUpdatePropertyCache, RemoveAllConformers, RemoveConformer, SetBoolProp, SetDoubleProp, SetIntProp, SetUnsignedProp, ToBinary, UpdatePropertyCache, __copy__, __deepcopy__, __ge__, __getinitargs__, __reduce__, __str__

Inherited from unreachable.instance: __new__

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Variables [hide private]
  __getstate_manages_dict__ = True

Inherited from rdchem.Mol: __instance_size__, __safe_for_unpickling__

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, mol)
(Constructor)

source code 

__init__( (object)arg1) -> None :
    Constructor, takes no arguments

    C++ signature :
        void __init__(_object*)

__init__( (object)arg1, (str)arg2) -> None :

    C++ signature :
        void __init__(_object*,std::string)

__init__( (object)arg1, (Mol)arg2) -> None :

    C++ signature :
        void __init__(_object*,RDKit::ROMol)

__init__( (object)arg1, (Mol)arg2, (bool)arg3) -> None :

    C++ signature :
        void __init__(_object*,RDKit::ROMol,bool)

__init__( (object)arg1, (Mol)arg2, (bool)arg3, (int)arg4) -> None :

    C++ signature :
        void __init__(_object*,RDKit::ROMol,bool,int)

Overrides: object.__init__
(inherited documentation)

SetProp(self, nm, val)

source code 

SetProp( (Mol)self, (str)key, (str)val [, (bool)computed=False]) -> None :
    Sets a molecular property
    
      ARGUMENTS:
        - key: the name of the property to be set (a string).
        - value: the property value (a string).
        - computed: (optional) marks the property as being computed.
                    Defaults to False.
    
    

    C++ signature :
        void SetProp(RDKit::ROMol,char const*,std::string [,bool=False])

Overrides: rdchem.Mol.SetProp
(inherited documentation)