File Coverage

blib/lib/Bio/Tools/Run/StandAloneBlastPlus/BlastMethods.pm
Criterion Covered Total %
statement 24 110 21.8
branch 0 46 0.0
condition 0 12 0.0
subroutine 8 14 57.1
pod 0 5 0.0
total 32 187 17.1


line stmt bran cond sub pod time code
1             #
2             # BioPerl module for Bio::Tools::Run::StandAloneBlastPlus::BlastMethods
3             #
4             # Please direct questions and support issues to
5             #
6             # Cared for by Mark A. Jensen
7             #
8             # Copyright Mark A. Jensen
9             #
10             # You may distribute this module under the same terms as perl itself
11              
12             # POD documentation - main docs before the code
13              
14             =head1 NAME
15              
16             Bio::Tools::Run::StandAloneBlastPlus::BlastMethods - Provides BLAST methods to StandAloneBlastPlus
17              
18             =head1 SYNOPSIS
19              
20             # create a factory:
21             $fac = Bio::Tools::Run::StandAloneBlastPlus->new(
22             -db_name => 'testdb'
23             );
24             # get your results
25             $result = $fac->blastn( -query => 'query_seqs.fas',
26             -outfile => 'query.bls',
27             -method_args => [ '-num_alignments' => 10 ] );
28              
29             $result = $fac->tblastx( -query => $an_alignment_object,
30             -outfile => 'query.bls',
31             -outformat => 7 );
32             # do a bl2seq
33             $fac->bl2seq( -method => 'blastp',
34             -query => $seq_object_1,
35             -subject => $seq_object_2 );
36              
37             =head1 DESCRIPTION
38              
39             This module provides the BLAST methods (blastn, blastp, psiblast,
40             etc.) to the L object.
41              
42             =head1 USAGE
43              
44             This POD describes the use of BLAST methods against a
45             L factory object. The object
46             itself has extensive facilities for creating, formatting, and masking
47             BLAST databases; please refer to
48             L POD for these details.
49              
50             Given a C factory, such as
51              
52             $fac = Bio::Tools::Run::StandAloneBlastPlus->new(
53             -db_name => 'testdb'
54             );
55              
56             you can run the desired BLAST method directly from the factory object,
57             against the database currently attached to the factory (in the
58             example, C). C<-query> is a required argument:
59              
60             $result = $fac->blastn( -query => 'query_seqs.fas' );
61              
62             Here, C<$result> is a L object.
63              
64             Other details:
65              
66             =over
67              
68             =item * The blast output file can be named explicitly:
69              
70             $result = $fac->blastn( -query => 'query_seqs.fas',
71             -outfile => 'query.bls' );
72              
73             =item * The output format can be specified:
74              
75             $result = $fac->blastn( -query => 'query_seqs.fas',
76             -outfile => 'query.bls',
77             -outformat => 7 ); #tabular
78              
79             =item * Additional arguments to the method can be specified:
80              
81             $result = $fac->blastn( -query => 'query_seqs.fas',
82             -outfile => 'query.bls',
83             -method_args => [ '-num_alignments' => 10 ,
84             '-evalue' => 100 ]);
85              
86             =item * HTML output can be created using this workaround:
87              
88             $result = $fac->blastn( -query => 'query_seqs.fas',
89             -outfile => 'query.bls',
90             -method_args => [ -html => ' ' );
91              
92             =item * To get the name of the blast output file, do
93              
94             $file = $fac->blast_out;
95              
96             =item * To clean up the temp files (you must do this explicitly):
97              
98             $fac->cleanup;
99              
100             =back
101              
102             =head2 bl2seq()
103              
104             Running C is similar, but both C<-query> and C<-subject> are
105             required, and the attached database is ignored. The blast method must
106             be specified explicitly with the C<-method> parameter:
107              
108             $fac->bl2seq( -method => 'blastp',
109             -query => $seq_object_1,
110             -subject => $seq_object_2 );
111              
112             Other parameters ( C<-method_args>, C<-outfile>, and C<-outformat> ) are valid.
113              
114             =head2 Return values
115              
116             The return value is always a L
117             object on success, undef on failure.
118              
119             =head1 SEE ALSO
120              
121             L, L
122              
123             =head1 FEEDBACK
124              
125             =head2 Mailing Lists
126              
127             User feedback is an integral part of the evolution of this and other
128             Bioperl modules. Send your comments and suggestions preferably to
129             the Bioperl mailing list. Your participation is much appreciated.
130              
131             bioperl-l@bioperl.org - General discussion
132             http://bioperl.org/wiki/Mailing_lists - About the mailing lists
133              
134             =head2 Support
135              
136             Please direct usage questions or support issues to the mailing list:
137              
138             L
139              
140             rather than to the module maintainer directly. Many experienced and
141             reponsive experts will be able look at the problem and quickly
142             address it. Please include a thorough description of the problem
143             with code and data examples if at all possible.
144              
145             =head2 Reporting Bugs
146              
147             Report bugs to the Bioperl bug tracking system to help us keep track
148             of the bugs and their resolution. Bug reports can be submitted via
149             the web:
150              
151             http://redmine.open-bio.org/projects/bioperl/
152              
153             =head1 AUTHOR - Mark A. Jensen
154              
155             Email maj -at- fortinbras -dot- us
156              
157             Describe contact details here
158              
159             =head1 CONTRIBUTORS
160              
161             Additional contributors names and emails here
162              
163             =head1 APPENDIX
164              
165             The rest of the documentation details each of the object methods.
166             Internal methods are usually preceded with a _
167              
168             =cut
169              
170             # Let the code begin...
171              
172             # note: providing methods directly to the namespace...
173             package Bio::Tools::Run::StandAloneBlastPlus;
174 1     1   6 use strict;
  1         2  
  1         24  
175 1     1   5 use warnings;
  1         2  
  1         20  
176              
177 1     1   290 use Bio::SearchIO;
  1         6959  
  1         28  
178 1     1   6 use lib '../../../..';
  1         2  
  1         5  
179 1     1   664 use Bio::Tools::Run::BlastPlus;
  1         4  
  1         29  
180 1     1   6 use File::Temp;
  1         2  
  1         66  
181 1     1   5 use File::Copy;
  1         2  
  1         37  
182 1     1   5 use File::Spec;
  1         2  
  1         820  
183             our @BlastMethods = qw( blastp blastn blastx tblastn tblastx
184             psiblast rpsblast rpstblastn );
185              
186             =head2 run()
187              
188             Title : run
189             Usage :
190             Function: Query the attached database using a specified blast
191             method
192             Returns : Bio::Search::Result::BlastResult object
193             Args : key => value:
194             -method => $method [blastp|blastn|blastx|tblastx|tblastn|
195             rpsblast|psiblast|rpstblastn]
196             -query => $query_sequences (a fasta file name or BioPerl sequence
197             object or sequence collection object)
198             -outfile => $blast_report_file (optional: default creates a tempfile)
199             -outformat => $format_code (integer in [0..10], see blast+ docs)
200             -method_args => [ -key1 => $value1, ... ] (additional arguments
201             for the given method)
202              
203             =cut
204              
205             sub run {
206 0     0 0   my $self = shift;
207 0           my @args = @_;
208 0           my ( $method, $query, $outfile, $outformat, $method_args )
209             = $self->_rearrange(
210             [ qw(
211             METHOD
212             QUERY
213             OUTFILE
214             OUTFORMAT
215             METHOD_ARGS
216             )
217             ],
218             @args
219             );
220 0           my $ret;
221 0           my ( %blast_args, %usr_args );
222              
223 0 0         unless ($method) {
224 0           $self->throw("Blast run: method not specified, use -method");
225             }
226 0 0         unless ($query) {
227 0           $self->throw("Blast run: query data required, use -query");
228             }
229 0 0         unless ($outfile) { # create a tempfile name
230 0           my $fh = File::Temp->new(
231             TEMPLATE => 'BLOXXXXX',
232             DIR => $self->db_dir,
233             UNLINK => 0
234             );
235 0           $outfile = $fh->filename;
236 0           $fh->close;
237 0           $self->_register_temp_for_cleanup($outfile);
238             }
239              
240 0 0         if ($outformat) {
241 0 0         unless ( $outformat =~ /^"?[0-9]{1,2}/ ) {
242 0           $self->throw(
243             "Blast run: output format code should be integer 0-10");
244             }
245 0           $blast_args{'-outfmt'} = $outformat;
246             }
247              
248 0 0         if ($method_args) {
249 0 0         $self->throw(
250             "Blast run: method arguments must be name => value pairs")
251             unless ! ( @$method_args % 2 );
252 0           %usr_args = @$method_args;
253             }
254              
255             # make db if necessary
256             $self->make_db
257             unless $self->check_db
258             or $self->is_remote
259             or $usr_args{'-subject'}
260 0 0 0       or $usr_args{'-SUBJECT'}; # no db nec if this is bl2seq...
      0        
      0        
261             $self->{_factory}
262 0           = Bio::Tools::Run::BlastPlus->new( -command => $method );
263 0 0         if (%usr_args) {
264 0           my @avail_parms = $self->factory->available_parameters('all');
265 0           while ( my ( $key, $value ) = each %usr_args ) {
266 0           $key =~ s/^-//;
267 0 0         unless ( grep /^$key$/, @avail_parms ) {
268 0           $self->throw(
269             "Blast run: parameter '$key' is not available for method '$method'"
270             );
271             }
272             }
273             }
274              
275             # remove a leading ./ on remote databases. Something adds that in the
276             # factory, easier to remove here.
277 0           my $db = $self->db_path;
278 0 0         if ( $self->is_remote ) {
279 0           $db =~ s#^\./##;
280             }
281 0           $blast_args{-db} = $db;
282 0           $blast_args{-query} = $self->_fastize($query);
283 0           $blast_args{-out} = $outfile;
284              
285             # user arg override
286 0 0         if (%usr_args) {
287 0           $blast_args{$_} = $usr_args{$_} for keys %usr_args;
288             }
289              
290             # override for bl2seq;
291 0 0 0       if ( $blast_args{'-db'} && $blast_args{'-subject'} ) {
292 0           delete $blast_args{'-db'};
293             }
294              
295 0           $self->factory->set_parameters(%blast_args);
296 0           $self->factory->no_throw_on_crash( $self->no_throw_on_crash );
297 0           my $status = $self->_run;
298              
299 0 0         return $status unless $status;
300              
301             # kludge to demodernize the bl2seq output
302 0 0         if ( $blast_args{'-subject'} ) {
303 0 0         unless ( _demodernize($outfile) ) {
304 0           $self->throw("Demodernization failed!");
305             }
306             }
307              
308             # if here, success
309 0           for ($method) {
310 0 0         m/^(t|psi|rps|rpst)?blast[npx]?/ && do {
311 0           $ret = Bio::SearchIO->new( -file => $outfile );
312              
313 0           $self->{_blastout} = $outfile;
314 0           $self->{_results} = $ret;
315 0           $ret = $ret->next_result;
316 0           last;
317             };
318 0           do {
319 0           1; # huh?
320             };
321             }
322              
323 0           return $ret;
324             }
325              
326             =head2 bl2seq()
327              
328             Title : bl2seq
329             Usage :
330             Function: emulate bl2seq using blast+ programs
331             Returns : Bio::Search::Result::BlastResult object
332             Args : key => value
333             -method => $blast_method [blastn|blastp|blastx|
334             tblastn|tblastx]
335             -query => $query (fasta file or BioPerl sequence object
336             -subject => $subject (fasta file or BioPerl sequence object)
337             -outfile => $blast_report_file
338             -method_args => [ $key1 => $value1, ... ] (additional method
339             parameters)
340              
341             =cut
342              
343             sub bl2seq {
344 0     0 0   my $self = shift;
345 0           my @args = @_;
346 0           my ( $method, $query, $subject, $outfile, $outformat, $method_args )
347             = $self->_rearrange(
348             [ qw(
349             METHOD
350             QUERY
351             SUBJECT
352             OUTFILE
353             OUTFORMAT
354             METHOD_ARGS
355             )
356             ],
357             @args
358             );
359              
360 0 0         unless ($method) {
361 0           $self->throw("bl2seq: blast method not specified, use -method");
362             }
363 0 0         unless ($query) {
364 0           $self->throw("bl2seq: query data required, use -query");
365             }
366 0 0         unless ($subject) {
367 0           $self->throw("bl2seq: subject data required, use -subject");
368             }
369 0           $subject = $self->_fastize($subject);
370              
371 0           my @run_args;
372 0 0         if ($method_args) {
373 0           @run_args = @$method_args;
374             }
375 0           return $self->run(
376             -method => $method,
377             -query => $query,
378             -outfile => $outfile,
379             -outformat => $outformat,
380             -method_args => [ @run_args, '-subject' => $subject ]
381             );
382              
383             }
384              
385             =head2 next_result()
386              
387             Title : next_result
388             Usage : $result = $fac->next_result;
389             Function: get the next BLAST result
390             Returns : Bio::Search::Result::BlastResult object
391             Args : none
392              
393             =cut
394              
395             sub next_result() {
396 0     0 0   my $self = shift;
397 0 0         return unless $self->{_results};
398 0           return $self->{_results}->next_result;
399             }
400              
401             =head2 rewind_results()
402              
403             Title : rewind_results
404             Usage : $fac->rewind_results;
405             Function: rewind BLAST results
406             Returns : true on success
407             Args :
408              
409             =cut
410              
411             sub rewind_results {
412 0     0 0   my $self = shift;
413 0 0         return unless $self->blast_out;
414 0           $self->{_results} = Bio::SearchIO->new( -file => $self->blast_out );
415 0           return 1;
416             }
417              
418             =head2 blast_out()
419              
420             Title : blast_out
421             Usage : $file = $fac->blast_out
422             Function: get the filename of the blast report file
423             Returns : scalar string
424             Args : none
425              
426             =cut
427              
428 0     0 0   sub blast_out { shift->{_blastout} }
429              
430             # =head2 _demodernize()
431              
432             # Title : _demodernize
433             # Usage :
434             # Function:
435             # Returns :
436             # Args :
437              
438             # =cut
439              
440             sub _demodernize {
441 0     0     my $file = shift;
442 0           my $tf = File::Temp->new();
443 0           open( my $f, $file );
444 0           while (<$f>) {
445 0           s/^Subject=\s+/>/;
446 0           print $tf $_;
447             }
448 0           $tf->close;
449 0           copy( $tf->filename, $file );
450             }
451              
452             1;