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

Source Code for Module Bio.Blast.Record

  1  # Copyright 1999-2000 by Jeffrey Chang.  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  """Record classes to hold BLAST output. 
  7   
  8  Classes: 
  9  Blast              Holds all the information from a blast search. 
 10  PSIBlast           Holds all the information from a psi-blast search. 
 11   
 12  Header             Holds information from the header. 
 13  Description        Holds information about one hit description. 
 14  Alignment          Holds information about one alignment hit. 
 15  HSP                Holds information about one HSP. 
 16  MultipleAlignment  Holds information about a multiple alignment. 
 17  DatabaseReport     Holds information from the database report. 
 18  Parameters         Holds information from the parameters. 
 19   
 20  """ 
 21  # XXX finish printable BLAST output 
 22   
 23  from Bio.Seq import Seq 
 24  from Bio.SeqRecord import SeqRecord 
 25  from Bio.Align import MultipleSeqAlignment 
 26   
 27  __docformat__ = "restructuredtext en" 
 28   
 29   
30 -class Header(object):
31 """Saves information from a blast header. 32 33 Members: 34 application The name of the BLAST flavor that generated this data. 35 version Version of blast used. 36 date Date this data was generated. 37 reference Reference for blast. 38 39 query Name of query sequence. 40 query_letters Number of letters in the query sequence. (int) 41 42 database Name of the database. 43 database_sequences Number of sequences in the database. (int) 44 database_letters Number of letters in the database. (int) 45 46 """
47 - def __init__(self):
48 self.application = '' 49 self.version = '' 50 self.date = '' 51 self.reference = '' 52 53 self.query = '' 54 self.query_letters = None 55 56 self.database = '' 57 self.database_sequences = None 58 self.database_letters = None
59 60
61 -class Description(object):
62 """Stores information about one hit in the descriptions section. 63 64 Members: 65 title Title of the hit. 66 score Number of bits. (int) 67 bits Bit score. (float) 68 e E value. (float) 69 num_alignments Number of alignments for the same subject. (int) 70 """
71 - def __init__(self):
72 self.title = '' 73 self.score = None 74 self.bits = None 75 self.e = None 76 self.num_alignments = None
77
78 - def __str__(self):
79 return "%-66s %5s %s" % (self.title, self.score, self.e)
80 81
82 -class Alignment(object):
83 """Stores information about one hit in the alignments section. 84 85 Members: 86 title Name. 87 hit_id Hit identifier. (str) 88 hit_def Hit definition. (str) 89 length Length. (int) 90 hsps A list of HSP objects. 91 92 """
93 - def __init__(self):
94 self.title = '' 95 self.hit_id = '' 96 self.hit_def = '' 97 self.length = None 98 self.hsps = []
99
100 - def __str__(self):
101 lines = self.title.split('\n') 102 lines.append("Length = %s\n" % self.length) 103 return '\n '.join(lines)
104 105
106 -class HSP(object):
107 """Stores information about one hsp in an alignment hit. 108 109 Members: 110 - score BLAST score of hit. (float) 111 - bits Number of bits for that score. (float) 112 - expect Expect value. (float) 113 - num_alignments Number of alignments for same subject. (int) 114 - identities Number of identities (int) if using the XML parser. 115 Tuple of numer of identities/total aligned (int, int) 116 if using the (obsolete) plain text parser. 117 - positives Number of positives (int) if using the XML parser. 118 Tuple of numer of positives/total aligned (int, int) 119 if using the (obsolete) plain text parser. 120 - gaps Number of gaps (int) if using the XML parser. 121 Tuple of numer of gaps/total aligned (int, int) if 122 using the (obsolete) plain text parser. 123 - align_length Length of the alignment. (int) 124 - strand Tuple of (query, target) strand. 125 - frame Tuple of 1 or 2 frame shifts, depending on the flavor. 126 127 - query The query sequence. 128 - query_start The start residue for the query sequence. (1-based) 129 - query_end The end residue for the query sequence. (1-based) 130 - match The match sequence. 131 - sbjct The sbjct sequence. 132 - sbjct_start The start residue for the sbjct sequence. (1-based) 133 - sbjct_end The end residue for the sbjct sequence. (1-based) 134 135 Not all flavors of BLAST return values for every attribute:: 136 137 score expect identities positives strand frame 138 BLASTP X X X X 139 BLASTN X X X X X 140 BLASTX X X X X X 141 TBLASTN X X X X X 142 TBLASTX X X X X X/X 143 144 Note: for BLASTX, the query sequence is shown as a protein sequence, 145 but the numbering is based on the nucleotides. Thus, the numbering 146 is 3x larger than the number of amino acid residues. A similar effect 147 can be seen for the sbjct sequence in TBLASTN, and for both sequences 148 in TBLASTX. 149 150 Also, for negative frames, the sequence numbering starts from 151 query_start and counts down. 152 153 """
154 - def __init__(self):
155 self.score = None 156 self.bits = None 157 self.expect = None 158 self.num_alignments = None 159 self.identities = (None, None) 160 self.positives = (None, None) 161 self.gaps = (None, None) 162 self.align_length = None 163 self.strand = (None, None) 164 self.frame = () 165 166 self.query = '' 167 self.query_start = None 168 self.query_end = None 169 self.match = '' 170 self.sbjct = '' 171 self.sbjct_start = None 172 self.sbjct_end = None
173
174 - def __str__(self):
175 lines = ["Score %i (%i bits), expectation %0.1e, alignment length %i" 176 % (self.score, self.bits, self.expect, self.align_length)] 177 if self.align_length < 50: 178 lines.append("Query:%s %s %s" % (str(self.query_start).rjust(8), 179 str(self.query), 180 str(self.query_end))) 181 lines.append(" %s" 182 % (str(self.match))) 183 lines.append("Sbjct:%s %s %s" % (str(self.sbjct_start).rjust(8), 184 str(self.sbjct), 185 str(self.sbjct_end))) 186 else: 187 lines.append("Query:%s %s...%s %s" 188 % (str(self.query_start).rjust(8), 189 str(self.query)[:45], 190 str(self.query)[-3:], 191 str(self.query_end))) 192 lines.append(" %s...%s" 193 % (str(self.match)[:45], 194 str(self.match)[-3:])) 195 lines.append("Sbjct:%s %s...%s %s" 196 % (str(self.sbjct_start).rjust(8), 197 str(self.sbjct)[:45], 198 str(self.sbjct)[-3:], 199 str(self.sbjct_end))) 200 return "\n".join(lines)
201 202
203 -class MultipleAlignment(object):
204 """Holds information about a multiple alignment. 205 206 Members: 207 alignment A list of tuples (name, start residue, sequence, end residue). 208 209 The start residue is 1-based. It may be blank, if that sequence is 210 not aligned in the multiple alignment. 211 212 """
213 - def __init__(self):
214 self.alignment = []
215
216 - def to_generic(self, alphabet):
217 """Retrieve generic alignment object for the given alignment. 218 219 Instead of the tuples, this returns a MultipleSeqAlignment object 220 from Bio.Align, through which you can manipulate and query 221 the object. 222 223 alphabet is the specified alphabet for the sequences in the code (for 224 example IUPAC.IUPACProtein). 225 226 Thanks to James Casbon for the code. 227 """ 228 # TODO - Switch to new Bio.Align.MultipleSeqAlignment class? 229 seq_parts = [] 230 seq_names = [] 231 parse_number = 0 232 n = 0 233 for name, start, seq, end in self.alignment: 234 if name == 'QUERY': # QUERY is the first in each alignment block 235 parse_number += 1 236 n = 0 237 238 if parse_number == 1: # create on first_parse, append on all others 239 seq_parts.append(seq) 240 seq_names.append(name) 241 else: 242 seq_parts[n] += seq 243 n += 1 244 245 generic = MultipleSeqAlignment([], alphabet) 246 for (name, seq) in zip(seq_names, seq_parts): 247 generic.append(SeqRecord(Seq(seq, alphabet), name)) 248 249 return generic
250 251
252 -class Round(object):
253 """Holds information from a PSI-BLAST round. 254 255 Members: 256 number Round number. (int) 257 reused_seqs Sequences in model, found again. List of Description objects. 258 new_seqs Sequences not found, or below threshold. List of Description. 259 alignments A list of Alignment objects. 260 multiple_alignment A MultipleAlignment object. 261 """
262 - def __init__(self):
263 self.number = None 264 self.reused_seqs = [] 265 self.new_seqs = [] 266 self.alignments = [] 267 self.multiple_alignment = None
268 269
270 -class DatabaseReport(object):
271 """Holds information about a database report. 272 273 Members: 274 database_name List of database names. (can have multiple dbs) 275 num_letters_in_database Number of letters in the database. (int) 276 num_sequences_in_database List of number of sequences in the database. 277 posted_date List of the dates the databases were posted. 278 ka_params A tuple of (lambda, k, h) values. (floats) 279 gapped # XXX this isn't set right! 280 ka_params_gap A tuple of (lambda, k, h) values. (floats) 281 282 """
283 - def __init__(self):
284 self.database_name = [] 285 self.posted_date = [] 286 self.num_letters_in_database = [] 287 self.num_sequences_in_database = [] 288 self.ka_params = (None, None, None) 289 self.gapped = 0 290 self.ka_params_gap = (None, None, None)
291 292
293 -class Parameters(object):
294 """Holds information about the parameters. 295 296 Members: 297 matrix Name of the matrix. 298 gap_penalties Tuple of (open, extend) penalties. (floats) 299 sc_match Match score for nucleotide-nucleotide comparison 300 sc_mismatch Mismatch penalty for nucleotide-nucleotide comparison 301 num_hits Number of hits to the database. (int) 302 num_sequences Number of sequences. (int) 303 num_good_extends Number of extensions. (int) 304 num_seqs_better_e Number of sequences better than e-value. (int) 305 hsps_no_gap Number of HSP's better, without gapping. (int) 306 hsps_prelim_gapped Number of HSP's gapped in prelim test. (int) 307 hsps_prelim_gapped_attemped Number of HSP's attempted in prelim. (int) 308 hsps_gapped Total number of HSP's gapped. (int) 309 query_length Length of the query. (int) 310 query_id Identifier of the query sequence. (str) 311 database_length Number of letters in the database. (int) 312 effective_hsp_length Effective HSP length. (int) 313 effective_query_length Effective length of query. (int) 314 effective_database_length Effective length of database. (int) 315 effective_search_space Effective search space. (int) 316 effective_search_space_used Effective search space used. (int) 317 frameshift Frameshift window. Tuple of (int, float) 318 threshold Threshold. (int) 319 window_size Window size. (int) 320 dropoff_1st_pass Tuple of (score, bits). (int, float) 321 gap_x_dropoff Tuple of (score, bits). (int, float) 322 gap_x_dropoff_final Tuple of (score, bits). (int, float) 323 gap_trigger Tuple of (score, bits). (int, float) 324 blast_cutoff Tuple of (score, bits). (int, float) 325 """
326 - def __init__(self):
327 self.matrix = '' 328 self.gap_penalties = (None, None) 329 self.sc_match = None 330 self.sc_mismatch = None 331 self.num_hits = None 332 self.num_sequences = None 333 self.num_good_extends = None 334 self.num_seqs_better_e = None 335 self.hsps_no_gap = None 336 self.hsps_prelim_gapped = None 337 self.hsps_prelim_gapped_attemped = None 338 self.hsps_gapped = None 339 self.query_id = None 340 self.query_length = None 341 self.database_length = None 342 self.effective_hsp_length = None 343 self.effective_query_length = None 344 self.effective_database_length = None 345 self.effective_search_space = None 346 self.effective_search_space_used = None 347 self.frameshift = (None, None) 348 self.threshold = None 349 self.window_size = None 350 self.dropoff_1st_pass = (None, None) 351 self.gap_x_dropoff = (None, None) 352 self.gap_x_dropoff_final = (None, None) 353 self.gap_trigger = (None, None) 354 self.blast_cutoff = (None, None)
355 356 357 # TODO - Add a friendly __str__ method to BLAST results
358 -class Blast(Header, DatabaseReport, Parameters):
359 """Saves the results from a blast search. 360 361 Members: 362 descriptions A list of Description objects. 363 alignments A list of Alignment objects. 364 multiple_alignment A MultipleAlignment object. 365 + members inherited from base classes 366 367 """
368 - def __init__(self):
369 Header.__init__(self) 370 DatabaseReport.__init__(self) 371 Parameters.__init__(self) 372 self.descriptions = [] 373 self.alignments = [] 374 self.multiple_alignment = None
375 376
377 -class PSIBlast(Header, DatabaseReport, Parameters):
378 """Saves the results from a blastpgp search. 379 380 Members: 381 rounds A list of Round objects. 382 converged Whether the search converged. 383 + members inherited from base classes 384 385 """
386 - def __init__(self):
387 Header.__init__(self) 388 DatabaseReport.__init__(self) 389 Parameters.__init__(self) 390 self.rounds = [] 391 self.converged = 0
392