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 43 reader_keywords = {'BLASTP': 'version', 44 'Iteration': 'iteration', 45 'Query': 'query', 46 'Database': 'database', 47 'Fields': 'fields'} 48 49
50 -class BlastTableReader(object):
51 - def __init__(self, handle):
52 self.handle = handle 53 inline = self.handle.readline() 54 # zip forward to start of record 55 while inline and 'BLASTP' not in inline: 56 inline = self.handle.readline() 57 self._lookahead = inline 58 self._n = 0 59 self._in_header = 1
60
61 - def __next__(self):
62 self.table_record = BlastTableRec() 63 self._n += 1 64 inline = self._lookahead 65 if not inline: 66 return None 67 while inline: 68 if inline[0] == '#': 69 if self._in_header: 70 self._in_header = self._consume_header(inline) 71 else: 72 break 73 else: 74 self._consume_entry(inline) 75 self._in_header = 0 76 77 inline = self.handle.readline() 78 self._lookahead = inline 79 self._in_header = 1 80 return self.table_record
81 82 if sys.version_info[0] < 3:
83 - def next(self):
84 """Python 2 style alias for Python 3 style __next__ method.""" 85 return self.__next__()
86
87 - def _consume_entry(self, inline):
88 current_entry = BlastTableEntry(inline) 89 self.table_record.add_entry(current_entry)
90
91 - def _consume_header(self, inline):
92 for keyword in reader_keywords: 93 if keyword in inline: 94 in_header = self._Parse('_parse_%s' % reader_keywords[keyword], inline) 95 break 96 return in_header
97
98 - def _parse_version(self, inline):
99 program, version, date = inline.split()[1:] 100 self.table_record.program = program 101 self.table_record.version = version 102 self.table_record.date = date 103 return 1
104
105 - def _parse_iteration(self, inline):
106 self.table_record.iteration = int(inline.split()[2]) 107 return 1
108
109 - def _parse_query(self, inline):
110 self.table_record.query = inline.split()[2:] 111 return 1
112
113 - def _parse_database(self, inline):
114 self.table_record.database = inline.split()[2] 115 return 1
116
117 - def _parse_fields(self, inline):
118 return 0
119
120 - def _Parse(self, method_name, inline):
121 return getattr(self, method_name)(inline)
122