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