1
2
3
4
5
6 from copy import copy
7
8 from PDBExceptions import PDBConstructionException, PDBException
9
10 __doc__="""
11 Base class for Residue, Chain, Model and Structure classes.
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
41 "Iterate over children."
42 for child in self.child_list:
43 yield child
44
45
46
48 """Return level in hierarchy.
49
50 A - atom
51 R - residue
52 C - chain
53 M - model
54 S - structure
55 """
56 return self.level
57
59 "Set the parent Entity object."
60 self.parent=entity
61
63 "Detach the parent."
64 self.parent=None
65
67 "Remove a child."
68 child=self.child_dict[id]
69 child.detach_parent()
70 del self.child_dict[id]
71 self.child_list=self.child_dict.values()
72 self.child_list.sort(self._sort)
73
74 - def add(self, entity):
75 "Add a child to the Entity."
76 entity_id=entity.get_id()
77 if self.has_id(entity_id):
78 raise PDBConstructionException( \
79 "%s defined twice" % str(entity_id))
80 entity.set_parent(self)
81 self.child_list.append(entity)
82
83 self.child_dict[entity_id]=entity
84
86 "Return iterator over children."
87 for child in self.child_list:
88 yield child
89
91 "Return a copy of the list of children."
92 return copy(self.child_list)
93
95 "Return 1 if a child with given id exists, otherwise 0."
96 return self.child_dict.has_key(id)
97
99 "Return the parent Entity object."
100 return self.parent
101
103 "Return the id."
104 return self.id
105
107 """Return the full id.
108
109 The full id is a tuple containing all id's starting from
110 the top object (Structure) down to the current object. A full id for
111 a Residue object e.g. is something like:
112
113 ("1abc", 0, "A", (" ", 10, "A"))
114
115 This corresponds to:
116
117 Structure with id "1abc"
118 Model with id 0
119 Chain with id "A"
120 Residue with id (" ", 10, "A")
121
122 The Residue id indicates that the residue is not a hetero-residue
123 (or a water) beacuse it has a blanc hetero field, that its sequence
124 identifier is 10 and its insertion code "A".
125 """
126 if self.full_id==None:
127 entity_id=self.get_id()
128 l=[entity_id]
129 parent=self.get_parent()
130 while not (parent is None):
131 entity_id=parent.get_id()
132 l.append(entity_id)
133 parent=parent.get_parent()
134 l.reverse()
135 self.full_id=tuple(l)
136 return self.full_id
137
138
139
141 """
142 This class is a simple wrapper class that groups a number of equivalent
143 Entities and forwards all method calls to one of them (the currently selected
144 object). DisorderedResidue and DisorderedAtom are subclasses of this class.
145
146 E.g.: A DisorderedAtom object contains a number of Atom objects,
147 where each Atom object represents a specific position of a disordered
148 atom in the structure.
149 """
151 self.id=id
152 self.child_dict={}
153 self.selected_child=None
154 self.parent=None
155
156
157
159 "Forward the method call to the selected child."
160 if not hasattr(self, 'selected_child'):
161
162
163 raise AttributeError
164 return getattr(self.selected_child, method)
165
167 "Add a child, associated with a certain id."
168 self.child_dict[id]=child
169
170
171
173 "Return the id."
174 return self.id
175
177 "Return 1 if there is an object present associated with this id."
178 return self.child_dict.has_key(id)
179
185
187 "Return parent."
188 return self.parent
189
191 "Set the parent for the object and its children."
192 self.parent=parent
193 for child in self.disordered_get_list():
194 child.set_parent(parent)
195
197 """Select the object with given id as the currently active object.
198
199 Uncaught method calls are forwarded to the selected child object.
200 """
201 self.selected_child=self.child_dict[id]
202
204 "This is implemented by DisorderedAtom and DisorderedResidue."
205 raise NotImplementedError
206
208 """
209 Return 2, indicating that this Entity is a collection of Entities.
210 """
211 return 2
212
214 "Return a list of id's."
215 l=self.child_dict.keys()
216
217 l.sort()
218 return l
219
221 """Get the child object associated with id.
222
223 If id is None, the currently selected child is returned.
224 """
225 if id==None:
226 return self.selected_child
227 return self.child_dict[id]
228
230 "Return list of children."
231 return self.child_dict.values()
232