1
2
3
4
5 """Parser for the MASE/IntelliGenetics alignment file format (DEPRECATED).
6
7 Please use Bio.SeqIO with the "ig" format instead."""
8
9 import warnings
10 warnings.warn("Bio.IntelliGenetics is deprecated." \
11 + " We hope the new 'ig' format support in Bio.SeqIO will be" \
12 + " suitable for most users. Please get in touch on the " \
13 + " mailing lists if this (or its removal) causes any problems "\
14 + "for you.",
15 DeprecationWarning)
16
17
18
19
20 import Martel
21 from Martel import RecordReader
22
23 from Bio.ParserSupport import EventGenerator
24 from Bio import File
25 import intelligenetics_format
26 import Record
28 """Iterator interface to move over a file of IntelliGenetics entries one at a time.
29 """
30 - def __init__(self, handle, parser = None):
31 """Initialize the iterator.
32
33 Arguments:
34 o handle - A handle with IntelliGenetics entries to iterate through.
35 o parser - An optional parser to pass the entries through before
36 returning them. If None, then the raw entry will be returned.
37 """
38 self.handle = File.UndoHandle( handle )
39 self._reader = IntelliGeneticsReader( self.handle )
40 self._parser = parser
41
43 """Return the next IntelliGenetics record from the handle.
44
45 Will return None if we ran out of records.
46 """
47 data = self._reader.next()
48
49 if self._parser is not None:
50 if data:
51 return self._parser.parse(File.StringHandle(data))
52
53 return data
54
56 return iter(self.next, None)
57
59 """Start up Martel to do the scanning of the file.
60
61 This initialzes the Martel based parser and connects it to a handler
62 that will generate events for a Feature Consumer.
63 """
65 """Initialize the scanner by setting up our caches.
66
67 Creating the parser takes a long time, so we want to cache it
68 to reduce parsing time.
69
70 Arguments:
71 o debug - The level of debugging that the parser should
72 display. Level 0 is no debugging, Level 2 displays the most
73 debugging info (but is much slower). See Martel documentation
74 for more info on this.
75 """
76
77
78 self.interest_tags = ["comment", "title_line", "sequence" ]
79
80
81 expression = Martel.select_names(intelligenetics_format.intelligenetics_record, self.interest_tags)
82 self._parser = expression.make_parser(debug_level = debug)
83
84 - def feed(self, handle, consumer):
85 """Feeed a set of data into the scanner.
86
87 Arguments:
88 o handle - A handle with the information to parse.
89 o consumer - The consumer that should be informed of events.
90 """
91 self._parser.setContentHandler( EventGenerator(consumer,
92 self.interest_tags))
93
94
95 self._parser.parseFile(handle)
96
98 """Create an IntelliGenetics Record object from scanner generated information.
99 """
102
103
106
110
114
116 """Parse IntelliGenetics files into Record objects
117 """
119 """Initialize the parser.
120
121 Arguments:
122 o debug_level - An optional argument that specifies the amount of
123 debugging information Martel should spit out. By default we have
124 no debugging info (the fastest way to do things), but if you want
125 you can set this as high as two and see exactly where a parse fails.
126 """
127 self._scanner = _Scanner(debug_level)
128
129 - def parse(self, handle):
130 """Parse the specified handle into a GenBank record.
131 """
132 self._consumer = _RecordConsumer()
133 self._scanner.feed(handle, self._consumer)
134 return self._consumer.data
135
137
140
142 infile = self.infile
143 state = 'COMMENT_STATE'
144 record = ''
145 while( state != 'DONE' ):
146 line = infile.readline()
147 if( line == '' ):
148 state = 'DONE'
149 break
150 if( line[ 0 ] == ';' ):
151 if( state == 'SEQUENCE_STATE' ):
152 state = 'DONE'
153 infile.saveline( line )
154 elif( state == 'COMMENT_STATE' ):
155 record = record + line
156 else:
157 if( state == 'COMMENT_STATE' ):
158 record = record + line
159 state = 'SEQUENCE_STATE'
160 elif( state == 'SEQUENCE_STATE' ):
161 record = record + line
162 return record
163