Package Bio :: Package PDB :: Module Entity
[hide private]
[frames] | no frames]

Source Code for Module Bio.PDB.Entity

  1  # Copyright (C) 2002, Thomas Hamelryck (thamelry@binf.ku.dk) 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  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   
16 -class Entity(object):
17 """ 18 Basic container object. Structure, Model, Chain and Residue 19 are subclasses of Entity. It deals with storage and lookup. 20 """
21 - def __init__(self, id):
22 self.id=id 23 self.full_id=None 24 self.parent=None 25 self.child_list=[] 26 self.child_dict={} 27 # Dictionary that keeps additional properties 28 self.xtra={}
29 30 # Special methods 31
32 - def __len__(self):
33 "Return the number of children." 34 return len(self.child_list)
35
36 - def __getitem__(self, id):
37 "Return the child with given id." 38 return self.child_dict[id]
39
40 - def __delitem__(self, id):
41 "Remove a child." 42 return self.detach_child(id)
43
44 - def __contains__(self, id):
45 "True if there is a child element with the given id." 46 return (id in self.child_dict)
47
48 - def __iter__(self):
49 "Iterate over children." 50 for child in self.child_list: 51 yield child
52 53 # Public methods 54
55 - def get_level(self):
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
66 - def set_parent(self, entity):
67 "Set the parent Entity object." 68 self.parent=entity
69
70 - def detach_parent(self):
71 "Detach the parent." 72 self.parent=None
73
74 - def detach_child(self, id):
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
101 - def get_iterator(self):
102 "Return iterator over children." 103 for child in self.child_list: 104 yield child
105
106 - def get_list(self):
107 "Return a copy of the list of children." 108 return copy(self.child_list)
109
110 - def has_id(self, id):
111 """True if a child with given id exists.""" 112 return (id in self.child_dict)
113
114 - def get_parent(self):
115 "Return the parent Entity object." 116 return self.parent
117
118 - def get_id(self):
119 "Return the id." 120 return self.id
121
122 - def get_full_id(self):
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
154 - def transform(self, rot, tran):
155 """ 156 Apply rotation and translation to the atomic coordinates. 157 158 Example: 159 >>> rotation=rotmat(pi, Vector(1, 0, 0)) 160 >>> translation=array((0, 0, 1), 'f') 161 >>> entity.transform(rotation, translation) 162 163 @param rot: A right multiplying rotation matrix 164 @type rot: 3x3 Numeric array 165 166 @param tran: the translation vector 167 @type tran: size 3 Numeric array 168 """ 169 for o in self.get_list(): 170 o.transform(rot, tran)
171
172 - def copy(self):
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
186 -class DisorderedEntityWrapper(object):
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 """
196 - def __init__(self, id):
197 self.id=id 198 self.child_dict={} 199 self.selected_child=None 200 self.parent=None
201 202 # Special methods 203
204 - def __getattr__(self, method):
205 "Forward the method call to the selected child." 206 if not hasattr(self, 'selected_child'): 207 # Avoid problems with pickling 208 # Unpickling goes into infinite loop! 209 raise AttributeError 210 return getattr(self.selected_child, method)
211
212 - def __getitem__(self, id):
213 "Return the child with the given id." 214 return self.selected_child[id]
215 216 # XXX Why doesn't this forward to selected_child? 217 # (NB: setitem was here before getitem, iter, len, sub)
218 - def __setitem__(self, id, child):
219 "Add a child, associated with a certain id." 220 self.child_dict[id]=child
221
222 - def __contains__(self, id):
223 "True if the child has the given id." 224 return (id in self.selected_child)
225
226 - def __iter__(self):
227 "Return the number of children." 228 return iter(self.selected_child)
229
230 - def __len__(self):
231 "Return the number of children." 232 return len(self.selected_child)
233
234 - def __sub__(self, other):
235 """Subtraction with another object.""" 236 return self.selected_child - other
237 238 # Public methods 239
240 - def get_id(self):
241 "Return the id." 242 return self.id
243
244 - def disordered_has_id(self, id):
245 """True if there is an object present associated with this id.""" 246 return (id in self.child_dict)
247
248 - def detach_parent(self):
249 "Detach the parent" 250 self.parent=None 251 for child in self.disordered_get_list(): 252 child.detach_parent()
253
254 - def get_parent(self):
255 "Return parent." 256 return self.parent
257
258 - def set_parent(self, parent):
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
264 - def disordered_select(self, id):
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
271 - def disordered_add(self, child):
272 "This is implemented by DisorderedAtom and DisorderedResidue." 273 raise NotImplementedError
274
275 - def is_disordered(self):
276 """ 277 Return 2, indicating that this Entity is a collection of Entities. 278 """ 279 return 2
280
281 - def disordered_get_id_list(self):
282 "Return a list of id's." 283 # sort id list alphabetically 284 return sorted(self.child_dict)
285
286 - def disordered_get(self, id=None):
287 """Get the child object associated with id. 288 289 If id is None, the currently selected child is returned. 290 """ 291 if id is None: 292 return self.selected_child 293 return self.child_dict[id]
294
295 - def disordered_get_list(self):
296 "Return list of children." 297 return list(self.child_dict.values())
298