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