rdkit.Chem.ChemUtils.BulkTester

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

# Source Code for Module rdkit.Chem.ChemUtils.BulkTester

     1  # $Id$
     2  #
     3  #  Copyright (C) 2005-2006 Rational Discovery LLC
     4  #
     5  #   @@ All Rights Reserved @@
     6  #  This file is part of the RDKit.
     7  #  The contents are covered by the terms of the BSD license
     8  #  which is included in the file license.txt, found at the root
     9  #  of the RDKit source tree.
    10  #
    11  from __future__ import print_function
    12
    13  from rdkit import Chem
    14  import sys
    15  from rdkit.Chem import Randomize
    16
    17
    18 -def TestMolecule(mol):
    19    try:
    20      Chem.SanitizeMol(mol)
    21      mol = Chem.RemoveHs(mol)
    22    except ValueError as msg:
    23      return -1
    24    except Exception:
    25      import traceback
    26      traceback.print_exc()
    27      return -2
    28    if mol.GetNumAtoms():
    29      try:
    30        Randomize.CheckCanonicalization(mol, 10)
    31      except Exception:
    32        import traceback
    33        traceback.print_exc()
    34        return -3
    35    return 0
    36
    37
    38 -def TestSupplier(suppl, stopAfter=-1, reportInterval=100, reportTo=sys.stderr, nameProp='_Name'):
    39    nDone = 0
    40    nFailed = 0
    41    while 1:
    42      try:
    43        mol = suppl.next()
    44      except StopIteration:
    45        break
    46      except Exception:
    47        import traceback
    48        traceback.print_exc()
    49        nFailed += 1
    50        reportTo.flush()
    51        print('Failure at mol %d' % nDone, file=reportTo)
    52      else:
    53        if mol:
    54          ok = TestMolecule(mol)
    55        else:
    56          ok = -3
    57        if ok < 0:
    58          nFailed += 1
    59          reportTo.flush()
    60          if ok == -3:
    61            print('Canonicalization', end='', file=reportTo)
    62          print('Failure at mol %d' % nDone, end='', file=reportTo)
    63          if mol:
    64            print(mol.GetProp(nameProp), end='', file=reportTo)
    65          print('', file=reportTo)
    66
    67      nDone += 1
    68      if nDone == stopAfter:
    69        break
    70      if not nDone % reportInterval:
    71        print('Done %d molecules, %d failures' % (nDone, nFailed))
    72    return nDone, nFailed
    73
    74
    75  if __name__ == '__main__':
    76    suppl = Chem.SDMolSupplier(sys.argv[1], False)
    77    if len(sys.argv) > 2:
    78      nameProp = sys.argv[2]
    79    else:
    80      nameProp = '_Name'
    81
    82    nDone, nFailed = TestSupplier(suppl, nameProp=nameProp)
    83    print('%d failures in %d mols' % (nFailed, nDone))
    84