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