1
2
3
4
5 """Parser for the NBRF/PIR file format (DEPRECATED).
6
7 Please use Bio.SeqIO with the "pir" format instead."""
8
9 import warnings
10 warnings.warn("Bio.NBRF is deprecated." \
11 + " We hope the new 'pir' 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 import string
19
20
21 import Martel
22 from Martel import RecordReader
23
24 from Bio.ParserSupport import EventGenerator
25 from Bio import File
26 import nbrf_format
27 import Record
28
30 """Iterator interface to move over a file of NBRF entries one at a time.
31 """
32 - def __init__(self, handle, parser = None):
33 """Initialize the iterator.
34
35 Arguments:
36 o handle - A handle with NBRF entries to iterate through.
37 o parser - An optional parser to pass the entries through before
38 returning them. If None, then the raw entry will be returned.
39 """
40 self.handle = File.UndoHandle( handle )
41 self._reader = RecordReader.StartsWith( self.handle, ">" )
42 self._parser = parser
43
45 """Return the next NBRF record from the handle.
46
47 Will return None if we ran out of records.
48 """
49 data = self._reader.next()
50
51 if self._parser is not None:
52 if data:
53 dumpfile = open( 'dump', 'w' )
54 dumpfile.write( data )
55 dumpfile.close()
56 return self._parser.parse(File.StringHandle(data))
57
58 return data
59
61 return iter(self.next, None)
62
64 """Start up Martel to do the scanning of the file.
65
66 This initialzes the Martel based parser and connects it to a handler
67 that will generate events for a Feature Consumer.
68 """
70 """Initialize the scanner by setting up our caches.
71
72 Creating the parser takes a long time, so we want to cache it
73 to reduce parsing time.
74
75 Arguments:
76 o debug - The level of debugging that the parser should
77 display. Level 0 is no debugging, Level 2 displays the most
78 debugging info (but is much slower). See Martel documentation
79 for more info on this.
80 """
81
82
83 self.interest_tags = [ "sequence_type", \
84 "sequence_name", \
85 "comment", \
86 "sequence_line", \
87 "sequence_final_text" ]
88
89
90 expression = Martel.select_names( nbrf_format.nbrf_record, self.interest_tags)
91 self._parser = expression.make_parser(debug_level = debug)
92
93 - def feed(self, handle, consumer):
94 """Feeed a set of data into the scanner.
95
96 Arguments:
97 o handle - A handle with the information to parse.
98 o consumer - The consumer that should be informed of events.
99 """
100 self._parser.setContentHandler( EventGenerator(consumer,
101 self.interest_tags))
102
103
104 self._parser.parseFile(handle)
105
107 """Create an NBRF Record object from scanner generated information.
108 """
112
115
118
121
123 new_seq = "".join(sequences)
124 parts = new_seq.split()
125 self._sequences.append("".join(parts))
126
127 - def sequence_final_text( self, sequences ):
128 new_seq = "".join(sequences)
129 parts = new_seq.split()
130 self._sequences.append("".join(parts))
131
132 self.data.sequence.data = "".join(self._sequences)
133
135 """Parse NBRF files into Record objects
136 """
138 """Initialize the parser.
139
140 Arguments:
141 o debug_level - An optional argument that specifies the amount of
142 debugging information Martel should spit out. By default we have
143 no debugging info (the fastest way to do things), but if you want
144 you can set this as high as two and see exactly where a parse fails.
145 """
146 self._scanner = _Scanner(debug_level)
147
148 - def parse(self, handle):
149 """Parse the specified handle into an NBRF record.
150 """
151 self._consumer = _RecordConsumer()
152 self._scanner.feed(handle, self._consumer)
153 return self._consumer.data
154