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  """ 
  7  Base class for Residue, Chain, Model and Structure classes. 
  8   
  9  It is a simple container class, with list and dictionary like properties. 
 10  """ 
 11   
 12  from copy import copy 
 13   
 14  from Bio.PDB.PDBExceptions import PDBConstructionException 
 15   
 16   
17 -class Entity(object):
18 """ 19 Basic container object. Structure, Model, Chain and Residue 20 are subclasses of Entity. It deals with storage and lookup. 21 """
22 - def __init__(self, id):
23 self.id = id 24 self.full_id = None 25 self.parent = None 26 self.child_list = [] 27 self.child_dict = {} 28 # Dictionary that keeps additional properties 29 self.xtra = {}
30 31 # Special methods 32
33 - def __len__(self):
34 "Return the number of children." 35 return len(self.child_list)
36
37 - def __getitem__(self, id):
38 "Return the child with given id." 39 return self.child_dict[id]
40
41 - def __delitem__(self, id):
42 "Remove a child." 43 return self.detach_child(id)
44
45 - def __contains__(self, id):
46 "True if there is a child element with the given id." 47 return (id in self.child_dict)
48
49 - def __iter__(self):
50 "Iterate over children." 51 for child in self.child_list: 52 yield child
53 54 # Public methods 55
56 - def get_level(self):
57 """Return level in hierarchy. 58 59 A - atom 60 R - residue 61 C - chain 62 M - model 63 S - structure 64 """ 65 return self.level
66
67 - def set_parent(self, entity):
68 "Set the parent Entity object." 69 self.parent = entity
70
71 - def detach_parent(self):
72 "Detach the parent." 73 self.parent = None
74
75 - def detach_child(self, id):
76 "Remove a child." 77 child = self.child_dict[id] 78 child.detach_parent() 79 del self.child_dict[id] 80 self.child_list.remove(child)
81
82 - def add(self, entity):
83 "Add a child to the Entity." 84 entity_id = entity.get_id() 85 if self.has_id(entity_id): 86 raise PDBConstructionException( 87 "%s defined twice" % str(entity_id)) 88 entity.set_parent(self) 89 self.child_list.append(entity) 90 self.child_dict[entity_id] = entity
91
92 - def insert(self, pos, entity):
93 "Add a child to the Entity at a specified position." 94 entity_id = entity.get_id() 95 if self.has_id(entity_id): 96 raise PDBConstructionException( 97 "%s defined twice" % str(entity_id)) 98 entity.set_parent(self) 99 self.child_list[pos:pos] = [entity] 100 self.child_dict[entity_id] = entity
101
102 - def get_iterator(self):
103 "Return iterator over children." 104 for child in self.child_list: 105 yield child
106
107 - def get_list(self):
108 "Return a copy of the list of children." 109 return copy(self.child_list)
110
111 - def has_id(self, id):
112 """True if a child with given id exists.""" 113 return (id in self.child_dict)
114
115 - def get_parent(self):
116 "Return the parent Entity object." 117 return self.parent
118
119 - def get_id(self):
120 "Return the id." 121 return self.id
122
123 - def get_full_id(self):
124 """Return the full id. 125 126 The full id is a tuple containing all id's starting from 127 the top object (Structure) down to the current object. A full id for 128 a Residue object e.g. is something like: 129 130 ("1abc", 0, "A", (" ", 10, "A")) 131 132 This corresponds to: 133 134 Structure with id "1abc" 135 Model with id 0 136 Chain with id "A" 137 Residue with id (" ", 10, "A") 138 139 The Residue id indicates that the residue is not a hetero-residue 140 (or a water) because it has a blank hetero field, that its sequence 141 identifier is 10 and its insertion code "A". 142 """ 143 if self.full_id is None: 144 entity_id = self.get_id() 145 l = [entity_id] 146 parent = self.get_parent() 147 while parent is not None: 148 entity_id = parent.get_id() 149 l.append(entity_id) 150 parent = parent.get_parent() 151 l.reverse() 152 self.full_id = tuple(l) 153 return self.full_id
154
155 - def transform(self, rot, tran):
156 """ 157 Apply rotation and translation to the atomic coordinates. 158 159 Example: 160 >>> rotation=rotmat(pi, Vector(1, 0, 0)) 161 >>> translation=array((0, 0, 1), 'f') 162 >>> entity.transform(rotation, translation) 163 164 @param rot: A right multiplying rotation matrix 165 @type rot: 3x3 Numeric array 166 167 @param tran: the translation vector 168 @type tran: size 3 Numeric array 169 """ 170 for o in self.get_list(): 171 o.transform(rot, tran)
172
173 - def copy(self):
174 shallow = copy(self) 175 176 shallow.child_list = [] 177 shallow.child_dict = {} 178 shallow.xtra = copy(self.xtra) 179 180 shallow.detach_parent() 181 182 for child in self.child_list: 183 shallow.add(child.copy()) 184 return shallow
185 186
187 -class DisorderedEntityWrapper(object):
188 """ 189 This class is a simple wrapper class that groups a number of equivalent 190 Entities and forwards all method calls to one of them (the currently selected 191 object). DisorderedResidue and DisorderedAtom are subclasses of this class. 192 193 E.g.: A DisorderedAtom object contains a number of Atom objects, 194 where each Atom object represents a specific position of a disordered 195 atom in the structure. 196 """
197 - def __init__(self, id):
198 self.id = id 199 self.child_dict = {} 200 self.selected_child = None 201 self.parent = None
202 203 # Special methods 204
205 - def __getattr__(self, method):
206 "Forward the method call to the selected child." 207 if not hasattr(self, 'selected_child'): 208 # Avoid problems with pickling 209 # Unpickling goes into infinite loop! 210 raise AttributeError 211 return getattr(self.selected_child, method)
212
213 - def __getitem__(self, id):
214 "Return the child with the given id." 215 return self.selected_child[id]
216 217 # XXX Why doesn't this forward to selected_child? 218 # (NB: setitem was here before getitem, iter, len, sub)
219 - def __setitem__(self, id, child):
220 "Add a child, associated with a certain id." 221 self.child_dict[id] = child
222
223 - def __contains__(self, id):
224 "True if the child has the given id." 225 return (id in self.selected_child)
226
227 - def __iter__(self):
228 "Return the number of children." 229 return iter(self.selected_child)
230
231 - def __len__(self):
232 "Return the number of children." 233 return len(self.selected_child)
234
235 - def __sub__(self, other):
236 """Subtraction with another object.""" 237 return self.selected_child - other
238 239 # Public methods 240
241 - def get_id(self):
242 "Return the id." 243 return self.id
244
245 - def disordered_has_id(self, id):
246 """True if there is an object present associated with this id.""" 247 return (id in self.child_dict)
248
249 - def detach_parent(self):
250 "Detach the parent" 251 self.parent = None 252 for child in self.disordered_get_list(): 253 child.detach_parent()
254
255 - def get_parent(self):
256 "Return parent." 257 return self.parent
258
259 - def set_parent(self, parent):
260 "Set the parent for the object and its children." 261 self.parent = parent 262 for child in self.disordered_get_list(): 263 child.set_parent(parent)
264
265 - def disordered_select(self, id):
266 """Select the object with given id as the currently active object. 267 268 Uncaught method calls are forwarded to the selected child object. 269 """ 270 self.selected_child = self.child_dict[id]
271
272 - def disordered_add(self, child):
273 "This is implemented by DisorderedAtom and DisorderedResidue." 274 raise NotImplementedError
275
276 - def is_disordered(self):
277 """ 278 Return 2, indicating that this Entity is a collection of Entities. 279 """ 280 return 2
281
282 - def disordered_get_id_list(self):
283 "Return a list of id's." 284 # sort id list alphabetically 285 return sorted(self.child_dict)
286
287 - def disordered_get(self, id=None):
288 """Get the child object associated with id. 289 290 If id is None, the currently selected child is returned. 291 """ 292 if id is None: 293 return self.selected_child 294 return self.child_dict[id]
295
296 - def disordered_get_list(self):
297 "Return list of children." 298 return list(self.child_dict.values())
299