1
2
3
4
5
6 from copy import copy
7
8 from Bio.PDB.PDBExceptions import PDBConstructionException, PDBException
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 "Iterate over children."
46 for child in self.child_list:
47 yield child
48
49
50
52 """Return level in hierarchy.
53
54 A - atom
55 R - residue
56 C - chain
57 M - model
58 S - structure
59 """
60 return self.level
61
63 "Set the parent Entity object."
64 self.parent=entity
65
67 "Detach the parent."
68 self.parent=None
69
71 "Remove a child."
72 child=self.child_dict[id]
73 child.detach_parent()
74 del self.child_dict[id]
75 self.child_list.remove(child)
76
77 - def add(self, entity):
78 "Add a child to the Entity."
79 entity_id=entity.get_id()
80 if self.has_id(entity_id):
81 raise PDBConstructionException( \
82 "%s defined twice" % str(entity_id))
83 entity.set_parent(self)
84 self.child_list.append(entity)
85 self.child_dict[entity_id]=entity
86
88 "Return iterator over children."
89 for child in self.child_list:
90 yield child
91
93 "Return a copy of the list of children."
94 return copy(self.child_list)
95
97 """True if a child with given id exists."""
98 return (id in self.child_dict)
99
101 "Return the parent Entity object."
102 return self.parent
103
105 "Return the id."
106 return self.id
107
109 """Return the full id.
110
111 The full id is a tuple containing all id's starting from
112 the top object (Structure) down to the current object. A full id for
113 a Residue object e.g. is something like:
114
115 ("1abc", 0, "A", (" ", 10, "A"))
116
117 This corresponds to:
118
119 Structure with id "1abc"
120 Model with id 0
121 Chain with id "A"
122 Residue with id (" ", 10, "A")
123
124 The Residue id indicates that the residue is not a hetero-residue
125 (or a water) beacuse it has a blank hetero field, that its sequence
126 identifier is 10 and its insertion code "A".
127 """
128 if self.full_id==None:
129 entity_id=self.get_id()
130 l=[entity_id]
131 parent=self.get_parent()
132 while not (parent is None):
133 entity_id=parent.get_id()
134 l.append(entity_id)
135 parent=parent.get_parent()
136 l.reverse()
137 self.full_id=tuple(l)
138 return self.full_id
139
140
141
143 """
144 This class is a simple wrapper class that groups a number of equivalent
145 Entities and forwards all method calls to one of them (the currently selected
146 object). DisorderedResidue and DisorderedAtom are subclasses of this class.
147
148 E.g.: A DisorderedAtom object contains a number of Atom objects,
149 where each Atom object represents a specific position of a disordered
150 atom in the structure.
151 """
153 self.id=id
154 self.child_dict={}
155 self.selected_child=None
156 self.parent=None
157
158
159
161 "Forward the method call to the selected child."
162 if not hasattr(self, 'selected_child'):
163
164
165 raise AttributeError
166 return getattr(self.selected_child, method)
167
169 "Return the child with the given id."
170 return self.selected_child[id]
171
172
173
175 "Add a child, associated with a certain id."
176 self.child_dict[id]=child
177
179 "Return the number of children."
180 return iter(self.selected_child)
181
183 "Return the number of children."
184 return len(self.selected_child)
185
187 """Subtraction with another object."""
188 return self.selected_child - other
189
190
191
193 "Return the id."
194 return self.id
195
197 """True if there is an object present associated with this id."""
198 return (id in self.child_dict)
199
205
207 "Return parent."
208 return self.parent
209
211 "Set the parent for the object and its children."
212 self.parent=parent
213 for child in self.disordered_get_list():
214 child.set_parent(parent)
215
217 """Select the object with given id as the currently active object.
218
219 Uncaught method calls are forwarded to the selected child object.
220 """
221 self.selected_child=self.child_dict[id]
222
224 "This is implemented by DisorderedAtom and DisorderedResidue."
225 raise NotImplementedError
226
228 """
229 Return 2, indicating that this Entity is a collection of Entities.
230 """
231 return 2
232
234 "Return a list of id's."
235 l=self.child_dict.keys()
236
237 l.sort()
238 return l
239
241 """Get the child object associated with id.
242
243 If id is None, the currently selected child is returned.
244 """
245 if id==None:
246 return self.selected_child
247 return self.child_dict[id]
248
250 "Return list of children."
251 return self.child_dict.values()
252