1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """
25 This is a helper class for ComplexList.
26 """
27
28 from Biskit.Dock import Complex
29 from Biskit.Errors import BiskitError
30 from Biskit import LocalPath, PDBModel
31
32 import Biskit.tools as T
33
35 pass
36
38 """
39 This is a helper class for ComplexList.
40
41 Keep unique copies of the rec and lig models from many Complexes.
42 Make sure that 2 Complexes with the same rec_model (same by file
43 name and unchanged) always point to the same PDBModel instance.
44 """
45
47
48 self.rec_f2model = {}
49 self.lig_f2model = {}
50
51 self.rec_f2com = {}
52 self.lig_f2com = {}
53
54 self.initVersion = self.version()
55
56
58 """
59 Version of class.
60
61 @return: version of class
62 @rtype: str
63 """
64 return 'ComplexModelRegistry $Revision: 2.13 $'
65
66
68 """
69 Register Complex with the registry.
70
71 @param com: complex
72 @type com: Complex
73 """
74 com.rec_model,fr = self.__sync_model( com.rec_model, self.rec_f2model )
75 com.lig_model,fl = self.__sync_model( com.lig_model, self.lig_f2model )
76
77
78 if fr != None:
79 coms = self.rec_f2com.get( fr, None )
80 if coms == None:
81 self.rec_f2com[ fr ] = [ com ]
82 else:
83 coms.append( com )
84
85 if fl != None:
86 coms = self.lig_f2com.get( fl, None )
87 if coms == None:
88 self.lig_f2com[ fl ] = [ com ]
89 else:
90 coms.append( com )
91
92
94 """
95 Remove a Complex from the registry.
96
97 @param com: complex
98 @type com: Complex
99 """
100 self.__removeModel(com.rec_model,com, self.rec_f2model, self.rec_f2com)
101 self.__removeModel(com.lig_model,com, self.lig_f2model, self.lig_f2com)
102
103
105 """
106 Remove model of a complex from the registry.
107
108 @param model: receptor or ligand model
109 @type model: PDBModel
110 @param com: complex
111 @type com: Complex
112 @param f2model: dictionary mapping files to models
113 @type f2model: {str:PDBModel}
114 @param f2com: dictionary mapping files to complexes
115 @type f2com: {str:Complex}
116 """
117
118 f = model.source
119 coms = f2com[ f ]
120 coms.remove( com )
121
122 if len( coms ) == 0:
123 del f2com[ f ]
124 del f2model[ f ]
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
167 """
168 Get receptor model belonging to source.
169
170 @param source: path
171 @type source: str
172
173 @return: model
174 @rtype: PDBModel
175 """
176 return self.rec_f2model[source]
177
178
180 """
181 Get ligand model belonging to source.
182
183 @param source: path
184 @type source: str
185
186 @return: model
187 @rtype: PDBModel
188 """
189 return self.lig_f2model[source]
190
191
193 """
194 Get model belonging to source.
195
196 @param source: path
197 @type source: str
198
199 @return: model
200 @rtype: PDBModel
201 """
202 if source in self.rec_f2model:
203 return self.rec_f2model[ source ]
204
205 return self.lig_f2model[ source ]
206
207
209 """
210 Get a list with all receptor models.
211
212 @return: list of models
213 @rtype: [PDBModel]
214 """
215 return self.rec_f2model.values()
216
217
219 """
220 Get a list with all ligand models.
221
222 @return: list of models
223 @rtype: [PDBModel]
224 """
225 return self.lig_f2model.values()
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
247 """
248 Get the complexes of which a given receptor model is a component.
249
250 @param model: LocalPath or PDBModel
251 @type model: object
252
253 @return: list of Complexes
254 @rtype: [Complex]
255 """
256 return self.__getComplexes( model, self.rec_f2com )
257
258
260 """
261 Get the complexes of which a given ligand model is a component.
262
263 @param model: LocalPath or PDBModel
264 @type model: object
265
266 @return: list of Complexes
267 @rtype: [Complex]
268 """
269 return self.__getComplexes( model, self.lig_f2com )
270
271
273 """
274 Get the complexes of which a given ligand model is a component.
275
276 @param model: LocalPath or PDBModel
277 @type model: object
278 @param f2com: dictionary mapping paths to complexes
279 @type f2com: {str:Complex}
280
281 @return: list of Complexes
282 @rtype: [Complex]
283
284 @raise RegistryError: if model has no source file.
285 """
286 if isinstance( model, LocalPath):
287 f = model
288
289 try:
290 if isinstance( model, PDBModel ):
291 f = model.source
292
293 return f2com[ f ]
294
295 except KeyError, why:
296 raise RegistryError( "Model with source '%r' is unknown" % f )
297
298
300 """
301 Get a shared model instance that is equal to m. If there is no such
302 instance in the registry, m is returned and m is added to the
303 registry - but only if it has been pickled to disc and hasn't
304 changed since.
305
306 @param m: PDBModel
307 @type m: PDBModel
308 @param f2model: dictionary with the path to the file as key
309 @type f2model: { str:PDBModel }
310
311 @return: Model from f2model equivalent to m, otherwise add m to f2model
312 and return m. File name or None for stray model
313 @rtype: PDBModel, str
314 """
315
316
317 assert isinstance( m, PDBModel )
318
319 if isinstance( m.source, LocalPath ):
320 f = m.source
321 else:
322 f = None
323
324 if f is not None:
325 if m.xyzChanged:
326
327 return m, f
328
329 found = f2model.get( f, None )
330 if found is not None:
331
332
333 return found, f
334
335
336 if m.source.exists():
337 f2model[ f ] = m
338
339 return m, f
340
341
343 s = "Receptor models:"
344 for f in self.rec_f2model:
345 s += "\n%s: %i complexes" % \
346 (f.formatted(), len( self.rec_f2com[ f ] ) )
347
348 s += "\nLigand models:"
349 for f in self.lig_f2model:
350 s += "\n%s: %i complexes" % \
351 (f.formatted(), len( self.lig_f2com[ f ] ) )
352
353 return s
354
355
357 return "ComplexModelRegistry\n" + self.__str__()
358
359
360
361
362
363 import Biskit.test as BT
364
365 -class Test(BT.BiskitTest):
383
384
385 if __name__ == '__main__':
386
387 BT.localTest()
388