Package Bio :: Package NBRF
[hide private]
[frames] | no frames]

Source Code for Package Bio.NBRF

  1  # Copyright 2001 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  """Parser for the NBRF/PIR file format. 
  6   
  7  http://www.psc.edu/general/software/packages/seq-intro/nbrffile.html 
  8  """ 
  9   
 10  # standard library 
 11  import string 
 12  import array 
 13  import os 
 14  import re 
 15  import sgmllib 
 16  import urlparse 
 17   
 18  # XML from python 2.0 
 19  from xml.sax import handler 
 20   
 21  # Martel 
 22  import Martel 
 23  from Martel import RecordReader 
 24   
 25  from Bio.ParserSupport import EventGenerator 
 26  from Bio.ParserSupport import AbstractConsumer 
 27  from Bio import File 
 28  import nbrf_format 
 29  import Record 
 30   
31 -class Iterator:
32 """Iterator interface to move over a file of NBRF entries one at a time. 33 """
34 - def __init__(self, handle, parser = None):
35 """Initialize the iterator. 36 37 Arguments: 38 o handle - A handle with NBRF entries to iterate through. 39 o parser - An optional parser to pass the entries through before 40 returning them. If None, then the raw entry will be returned. 41 """ 42 self.handle = File.UndoHandle( handle ) 43 self._reader = RecordReader.StartsWith( self.handle, ">" ) 44 self._parser = parser
45
46 - def next(self):
47 """Return the next NBRF record from the handle. 48 49 Will return None if we ran out of records. 50 """ 51 data = self._reader.next() 52 53 if self._parser is not None: 54 if data: 55 dumpfile = open( 'dump', 'w' ) 56 dumpfile.write( data ) 57 dumpfile.close() 58 return self._parser.parse(File.StringHandle(data)) 59 60 return data
61
62 - def __iter__(self):
63 return iter(self.next, None)
64
65 -class _Scanner:
66 """Start up Martel to do the scanning of the file. 67 68 This initialzes the Martel based parser and connects it to a handler 69 that will generate events for a Feature Consumer. 70 """
71 - def __init__(self, debug = 0):
72 """Initialize the scanner by setting up our caches. 73 74 Creating the parser takes a long time, so we want to cache it 75 to reduce parsing time. 76 77 Arguments: 78 o debug - The level of debugging that the parser should 79 display. Level 0 is no debugging, Level 2 displays the most 80 debugging info (but is much slower). See Martel documentation 81 for more info on this. 82 """ 83 # a listing of all tags we are interested in scanning for 84 # in the MartelParser 85 self.interest_tags = [ "sequence_type", \ 86 "sequence_name", \ 87 "comment", \ 88 "sequence_line", \ 89 "sequence_final_text" ] 90 91 # make a parser that returns only the tags we are interested in 92 expression = Martel.select_names( nbrf_format.nbrf_record, self.interest_tags) 93 self._parser = expression.make_parser(debug_level = debug)
94
95 - def feed(self, handle, consumer):
96 """Feeed a set of data into the scanner. 97 98 Arguments: 99 o handle - A handle with the information to parse. 100 o consumer - The consumer that should be informed of events. 101 """ 102 self._parser.setContentHandler( EventGenerator(consumer, 103 self.interest_tags)) 104 # self._parser.setErrorHandler(handle.ErrorHandler()) 105 106 self._parser.parseFile(handle)
107
108 -class _RecordConsumer:
109 """Create an NBRF Record object from scanner generated information. 110 """
111 - def __init__(self):
112 self.data = Record.Record() 113 self._sequences = []
114
115 - def sequence_type(self, sequence_type ):
116 self.data.sequence_type = "".join(sequence_type)
117
118 - def sequence_name(self, sequence_name ):
119 self.data.sequence_name = "".join(sequence_name)
120
121 - def comment(self, comment ):
122 self.data.comment = "".join(comment)
123
124 - def sequence_line( self, sequences ):
125 new_seq = "".join(sequences) 126 parts = new_seq.split() 127 self._sequences.append("".join(parts))
128
129 - def sequence_final_text( self, sequences ):
130 new_seq = "".join(sequences) 131 parts = new_seq.split() 132 self._sequences.append("".join(parts)) 133 134 self.data.sequence.data = "".join(self._sequences)
135
136 -class RecordParser:
137 """Parse NBRF files into Record objects 138 """
139 - def __init__(self, debug_level = 0):
140 """Initialize the parser. 141 142 Arguments: 143 o debug_level - An optional argument that specifies the amount of 144 debugging information Martel should spit out. By default we have 145 no debugging info (the fastest way to do things), but if you want 146 you can set this as high as two and see exactly where a parse fails. 147 """ 148 self._scanner = _Scanner(debug_level)
149
150 - def parse(self, handle):
151 """Parse the specified handle into an NBRF record. 152 """ 153 self._consumer = _RecordConsumer() 154 self._scanner.feed(handle, self._consumer) 155 return self._consumer.data
156