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