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