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

Source Code for Package Bio.IntelliGenetics

  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 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  # Martel 
 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 
27 -class Iterator:
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
42 - def next(self):
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
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 = ["comment", "title_line", "sequence" ] 79 80 # make a parser that returns only the tags we are interested in 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 # self._parser.setErrorHandler(handle.ErrorHandler()) 94 95 self._parser.parseFile(handle)
96
97 -class _RecordConsumer:
98 """Create an IntelliGenetics Record object from scanner generated information. 99 """
100 - def __init__(self):
101 self.data = Record.Record()
102 103
104 - def title_line(self, title):
105 self.data.title = title[ 0 ]
106
107 - def comment(self, comments ):
108 for comment in comments: 109 self.data.comments.append( comment )
110
111 - def sequence( self, sequences ):
112 for sequence in sequences: 113 self.data.sequence.data = self.data.sequence.data + sequence.strip()
114
115 -class RecordParser:
116 """Parse IntelliGenetics files into Record objects 117 """
118 - def __init__(self, debug_level = 0):
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
136 -class IntelliGeneticsReader( Martel.RecordReader.RecordReader ):
137
138 - def __init__( self, infile ):
140
141 - def next( self ):
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