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

Source Code for Module rdkit.Chem.AllChem

  1  ## Automatically adapted for numpy.oldnumeric Jun 27, 2008 by -c 
  2   
  3  # $Id: AllChem.py 1038 2009-03-30 04:39:20Z 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  from rdkit import rdBase 
 13  from rdkit import RDConfig 
 14  from rdkit import DataStructs 
 15  from rdkit.Geometry import rdGeometry 
 16  from rdkit.Chem import * 
 17  from rdPartialCharges import * 
 18  from rdDepictor import * 
 19  from rdForceFieldHelpers import * 
 20  from rdkit.Chem.ChemicalFeatures import * 
 21  from rdDistGeom import * 
 22  from rdMolAlign import * 
 23  from rdMolTransforms import * 
 24  from rdShapeHelpers import * 
 25  from rdChemReactions import * 
 26  from rdSLNParse import * 
 27  from rdMolDescriptors import * 
 28  from rdkit import ForceField 
 29  Mol.Compute2DCoords = Compute2DCoords 
 30  Mol.ComputeGasteigerCharges = ComputeGasteigerCharges 
 31  import numpy 
 32   
33 -def TransformMol(mol,tform):
34 """ Applies the transformation to a molecule and sets it up with 35 a single conformer 36 37 """ 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 = numpy.dot(tform,numpy.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 = numpy.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 >>> from rdkit import Chem 112 >>> from rdkit.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 rms =AlignMol(mol,core,atomMap=algMap) 192 else: 193 # rotate the embedded conformation onto the core: 194 rms = 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 rms = AlignMol(mol,core,atomMap=algMap) 209 print rms 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