Package rdkit :: Package Chem :: Module Randomize

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.Randomize

     1  # $Id$
     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.six.moves import range
    13  from rdkit import Chem
    14
    15 -def RandomizeMolBlock(molB):
    16    splitB = molB.split('\n')
    17    res = []
    18    res.extend(splitB[0:3])
    19    idx = 3
    20    inL = splitB[idx]
    21    res.append(inL)
    22    nAts = int(inL[0:3])
    23    nBonds = int(inL[3:6])
    24
    25    idx+=1
    26    atLines = splitB[idx:idx+nAts]
    27
    28    order = list(range(nAts))
    29    random.shuffle(order,random=random.random)
    30
    31    for i in order:
    32      res.append(atLines[i])
    33
    34    #print 'ORDER:',order
    35    idx += nAts
    36    for i in range(nBonds):
    37      inL = splitB[idx]
    38      idx1 = int(inL[0:3])-1
    39      idx2 = int(inL[3:6])-1
    40      idx1 = order.index(idx1)
    41      idx2 = order.index(idx2)
    42      inL = '% 3d% 3d'%(idx1+1,idx2+1)+inL[6:]
    43      res.append(inL)
    44      idx += 1
    45    res.append('M  END')
    46    return '\n'.join(res)
    47
    48 -def RandomizeMol(mol):
    49    mb = Chem.MolToMolBlock(mol)
    50    #print '-----------------'
    51    #print mb
    52    mb = RandomizeMolBlock(mb)
    53    #print mb
    54    return Chem.MolFromMolBlock(mb)
    55
    56 -def CheckCanonicalization(mol,nReps=10):
    57    refSmi = Chem.MolToSmiles(mol,False)
    58    for i in range(nReps):
    59      m2 = RandomizeMol(mol)
    60      smi = Chem.MolToSmiles(m2,False)
    61      if smi!=refSmi:
    62        raise ValueError('\nRef: %s\n   : %s'%(refSmi,smi))
    63
    64
    65
    66  if __name__=='__main__':
    67    from rdkit.Chem import Randomize
    68    CheckCanonicalization(Chem.MolFromSmiles('CON'))
    69    CheckCanonicalization(Chem.MolFromSmiles('c1ccccn1'))
    70    CheckCanonicalization(Chem.MolFromSmiles('C/C=C/F'))
    71