Package rdkit :: Package Chem :: Package ChemUtils :: Module AlignDepict

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.Chem.ChemUtils.AlignDepict

     1  # $Id: AlignDepict.py 736 2008-02-14 14:09:36Z landrgr1 $
     2  #
     3  #  Copyright (C) 2006 Greg Landrum
     4  #  This file is part of RDKit and covered by $RDBASE/license.txt
     5  #
     6  from __future__ import print_function
     7  from rdkit import Chem
     8  from rdkit.Chem import rdDepictor
     9  from rdkit import Geometry
    10
    11 -def AlignDepict(mol,core,corePattern=None,acceptFailure=False):
    12    """
    13
    14    Arguments:
    15      - mol:          the molecule to be aligned, this will come back
    16                      with a single conformer.
    17      - core:         a molecule with the core atoms to align to;
    18                      this should have a depiction.
    19      - corePattern:  (optional) an optional molecule to be used to
    20                      generate the atom mapping between the molecule
    21                      and the core.
    22    """
    23    if core and corePattern:
    24      if not core.GetNumAtoms(onlyExplicit=True)==corePattern.GetNumAtoms(onlyExplicit=True):
    25        raise ValueError('When a pattern is provided, it must have the same number of atoms as the core')
    26      coreMatch = core.GetSubstructMatch(corePattern)
    27      if not coreMatch:
    28        raise ValueError("Core does not map to itself")
    29    else:
    30      coreMatch = range(core.GetNumAtoms(onlyExplicit=True))
    31    if corePattern:
    32      match = mol.GetSubstructMatch(corePattern)
    33    else:
    34      match = mol.GetSubstructMatch(core)
    35
    36    if not match:
    37      if not acceptFailure:
    38        raise ValueError('Substructure match with core not found.')
    39      else:
    40        coordMap={}
    41    else:
    42      conf = core.GetConformer()
    43      coordMap={}
    44      for i,idx in enumerate(match):
    45        pt3 = conf.GetAtomPosition(coreMatch[i])
    46        pt2 = Geometry.Point2D(pt3.x,pt3.y)
    47        coordMap[idx] = pt2
    48    rdDepictor.Compute2DCoords(mol,clearConfs=True,coordMap=coordMap,canonOrient=False)
    49
    50  if __name__=='__main__':
    51    import sys,getopt
    52
    53 -  def Usage():
    54      pass
    55
    56    args,extras = getopt.getopt(sys.argv[1:],'p:ho:',['smiles','pattern='])
    57    if len(extras)!=2:
    58      print('ERROR: Not enough arguments', file=sys.stderr)
    59      Usage()
    60      sys.exit(1)
    61    patt = None
    62    useSmiles = False
    63    outF=None
    64    for arg,val in args:
    65      if arg=='-h':
    66        Usage()
    67        sys.exit(0)
    68      elif arg=='-p' or arg=='--pattern':
    69        patt = Chem.MolFromSmarts(val)
    70      elif arg=='--smiles':
    71        useSmiles = True
    72      elif arg=='-o':
    73        outF = val
    74
    75    if not useSmiles:
    76      core = Chem.MolFromMolFile(extras[0])
    77    else:
    78      core = Chem.MolFromSmiles(extras[0])
    79      rdDepictor.Compute2DCoords(core)
    80
    81    if not useSmiles:
    82      mol = Chem.MolFromMolFile(extras[1])
    83    else:
    84      mol = Chem.MolFromSmiles(extras[1])
    85
    86    AlignDepict(mol,core,patt)
    87
    88    if outF:
    89      outF = open(outF,'w+')
    90    else:
    91      outF = sys.stdout
    92
    93    print(Chem.MolToMolBlock(mol), file=outF)
    94