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