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