Chem.AllChem

Package Chem :: Module AllChem |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.AllChem

      1  # $Id: AllChem.py 687 2008-05-21 10:35:41Z glandrum $
      2  #
      3  #  Copyright (C) 2006-2008  greg Landrum and Rational Discovery LLC
      4  #
      5  #   @@ All Rights Reserved  @@
      6  #
      7  """ Import all RDKit chemistry modules
      8
      9  """
     10  import rdBase
     11  import RDConfig
     12  import Numeric
     13  import DataStructs
     14  from Geometry import rdGeometry
     15  from Chem import *
     16  from rdPartialCharges import *
     17  from rdDepictor import *
     18  from rdForceFieldHelpers import *
     19  from Chem.ChemicalFeatures import *
     20  from rdDistGeom import *
     21  from rdMolAlign import *
     22  from rdMolTransforms import *
     23  from rdShapeHelpers import *
     24  from rdChemReactions import *
     25  import ForceField
     26  Mol.Compute2DCoords = Compute2DCoords
     27  Mol.ComputeGasteigerCharges = ComputeGasteigerCharges
     28
     29
     30 -def TransformMol(mol,tform):
     31    """  Applies the transformation to a molecule and sets it up with
     32    a single conformer
     33
     34    """
     35    import Numeric
     36    newConf = Conformer()
     37    newConf.SetId(0)
     38    refConf = mol.GetConformer()
     39    for i in range(refConf.GetNumAtoms()):
     40      pos = list(refConf.GetAtomPosition(i))
     41      pos.append(1.0)
     42      newPos = Numeric.matrixmultiply(tform,Numeric.array(pos))
     43      newConf.SetAtomPosition(i,list(newPos)[:3])
     44    mol.RemoveAllConformers()
     45    mol.AddConformer(newConf)
     46
     47 -def ComputeMolShape(mol,confId=-1,boxDim=(20,20,20),spacing=0.5,**kwargs):
     48    res = rdGeometry.UniformGrid3D(boxDim[0],boxDim[1],boxDim[2],spacing=spacing)
     49    apply(EncodeShape,(mol,res,confId),kwargs)
     50    return res
     51
     52 -def ComputeMolVolume(mol,confId=-1,gridSpacing=0.1,boxMargin=2.0):
     53    import copy
     54    mol = copy.deepcopy(mol)
     55    conf = mol.GetConformer(confId)
     56    CanonicalizeConformer(conf)
     57    box = ComputeConfBox(conf)
     58    sideLen = ( box[1].x-box[0].x + 2*boxMargin, \
     59                box[1].y-box[0].y + 2*boxMargin, \
     60                box[1].z-box[0].z + 2*boxMargin )
     61    shape = rdGeometry.UniformGrid3D(sideLen[0],sideLen[1],sideLen[2],
     62                                     spacing=gridSpacing)
     63    EncodeShape(mol,shape,confId,ignoreHs=False,vdwScale=1.0)
     64    voxelVol = gridSpacing**3
     65    vol = 0.0
     66    occVect = shape.GetOccupancyVect()
     67    for i in range(len(occVect)):
     68      if occVect[i]==3:
     69        vol+= voxelVol
     70    return vol
     71
     72 -def GenerateDepictionMatching3DStructure(mol,reference,confId=-1,
     73                                           **kwargs):
     74    stripRef = RemoveHs(reference)
     75    nAts = mol.GetNumAtoms()
     76    dm = []
     77    conf = stripRef.GetConformer(confId)
     78    for i in range(nAts):
     79      pi = conf.GetAtomPosition(i)
     80      for j in range(i+1,nAts):
     81        pj = conf.GetAtomPosition(j)
     82        dm.append((pi-pj).Length())
     83    dm = Numeric.array(dm)
     84    apply(Compute2DCoordsMimicDistmat,(mol,dm),kwargs)
     85
     86 -def EnumerateLibraryFromReaction(reaction,sidechainSets) :
     87    """ Returns a generator for the virtual library defined by
     88     a reaction and a sequence of sidechain sets
     89
     90    >>> import Chem
     91    >>> from Chem import AllChem
     92    >>> s1=[Chem.MolFromSmiles(x) for x in ('NC','NCC')]
     93    >>> s2=[Chem.MolFromSmiles(x) for x in ('OC=O','OC(=O)C')]
     94    >>> rxn = AllChem.ReactionFromSmarts('[O:2]=[C:1][OH].[N:3]>>[O:2]=[C:1][N:3]')
     95    >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[s2,s1])
     96    >>> [Chem.MolToSmiles(x[0]) for x in list(r)]
     97    ['CNC=O', 'CCNC=O', 'CNC(=O)C', 'CCNC(=O)C']
     98
     99    Note that this is all done in a lazy manner, so "infinitely" large libraries can
    100    be done without worrying about running out of memory. Your patience will run out first:
    101
    102    Define a set of 10000 amines:
    103    >>> amines = (Chem.MolFromSmiles('N'+'C'*x) for x in range(10000))
    104
    105    ... a set of 10000 acids
    106    >>> acids = (Chem.MolFromSmiles('OC(=O)'+'C'*x) for x in range(10000))
    107
    108    ... now the virtual library (1e8 compounds in principle):
    109    >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[acids,amines])
    110
    111    ... look at the first 4 compounds:
    112    >>> [Chem.MolToSmiles(r.next()[0]) for x in range(4)]
    113    ['NC=O', 'CNC=O', 'CCNC=O', 'CCCNC=O']
    114
    115
    116    """
    117    if len(sidechainSets) != reaction.GetNumReactantTemplates():
    118      raise ValueError,'%d sidechains provided, %d required'%(len(sidechainSets),reaction.getNumReactantTemplates())
    119
    120    def _combiEnumerator(items,depth=0):
    121      for item in items[depth]:
    122        if depth+1 < len(items):
    123          v = _combiEnumerator(items,depth+1)
    124          for entry in v:
    125            l=[item]
    126            l.extend(entry)
    127            yield l
    128        else:
    129          yield [item]
    130    for chains in _combiEnumerator(sidechainSets):
    131      prodSets = reaction.RunReactants(chains)
    132      for prods in prodSets:
    133        yield prods
    134
    135  #------------------------------------
    136  #
    137  #  doctest boilerplate
    138  #
    139 -def _test():
    140    import doctest,sys
    141    return doctest.testmod(sys.modules["__main__"])
    142
    143
    144  if __name__ == '__main__':
    145    import sys
    146    failed,tried = _test()
    147    sys.exit(failed)
    148