Chem.ChemUtils.BulkTester

Package Chem :: Package ChemUtils :: Module BulkTester |
[hide private]
[frames] | no frames]

# Source Code for Module Chem.ChemUtils.BulkTester

     1  # $Id: BulkTester.py 2 2006-05-06 22:54:39Z glandrum $

     2  #

     3  #  Copyright (C) 2005-2006 Rational Discovery LLC

     4  #

     5  #   @@ All Rights Reserved  @@

     6  #

     7  import Chem
     8  import sys
     9  from Chem import Randomize
    10

    11 -def TestMolecule(mol):
    12    try:
    13      Chem.SanitizeMol(mol)
    14      mol = Chem.RemoveHs(mol)
    15    except ValueError,msg:
    16      return -1
    17    except:
    18      import traceback
    19      traceback.print_exc()
    20      return -2
    21    if mol.GetNumAtoms():
    22      try:
    23        Randomize.CheckCanonicalization(mol,10)
    24      except:
    25        import traceback
    26        traceback.print_exc()
    27        return -3
    28    return 0
    29

    30

    31 -def TestSupplier(suppl,stopAfter=-1,reportInterval=100,reportTo=sys.stderr,

    32                   nameProp='_Name'):
    33    nDone = 0
    34    nFailed = 0
    35    while 1:
    36      try:
    37        mol = suppl.next()
    38      except StopIteration:
    39        break
    40      except:
    41        import traceback
    42        traceback.print_exc()
    43        nFailed += 1
    44        reportTo.flush()
    45        print >>reportTo,'Failure at mol %d'%nDone
    46      else:
    47        if mol:
    48          ok = TestMolecule(mol)
    49        else:
    50          ok = -3
    51        if ok<0:
    52          nFailed += 1
    53          reportTo.flush()
    54          if ok==-3:
    55            print >>reportTo,'Canonicalization',
    56          print >>reportTo,'Failure at mol %d'%nDone,
    57          if mol:
    58            print >>reportTo,mol.GetProp(nameProp),
    59          print >>reportTo,''
    60

    61

    62      nDone += 1
    63      if nDone==stopAfter:
    64        break
    65      if not nDone%reportInterval:
    66        print 'Done %d molecules, %d failures'%(nDone,nFailed)
    67    return nDone,nFailed
    68  if __name__=='__main__':
    69    suppl = Chem.SDMolSupplier(sys.argv[1],False)
    70    if len(sys.argv)>2:
    71      nameProp = sys.argv[2]
    72    else:
    73      nameProp = '_Name'
    74

    75    nDone,nFailed = TestSupplier(suppl,nameProp=nameProp)
    76    print '%d failures in %d mols'%(nFailed,nDone)
    77