rdkit.Chem.Randomize

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

# Source Code for Module rdkit.Chem.Randomize

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