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

Source Code for Module rdkit.Chem.Randomize

 1  # $Id: Randomize.py 1528 2010-09-26 17:04:37Z glandrum $ 
 2  # 
 3  #  Copyright (C) 2005-2006 Rational Discovery LLC 
 4  # 
 5  #   @@ All Rights Reserved @@ 
 6  #  This file is part of the RDKit. 
 7  #  The contents are covered by the terms of the BSD license 
 8  #  which is included in the file license.txt, found at the root 
 9  #  of the RDKit source tree. 
10  # 
11  import random 
12  from rdkit import Chem 
13   
14 -def RandomizeMolBlock(molB):
15 splitB = molB.split('\n') 16 res = [] 17 res.extend(splitB[0:3]) 18 idx = 3 19 inL = splitB[idx] 20 res.append(inL) 21 nAts = int(inL[0:3]) 22 nBonds = int(inL[3:6]) 23 24 idx+=1 25 atLines = splitB[idx:idx+nAts] 26 27 order = range(nAts) 28 random.shuffle(order) 29 30 for i in order: 31 res.append(atLines[i]) 32 33 #print 'ORDER:',order 34 idx += nAts 35 for i in range(nBonds): 36 inL = splitB[idx] 37 idx1 = int(inL[0:3])-1 38 idx2 = int(inL[3:6])-1 39 idx1 = order.index(idx1) 40 idx2 = order.index(idx2) 41 inL = '% 3d% 3d'%(idx1+1,idx2+1)+inL[6:] 42 res.append(inL) 43 idx += 1 44 res.append('M END') 45 return '\n'.join(res)
46
47 -def RandomizeMol(mol):
48 mb = Chem.MolToMolBlock(mol) 49 #print '-----------------' 50 #print mb 51 mb = RandomizeMolBlock(mb) 52 #print mb 53 return Chem.MolFromMolBlock(mb)
54
55 -def CheckCanonicalization(mol,nReps=10):
56 refSmi = Chem.MolToSmiles(mol,False) 57 for i in range(nReps): 58 m2 = RandomizeMol(mol) 59 smi = Chem.MolToSmiles(m2,False) 60 if smi!=refSmi: 61 raise ValueError,'\nRef: %s\n : %s'%(refSmi,smi)
62 63 64 65 if __name__=='__main__': 66 from rdkit.Chem import Randomize 67 CheckCanonicalization(Chem.MolFromSmiles('CON')) 68 CheckCanonicalization(Chem.MolFromSmiles('c1ccccn1')) 69 CheckCanonicalization(Chem.MolFromSmiles('C/C=C/F')) 70