Tools/Run/StandAloneBlastPlus/BlastMethods.pm

filter: Statementstmt 23.2% 26 / 112 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 5 totaltotal 17.0% 34 / 199 CC 32 SCAR 59.0
CC
32
Cov
15%
run
650.0
bl2seq
30.0
next_result
6.0
CRAP 365.5
« Tools/Run/Infernal.pm ↑ Summary Tools/Run/StandAloneBlastPlus.pm »
1 #
2 # BioPerl module for Bio::Tools::Run::StandAloneBlastPlus::BlastMethods
3 #
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
5 #
6 # Cared for by Mark A. Jensen <maj -at- fortinbras -dot- us>
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<Bio::Tools::Run::StandAloneBlastPlus> object.
41  
42 =head1 USAGE
43  
44 This POD describes the use of BLAST methods against a
45 L<Bio::Tools::Run::StandAloneBlastPlus> factory object. The object
46 itself has extensive facilities for creating, formatting, and masking
47 BLAST databases; please refer to
48 L<Bio::Tools::Run::StandAloneBlastPlus> POD for these details.
49  
50 Given a C<StandAloneBlastPlus> 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<testdb>). C<-query> is a required argument:
59  
60 $result = $fac->blastn( -query => 'query_seqs.fas' );
61  
62 Here, C<$result> is a L<Bio::Search::Result::BlastResult> 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<bl2seq> 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<Bio::Search::Result::BlastResult>
117 object on success, undef on failure.
118  
119 =head1 SEE ALSO
120  
121 L<Bio::Tools::Run::StandAloneBlastPlus>, L<Bio::Tools::Run::BlastPlus>
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<bioperl-l@bioperl.org>
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 1 ▶use Bio::SearchIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 1 ▶use lib '../../../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 1 ▶use Bio::Tools::Run::BlastPlus;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 1 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 1 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 0 ▶sub run {
Subroutine run: not called CC=25 SCAR=64.8
Pod undocumented
206 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 ▶ unless ($method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 0 0
224 0 $self->throw("Blast run: method not specified, use -method");
225 }
226 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
227 0 $self->throw("Blast run: query data required, use -query");
228 }
229 0 ▶ unless ($outfile) { # create a tempfile name
Branch: 1 branch
BranchTrueFalseTotal
unless ($outfile) 0 0 0
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 ▶ if ($outformat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outformat) 0 0 0
241 0 ▶ unless ( $outformat =~ /^"?[0-9]{1,2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($outformat =~ /^"?[0-9]{1,2}/) 0 0 0
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 ▶ if ($method_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args) 0 0 0
249 0 ▶ $self->throw(
Branch: 1 branch
BranchTrueFalseTotal
if @$method_args % 2 0 0 0
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 ▶ or $usr_args{'-SUBJECT'}; # no db nec if this is bl2seq...
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_db or $self->is_remote or $usr_args{"-subject"} or $usr_args{"-SUBJECT"} 0 0 0
Condition0%0/9
$self->check_db or $self->is_remote or $usr_args{"-subject"} or $usr_args{"-SUBJECT"}
l!l&&r!l&&!r
000
$self->check_db or $self->is_remote or $usr_args{"-subject"}
l!l&&r!l&&!r
000
$self->check_db or $self->is_remote
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->check_db or $self->is_remote or $usr_args{"-subject"} or $usr_args{"-SUBJECT"}
$self->check_db $self->is_remote $usr_args{"-subject"} $usr_args{"-SUBJECT"}
Truth table0%0/5
A or B or C or D
A= $self->check_dbB= $self->is_remoteC= $usr_args{"-subject"}D= $usr_args{"-SUBJECT"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
261 $self->{_factory}
262 0 = Bio::Tools::Run::BlastPlus->new( -command => $method );
263 0 ▶ if (%usr_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%usr_args) 0 0 0
264 0 my @avail_parms = $self->factory->available_parameters('all');
265 0 ▶ while ( my ( $key, $value ) = each %usr_args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %usr_args) 0 0 0
266 0 $key =~ s/^-//;
267 0 ▶ unless ( grep /^$key$/, @avail_parms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /^$key$/, @avail_parms) 0 0 0
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 ▶ if ( $self->is_remote ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_remote) 0 0 0
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 ▶ if (%usr_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%usr_args) 0 0 0
287 0 $blast_args{$_} = $usr_args{$_} for keys %usr_args;
288 }
289  
290 # override for bl2seq;
291 0 ▶ if ( $blast_args{'-db'} && $blast_args{'-subject'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blast_args{"-db"} and $blast_args{"-subject"}) 0 0 0
Condition0%0/3
$blast_args{"-db"} and $blast_args{"-subject"}
!ll&&!rl&&r
000
MC/DC0%0/2
$blast_args{"-db"} and $blast_args{"-subject"}
$blast_args{"-db"} $blast_args{"-subject"}
Truth table0%0/3
A and B
A= $blast_args{"-db"}B= $blast_args{"-subject"}
ABresult
0X0
100
111
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 ▶ return $status unless $status;
Branch: 1 branch
BranchTrueFalseTotal
unless $status 0 0 0
300  
301 # kludge to demodernize the bl2seq output
302 0 ▶ if ( $blast_args{'-subject'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blast_args{"-subject"}) 0 0 0
303 0 ▶ unless ( _demodernize($outfile) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_demodernize($outfile)) 0 0 0
304 0 $self->throw("Demodernization failed!");
305 }
306 }
307  
308 # if here, success
309 0 for ($method) {
310 0 ▶ m/^(t|psi|rps|rpst)?blast[npx]?/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/^(t|psi|rps|rpst)?blast[npx]?/ and do { $ret = "Bio::SearchIO"->new("-file", $outfile); $self->{"_blastout"} = $outfile; $self->{"_results"} = $ret; $ret = $ret->next_result; last } 0 0 0
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 0 ▶sub bl2seq {
Subroutine bl2seq: not called CC=5 SCAR=34.0
Pod undocumented
344 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 ▶ unless ($method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 0 0 0
361 0 $self->throw("bl2seq: blast method not specified, use -method");
362 }
363 0 ▶ unless ($query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
364 0 $self->throw("bl2seq: query data required, use -query");
365 }
366 0 ▶ unless ($subject) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subject) 0 0 0
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 ▶ if ($method_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_args) 0 0 0
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 0 ▶sub next_result() {
Subroutine next_result: not called CC=2 SCAR=17.9
Pod undocumented
396 0 my $self = shift;
397 0 ▶ return unless $self->{_results};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_results"} 0 0 0
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 0 ▶sub rewind_results {
Subroutine rewind_results: not called CC=2 SCAR=17.9
Pod undocumented
412 0 my $self = shift;
413 0 ▶ return unless $self->blast_out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->blast_out 0 0 0
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 ▶sub blast_out { shift->{_blastout} }
Subroutine blast_out: not called CC=1 SCAR=6.9
Pod undocumented
429  
430 # =head2 _demodernize()
431  
432 # Title : _demodernize
433 # Usage :
434 # Function:
435 # Returns :
436 # Args :
437  
438 # =cut
439  
440 0 ▶sub _demodernize {
Subroutine _demodernize: not called CC=2 SCAR=17.9
441 0 my $file = shift;
442 0 my $tf = File::Temp->new();
443 0 open( my $f, $file );
444 0 ▶ while (<$f>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $f)) 0 0 0
445 0 s/^Subject=\s+/>/;
446 0 print $tf $_;
447 }
448 0 $tf->close;
449 0 copy( $tf->filename, $file );
450 }
451  
452 1 1;
« Tools/Run/Infernal.pm ↑ Summary Tools/Run/StandAloneBlastPlus.pm »