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 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   
16 -class Entity:
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 addictional 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 __iter__(self):
41 "Iterate over children." 42 for child in self.child_list: 43 yield child
44 45 # Public methods 46
47 - def get_level(self):
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
58 - def set_parent(self, entity):
59 "Set the parent Entity object." 60 self.parent=entity
61
62 - def detach_parent(self):
63 "Detach the parent." 64 self.parent=None
65
66 - def detach_child(self, id):
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 #self.child_list.sort(self._sort) 83 self.child_dict[entity_id]=entity
84
85 - def get_iterator(self):
86 "Return iterator over children." 87 for child in self.child_list: 88 yield child
89
90 - def get_list(self):
91 "Return a copy of the list of children." 92 return copy(self.child_list)
93
94 - def has_id(self, id):
95 "Return 1 if a child with given id exists, otherwise 0." 96 return self.child_dict.has_key(id)
97
98 - def get_parent(self):
99 "Return the parent Entity object." 100 return self.parent
101
102 - def get_id(self):
103 "Return the id." 104 return self.id
105
106 - def get_full_id(self):
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
140 -class DisorderedEntityWrapper:
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 """
150 - def __init__(self, id):
151 self.id=id 152 self.child_dict={} 153 self.selected_child=None 154 self.parent=None
155 156 # Special methods 157
158 - def __getattr__(self, method):
159 "Forward the method call to the selected child." 160 if not hasattr(self, 'selected_child'): 161 # Avoid problems with pickling 162 # Unpickling goes into infinite loop! 163 raise AttributeError 164 return getattr(self.selected_child, method)
165
166 - def __setitem__(self, id, child):
167 "Add a child, associated with a certain id." 168 self.child_dict[id]=child
169 170 # Public methods 171
172 - def get_id(self):
173 "Return the id." 174 return self.id
175
176 - def disordered_has_id(self, id):
177 "Return 1 if there is an object present associated with this id." 178 return self.child_dict.has_key(id)
179
180 - def detach_parent(self):
181 "Detach the parent" 182 self.parent=None 183 for child in self.disordered_get_list(): 184 child.detach_parent()
185
186 - def get_parent(self):
187 "Return parent." 188 return self.parent
189
190 - def set_parent(self, parent):
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
196 - def disordered_select(self, id):
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
203 - def disordered_add(self, child):
204 "This is implemented by DisorderedAtom and DisorderedResidue." 205 raise NotImplementedError
206
207 - def is_disordered(self):
208 """ 209 Return 2, indicating that this Entity is a collection of Entities. 210 """ 211 return 2
212
213 - def disordered_get_id_list(self):
214 "Return a list of id's." 215 l=self.child_dict.keys() 216 # sort id list alphabetically 217 l.sort() 218 return l
219
220 - def disordered_get(self, id=None):
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
229 - def disordered_get_list(self):
230 "Return list of children." 231 return self.child_dict.values()
232