rdkit.Chem

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

# Source Code for Package rdkit.Chem

      1  #
      2  #  Copyright (C) 2000-2017  greg Landrum and Rational Discovery LLC
      3  #
      4  #   @@ All Rights Reserved @@
      5  #  This file is part of the RDKit.
      6  #  The contents are covered by the terms of the BSD license
      7  #  which is included in the file license.txt, found at the root
      8  #  of the RDKit source tree.
      9  #
     10  """ A module for molecules and stuff
     11
     12   see Chem/index.html in the doc tree for documentation
     13
     14  """
     15  from rdkit import rdBase
     16  from rdkit import RDConfig
     17
     18  from rdkit import DataStructs
     19  from rdkit.Geometry import rdGeometry
     20  from rdkit.Chem import PeriodicTable as pyPeriodicTable
     21  from rdkit.Chem import rdchem
     22  _HasSubstructMatchStr = rdchem._HasSubstructMatchStr
     23  from rdkit.Chem.rdchem import *
     24  from rdkit.Chem.rdmolfiles import *
     25  from rdkit.Chem.rdmolops import *
     26  from rdkit.Chem.inchi import *
     27
     28
     29 -def QuickSmartsMatch(smi, sma, unique=True, display=False):
     30    m = MolFromSmiles(smi)
     31    p = MolFromSmarts(sma)
     32    res = m.GetSubstructMatches(p, unique)
     33    if display:
     34      pass
     35    return res
     36
     37
     38 -def CanonSmiles(smi, useChiral=1):
     39    m = MolFromSmiles(smi)
     40    return MolToSmiles(m, useChiral)
     41
     42
     43 -def SupplierFromFilename(fileN, delim='', **kwargs):
     44    ext = fileN.split('.')[-1].lower()
     45    if ext == 'sdf':
     46      suppl = SDMolSupplier(fileN, **kwargs)
     47    elif ext == 'csv':
     48      if not delim:
     49        delim = ','
     50      suppl = SmilesMolSupplier(fileN, delimiter=delim, **kwargs)
     51    elif ext == 'txt':
     52      if not delim:
     53        delim = '\t'
     54      suppl = SmilesMolSupplier(fileN, delimiter=delim, **kwargs)
     55    elif ext == 'tdt':
     56      suppl = TDTMolSupplier(fileN, delimiter=delim, **kwargs)
     57    else:
     58      raise ValueError("unrecognized extension: %s" % ext)
     59
     60    return suppl
     61
     62
     63 -def FindMolChiralCenters(mol, force=True, includeUnassigned=False):
     64    """
     65      >>> from rdkit import Chem
     66      >>> mol = Chem.MolFromSmiles('[C@H](Cl)(F)Br')
     67      >>> FindMolChiralCenters(mol)
     68      [(0, 'R')]
     69      >>> mol = Chem.MolFromSmiles('[C@@H](Cl)(F)Br')
     70      >>> FindMolChiralCenters(mol)
     71      [(0, 'S')]
     72
     73      >>> FindMolChiralCenters(Chem.MolFromSmiles('CCC'))
     74      []
     75
     76      By default unassigned stereo centers are not reported:
     77      >>> mol = Chem.MolFromSmiles('C[C@H](F)C(F)(Cl)Br')
     78      >>> FindMolChiralCenters(mol,force=True)
     79      [(1, 'S')]
     80
     81      but this can be changed:
     82      >>> FindMolChiralCenters(mol,force=True,includeUnassigned=True)
     83      [(1, 'S'), (3, '?')]
     84
     85      The handling of unassigned stereocenters for dependent stereochemistry is not correct:
     86      >>> Chem.FindMolChiralCenters(Chem.MolFromSmiles('C1CC(C)C(C)C(C)C1'),includeUnassigned=True)
     87      [(2, '?'), (6, '?')]
     88      >>> Chem.FindMolChiralCenters(Chem.MolFromSmiles('C1C[C@H](C)C(C)[C@H](C)C1'),includeUnassigned=True)
     89      [(2, 'S'), (4, '?'), (6, 'R')]
     90
     91    """
     92    AssignStereochemistry(mol, force=force, flagPossibleStereoCenters=includeUnassigned)
     93    centers = []
     94    for atom in mol.GetAtoms():
     95      if atom.HasProp('_CIPCode'):
     96        centers.append((atom.GetIdx(), atom.GetProp('_CIPCode')))
     97      elif includeUnassigned and atom.HasProp('_ChiralityPossible'):
     98        centers.append((atom.GetIdx(), '?'))
     99    return centers
    100
    101
    102  #------------------------------------
    103  #
    104  #  doctest boilerplate
    105  #
    106 -def _test():
    107    import doctest, sys
    108    return doctest.testmod(sys.modules["__main__"])
    109
    110
    111  if __name__ == '__main__':
    112    import sys
    113    failed, tried = _test()
    114    sys.exit(failed)
    115