1#
2# BioPerl module for Bio::Tools::Run::StandAloneBlast
3#
4# Copyright Peter Schattner
5#
6# You may distribute this module under the same terms as perl itself
7
8# POD documentation - main docs before the code
9
10=head1 NAME
11
12Bio::Tools::Run::StandAloneWUBlast - Object for the local execution
13of WU-Blast.
14
15=head1 SYNOPSIS
16
17 # Do not use directly; use Bio::Tools::Run::StandAloneBlast
18
19=head1 DESCRIPTION
20
21See Bio::Tools::Run::StandAloneBlast
22
23=head1 FEEDBACK
24
25=head2 Mailing Lists
26
27User feedback is an integral part of the evolution of this and other
28Bioperl modules. Send your comments and suggestions preferably to one
29of the Bioperl mailing lists.  Your participation is much appreciated.
30
31  bioperl-l@bioperl.org                  - General discussion
32  http://bioperl.org/wiki/Mailing_lists  - About the mailing lists
33
34=head2 Support
35
36Please direct usage questions or support issues to the mailing list:
37
38I<bioperl-l@bioperl.org>
39
40rather than to the module maintainer directly. Many experienced and
41reponsive experts will be able look at the problem and quickly
42address it. Please include a thorough description of the problem
43with code and data examples if at all possible.
44
45=head2 Reporting Bugs
46
47Report bugs to the Bioperl bug tracking system to help us keep track
48the bugs and their resolution.  Bug reports can be submitted via
49the web:
50
51  https://github.com/bioperl/bioperl-live/issues
52
53=head1 AUTHOR - Peter Schattner
54
55Email schattner at alum.mit.edu
56
57=head1 MAINTAINER - Torsten Seemann
58
59Email torsten at infotech.monash.edu.au
60
61=head1 CONTRIBUTORS
62
63Sendu Bala  bix@sendu.me.uk (reimplementation)
64
65=head1 APPENDIX
66
67The rest of the documentation details each of the object
68methods. Internal methods are usually preceded with a _
69
70=cut
71
72package Bio::Tools::Run::StandAloneWUBlast;
73
74use strict;
75
76use base qw(Bio::Tools::Run::StandAloneBlast);
77
78our $AUTOLOAD;
79our $DEFAULTREADMETHOD = 'BLAST';
80
81# If local BLAST databases are not stored in the standard
82# /data directory, the variable BLASTDATADIR will need to be
83# set explicitly
84our $DATADIR = $Bio::Tools::Run::StandAloneBlast::DATADIR;
85
86our %GENERAL_PARAMS  = (i => 'input',
87                        o => 'outfile',
88                        p => 'program',
89                        d => 'database');
90our @WUBLAST_PARAMS  = qw(e s e2 s2 w t x m y z l k h v b q r
91    matrix filter wordmask filter maskextra  hitdist wink ctxfactor gape
92    gaps gape2 gaps2 gapw gapx olf golf  olmax golmax gapdecayrate
93    topcombon topcomboe sumstatsmethod hspsepqmax hspsepsmax gapsepqmax
94    gapsepsmax altscore hspmax gspmax qoffset nwstart nwlen qrecmin qrecmax
95    dbrecmin dbrecmax vdbdescmax dbchunks sort_by_pvalue  cpus putenv
96    getenv progress);
97our @WUBLAST_SWITCH = qw(kap sump poissonp lcfilter lcmask echofilter
98    stats nogap gapall pingpong nosegs postsw span2 span1 span prune
99    consistency links ucdb gi noseqs qtype qres sort_by_pvalue
100    sort_by_count sort_by_highscore sort_by_totalscore
101    sort_by_subjectlength mmio nonnegok novalidctxok shortqueryok notes
102    warnings errors endputenv getenv endgetenv abortonerror abortonfatal);
103
104our @OTHER_PARAMS = qw(_READMETHOD);
105
106
107=head2 new
108
109 Title   : new
110 Usage   : my $obj = Bio::Tools::Run::StandAloneBlast->new();
111 Function: Builds a newBio::Tools::Run::StandAloneBlast object
112 Returns : Bio::Tools::Run::StandAloneBlast
113 Args    : -quiet => boolean # make program execution quiet
114           -_READMETHOD => 'BLAST' (default, synonym 'SearchIO') || 'blast_pull'
115                           # the parsing method, case insensitive
116
117Essentially all BLAST parameters can be set via StandAloneBlast.pm.
118Some of the most commonly used parameters are listed below. All
119parameters have defaults and are optional except for -p.
120
121  -p Program Name [String]
122        Input should be one of "wublastp", "wublastn", "wublastx",
123        "wutblastn", or "wutblastx".
124  -d  Database [String] default = nr
125        The database specified must first be formatted with xdformat.
126  -E  Expectation value (E) [Real] default = 10.0
127  -o  BLAST report Output File [File Out]  Optional,
128	    default = ./blastreport.out ; set by StandAloneBlast.pm
129
130=cut
131
132sub new {
133    my ($caller, @args) = @_;
134    my $self = $caller->SUPER::new(@args);
135
136    $self->_set_from_args(\@args, -methods => {(map { $_ => $GENERAL_PARAMS{$_} } keys %GENERAL_PARAMS),
137                                               (map { $_ => $_ } (@OTHER_PARAMS,
138                                                                  @WUBLAST_PARAMS,
139                                                                  @WUBLAST_SWITCH))},
140                                  -create => 1,
141                                  -force => 1);
142
143    my ($tfh, $tempfile) = $self->io->tempfile();
144    my $outfile = $self->o || $self->outfile || $tempfile;
145    $self->o($outfile);
146    close($tfh);
147
148    $self->_READMETHOD($DEFAULTREADMETHOD) unless $self->_READMETHOD;
149
150    return $self;
151}
152
153# We let get/setter method names be case-insensitve
154sub AUTOLOAD {
155    my $self = shift;
156    my $attr = $AUTOLOAD;
157    $attr =~ s/.*:://;
158
159    my $orig = $attr;
160
161    $attr = lc($attr);
162
163    $self->can($attr) || $self->throw("Unallowed parameter: $orig !");
164
165    return $self->$attr(@_);
166}
167
168=head2  wublast
169
170 Title   : wublast
171 Usage   :  $blast_report = $factory->wublast('t/testquery.fa');
172	or
173	       $input = Bio::Seq->new(-id=>"test query",
174				      -seq=>"ACTACCCTTTAAATCAGTGGGGG");
175	       $blast_report = $factory->wublast($input);
176	or
177	      $seq_array_ref = \@seq_array;  # where @seq_array is an array of Bio::Seq objects
178	      $blast_report = $factory->wublast(\@seq_array);
179 Returns :  Reference to a Blast object
180 Args    : Name of a file or Bio::Seq object or an array of
181           Bio::Seq object containing the query sequence(s).
182           Throws an exception if argument is not either a string
183           (eg a filename) or a reference to a Bio::Seq object
184           (or to an array of Seq objects).  If argument is string,
185           throws exception if file corresponding to string name can
186           not be found.
187
188=cut
189
190sub wublast {
191    my ($self, $input1) = @_;
192    $self->io->_io_cleanup();
193    my $executable = 'wublast';
194
195    # Create input file pointer
196    my $infilename1 = $self->_setinput($executable, $input1) || $self->throw("$input1 not Bio::Seq object or array of Bio::Seq objects or file name!");
197    $self->i($infilename1);
198
199    my $blast_report = $self->_generic_local_wublast($executable);
200}
201
202=head2  _generic_local_wublast
203
204 Title   : _generic_local_wublast
205 Usage   :  internal function not called directly
206 Returns :  Blast object
207 Args    :   Reference to calling object and name of BLAST executable
208
209=cut
210
211sub _generic_local_wublast {
212    my $self = shift;
213    my $executable = shift;
214
215    # Create parameter string to pass to Blast program
216    my $param_string = $self->_setparams($executable);
217    $param_string = " ".$self->database." ".$self->input." ".$param_string;
218
219    # run Blast
220    my $blast_report = $self->_runwublast($executable, $param_string);
221}
222
223=head2  _runwublast
224
225 Title   :  _runwublast
226 Usage   :  Internal function, not to be called directly
227 Function:   makes actual system call to WU-Blast program
228 Example :
229 Returns : Report Blast object
230 Args    : Reference to calling object, name of BLAST executable,
231           and parameter string for executable
232
233=cut
234
235sub _runwublast {
236	my ($self, $executable, $param_string) = @_;
237	my ($blast_obj, $exe);
238	if (! ($exe = $self->executable($self->p))){
239        $self->warn("cannot find path to $executable");
240        return;
241	}
242
243    # Use double quotes if executable path have empty spaces
244    if ($exe =~ m/ /) {
245        $exe = "\"$exe\"";
246    }
247	my $commandstring = $exe.$param_string;
248
249	$self->debug("$commandstring\n");
250	system($commandstring) && $self->throw("$executable call crashed: $? | $! | $commandstring\n");
251
252    # get outputfilename
253	my $outfile = $self->o();
254	$blast_obj = Bio::SearchIO->new(-file => $outfile, -format => 'blast');
255
256	return $blast_obj;
257}
258
259=head2  _setparams
260
261 Title   : _setparams
262 Usage   : Internal function, not to be called directly
263 Function: Create parameter inputs for Blast program
264 Example :
265 Returns : parameter string to be passed to Blast
266 Args    : Reference to calling object and name of BLAST executable
267
268=cut
269
270sub _setparams {
271    my ($self, $executable) = @_;
272    my ($attr, $value, @execparams);
273
274    @execparams = @WUBLAST_PARAMS;
275
276    # of the general params, wublast only takes outfile at
277    # this stage (we add in program, input and database manually elsewhere)
278    push(@execparams, 'o');
279
280    # workaround for problems with shell metacharacters [bug 2707]
281    # simply quoting does not always work!
282    # Fixed so Windows files are not quotemeta'd
283    my $tmp = $self->o;
284    $self->o(quotemeta($tmp)) if ($tmp && $^O !~ /^MSWin/);
285
286    my $param_string = $self->SUPER::_setparams(-params => [@execparams],
287                                                -switches => \@WUBLAST_SWITCH,
288                                                -dash => 1);
289
290    $self->o($tmp) if ($tmp && $^O !~ /^MSWin/);
291
292    if ($self->quiet()) {
293        $param_string .= ' 2> '.File::Spec->devnull;
294    }
295
296    return $param_string;
297}
298
2991;
300