1
2
3
4
5
6 import sys,types
7 from rdkit import Chem
8
9 from rdkit.VLib.Output import OutputNode as BaseOutputNode
10
12 """ dumps smiles output
13
14 Assumptions:
15
16 - destination supports a write() method
17
18 - inputs (parents) can be stepped through in lockstep
19
20
21 Usage Example:
22 >>> smis = ['C1CCC1','C1CC1','C=O','NCC']
23 >>> mols = [Chem.MolFromSmiles(x) for x in smis]
24 >>> from rdkit.VLib.Supply import SupplyNode
25 >>> suppl = SupplyNode(contents=mols)
26 >>> import StringIO
27 >>> io = StringIO.StringIO()
28 >>> node = OutputNode(dest=io,delim=', ')
29 >>> node.AddParent(suppl)
30 >>> ms = [x for x in node]
31 >>> len(ms)
32 4
33 >>> txt = io.getvalue()
34 >>> repr(txt)
35 "'1, C1CCC1\\\\n2, C1CC1\\\\n3, C=O\\\\n4, CCN\\\\n'"
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
46 BaseOutputNode.reset(self)
47 self._nDumped=0
48
50 self._nDumped += 1
51 if type(mol) in [types.TupleType,types.ListType]:
52 args = mol
53 mol = args[0]
54 if len(args)>1:
55 args = 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
73
75 import doctest,sys
76 return doctest.testmod(sys.modules["__main__"])
77
78 if __name__ == '__main__':
79 import sys
80 failed,tried = _test()
81 sys.exit(failed)
82