Package Bio :: Module FilteredReader
[hide private]
[frames] | no frames]

Source Code for Module Bio.FilteredReader

  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   
  6  """Code for more fancy file handles (DEPRECATED). 
  7   
  8  This module is now deprecated, and will be removed in a future release of 
  9  Biopython. 
 10   
 11  Classes: 
 12  Filtered is a decorator for File that allows the user to filter the output 
 13  on a line by line basis. 
 14   
 15  The FilteredReader module reads a file and applies a sequence of filters to the input 
 16  The constructor sets a default filter chain, but the user can select another filter by setting 
 17  Bio.FilteredReader.filter_chain. 
 18   
 19  handle = open( "filename" ) 
 20  filtered_reader = Bio.FilteredReader( handle ) 
 21  filtered_reader.filter_chain = [ remove_asterisks, replace_dot_with_dash ] 
 22  filtered_reasder.read() 
 23   
 24  All filters in the chain must provide the same interface with a line of text as the single 
 25  input parameter and altered text as the return value. 
 26  """ 
 27   
 28  import warnings 
 29  warnings.warn("Bio.FilteredReader is deprecated, and will be removed in a"\ 
 30                " future release of Biopython.  If you want to continue to use"\ 
 31                " this code, please get in contact with the developers"\ 
 32                " via the mailing lists to avoid its permanent removal from"\ 
 33                " Biopython.", DeprecationWarning) 
 34   
35 -def dump_saved( name, text, j ):
36 """Used for debugging.""" 37 dump_file = open( name + '%d' % j, "w" ) 38 k = 0 39 for i in range ( 0, len( text ), 80 ): 40 dump_file.write( '%s\n' % text[ i : i + 80 ] ) 41 dump_file.close()
42
43 -def remove_leading_whitespace( line ):
44 return line.lstrip()
45 46
47 -def remove_empty_line( line ):
48 stripped_line = line.strip() 49 if( stripped_line ): 50 return line[ : ] 51 else: 52 return ''
53
54 -def remove_useless_dot( line ):
55 before = line 56 while( 1 ): 57 after = before.replace( "\t.\t", "\t\t" ) 58 if( len( before ) == len( after ) ): 59 break 60 before = after 61 if( after.endswith( '.' ) ): 62 after = after[ :-1 ] 63 return after
64
65 -def fix_punctuation( line ):
66 line = line.replace( "'", '' ) 67 line = line.replace( '"', '' ) 68 line = line.replace( ';', '\t' ) 69 line = line.replace( 'entryname', 'id' ) 70 # line = line.lower( ) 71 if( line ): 72 return line[ : ] 73 else: 74 return ''
75 76 77
78 -class FilteredReader:
79 - def __init__(self, handle ):
80 self._handle = handle 81 self._start_line = '' 82 self._debug_count = 0 83 self.filter_chain = [ remove_empty_line, remove_useless_dot, fix_punctuation ]
84
85 - def __getattr__(self, attr):
86 return getattr(self._handle, attr)
87 88 89
90 - def close(self, *args, **keywds ):
91 return self._handle.close( *args, **keywds)
92
93 - def read( self, *args, **keywds ):
94 line = '' 95 len_expected = self._get_len_expected( args, keywds ) 96 if( len_expected ): 97 filtered_text = self.read_block( len_expected ) 98 else: 99 filtered_text = self.read_to_end() 100 return filtered_text
101
102 - def read_block( self, len_expected ):
103 104 len_filtered = 0 105 len_adjusted -= len( self._start_line ) 106 filtered_text = '' 107 while( len_filtered < len_expected ): 108 109 text_read = self._handle.read( len_adjusted ) 110 full_text = self._start_line + text_read 111 lines = full_text.splitlines( 1 ) 112 if( text_read == '' ): 113 filtered_text = filtered_text + self.filter( lines ) 114 break 115 else: 116 all_but_last_line = lines[ :-1 ] 117 self._start_line = lines[ -1 ] 118 filtered_text = filtered_text + self.filter( all_but_last_line ) 119 len_filtered_text = len( filtered_text ) 120 len_adjusted = len_adjusted - len_filtered_text 121 return filtered_text[ : ]
122
123 - def read_to_end( self ):
124 filtered_text = '' 125 text_read = self._handle.read() 126 full_text = self._start_line + text_read 127 lines = full_text.splitlines( 1 ) 128 filtered_text += self.filter( lines[:] ) 129 return filtered_text[ : ]
130
131 - def _get_len_expected( self, args, keywds ):
132 133 if( len( args) > 0 ): 134 len_expected = args[ 0 ] 135 if( len_expected < 0 ): 136 len_expected = None 137 elif 'size' in keywds: 138 len_expected = keywds['size'] 139 else: 140 len_expected = None 141 return len_expected
142
143 - def filter( self, lines ):
144 filter_chain = self.filter_chain 145 filtered_text = '' 146 for line in lines: 147 for filter in filter_chain: 148 line = filter( *( line, ) ) 149 filtered_text += line 150 151 return filtered_text
152
153 -def has_trailing_linefeed( line ):
154 if( line.endswith( chr( 13 ) ) or \ 155 line.endswith( chr( 10 ) ) ): 156 return 1 157 else: 158 return 0
159