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