-
Notifications
You must be signed in to change notification settings - Fork 26
/
_split_blast.pl
executable file
·235 lines (197 loc) · 7.22 KB
/
_split_blast.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
#!/usr/bin/env perl
# 2013-20 Bruno Contreras
# Script to take advantage of multicore machines when running BLAST,
# which seems to scale up to the number of physical cores in our tests.
# Supports old BLAST (blastall) and new BLAST+ binaries.
use strict;
use warnings;
use Benchmark;
use File::Temp qw/tempfile/;
use FindBin '$Bin';
use lib "$Bin/lib";
use lib "$Bin/lib/bioperl-1.5.2_102/";
use phyTools qw(read_FASTA_file_array SEQ NAME);
use ForkManager;
my $VERBOSE = 0;
my $DEFAULTBATCHSIZE = 100;
my $BLASTDBVARNAME = 'BLASTDB';
my ($n_of_cores,$batchsize,$raw_command,$command) = (1,$DEFAULTBATCHSIZE,'','');
my ($outfileOK,$blastplusOK,$input_seqfile,$output_file,$db_file) = (0,0,'','','');
if(!$ARGV[2])
{
print "\nUsage: split_blast.pl <number of processors/cores> <batch size> <blast command>\n\n";
print "<number of processors/cores> : while 1 is accepted, at least 2 should be requested\n";
print "<batch size> : is the number of sequences to be blasted in each batch, $DEFAULTBATCHSIZE works well in our tests\n";
print "<blast command> : is the explicit blast command that you would run in your terminal, either BLAST+ or old BLAST\n\n";
print "Example: split_blast.pl 8 250 blastp -query input.faa -db uniprot.fasta -outfmt 6 -out out.blast\n\n";
print "Please escape any quotes in your BLAST command. For instance:\n\n";
print "blastall -p blastp -i input.faa -d nr.fasta -m 8 -F 'm S' -o out.blast\n\n";
print "should be escaped like this:\n\n";
print "blastall -p blastp -i input.faa -d nr.fasta -m 8 -F \\'m\\ S\\' -o out.blast\n";
exit(0);
}
else ## parse command-line arguments
{
$n_of_cores = shift(@ARGV);
if($n_of_cores < 0){ $n_of_cores = 1 }
$batchsize = shift(@ARGV);
if($batchsize < 0){ $batchsize = $DEFAULTBATCHSIZE } # optimal in our tests
$raw_command = join(' ',@ARGV);
if($raw_command =~ /\-i (\S+)/)
{
$input_seqfile = $1;
if($raw_command =~ /\-o (\S+)/){ $output_file = $1 }
if($raw_command =~ /\-d (\S+)/){ $db_file = $1 }
}
elsif($raw_command =~ /\-query (\S+)/)
{
$blastplusOK = 1;
$input_seqfile = $1;
if($raw_command =~ /\-out (\S+)/){ $output_file = $1 }
if($raw_command =~ /\-db (\S+)/){ $db_file = $1 }
}
else{ die "# ERROR: BLAST command must include an input file [-i,-query]\n" }
if(!-s $input_seqfile){ die "# ERROR: cannot find input file $input_seqfile\n" }
elsif(!-s $db_file && !glob("$db_file*") &&
($ENV{$BLASTDBVARNAME} && !-s $ENV{$BLASTDBVARNAME}.'/'.$db_file &&
!glob($ENV{$BLASTDBVARNAME}.'/'.$db_file."*") ))
{
die "# ERROR: cannot find BLAST database file $db_file\n"
}
# remove BLAST threads commands
$command = $raw_command;
$command =~ s/\-num_threads \d+//;
$command =~ s/\-a \d+//;
if(!$output_file)
{
my ($fh, $filename) = tempfile();
$output_file = $filename;
if($blastplusOK){ $command .= " -out $output_file " }
else{ $command .= " -o $output_file " } #print "$command\n";
}
else{ $outfileOK = 1 }
print "# parameters: max number of processors=$n_of_cores ".
"batchsize=$batchsize \$VERBOSE=$VERBOSE\n";
print "# raw BLAST command: $raw_command\n\n";
}
my $start_time = new Benchmark();
## parse sequences
my $fasta_ref = read_FASTA_file_array($input_seqfile);
if(!@$fasta_ref)
{
die "# ERROR: could not extract sequences from file $input_seqfile\n";
}
## split input in batches of max $BLASTBATCHSIZE sequences
my ($batch,$batch_command,$fasta_file,$blast_file,@batches,@tmpfiles);
my $lastseq = scalar(@$fasta_ref)-1;
my $total_seqs_batch = $batch = 0;
$fasta_file = $input_seqfile . $batch;
$blast_file = $input_seqfile . $batch . '.blast';
$batch_command = $command;
if($batch_command =~ /\-i (\S+)/){ $batch_command =~ s/-i $1/-i $fasta_file/ }
if($batch_command =~ /\-query (\S+)/){ $batch_command =~ s/-query $1/-query $fasta_file/ }
$batch_command =~ s/\Q$output_file\E/$blast_file/;
push(@batches,$batch_command);
push(@tmpfiles,[$fasta_file,$blast_file,$batch_command]);
open(BATCH,">$fasta_file") || die "# EXIT : cannot create batch file $fasta_file : $!\n";
foreach my $seq (0 .. $#{$fasta_ref})
{
$total_seqs_batch++;
print BATCH ">$fasta_ref->[$seq][NAME]\n$fasta_ref->[$seq][SEQ]\n";
if($seq == $lastseq || ($batchsize && $total_seqs_batch == $batchsize))
{
close(BATCH);
if($seq < $lastseq) # take care of remaining sequences/batches
{
$total_seqs_batch = 0;
$batch++;
$fasta_file = $input_seqfile . $batch;
$blast_file = $input_seqfile . $batch . '.blast';
$batch_command = $command;
if($batch_command =~ /\-i (\S+)/){ $batch_command =~ s/-i $1/-i $fasta_file/ }
if($batch_command =~ /\-query (\S+)/){ $batch_command =~ s/-query $1/-query $fasta_file/ }
$batch_command =~ s/\Q$output_file\E/$blast_file/;
push(@batches,$batch_command);
push(@tmpfiles,[$fasta_file,$blast_file,$batch_command]);
open(BATCH,">$fasta_file") ||
die "# ERROR : cannot create batch file $fasta_file : $!\n";
}
}
}
undef($fasta_ref);
## create requested number of threads
if($batch+1 < $n_of_cores)
{
$n_of_cores = $batch;
print "# WARNING: using only $n_of_cores cores\n";
}
my $pm = ForkManager->new($n_of_cores);
## submit batches to allocated threads
foreach $batch_command (@batches)
{
$pm->start($batch_command) and next; # fork
print "# running $batch_command in child process $$\n" if($VERBOSE);
open(BATCHJOB,"$batch_command 2>&1 |");
while(<BATCHJOB>)
{
if(/Error/){ print; last }
elsif($VERBOSE){ print }
}
close(BATCHJOB);
if($VERBOSE)
{
printf("# memory footprint of child process %d is %s\n",
$$,calc_memory_footprint($$));
}
$pm->finish(); # exit the child process
}
$pm->wait_all_children();
## put together BLAST results, no sort needed
unlink($output_file) if(-s $output_file && $outfileOK);
#open(OUTBLAST,">>$output_file") || die "# ERROR : cannot create output file $output_file : $!\n";# comment if using cat
foreach my $file (@tmpfiles)
{
($fasta_file,$blast_file,$batch_command) = @$file;
if(!-e $blast_file) # might be empty!
{
#close(OUTBLAST);# comment if using cat
unlink($output_file) if(-e $output_file);
die "# ERROR : did not produce BLAST file $blast_file ,".
" probably job failed: $batch_command\n";
}
else
{
print "# adding $blast_file results to $output_file\n" if($VERBOSE);
system("cat $blast_file >> $output_file"); # more efficient, less portable
#open(TMPBLAST,$blast_file) || die "# ERROR : cannot read file $blast_file : $!\n";# comment if using cat
#while(<TMPBLAST>){ print OUTBLAST }# comment if using cat
#close(TMPBLAST);# comment if using cat
unlink($fasta_file,$blast_file);
}
}
#close(OUTBLAST);# comment if using cat
if(!$outfileOK)
{
open(OUTBLAST,$output_file) || die "# ERROR : cannot read temporary outfile $output_file : $!\n";
while(<OUTBLAST>)
{
print;
}
close(OUTBLAST);
unlink($output_file);
}
my $end_time = new Benchmark();
print "\n# runtime: ".timestr(timediff($end_time,$start_time),'all')."\n";
if($VERBOSE)
{
printf("# memory footprint of parent process %d is %s\n",
$$,calc_memory_footprint($$));
}
sub calc_memory_footprint
{
my ($pid) = @_;
my $KB = 0;
my $ps = `ps -o rss,vsz $pid 2>&1`;
while($ps =~ /(\d+)/g){ $KB += $1 }
return sprintf("%3.1f MB",$KB/1024);
}