| 1 | package Boulder::Blast::WU; | ||
| 2 | |||
| 3 | # WUBLAST file format parsing | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Boulder::Blast::WU - Parse and read WU-BLAST files | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | Not for direct use. Use Boulder::Blast instead. | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Specialized parser for WUBLAST format BLAST output. Loaded | ||
| 16 | automatically by Boulder::Blast. | ||
| 17 | |||
| 18 | =head1 SEE ALSO | ||
| 19 | |||
| 20 | L<Boulder>, L<Boulder::GenBank>, L<Boulder::Blast> | ||
| 21 | |||
| 22 | =head1 AUTHOR | ||
| 23 | |||
| 24 | Lincoln Stein <lstein@cshl.org>. | ||
| 25 | |||
| 26 | Copyright (c) 1998 Cold Spring Harbor Laboratory | ||
| 27 | |||
| 28 | This library is free software; you can redistribute it and/or modify | ||
| 29 | it under the same terms as Perl itself. See DISCLAIMER.txt for | ||
| 30 | disclaimers of warranty. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use Stone; | ||
| 36 | use Boulder::Stream; | ||
| 37 | use Carp; | ||
| 38 | use vars qw($VERSION @ISA); | ||
| 39 | @ISA = 'Boulder::Blast'; | ||
| 40 | |||
| 41 | $VERSION = 1.00; | ||
| 42 | |||
| 43 | sub _read_record { | ||
| 44 | my $self = shift; | ||
| 45 | my $fh = shift; | ||
| 46 | my $stone = shift; | ||
| 47 | |||
| 48 | # we don't find out about the name of the database or the parameters until we | ||
| 49 | # get to the bottom of the file. Too bad. | ||
| 50 | |||
| 51 | # loop until we find the query name | ||
| 52 | my $line; | ||
| 53 | do { | ||
| 54 | $line = <$fh>; | ||
| 55 | } until $line=~/^Query=\s+(\S+)/; | ||
| 56 | |||
| 57 | croak "Couldn't find query line!" unless $1; | ||
| 58 | $stone->insert(Blast_query => $1); | ||
| 59 | |||
| 60 | do { | ||
| 61 | $line = <$fh>; | ||
| 62 | } until $line=~/([\d,]+) letters/; | ||
| 63 | |||
| 64 | croak "Couldn't find query length!" unless $1; | ||
| 65 | (my $len = $1) =~ s/,//g; | ||
| 66 | $stone->insert(Blast_query_length => $len); | ||
| 67 | |||
| 68 | # Read down to the first hit, if any. If we hit /^Parameters/, then we had no | ||
| 69 | # hits. | ||
| 70 | while (<$fh> ) { | ||
| 71 | last if /^(>|Parameters)/; | ||
| 72 | } | ||
| 73 | |||
| 74 | if (/^>/) { # we found some hits | ||
| 75 | my $hits = $self->parse_hits($_); | ||
| 76 | $stone->insert(Blast_hits => $hits); | ||
| 77 | } | ||
| 78 | |||
| 79 | # At this point, one way or another, we're pointing at the Parameters | ||
| 80 | # line. We create a parameter stone to hold the results | ||
| 81 | my $parms = new Stone; | ||
| 82 | while (<$fh>) { | ||
| 83 | chomp; | ||
| 84 | last if /^Statistics/; | ||
| 85 | $parms->insert(Ctxfactor => $1) if /ctxfactor=(\S+)/; | ||
| 86 | $parms->insert(Gapall=>'yes') if /gapall/; | ||
| 87 | $parms->insert(Hspmax=>$1) if /hspmax=(\S+)/; | ||
| 88 | $parms->insert(Expectation=>$1) if /E=(\S+)/; | ||
| 89 | $parms->insert(Matrix=>$1) if /^\s+\+[0-3]\s+0\s+(\S+)/ && !$parms->get('Matrix'); # borscht | ||
| 90 | } | ||
| 91 | $stone->insert(Blast_parms => $parms); | ||
| 92 | |||
| 93 | # Now we should be pointing at statistics | ||
| 94 | while (<$fh>) { | ||
| 95 | $stone->insert(Blast_db => $1) if /Database: (.*)/; | ||
| 96 | $stone->insert(Blast_db_title => $1) if /Title: (.*)/; | ||
| 97 | $stone->insert(Blast_db_date => $1) if /Posted date:\s+(.*)/; | ||
| 98 | last if /End:/; | ||
| 99 | } | ||
| 100 | |||
| 101 | # finally done! | ||
| 102 | $stone; | ||
| 103 | } | ||
| 104 | |||
| 105 | # parse the hits and HSPs | ||
| 106 | sub parse_hits { | ||
| 107 | my $self = shift; | ||
| 108 | $_ = shift; | ||
| 109 | my $fh = $self->_fh; | ||
| 110 | my (@hits,@hsps,$accession,$orientation,$hit,$hsp); | ||
| 111 | my ($qstart,$qend,$tstart,$tend); | ||
| 112 | my ($query,$match,$target,$done,$new_hit,$new_hsp); | ||
| 113 | my $signif = 9999; | ||
| 114 | my $expect = 9999; | ||
| 115 | my $ident = 0; | ||
| 116 | |||
| 117 | while (!$done) { | ||
| 118 | chomp; | ||
| 119 | next unless length($_); | ||
| 120 | |||
| 121 | $done = /^Parameters/; # here's how we get out of the loop | ||
| 122 | $new_hit = /^>(\S+)/; | ||
| 123 | $new_hsp = $accession && /Score = (\d+) \((\S+) bits\)/; | ||
| 124 | |||
| 125 | # hit a new HSP section | ||
| 126 | if ( $done || $new_hit || $new_hsp ) { | ||
| 127 | if ($hsp) { | ||
| 128 | croak "base alignment is out of whack" | ||
| 129 | unless length($query) == length($target); | ||
| 130 | $hsp->insert(Query => $query, | ||
| 131 | Subject => $target, | ||
| 132 | Alignment => substr($match,0,length($query)), | ||
| 133 | ); | ||
| 134 | $hsp->insert(Query_start => $qstart, | ||
| 135 | Query_end => $qend, | ||
| 136 | Subject_start => $tstart, | ||
| 137 | Subject_end => $tend, | ||
| 138 | Length => 1 + $qend - $qstart, | ||
| 139 | Orientation => $tend > $tstart ? 'plus' : 'minus', | ||
| 140 | ); | ||
| 141 | push(@hsps,$hsp); | ||
| 142 | undef $hsp; | ||
| 143 | } | ||
| 144 | if ($new_hsp) { | ||
| 145 | $hsp = new Stone; | ||
| 146 | $hsp->insert(Score => $1, Bits => $2); | ||
| 147 | ($qstart,$qend,$tstart,$tend) = (undef,undef,undef,undef); # undef all | ||
| 148 | ($query,$match,$target) = (undef,undef,undef); # these too | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | # hit a new subject section | ||
| 153 | if ( $done || $new_hit ) { | ||
| 154 | $accession = $1; | ||
| 155 | if ($hit) { | ||
| 156 | $hit->insert(Hsps => \@hsps, | ||
| 157 | Signif => $signif, | ||
| 158 | Identity => $ident, | ||
| 159 | Expect => $expect, | ||
| 160 | ) if @hsps; | ||
| 161 | undef @hsps; | ||
| 162 | push(@hits,$hit); | ||
| 163 | ($signif,$expect,$ident) = (9999,9999,0); # reset max values | ||
| 164 | } | ||
| 165 | if ($new_hit) { | ||
| 166 | $hit = new Stone; | ||
| 167 | $hit->insert(Name => $accession); | ||
| 168 | next; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # hit the length = line | ||
| 173 | if (/Length\s*=\s*([\d,]+)/) { | ||
| 174 | (my $len = $1) =~ s/,//g; | ||
| 175 | $hit->insert(Length => $len); | ||
| 176 | next; | ||
| 177 | } | ||
| 178 | |||
| 179 | # hit the Plus|Minus Strand line | ||
| 180 | if (/(Plus|Minus) Strand HSPs/) { | ||
| 181 | $orientation = lc $1; | ||
| 182 | next; | ||
| 183 | } | ||
| 184 | |||
| 185 | # None of the following is relevant unless $hsp is defined | ||
| 186 | next unless $hsp; | ||
| 187 | |||
| 188 | if (/Expect = ([+e\d\.-]+)/) { | ||
| 189 | $hsp->insert(Expect => $1); | ||
| 190 | $expect = $1 < $expect ? $1 : $expect; | ||
| 191 | } | ||
| 192 | |||
| 193 | if (/P(?:\(\d+\))? = (\S+)/) { | ||
| 194 | $hsp->insert(Signif => $1); | ||
| 195 | $signif = $1 < $signif ? $1 : $signif; | ||
| 196 | } | ||
| 197 | |||
| 198 | if (/Identities = \S+ \((\d+%?)\)/) { | ||
| 199 | my $idn = $1; | ||
| 200 | $hsp->insert(Identity => $idn); | ||
| 201 | $ident = $idn > $ident ? $idn : $ident; | ||
| 202 | } | ||
| 203 | |||
| 204 | $hsp->insert(Positives => $1) if /Positives = \S+ \((\S+)\)/; | ||
| 205 | $hsp->insert(Strand => $1) if /Strand =\s+([^,]+)/; | ||
| 206 | $hsp->insert(Frame => $1) if /Frame =\s+([^,]+)/; | ||
| 207 | |||
| 208 | # process the query sequence | ||
| 209 | if (/^Query:\s+(\d+)\s+(\S+)\s+(\d+)/) { | ||
| 210 | $qstart ||= $1; | ||
| 211 | $qend = $3; | ||
| 212 | $query .= $2; | ||
| 213 | next; | ||
| 214 | } | ||
| 215 | |||
| 216 | # process the target sequence | ||
| 217 | if (/^Sbjct:\s+(\d+)\s+(\S+)\s+(\d+)/) { | ||
| 218 | $tstart ||= $1; | ||
| 219 | $tend = $3; | ||
| 220 | $target .= $2; | ||
| 221 | next; | ||
| 222 | } | ||
| 223 | |||
| 224 | # anything else is going to be the match string | ||
| 225 | # this is REALLY UGLY because we have to extract absolute | ||
| 226 | # positions | ||
| 227 | $match .= substr($_,13,60) if $query; | ||
| 228 | } continue { | ||
| 229 | $_ = <$fh>; | ||
| 230 | } | ||
| 231 | |||
| 232 | return \@hits; | ||
| 233 | } | ||
| 234 | |||
| 235 | 1; | ||
| 236 |