rdkit.Chem

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

# Source Code for Package rdkit.Chem

      1  ## Automatically adapted for numpy.oldnumeric Jun 27, 2008 by -c
      2
      3  # $Id: __init__.py 997 2009-02-25 06:12:43Z glandrum $
      4  #
      5  #  Copyright (C) 2000-2008  greg Landrum and Rational Discovery LLC
      6  #
      7  #   @@ All Rights Reserved  @@
      8  #
      9  """ A module for molecules and stuff
     10
     11   see Chem/index.html in the doc tree for documentation
     12
     13  """
     14  from rdkit import rdBase
     15  from rdkit import RDConfig
     16
     17  from rdkit import DataStructs
     18  from rdkit.Geometry import rdGeometry
     19  import PeriodicTable as pyPeriodicTable
     20  import rdchem
     21  _HasSubstructMatchStr=rdchem._HasSubstructMatchStr
     22  from rdchem import *
     23  from rdmolfiles import *
     24  from rdmolops import *
     25
     26 -def GetSmartsMatchCDXML(mol,patt,maps,which=0,showAllAtoms=0):
     27    try:
     28      from rdkit.Chem import CDXMLWriter
     29    except:
     30      CDXMLWriter = None
     31    if CDXMLWriter is None:
     32      return ''
     33    try:
     34      from cStringIO import StringIO
     35    except:
     36      from StringIO import StringIO
     37    if not showAllAtoms and (which < 0 or not len(maps) or len(maps)<which+1):
     38      return ''
     39    io = StringIO()
     40    matchBonds = []
     41    matchAtoms = []
     42    if not showAllAtoms:
     43      mapL = maps[which]
     44      for bondIdx in range(patt.GetNumBonds()):
     45        bnd = patt.GetBondWithIdx(bondIdx)
     46        begIdx = bnd.GetBeginAtomIdx()
     47        endIdx = bnd.GetEndAtomIdx()
     48        molBegIdx = mapL[begIdx]
     49        molEndIdx = mapL[endIdx]
     50        matchBonds.append(mol.GetBondBetweenAtoms(molBegIdx,molEndIdx).GetIdx())
     51    else:
     52      for mapL in maps:
     53        for idx in mapL:
     54          if idx not in matchAtoms: matchAtoms.append(idx)
     55
     56    CDXMLWriter.MolToCDXML(mol,io,highlightBonds=matchBonds,
     57                           highlightAtoms=matchAtoms)
     58    cdxml = io.getvalue()
     59    cdxml = cdxml.replace('><','>\n<')
     60    return cdxml
     61
     62 -def DisplaySmartsMatch(mol,patt,maps,which=0,showAllAtoms=0):
     63    try:
     64      from rdkit.utils import chemdraw
     65    except:
     66      chemdraw = None
     67    cdxml = GetSmartsMatchCDXML(mol,patt,maps,which=which,showAllAtoms=showAllAtoms)
     68    if cdxml and chemdraw:
     69      chemdraw.CDXDisplay(cdxml)
     70    return cdxml
     71
     72 -def QuickSmartsMatch(smi,sma,unique=1,display=0):
     73    m = MolFromSmiles(smi)
     74    p = MolFromSmarts(sma)
     75    res = m.GetSubstructMatches(p,unique)
     76    if display:
     77      DisplaySmartsMatch(m,p,res)
     78    return res
     79
     80 -def CanonSmiles(smi,useChiral=1):
     81    m = MolFromSmiles(smi)
     82    return MolToSmiles(m,useChiral)
     83
     84 -def SmilesRoundtrip(smi,useChiral=1):
     85    m = MolFromSmiles(smi)
     86    refSmi = MolToSmiles(m,useChiral)
     87    m2 = MolFromSmiles(refSmi)
     88    smi = MolToSmiles(m2,useChiral)
     89    if smi!=refSmi:
     90      print refSmi
     91      print smi
     92    return refSmi==smi
     93
     94 -def SupplierFromFilename(fileN,delim='',**kwargs):
     95    ext = fileN.split('.')[-1].lower()
     96    if ext=='sdf':
     97      suppl = SDMolSupplier(fileN,**kwargs)
     98    elif ext=='csv':
     99      if not delim:
    100        delim = ','
    101      suppl = SmilesMolSupplier(fileN,delimiter=delim,**kwargs)
    102    elif ext=='txt':
    103      if not delim:
    104        delim='\t'
    105      suppl = SmilesMolSupplier(fileN,delimiter=delim,**kwargs)
    106    elif ext=='tdt':
    107      suppl = TDTMolSupplier(fileN,delimiter=delim,**kwargs)
    108    else:
    109      raise ValueError,"unrecognized extension: %s"%ext
    110
    111    return suppl
    112
    113 -def FindMolChiralCenters(mol):
    114    """
    115      >>> mol = Chem.MolFromSmiles('[C@H](Cl)(F)Br')
    116      >>> FindMolChiralCenters(mol)
    117      [(0, 'R')]
    118      >>> mol = Chem.MolFromSmiles('[C@@H](Cl)(F)Br')
    119      >>> FindMolChiralCenters(mol)
    120      [(0, 'S')]
    121
    122      >>> FindMolChiralCenters(Chem.MolFromSmiles('CCC'))
    123      []
    124
    125    """
    126    AssignAtomChiralCodes(mol)
    127    centers = []
    128    for atom in mol.GetAtoms():
    129      if atom.HasProp('_CIPCode'):
    130        centers.append((atom.GetIdx(),atom.GetProp('_CIPCode')))
    131    return centers
    132