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.remove(child)
72
73 - def add(self, entity):
74 "Add a child to the Entity."
75 entity_id=entity.get_id()
76 if self.has_id(entity_id):
77 raise PDBConstructionException( \
78 "%s defined twice" % str(entity_id))
79 entity.set_parent(self)
80 self.child_list.append(entity)
81 self.child_dict[entity_id]=entity
82
84 "Return iterator over children."
85 for child in self.child_list:
86 yield child
87
89 "Return a copy of the list of children."
90 return copy(self.child_list)
91
93 "Return 1 if a child with given id exists, otherwise 0."
94 return self.child_dict.has_key(id)
95
97 "Return the parent Entity object."
98 return self.parent
99
101 "Return the id."
102 return self.id
103
105 """Return the full id.
106
107 The full id is a tuple containing all id's starting from
108 the top object (Structure) down to the current object. A full id for
109 a Residue object e.g. is something like:
110
111 ("1abc", 0, "A", (" ", 10, "A"))
112
113 This corresponds to:
114
115 Structure with id "1abc"
116 Model with id 0
117 Chain with id "A"
118 Residue with id (" ", 10, "A")
119
120 The Residue id indicates that the residue is not a hetero-residue
121 (or a water) beacuse it has a blank hetero field, that its sequence
122 identifier is 10 and its insertion code "A".
123 """
124 if self.full_id==None:
125 entity_id=self.get_id()
126 l=[entity_id]
127 parent=self.get_parent()
128 while not (parent is None):
129 entity_id=parent.get_id()
130 l.append(entity_id)
131 parent=parent.get_parent()
132 l.reverse()
133 self.full_id=tuple(l)
134 return self.full_id
135
136
137
139 """
140 This class is a simple wrapper class that groups a number of equivalent
141 Entities and forwards all method calls to one of them (the currently selected
142 object). DisorderedResidue and DisorderedAtom are subclasses of this class.
143
144 E.g.: A DisorderedAtom object contains a number of Atom objects,
145 where each Atom object represents a specific position of a disordered
146 atom in the structure.
147 """
149 self.id=id
150 self.child_dict={}
151 self.selected_child=None
152 self.parent=None
153
154
155
157 "Forward the method call to the selected child."
158 if not hasattr(self, 'selected_child'):
159
160
161 raise AttributeError
162 return getattr(self.selected_child, method)
163
165 "Add a child, associated with a certain id."
166 self.child_dict[id]=child
167
168
169
171 "Return the id."
172 return self.id
173
175 "Return 1 if there is an object present associated with this id."
176 return self.child_dict.has_key(id)
177
183
185 "Return parent."
186 return self.parent
187
189 "Set the parent for the object and its children."
190 self.parent=parent
191 for child in self.disordered_get_list():
192 child.set_parent(parent)
193
195 """Select the object with given id as the currently active object.
196
197 Uncaught method calls are forwarded to the selected child object.
198 """
199 self.selected_child=self.child_dict[id]
200
202 "This is implemented by DisorderedAtom and DisorderedResidue."
203 raise NotImplementedError
204
206 """
207 Return 2, indicating that this Entity is a collection of Entities.
208 """
209 return 2
210
212 "Return a list of id's."
213 l=self.child_dict.keys()
214
215 l.sort()
216 return l
217
219 """Get the child object associated with id.
220
221 If id is None, the currently selected child is returned.
222 """
223 if id==None:
224 return self.selected_child
225 return self.child_dict[id]
226
228 "Return list of children."
229 return self.child_dict.values()
230