1
2
3
4
5
6
7
8
9
10 """ Supplies a class for working with fingerprints from databases
11 #DOC
12
13 """
14 from rdkit import DataStructs
15 from rdkit import six
16 from rdkit.VLib.Node import VLibNode
17 from rdkit.six.moves import cPickle
18
19
21 """
22 new fps come back with all additional fields from the
23 database set in a "_fieldsFromDb" data member
24
25 """
26
27 - def __init__(self, dbResults, fpColName='AutoFragmentFp', usePickles=True):
28 """
29
30 DbResults should be a subclass of Dbase.DbResultSet.DbResultBase
31
32 """
33 VLibNode.__init__(self)
34 self._usePickles = usePickles
35 self._data = dbResults
36 self._fpColName = fpColName.upper()
37 self._colNames = [x.upper() for x in self._data.GetColumnNames()]
38 if self._fpColName not in self._colNames:
39 raise ValueError('fp column name "%s" not found in result set: %s' %
40 (self._fpColName, str(self._colNames)))
41 self.fpCol = self._colNames.index(self._fpColName)
42 del self._colNames[self.fpCol]
43 self._colNames = tuple(self._colNames)
44 self._numProcessed = 0
45
48
50 data = list(data)
51 if six.PY3:
52 pkl = bytes(data[self.fpCol], encoding='Latin1')
53 else:
54 pkl = str(data[self.fpCol])
55 del data[self.fpCol]
56 self._numProcessed += 1
57 try:
58 if self._usePickles:
59 newFp = cPickle.loads(pkl, encoding='bytes')
60 else:
61 newFp = DataStructs.ExplicitBitVect(pkl)
62 except Exception:
63 import traceback
64 traceback.print_exc()
65 newFp = None
66 if newFp:
67 newFp._fieldsFromDb = data
68 return newFp
69
71 itm = self.NextItem()
72 if itm is None:
73 raise StopIteration
74 return itm
75
76 __next__ = next
77
78
80 """ DbFp supplier supporting only forward iteration
81
82 >>> from rdkit import RDConfig
83 >>> from rdkit.Dbase.DbConnection import DbConnect
84 >>> fName = RDConfig.RDTestDatabase
85 >>> conn = DbConnect(fName,'simple_combined')
86 >>> suppl = ForwardDbFpSupplier(conn.GetData())
87
88 we can loop over the supplied fingerprints:
89 >>> fps = []
90 >>> for fp in suppl:
91 ... fps.append(fp)
92 >>> len(fps)
93 12
94
95 """
96
100
104
106 """
107
108 NOTE: this has side effects
109
110 """
111 try:
112 d = next(self._dataIter)
113 except StopIteration:
114 d = None
115 if d is not None:
116 newFp = self._BuildFp(d)
117 else:
118 newFp = None
119 return newFp
120
121
123 """ DbFp supplier supporting random access:
124 >>> import os.path
125 >>> from rdkit import RDConfig
126 >>> from rdkit.Dbase.DbConnection import DbConnect
127 >>> fName = RDConfig.RDTestDatabase
128 >>> conn = DbConnect(fName,'simple_combined')
129 >>> suppl = RandomAccessDbFpSupplier(conn.GetData())
130 >>> len(suppl)
131 12
132
133 we can pull individual fingerprints:
134 >>> fp = suppl[5]
135 >>> fp.GetNumBits()
136 128
137 >>> fp.GetNumOnBits()
138 54
139
140 a standard loop over the fingerprints:
141 >>> fps = []
142 >>> for fp in suppl:
143 ... fps.append(fp)
144 >>> len(fps)
145 12
146
147 or we can use an indexed loop:
148 >>> fps = [None]*len(suppl)
149 >>> for i in range(len(suppl)):
150 ... fps[i] = suppl[i]
151 >>> len(fps)
152 12
153
154 """
155
159
161 return len(self._data)
162
164 newD = self._data[idx]
165 return self._BuildFp(newD)
166
169
171 self._pos += 1
172 res = None
173 if self._pos < len(self):
174 res = self[self._pos]
175 return res
176
177
178
179
180
181
183 import sys
184 import doctest
185 failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
186 sys.exit(failed)
187
188
189 if __name__ == '__main__':
190 _runDoctests()
191