rdkit.Chem.ChemUtils.SDFToCSV

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

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

     1  #
     2  # Copyright (C) 2004-2006 Rational Discovery LLC
     3  #
     4  #   @@ All Rights Reserved @@
     5  #  This file is part of the RDKit.
     6  #  The contents are covered by the terms of the BSD license
     7  #  which is included in the file license.txt, found at the root
     8  #  of the RDKit source tree.
     9  #
    10  import argparse
    11  import csv
    12  import os
    13  import sys
    14
    15  from rdkit import Chem
    16
    17
    18 -def Convert(suppl, outFile, keyCol=None, stopAfter=-1, includeChirality=False, smilesFrom=''):
    19    w = csv.writer(outFile)
    20    mol = suppl[0]
    21    propNames = list(mol.GetPropNames())
    22    if keyCol and keyCol in propNames:
    23      propNames.remove(keyCol)
    24
    25    outL = []
    26    if keyCol:
    27      outL.append(keyCol)
    28    outL.append('SMILES')
    29    outL.extend(propNames)
    30    w.writerow(outL)
    31    for nDone, mol in enumerate(suppl, 1):
    32      if not mol:
    33        continue
    34      if not smilesFrom or not mol.HasProp(smilesFrom):
    35        smi = Chem.MolToSmiles(mol, isomericSmiles=includeChirality)
    36      else:
    37        smi = mol.GetProp(smilesFrom)
    38        tMol = Chem.MolFromSmiles(smi)
    39        smi = Chem.MolToSmiles(tMol, isomericSmiles=includeChirality)
    40      outL = []
    41      if keyCol:
    42        outL.append(str(mol.GetProp(keyCol)))
    43      outL.append(smi)
    44      for prop in propNames:
    45        if mol.HasProp(prop):
    46          outL.append(str(mol.GetProp(prop)))
    47        else:
    48          outL.append('')
    49      w.writerow(outL)
    50      if nDone == stopAfter:
    51        break
    52    return
    53
    54
    55 -def initParser():
    56    """ Initialize the parser for the CLI """
    57    parser = argparse.ArgumentParser(description='Convert SDF file to CSV',
    58                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    59    parser.add_argument('--key', '-k', metavar='keyCol', default=None, dest='keyCol')
    60    parser.add_argument('--chiral', default=False, action='store_true', dest='useChirality')
    61    parser.add_argument('--smilesCol', metavar='smilesCol', default='')
    62    parser.add_argument('inFilename', metavar='inFile.sdf', type=existingFile)
    63    parser.add_argument('outF', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
    64    return parser
    65
    66
    67 -def existingFile(filename):
    68      """ 'type' for argparse - check that filename exists """
    69      if not os.path.exists(filename):
    70        raise argparse.ArgumentTypeError("{0} does not exist".format(filename))
    71      return filename
    72
    73
    74 -def main():
    75    """ Main application """
    76    parser = initParser()
    77    args = parser.parse_args()
    78    suppl = Chem.SDMolSupplier(args.inFilename)
    79    Convert(suppl, args.outF, keyCol=args.keyCol, includeChirality=args.useChirality,
    80            smilesFrom=args.smilesCol)
    81
    82
    83  if __name__ == '__main__':
    84    main()
    85