1
2
3
4
5 """Parser for the NBRF/PIR file format.
6
7 http://www.psc.edu/general/software/packages/seq-intro/nbrffile.html
8 """
9
10
11 import string
12 import array
13 import os
14 import re
15 import sgmllib
16 import urlparse
17
18
19 from xml.sax import handler
20
21
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
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
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
63 return iter(self.next, None)
64
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 """
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
84
85 self.interest_tags = [ "sequence_type", \
86 "sequence_name", \
87 "comment", \
88 "sequence_line", \
89 "sequence_final_text" ]
90
91
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
105
106 self._parser.parseFile(handle)
107
109 """Create an NBRF Record object from scanner generated information.
110 """
114
117
120
123
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
137 """Parse NBRF files into Record objects
138 """
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