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

Source Code for Module Chem.AllChem

  1  ## Automatically adapted for numpy.oldnumeric Jun 27, 2008 by -c 
  2   
  3  # $Id: AllChem.py 816 2008-08-29 10:02:38Z glandrum $ 
  4  # 
  5  #  Copyright (C) 2006-2008  greg Landrum and Rational Discovery LLC 
  6  # 
  7  #   @@ All Rights Reserved  @@ 
  8  # 
  9  """ Import all RDKit chemistry modules 
 10    
 11  """ 
 12  import rdBase 
 13  import RDConfig 
 14  import numpy.oldnumeric as Numeric 
 15  import DataStructs 
 16  from Geometry import rdGeometry 
 17  from Chem import * 
 18  from rdPartialCharges import * 
 19  from rdDepictor import * 
 20  from rdForceFieldHelpers import * 
 21  from Chem.ChemicalFeatures import * 
 22  from rdDistGeom import * 
 23  from rdMolAlign import * 
 24  from rdMolTransforms import * 
 25  from rdShapeHelpers import * 
 26  from rdChemReactions import * 
 27  import ForceField 
 28  Mol.Compute2DCoords = Compute2DCoords 
 29  Mol.ComputeGasteigerCharges = ComputeGasteigerCharges 
 30  import numpy 
 31   
32 -def TransformMol(mol,tform):
33 """ Applies the transformation to a molecule and sets it up with 34 a single conformer 35 36 """ 37 import numpy.oldnumeric as Numeric 38 newConf = Conformer() 39 newConf.SetId(0) 40 refConf = mol.GetConformer() 41 for i in range(refConf.GetNumAtoms()): 42 pos = list(refConf.GetAtomPosition(i)) 43 pos.append(1.0) 44 newPos = Numeric.dot(tform,Numeric.array(pos)) 45 newConf.SetAtomPosition(i,list(newPos)[:3]) 46 mol.RemoveAllConformers() 47 mol.AddConformer(newConf)
48
49 -def ComputeMolShape(mol,confId=-1,boxDim=(20,20,20),spacing=0.5,**kwargs):
50 res = rdGeometry.UniformGrid3D(boxDim[0],boxDim[1],boxDim[2],spacing=spacing) 51 apply(EncodeShape,(mol,res,confId),kwargs) 52 return res
53
54 -def ComputeMolVolume(mol,confId=-1,gridSpacing=0.1,boxMargin=2.0):
55 import copy 56 mol = copy.deepcopy(mol) 57 conf = mol.GetConformer(confId) 58 CanonicalizeConformer(conf) 59 box = ComputeConfBox(conf) 60 sideLen = ( box[1].x-box[0].x + 2*boxMargin, \ 61 box[1].y-box[0].y + 2*boxMargin, \ 62 box[1].z-box[0].z + 2*boxMargin ) 63 shape = rdGeometry.UniformGrid3D(sideLen[0],sideLen[1],sideLen[2], 64 spacing=gridSpacing) 65 EncodeShape(mol,shape,confId,ignoreHs=False,vdwScale=1.0) 66 voxelVol = gridSpacing**3 67 vol = 0.0 68 occVect = shape.GetOccupancyVect() 69 for i in range(len(occVect)): 70 if occVect[i]==3: 71 vol+= voxelVol 72 return vol
73
74 -def GenerateDepictionMatching3DStructure(mol,reference,confId=-1, 75 **kwargs):
76 nAts = mol.GetNumAtoms() 77 dm = [] 78 conf = mol.GetConformer(confId) 79 for i in range(nAts): 80 pi = conf.GetAtomPosition(i) 81 for j in range(i+1,nAts): 82 pj = conf.GetAtomPosition(j) 83 dm.append((pi-pj).Length()) 84 dm = Numeric.array(dm) 85 apply(Compute2DCoordsMimicDistmat,(mol,dm),kwargs)
86
87 -def GetBestRMS(ref,probe,refConfId=-1,probeConfId=-1,maps=None):
88 if not maps: 89 query = RemoveHs(probe) 90 matches = ref.GetSubstructMatches(query) 91 if not matches: 92 raise ValueError,'mol %s does not match mol %s'%(ref.GetProp('_Name'), 93 probe.GetProp('_Name')) 94 maps = [] 95 for match in matches: 96 t=[] 97 for j,idx in enumerate(match): 98 t.append((j,idx)) 99 maps.append(t) 100 bestRMS=1000. 101 for amap in maps: 102 rms=AlignMol(probe,ref,probeConfId,refConfId,atomMap=amap) 103 if rms<bestRMS: 104 bestRMS=rms 105 return bestRMS
106
107 -def EnumerateLibraryFromReaction(reaction,sidechainSets) :
108 """ Returns a generator for the virtual library defined by 109 a reaction and a sequence of sidechain sets 110 111 >>> import Chem 112 >>> from Chem import AllChem 113 >>> s1=[Chem.MolFromSmiles(x) for x in ('NC','NCC')] 114 >>> s2=[Chem.MolFromSmiles(x) for x in ('OC=O','OC(=O)C')] 115 >>> rxn = AllChem.ReactionFromSmarts('[O:2]=[C:1][OH].[N:3]>>[O:2]=[C:1][N:3]') 116 >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[s2,s1]) 117 >>> [Chem.MolToSmiles(x[0]) for x in list(r)] 118 ['CNC=O', 'CCNC=O', 'CNC(=O)C', 'CCNC(=O)C'] 119 120 Note that this is all done in a lazy manner, so "infinitely" large libraries can 121 be done without worrying about running out of memory. Your patience will run out first: 122 123 Define a set of 10000 amines: 124 >>> amines = (Chem.MolFromSmiles('N'+'C'*x) for x in range(10000)) 125 126 ... a set of 10000 acids 127 >>> acids = (Chem.MolFromSmiles('OC(=O)'+'C'*x) for x in range(10000)) 128 129 ... now the virtual library (1e8 compounds in principle): 130 >>> r = AllChem.EnumerateLibraryFromReaction(rxn,[acids,amines]) 131 132 ... look at the first 4 compounds: 133 >>> [Chem.MolToSmiles(r.next()[0]) for x in range(4)] 134 ['NC=O', 'CNC=O', 'CCNC=O', 'CCCNC=O'] 135 136 137 """ 138 if len(sidechainSets) != reaction.GetNumReactantTemplates(): 139 raise ValueError,'%d sidechains provided, %d required'%(len(sidechainSets),reaction.getNumReactantTemplates()) 140 141 def _combiEnumerator(items,depth=0): 142 for item in items[depth]: 143 if depth+1 < len(items): 144 v = _combiEnumerator(items,depth+1) 145 for entry in v: 146 l=[item] 147 l.extend(entry) 148 yield l 149 else: 150 yield [item]
151 for chains in _combiEnumerator(sidechainSets): 152 prodSets = reaction.RunReactants(chains) 153 for prods in prodSets: 154 yield prods 155 156 157
158 -def ConstrainedEmbed(mol,core,useTethers,randomseed=2342):
159 match = mol.GetSubstructMatch(core) 160 if not match: 161 raise ValueError,"molecule doesn't match the core" 162 coordMap={} 163 coreConf = core.GetConformer() 164 for i,idxI in enumerate(match): 165 corePtI = coreConf.GetAtomPosition(i) 166 coordMap[idxI]=corePtI 167 168 ci = EmbedMolecule(mol,coordMap=coordMap,randomSeed=randomseed) 169 if ci<0: 170 logger.error('could not embed molecule %s, no coordinates generated.'%mol.GetProp('_Name')) 171 172 algMap=[] 173 for i,itm in enumerate(match): 174 algMap.append((itm,i)) 175 176 if not useTethers: 177 # clean up the conformation 178 ff = UFFGetMoleculeForceField(mol,confId=0) 179 for i,idxI in enumerate(match): 180 for j in range(i+1,len(match)): 181 idxJ = match[j] 182 d = coordMap[idxI].Distance(coordMap[idxJ]) 183 ff.AddDistanceConstraint(idxI,idxJ,d,d,100.) 184 ff.Initialize() 185 n=4 186 more=ff.Minimize() 187 while more and n: 188 more=ff.Minimize() 189 n-=1 190 # rotate the embedded conformation onto the core: 191 ssd =AlignMol(mol,core,atomMap=algMap) 192 else: 193 # rotate the embedded conformation onto the core: 194 ssd = AlignMol(mol,core,atomMap=algMap) 195 ff = UFFGetMoleculeForceField(mol,confId=0) 196 conf = core.GetConformer() 197 for i in range(core.GetNumAtoms()): 198 p =conf.GetAtomPosition(i) 199 pIdx=ff.AddExtraPoint(p.x,p.y,p.z,fixed=True)-1 200 ff.AddDistanceConstraint(pIdx,match[i],0,0,100.) 201 ff.Initialize() 202 n=4 203 more=ff.Minimize(energyTol=1e-4,forceTol=1e-3) 204 while more and n: 205 more=ff.Minimize(energyTol=1e-4,forceTol=1e-3) 206 n-=1 207 # realign 208 ssd = AlignMol(mol,core,atomMap=algMap) 209 print numpy.sqrt(ssd/len(algMap)) 210 return mol
211 212 213 #------------------------------------ 214 # 215 # doctest boilerplate 216 #
217 -def _test():
218 import doctest,sys 219 return doctest.testmod(sys.modules["__main__"])
220 221 222 if __name__ == '__main__': 223 import sys 224 failed,tried = _test() 225 sys.exit(failed) 226