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:
     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:
     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      try:
     95        mol = Chem.MolFromSmiles(smi)
     96      except:
     97        mol = None
     98
     99      if mol:
    100        print('Mol-%d\t%s'%(lineNo,smi))
    101
    102        if not reverseIt:
    103          featInfo = GetAtomFeatInfo(factory,mol)
    104          for i,v in enumerate(featInfo):
    105            print('\t% 2s(%d)'%(mol.GetAtomWithIdx(i).GetSymbol(),i+1),end='')
    106            if v:
    107              print('\t',', '.join(v))
    108            else:
    109              print()
    110        else:
    111          feats = factory.GetFeaturesForMol(mol)
    112          for feat in feats:
    113            print('\t%s-%s: '%(feat.GetFamily(),feat.GetType()),end='')
    114            print(', '.join([str(x) for x in feat.GetAtomIds()]))
    115      else:
    116        logger.warning("Could not process smiles '%s' on line %d."%(smi,lineNo))
    117