rdkit.Chem.Fragments

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

# Source Code for Module rdkit.Chem.Fragments

     1  # $Id: Fragments.py 997 2009-02-25 06:12:43Z glandrum $
     2  #
     3  # Copyright (C) 2002-2006 greg Landrum and Rational Discovery LLC
     4  #
     5  #   @@ All Rights Reserved  @@
     6  #
     7  """ functions to match a bunch of fragment descriptors from a file
     8
     9  No user-servicable parts inside.  ;-)
    10
    11  """
    12  import os
    13  from rdkit import RDConfig
    14  from rdkit import Chem
    15
    16
    17  defaultPatternFileName = os.path.join(RDConfig.RDDataDir,'FragmentDescriptors.csv')
    18
    19 -def _CountMatches(mol,patt,unique=1):
    20    res = 0
    21    res = len(mol.GetSubstructMatches(patt))
    22    return res
    23
    24  fns = []
    25 -def _LoadPatterns(fileName=None):
    26    if fileName is None:
    27      fileName = defaultPatternFileName
    28    try:
    29      inF = open(fileName,'r')
    30    except IOError:
    31      pass
    32    else:
    33      for line in inF.readlines():
    34        if len(line) and line[0] != '#':
    35          splitL = line.split('\t')
    36          if len(splitL)>=3:
    37            name = splitL[0]
    38            descr = splitL[1]
    39            sma = splitL[2]
    40            descr=descr.replace('"','')
    41            ok=1
    42            try:
    43              patt = Chem.MolFromSmarts(sma)
    44            except:
    45              ok=0
    46            else:
    47              if not patt or patt.GetNumAtoms()==0: ok=0
    48            if not ok: raise ImportError,'Smarts %s could not be parsed'%(repr(sma))
    49            fn = lambda x,y=1,z=patt:_CountMatches(x,z,unique=y)
    50            fn.__doc__ = descr
    51            name = name.replace('=','_')
    52            name = name.replace('-','_')
    53            fns.append((name,fn))
    54
    55  _LoadPatterns()
    56  for name,fn in fns:
    57    exec('%s=fn'%(name))
    58  fn=None
    59