-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmpblast.pl
617 lines (520 loc) · 17.9 KB
/
mpblast.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/perl -w
#BEGIN{require 5.6.0}
#use warnings;
use strict;
use sigtrap;
use constant e => 2.718281828;
use BPlite;
use FAlite;
use Getopt::Std;
use vars qw($opt_h $opt_w $opt_s $opt_n $opt_m $opt_l $opt_t $opt_b);
#our ($opt_h, $opt_w, $opt_s, $opt_n, $opt_m, $opt_b);
#############################################
# usage and commandline argument processing #
#############################################
my $usage = '
MPBLAST - multiplex-BLAST
usage: mpblast [options] <blast commandline except query is fasta database>
options:
-h prints documentation
TYPE_OF_BLAST - will guess if these are not chosen
-w WU-BLAST
-n NCBI-BLAST
MPBLAST SPECIFICS - change only if necessary
-l <int> length of spacer (not -s, default 100)
-s use WU_BLAST segmentation optimizations (implies -w)
-m <int> maximum length of multiplex (default 100,000)
OUTPUT OPTION - default is table
-t <char> table output separator
-b blast-style output format (blastn only)
';
getopts('hwsnm:l:t:b');
if ($opt_h) {system("pod2text $0"); exit}
die $usage unless @ARGV;
my $LARGE_NUMBER = 1e7; # hspmax
my $PLEX = $opt_m ? $opt_m : 100000;
my $BLAST_OUTPUT = $opt_b;
my $WU_BLAST = $opt_w;
my $NCBI_BLAST = $opt_n;
my $SEGMENTING = $opt_s;
my $TABLE_SEPARATOR = $opt_t ? $opt_t : " ";
my $SPACER_LENGTH = $opt_l ? $opt_l : 100;
$WU_BLAST = 1 if $SEGMENTING;
if (not $WU_BLAST and not $NCBI_BLAST) {
if ($ARGV[0] =~ /blastall/) {
$NCBI_BLAST = 1;
print STDERR "MPBLAST: assuming NCBI-BLAST\n";
}
else {
$WU_BLAST = 1;
print STDERR "MPBLAST: assuming WU-BLAST\n";
}
}
my $Segnum = 0; # for blast output
#####################################
# WU-BLAST and NCBI-BLAST specifics #
#####################################
my $FH; # filehandle for file or stdin
my ($Program, $Database, $Query, $Dash, @BLAST_OPTIONS);
my ($SEGMENT_SEPARATOR, $SEGLEN);
if ($WU_BLAST) {
# WU-BLAST command specifics
$Dash = '-'; # used to signify stdin
($Program, $Database, $Query, @BLAST_OPTIONS) = @ARGV;
$Database = "\"$Database\""; # for virtual database
die $usage unless defined $Query;
if ($Query eq '-') {$FH = \*STDIN}
else {
open(QUERY, $Query)
or die "MPBLAST ERROR query file ($Query) not found\n";
$FH = \*QUERY;
}
# WU-BLAST multiplex specifics
push @BLAST_OPTIONS, "V=0 B=10000000 hspmax=$LARGE_NUMBER";
if ($SEGMENTING) {
$SEGMENT_SEPARATOR = '-';
push @BLAST_OPTIONS, "kap";
}
else {
$SEGMENT_SEPARATOR = '-' x $SPACER_LENGTH;
#push @BLAST_OPTIONS, "hspsepqmax=1 gapsepqmax=1"; # not faster...
}
}
elsif ($NCBI_BLAST) {
# NCBI-BLAST command specifics
$Dash = ''; # no dash used to signify stdin
my ($prog, %arg) = @ARGV; # not guaranteed to parse commandline correctly
die "no program specified\n" unless defined $arg{-p};
$arg{-d} = "nr" unless defined $arg{-d};
$Program = "$prog -p $arg{-p}";
$Database = "-d $arg{-d}";
if (not defined $arg{-i}) {$FH = \*STDIN}
else {
open(QUERY, $arg{-i})
or die "MPBLAST ERROR query file ($arg{-i}) not found\n";
$FH = \*QUERY;
}
# NCBI-BLAST multiplex specifics
delete $arg{-p};
delete $arg{-d} if defined $arg{-d};
delete $arg{-i} if defined $arg{-i};
delete $arg{-b} if defined $arg{-b};
delete $arg{-m} if defined $arg{-m};
$arg{-b} = $LARGE_NUMBER;
$arg{-F} = "\"$arg{-F}\"" if $arg{-F}; # thanks Joey Bedell
push @BLAST_OPTIONS, %arg;
$SEGMENT_SEPARATOR = 'N' x $SPACER_LENGTH;
}
$SEGLEN = length($SEGMENT_SEPARATOR);
#############################################
# temp file - better than pipe in this case #
#############################################
my $TEMP_FILE = "/tmp/mpblast.$$.tmp";
END{system "rm $TEMP_FILE" if defined $TEMP_FILE and -e $TEMP_FILE}
#####################
# main program loop #
#####################
my $fasta = new FAlite($FH);
my $length = 0;
my @pool; # pool of fasta files
while (my $entry = $fasta->nextEntry) {
$length += length($entry->seq);
push @pool, $entry;
if ($length >= $PLEX) {
executeMultiplex(\@pool);
$length = 0;
@pool = ();
}
}
executeMultiplex(\@pool) if @pool;
exit(0);
###############################################################################
# subroutines below #
###############################################################################
#################### 1. creates a lookup of segments
# executeMultiplex # 2. executes blast with multiplex
#################### 3. sends results to ouput routine
sub executeMultiplex {
my ($pool) = @_;
my $numseq = @$pool;
#print STDERR "MPBLAST segment size:$numseq\n";
# concatenate sequences and deflines and make a coordinate lookup
my (@def, @seq); # will contain concatenated defs and seqs
my @segment; # segment info stored here
my @bucket; # bucket sort for coordinate lookup
my $minlen = $PLEX; # minimum sequence length
my ($sum, $lookup) = (0, 0);
foreach my $entry (@$pool) {
push @def, $entry->def;
push @seq, $entry->seq;
my ($name) = $entry->def =~ /^>(\S+)/;
my $length = length($entry->seq);
for(my $i=$sum;$i<($length+$sum);$i++) {$bucket[$i+1] = $lookup}
$sum += $length + $SEGLEN;
push @segment, {
length => $length,
sum => $sum,
name => $name,
def => $entry->def};
$lookup++;
$minlen = $length if $length < $minlen;
}
# set the query size (search space size) for the smallest query
my $Y = $WU_BLAST ? "Y=$minlen" : ""; # WU-BLAST only
# pipe sequence to blast process
open(TEMP, "| $Program $Database $Dash @BLAST_OPTIONS $Y > $TEMP_FILE")
or die "MPBLAST ERROR running blast ($Program)\n";
@def = (">mpblast.$$"); # long deflines screw up NCBI-BLAST
print TEMP join(" : ", @def), "\n";
print TEMP join($SEGMENT_SEPARATOR, @seq), "\n";
close TEMP;
my ($output) = readBlast(\@segment, \@bucket);
output(\@segment, $output);
}
#############
# readBlast # reads the BLAST report and returns the output
#############
sub readBlast {
my ($seg, $bucket) = @_;
open(BLAST, $TEMP_FILE) or die;
my @output; # used for sorted output
my $handle;
my $blast = new BPlite(\*BLAST);
while (my $sbjct = $blast->nextSbjct) {
my ($def) = $sbjct->name;# =~ /^>(\S+)/;
while (my $hsp = $sbjct->nextHSP) {
my ($begin, $end) = ($hsp->qb, $hsp->qe);
# begin and end should be in the same segment
my $handle = $bucket->[$begin];
my $handle2 = $bucket->[$end];
#################################################################
# error if begin and end are not defined or not in same segment #
#################################################################
if (not defined $handle or not defined $handle2 or
$handle != $handle2) {
#system("cp $TEMP_FILE ./error_at_$begin");
#print "Begin:$handle\nEnd:$handle2\n$hsp\n";
#use DataBrowser; browse($bucket);
die "MPBLAST FATAL ERROR\n",
"Alignments are crossing the multiplex spacer.\n",
"Try setting -l or changing parameters\n";
}
# remap the coordinates of the segment
my $segment = $seg->[$handle];
my $extra = $segment->{sum} - $segment->{length};
my $new_begin = $begin - $extra + $SEGLEN;
my $new_end = $end - $extra + $SEGLEN;
$hsp->{QB} = $new_begin;
$hsp->{QE} = $new_end;
$hsp->{QUERY_NAME} = $seg->[$handle]{name};
$hsp->{SBJCT_NAME} = $def;
$hsp->{SBJCT_LENGTH} = $sbjct->length;
unless ($BLAST_OUTPUT) {
delete $hsp->{QL};
delete $hsp->{SL};
delete $hsp->{AS};
($hsp->{SBJCT_NAME}) = $hsp->{SBJCT_NAME} =~ /^>(\S+)/;
}
push @{$output[$handle]{$def}}, $hsp;
}
}
close BLAST;
return \@output;
}
#################
# displayOutput # display the results
#################
sub output {
my ($segment, $output) = @_;
# need to get header and footer from temp file
# footer for Karlin-Altschul parameters
# header and footer for BLAST output
open(HF, $TEMP_FILE) or die;
my (@header, @footer);
while(<HF>) {last if /^Query=/; push @header, $_;}
while(<HF>) {last if /^Parameters|^\s+Database:/}
push @footer, $_;
while(<HF>) {push @footer, $_}
# Karlin-Altschul stats
my ($L, $K, $H, $D) = KAstats(\@footer); # K-A stats
for (my $i=0;$i<@$segment;$i++) {
my $M = $segment->[$i]{length}; # length of query
foreach my $sbjct (keys %{$output->[$i]}) {
foreach my $hsp (@{$output->[$i]{$sbjct}}) {
my $S = $hsp->score;
my $N = $hsp->{SBJCT_LENGTH}; # length of sbjct
# edge correction for M and N
my $edge_correction = $L * $S / $H;
my $m_prime = $M - $edge_correction;
my $n_prime = $N - $edge_correction;
$m_prime = 1 if $m_prime < 1;
$n_prime = 1 if $n_prime < 1;
# calculation of E and P
my $E = $K * $m_prime * $n_prime * e ** -($L * $S);
$E = $E * $D / $N; # edge corrected E
my $P = ($E < 0.01) ? $E : 1 - e ** -$E;
# store the values
$hsp->{E} = reformat($E);
$hsp->{P} = reformat($P);
}
}
}
if ($BLAST_OUTPUT) {
for (my $i=0;$i<@$segment;$i++) {
$Segnum++;
print "MPBLAST SEGMENT $Segnum START\n\n";
#print "MPBLAST SEGMENT $Segnum ($segment->[$i]{name}) START\n\n";
print @header;
print wrap("Query= " . substr($segment->[$i]{def}, 1) .
" ($segment->[$i]{length} letters)"), "\n";
print "Database: $Database\n\n\n";
foreach my $sbjct (keys %{$output->[$i]}) {
my $n = $output->[$i]{$sbjct}[0]{SBJCT_LENGTH};
print wrap($sbjct), " Length = $n\n\n";
foreach my $hsp (@{$output->[$i]{$sbjct}}) {
displayBLAST($hsp);
}
}
print "\n";
print @footer;
print "\nMPBLAST SEGMENT $Segnum END\n\n";
}
}
else {
for (my $i=0;$i<@$segment;$i++) {
my $count = 0;
foreach my $sbjct (keys %{$output->[$i]}) {
foreach my $hsp (@{$output->[$i]{$sbjct}}) {
print join(" ", $hsp->qb, $hsp->qe, $hsp->{QUERY_NAME},
$hsp->sb, $hsp->se, $hsp->{SBJCT_NAME}, $hsp->score,
$hsp->bits, $hsp->{E}, $hsp->{P}, $hsp->percent,
$hsp->match, $hsp->positive, $hsp->length,
$segment->[$i]{length}, $hsp->{SBJCT_LENGTH}, $hsp->qg,
$hsp->sg), "\n";
$count++;
}
}
print "\n" if $count;
}
}
}
############
# reformat # just changes the P and E values to something less precise
############
sub reformat {
my ($V) = @_;
if (length($V) > 7) {
if ($V =~ /^(\d)\.(\d+)(e[\+\-]?\d+)/) {
$V = $1 . "." . substr($2, 0, 2) . $3;
}
elsif ($V < 1) {
$V = substr($V, 0, 7);
}
else {
$V = substr($V, 0, index($V, "."));
}
}
return $V;
}
###########
# KAstats # retrieves K-A stats from footer
###########
sub KAstats {
my ($footer) = @_;
my ($L, $K, $H, $N);
for(my $i=0;$i<@$footer;$i++) {
if ($footer->[$i] =~ /As Used/) {
my @f = split(/\s+/, $footer->[$i+3]);
($L, $K, $H) = ($f[4], $f[5], $f[6]); # WU-BLAST
#print $footer->[$i+3], "\n";
#print $L, "\n";
#die;
}
elsif ($footer->[$i] =~ /^Gapped/ and $footer->[$i+1] =~ /^Lambda/) {
my ($foo, $lambda, $k, $h) = split(/\s+/, $footer->[$i+2]);
($L, $K, $H) = ($lambda, $k, $h); # NCBI-BLAST
}
elsif ($footer->[$i] =~ /letters in database:\s+(\S+)/) {
$N = $1; # works for both
$N =~ s/,//g;
}
}
return($L, $K, $H, $N);
}
################
# displayBLAST # blast-like output (not identical though)
################
sub displayBLAST {
my ($hsp) = @_;
my $similar = int(1000*$hsp->positive/$hsp->length)/10;
print " Score = ", $hsp->score, " (", $hsp->bits, " bits), Expect = ",
$hsp->{E}, ", P = ", $hsp->{P}, "\n", " Identities = ", $hsp->match,
"/", $hsp->length, " (", $hsp->percent, "%), ", "Positives = ",
$hsp->positive, "/", $hsp->length, " (", $similar, "%)\n\n";
my $size = 60;
my $qgaps = 0;
my $sgaps = 0;
my ($QB, $QE, $SB, $SE) =
($hsp->qb, $hsp->qe, $hsp->sb, $hsp->se);
for (my $i=0;$i<length($hsp->{QL});$i+=$size) {
my $qchunk = substr($hsp->{QL}, $i, $size);
my $schunk = substr($hsp->{SL}, $i, $size);
my $achunk = substr($hsp->{AS}, $i, $size);
my $qgf = $qchunk =~ tr/-/-/;
my $sgf = $schunk =~ tr/-/-/;
my ($qb, $qe, $sb, $se);
if ($QB < $QE) {
$qb = $QB + $i - $qgaps;
$qe = $qb + length($qchunk) - $qgf - 1;
}
else {
$qb = $QB - $i + $qgaps;
$qe = $qb - length($qchunk) + $qgf + 1;
}
if ($SB < $SE) {
$sb = $SB + $i - $sgaps;
$se = $sb + length($schunk) - $sgf - 1;
}
else {
$sb = $SB - $i + $sgaps;
$se = $sb - length($schunk) + $sgf + 1;
}
$qgaps += $qgf;
$sgaps += $sgf;
my $qspace = length($qb);
my $sspace = length($sb);
my $longest = $qspace >= $sspace ? $qspace : $sspace;
$qspace = ' ' x ($longest - $qspace);
$sspace = ' ' x ($longest - $sspace);
my $aspace = ' ' x ($longest + 8);
print
"Query: $qspace$qb $qchunk $qe\n",
$aspace, $achunk, "\n",
"Sbjct: $sspace$sb $schunk $se\n\n";
}
}
########
# wrap # for wrapping long lines in the blast output
########
sub wrap {
my ($line) = @_;
my @output;
my @word = split(/\s+/, $line);
my $linelen = 0;
my $firstline = 1;
for(my $i=0; $i<@word; $i++) {
if ($i == @word -1) {
push @output, "$word[$i]";
}
elsif ($linelen + length($word[$i]) + length($word[$i+1]) < 80) {
push @output, "$word[$i] ";
$linelen += length($word[$i]) + 1;
}
else {
$firstline = 0;
push @output, "$word[$i]\n";
push @output, (" " x 12);
$linelen = 12;
}
}
push @output, "\n";
return join("", @output);
}
__END__
=head1 NAME
MPBLAST - multiplex BLAST
=head1 SYNOPSIS
mpblast [hwsnmltb] <blast command line>
=head1 DESCRIPTION
MPBLAST improves the performance of BLAST searches by combining short querries
into one multiplex query. For example, instead of executing BLASTN 100 times
with 500 bp querries, you execute BLAST once on a multiplexed 50,000 bp query.
The optimal size of a multiplex query is about 100,000 characters. Therefore if
you execute MPBLAST with 10,000 500 bp querries, it would break this up into 50
separate searches of 100,000 bp each. The size of each multiplex is a
commandline option should you wish to change it. Each version of BLASTN has its
own optimal multiplex size, so you may want to experiment and find the optimal
size for your use.
=head1 COMMANDLINE OPTIONS
=head2 -w -n
Sets the type of BLAST to either WU-BLAST or NCBI-BLAST. If one of these flags
are not set, MPBLAST will guess based on the program name.
=head2 -l -s
The length of the segment separator can be set with the -l option. The
separator is used to prevent alignments from crossing the single-sequence
boundaries in the multiplex query. The default is to use 100 -'s for WU-BLAST
and 100 N's for NCBI-BLAST.
If using WU-BLAST, you can use the -s option to make the multiplex segmentation
more efficient. -s makes each segment separator into a single '-' instead of
100 char string.
=head2 -m
The default length of multiplexes is 100,000 bp. Empirically, this value works
well. The optimal length is determined by various factors though, and there may
be applications where smaller or larger values are more suitable. For example,
on machines with little RAM, -m should be set smaller (also see -b below). I
have noticed on some platforms that the optimial multipliex size for NCBI-BLAST
is more than 200,000 bp.
=head2 -t -b
The default output format is space-delimited fields (see below). You can change
the delimiter with the -s option.
The -b option makes the output look like a BLAST report.
=head1 OUTPUT FORMATS
MPBLAST has 2 output formats. The default is tabular. You may set the record
separator with the -t option. The default is a single space.
The column definitions are as follows:
1: query begin
2: query end
3: query name
4: sbjct begin
5: sbjct end
6: sbjct name
7: raw score
8: bits (normalized score)
9: E-value
10: P-value
11: percent identity
12: number of matches
13: number of positive scores (similarities)
14: length of alignment
15: length of query
16: length of sbjct
17: number of gaps in query alignment
18: number of gaps in sbjct alignment
Each row of the table corresponds to an alignment. Blank lines separate query
sequences.
The other format looks like concatenated WU-BLAST reports. Between each report
are tags that identify each segment of the multiplex.
MPBLAST SEGMENT 0 START
:
: (the blast report)
:
MPBLAST SEGMENT 0 END
MPBLAST SEGMENT 1 START
:
MPBLAST SEGMENT 1 END
The -b switch turns on BLAST-style output. Note that this takes more memory.
=head1 PERFORMANCE
The peformance improvement is typically around 10x, but this depends on several
factors. These include the size of each sequence in the multiplex, the size of
the multiplex, the size of the database relative to RAM (caching or thrashing),
the similarity between the sequences in the multiplex, and the version of
BLAST. In my tests, WU-BLAST is faster than NCBI-BLAST, but NCBI-BLAST benefits
more from multiplexing.
=head1 LIMITATIONS
Various BLAST commandline options are not supported or do not behave in the
expected manner. For example, -V and -B (-v and -b in NCBI-BLAST) are disabled.
Combining HSPs with Sum or Poisson statistics is not supported.
=head1 SEE ALSO
WU-BLAST (http://blast.wustl.edu)
NCBI-BLAST (http://www.ncbi.nlm.nih.gov)
=head1 AUTHORS
Ian Korf (http://sapiens.wustl.edu/~ikorf)
=head1 ACKNOWLEDGEMENTS
This software was developed at the Genome Sequencing Center at Washington
Univeristy, St. Louis, MO.
=head1 COPYRIGHT
Copyright (C) 2000 Ian Korf. All Rights Reserved.
=head1 DISCLAIMER
This software is provided "as is" without warranty of any kind. This software
may not be redistributed without permission of the authors.
=cut