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
19 import Martel
20 from Martel import RecordReader
21
22 from Bio.ParserSupport import EventGenerator
23 from Bio import File
24 import nbrf_format
25 import Record
26
28 """Iterator interface to move over a file of NBRF 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 NBRF 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 = RecordReader.StartsWith( self.handle, ">" )
40 self._parser = parser
41
43 """Return the next NBRF 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 = [ "sequence_type", \
79 "sequence_name", \
80 "comment", \
81 "sequence_line", \
82 "sequence_final_text" ]
83
84
85 expression = Martel.select_names( nbrf_format.nbrf_record, self.interest_tags)
86 self._parser = expression.make_parser(debug_level = debug)
87
88 - def feed(self, handle, consumer):
89 """Feeed a set of data into the scanner.
90
91 Arguments:
92 o handle - A handle with the information to parse.
93 o consumer - The consumer that should be informed of events.
94 """
95 self._parser.setContentHandler( EventGenerator(consumer,
96 self.interest_tags))
97
98
99 self._parser.parseFile(handle)
100
102 """Create an NBRF Record object from scanner generated information.
103 """
107
110
113
116
118 new_seq = "".join(sequences)
119 parts = new_seq.split()
120 self._sequences.append("".join(parts))
121
122 - def sequence_final_text( self, sequences ):
123 new_seq = "".join(sequences)
124 parts = new_seq.split()
125 self._sequences.append("".join(parts))
126
127 self.data.sequence.data = "".join(self._sequences)
128
130 """Parse NBRF files into Record objects
131 """
133 """Initialize the parser.
134
135 Arguments:
136 o debug_level - An optional argument that specifies the amount of
137 debugging information Martel should spit out. By default we have
138 no debugging info (the fastest way to do things), but if you want
139 you can set this as high as two and see exactly where a parse fails.
140 """
141 self._scanner = _Scanner(debug_level)
142
143 - def parse(self, handle):
144 """Parse the specified handle into an NBRF record.
145 """
146 self._consumer = _RecordConsumer()
147 self._scanner.feed(handle, self._consumer)
148 return self._consumer.data
149