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