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