sync trunk with branch
[bioperl-live.git] / Bio / Tools / Run / StandAloneWUBlast.pm
blob5173921e4427a037d2aeda0b3e8bac54cd024902
1 # $Id$
3 # BioPerl module for Bio::Tools::Run::StandAloneBlast
5 # Copyright Peter Schattner
7 # You may distribute this module under the same terms as perl itself
9 # POD documentation - main docs before the code
11 =head1 NAME
13 Bio::Tools::Run::StandAloneWUBlast - Object for the local execution
14 of WU-Blast.
16 =head1 SYNOPSIS
18 # Do not use directly; use Bio::Tools::Run::StandAloneBlast
20 =head1 DESCRIPTION
22 See Bio::Tools::Run::StandAloneBlast
24 =head1 FEEDBACK
26 =head2 Mailing Lists
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
35 =head2 Reporting Bugs
37 Report bugs to the Bioperl bug tracking system to help us keep track
38 the bugs and their resolution. Bug reports can be submitted via
39 the web:
41 http://bugzilla.open-bio.org/
43 =head1 AUTHOR - Peter Schattner
45 Email schattner at alum.mit.edu
47 =head1 MAINTAINER - Torsten Seemann
49 Email torsten at infotech.monash.edu.au
51 =head1 CONTRIBUTORS
53 Sendu Bala bix@sendu.me.uk (reimplementation)
55 =head1 APPENDIX
57 The rest of the documentation details each of the object
58 methods. Internal methods are usually preceded with a _
60 =cut
62 package Bio::Tools::Run::StandAloneWUBlast;
64 use strict;
66 use base qw(Bio::Tools::Run::StandAloneBlast);
68 our $AUTOLOAD;
69 our $DEFAULTREADMETHOD = 'BLAST';
71 # If local BLAST databases are not stored in the standard
72 # /data directory, the variable BLASTDATADIR will need to be
73 # set explicitly
74 our $DATADIR = $Bio::Tools::Run::StandAloneBlast::DATADIR;
76 our %GENERAL_PARAMS = (i => 'input',
77 o => 'outfile',
78 p => 'program',
79 d => 'database');
80 our @WUBLAST_PARAMS = qw(e s e2 s2 w t x m y z l k h v b q r
81 matrix filter wordmask filter maskextra hitdist wink ctxfactor gape
82 gaps gape2 gaps2 gapw gapx olf golf olmax golmax gapdecayrate
83 topcombon topcomboe sumstatsmethod hspsepqmax hspsepsmax gapsepqmax
84 gapsepsmax altscore hspmax gspmax qoffset nwstart nwlen qrecmin qrecmax
85 dbrecmin dbrecmax vdbdescmax dbchunks sort_by_pvalue cpus putenv
86 getenv progress);
87 our @WUBLAST_SWITCH = qw(kap sump poissonp lcfilter lcmask echofilter
88 stats nogap gapall pingpong nosegs postsw span2 span1 span prune
89 consistency links ucdb gi noseqs qtype qres sort_by_pvalue
90 sort_by_count sort_by_highscore sort_by_totalscore
91 sort_by_subjectlength mmio nonnegok novalidctxok shortqueryok notes
92 warnings errors endputenv getenv endgetenv abortonerror abortonfatal);
94 our @OTHER_PARAMS = qw(_READMETHOD);
97 =head2 new
99 Title : new
100 Usage : my $obj = Bio::Tools::Run::StandAloneBlast->new();
101 Function: Builds a newBio::Tools::Run::StandAloneBlast object
102 Returns : Bio::Tools::Run::StandAloneBlast
103 Args : -quiet => boolean # make program execution quiet
104 -_READMETHOD => 'BLAST' (default, synonym 'SearchIO') || 'blast_pull'
105 # the parsing method, case insensitive
107 Essentially all BLAST parameters can be set via StandAloneBlast.pm.
108 Some of the most commonly used parameters are listed below. All
109 parameters have defaults and are optional except for -p.
111 -p Program Name [String]
112 Input should be one of "wublastp", "wublastn", "wublastx",
113 "wutblastn", or "wutblastx".
114 -d Database [String] default = nr
115 The database specified must first be formatted with xdformat.
116 -E Expectation value (E) [Real] default = 10.0
117 -o BLAST report Output File [File Out] Optional,
118 default = ./blastreport.out ; set by StandAloneBlast.pm
120 =cut
122 sub new {
123 my ($caller, @args) = @_;
124 my $self = $caller->SUPER::new(@args);
126 $self->_set_from_args(\@args, -methods => {(map { $_ => $GENERAL_PARAMS{$_} } keys %GENERAL_PARAMS),
127 (map { $_ => $_ } (@OTHER_PARAMS,
128 @WUBLAST_PARAMS,
129 @WUBLAST_SWITCH))},
130 -create => 1,
131 -force => 1);
133 my ($tfh, $tempfile) = $self->io->tempfile();
134 my $outfile = $self->o || $self->outfile || $tempfile;
135 $self->o($outfile);
136 close($tfh);
138 $self->_READMETHOD($DEFAULTREADMETHOD) unless $self->_READMETHOD;
140 return $self;
143 # We let get/setter method names be case-insensitve
144 sub AUTOLOAD {
145 my $self = shift;
146 my $attr = $AUTOLOAD;
147 $attr =~ s/.*:://;
149 my $orig = $attr;
151 $attr = lc($attr);
153 $self->can($attr) || $self->throw("Unallowed parameter: $orig !");
155 return $self->$attr(@_);
158 =head2 wublast
160 Title : wublast
161 Usage : $blast_report = $factory->wublast('t/testquery.fa');
163 $input = Bio::Seq->new(-id=>"test query",
164 -seq=>"ACTACCCTTTAAATCAGTGGGGG");
165 $blast_report = $factory->wublast($input);
167 $seq_array_ref = \@seq_array; # where @seq_array is an array of Bio::Seq objects
168 $blast_report = $factory->wublast(\@seq_array);
169 Returns : Reference to a Blast object
170 Args : Name of a file or Bio::Seq object or an array of
171 Bio::Seq object containing the query sequence(s).
172 Throws an exception if argument is not either a string
173 (eg a filename) or a reference to a Bio::Seq object
174 (or to an array of Seq objects). If argument is string,
175 throws exception if file corresponding to string name can
176 not be found.
178 =cut
180 sub wublast {
181 my ($self, $input1) = @_;
182 $self->io->_io_cleanup();
183 my $executable = 'wublast';
185 # Create input file pointer
186 my $infilename1 = $self->_setinput($executable, $input1) || $self->throw("$input1 not Bio::Seq object or array of Bio::Seq objects or file name!");
187 $self->i($infilename1);
189 my $blast_report = $self->_generic_local_wublast($executable);
192 =head2 _generic_local_wublast
194 Title : _generic_local_wublast
195 Usage : internal function not called directly
196 Returns : Blast object
197 Args : Reference to calling object and name of BLAST executable
199 =cut
201 sub _generic_local_wublast {
202 my $self = shift;
203 my $executable = shift;
205 # Create parameter string to pass to Blast program
206 my $param_string = $self->_setparams($executable);
207 $param_string = " ".$self->database." ".$self->input." ".$param_string;
209 # run Blast
210 my $blast_report = $self->_runwublast($executable, $param_string);
213 =head2 _runwublast
215 Title : _runwublast
216 Usage : Internal function, not to be called directly
217 Function: makes actual system call to WU-Blast program
218 Example :
219 Returns : Report Blast object
220 Args : Reference to calling object, name of BLAST executable,
221 and parameter string for executable
223 =cut
225 sub _runwublast {
226 my ($self, $executable, $param_string) = @_;
227 my ($blast_obj, $exe);
228 if (! ($exe = $self->executable($self->p))){
229 $self->warn("cannot find path to $executable");
230 return;
233 my $commandstring = $exe.$param_string;
235 $self->debug("$commandstring\n");
236 system($commandstring) && $self->throw("$executable call crashed: $? | $! | $commandstring\n");
238 # get outputfilename
239 my $outfile = $self->o();
240 $blast_obj = Bio::SearchIO->new(-file => $outfile, -format => 'blast');
242 return $blast_obj;
245 =head2 _setparams
247 Title : _setparams
248 Usage : Internal function, not to be called directly
249 Function: Create parameter inputs for Blast program
250 Example :
251 Returns : parameter string to be passed to Blast
252 Args : Reference to calling object and name of BLAST executable
254 =cut
256 sub _setparams {
257 my ($self, $executable) = @_;
258 my ($attr, $value, @execparams);
260 @execparams = @WUBLAST_PARAMS;
262 # of the general params, wublast only takes outfile at
263 # this stage (we add in program, input and database manually elsewhere)
264 push(@execparams, 'o');
266 # workaround for problems with shell metacharacters [bug 2707]
267 # simply quoting does not always work!
268 my $tmp = $self->o;
269 $self->o(quotemeta($tmp)) if $tmp;
271 my $param_string = $self->SUPER::_setparams(-params => [@execparams],
272 -switches => \@WUBLAST_SWITCH,
273 -dash => 1);
275 $self->o($tmp) if $tmp;
277 if ($self->quiet()) {
278 $param_string .= ' 2> '.File::Spec->devnull;
281 return $param_string;