Package install :: Package MoSTBioDat :: Package DataBase :: Package ImportData :: Package Data2DB :: Module Smile
[hide private]
[frames] | no frames]

Source Code for Module install.MoSTBioDat.DataBase.ImportData.Data2DB.Smile

  1  #!/usr/bin/env python 
  2  ########################## 
  3  # Smile.py               # 
  4  # Operate on SMILE code  # 
  5  ########################## 
  6   
  7  ###################################################### 
  8  # Copyright (c) 2007-2008 Andrzej Bak                # 
  9  # ARC Seibersdorf & University of Silesia            # 
 10  # Author: Andrzej Bak <Andrzej.Bak@us.edu.pl>        # 
 11  # License: GNU General Public License, version: 3    # 
 12  # URL: http://chemoinformatyka.us.edu.pl/mostbiodat/ # 
 13  # Version: 1, 06.01.2010                             # 
 14  ###################################################### 
 15   
 16  try: 
 17      import sys 
 18      from openeye.oechem import * 
 19      from openeye.oedepict import * 
 20  except ImportError,e: 
 21      print 'Error: %s' %e 
 22      sys.exit(1) 
 23  ########## Import User class ######################### 
24 -class Smile(object):
25 - def __init__(self,smile=None):
26 """ 27 Operate on SMILE code 28 INPUT: 29 smile - str, SMILE string 30 OUTPUT: 31 class object 32 """ 33 if not smile: 34 print 'Error: Specify SMILE code!' 35 sys.exit(1) 36 self.mol=OEGraphMol() 37 if not (OEParseSmiles(self.mol,smile)==1): 38 sys.stderr.write('SMILE string is invalid!') 39 sys.exit(1)
40
41 - def getMol(self):
42 """ 43 get Mol object 44 INPUT: 45 class object 46 OUTPUT: 47 molecule object 48 """ 49 return self.mol
50
51 - def Smi2ConnTab(self,mol,aromaticflag=OEAroModelOpenEye):
52 """ 53 Create canonical connection table from SMILE 54 INPUT: 55 class object 56 smile - str - SMILE code 57 OUTPUT: 58 connection table - list 59 """ 60 molecule=mol 61 OEAssignAromaticFlags(molecule,aromaticflag) 62 OEFindRingAtomsAndBonds(molecule) 63 molecule=self.CanMol(mol=molecule,kek=False,aromodel=aromaticflag,verbose=0) 64 connTab=[] 65 66 for bond in molecule.GetBonds(): 67 bond.SetIntType(bond.GetOrder()) 68 69 for bond in molecule.GetBonds():#iterate bonds 70 bnd=[] 71 begatom=bond.GetBgnIdx()+1#begin bond atom 72 endatom=bond.GetEndIdx()+1#end bond atom 73 if begatom>endatom: 74 begatom,endatom=endatom,begatom 75 bnd.append(begatom) 76 bnd.append(endatom) 77 bType=bond.GetOrder()#integer bond type 78 bnd.append(bType) 79 connTab.append(bnd) 80 81 atSymQ=[] 82 for atom in molecule.GetAtoms(): 83 asq=[] 84 num=atom.GetIdx()+1 85 asq.append(num) 86 sym=atom.GetAtomicNum() 87 asq.append(sym) 88 q=atom.GetFormalCharge()#atom charge 89 asq.append(q) 90 atSymQ.append(asq) 91 return connTab,atSymQ
92
93 - def CanSmi(self,mol,iso,kek,verbose):
94 """ 95 Create canonical smile (unique or absolute) 96 INPUT: 97 mol - molecule class object 98 iso - boolean, create isomeric SMILE 99 kek - boolean, Kekule aromatic form 100 varbose - boolean, show warnings 101 OUTPUT: 102 smile string 103 """ 104 smiflag=OESMILESFlag_Canonical 105 if iso: 106 smiflag|=OESMILESFlag_ISOMERIC 107 OEPerceiveChiral(mol) 108 for atom in mol.GetAtoms(): 109 IsChiral=atom.IsChiral() 110 HasStereo=atom.HasStereoSpecified() 111 112 if HasStereo and not IsChiral: 113 if verbose: 114 OEThrow.Warning("correcting atom stereo inconsistency!") 115 nbrs=[] 116 for bond in atom.GetBonds(): 117 nbrs.append(bond.GetNbr(atom)) 118 atom.SetStereo(nbrs,OEAtomStereo_Tetrahedral,OEBondStereo_Undefined) 119 120 for bond in mol.GetBonds(): 121 if bond.GetOrder()!=2: continue 122 HasStereoSpecified=bond.HasStereoSpecified() 123 IsChiral=bond.IsChiral() 124 if HasStereoSpecified and not IsChiral: 125 if verbose: 126 OEThrow.Warning("correcting bond stereo inconsistency!") 127 a1=bond.GetBgn() 128 a2=bond.GetEnd() 129 bond.SetStereo([a1,a2],OEBondStereo_CisTrans,OEBondStereo_Undefined) 130 131 if kek: 132 OEFindRingAtomsAndBonds(mol) 133 OEAssignAromaticFlags(mol,OEAroModelOpenEye) 134 for bond in mol.GetBonds(OEIsAromaticBond()): 135 bond.SetIntType(5) 136 OECanonicalOrderAtoms(mol) 137 OECanonicalOrderBonds(mol) 138 OEClearAromaticFlags(mol) 139 OEKekulize(mol) 140 141 smi=OECreateSmiString(mol,smiflag) 142 return smi
143
144 - def CanMol(self,mol,kek=False,aromodel=OEAroModelOpenEye,verbose=0):
145 """ 146 Create canonical connection table 147 INPUT: 148 mol - molecule class object 149 kek - boolean, Kekule aromatic form 150 aromodel - aromatic OpenEye model, default OEAroModelOpenEye 151 varbose - boolean, show warnings 152 OUPTUT: 153 molecule object 154 """ 155 for atom in mol.GetAtoms(): 156 if atom.HasStereoSpecified(OEAtomStereo_Tetra): 157 nbrs=OEAtomVector() 158 for bond in atom.GetBonds(): 159 nbrs.append(bond.GetNbr(atom)) 160 if len(nbrs)<3: OEThrow.Info('ERROR: stereo nbrs <3') 161 atom.SetStereo(nbrs,OEAtomStereo_Tetra,OEAtomStereo_Undefined) 162 163 for bond in mol.GetBonds(): 164 if bond.HasStereoSpecified(OEBondStereo_CisTrans): 165 nbrs=OEAtomVector() 166 nbrs.append(bond.GetBgn()) 167 nbrs.append(bond.GetEnd()) 168 bond.SetStereo(nbrs,OEBondStereo_CisTrans,OEBondStereo_Undefined) 169 OEMDLClearParity(mol) 170 OEMDLPerceiveParity(mol) ## Set MDL atom parity from OEChem stereo 171 paritytag=OEGetTag("MDLParity") 172 for atom in mol.GetAtoms(): 173 atom.SetIntData(paritytag,0) ## do not set the parity=3 174 OEMDLPerceiveBondStereo(mol) ## Set wedge/hash from OEChem stereo 175 OECanonicalOrderAtoms(mol) 176 OECanonicalOrderBonds(mol) 177 mol.Sweep() 178 179 if kek: 180 OEFindRingAtomsAndBonds(mol) 181 OEAssignAromaticFlags(mol,aromodel) 182 for bond in mol.GetBonds(OEIsAromaticBond()): 183 bond.SetIntType(5) 184 OEClearAromaticFlags(mol) 185 OEKekulize(mol) 186 return mol
187
188 - def addCoords(self,mol):
189 """ 190 add x,y,z coordinates 191 INPUT: 192 mol - molecule object 193 OUTPUT: 194 molecule object 195 """ 196 OESetDimensionFromCoords(mol) 197 OEAddDepictionHydrogens(mol) 198 OEDepictCoordinates(mol) 199 OEMDLPerceiveBondStereo(mol) 200 return mol
201
202 - def getCoords(self,mol):
203 """ 204 get x,y,z coordinates 205 INPUT: 206 mol - molecule object 207 OUTPUT: 208 result - dictionary with num,x,y,x atnum q list 209 """ 210 coords=[] 211 result={} 212 for atom in mol.GetAtoms():#iterate atoms 213 coord=[] 214 num=atom.GetIdx()+1#atom number in SDF 215 coord.append(num) 216 xyz=mol.GetCoords(atom)#atom coordinate 217 xyz=list(xyz)#change tuple to list 218 coord.extend(xyz) 219 sym=atom.GetAtomicNum()#atom symbol 220 coord.append(sym) 221 q=atom.GetFormalCharge()#atom charge 222 coord.append(q) 223 coords.append(coord) 224 result['coords']=coords 225 return result
226
227 - def getCoords1(self,mol):
228 """ 229 get x,y,z coordinates 230 INPUT: 231 mol - molecule object 232 OUTPUT: 233 result - dictionary with num,x,y,x atnum q list 234 """ 235 coords=[] 236 result={} 237 for atom in mol.GetAtoms():#iterate atoms 238 coord=[] 239 num=atom.GetIdx()+1#atom number in SDF 240 coord.append(num) 241 xyz=mol.GetCoords(atom)#atom coordinate 242 xyz=list(xyz)#change tuple to list 243 coord.extend(xyz) 244 sym=atom.GetAtomicNum()#atom symbol 245 coord.append(sym) 246 q=atom.GetFormalCharge()#atom charge 247 coord.append(q) 248 atom1=OEAtomGetResidue(atom) 249 atsernum=atom1.GetSerialNumber()#get atomic number 250 coord.append(atsernum) 251 atname=atom.GetName() 252 atname=atname.strip() 253 coord.append(atname) 254 atoccup=atom1.GetOccupancy() 255 coord.append(atoccup) 256 atbfactor=atom1.GetBFactor() 257 coord.append(atbfactor) 258 ataltloc=atom1.GetAlternateLocation() 259 coord.append(ataltloc) 260 atinscode=atom1.GetInsertCode() 261 coord.append(atinscode) 262 coords.append(coord) 263 result['coords']=coords 264 return result
265
266 - def getBonds(self,mol):
267 """ 268 get bonds 269 INPUT; 270 mol - molecule object 271 OUTPUT: 272 result - dictionary with begatom endatom bondType list 273 """ 274 result={} 275 bonds=[] 276 for bond in mol.GetBonds():#iterate bonds 277 bnd=[] 278 begatom=bond.GetBgnIdx()+1#begin bond atom 279 endatom=bond.GetEndIdx()+1#end bond atom 280 if begatom>endatom: 281 begatom,endatom=endatom,begatom 282 bnd.append(begatom) 283 bnd.append(endatom) 284 bType=bond.GetIntType()#integer bond type 285 bnd.append(bType) 286 bonds.append(bnd) 287 result['bonds']=bonds 288 return result
289
290 - def addH(self,mol,aromaticflag=OEAroModelOpenEye):
291 """ 292 add hydrogens 293 INPUT: 294 mol - molecule class object 295 OUTPUT: 296 mol - molecule class object 297 """ 298 OEAssignAromaticFlags(mol,aromaticflag) 299 OEFindRingAtomsAndBonds(mol) 300 if not OEHasImplicitHydrogens(mol): 301 OEAssignImplicitHydrogens(mol) 302 OEAssignFormalCharges(mol) 303 OEAddExplicitHydrogens(mol) 304 return mol
305
306 - def getAtoms(self,mol):
307 """ 308 get atoms 309 INPUT: 310 mol - molecule class object 311 OUTPUT: 312 atSymQ - dict, atom symbol, charge dictionary 313 """ 314 result={} 315 atSymQ=[] 316 for atom in mol.GetAtoms(): 317 asq=[] 318 num=atom.GetIdx()+1 319 asq.append(num) 320 sym=atom.GetAtomicNum() 321 asq.append(sym) 322 q=atom.GetFormalCharge()#atom charge 323 asq.append(q) 324 atSymQ.append(asq) 325 result['symq']=atSymQ 326 return result
327 ################## End of class ######################################################## 328 ############## MAIN ################################################################ 329 ############ Example of usage ########################################################## 330 if __name__=='__main__': 331 pass 332 # smiles='COc1ccc(cc1OC2CCCC2)[C@@H]3CC(=O)NC3' 333 # smiles='CC(=O)[O-]' 334 # smiles='CC' 335 # A=Smile(smile=smiles) 336 # A.addH(mol=A.getMol()) 337 # b=A.addCoords(mol=A.getMol()) 338 # c=A.getCoords(b) 339 # print c 340 # isosmi=A.CanSmi(mol=A.getMol(),iso=True,kek=False,verbose=True)#create isomeric smile 341 # smiobj=Smile(smile=smiles) 342 # mol=smiobj.CanMol(mol=A.getMol(),kek=False,aromodel=OEAroModelOpenEye,verbose=0) 343 # for bond in mol.GetBonds(): 344 # bond.SetIntType(bond.GetOrder()) 345 # a,b=smiobj.Smi2ConnTab(mol=A.getMol()) 346 # print a 347 # print b 348 # d=A.getBonds(mol=A.getMol()) 349 # print d 350