lib/Bio/MUST/Drivers/Roles/Blastable.pm

filter: Statementstmt 43.9% 47 / 107 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 65.2% 15 / 23 Podpod 0.0% 0 / 7 totaltotal 35.8% 62 / 173 CC 15 SCAR 37.3
CC
15
Cov
34%
_blast
132.0
blast
12.0
blastdbcmd
12.0
CRAP 41.9
« lib/Bio/MUST/Drivers/Exonerate/Aligned.pm ↑ Summary lib/Bio/MUST/Drivers/CdHit.pm »
1 package Bio::MUST::Drivers::Roles::Blastable;
2 # ABSTRACT: BLAST database-related methods
3 5 $Bio::MUST::Drivers::Roles::Blastable::VERSION = '0.252830';
4 5 ▶use 5.018; # to avoid a crash due to call to "can" below
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use Smart::Comments '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use IPC::System::Simple qw(system);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use aliased 'Bio::MUST::Core::Ali::Stash';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use aliased 'Bio::FastParsers::Blast::Table';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use aliased 'Bio::FastParsers::Blast::Xml';
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 ▶use Bio::MUST::Drivers::Utils qw(stringify_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 # TODO: avoid hard-coded convenience methods?
26  
27 0 ▶sub blastn { ## no critic (RequireArgUnpacking)
Subroutine blastn: not called CC=1 SCAR=6.9
Pod undocumented
28 0 return shift->_blast( 'blastn', @_);
29 }
30  
31 0 ▶sub blastp { ## no critic (RequireArgUnpacking)
Subroutine blastp: not called CC=1 SCAR=6.9
Pod undocumented
32 0 return shift->_blast( 'blastp', @_);
33 }
34  
35 0 ▶sub blastx { ## no critic (RequireArgUnpacking)
Subroutine blastx: not called CC=1 SCAR=6.9
Pod undocumented
36 0 return shift->_blast( 'blastx', @_);
37 }
38  
39 0 ▶sub tblastn { ## no critic (RequireArgUnpacking)
Subroutine tblastn: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return shift->_blast('tblastn', @_);
41 }
42  
43 0 ▶sub tblastx { ## no critic (RequireArgUnpacking)
Subroutine tblastx: not called CC=1 SCAR=6.9
Pod undocumented
44 0 return shift->_blast('tblastx', @_);
45 }
46  
47 5 my %pgm_for = ( # cannot be made constant to allow undefined keys
48 'nucl:nucl' => 'blastn',
49 'nucl:prot' => 'blastx',
50 'prot:prot' => 'blastp',
51 'prot:nucl' => 'tblastn',
52 );
53  
54 0 ▶sub blast { ## no critic (RequireArgUnpacking)
Subroutine blast: not called CC=3 SCAR=24.8
Pod undocumented
55 0 my $self = shift;
56 0 my $query = shift;
57  
58 # abort if no Ali::Temporary-like object
59 # this seems to work both with Path::Class::File and plain filenames
60 # however, the can construct here requires perl-5.18 (cannot find why)
61 0 ▶ croak "[BMD] Error: Cannot autoselect BLAST program for $query; aborting!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $query->can("type") and $query->can("filename") 0 0 0
Condition0%0/3
$query->can("type") and $query->can("filename")
!ll&&!rl&&r
000
MC/DC0%0/2
$query->can("type") and $query->can("filename")
$query->can("type") $query->can("filename")
Truth table0%0/3
A and B
A= $query->can("type")B= $query->can("filename")
ABresult
0X0
100
111
62 . 'Use Ali::Temporary to autodetect query sequence type.'
63 unless $query->can('type') && $query->can('filename');
64  
65 # auto-select BLAST program based on query/database type
66 0 my $pgm = $pgm_for{ $query->type . ':' . $self->type };
67  
68 0 return $self->_blast($pgm, $query->filename, @_);
69 }
70  
71 0 ▶sub _blast {
Subroutine _blast: not called CC=11 SCAR=48.8
72 0 my $self = shift;
73 0 my $pgm = shift;
74 0 my $query = shift;
75 0 ▶ my $args = shift // {};
Condition0%0/2
shift() // {}
l!l
00
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
76  
77 #### $pgm
78 #### $args
79  
80 # provision executable
81 0 my $app = use_module('Bio::MUST::Provision::Blast')->new;
82 0 $app->meet();
83  
84 # setup output file and output format
85 # Note: only tabular, XML and HTML outputs are allowed
86 # if specified -html takes precedence on -outfmt
87 0 my $suffix = ".$pgm";
88 0 ▶ if (exists $args->{-html}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{'-html'}) { } 0 0 0
89 0 $suffix .= '.html';
90 0 delete $args->{-outfmt}; # enforce precedence policy
91 }
92 else {
93 0 ▶ unless (defined $args->{-outfmt} && $args->{-outfmt} =~ m/[567]/xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args->{"-outfmt"} and $args->{"-outfmt"} =~ /[567]/msux) 0 0 0
Condition0%0/3
defined $args->{"-outfmt"} and $args->{"-outfmt"} =~ /[567]/msux
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"-outfmt"} and $args->{"-outfmt"} =~ /[567]/msux
defined $args->{"-outfmt"} $args->{"-outfmt"} =~ /[567]/msux
Truth table0%0/3
A and B
A= defined $args->{"-outfmt"}B= $args->{"-outfmt"} =~ /[567]/msux
ABresult
0X0
100
111
94 0 carp '[BMD] Warning: no valid -outfmt specified;'
95 . ' defaulting to tabular!';
96 0 $args->{-outfmt} = 6;
97 }
98 }
99 0 my $out = File::Temp->new(UNLINK => 0, EXLOCK => 0, SUFFIX => $suffix);
100  
101 # automatically setup remote BLAST based on database "class"
102 0 ▶ $args->{-remote} = undef if $self->remote;
Branch: 1 branch
BranchTrueFalseTotal
if $self->remote 0 0 0
103  
104 # format BLAST (optional) arguments
105 0 ▶ $args->{-query} = $query->can('filename') ? $query->filename : $query;
Branch: 1 branch
BranchTrueFalseTotal
$query->can('filename') ? : 0 0 0
106 0 $args->{-db} = $self->filename; # handle query plain filenames too
107 0 $args->{-out} = $out->filename;
108 0 my $args_str = stringify_args($args);
109  
110 # create BLAST command
111 # Note: we need to untaint data here
112 0 ($pgm) = file($ENV{BMD_BLAST_BINDIR}, $pgm) =~ m/^(\S+)$/xms;
113 0 ▶ ### assert: $pgm
Branch: 1 branch
BranchTrueFalseTotal
unless ($pgm) 0 0 0
114 0 my $cmd = join q{ }, $pgm, $args_str, '> /dev/null 2> /dev/null';
115 #### $cmd
116  
117 # try to robustly execute BLAST
118 0 my $ret_code = system( [ 0, 127 ], $cmd);
119 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
120 0 carp "[BMD] Warning: cannot execute $pgm command;"
121 . ' returning without parser!';
122 0 return;
123 }
124  
125 # return Bio::FastParsers::Blast of the right subclass
126 # depending on the output format (XML or tabular)
127 # or the Path::Class::File of the report for HTML output
128 return exists $args->{-html} ? $out->filename :
129 0 ▶ $args->{-outfmt} == 5 ? Xml->new(file => $out->filename) :
Branch: 2 branches
BranchTrueFalseTotal
exists $args->{'-html'} ? : 0 0 0
$args->{'-outfmt'} == 5 ? : 0 0 0
130 Table->new(file => $out->filename)
131 ;
132  
133 # TODO: devise a way to unlink report without affecting parsing
134 # should be an option of the FastParsers?
135 }
136  
137 0 ▶sub blastdbcmd {
Subroutine blastdbcmd: not called CC=3 SCAR=24.8
Pod undocumented
138 0 my $self = shift;
139 0 my $ids = shift;
140 0 ▶ my $args = shift // {};
Condition0%0/2
shift() // {}
l!l
00
MC/DC0%0/1
shift() // {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
141  
142 # setup temporary input/output files (will be automatically unlinked)
143 0 my $in = File::Temp->new(UNLINK => 1, EXLOCK => 0);
144 0 my $out = File::Temp->new(UNLINK => 1, EXLOCK => 0);
145 # TODO: check if lifespan of $out temp file long enough for loading
146  
147 # write id list for -entry_batch
148 0 say {$in} join "\n", @{$ids};
149 0 $in->flush; # for robustness ; might be not needed
150  
151 # format blastdbcmd (optional) arguments
152 0 $args->{-db} = $self->filename;
153 0 $args->{-entry_batch} = $in->filename;
154 # $args->{-out} = $out->filename; # see sed workaround below
155 0 my $args_str = stringify_args($args);
156  
157 # create blastdbcmd command
158 0 my $pgm = file($ENV{BMD_BLAST_BINDIR}, 'blastdbcmd');
159 0 my $cmd = join q{ }, $pgm, $args_str;
160 # pipe to sed to remove extraneous whitespace added after accession
161 0 $cmd .= q{ | sed 's/[[:space:]]$//' > } . $out->filename;
162 #### $cmd
163  
164 # try to robustly execute blastdbcmd
165 0 my $ret_code = system( [ 0, 127 ], $cmd);
166 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
167 0 carp "[BMD] Warning: cannot execute $pgm command;"
168 . ' returning without seqs!';
169 0 return;
170 }
171  
172 # TODO: allow for auto-restore of long ids?
173 0 return Stash->load( $out->filename );
174 }
175  
176 5 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 5 1;
178  
179 __END__
« lib/Bio/MUST/Drivers/Exonerate/Aligned.pm ↑ Summary lib/Bio/MUST/Drivers/CdHit.pm »