rdkit.Chem.FeatFinderCLI

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

# Source Code for Module rdkit.Chem.FeatFinderCLI

      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  _version = "$Rev$"
     14  _splashMessage="""
     15  -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     16    FeatFinderCLI version %s
     17
     18    Copyright (C) 2005 Rational Discovery LLC
     19
     20    This software is copyrighted.  The software may not be copied,
     21    reproduced, translated or reduced to any electronic medium or
     22    machine-readable form without the prior written consent of
     23    Rational Discovery LLC.
     24  -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     25  """%_version
     26  from rdkit import Chem
     27  from rdkit.Chem import ChemicalFeatures
     28  from rdkit import RDLogger
     29  logger = RDLogger.logger()
     30  import sys,os
     31  import re
     32  splitExpr = re.compile(r'[ \t,]')
     33
     34 -def GetAtomFeatInfo(factory,mol):
     35    res = [None]*mol.GetNumAtoms()
     36    feats = factory.GetFeaturesForMol(mol)
     37    for feat in feats:
     38      ids = feat.GetAtomIds()
     39      for id in ids:
     40        if res[id] is None:
     41          res[id] = []
     42        res[id].append("%s-%s"%(feat.GetFamily(),feat.GetType()))
     43    return res
     44
     45  if __name__ == '__main__':
     46 -  def Usage():
     47      message="""
     48      Usage: FeatFinderCLI [-r] <fdefFilename> <smilesFilename>
     49
     50      NOTE:
     51        - the smiles file should have SMILES in the first column
     52
     53      """
     54      print(message, file=sys.stderr)
     55
     56
     57    import getopt
     58    args,extras = getopt.getopt(sys.argv[1:],'r')
     59    reverseIt=False
     60    for arg,val in args:
     61      if arg=='-r':
     62        reverseIt=True
     63
     64    if len(extras)<2:
     65      Usage()
     66      sys.exit(-1)
     67    print(_splashMessage, file=sys.stderr)
     68    fdefFilename = extras[0]
     69    if not os.path.exists(fdefFilename):
     70      logger.error("Fdef file %s does not exist."%fdefFilename)
     71      sys.exit(-1)
     72    try:
     73      factory = ChemicalFeatures.BuildFeatureFactory(fdefFilename)
     74    except Exception:
     75      logger.error("Could not parse Fdef file %s."%fdefFilename,exc_info=True)
     76      sys.exit(-1)
     77
     78    smilesFilename = extras[1]
     79    if not os.path.exists(smilesFilename):
     80      logger.error("Smiles file %s does not exist."%smilesFilename)
     81      sys.exit(-1)
     82
     83    try:
     84      inF = file(smilesFilename,'r')
     85    except Exception:
     86      logger.error("Could not open smiles file %s."%smilesFilename,exc_info=True)
     87      sys.exit(-1)
     88
     89    lineNo=0
     90    for line in inF.readlines():
     91      lineNo+=1
     92      line = line.strip()
     93      smi = splitExpr.split(line)[0].strip()
     94      mol = Chem.MolFromSmiles(smi)
     95
     96      if mol is not None:
     97        print('Mol-%d\t%s'%(lineNo,smi))
     98
     99        if not reverseIt:
    100          featInfo = GetAtomFeatInfo(factory,mol)
    101          for i,v in enumerate(featInfo):
    102            print('\t% 2s(%d)'%(mol.GetAtomWithIdx(i).GetSymbol(),i+1),end='')
    103            if v:
    104              print('\t',', '.join(v))
    105            else:
    106              print()
    107        else:
    108          feats = factory.GetFeaturesForMol(mol)
    109          for feat in feats:
    110            print('\t%s-%s: '%(feat.GetFamily(),feat.GetType()),end='')
    111            print(', '.join([str(x) for x in feat.GetAtomIds()]))
    112      else:
    113        logger.warning("Could not process smiles '%s' on line %d."%(smi,lineNo))
    114