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$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #     All Rights Reserved
     5  #
     6  from __future__ import print_function
     7  from rdkit import RDConfig
     8  from rdkit import Chem
     9  import os.path
    10
    11  from rdkit.VLib.NodeLib import *
    12  from rdkit.VLib import Supply, Filter
    13
    14  # this would be a real input, from an sd file:
    15  #fName = os.path.join(RDConfig.RDCodeDir,'VLib','NodeLib','test_data','NCI_aids.10.dupes.sdf')
    16  #supplier = SDSupply.SDSupplyNode(fName)
    17  # instead though, we want a simpler input:
    18  smis = ['CCOC', 'CCO.Cl', 'CC(=O)[O-].[Na+]', 'CC[Cu]CC', 'OCC', 'C[N+](C)(C)C.[Cl-]',
    19          '[Na+].[Cl-]']
    20  mols = [Chem.MolFromSmiles(x) for x in smis]
    21  # name the molecules (only needed because we built them from smiles):
    22  for i in range(len(mols)):
    23    mols[i].SetProp('Name', 'Mol-%d' % (i + 1))
    24  supplier = Supply.SupplyNode(contents=mols)
    25  # should be 7 here
    26  print('initial:', len([x for x in supplier]))
    27
    28  # filter out anything with a transition metal or lanthanide:
    29  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]'
    30  smaFilter = SmartsMolFilter.SmartsFilter(patterns=[metals], counts=[1])
    31  smaFilter.SetNegate(1)
    32  smaFilter.AddParent(supplier)
    33  # should be 6 here
    34  print('post-smaFilter:', len([x for x in smaFilter]))
    35
    36  salts = ['[Cl;H1&X1,-]', '[Na+]', '[O;H2,H1&-,X0&-2]']
    37  remover = SmartsRemover.SmartsRemover(patterns=salts)
    38  remover.AddParent(smaFilter)
    39  atsFilter = Filter.FilterNode(func=lambda x: x.GetNumAtoms() > 1)
    40  atsFilter.AddParent(remover)
    41  # should be 5 here
    42  print('post-remover:', len([x for x in atsFilter]))
    43
    44  dupeFilter = SmilesDupeFilter.DupeFilter()
    45  dupeFilter.AddParent(atsFilter)
    46  # should be 4 here
    47  print('post-dupes:', len([x for x in dupeFilter]))
    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