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  # standard library 
 18  import string 
 19   
 20  # Martel 
 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   
29 -class Iterator:
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
44 - def next(self):
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
60 - def __iter__(self):
61 return iter(self.next, None)
62
63 -class _Scanner:
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 """
69 - def __init__(self, debug = 0):
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 # a listing of all tags we are interested in scanning for 82 # in the MartelParser 83 self.interest_tags = [ "sequence_type", \ 84 "sequence_name", \ 85 "comment", \ 86 "sequence_line", \ 87 "sequence_final_text" ] 88 89 # make a parser that returns only the tags we are interested in 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 # self._parser.setErrorHandler(handle.ErrorHandler()) 103 104 self._parser.parseFile(handle)
105
106 -class _RecordConsumer:
107 """Create an NBRF Record object from scanner generated information. 108 """
109 - def __init__(self):
110 self.data = Record.Record() 111 self._sequences = []
112
113 - def sequence_type(self, sequence_type ):
114 self.data.sequence_type = "".join(sequence_type)
115
116 - def sequence_name(self, sequence_name ):
117 self.data.sequence_name = "".join(sequence_name)
118
119 - def comment(self, comment ):
120 self.data.comment = "".join(comment)
121
122 - def sequence_line( self, sequences ):
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
134 -class RecordParser:
135 """Parse NBRF files into Record objects 136 """
137 - def __init__(self, debug_level = 0):
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