| 1 | package Boulder::Blast; | ||
| 2 | # WUBLAST/NCBI BLAST file format parsing | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | Boulder::Blast - Parse and read BLAST files | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | use Boulder::Blast; | ||
| 11 | |||
| 12 | # parse from a single file | ||
| 13 | $blast = Boulder::Blast->parse('run3.blast'); | ||
| 14 | |||
| 15 | # parse and read a set of blast output files | ||
| 16 | $stream = Boulder::Blast->new('run3.blast','run4.blast'); | ||
| 17 | while ($blast = $stream->get) { | ||
| 18 | # do something with $blast object | ||
| 19 | } | ||
| 20 | |||
| 21 | # parse and read a whole directory of blast runs | ||
| 22 | $stream = Boulder::Blast->new(<*.blast>); | ||
| 23 | while ($blast = $stream->get) { | ||
| 24 | # do something with $blast object | ||
| 25 | } | ||
| 26 | |||
| 27 | # parse and read from STDIN | ||
| 28 | $stream = Boulder::Blast->new; | ||
| 29 | while ($blast = $stream->get) { | ||
| 30 | # do something with $blast object | ||
| 31 | } | ||
| 32 | |||
| 33 | # parse and read as a filehandle | ||
| 34 | $stream = Boulder::Blast->newFh(<*.blast>); | ||
| 35 | while ($blast = <$stream>) { | ||
| 36 | # do something with $blast object | ||
| 37 | } | ||
| 38 | |||
| 39 | # once you have a $blast object, you can get info about it: | ||
| 40 | $query = $blast->Blast_query; | ||
| 41 | @hits = $blast->Blast_hits; | ||
| 42 | foreach $hit (@hits) { | ||
| 43 | $hit_sequence = $hit->Name; # get the ID | ||
| 44 | $significance = $hit->Signif; # get the significance | ||
| 45 | @hsps = $hit->Hsps; # list of HSPs | ||
| 46 | foreach $hsp (@hsps) { | ||
| 47 | $query = $hsp->Query; # query sequence | ||
| 48 | $subject = $hsp->Subject; # subject sequence | ||
| 49 | $signif = $hsp->Signif; # significance of HSP | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | The I<Boulder::Blast> class parses the output of the B<Washington | ||
| 57 | University (WU)> or National Cenber for Biotechnology Information | ||
| 58 | (NCBI) series of BLAST programs and turns them into I<Stone> records. | ||
| 59 | You may then use the standard Stone access methods to retrieve | ||
| 60 | information about the BLAST run, or add the information to a Boulder | ||
| 61 | stream. | ||
| 62 | |||
| 63 | The parser works equally well on the contents of a static file, or on | ||
| 64 | information read dynamically from a filehandle or pipe. | ||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | =head2 parse() Method | ||
| 69 | |||
| 70 | $stone = Boulder::Blast->parse($file_path); | ||
| 71 | $stone = Boulder::Blast->parse($filehandle); | ||
| 72 | |||
| 73 | The I<parse()> method accepts a path to a file or a filehandle, parses | ||
| 74 | its contents, and returns a Boulder Stone object. The file path may | ||
| 75 | be absolute or relative to the current directgly. The filehandle may | ||
| 76 | be specified as an IO::File object, a FileHandle object, or a | ||
| 77 | reference to a glob (C<\*FILEHANDLE> notation). If you call | ||
| 78 | I<parse()> without any arguments, it will try to parse the contents of | ||
| 79 | standard input. | ||
| 80 | |||
| 81 | =head2 new() Method | ||
| 82 | |||
| 83 | $stream = Boulder::Blast->new; | ||
| 84 | $stream = Boulder::Blast->new($file [,@more_files]); | ||
| 85 | $stream = Boulder::Blast->new(\*FILEHANDLE); | ||
| 86 | |||
| 87 | If you wish, you may create the parser first with I<Boulder::Blast> | ||
| 88 | I<new()>, and then invoke the parser object's I<parse()> method as | ||
| 89 | many times as you wish to, producing a Stone object each time. | ||
| 90 | |||
| 91 | =head1 TAGS | ||
| 92 | |||
| 93 | The following tags are defined in the parsed Blast Stone object: | ||
| 94 | |||
| 95 | =head2 Information about the program | ||
| 96 | |||
| 97 | These top-level tags provide information about the version of the | ||
| 98 | BLAST program itself. | ||
| 99 | |||
| 100 | =over 4 | ||
| 101 | |||
| 102 | =item Blast_program | ||
| 103 | |||
| 104 | The name of the algorithm used to run the analysis. Possible values | ||
| 105 | include: | ||
| 106 | |||
| 107 | blastn | ||
| 108 | blastp | ||
| 109 | blastx | ||
| 110 | tblastn | ||
| 111 | tblastx | ||
| 112 | fasta3 | ||
| 113 | fastx3 | ||
| 114 | fasty3 | ||
| 115 | tfasta3 | ||
| 116 | tfastx3 | ||
| 117 | tfasty3 | ||
| 118 | |||
| 119 | =item Blast_version | ||
| 120 | |||
| 121 | This gives the version of the program in whatever form appears | ||
| 122 | on the banner page, e.g. "2.0a19-WashU". | ||
| 123 | |||
| 124 | =item Blast_program_date | ||
| 125 | |||
| 126 | This gives the date at which the program was compiled, if and | ||
| 127 | only if it appears on the banner page. | ||
| 128 | |||
| 129 | =back | ||
| 130 | |||
| 131 | =head2 Information about the run | ||
| 132 | |||
| 133 | These top-level tags give information about the particular run, such | ||
| 134 | as the parameters that were used for the algorithm. | ||
| 135 | |||
| 136 | =over 4 | ||
| 137 | |||
| 138 | =item Blast_run_date | ||
| 139 | |||
| 140 | This gives the date and time at which the similarity analysis | ||
| 141 | was run, in the format "Fri Jul 6 09:32:36 1998" | ||
| 142 | |||
| 143 | =item Blast_parms | ||
| 144 | |||
| 145 | This points to a subrecord containing information about the | ||
| 146 | algorithm's runtime parameters. The following subtags are | ||
| 147 | used. Others may be added in the future: | ||
| 148 | |||
| 149 | Hspmax the value of the -hspmax argument | ||
| 150 | Expectation the value of E | ||
| 151 | Matrix the matrix in use, e.g. BLOSUM62 | ||
| 152 | Ctxfactor the value of the -ctxfactor argument | ||
| 153 | Gapall The value of the -gapall argument | ||
| 154 | |||
| 155 | =back | ||
| 156 | |||
| 157 | =head2 Information about the query sequence and subject database | ||
| 158 | |||
| 159 | Thse top-level tags give information about the query sequence and the | ||
| 160 | database that was searched on. | ||
| 161 | |||
| 162 | =over 4 | ||
| 163 | |||
| 164 | =item Blast_query | ||
| 165 | |||
| 166 | The identifier for the search sequence, as defined by the | ||
| 167 | FASTA format. This will be the first set of non-whitespace | ||
| 168 | characters following the ">" character. In other words, the search | ||
| 169 | sequence "name". | ||
| 170 | |||
| 171 | =item Blast_query_length | ||
| 172 | |||
| 173 | The length of the query sequence, in base pairs. | ||
| 174 | |||
| 175 | =item Blast_db | ||
| 176 | |||
| 177 | The Unix filesystem path to the subject database. | ||
| 178 | |||
| 179 | =item Blast_db_title | ||
| 180 | |||
| 181 | The title of the subject database. | ||
| 182 | |||
| 183 | =back | ||
| 184 | |||
| 185 | =head2 The search results: the I<Blast_hits> tag. | ||
| 186 | |||
| 187 | Each BLAST hit is represented by the tag I<Blast_hits>. There may be | ||
| 188 | zero, one, or many such tags. They will be presented in reverse | ||
| 189 | sorted order of significance, i.e. most significant hit first. | ||
| 190 | |||
| 191 | Each I<Blast_hits> tag is a Stone subrecord containing the following | ||
| 192 | subtags: | ||
| 193 | |||
| 194 | =over 4 | ||
| 195 | |||
| 196 | =item Name | ||
| 197 | |||
| 198 | The name/identifier of the sequence that was hit. | ||
| 199 | |||
| 200 | =item Length | ||
| 201 | |||
| 202 | The total length of the sequence that was hit | ||
| 203 | |||
| 204 | =item Signif | ||
| 205 | |||
| 206 | The significance of the hit. If there are multiple HSPs in the hit, | ||
| 207 | this will be the most significant (smallest) value. | ||
| 208 | |||
| 209 | =item Identity | ||
| 210 | |||
| 211 | The percent identity of the hit. If there are multiple HSPs, this | ||
| 212 | will be the one with the highest percent identity. | ||
| 213 | |||
| 214 | =item Expect | ||
| 215 | |||
| 216 | The expectation value for the hit. If there are multiple HSPs, this | ||
| 217 | will be the lowest expectation value in the set. | ||
| 218 | |||
| 219 | =item Hsps | ||
| 220 | |||
| 221 | One or more sub-sub-tags, pointing to a nested record containing | ||
| 222 | information about each high-scoring segment pair (HSP). See the next | ||
| 223 | section for details. | ||
| 224 | |||
| 225 | =back | ||
| 226 | |||
| 227 | =head2 The Hsp records: the I<Hsps> tag | ||
| 228 | |||
| 229 | Each I<Blast_hit> tag will have at least one, and possibly several | ||
| 230 | I<Hsps> tags, each one corresponding to a high-scoring segment pair | ||
| 231 | (HSP). These records contain detailed information about the hit, | ||
| 232 | including the alignments. Tags are as follows: | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | =item Signif | ||
| 237 | |||
| 238 | The significance (P value) of this HSP. | ||
| 239 | |||
| 240 | =item Bits | ||
| 241 | |||
| 242 | The number of bits of significance. | ||
| 243 | |||
| 244 | =item Expect | ||
| 245 | |||
| 246 | Expectation value for this HSP. | ||
| 247 | |||
| 248 | =item Identity | ||
| 249 | |||
| 250 | Percent identity. | ||
| 251 | |||
| 252 | =item Positives | ||
| 253 | |||
| 254 | Percent positive matches. | ||
| 255 | |||
| 256 | =item Score | ||
| 257 | |||
| 258 | The Smith-Waterman alignment score. | ||
| 259 | |||
| 260 | =item Orientation | ||
| 261 | |||
| 262 | The word "plus" or "minus". This tag is only present for nucleotide | ||
| 263 | searches, when the reverse complement match may be present. | ||
| 264 | |||
| 265 | =item Strand | ||
| 266 | |||
| 267 | Depending on algorithm used, indicates complementarity of match and | ||
| 268 | possibly the reading frame. This is copied out of the blast report. | ||
| 269 | Possibilities include: | ||
| 270 | |||
| 271 | "Plus / Minus" "Plus / Plus" -- blastn algorithm | ||
| 272 | "+1 / -2" "+2 / -2" -- blastx, tblastx | ||
| 273 | |||
| 274 | =item Query_start | ||
| 275 | |||
| 276 | Position at which the HSP starts in the query sequence (1-based | ||
| 277 | indexing). | ||
| 278 | |||
| 279 | =item Query_end | ||
| 280 | |||
| 281 | Position at which the HSP stops in the query sequence. | ||
| 282 | |||
| 283 | =item Subject_start | ||
| 284 | |||
| 285 | Position at which the HSP starts in the subject (target) sequence. | ||
| 286 | |||
| 287 | =item Subject_end | ||
| 288 | |||
| 289 | Position at which the HSP stops in the subject (target) sequence. | ||
| 290 | |||
| 291 | =item Query, Subject, Alignment | ||
| 292 | |||
| 293 | These three tags contain strings which, together, create the gapped | ||
| 294 | alignment of the query sequence with the subject sequence. | ||
| 295 | |||
| 296 | For example, to print the alignment of the first HSP of the first | ||
| 297 | match, you might say: | ||
| 298 | |||
| 299 | $hsp = $blast->Blast_hits->Hsps; | ||
| 300 | print join("\n",$hsp->Query,$hsp->Alignment,$hsp->Subject),"\n"; | ||
| 301 | |||
| 302 | =back | ||
| 303 | |||
| 304 | See the bottom of this manual page for an example BLAST run. | ||
| 305 | |||
| 306 | =head1 CAVEATS | ||
| 307 | |||
| 308 | This module has been extensively tested with WUBLAST, but very little | ||
| 309 | with NCBI BLAST. It probably will not work with PSI Blast or other | ||
| 310 | variants. | ||
| 311 | |||
| 312 | The author plans to adapt this module to parse other formats, as well | ||
| 313 | as non-BLAST formats such as the output of Fastn. | ||
| 314 | |||
| 315 | =head1 SEE ALSO | ||
| 316 | |||
| 317 | L<Boulder>, L<Boulder::GenBank> | ||
| 318 | |||
| 319 | =head1 AUTHOR | ||
| 320 | |||
| 321 | Lincoln Stein <lstein@cshl.org>. | ||
| 322 | |||
| 323 | Copyright (c) 1998-1999 Cold Spring Harbor Laboratory | ||
| 324 | |||
| 325 | This library is free software; you can redistribute it and/or modify | ||
| 326 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 327 | disclaimers of warranty. | ||
| 328 | |||
| 329 | =head1 EXAMPLE BLASTN RUN | ||
| 330 | |||
| 331 | This output was generated by the I<quickblast.pl> program, which is | ||
| 332 | located in the F<eg/> subdirectory of the I<Boulder> distribution | ||
| 333 | directory. It is a typical I<blastn> (nucleotide->nucleotide) run; | ||
| 334 | however long lines (usually DNA sequences) have been truncated. Also | ||
| 335 | note that per the Boulder protocol, the percent sign (%) is escaped in | ||
| 336 | the usual way. It will be unescaped when reading the stream | ||
| 337 | back in. | ||
| 338 | |||
| 339 | Blast_run_date=Fri Nov 6 14:40:41 1998 | ||
| 340 | Blast_db_date=2:40 PM EST Nov 6, 1998 | ||
| 341 | Blast_parms={ | ||
| 342 | Hspmax=10 | ||
| 343 | Expectation=10 | ||
| 344 | Matrix=+5,-4 | ||
| 345 | Ctxfactor=2.00 | ||
| 346 | } | ||
| 347 | Blast_program_date=05-Feb-1998 | ||
| 348 | Blast_db= /usr/tmp/quickblast18202aaaa | ||
| 349 | Blast_version=2.0a19-WashU | ||
| 350 | Blast_query=BCD207R | ||
| 351 | Blast_db_title= test.fasta | ||
| 352 | Blast_query_length=332 | ||
| 353 | Blast_program=blastn | ||
| 354 | Blast_hits={ | ||
| 355 | Signif=3.5e-74 | ||
| 356 | Expect=3.5e-74, | ||
| 357 | Name=BCD207R | ||
| 358 | Identity=100%25 | ||
| 359 | Length=332 | ||
| 360 | Hsps={ | ||
| 361 | Subject=GTGCTTTCAAACATTGATGGATTCCTCCCCTTGACATATATATATACTTTGGGTTCCCGCAA... | ||
| 362 | Signif=3.5e-74 | ||
| 363 | Length=332 | ||
| 364 | Bits=249.1 | ||
| 365 | Query_start=1 | ||
| 366 | Subject_end=332 | ||
| 367 | Query=GTGCTTTCAAACATTGATGGATTCCTCCCCTTGACATATATATATACTTTGGGTTCCCGCAA... | ||
| 368 | Positives=100%25 | ||
| 369 | Expect=3.5e-74, | ||
| 370 | Identity=100%25 | ||
| 371 | Query_end=332 | ||
| 372 | Orientation=plus | ||
| 373 | Score=1660 | ||
| 374 | Strand=Plus / Plus | ||
| 375 | Subject_start=1 | ||
| 376 | Alignment=||||||||||||||||||||||||||||||||||||||||||||||||||||||||||... | ||
| 377 | } | ||
| 378 | } | ||
| 379 | = | ||
| 380 | |||
| 381 | =head1 Example BLASTP run | ||
| 382 | |||
| 383 | Here is the output from a typical I<blastp> (protein->protein) run. | ||
| 384 | Long lines have again been truncated. | ||
| 385 | |||
| 386 | Blast_run_date=Fri Nov 6 14:37:23 1998 | ||
| 387 | Blast_db_date=2:36 PM EST Nov 6, 1998 | ||
| 388 | Blast_parms={ | ||
| 389 | Hspmax=10 | ||
| 390 | Expectation=10 | ||
| 391 | Matrix=BLOSUM62 | ||
| 392 | Ctxfactor=1.00 | ||
| 393 | } | ||
| 394 | Blast_program_date=05-Feb-1998 | ||
| 395 | Blast_db= /usr/tmp/quickblast18141aaaa | ||
| 396 | Blast_version=2.0a19-WashU | ||
| 397 | Blast_query=YAL004W | ||
| 398 | Blast_db_title= elegans.fasta | ||
| 399 | Blast_query_length=216 | ||
| 400 | Blast_program=blastp | ||
| 401 | Blast_hits={ | ||
| 402 | Signif=0.95 | ||
| 403 | Expect=3.0, | ||
| 404 | Name=C28H8.2 | ||
| 405 | Identity=30%25 | ||
| 406 | Length=51 | ||
| 407 | Hsps={ | ||
| 408 | Subject=HMTVEFHVTSQSW---FGFEDHFHMIIR-AVNDENVGWGVRYLSMAF | ||
| 409 | Signif=0.95 | ||
| 410 | Length=46 | ||
| 411 | Bits=15.8 | ||
| 412 | Query_start=100 | ||
| 413 | Subject_end=49 | ||
| 414 | Query=HLTQD-HGGDLFWGKVLGFTLKFNLNLRLTVNIDQLEWEVLHVSLHF | ||
| 415 | Positives=52%25 | ||
| 416 | Expect=3.0, | ||
| 417 | Identity=30%25 | ||
| 418 | Query_end=145 | ||
| 419 | Orientation=plus | ||
| 420 | Score=45 | ||
| 421 | Subject_start=7 | ||
| 422 | Alignment=H+T + H W GF F++ +R VN + + W V ++S+ F | ||
| 423 | } | ||
| 424 | } | ||
| 425 | Blast_hits={ | ||
| 426 | Signif=0.99 | ||
| 427 | Expect=4.7, | ||
| 428 | Name=ZK896.2 | ||
| 429 | Identity=24%25 | ||
| 430 | Length=340 | ||
| 431 | Hsps={ | ||
| 432 | Subject=FSGKFTTFVLNKDQATLRMSSAEKTAEWNTAFDSRRGFF----TSGNYGL... | ||
| 433 | Signif=0.99 | ||
| 434 | Length=101 | ||
| 435 | Bits=22.9 | ||
| 436 | Query_start=110 | ||
| 437 | Subject_end=243 | ||
| 438 | Query=FWGKVLGFTL-KFNLNLRLTVNIDQLEWEVLHVSLHFWVVEVSTDQTLSVE... | ||
| 439 | Positives=41%25 | ||
| 440 | Expect=4.7, | ||
| 441 | Identity=24%25 | ||
| 442 | Query_end=210 | ||
| 443 | Orientation=plus | ||
| 444 | Score=65 | ||
| 445 | Subject_start=146 | ||
| 446 | Alignment=F GK F L K LR++ EW S + T +... | ||
| 447 | } | ||
| 448 | } | ||
| 449 | = | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | use strict; | ||
| 454 | use Stone; | ||
| 455 | use Boulder::Stream; | ||
| 456 | use Carp; | ||
| 457 | use vars qw($VERSION @ISA); | ||
| 458 | @ISA = 'Boulder::Stream'; | ||
| 459 | |||
| 460 | *get = \&read_record; | ||
| 461 | |||
| 462 | $VERSION = 1.01; | ||
| 463 | |||
| 464 | sub new { | ||
| 465 | my $self = shift; | ||
| 466 | $self = bless {},$self unless ref $self; | ||
| 467 | $self->_open(@_); | ||
| 468 | return $self; | ||
| 469 | } | ||
| 470 | |||
| 471 | # parse the contents of filehandle and emit a boulderio stream to stdout | ||
| 472 | sub parse { | ||
| 473 | my $self = shift; | ||
| 474 | $self = $self->new(shift) unless ref($self); | ||
| 475 | $self->read_record(); | ||
| 476 | } | ||
| 477 | |||
| 478 | sub _fh { | ||
| 479 | my $self = shift; | ||
| 480 | $self->{'fh'} = $_[0] if defined($_[0]); | ||
| 481 | return $self->{'fh'}; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub read_record { | ||
| 485 | my $self = shift; | ||
| 486 | return if $self->done; | ||
| 487 | my $fh = $self->_fh; | ||
| 488 | my $stone = new Stone; | ||
| 489 | local $/ = "\n"; # normalize input stream | ||
| 490 | |||
| 491 | return unless defined(my $line = <$fh>); | ||
| 492 | croak "Doesn't look like a BLAST stream to me - top line = '$_'" unless $line=~/BLAST/; | ||
| 493 | return unless my ($program,$version,$date) = $line=~ /^(\S+) (\S+) \[([^\]]+)\]/; | ||
| 494 | |||
| 495 | $stone->insert ( Blast_version => $version, | ||
| 496 | Blast_program => lc $program, | ||
| 497 | Blast_program_date => $date ); | ||
| 498 | |||
| 499 | # the date isn't part of the file, so we use the creation date of the file | ||
| 500 | # for this purpose. If not available, then we are reading from a pipe | ||
| 501 | # (maybe) and we use the current time. | ||
| 502 | my $timestamp = -f $fh ? (stat(_))[9] : time; | ||
| 503 | $stone->insert(Blast_run_date => scalar localtime($timestamp)); | ||
| 504 | |||
| 505 | if ($version =~ /WashU/) { | ||
| 506 | require Boulder::Blast::WU; | ||
| 507 | bless $self,'Boulder::Blast::WU'; | ||
| 508 | } else { | ||
| 509 | require Boulder::Blast::NCBI; | ||
| 510 | bless $self,'Boulder::Blast::NCBI'; | ||
| 511 | } | ||
| 512 | |||
| 513 | $self->_read_record($fh,$stone); | ||
| 514 | } | ||
| 515 | |||
| 516 | sub _read_record { | ||
| 517 | croak "unimplemented"; | ||
| 518 | } | ||
| 519 | |||
| 520 | sub _open { | ||
| 521 | my $self = shift; | ||
| 522 | if (@_ > 1) { | ||
| 523 | push @ARGV,@_; | ||
| 524 | $self->_fh(\*ARGV); | ||
| 525 | return; | ||
| 526 | } | ||
| 527 | my $fh = shift; | ||
| 528 | unless (defined $fh) { | ||
| 529 | # if $fh is null, then set it to ARGV | ||
| 530 | $fh ||= \*ARGV; | ||
| 531 | } elsif (!UNIVERSAL::isa($fh,'GLOB') && !UNIVERSAL::isa($fh,'FileHandle')) { | ||
| 532 | # if $fh isn't a filehandle, then treat it as a filename to open | ||
| 533 | croak "File does not exist" unless -e (my $name = $fh); | ||
| 534 | $fh = Symbol::gensym; | ||
| 535 | open($fh,$name) or croak "Can't open $name: $!\n"; | ||
| 536 | } | ||
| 537 | $self->_fh($fh); | ||
| 538 | } | ||
| 539 | |||
| 540 | 1; | ||
| 541 |