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 __future__ import generators 
  7   
  8  from Numeric import Float0 
  9  from copy import copy 
 10   
 11  from PDBExceptions import PDBConstructionException, PDBException 
 12   
 13  __doc__=""" 
 14  Base class for Residue, Chain, Model and Structure classes.  
 15  It is a simple container class, with list and dictionary like properties. 
 16  """ 
 17   
 18   
19 -class Entity:
20 """ 21 Basic container object. Structure, Model, Chain and Residue 22 are subclasses of Entity. It deals with storage and lookup. 23 """
24 - def __init__(self, id):
25 self.id=id 26 self.full_id=None 27 self.parent=None 28 self.child_list=[] 29 self.child_dict={} 30 # Dictionary that keeps addictional properties 31 self.xtra={}
32 33 # Special methods 34
35 - def __len__(self):
36 "Return the number of children." 37 return len(self.child_list)
38
39 - def __getitem__(self, id):
40 "Return the child with given id." 41 return self.child_dict[id]
42
43 - def __iter__(self):
44 "Iterate over children." 45 for child in self.child_list: 46 yield child
47 48 # Public methods 49
50 - def get_level(self):
51 """Return level in hierarchy. 52 53 A - atom 54 R - residue 55 C - chain 56 M - model 57 S - structure 58 """ 59 return self.level
60
61 - def set_parent(self, entity):
62 "Set the parent Entity object." 63 self.parent=entity
64
65 - def detach_parent(self):
66 "Detach the parent." 67 self.parent=None
68
69 - def detach_child(self, id):
70 "Remove a child." 71 child=self.child_dict[id] 72 child.detach_parent() 73 del self.child_dict[id] 74 self.child_list=self.child_dict.values() 75 self.child_list.sort(self._sort)
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, "%s defined twice" % str(entity_id) 82 entity.set_parent(self) 83 self.child_list.append(entity) 84 #self.child_list.sort(self._sort) 85 self.child_dict[entity_id]=entity
86
87 - def get_iterator(self):
88 "Return iterator over children." 89 for child in self.child_list: 90 yield child
91
92 - def get_list(self):
93 "Return a copy of the list of children." 94 return copy(self.child_list)
95
96 - def has_id(self, id):
97 "Return 1 if a child with given id exists, otherwise 0." 98 return self.child_dict.has_key(id)
99
100 - def get_parent(self):
101 "Return the parent Entity object." 102 return self.parent
103
104 - def get_id(self):
105 "Return the id." 106 return self.id
107
108 - def get_full_id(self):
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 blanc 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
142 -class DisorderedEntityWrapper:
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 """
152 - def __init__(self, id):
153 self.id=id 154 self.child_dict={} 155 self.selected_child=None 156 self.parent=None
157 158 # Special methods 159
160 - def __getattr__(self, method):
161 "Forward the method call to the selected child." 162 if not hasattr(self, 'selected_child'): 163 # Avoid problems with pickling 164 # Unpickling goes into infinite loop! 165 raise AttributeError 166 return getattr(self.selected_child, method)
167
168 - def __setitem__(self, id, child):
169 "Add a child, associated with a certain id." 170 self.child_dict[id]=child
171 172 # Public methods 173
174 - def get_id(self):
175 "Return the id." 176 return self.id
177
178 - def disordered_has_id(self, id):
179 "Return 1 if there is an object present associated with this id." 180 return self.child_dict.has_key(id)
181
182 - def detach_parent(self):
183 "Detach the parent" 184 self.parent=None 185 for child in self.disordered_get_list(): 186 child.detach_parent()
187
188 - def get_parent(self):
189 "Return parent." 190 return self.parent
191
192 - def set_parent(self, parent):
193 "Set the parent for the object and its children." 194 self.parent=parent 195 for child in self.disordered_get_list(): 196 child.set_parent(parent)
197
198 - def disordered_select(self, id):
199 """Select the object with given id as the currently active object. 200 201 Uncaught method calls are forwarded to the selected child object. 202 """ 203 self.selected_child=self.child_dict[id]
204
205 - def disordered_add(self, child):
206 "This is implemented by DisorderedAtom and DisorderedResidue." 207 raise NotImplementedError
208
209 - def is_disordered(self):
210 """ 211 Return 2, indicating that this Entity is a collection of Entities. 212 """ 213 return 2
214
215 - def disordered_get_id_list(self):
216 "Return a list of id's." 217 l=self.child_dict.keys() 218 # sort id list alphabetically 219 l.sort() 220 return l
221
222 - def disordered_get(self, id=None):
223 """Get the child object associated with id. 224 225 If id is None, the currently selected child is returned. 226 """ 227 if id==None: 228 return self.selected_child 229 return self.child_dict[id]
230
231 - def disordered_get_list(self):
232 "Return list of children." 233 return self.child_dict.values()
234