| 1 | package Boulder::Blast::NCBI; | ||
| 2 | |||
| 3 | # NCBI BLAST file format parsing | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Boulder::Blast::NCBI - Parse and read NCBI 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 NCBI 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 File::Basename; | ||
| 36 | use Stone; | ||
| 37 | use Boulder::Stream; | ||
| 38 | use Carp; | ||
| 39 | use vars qw($VERSION @ISA); | ||
| 40 | @ISA = 'Boulder::Blast'; | ||
| 41 | |||
| 42 | $VERSION = 1.02; | ||
| 43 | |||
| 44 | sub _read_record { | ||
| 45 | my $self = shift; | ||
| 46 | my $fh = shift; | ||
| 47 | my $stone = shift; | ||
| 48 | |||
| 49 | # we don't find out about the name of the database or the parameters until we | ||
| 50 | # get to the bottom of the file. Too bad. | ||
| 51 | |||
| 52 | # loop until we find the query name | ||
| 53 | my $line; | ||
| 54 | do { | ||
| 55 | $line = <$fh>; | ||
| 56 | } until ($line && $line=~/^Query=/); | ||
| 57 | |||
| 58 | if ($line && $line =~ /^Query=\s+(\S+)/) { | ||
| 59 | $stone->insert(Blast_query => $1); | ||
| 60 | } else { | ||
| 61 | croak "Couldn't find query line!"; | ||
| 62 | } | ||
| 63 | |||
| 64 | do { | ||
| 65 | $line = <$fh>; | ||
| 66 | } until $line=~/([\d,]+) letters/; | ||
| 67 | |||
| 68 | croak "Couldn't find query length!" unless $1; | ||
| 69 | (my $len = $1) =~ s/,//g; | ||
| 70 | $stone->insert(Blast_query_length => $len); | ||
| 71 | |||
| 72 | # Read down to the first hit, if any. If we hit /^Parameters/, then we had no | ||
| 73 | # hits. | ||
| 74 | while (<$fh> ) { | ||
| 75 | last if /^(>|\s+Database)/; | ||
| 76 | } | ||
| 77 | |||
| 78 | if (/^>/) { # we found some hits | ||
| 79 | my $hits = $self->parse_hits($_); | ||
| 80 | $stone->insert(Blast_hits => $hits); | ||
| 81 | } | ||
| 82 | |||
| 83 | # At this point, one way or another, we're pointing at the Database | ||
| 84 | # line. | ||
| 85 | |||
| 86 | # Now we should be pointing at statistics | ||
| 87 | while (<$fh>) { | ||
| 88 | if (/Database: (.*)/) { | ||
| 89 | my $db = $1; | ||
| 90 | $stone->insert(Blast_db => $db); | ||
| 91 | $stone->insert(Blast_db_title => basename($db)); | ||
| 92 | } | ||
| 93 | # in case match title, overwrite previous setting of Blast_db_title | ||
| 94 | $stone->insert(Blast_db_title => $1) if /Title: (.*)/; | ||
| 95 | $stone->insert(Blast_db_date => $1) if /Posted date:\s+(.*)/; | ||
| 96 | last if /Lambda/; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | # At this point, one way or another, we're pointing at the Matrix | ||
| 101 | # line. We create a parameter stone to hold the results | ||
| 102 | my $parms = new Stone; | ||
| 103 | chomp ($line = <$fh>); | ||
| 104 | if (my ($lambda,$k,$h) = $line =~ /([\d.-]+)/g) { | ||
| 105 | $parms->insert('Lambda' => {Lambda => $lambda, K => $k, H => $h}); | ||
| 106 | } | ||
| 107 | chomp ($line = <$fh>); | ||
| 108 | chomp ($line = <$fh>); | ||
| 109 | if ($line =~ /^Gapped/) { | ||
| 110 | chomp ($line = <$fh>); | ||
| 111 | if (my ($lambda,$k,$h) = $line =~ /([\d.-]+)/g) { | ||
| 112 | $parms->insert('GappedLambda' => {Lambda => $lambda, K => $k, H => $h}); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | while (<$fh>) { | ||
| 118 | chomp; | ||
| 119 | $parms->insert(GapPenalties => $1) if /^Gap Penalties: (.+)/; | ||
| 120 | $parms->insert(Matrix => $1) if /^Matrix: (.+)/; | ||
| 121 | $parms->insert(HSPLength => $1) if /^effective HSP length: (.+)/; | ||
| 122 | $parms->insert(DBLength => $1) if /^length of database: (.+)/; | ||
| 123 | $parms->insert(T => $1) if /T: (.+)/; | ||
| 124 | $parms->insert(A => $1) if /A: (.+)/; | ||
| 125 | $parms->insert(X1 => $1) if /X1: (\d+)/; | ||
| 126 | $parms->insert(X2 => $1) if /X2: (\d+)/; | ||
| 127 | $parms->insert(S1 => $1) if /S1: (\d+)/; | ||
| 128 | $parms->insert(S2 => $1) if /S2: (\d+)/; | ||
| 129 | last if /^S2/; | ||
| 130 | } | ||
| 131 | $stone->insert(Blast_parms => $parms); | ||
| 132 | |||
| 133 | # finally done! | ||
| 134 | $stone; | ||
| 135 | } | ||
| 136 | |||
| 137 | # parse the hits and HSPs | ||
| 138 | sub parse_hits { | ||
| 139 | my $self = shift; | ||
| 140 | $_ = shift; | ||
| 141 | my $fh = $self->_fh; | ||
| 142 | my (@hits,@hsps,$accession,$orientation,$hit,$hsp); | ||
| 143 | my ($qstart,$qend,$tstart,$tend); | ||
| 144 | my ($query,$match,$target,$done,$new_hit,$new_hsp); | ||
| 145 | my $signif = 9999; | ||
| 146 | my $expect = 9999; | ||
| 147 | my $ident = 0; | ||
| 148 | |||
| 149 | while (!$done) { | ||
| 150 | chomp; | ||
| 151 | next unless length($_); | ||
| 152 | |||
| 153 | $done = /^\s+Database/; # here's how we get out of the loop | ||
| 154 | $new_hit = /^>(\S+)/; | ||
| 155 | $new_hsp = $accession && /Score\s+=\s+([\d.e+]+)\s+bits\s+\((\S+)\)/; | ||
| 156 | |||
| 157 | # hit a new HSP section | ||
| 158 | if ( $done || $new_hit || $new_hsp ) { | ||
| 159 | if ($hsp) { | ||
| 160 | croak "base alignment is out of whack" | ||
| 161 | unless length($query) == length($target); | ||
| 162 | $hsp->insert(Query => $query, | ||
| 163 | Subject => $target, | ||
| 164 | Alignment => substr($match,0,length($query)), | ||
| 165 | ); | ||
| 166 | $hsp->insert(Query_start => $qstart, | ||
| 167 | Query_end => $qend, | ||
| 168 | Subject_start => $tstart, | ||
| 169 | Subject_end => $tend, | ||
| 170 | Length => 1 + $qend - $qstart, | ||
| 171 | Orientation => $tend > $tstart ? 'plus' : 'minus', | ||
| 172 | ); | ||
| 173 | push(@hsps,$hsp); | ||
| 174 | undef $hsp; | ||
| 175 | } | ||
| 176 | if ($new_hsp) { | ||
| 177 | $hsp = new Stone; | ||
| 178 | $hsp->insert(Score => $2, Bits => $1); | ||
| 179 | ($qstart,$qend,$tstart,$tend) = (undef,undef,undef,undef); # undef all | ||
| 180 | ($query,$match,$target) = (undef,undef,undef); # these too | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | # hit a new subject section | ||
| 185 | if ( $done || $new_hit ) { | ||
| 186 | $accession = $1; | ||
| 187 | if ($hit) { | ||
| 188 | $signif = $expect if $signif == 9999; | ||
| 189 | $hit->insert(Hsps => \@hsps, | ||
| 190 | Signif => $signif, | ||
| 191 | Identity => $ident, | ||
| 192 | Expect => $expect, | ||
| 193 | ) if @hsps; | ||
| 194 | undef @hsps; | ||
| 195 | push(@hits,$hit); | ||
| 196 | ($signif,$expect,$ident) = (9999,9999,0); # reset max values | ||
| 197 | } | ||
| 198 | if ($new_hit) { | ||
| 199 | $hit = new Stone; | ||
| 200 | $hit->insert(Name => $accession); | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | # hit the length = line | ||
| 206 | if (/Length\s*=\s*([\d,]+)/) { | ||
| 207 | (my $len = $1) =~ s/,//g; | ||
| 208 | $hit->insert(Length => $len); | ||
| 209 | next; | ||
| 210 | } | ||
| 211 | |||
| 212 | # hit the Plus|Minus Strand line | ||
| 213 | if (/(Plus|Minus) Strand HSPs/) { | ||
| 214 | $orientation = lc $1; | ||
| 215 | next; | ||
| 216 | } | ||
| 217 | |||
| 218 | # None of the following is relevant unless $hsp is defined | ||
| 219 | next unless $hsp; | ||
| 220 | |||
| 221 | if (/Expect = ([+e\d\.-]+)/) { | ||
| 222 | $hsp->insert(Expect => $1); | ||
| 223 | $expect = $1 < $expect ? $1 : $expect; | ||
| 224 | } | ||
| 225 | |||
| 226 | if (/P(?:\(\d+\))? = (\S+)/) { | ||
| 227 | $hsp->insert(Signif => $1); | ||
| 228 | $signif = $1 < $signif ? $1 : $signif; | ||
| 229 | } | ||
| 230 | |||
| 231 | if (/Identities = \S+ \((\d+)%?\)/) { | ||
| 232 | my $idn = $1; | ||
| 233 | $hsp->insert(Identity => "$idn%"); | ||
| 234 | $ident = $idn > $ident ? $idn : $ident; | ||
| 235 | } | ||
| 236 | |||
| 237 | $hsp->insert(Positives => $1) if /Positives = \S+ \((\S+)\)/; | ||
| 238 | $hsp->insert(Strand => $1) if /Strand =\s+([^,]+)/; | ||
| 239 | $hsp->insert(Frame => $1) if /Frame =\s+([^,]+)/; | ||
| 240 | |||
| 241 | # process the query sequence | ||
| 242 | if (/^Query:\s+(\d+)\s*(\S+)\s+(\d+)/) { | ||
| 243 | $qstart ||= $1; | ||
| 244 | $qend = $3; | ||
| 245 | $query .= $2; | ||
| 246 | next; | ||
| 247 | } | ||
| 248 | |||
| 249 | # process the target sequence | ||
| 250 | if (/^Sbjct:\s+(\d+)\s*(\S+)\s+(\d+)/) { | ||
| 251 | $tstart ||= $1; | ||
| 252 | $tend = $3; | ||
| 253 | $target .= $2; | ||
| 254 | next; | ||
| 255 | } | ||
| 256 | |||
| 257 | # anything else is going to be the match string | ||
| 258 | # this is REALLY UGLY because we have to extract absolute | ||
| 259 | # positions | ||
| 260 | $match .= substr($_,12,60) if $query; | ||
| 261 | } continue { | ||
| 262 | $_ = <$fh>; | ||
| 263 | } | ||
| 264 | |||
| 265 | return \@hits; | ||
| 266 | } | ||
| 267 | |||
| 268 | 1; | ||
| 269 |