File: __init__.py

package info (click to toggle)
python-biopython 1.42-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,584 kB
  • ctags: 12,272
  • sloc: python: 80,461; xml: 13,834; ansic: 7,902; cpp: 1,855; sql: 1,144; makefile: 203
file content (471 lines) | stat: -rw-r--r-- 14,811 bytes parent folder | download | duplicates (3)
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# 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 Gobase.
http://megasun.bch.umontreal.ca/gobase/


Classes:
Record             Holds gobase sequence data.
Iterator           Iterates over sequence data in a gobase file.
Dictionary         Accesses a gobase file using a dictionary interface.
RecordParser       Parses gobase sequence data into a Record object.

_Scanner           Scans a gobase-format stream.
_RecordConsumer    Consumes gobase data to a Record object.


Functions:
index_file         Index a FASTA file for a Dictionary.

"""
from types import *
import string
import re
from Bio import File
from Bio import Index
from Bio.ParserSupport import *

class Record:
    """Holds information from a Gobase record.

    Members:
    species_name
    taxon_division
    gobase_id
    """
    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.species_name = ''
        self.taxon_division = ''

class SequenceRecord( Record ):
    """Holds information from a Gobase record.

    Members:
    molecule_type
    is_plasmid
    shape
    submission_date
    update_date
    entrez_record
    genbank_accession
    """
    def __init__(self, colwidth=60):
        """__init__(self, colwidth=60)

        Create a new Record.  colwidth specifies the number of residues
        to put on each line.

        """
        Record.__init__( self )
        self.molecule_type = ''
        self.is_plasmid = ''
        self.shape = ''
        self.submission_date = ''
        self.update_date = ''
        self.entrez_record = ''
        self.genbank_accession = ''

class GeneRecord( Record ):
    """Holds information from a Gobase record.

    Members:
     """
    def __init__(self, colwidth=60):
        """__init__(self, colwidth=60)

        Create a new Record.  colwidth specifies the number of residues
        to put on each line.

        """
        Record.__init__( self )
        self.gene_class = ''
        self.plasmid_encoded = ''
        self.is_partial_gene = ''
        self.is_pseudo_gene = ''
        self.is_transpliced_gene = ''
        self.chloroplast_origin = ''
        self.contains_intron = ''
        self.orf = ''
        self.included_in_intron = ''
        self.published_info = ''
        self.genbank_accession = ''
        self.entrez_record = ''
        self.product_type = ''
        self.product_class = ''

class ProteinRecord( Record ):
    """Holds information from a Gobase record.

    Members:
    product_class
    gene_class
    is_partial_protein
    is_plasmid
    function
    entry_record
    """
    def __init__(self, colwidth=60):
        """__init__(self, colwidth=60)

        Create a new Record.  colwidth specifies the number of residues
        to put on each line.

        """
        Record.__init__( self )
        self.product_class = ''
        self.gene_class = ''
        self.is_partial_protein = ''
        self.is_plasmid = ''
        self.is_pseudo = ''
        self.function = ''
        self.entry_record = ''

class Iterator:
    """Returns one record at a time from a Gobase 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 gobase 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 gobase file using a dictionary interface.

    """
    __filename_key = '__filename'

    def __init__(self, indexname, parser=None):
        """__init__(self, indexname, parser=None)

        Open a Gobase 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 Gobase 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 gobase file.

    Methods:
    feed   Feed in one gobase record.

    """
    def feed(self, handle, consumer):
        """feed(self, handle, consumer)

        Feed in gobase data for scanning.  handle is a file-like object
        containing gobase data.  consumer is a Consumer object that will
        receive events as the gobase 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 ):
            try:
                line = self._scan_line( uhandle )
                text = text + line
            except:
                if( line == '' ):
                    return text
        return text

    def _scan_sequence_record( self, text, consumer ):
        data = consumer.data
        next_item = self._scan_field( text, 'Molecule type:', 'Species name:' )
        data.molecule_type = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Shape of molecule:', 'Sequence length:' )
        data.shape = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Plasmid:', 'Complete genome:' )
        data.is_plasmid = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'NCBI Entrez record:', 'Genbank accession:' )
        data.entrez_record = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Genbank accession:', 'Coding gene(s):' )
        data.genbank_accession = consumer.text_field( next_item )
        consumer.data = data

    def _scan_gene_record( self, text, consumer ):
        data = consumer.data
        next_item = self._scan_field( text, 'Gene Class:', 'Species name:' )
        data.gene_class = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Plasmid encoded:', 'Partial gene:' )
        data.is_plasmid = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'Partial gene:', 'Pseudo:' )
        data.is_partial_gene = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Pseudo:', 'Transpliced gene:' )
        data.is_pseudo_gene = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Transpliced gene:', 'Chloroplast origin:' )
        data.is_transpliced_gene = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Chloroplast origin:', 'Contains intron(s):' )
        data.chloroplast_origin = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'Contains intron(s):' )
        data.contains_intron = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'Included in intron:' )
        data.included_in_intron = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'ORF:' )
        data.orf = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'NCBI Entrez record:' )
        data.entrez_record = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'Genbank accession:', 'Product type:' )
        data.genbank_accession = consumer.word_field( next_item )

        next_item = self._scan_field( text, 'Product type:', 'Product Class:' )
        data.product_type = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Product Class:' )
        data.product_class = consumer.text_field( next_item )

        consumer.data = data

    def _scan_protein_record( self, text, consumer ):
        data = consumer.data
        next_item = self._scan_field( text, 'Product Class:', 'Species name:' )
        data.product_class = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Gene Class:', 'Partial protein:' )
        data.gene_class = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Partial protein:', 'Conflict:' )
        data.is_partial_protein = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Plasmid:', 'Sequence length:' )
        data.is_plasmid = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'General function:' )
        data.function = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'NCBI Entrez record:' )
        data.entrez_record = consumer.word_field( next_item )

        consumer.data = data

    def _scan_record(self, uhandle, consumer):
        text = ''
        text = self._text_in( uhandle, text, 100 )
        text = string.lstrip( text )

        if( string.find( text, 'Sequence' ) == 0 ):
            consumer.data = SequenceRecord()
            self._scan_sequence_record( text, consumer )
        elif( string.find( text, 'Gene' ) == 0 ):
            consumer.data = GeneRecord()
            self._scan_gene_record( text, consumer )
        elif( string.find( text, 'Protein' ) == 0 ):
            consumer.data = ProteinRecord()
            self._scan_protein_record( text, consumer )
        else:
            print 'UNKNOWN!!!!!!'

        data = consumer.data
        next_item = self._scan_field( text, 'Species name:', 'Taxon division' )
        data.species_name = consumer.text_field( next_item )

        next_item = self._scan_field( text, 'Taxon division:' )
        print next_item
        data.taxon_division = consumer.word_field( next_item )
        consumer.data = data

#        consumer.end_sequence()


    def _scan_field(self, text, field, next_field = None ):
        start = string.find( text, field )
        if( start == -1 ):
            return ''
        if( next_field == None ):
            pattern = re.compile( '[A-Z][a-z0-9 ]+:' )
            offset = start + len( field )
            match = pattern.search( text[ offset: ] )
            if match:
                end = offset + match.start()
            else:
                end = start + 40
        else:
            end = string.find( text, next_field )
            if( end == -1 ):
                return ''
        next_item = text[ start:end ]
        return( next_item )


class _RecordConsumer(AbstractConsumer):
    """Consumer that converts a gobase record to a Record object.

    Members:
    data    Record with gobase data.

    """
    def __init__(self):
        self.data = None

    def end_sequence(self):
        pass

    def text_field( self, line ):
        if( line == '' ):
            return ''
        cols = string.split( line, ': ' )
        return( cols[ 1 ] )

    def int_field( self, line ):
        if( line == '' ):
            return None
        cols = string.split( line, ': ' )
        return( int( cols[ 1 ] ) )

    def word_field( self, line ):
        if( line == '' ):
            return ''
        cols = string.split( line, ': ' )
        cols = string.split( cols[ 1 ] )
        return( cols[ 0 ] )

    def date_field( self, line ):
        if( line == '' ):
            return ''
        cols = string.split( line, ':' )
        cols = string.split( cols[ 1 ] )
        return( string.join( cols[ :3 ] ) )


def index_file(filename, indexname, rec2key=None):
    """index_file(filename, ind/exname, rec2key=None)

    Index a gobase 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