rdkit.VLib.NodeLib.demo

Package rdkit :: Package VLib :: Package NodeLib :: Module demo |
[hide private]
[frames] | no frames]

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

     1  #  $Id: demo.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #     All Rights Reserved
     5  #
     6  from rdkit import RDConfig
     7  from rdkit import Chem
     8  import os.path
     9
    10  from rdkit.VLib.NodeLib import *
    11  from rdkit.VLib import Supply,Filter
    12
    13  # this would be a real input, from an sd file:
    14  #fName = os.path.join(RDConfig.RDCodeDir,'VLib','NodeLib','test_data','NCI_aids.10.dupes.sdf')
    15  #supplier = SDSupply.SDSupplyNode(fName)
    16  # instead though, we want a simpler input:
    17  smis = ['CCOC','CCO.Cl','CC(=O)[O-].[Na+]','CC[Cu]CC','OCC','C[N+](C)(C)C.[Cl-]',
    18          '[Na+].[Cl-]']
    19  mols = [Chem.MolFromSmiles(x) for x in smis]
    20  # name the molecules (only needed because we built them from smiles):
    21  for i in range(len(mols)):
    22    mols[i].SetProp('Name','Mol-%d'%(i+1))
    23  supplier = Supply.SupplyNode(contents=mols)
    24  # should be 7 here
    25  print 'initial:',len([x for x in supplier])
    26
    27  # filter out anything with a transition metal or lanthanide:
    28  metals = '[#21,#22,#23,#24,#25,#26,#27,#28,#29,#39,#40,#41,#42,#43,#44,#45,#46,#47,#57,#58,#59,#60,#61,#62,#63,#64,#65,#66,#67,#68,#69,#70,#71,#72,#73,#74,#75,#76,#77,#78,#79]'
    29  smaFilter= SmartsMolFilter.SmartsFilter(patterns=[metals],counts=[1])
    30  smaFilter.SetNegate(1)
    31  smaFilter.AddParent(supplier)
    32  # should be 6 here
    33  print 'post-smaFilter:',len([x for x in smaFilter])
    34
    35  salts = ['[Cl;H1&X1,-]','[Na+]','[O;H2,H1&-,X0&-2]']
    36  remover = SmartsRemover.SmartsRemover(patterns=salts)
    37  remover.AddParent(smaFilter)
    38  atsFilter = Filter.FilterNode(func=lambda x:x.GetNumAtoms()>1)
    39  atsFilter.AddParent(remover)
    40  # should be 5 here
    41  print 'post-remover:',len([x for x in atsFilter])
    42
    43  dupeFilter = SmilesDupeFilter.DupeFilter()
    44  dupeFilter.AddParent(atsFilter)
    45  # should be 4 here
    46  print 'post-dupes:',len([x for x in dupeFilter])
    47
    48
    49  import StringIO
    50  # a StringIO object acts like a file:
    51  io = StringIO.StringIO()
    52  output = SmilesOutput.OutputNode(dest=io,delim=', ',idField='Name')
    53  output.AddParent(dupeFilter)
    54  print 'post-output:',len([x for x in output])
    55  print 'OUTPUT:'
    56  print io.getvalue()
    57