1
2
3
4
5
6 from copy import copy
7
8 from Bio.PDB.PDBExceptions import PDBConstructionException
9
10 """Base class for Residue, Chain, Model and Structure classes.
11
12 It is a simple container class, with list and dictionary like properties.
13 """
14
15
17 """
18 Basic container object. Structure, Model, Chain and Residue
19 are subclasses of Entity. It deals with storage and lookup.
20 """
22 self.id=id
23 self.full_id=None
24 self.parent=None
25 self.child_list=[]
26 self.child_dict={}
27
28 self.xtra={}
29
30
31
33 "Return the number of children."
34 return len(self.child_list)
35
37 "Return the child with given id."
38 return self.child_dict[id]
39
43
45 "True if there is a child element with the given id."
46 return (id in self.child_dict)
47
49 "Iterate over children."
50 for child in self.child_list:
51 yield child
52
53
54
56 """Return level in hierarchy.
57
58 A - atom
59 R - residue
60 C - chain
61 M - model
62 S - structure
63 """
64 return self.level
65
67 "Set the parent Entity object."
68 self.parent=entity
69
71 "Detach the parent."
72 self.parent=None
73
75 "Remove a child."
76 child=self.child_dict[id]
77 child.detach_parent()
78 del self.child_dict[id]
79 self.child_list.remove(child)
80
81 - def add(self, entity):
82 "Add a child to the Entity."
83 entity_id=entity.get_id()
84 if self.has_id(entity_id):
85 raise PDBConstructionException(
86 "%s defined twice" % str(entity_id))
87 entity.set_parent(self)
88 self.child_list.append(entity)
89 self.child_dict[entity_id]=entity
90
91 - def insert(self, pos, entity):
92 "Add a child to the Entity at a specified position."
93 entity_id=entity.get_id()
94 if self.has_id(entity_id):
95 raise PDBConstructionException(
96 "%s defined twice" % str(entity_id))
97 entity.set_parent(self)
98 self.child_list[pos:pos] = [entity]
99 self.child_dict[entity_id]=entity
100
102 "Return iterator over children."
103 for child in self.child_list:
104 yield child
105
107 "Return a copy of the list of children."
108 return copy(self.child_list)
109
111 """True if a child with given id exists."""
112 return (id in self.child_dict)
113
115 "Return the parent Entity object."
116 return self.parent
117
119 "Return the id."
120 return self.id
121
123 """Return the full id.
124
125 The full id is a tuple containing all id's starting from
126 the top object (Structure) down to the current object. A full id for
127 a Residue object e.g. is something like:
128
129 ("1abc", 0, "A", (" ", 10, "A"))
130
131 This corresponds to:
132
133 Structure with id "1abc"
134 Model with id 0
135 Chain with id "A"
136 Residue with id (" ", 10, "A")
137
138 The Residue id indicates that the residue is not a hetero-residue
139 (or a water) beacuse it has a blank hetero field, that its sequence
140 identifier is 10 and its insertion code "A".
141 """
142 if self.full_id is None:
143 entity_id=self.get_id()
144 l=[entity_id]
145 parent=self.get_parent()
146 while not (parent is None):
147 entity_id=parent.get_id()
148 l.append(entity_id)
149 parent=parent.get_parent()
150 l.reverse()
151 self.full_id=tuple(l)
152 return self.full_id
153
171
173 shallow = copy(self)
174
175 shallow.child_list = []
176 shallow.child_dict = {}
177 shallow.xtra = copy(self.xtra)
178
179 shallow.detach_parent()
180
181 for child in self.child_list:
182 shallow.add(child.copy())
183 return shallow
184
185
187 """
188 This class is a simple wrapper class that groups a number of equivalent
189 Entities and forwards all method calls to one of them (the currently selected
190 object). DisorderedResidue and DisorderedAtom are subclasses of this class.
191
192 E.g.: A DisorderedAtom object contains a number of Atom objects,
193 where each Atom object represents a specific position of a disordered
194 atom in the structure.
195 """
197 self.id=id
198 self.child_dict={}
199 self.selected_child=None
200 self.parent=None
201
202
203
205 "Forward the method call to the selected child."
206 if not hasattr(self, 'selected_child'):
207
208
209 raise AttributeError
210 return getattr(self.selected_child, method)
211
213 "Return the child with the given id."
214 return self.selected_child[id]
215
216
217
219 "Add a child, associated with a certain id."
220 self.child_dict[id]=child
221
223 "True if the child has the given id."
224 return (id in self.selected_child)
225
227 "Return the number of children."
228 return iter(self.selected_child)
229
231 "Return the number of children."
232 return len(self.selected_child)
233
235 """Subtraction with another object."""
236 return self.selected_child - other
237
238
239
241 "Return the id."
242 return self.id
243
245 """True if there is an object present associated with this id."""
246 return (id in self.child_dict)
247
253
255 "Return parent."
256 return self.parent
257
259 "Set the parent for the object and its children."
260 self.parent=parent
261 for child in self.disordered_get_list():
262 child.set_parent(parent)
263
265 """Select the object with given id as the currently active object.
266
267 Uncaught method calls are forwarded to the selected child object.
268 """
269 self.selected_child=self.child_dict[id]
270
272 "This is implemented by DisorderedAtom and DisorderedResidue."
273 raise NotImplementedError
274
276 """
277 Return 2, indicating that this Entity is a collection of Entities.
278 """
279 return 2
280
282 "Return a list of id's."
283 l=self.child_dict.keys()
284
285 l.sort()
286 return l
287
289 """Get the child object associated with id.
290
291 If id is None, the currently selected child is returned.
292 """
293 if id is None:
294 return self.selected_child
295 return self.child_dict[id]
296
298 "Return list of children."
299 return self.child_dict.values()
300