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