Package Bio :: Package NBRF
[hide private]
[frames] | no frames]

Source Code for Package Bio.NBRF

  1  # Copyright 2001 by Katharine Lindner.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  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  # Martel 
 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   
27 -class Iterator:
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
42 - def next(self):
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
55 - def __iter__(self):
56 return iter(self.next, None)
57
58 -class _Scanner:
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 """
64 - def __init__(self, debug = 0):
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 # a listing of all tags we are interested in scanning for 77 # in the MartelParser 78 self.interest_tags = [ "sequence_type", \ 79 "sequence_name", \ 80 "comment", \ 81 "sequence_line", \ 82 "sequence_final_text" ] 83 84 # make a parser that returns only the tags we are interested in 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 # self._parser.setErrorHandler(handle.ErrorHandler()) 98 99 self._parser.parseFile(handle)
100
101 -class _RecordConsumer:
102 """Create an NBRF Record object from scanner generated information. 103 """
104 - def __init__(self):
105 self.data = Record.Record() 106 self._sequences = []
107
108 - def sequence_type(self, sequence_type ):
109 self.data.sequence_type = "".join(sequence_type)
110
111 - def sequence_name(self, sequence_name ):
112 self.data.sequence_name = "".join(sequence_name)
113
114 - def comment(self, comment ):
115 self.data.comment = "".join(comment)
116
117 - def sequence_line( self, sequences ):
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
129 -class RecordParser:
130 """Parse NBRF files into Record objects 131 """
132 - def __init__(self, debug_level = 0):
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