| 1 |
############################################################################ |
| 2 |
# demotic_blast package |
| 3 |
# Parses blast output, stores extracted information in convenient vars. |
| 4 |
# Works for both WU-BLAST and NCBI BLAST output files, |
| 5 |
# and for PSIBLAST runs from checkpoint files (as I use in profmark). |
| 6 |
# (SRE originated, 10/2000) |
| 7 |
# |
| 8 |
############################################################################ |
| 9 |
|
| 10 |
package demotic_blast; |
| 11 |
|
| 12 |
# parse(\*STDIN) would parse BLAST output |
| 13 |
# coming in via stdin. |
| 14 |
# |
| 15 |
sub parse (*) { |
| 16 |
my $fh = shift; |
| 17 |
my $parsing_header = 1; |
| 18 |
my $parsing_hitlist = 0; |
| 19 |
my $parsing_alilist = 0; |
| 20 |
my $is_wublast = 0; |
| 21 |
my $target; |
| 22 |
my $firstchunk; |
| 23 |
|
| 24 |
# Initialize everything... so we can call the parser |
| 25 |
# repeatedly, one call per BLAST output. |
| 26 |
# |
| 27 |
# This section also documents what's available in |
| 28 |
# variables within the package. |
| 29 |
# |
| 30 |
# Arrays are indexed [0..nhits-1] or [0..nali-1] |
| 31 |
# |
| 32 |
$queryname = ""; # Name of the query sequence |
| 33 |
$querydesc = ""; # Description line for the query (or "") |
| 34 |
$querylen = 0; # Length of the query in residues |
| 35 |
$db = ""; # Name of the target database file |
| 36 |
$db_nseq = 0; # Number of sequences in the target database |
| 37 |
$db_nletters = ""; # Number of residues in the target database |
| 38 |
# (stored as a string so we can have large #'s) |
| 39 |
|
| 40 |
# The top hit list (still in rank order) |
| 41 |
$nhits = 0; # Number of entries in the hit list |
| 42 |
@hit_target = (); # Target sequence name (by rank) |
| 43 |
%target_desc = (); # Target sequence description (by targ name) |
| 44 |
@hit_bitscore = (); # Raw score (by rank) |
| 45 |
@hit_Eval = (); # E-val (by rank) |
| 46 |
|
| 47 |
# The alignment output (in order it came in) |
| 48 |
# all indexed by # of alignment [0..nali-1] |
| 49 |
$nali = 0; # Number of alignments |
| 50 |
@ali_target = (); # Target sequence name |
| 51 |
@ali_score = (); # Raw score of alignment |
| 52 |
@ali_bitscore = (); # bit score |
| 53 |
@ali_evalue = (); # E-value |
| 54 |
@ali_pvalue = (); # P-value |
| 55 |
@ali_nident = (); # Number of identical residues |
| 56 |
@ali_alen = (); # Length of alignment |
| 57 |
@ali_identity = (); # Percent identity |
| 58 |
@ali_npos = (); # Number of positives (similar positions) |
| 59 |
@ali_positive = (); # Percent of positions matched or similar |
| 60 |
@ali_qstart = (); # Start position on query |
| 61 |
@ali_qend = (); # End position on query |
| 62 |
@ali_tstart = (); # Start position on target |
| 63 |
@ali_tend = (); # End position on target |
| 64 |
@ali_qali = (); # Aligned string from query |
| 65 |
@ali_tali = (); # Aligned string from target (subject) |
| 66 |
@ali_hitidx = (); # index of hit |
| 67 |
$hitidx = -1; |
| 68 |
|
| 69 |
# Now, loop over the lines of our input, and parse 'em. |
| 70 |
# |
| 71 |
while (<$fh>) { |
| 72 |
if ($parsing_header) { |
| 73 |
if (/^\s*\*+ No hits found \*+\s*$/) { return 1; } |
| 74 |
|
| 75 |
if (/^Sequences producing /) { # wu and ncbi share this |
| 76 |
$parsing_header = 0; |
| 77 |
$parsing_hitlist = 1; |
| 78 |
<$fh>; # This discards the next (blank) line (ncbi, wu) |
| 79 |
next; |
| 80 |
} elsif (/^Query=\s*(\S*)\s*(.*)\s*$/) { # allows blank query |
| 81 |
$queryname = $1; |
| 82 |
$querydesc = $2; chomp $querydesc; |
| 83 |
if ($queryname eq "") { |
| 84 |
$queryname = "unnamed_query"; |
| 85 |
} |
| 86 |
while (1) { |
| 87 |
$_ = <$fh>; # perl quirk: unless part of a while() |
| 88 |
# loop, line must be explicitly assigned |
| 89 |
# to $_ or else it will be lost. |
| 90 |
if (/^\s+\((\S+) letters/) { $querylen = $1; last; } |
| 91 |
elsif (/^Length=(\d+)/) { $querylen = $1; last; } # NCBI |
| 92 |
elsif (/^\s*( .+)\s*$/) { $querydesc .= $1; chomp $querydesc; } |
| 93 |
} |
| 94 |
} elsif (/^Database:\s*(.+)\s*$/) { |
| 95 |
$db = $1; |
| 96 |
$_ = <$fh>; |
| 97 |
if (/^\s+(\S+) sequences; (\S+) total letters/) { |
| 98 |
$db_nseq = $1; |
| 99 |
$db_nletters = $2; |
| 100 |
$db_nseq =~ s/,//g; |
| 101 |
$db_nletters =~ s/,//g; |
| 102 |
} |
| 103 |
} elsif (/^Copyright.+Washington University/) { |
| 104 |
$is_wublast = 1; |
| 105 |
} |
| 106 |
} |
| 107 |
elsif ($parsing_hitlist) { |
| 108 |
# WUBLAST: Sequences producing High-scoring Segment Pairs: Score P(N) N |
| 109 |
# 2-Hacid_dh/1/17-338/439-757 domains: PTXD_PSEST/5-326 O28... 299 4.7e-27 1 |
| 110 |
# NCBI+: Sequences producing significant alignments: (Bits) Value |
| 111 |
# 2-Hacid_dh/4/753-1076/1224-1507 domains: Q20595_CAEEL/181-504 P... 95.1 4e-20 |
| 112 |
# NCBI: Sequences producing significant alignments: (bits) Value |
| 113 |
# 2-Hacid_dh/4/753-1076/1224-1507 domains: Q20595_CAEEL/181-504 PD... 95 4e-20 |
| 114 |
# In NCBI E-values, beware "e-xxx" without any leading number. |
| 115 |
# In NCBI+, beware bit scores can now be real numbers, not just integers. |
| 116 |
# |
| 117 |
if (/^\s*$/) { |
| 118 |
$parsing_hitlist = 0; |
| 119 |
$parsing_alilist = 1; |
| 120 |
next; |
| 121 |
} |
| 122 |
elsif (( $is_wublast && /^\s*(\S+)\s+(.+)\s+(\d+)\s+(\S+)\s+\d+\s*$/) || |
| 123 |
(! $is_wublast && /^\s*(\S+)\s+(.+)\s+(\S+)\s+(\S+)\s*$/)) |
| 124 |
{ |
| 125 |
$hit_target[$nhits] = $1; |
| 126 |
$target_desc{$1} = $2; |
| 127 |
$hit_bitscore[$nhits] = $3; |
| 128 |
|
| 129 |
if ($is_wublast) { $hit_Eval[$nhits] = $4; } # actually WU reports P-value |
| 130 |
else { $hit_Eval[$nhits] = &repair_evalue($4); } |
| 131 |
|
| 132 |
$nhits++; |
| 133 |
} |
| 134 |
} |
| 135 |
elsif ($parsing_alilist) { |
| 136 |
if (/^>\s*(\S+)\s*(.*)$/) { |
| 137 |
$target = $1; |
| 138 |
$hitidx ++; |
| 139 |
$target_desc{$target} = $2; |
| 140 |
|
| 141 |
$_ = <$fh>; |
| 142 |
if (/^\s+Length = (\S+)/) { |
| 143 |
$target_len{$target} = $1; |
| 144 |
} |
| 145 |
} |
| 146 |
elsif (/^ Score =\s+(\d+) \((\S+) bits\), Expect\s+=\s+(\S+), /) { # WU |
| 147 |
$nali++; |
| 148 |
$ali_target[$nali-1] = $target; |
| 149 |
$ali_score[$nali-1] = $1; |
| 150 |
$ali_bitscore[$nali-1] = $2; |
| 151 |
$ali_evalue[$nali-1] = $3; |
| 152 |
$ali_hitidx[$nali-1] = $hitidx; |
| 153 |
} |
| 154 |
elsif (/^ Score =\s+(\S+) bits \((\S+)\),\s*Expect\s+=\s+(\S+), /) { # NCBI |
| 155 |
$nali++; |
| 156 |
$ali_target[$nali-1] = $target; |
| 157 |
$ali_bitscore[$nali-1] = $1; |
| 158 |
$ali_score[$nali-1] = $2; |
| 159 |
$ali_evalue[$nali-1] = &repair_evalue($3); |
| 160 |
$ali_hitidx[$nali-1] = $hitidx; |
| 161 |
} |
| 162 |
elsif (/^ Identities = (\d+)\/(\d+) \((\d+)%\).+Positives = (\d+).+\((\d+)%/) { # NCBI or WU |
| 163 |
$ali_nident[$nali-1] = $1; |
| 164 |
$ali_alen[$nali-1] = $2; |
| 165 |
$ali_identity[$nali-1] = $3; |
| 166 |
$ali_npos[$nali-1] = $4; |
| 167 |
$ali_positive[$nali-1] = $5; |
| 168 |
$ali_hitidx[$nali-1] = $hitidx; |
| 169 |
$firstchunk = 1; |
| 170 |
} |
| 171 |
elsif (/^ Identities = (\d+)\/(\d+) \((\d+)%\).+/) { # NCBI megablast : no Positives |
| 172 |
$ali_nident[$nali-1] = $1; |
| 173 |
$ali_alen[$nali-1] = $2; |
| 174 |
$ali_identity[$nali-1] = $3; |
| 175 |
$ali_npos[$nali-1] = $1; |
| 176 |
$ali_positive[$nali-1] = $3; |
| 177 |
$ali_hitidx[$nali-1] = $hitidx; |
| 178 |
$firstchunk = 1; |
| 179 |
} |
| 180 |
elsif (/^Query:?\s+(\d+)\s+(\S+)\s+(\d+)\s*$/) { |
| 181 |
if ($firstchunk) { $ali_qstart[$nali-1] = $1; } |
| 182 |
$ali_qali[$nali-1] .= $2; |
| 183 |
$ali_qend[$nali-1] = $3; |
| 184 |
} |
| 185 |
elsif (/^Sbjct:?\s+(\d+)\s+(\S+)\s+(\d+)\s*$/) { |
| 186 |
if ($firstchunk) { $ali_tstart[$nali-1] = $1; } |
| 187 |
$ali_tali[$nali-1] .= $2; |
| 188 |
$ali_tend[$nali-1] = $3; |
| 189 |
$firstchunk = 0; |
| 190 |
} |
| 191 |
|
| 192 |
elsif (/^BLAST/) { return 1; } # normal return; output from a new query is starting |
| 193 |
elsif (/^Effective search space used:/) { return 1; } #normal end of query for NCBI BLAST+ |
| 194 |
|
| 195 |
} |
| 196 |
} # this closes the loop over lines in the input stream. |
| 197 |
|
| 198 |
if ($parsing_alilist) { return 1; } else { return 0; } |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
# NCBI BLAST now has a nasty habit of abbreviating |
| 203 |
# 1e-100 as e-100, which isn't a number format. |
| 204 |
|
| 205 |
sub repair_evalue |
| 206 |
{ |
| 207 |
my $value = shift; |
| 208 |
if ($value =~ /^e-\d+$/) { $value = "1$value"; } |
| 209 |
$value; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
sub exblxout { |
| 214 |
my $ofh = shift; |
| 215 |
my $i; |
| 216 |
|
| 217 |
for ($i = 0; $i <= $nali-1; $i++) { |
| 218 |
printf $ofh "%s\t%d\t%d\t%d\t%s\t%d\t%d\t%s\n", |
| 219 |
$ali_evalue[$i], |
| 220 |
$ali_identity[$i], |
| 221 |
$ali_tstart[$i], |
| 222 |
$ali_tend[$i], |
| 223 |
$ali_target[$i], |
| 224 |
$ali_qstart[$i], |
| 225 |
$ali_qend[$i], |
| 226 |
$queryname; |
| 227 |
} |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
sub tblout { |
| 233 |
my $ofh = shift; |
| 234 |
my $i; |
| 235 |
|
| 236 |
for ($i = 0; $i <= $nali-1; $i++) { |
| 237 |
printf $ofh "%s\t%d\t%d\t%d\t%s\t%d\t%d\t%s\t%s\n", |
| 238 |
$ali_evalue[$i], |
| 239 |
$ali_identity[$i], |
| 240 |
$ali_qstart[$i], |
| 241 |
$ali_qend[$i], |
| 242 |
$queryname, |
| 243 |
$ali_tstart[$i], |
| 244 |
$ali_tend[$i], |
| 245 |
$ali_target[$i], |
| 246 |
$target_desc{$ali_target[$i]}; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
sub gffout { |
| 252 |
my $ofh = shift; |
| 253 |
my $source = shift; |
| 254 |
my $feature = shift; |
| 255 |
my $i; |
| 256 |
my $strand; |
| 257 |
|
| 258 |
for ($i = 0; $i <= $nali-1; $i++) { |
| 259 |
if ($ali_qstart[$i] > $ali_qend[$i]) { $strand = "-"; } |
| 260 |
else { $strand = "+"; } |
| 261 |
|
| 262 |
printf $ofh "%s\t%s\t%s\t%d\t%d\t%.1f\t%s\t.\tgene \"%s\"\n", |
| 263 |
$ali_target[$i], |
| 264 |
$source, |
| 265 |
$feature, |
| 266 |
$ali_tstart[$i], |
| 267 |
$ali_tend[$i], |
| 268 |
$ali_bitscore[$i], |
| 269 |
$strand, |
| 270 |
$queryname; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
sub profmarkout { |
| 275 |
my $ofh = shift; |
| 276 |
my $i; |
| 277 |
|
| 278 |
for ($i = 0; $i < $nhits; $i++) { |
| 279 |
printf $ofh "%g\t%.1f\t%s\t%s\n", $hit_Eval[$i], $hit_bitscore[$i], $hit_target[$i], $queryname; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
1; |
| 285 |
__END__ |