rdkit.Chem.Randomize

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