Package Bio :: Package Blast :: Module ParseBlastTable
[hide private]
[frames] | no frames]

Source Code for Module Bio.Blast.ParseBlastTable

  1  # Copyright 2003 Iddo Friedberg. 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  """A parser for the NCBI blastpgp version 2.2.5 output format. Currently only supports 
  7  the '-m 9' option, (table w/ annotations). 
  8  Returns a BlastTableRec instance 
  9  """ 
 10   
 11  import sys 
 12   
13 -class BlastTableEntry(object):
14 - def __init__(self, in_rec):
15 bt_fields = in_rec.split() 16 self.qid = bt_fields[0].split('|') 17 self.sid = bt_fields[1].split('|') 18 self.pid = float(bt_fields[2]) 19 self.ali_len = int(bt_fields[3]) 20 self.mis = int(bt_fields[4]) 21 self.gaps = int(bt_fields[5]) 22 self.q_bounds = (int(bt_fields[6]), int(bt_fields[7])) 23 self.s_bounds = (int(bt_fields[8]), int(bt_fields[9])) 24 self.e_value = float(bt_fields[10]) 25 self.bit_score = float(bt_fields[11])
26 27
28 -class BlastTableRec(object):
29 - def __init__(self):
30 self.program = None 31 self.version = None 32 self.date = None 33 self.iteration = None 34 self.query = None 35 self.database = None 36 self.entries = []
37
38 - def add_entry(self, entry):
39 self.entries.append(entry)
40 41 reader_keywords = {'BLASTP': 'version', 42 'Iteration': 'iteration', 43 'Query': 'query', 44 'Database': 'database', 45 'Fields': 'fields'} 46 47
48 -class BlastTableReader(object):
49 - def __init__(self, handle):
50 self.handle = handle 51 inline = self.handle.readline() 52 # zip forward to start of record 53 while inline and 'BLASTP' not in inline: 54 inline = self.handle.readline() 55 self._lookahead = inline 56 self._n = 0 57 self._in_header = 1
58
59 - def __next__(self):
60 self.table_record = BlastTableRec() 61 self._n += 1 62 inline = self._lookahead 63 if not inline: 64 return None 65 while inline: 66 if inline[0] == '#': 67 if self._in_header: 68 self._in_header = self._consume_header(inline) 69 else: 70 break 71 else: 72 self._consume_entry(inline) 73 self._in_header = 0 74 75 inline = self.handle.readline() 76 self._lookahead = inline 77 self._in_header = 1 78 return self.table_record
79 80 if sys.version_info[0] < 3:
81 - def next(self):
82 """Python 2 style alias for Python 3 style __next__ method.""" 83 return self.__next__()
84
85 - def _consume_entry(self, inline):
86 current_entry = BlastTableEntry(inline) 87 self.table_record.add_entry(current_entry)
88
89 - def _consume_header(self, inline):
90 for keyword in reader_keywords: 91 if keyword in inline: 92 in_header = self._Parse('_parse_%s' % reader_keywords[keyword], inline) 93 break 94 return in_header
95
96 - def _parse_version(self, inline):
97 program, version, date = inline.split()[1:] 98 self.table_record.program = program 99 self.table_record.version = version 100 self.table_record.date = date 101 return 1
102
103 - def _parse_iteration(self, inline):
104 self.table_record.iteration = int(inline.split()[2]) 105 return 1
106
107 - def _parse_query(self, inline):
108 self.table_record.query = inline.split()[2:] 109 return 1
110
111 - def _parse_database(self, inline):
112 self.table_record.database = inline.split()[2] 113 return 1
114
115 - def _parse_fields(self, inline):
116 return 0
117
118 - def _Parse(self, method_name, inline):
119 return getattr(self, method_name)(inline)
120