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 -def TestMolecule(mol):
    18    try:
    19      Chem.SanitizeMol(mol)
    20      mol = Chem.RemoveHs(mol)
    21    except ValueError as msg:
    22      return -1
    23    except Exception:
    24      import traceback
    25      traceback.print_exc()
    26      return -2
    27    if mol.GetNumAtoms():
    28      try:
    29        Randomize.CheckCanonicalization(mol,10)
    30      except Exception:
    31        import traceback
    32        traceback.print_exc()
    33        return -3
    34    return 0
    35
    36
    37 -def TestSupplier(suppl,stopAfter=-1,reportInterval=100,reportTo=sys.stderr,
    38                   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
    68      nDone += 1
    69      if nDone==stopAfter:
    70        break
    71      if not nDone%reportInterval:
    72        print('Done %d molecules, %d failures'%(nDone,nFailed))
    73    return nDone,nFailed
    74  if __name__=='__main__':
    75    suppl = Chem.SDMolSupplier(sys.argv[1],False)
    76    if len(sys.argv)>2:
    77      nameProp = sys.argv[2]
    78    else:
    79      nameProp = '_Name'
    80
    81    nDone,nFailed = TestSupplier(suppl,nameProp=nameProp)
    82    print('%d failures in %d mols'%(nFailed,nDone))
    83