2 # BioPerl module for Bio::Tools::Run::StandAloneBlast
4 # Copyright Peter Schattner
6 # You may distribute this module under the same terms as perl itself
8 # POD documentation - main docs before the code
12 Bio::Tools::Run::StandAloneNCBIBlast - Object for the local execution
13 of the NCBI BLAST program suite (blastall, blastpgp, bl2seq). With
14 experimental support for NCBI rpsblast.
18 # Do not use directly; see Bio::Tools::Run::StandAloneBlast
22 See Bio::Tools::Run::StandAloneBlast
28 User feedback is an integral part of the evolution of this and other
29 Bioperl modules. Send your comments and suggestions preferably to one
30 of the Bioperl mailing lists. Your participation is much appreciated.
32 bioperl-l@bioperl.org - General discussion
33 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
37 Please direct usage questions or support issues to the mailing list:
39 I<bioperl-l@bioperl.org>
41 rather than to the module maintainer directly. Many experienced and
42 reponsive experts will be able look at the problem and quickly
43 address it. Please include a thorough description of the problem
44 with code and data examples if at all possible.
48 Report bugs to the Bioperl bug tracking system to help us keep track
49 the bugs and their resolution. Bug reports can be submitted via
52 https://redmine.open-bio.org/projects/bioperl/
54 =head1 AUTHOR - Peter Schattner
56 Email schattner at alum.mit.edu
58 =head1 MAINTAINER - Torsten Seemann
60 Email torsten at infotech.monash.edu.au
64 Sendu Bala bix@sendu.me.uk (reimplementation)
68 The rest of the documentation details each of the object
69 methods. Internal methods are usually preceded with a _
73 package Bio
::Tools
::Run
::StandAloneNCBIBlast
;
78 use base
qw(Bio::Tools::Run::StandAloneBlast);
81 our $DEFAULTREADMETHOD = 'BLAST';
83 # If local BLAST databases are not stored in the standard
84 # /data directory, the variable BLASTDATADIR will need to be
86 our $DATADIR = $Bio::Tools
::Run
::StandAloneBlast
::DATADIR
;
88 our %GENERAL_PARAMS = (i
=> 'input',
92 our @BLASTALL_PARAMS = qw(A B C D E F G K L M O P Q R S W X Y Z a b e f l m q r t v w y z n);
93 our @BLASTALL_SWITCH = qw(I g J T U n V s);
94 our @BLASTPGP_PARAMS = qw(A B C E F G H I J K L M N O P Q R S T U W X Y Z a b c e f h j k l m q s t u v y z);
95 our @RPSBLAST_PARAMS = qw(F I J L N O P T U V X Y Z a b e l m v y z);
96 our @BL2SEQ_PARAMS = qw(A D E F G I J M S T U V W X Y a e g j m q r t);
98 our @OTHER_PARAMS = qw(_READMETHOD);
104 Usage : my $obj = Bio::Tools::Run::StandAloneBlast->new();
105 Function: Builds a newBio::Tools::Run::StandAloneBlast object
106 Returns : Bio::Tools::Run::StandAloneBlast
107 Args : -quiet => boolean # make program execution quiet
108 -_READMETHOD => 'BLAST' (default, synonym 'SearchIO') || 'blast_pull'
109 # the parsing method, case insensitive
111 Essentially all BLAST parameters can be set via StandAloneBlast.pm.
112 Some of the most commonly used parameters are listed below. All
113 parameters have defaults and are optional except for -p in those programs that
114 have it. For a complete listing of settable parameters, run the relevant
115 executable BLAST program with the option "-" as in blastall -
116 Note that the input parameters (-i, -j, -input) should not be set directly by
117 you: this module sets them when you call one of the executable methods.
121 -p Program Name [String]
122 Input should be one of "blastp", "blastn", "blastx",
123 "tblastn", or "tblastx".
124 -d Database [String] default = nr
125 The database specified must first be formatted with formatdb.
126 Multiple database names (bracketed by quotations) will be accepted.
127 An example would be -d "nr est"
128 -e Expectation value (E) [Real] default = 10.0
129 -o BLAST report Output File [File Out] Optional,
130 default = ./blastreport.out ; set by StandAloneBlast.pm
131 -S Query strands to search against database (for blast[nx], and tblastx). 3 is both, 1 is top, 2 is bottom [Integer]
134 Blastpgp (including Psiblast)
136 -j is the maximum number of rounds (default 1; i.e., regular BLAST)
137 -h is the e-value threshold for including sequences in the
138 score matrix model (default 0.001)
139 -c is the "constant" used in the pseudocount formula specified in the paper (default 10)
140 -B Multiple alignment file for PSI-BLAST "jump start mode" Optional
141 -Q Output File for PSI-BLAST Matrix in ASCII [File Out] Optional
145 -d Database [String] default = (none - you must specify a database)
146 The database specified must first be formatted with formatdb.
147 Multiple database names (bracketed by quotations) will be accepted.
148 An example would be -d "Cog Smart"
149 -e Expectation value (E) [Real] default = 10.0
150 -o BLAST report Output File [File Out] Optional,
151 default = ./blastreport.out ; set by StandAloneBlast.pm
155 -p Program name: blastp, blastn, blastx. For blastx 1st argument should be nucleotide [String]
157 -o alignment output file [File Out] default = stdout
158 -e Expectation value (E) [Real] default = 10.0
159 -S Query strands to search against database (blastn only). 3 is both, 1 is top, 2 is bottom [Integer]
165 my ($caller, @args) = @_;
166 my $self = $caller->SUPER::new
(@args);
168 # StandAloneBlast is special in that "one can modify the name of
169 # the (ncbi) BLAST parameters as desired as long as the initial letter (and
170 # case) of the parameter are preserved". We handle this by truncating input
171 # args to their first char
174 while (my ($attr, $value) = each %args) {
176 $attr = substr($attr, 0, 1) unless $attr =~ /^_/;
177 push(@args, $attr, $value);
180 $self->_set_from_args(\
@args, -methods
=> {(map { $_ => $GENERAL_PARAMS{$_} } keys %GENERAL_PARAMS),
181 (map { $_ => $_ } (@OTHER_PARAMS,
187 -code
=> { map { $_ => 'my $self = shift;
190 if ($value && $value ne \'F\') {
196 $self->{\'_\'.$method} = $value;
198 return $self->{\'_\'.$method} || return;' } @BLASTALL_SWITCH }, # these methods can take boolean or 'T' and 'F'
201 -case_sensitive
=> 1);
203 my ($tfh, $tempfile) = $self->io->tempfile();
204 my $outfile = $self->o || $self->outfile || $tempfile;
208 $self->_READMETHOD($DEFAULTREADMETHOD) unless $self->_READMETHOD;
213 # StandAloneBlast is special in that "one can modify the name of
214 # the (ncbi) BLAST parameters as desired as long as the initial letter (and
215 # case) of the parameter are preserved". We handle this with AUTOLOAD
216 # redirecting to the automatically created methods from _set_from_args() !
219 my $attr = $AUTOLOAD;
224 $attr = substr($attr, 0, 1);
226 $self->can($attr) || $self->throw("Unallowed parameter: $orig !");
228 return $self->$attr(@_);
234 Usage : $blast_report = $factory->blastall('t/testquery.fa');
236 $input = Bio::Seq->new(-id=>"test query",
237 -seq=>"ACTACCCTTTAAATCAGTGGGGG");
238 $blast_report = $factory->blastall($input);
240 $seq_array_ref = \@seq_array;
241 # where @seq_array is an array of Bio::Seq objects
242 $blast_report = $factory->blastall($seq_array_ref);
243 Returns : Reference to a Blast object containing the blast report.
244 Args : Name of a file or Bio::Seq object or an array of
245 Bio::Seq object containing the query sequence(s).
246 Throws an exception if argument is not either a string
247 (eg a filename) or a reference to a Bio::Seq object
248 (or to an array of Seq objects). If argument is string,
249 throws exception if file corresponding to string name can
255 my ($self, $input1) = @_;
256 $self->io->_io_cleanup();
257 my $executable = 'blastall';
259 # Create input file pointer
260 my $infilename1 = $self->_setinput($executable, $input1) || $self->throw("$input1 not Bio::Seq object or array of Bio::Seq objects or file name!");
261 $self->i($infilename1);
263 my $blast_report = $self->_generic_local_blast($executable);
269 Usage : $blast_report = $factory-> blastpgp('t/testquery.fa');
271 $input = Bio::Seq->new(-id=>"test query",
272 -seq=>"ACTADDEEQQPPTCADEEQQQVVGG");
273 $blast_report = $factory->blastpgp ($input);
275 $seq_array_ref = \@seq_array;
276 # where @seq_array is an array of Bio::Seq objects
277 $blast_report = $factory-> blastpgp(\@seq_array);
278 Returns : Reference to a Bio::SearchIO object containing the blast report
279 Args : Name of a file or Bio::Seq object. In psiblast jumpstart
280 mode two additional arguments are required: a SimpleAlign
281 object one of whose elements is the query and a "mask" to
282 determine how BLAST should select scoring matrices see
283 DESCRIPTION above for more details.
285 Throws an exception if argument is not either a string
286 (eg a filename) or a reference to a Bio::Seq object
287 (or to an array of Seq objects). If argument is string,
288 throws exception if file corresponding to string name can
290 Returns : Reference to Bio::SearchIO object containing the blast report.
296 my $executable = 'blastpgp';
299 # used by blastpgp's -B option to specify which
300 # residues are position aligned
303 my ($infilename1, $infilename2 ) = $self->_setinput($executable,
306 if (!$infilename1) {$self->throw("$input1 not Bio::Seq object or array of Bio::Seq objects or file name!");}
307 $self->i($infilename1); # set file name of sequence to be blasted to inputfilename1 (-i param of blastpgp)
309 unless ($infilename2) {$self->throw("$input2 not SimpleAlign Object in pre-aligned psiblast\n");}
310 $self->B($infilename2); # set file name of partial alignment to inputfilename2 (-B param of blastpgp)
313 my $blast_report = $self->_generic_local_blast($executable);
319 Usage : $blast_report = $factory->rpsblast('t/testquery.fa');
321 $input = Bio::Seq->new(-id=>"test query",
322 -seq=>"MVVLCRADDEEQQPPTCADEEQQQVVGG");
323 $blast_report = $factory->rpsblast($input);
325 $seq_array_ref = \@seq_array;
326 # where @seq_array is an array of Bio::Seq objects
327 $blast_report = $factory->rpsblast(\@seq_array);
328 Args : Name of a file or Bio::Seq object or an array of
329 Bio::Seq object containing the query sequence(s).
330 Throws an exception if argument is not either a string
331 (eg a filename) or a reference to a Bio::Seq object
332 (or to an array of Seq objects). If argument is string,
333 throws exception if file corresponding to string name can
335 Returns : Reference to a Bio::SearchIO object containing the blast report
340 my ($self, $input1) = @_;
341 $self->io->_io_cleanup();
342 my $executable = 'rpsblast';
344 # Create input file pointer
345 my $infilename1 = $self->_setinput($executable, $input1) || $self->throw("$input1 not Bio::Seq object or array of Bio::Seq objects or file name!");
346 $self->i($infilename1);
348 my $blast_report = $self->_generic_local_blast($executable);
354 Usage : $factory-> bl2seq('t/seq1.fa', 't/seq2.fa');
356 $input1 = Bio::Seq->new(-id=>"test query1",
357 -seq=>"ACTADDEEQQPPTCADEEQQQVVGG");
358 $input2 = Bio::Seq->new(-id=>"test query2",
359 -seq=>"ACTADDEMMMMMMMDEEQQQVVGG");
360 $blast_report = $factory->bl2seq ($input1, $input2);
361 Returns : Reference to a BPbl2seq object containing the blast report.
362 Args : Names of 2 files or 2 Bio::Seq objects containing the
363 sequences to be aligned by bl2seq.
365 Throws an exception if argument is not either a pair of
366 strings (eg filenames) or references to Bio::Seq objects.
367 If arguments are strings, throws exception if files
368 corresponding to string names can not be found.
374 my $executable = 'bl2seq';
378 # Create input file pointer
379 my ($infilename1, $infilename2 ) = $self->_setinput($executable,
381 if (!$infilename1){$self->throw(" $input1 not Seq Object or file name!");}
382 if (!$infilename2){$self->throw("$input2 not Seq Object or file name!");}
384 $self->i($infilename1); # set file name of first sequence to
385 # be aligned to inputfilename1
386 # (-i param of bl2seq)
387 $self->j($infilename2); # set file name of first sequence to
388 # be aligned to inputfilename2
389 # (-j param of bl2seq)
391 my $blast_report = $self->_generic_local_blast($executable);
394 =head2 _generic_local_blast
396 Title : _generic_local_blast
397 Usage : internal function not called directly
398 Returns : Bio::SearchIO
399 Args : Reference to calling object and name of BLAST executable
403 sub _generic_local_blast
{
405 my $executable = shift;
407 # Create parameter string to pass to Blast program
408 my $param_string = $self->_setparams($executable);
411 my $blast_report = $self->_runblast($executable, $param_string);
417 Usage : Internal function, not to be called directly
418 Function: makes actual system call to Blast program
420 Returns : Report Bio::SearchIO object in the appropriate format
421 Args : Reference to calling object, name of BLAST executable,
422 and parameter string for executable
427 my ($self, $executable, $param_string) = @_;
428 my ($blast_obj, $exe);
429 if (! ($exe = $self->executable($executable)) ) {
430 $self->warn("cannot find path to $executable");
434 # Use double quotes if executable path have empty spaces
438 my $commandstring = $exe.$param_string;
440 $self->debug("$commandstring\n");
441 system($commandstring) && $self->throw("$executable call crashed: $? | $! | $commandstring\n");
443 # set significance cutoff to set expectation value or default value
444 # (may want to make this value vary for different executables)
445 my $signif = $self->e() || 1e-5;
448 my $outfile = $self->o();
450 # this should allow any blast SearchIO parser (not just 'blast_pull' or 'blast',
451 # but 'blastxml' and 'blasttable'). Fall back to 'blast' if not stipulated.
452 my $method = $self->_READMETHOD;
453 if ($method =~ /^(?:blast|SearchIO)/i ) {
454 $method = 'blast' if $method =~ m{SearchIO}i;
455 $blast_obj = Bio
::SearchIO
->new(-file
=> $outfile,
458 # should these be here? They have been deprecated...
459 elsif ($method =~ /BPlite/i ) {
460 if ($executable =~ /bl2seq/i) {
461 # Added program info so BPbl2seq can compute strand info
462 $self->throw("Use of Bio::Tools::BPbl2seq is deprecated; use Bio::SearchIO modules instead");
464 elsif ($executable =~ /blastpgp/i && defined $self->j() && $self->j() > 1) {
465 $self->throw("Use of Bio::Tools::BPpsilite is deprecated; use Bio::SearchIO modules instead");
467 elsif ($executable =~ /blastall|rpsblast/i) {
468 $self->throw("Use of Bio::Tools::BPlite is deprecated; use Bio::SearchIO modules instead");
471 $self->warn("Unrecognized executable $executable");
475 $self->warn("Unrecognized readmethod $method");
484 Usage : Internal function, not to be called directly
485 Function: Create parameter inputs for Blast program
487 Returns : parameter string to be passed to Blast
488 Args : Reference to calling object and name of BLAST executable
493 my ($self, $executable) = @_;
494 my ($attr, $value, @execparams);
496 if ($executable eq 'blastall') { @execparams = (@BLASTALL_PARAMS,
498 elsif ($executable eq 'blastpgp') { @execparams = @BLASTPGP_PARAMS; }
499 elsif ($executable eq 'rpsblast') { @execparams = @RPSBLAST_PARAMS; }
500 elsif ($executable eq 'bl2seq' ) { @execparams = @BL2SEQ_PARAMS; }
502 # we also have all the general params
503 push(@execparams, keys %GENERAL_PARAMS);
505 my $database = $self->d;
506 if ($database && $executable ne 'bl2seq') {
507 # Need to prepend datadirectory to database name
508 my @dbs = split(/ /, $database);
509 for my $i (0..$#dbs) {
510 # (works with multiple databases)
511 if (! (-e
$dbs[$i].".nin" || -e
$dbs[$i].".pin") &&
512 ! (-e
$dbs[$i].".nal" || -e
$dbs[$i].".pal") ) {
513 $dbs[$i] = File
::Spec
->catdir($DATADIR, $dbs[$i]);
516 $self->d('"'.join(" ", @dbs).'"');
519 # workaround for problems with shell metacharacters [bug 2707]
520 # simply quoting does not always work!
522 $self->o(quotemeta($tmp)) if ($tmp && $^O
!~ /^MSWin/);
524 my $param_string = $self->SUPER::_setparams
(-params
=> [@execparams],
527 $self->o($tmp) if ($tmp && $^O
!~ /^MSWin/);
529 $self->d($database) if $database;
531 if ($self->quiet()) {
532 $param_string .= ' 2> '.File
::Spec
->devnull;
535 return $param_string;