Package Bio :: Package Crystal
[hide private]
[frames] | no frames]

Source Code for Package Bio.Crystal

  1  # Copyright 2002 by Katharine Lindner.  All rights reserved. 
  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  Module to represent the NDB Atlas structure (a minimal subset of PDB format). 
  8   
  9  Hetero, Crystal and Chain exist to represent the NDB Atlas structure.  Atlas 
 10  is a minimal subset of the PDB format.  Heteo supports a 3 alphameric code. 
 11  The NDB web interface is located at http://ndbserver.rutgers.edu/NDB/index.html 
 12  """ 
 13   
 14  import copy 
 15   
16 -class CrystalError(Exception):
17 pass
18
19 -def wrap_line(line):
20 output = '' 21 for i in range(0, len(line), 80): 22 output = output + '%s\n' % line[ i: i + 80 ] 23 return output
24
25 -def validate_key(key):
26 if type(key) != type(''): 27 raise CrystalError('chain requires a string label') 28 if len(key) != 1: 29 raise CrystalError('chain label should contain one letter')
30
31 -class Hetero:
32 """ 33 This class exists to support the PDB hetero codes. 34 35 Supports only the 3 alphameric code. 36 The annotation is available from http://alpha2.bmc.uu.se/hicup/ 37 """
38 - def __init__(self, data):
39 # Enforce string storage 40 if type(data) != type(""): 41 raise CrystalError('Hetero data must be an alphameric string') 42 if data.isalnum() == 0: 43 raise CrystalError('Hetero data must be an alphameric string') 44 if len(data) > 3: 45 raise CrystalError('Hetero data may contain up to 3 characters') 46 if len(data) < 1: 47 raise CrystalError('Hetero data must not be empty') 48 49 self.data = data[:].lower()
50
51 - def __eq__(self, other):
52 return self.data == other.data
53
54 - def __ne__(self, other):
55 """Returns true iff self is not equal to other.""" 56 return not self.__eq__(other)
57
58 - def __repr__(self):
59 return "%s" % self.data
60
61 - def __str__(self):
62 return "%s" % self.data
63
64 - def __len__(self): return len(self.data)
65
66 -class Chain:
67 - def __init__(self, residues = ''):
68 self.data = [] 69 if type(residues) == type(''): 70 residues = residues.replace('*', ' ') 71 residues = residues.strip() 72 elements = residues.split() 73 self.data = map(Hetero, elements) 74 elif type(residues) == type([]): 75 for element in residues: 76 if not isinstance(element, Hetero): 77 raise CrystalError('Text must be a string') 78 for residue in residues: 79 self.data.append(residue) 80 elif isinstance(residues, Chain): 81 for residue in residues: 82 self.data.append(residue) 83 self.validate()
84
85 - def validate(self):
86 data = self.data 87 for element in data: 88 self.validate_element(element)
89
90 - def validate_element(self, element):
91 if not isinstance(element, Hetero): 92 raise TypeError
93
94 - def __str__(self):
95 output = '' 96 i = 0 97 for element in self.data: 98 output = output + '%s ' % element 99 output = output.strip() 100 output = wrap_line(output) 101 return output
102 103
104 - def __eq__(self, other):
105 if len(self.data) != len(other.data): 106 return 0 107 ok = reduce(lambda x, y: x and y, map(lambda x, y: x == y, self.data, other.data)) 108 return ok
109
110 - def __ne__(self, other):
111 """Returns true iff self is not equal to other.""" 112 return not self.__eq__(other)
113
114 - def __len__(self): return len(self.data)
115 - def __getitem__(self, i): return self.data[i]
116
117 - def __setitem__(self, i, item):
118 try: 119 self.validate_element(item) 120 except TypeError: 121 item = Hetero(item.lower()) 122 self.data[i] = item
123
124 - def __delitem__(self, i):
125 del self.data[i]
126
127 - def __getslice__(self, i, j):
128 i = max(i, 0); j = max(j, 0) 129 return self.__class__(self.data[i:j])
130
131 - def __setslice__(self, i, j, other):
132 i = max(i, 0); j = max(j, 0) 133 if isinstance(other, Chain): 134 self.data[i:j] = other.data 135 elif isinstance(other, type(self.data)): 136 self.data[i:j] = other 137 elif type(other) == type(''): 138 self.data[ i:j ] = Chain(other).data 139 else: 140 raise TypeError
141
142 - def __delslice__(self, i, j):
143 i = max(i, 0); j = max(j, 0) 144 del self.data[i:j]
145
146 - def __contains__(self, item):
147 try: 148 self.validate_element(item) 149 except TypeError: 150 item = Hetero(item.lower()) 151 return item in self.data
152
153 - def append(self, item):
154 try: 155 self.validate_element(item) 156 except TypeError: 157 item = Hetero(item.lower()) 158 self.data.append(item)
159
160 - def insert(self, i, item):
161 try: 162 self.validate_element(item) 163 except TypeError: 164 item = Hetero(item.lower()) 165 self.data.insert(i, item)
166
167 - def remove(self, item):
168 item = Hetero(item.lower()) 169 self.data.remove(item)
170
171 - def count(self, item):
172 try: 173 self.validate_element(item) 174 except TypeError: 175 item = Hetero(item.lower()) 176 return self.data.count(item)
177
178 - def index(self, item):
179 try: 180 self.validate_element(item) 181 except TypeError: 182 item = Hetero(item.lower()) 183 return self.data.index(item)
184
185 - def __add__(self, other):
186 if isinstance(other, Chain): 187 return self.__class__(self.data + other.data) 188 elif type(other) == type(''): 189 return self.__class__(self.data + Chain(other).data) 190 else: 191 raise TypeError
192
193 - def __radd__(self, other):
194 if isinstance(other, Chain): 195 return self.__class__(other.data + self.data) 196 elif type(other) == type(''): 197 return self.__class__(Chain(other).data + self.data) 198 else: 199 raise TypeError
200
201 - def __iadd__(self, other):
202 if isinstance(other, Chain): 203 self.data += other.data 204 elif type(other) == type(''): 205 self.data += Chain(other).data 206 else: 207 raise TypeError 208 return self
209
210 -class Crystal:
211 - def __init__(self, data = {}):
212 # Enforcestorage 213 if type(data) != type({}): 214 raise CrystalError('Crystal must be a dictionary') 215 self.data = data 216 self.fix()
217
218 - def fix(self):
219 data = self.data 220 for key in data.keys(): 221 element = data[ key ] 222 if isinstance(element, Chain): 223 pass 224 elif type(element) == type(''): 225 data[ key ] = Chain(element) 226 else: 227 raise TypeError
228
229 - def __repr__(self):
230 output = '' 231 keys = self.data.keys() 232 keys.sort() 233 for key in keys: 234 output = output + '%s : %s\n' % (key, self.data[ key ]) 235 return output
236
237 - def __str__(self):
238 output = '' 239 keys = self.data.keys() 240 keys.sort() 241 for key in keys: 242 output = output + '%s : %s\n' % (key, self.data[ key ]) 243 return output
244
245 - def tostring(self):
246 return self.data
247
248 - def __len__(self): return len(self.data)
249 - def __getitem__(self, key): return self.data[key]
250 - def __setitem__(self, key, item):
251 if isinstance(item, Chain): 252 self.data[key] = item 253 elif type(item) == type(''): 254 self.data[ key ] = Chain(item) 255 else: 256 raise TypeError
257
258 - def __delitem__(self, key): del self.data[key]
259 - def clear(self): self.data.clear()
260 - def copy(self):
261 return copy.copy(self)
262 - def keys(self): return self.data.keys()
263 - def items(self): return self.data.items()
264 - def values(self): return self.data.values()
265 266 #TODO - Define the __in__ method? 267
268 - def has_key(self, key): return key in self.data
269 - def get(self, key, failobj=None):
270 return self.data.get(key, failobj)
271 - def setdefault(self, key, failobj=None):
272 if key not in self.data: 273 self.data[key] = failobj 274 return self.data[key]
275 - def popitem(self):
276 return self.data.popitem()
277