1
2
3
4
5 """Parser for SAF (Simple Alignment Format).
6
7 http://www.embl-heidelberg.de/predictprotein/Dexa/optin_safDes.html
8 """
9
10
11 import Martel
12 from Martel import RecordReader
13 from Martel import Dispatch
14
15
16 from Bio import File
17 import saf_format
18 import Record
19
20
22 """Iterator interface to move over a file of Saf entries one at a time.
23 """
24 - def __init__(self, handle, parser = None):
25 """Initialize the iterator.
26
27 Arguments:
28 o handle - A handle with Saf entries to iterate through.
29 o parser - An optional parser to pass the entries through before
30 returning them. If None, then the raw entry will be returned.
31 """
32 self.handle = File.UndoHandle( handle )
33 self._reader = RecordReader.Everything( self.handle )
34 self._parser = parser
35
37 """Return the next Saf record from the handle.
38
39 Will return None if we ran out of records.
40 """
41 data = self._reader.next()
42
43 if self._parser is not None:
44 if data:
45 dumpfile = open( 'dump', 'w' )
46 dumpfile.write( data )
47 dumpfile.close()
48 return self._parser.parse(File.StringHandle(data))
49
50 return data
51
53 return iter(self.next, None)
54
56 """Start up Martel to do the scanning of the file.
57
58 This initialzes the Martel based parser and connects it to a handler
59 that will generate events for a Feature Consumer.
60 """
62 """Initialize the scanner by setting up our caches.
63
64 Creating the parser takes a long time, so we want to cache it
65 to reduce parsing time.
66
67 Arguments:
68 o debug - The level of debugging that the parser should
69 display. Level 0 is no debugging, Level 2 displays the most
70 debugging info (but is much slower). See Martel documentation
71 for more info on this.
72 """
73
74
75 self.interest_tags = [ 'candidate_line', 'saf_record' ]
76
77
78 expression = Martel.select_names( saf_format.saf_record, self.interest_tags)
79 self._parser = expression.make_parser(debug_level = debug)
80
81 - def feed(self, handle, consumer):
82 """Feed a set of data into the scanner.
83
84 Arguments:
85 o handle - A handle with the information to parse.
86 o consumer - The consumer that should be informed of events.
87 """
88 consumer.set_interest_tags( self.interest_tags )
89 self._parser.setContentHandler( consumer )
90
91
92 self._parser.parseFile(handle)
93
95 """Create a Saf Record object from scanner generated information.
96 """
101
103 self._sequences = {}
104 self._names = {}
105 self._history = []
106 self._guide = ''
107 self._ref_length = 0
108 self._ordinal = 0
109
112
116
117
120
145
148
157
159 """Parse Saf files into Record objects
160 """
162 """Initialize the parser.
163
164 Arguments:
165 o debug_level - An optional argument that specifies the amount of
166 debugging information Martel should spit out. By default we have
167 no debugging info (the fastest way to do things), but if you want
168 you can set this as high as two and see exactly where a parse fails.
169 """
170 self._scanner = _Scanner(debug_level)
171
172 - def parse(self, handle):
173 """Parse the specified handle into a SAF record.
174 """
175 self._consumer = _RecordConsumer()
176 self._scanner.feed(handle, self._consumer)
177 return self._consumer.data
178