/
__init__.py
441 lines (350 loc) · 13.8 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2000 by Katharine Lindner. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
"""
This module provides code to work with files from Rebase.
http://rebase.neb.com/rebase/rebase.html
Classes:
Record Holds rebase sequence data.
Iterator Iterates over sequence data in a rebase file.
Dictionary Accesses a rebase file using a dictionary interface.
RecordParser Parses rebase sequence data into a Record object.
_Scanner Scans a rebase-format stream.
_RecordConsumer Consumes rebase data to a Record object.
Functions:
index_file Index a FASTA file for a Dictionary.
"""
import warnings
warnings.warn("Bio.Rebase was deprecated, as it does not seem to be able to parse recent HTML files from Rebase. If you want to continue to use this module, please get in contact with the Biopython developers at biopython-dev@biopython.org to avoid permanent removal of this module from Biopython", DeprecationWarning)
from types import *
import string
from Bio import File
from Bio import Index
from Bio.ParserSupport import *
class Record:
"""Holds information from a FASTA record.
Members:
seq_5_to_3 The sequence.
seq_3_to_5
enzyme_num The enzyme number
pos Position of cleavage
prototype Prototype
source
microorganism
temperature Growth temperature
misc Miscellaneous information
date_entered
date_modified
num_Adeno2
num_Lambda
num_pBR322
num_PhiX174
num_SV40
"""
def __init__(self, colwidth=60):
"""__init__(self, colwidth=60)
Create a new Record. colwidth specifies the number of residues
to put on each line.
"""
self.seq_5_to_3 = ''
self.seq_3_to_5 = ''
self.methylation = ''
self.enzyme_num = None
self.prototype = ''
self.source = ''
self.microorganism = ''
self.temperature = None
self.misc = ''
self.date_entered = ''
self.date_modified = ''
self._colwidth = colwidth
self.num_Adeno2 = 0
self.num_Lambda = 0
self.num_pBR322 = 0
self.num_PhiX174 = 0
self.num_SV40 = 0
class Iterator:
"""Returns one record at a time from a Rebase file.
Methods:
next Return the next record from the stream, or None.
"""
def __init__(self, handle, parser=None):
"""__init__(self, handle, parser=None)
Create a new iterator. handle is a file-like object. parser
is an optional Parser object to change the results into another form.
If set to None, then the raw contents of the file will be returned.
"""
if type(handle) is not FileType and type(handle) is not InstanceType:
raise ValueError, "I expected a file handle or file-like object"
self._uhandle = SGMLHandle( File.UndoHandle( handle ) )
self._parser = parser
def next(self):
"""next(self) -> object
Return the next rebase record from the file. If no more records,
return None.
"""
lines = []
first_tag = 'Recognition Sequence'
while 1:
line = self._uhandle.readline()
if not line:
break
if line[:len( first_tag )] == 'first_tag':
self._uhandle.saveline(line)
break
if not line:
return None
if self._parser is not None:
return self._parser.parse(File.StringHandle(data))
return data
def __iter__(self):
return iter(self.next, None)
class Dictionary:
"""Accesses a rebase file using a dictionary interface.
"""
__filename_key = '__filename'
def __init__(self, indexname, parser=None):
"""__init__(self, indexname, parser=None)
Open a Fasta Dictionary. indexname is the name of the
index for the dictionary. The index should have been created
using the index_file function. parser is an optional Parser
object to change the results into another form. If set to None,
then the raw contents of the file will be returned.
"""
self._index = Index.Index(indexname)
self._handle = open(self._index[Dictionary.__filename_key])
self._parser = parser
def __len__(self):
return len(self._index)
def __getitem__(self, key):
start, len = self._index[key]
self._handle.seek(start)
data = self._handle.read(len)
if self._parser is not None:
return self._parser.parse(File.StringHandle(data))
return data
def __getattr__(self, name):
return getattr(self._index, name)
class RecordParser:
"""Parses FASTA sequence data into a Record object.
"""
def __init__(self):
self._scanner = _Scanner()
self._consumer = _RecordConsumer()
def parse(self, handle):
self._scanner.feed(handle, self._consumer)
return self._consumer.data
class _Scanner:
"""Scans a rebase file.
Methods:
feed Feed in one rebase record.
"""
def feed(self, handle, consumer):
"""feed(self, handle, consumer)
Feed in rebase data for scanning. handle is a file-like object
containing rebase data. consumer is a Consumer object that will
receive events as the rebase data is scanned.
"""
if isinstance(handle, File.UndoHandle):
uhandle = handle
else:
uhandle = File.UndoHandle(handle)
uhandle = File.SGMLHandle( uhandle )
if uhandle.peekline():
self._scan_record(uhandle, consumer)
def _scan_line(self, uhandle ):
line = safe_readline( uhandle )
line = string.join( string.split( line ), ' ' ) + ' '
return line
def _text_in( self, uhandle, text, count ):
for j in range( count ):
line = self._scan_line( uhandle )
text = text + line
return text
def _scan_record(self, uhandle, consumer):
consumer.start_sequence()
text = ''
text = self._text_in( uhandle, text, 100 )
self._scan_sequence( text, consumer)
self._scan_methylation( text, consumer)
self._scan_enzyme_num( text, consumer )
self._scan_prototype( text, consumer )
self._scan_source( text, consumer )
self._scan_microorganism( text, consumer )
self._scan_temperature( text, consumer)
self._scan_date_entered( text, consumer)
self._scan_date_modified( text, consumer)
self._scan_Adeno2( text, consumer)
self._scan_Lambda( text, consumer)
self._scan_pBR322( text, consumer)
self._scan_PhiX174( text, consumer)
self._scan_SV40( text, consumer)
# consumer.end_sequence()
def _scan_sequence(self, text, consumer ):
start = string.find( text, 'Recognition Sequence:' )
end = string.find( text, 'Base (Type of methylation):' )
if( end == -1 ):
end = string.find( text, 'REBASE enzyme #:' )
next_item = text[ start:end ]
consumer.sequence( next_item )
def _scan_methylation(self, text, consumer ):
start = string.find( text, 'Base (Type of methylation):' )
if( start != -1 ):
end = string.find( text, 'REBASE enzyme #:' )
next_item = text[ start:end ]
consumer.methylation( next_item )
def _scan_enzyme_num(self, text, consumer ):
start = string.find( text, 'REBASE enzyme #:' )
end = string.find( text, 'Prototype:' )
next_item = text[ start:end ]
consumer.enzyme_num( next_item )
def _scan_prototype(self, text, consumer ):
start = string.find( text, 'Prototype:' )
end = string.find( text, 'Source:' )
next_item = text[ start:end ]
consumer.prototype( next_item )
def _scan_source(self, text, consumer ):
start = string.find( text, 'Source:' )
end = string.find( text, 'Microorganism:' )
next_item = text[ start:end ]
consumer.source( next_item )
def _scan_microorganism(self, text, consumer ):
start = string.find( text, 'Microorganism:' )
end = string.find( text, 'Growth Temperature:' )
next_item = text[ start:end ]
consumer.microorganism( next_item )
def _scan_temperature(self, text, consumer):
start = string.find( text, 'Growth Temperature:' )
end = start + 30
next_item = text[ start:end ]
consumer.temperature( next_item )
def _scan_date_entered(self, text, consumer):
start = string.find( text, 'Entered:' )
end = start + 30
next_item = text[ start:end ]
consumer.data_entered( next_item )
def _scan_date_modified(self, text, consumer):
start = string.find( text, 'Modified:' )
if( start != -1 ):
end = start + 30
next_item = text[ start:end ]
consumer.data_modified( next_item )
def _scan_Adeno2( self, text, consumer ):
start = string.find( text, 'Adeno2:' )
end = string.find( text, 'Lambda:' )
next_item = text[ start:end ]
consumer.num_Adeno2( next_item )
def _scan_Lambda( self, text, consumer ):
start = string.find( text, 'Lambda:' )
end = string.find( text, 'pBR322:' )
next_item = text[ start:end ]
consumer.num_Lambda( next_item )
def _scan_pBR322(self, text, consumer ):
start = string.find( text, 'pBR322:' )
end = string.find( text, 'PhiX174:' )
next_item = text[ start:end ]
consumer.num_pBR322( next_item )
def _scan_PhiX174(self, text, consumer ):
start = string.find( text, 'PhiX174:' )
end = string.find( text, 'SV40:' )
next_item = text[ start:end ]
consumer.num_PhiX174( next_item )
def _scan_SV40(self, text, consumer ):
start = string.find( text, 'SV40:' )
end = start + 30
next_item = text[ start:end ]
consumer.num_SV40( next_item )
class _RecordConsumer(AbstractConsumer):
"""Consumer that converts a rebase record to a Record object.
Members:
data Record with rebase data.
"""
def __init__(self):
self.data = None
def start_sequence(self):
self.data = Record()
def end_sequence(self):
pass
def sequence( self, line ):
cols = string.split( line, ': ' )
sequence = cols[ 1 ]
sequence = string.strip( sequence )
if( string.find( sequence, ' ...' ) != -1 ):
cols = string.split( sequence, '...' )
self.data.seq_5_to_3 = cols[ 1 ]
elif( string.lower( sequence ) != 'unknown' ):
seq_len = len( sequence ) / 2
self.data.seq_5_to_3 = string.strip( sequence[ :seq_len ] )
self.data.seq_3_to_5 = string.strip( sequence[ seq_len: ] )
def methylation( self, line ):
cols = string.split( line, ': ' )
self.data.methylation = cols[ 1 ]
def enzyme_num( self, line ):
cols = string.split( line, ': ' )
self.data.enzyme_num = int( cols[ 1 ] )
def prototype( self, line ):
cols = string.split( line, ': ' )
self.data.prototype = cols[ 1 ]
def source( self, line ):
cols = string.split( line, ': ' )
self.data.source = cols[ 1 ]
def microorganism( self, line ):
cols = string.split( line, ': ' )
self.data.microorganism = cols[ 1 ]
def temperature( self, line ):
cols = string.split( line, ':' )
cols = string.split( cols[ 1 ], ' ' )
self.data.temperature = cols[ 1 ]
def data_entered( self, line ):
cols = string.split( line, ':' )
cols = string.split( cols[ 1 ] )
self.data.date_entered = string.join( cols[ :3 ] )
def data_modified( self, line ):
cols = string.split( line, ':' )
cols = string.split( cols[ 1 ] )
self.data.date_modified = string.join( cols[ :3 ] )
def num_Adeno2( self, line ):
cols = string.split( line, ': ' )
self.data.num_Adeno2 = int( cols[ 1 ] )
def num_Lambda( self, line ):
cols = string.split( line, ': ' )
self.data.num_Lambda = int( cols[ 1 ] )
def num_pBR322( self, line ):
cols = string.split( line, ': ' )
self.data.num_pBR322 = int( cols[ 1 ] )
def num_PhiX174( self, line ):
cols = string.split( line, ': ' )
self.data.num_PhiX174 = int( cols[ 1 ] )
def num_SV40( self, line ):
cols = string.split( line, ':' )
cols = string.split( cols[ 1 ], ' ' )
self.data.num_SV40 = cols[ 1 ]
def index_file(filename, indexname, rec2key=None):
"""index_file(filename, ind/exname, rec2key=None)
Index a rebase file. filename is the name of the file.
indexname is the name of the dictionary. rec2key is an
optional callback that takes a Record and generates a unique key
(e.g. the accession number) for the record. If not specified,
the sequence title will be used.
"""
if not os.path.exists(filename):
raise ValueError, "%s does not exist" % filename
index = Index.Index(indexname, truncate=1)
index[Dictionary._Dictionary__filename_key] = filename
iter = Iterator(open(filename), parser=RecordParser())
while 1:
start = iter._uhandle.tell()
rec = iter.next()
length = iter._uhandle.tell() - start
if rec is None:
break
if rec2key is not None:
key = rec2key(rec)
else:
key = rec.title
if not key:
raise KeyError, "empty sequence key was produced"
elif index.has_key(key):
raise KeyError, "duplicate key %s found" % key
index[key] = start, length