rdkit.Chem.TemplateAlign

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

# Source Code for Module rdkit.Chem.TemplateAlign

      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  from rdkit import Chem
     12  from rdkit.Chem import rdDepictor
     13  from rdkit import Geometry
     14
     15
     16 -def AlignMolToTemplate2D(mol,
     17                           template,
     18                           match=None,
     19                           clearConfs=False,
     20                           templateConfId=-1, ):
     21    """
     22     Arguments:
     23
     24       - mol:      the molecule to be aligned
     25       - template: the template to align to
     26       - match:    If provided, this should be a sequence of integers
     27                   containing the indices of the atoms in mol that match
     28                   those in template. This is the result of calling:
     29                     mol.GetSubstructMatch(template)
     30       - clearConfs: toggles removing any existing conformers on mol
     31
     32      Returns the confId of the conformer containing the depiction
     33
     34      >>> patt = Chem.MolFromSmiles('C1CC1')
     35      >>> rdDepictor.Compute2DCoords(patt)
     36      0
     37      >>> mol = Chem.MolFromSmiles('OC1CC1CC1CCC1')
     38      >>> rdDepictor.Compute2DCoords(mol)
     39      0
     40      >>> pc = patt.GetConformer(0)
     41      >>> mc = mol.GetConformer(0)
     42
     43      We start out with the molecules not aligned:
     44      >>> vs = [abs(pc.GetAtomPosition(i).x-mc.GetAtomPosition(i+1).x) for i in range(pc.GetNumAtoms())]
     45      >>> [x<1e-4 for x in vs]
     46      [False, False, False]
     47
     48      But then we can replace the conformer of mol:
     49      >>> AlignMolToTemplate2D(mol,patt,clearConfs=True)
     50      0
     51      >>> mol.GetNumConformers()
     52      1
     53      >>> pc = patt.GetConformer(0)
     54      >>> mc = mol.GetConformer(0)
     55      >>> vs = [abs(pc.GetAtomPosition(i).x-mc.GetAtomPosition(i+1).x) for i in range(pc.GetNumAtoms())]
     56      >>> [x<1e-4 for x in vs]
     57      [True, True, True]
     58
     59      If we like, we can specify the atom map explicitly in order to align to the second
     60      matching ring in the probe molecule:
     61      >>> match = (5,6,7)
     62      >>> AlignMolToTemplate2D(mol,patt,clearConfs=True,match=match)
     63      0
     64      >>> mol.GetNumConformers()
     65      1
     66      >>> pc = patt.GetConformer(0)
     67      >>> mc = mol.GetConformer(0)
     68      >>> vs = [abs(pc.GetAtomPosition(i).x-mc.GetAtomPosition(i+1).x) for i in range(pc.GetNumAtoms())]
     69      >>> [x<1e-4 for x in vs]
     70      [False, False, False]
     71      >>> vs = [abs(pc.GetAtomPosition(i).x-mc.GetAtomPosition(i+5).x) for i in range(pc.GetNumAtoms())]
     72      >>> [x<1e-4 for x in vs]
     73      [True, True, True]
     74
     75
     76
     77    """
     78    if not match:
     79      match = mol.GetSubstructMatch(template)
     80    if not match:
     81      raise ValueError('no match between mol and template')
     82
     83    atomMap = {}
     84    templateConf = template.GetConformer(templateConfId)
     85    for i, idx in enumerate(match):
     86      p = templateConf.GetAtomPosition(i)
     87      atomMap[idx] = Geometry.Point2D(p.x, p.y)
     88    molConfId = rdDepictor.Compute2DCoords(mol, clearConfs=clearConfs, coordMap=atomMap)
     89    return molConfId
     90
     91
     92  #------------------------------------
     93  #
     94  #  doctest boilerplate
     95  #
     96 -def _test():
     97    import doctest, sys
     98    return doctest.testmod(sys.modules["__main__"])
     99
    100
    101  if __name__ == '__main__':
    102    import sys
    103    failed, tried = _test()
    104    sys.exit(failed)
    105