rdkit.VLib.NodeLib.SmilesOutput

Package rdkit :: Package VLib :: Package NodeLib :: Module SmilesOutput |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.VLib.NodeLib.SmilesOutput

     1  #  $Id$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #     All Rights Reserved
     5  #
     6  from rdkit import Chem
     7  from rdkit.VLib.Output import OutputNode as BaseOutputNode
     8
     9
    10 -class OutputNode(BaseOutputNode):
    11    """ dumps smiles output
    12
    13    Assumptions:
    14
    15      - destination supports a write() method
    16
    17      - inputs (parents) can be stepped through in lockstep
    18
    19
    20    Usage Example:
    21      >>> smis = ['C1CCC1','C1CC1','C=O','NCC']
    22      >>> mols = [Chem.MolFromSmiles(x) for x in smis]
    23      >>> from rdkit.VLib.Supply import SupplyNode
    24      >>> suppl = SupplyNode(contents=mols)
    25      >>> from rdkit.six import StringIO
    26      >>> sio = StringIO()
    27      >>> node = OutputNode(dest=sio,delim=', ')
    28      >>> node.AddParent(suppl)
    29      >>> ms = [x for x in node]
    30      >>> len(ms)
    31      4
    32      >>> txt = sio.getvalue()
    33      >>> repr(txt)
    34      "'1, C1CCC1\\\\n2, C1CC1\\\\n3, C=O\\\\n4, CCN\\\\n'"
    35
    36    """
    37
    38 -  def __init__(self, dest=None, delim='\t', idField=None, **kwargs):
    39      BaseOutputNode.__init__(self, dest=dest, strFunc=self.smilesOut)
    40      self._dest = dest
    41      self._idField = idField
    42      self._delim = delim
    43      self._nDumped = 0
    44
    45 -  def reset(self):
    46      BaseOutputNode.reset(self)
    47      self._nDumped = 0
    48
    49 -  def smilesOut(self, mol):
    50      self._nDumped += 1
    51      if isinstance(mol, (tuple, list)):
    52        args = mol
    53        mol = args[0]
    54        if len(args) > 1:
    55          args = list(args[1:])
    56        else:
    57          args = []
    58      else:
    59        args = []
    60
    61      if self._idField and mol.HasProp(self._idField):
    62        label = mol.GetProp(self._idField)
    63      else:
    64        label = str(self._nDumped)
    65      smi = Chem.MolToSmiles(mol)
    66      outp = [label, smi] + args
    67      return '%s\n' % (self._delim.join(outp))
    68
    69
    70  # ------------------------------------
    71  #
    72  #  doctest boilerplate
    73  #
    74 -def _runDoctests(verbose=None):  # pragma: nocover
    75    import doctest
    76    import sys
    77    failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
    78    sys.exit(failed)
    79
    80
    81  if __name__ == '__main__':  # pragma: nocover
    82    _runDoctests()
    83