Package rdkit :: Package VLib :: Package NodeLib :: Module SmartsRemover

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.VLib.NodeLib.SmartsRemover

      1  #  $Id$
      2  #
      3  #  Copyright (C) 2003 Rational Discovery LLC
      4  #     All Rights Reserved
      5  #
      6  from rdkit import RDConfig
      7  from rdkit import six
      8  import sys,os,types
      9  from rdkit import Chem
     10  from rdkit.VLib.Transform import TransformNode
     11
     12 -class SmartsRemover(TransformNode):
     13    """ transforms molecules by removing atoms matching smarts patterns
     14
     15    Assumptions:
     16
     17      - inputs are molecules
     18
     19
     20    Sample Usage:
     21      >>> smis = ['C1CCC1.C=O','C1CCC1C=O','CCC=O.C=O','NCC=O.C=O.CN']
     22      >>> mols = [Chem.MolFromSmiles(x) for x in smis]
     23      >>> from rdkit.VLib.Supply import SupplyNode
     24      >>> suppl = SupplyNode(contents=mols)
     25      >>> ms = [x for x in suppl]
     26      >>> len(ms)
     27      4
     28
     29      We can pass in SMARTS strings:
     30      >>> smas = ['C=O','CN']
     31      >>> tform = SmartsRemover(patterns=smas)
     32      >>> tform.AddParent(suppl)
     33      >>> ms = [x for x in tform]
     34      >>> len(ms)
     35      4
     36      >>> Chem.MolToSmiles(ms[0])
     37      'C1CCC1'
     38      >>> Chem.MolToSmiles(ms[1])
     39      'O=CC1CCC1'
     40      >>> Chem.MolToSmiles(ms[2])
     41      'CCC=O'
     42      >>> Chem.MolToSmiles(ms[3])
     43      'NCC=O'
     44
     45      We can also remove pieces of the molecule that are not complete
     46      fragments:
     47      >>> tform.Destroy()
     48      >>> smas = ['C=O','CN']
     49      >>> smas = [Chem.MolFromSmarts(x) for x in smas]
     50      >>> tform = SmartsRemover(patterns=smas,wholeFragments=0)
     51      >>> tform.AddParent(suppl)
     52      >>> ms = [x for x in tform]
     53      >>> len(ms)
     54      4
     55      >>> Chem.MolToSmiles(ms[0])
     56      'C1CCC1'
     57      >>> Chem.MolToSmiles(ms[1])
     58      'C1CCC1'
     59      >>> Chem.MolToSmiles(ms[3])
     60      ''
     61
     62      Or patterns themselves:
     63      >>> tform.Destroy()
     64      >>> smas = ['C=O','CN']
     65      >>> smas = [Chem.MolFromSmarts(x) for x in smas]
     66      >>> tform = SmartsRemover(patterns=smas)
     67      >>> tform.AddParent(suppl)
     68      >>> ms = [x for x in tform]
     69      >>> len(ms)
     70      4
     71      >>> Chem.MolToSmiles(ms[0])
     72      'C1CCC1'
     73      >>> Chem.MolToSmiles(ms[3])
     74      'NCC=O'
     75
     76
     77    """
     78 -  def __init__(self,patterns=[],wholeFragments=1,**kwargs):
     79      TransformNode.__init__(self,func=self.transform,**kwargs)
     80      self._wholeFragments = wholeFragments
     81      self._initPatterns(patterns)
     82
     83 -  def _initPatterns(self,patterns):
     84      nPatts = len(patterns)
     85      targets = [None]*nPatts
     86      for i in range(nPatts):
     87        p = patterns[i]
     88        if type(p) in (str,bytes):
     89          m = Chem.MolFromSmarts(p)
     90          if not m:
     91            raise ValueError('bad smarts: %s'%(p))
     92          p = m
     93        targets[i] = p
     94      self._patterns = tuple(targets)
     95
     96 -  def transform(self,cmpd):
     97      #sys.stderr.write('\tTRANSFORM: %s\n'%(Chem.MolToSmiles(cmpd)))
     98      for patt in self._patterns:
     99        cmpd = Chem.DeleteSubstructs(cmpd,patt,onlyFrags=self._wholeFragments)
    100        #sys.stderr.write('\t\tAfter %s: %s\n'%(Chem.MolToSmiles(patt),Chem.MolToSmiles(cmpd)))
    101
    102      return cmpd
    103
    104  biggerTest="""
    105  >>> smis = ['CCOC','CCO.Cl','CC(=O)[O-].[Na+]','OCC','C[N+](C)(C)C.[Cl-]']
    106  >>> mols = [Chem.MolFromSmiles(x) for x in smis]
    107  >>> from rdkit.VLib.Supply import SupplyNode
    108  >>> suppl = SupplyNode(contents=mols)
    109  >>> ms = [x for x in suppl]
    110  >>> len(ms)
    111  5
    112
    113  #>>> salts = ['[Cl;H1&X1,-]','[Na+]','[O;H2,H1&-,X0&-2]']
    114
    115  >>> salts = ['[Cl;H1&X1,-]','[Na+]','[O;H2,H1&-,X0&-2]']
    116  >>> m = mols[2]
    117  >>> m.GetNumAtoms()
    118  5
    119  >>> patts = [Chem.MolFromSmarts(x) for x in salts]
    120  >>> m2 = Chem.DeleteSubstructs(m,patts[0],1)
    121  >>> m2.GetNumAtoms()
    122  5
    123  >>> m2 = Chem.DeleteSubstructs(m2,patts[1],1)
    124  >>> m2.GetNumAtoms()
    125  4
    126  >>> m2 = Chem.DeleteSubstructs(m2,patts[2],1)
    127  >>> m2.GetNumAtoms()
    128  4
    129
    130  >>> tform = SmartsRemover(patterns=salts)
    131  >>> tform.AddParent(suppl)
    132  >>> ms = [x for x in tform]
    133  >>> len(ms)
    134  5
    135
    136  """
    137
    138  #------------------------------------
    139  #
    140  #  doctest boilerplate
    141  #
    142  __test__={'bigger':biggerTest}
    143 -def _test():
    144    import doctest,sys
    145    return doctest.testmod(sys.modules["__main__"])
    146
    147
    148  if __name__ == '__main__':
    149    import sys
    150    failed,tried = _test()
    151    sys.exit(failed)
    152