lib/Bio/MUST/Apps/OmpaPa/Roles/Parsable.pm

filter: Statementstmt 48.9% 201 / 411 Branchbran 27.5% 22 / 80 Conditioncond 44.4% 8 / 18 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 72.4% 42 / 58 Podpod 0.0% 0 / 6 totaltotal 47.5% 277 / 583 CC 60 SCAR 25.7
CC
60
Cov
45%
_setup_gnuplot
72.0
print_plot
42.0
_set_col_variables
30.0
CRAP 13.1
« lib/Bio/MUST/Apps/OmpaPa/Blast.pm ↑ Summary lib/Bio/MUST/Apps/OmpaPa/Parameters.pm »
1 package Bio::MUST::Apps::OmpaPa::Roles::Parsable;
2 # ABSTRACT: Parsable Moose role for search report objects
3 # CONTRIBUTOR: Amandine BERTRAND <amandine.bertrand@doct.uliege.be>
4 1 $Bio::MUST::Apps::OmpaPa::Roles::Parsable::VERSION = '0.260260';
5 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Smart::Comments '###';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use List::AllUtils qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 1 ▶use IO::Prompter [
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 -verbatim,
29 -style => 'blue strong',
30 -must => { 'be a string' => qr{\S+}xms }
31 1 ];
32  
33 1 ▶use Bio::MUST::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Bio::MUST::Core::Utils qw(change_suffix);
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use aliased 'Bio::MUST::Drivers::Blast::Database';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Bio::MUST::Apps::OmpaPa::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use aliased 'Bio::MUST::Apps::OmpaPa::Parameters';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 requires 'file', 'collect_hits';
42  
43 # TODO: improve wording consistency with CLI
44 # TODO: update internal variable names
45 # TODO: break up long lines
46 # TODO: refine code layout
47  
48 1 has 'database' => (
49 is => 'ro',
50 isa => 'Bio::MUST::Core::Types::File',
51 required => 0, # database is now optional for convenience
52 coerce => 1,
53 );
54  
55 1 has 'scheme' => (
56 is => 'ro',
57 isa => 'Bio::MUST::Core::Taxonomy::ColorScheme',
58 required => 0, # scheme is optional
59 );
60  
61 1 has 'extract_seqs' => (
62 is => 'ro',
63 isa => 'Bool',
64 default => 0,
65 );
66  
67 1 has 'extract_tax' => (
68 is => 'ro',
69 isa => 'Bool',
70 default => 0,
71 );
72  
73 1 has 'parameters' => (
74 is => 'ro',
75 isa => 'Bio::MUST::Apps::OmpaPa::Parameters',
76 lazy => 1,
77 coerce => 1,
78 builder => '_build_parameters',
79 handles => qr{.*}xms,
80 );
81  
82 1 has 'restore_last_params' => (
83 is => 'ro',
84 isa => 'Bool',
85 default => 0,
86 );
87  
88 1 has 'gnuplot_term' => (
89 is => 'ro',
90 isa => 'Str',
91 );
92  
93 1 has 'gnuplot_vers' => (
94 is => 'ro',
95 isa => 'version',
96 init_arg => undef,
97 lazy => 1,
98 builder => '_build_gnuplot_vers',
99 );
100  
101 # TODO: switch to coercions? what about Stash?
102  
103 1 has '_blastdb' => (
104 is => 'ro',
105 isa => 'Bio::MUST::Drivers::Blast::Database',
106 init_arg => undef,
107 lazy => 1,
108 builder => '_build_blastdb',
109 );
110  
111 1 has '_hits' => (
112 traits => ['Array'],
113 is => 'ro',
114 isa => 'ArrayRef[HashRef[Any]]',
115 init_arg => undef,
116 lazy => 1,
117 builder => 'collect_hits',
118 handles => {
119 count_hits => 'count',
120 all_hits => 'elements',
121 filter_hits => 'grep',
122 map_hits => 'map',
123 },
124 );
125  
126 1 has '_coeffs' => (
127 traits => ['Hash'],
128 is => 'ro',
129 isa => 'HashRef[HashRef[Any]]',
130 init_arg => undef,
131 lazy => 1,
132 builder => '_build_coeffs',
133 handles => {
134 count_coeffs => 'count',
135 all_coeffs => 'elements',
136 get_coeffs => 'get',
137 },
138 );
139  
140 1 has '_selection' => (
141 traits => ['Array'],
142 is => 'ro',
143 isa => 'ArrayRef[HashRef[Any]]',
144 init_arg => undef,
145 lazy => 1,
146 builder => '_build_selection',
147 clearer => '_clear_selection',
148 writer => '_set_selection',
149 handles => {
150 count_selection => 'count',
151 all_selection => 'elements',
152 filter_selection => 'grep',
153 map_selection => 'map',
154 },
155 );
156  
157 1 has '_filter' => (
158 traits => ['Hash'],
159 is => 'ro',
160 isa => 'HashRef[Any]',
161 init_arg => undef,
162 lazy => 1,
163 builder => '_build_filter',
164 clearer => '_clear_filter',
165 writer => '_set_filter',
166 handles => {
167 count_filter => 'count',
168 all_filter => 'elements',
169 get_filter => 'get',
170 },
171 );
172  
173 1 has '_avg_len' => ( # average length of top-25% hits
174 is => 'ro',
175 isa => 'Num',
176 init_arg => undef,
177 lazy => 1,
178 builder => '_build_avg_len',
179 );
180  
181 has $_ . '_file' => (
182 is => 'ro',
183 isa => 'Bio::MUST::Core::Types::File',
184 init_arg => undef,
185 lazy => 1,
186 builder => '_build_' . $_ . '_file',
187 1 ) for qw(idl fas tax json list);
188  
189 has $_ . '_parameter_file' => (
190 is => 'ro',
191 isa => 'Bio::MUST::Core::Types::File',
192 init_arg => undef,
193 lazy => 1,
194 builder => '_build_' . $_ . '_parameter_file',
195 1 ) for qw(last new);
196  
197 1 has '_data_handle' => (
198 is => 'ro',
199 isa => 'FileHandle',
200 init_arg => undef,
201 lazy => 1,
202 builder => '_build_data_handle',
203 handles => {
204 _data_file => 'filename',
205 },
206 );
207  
208 1 has '_plot_handle' => (
209 is => 'ro',
210 isa => 'FileHandle',
211 init_arg => undef,
212 lazy => 1,
213 builder => '_build_plot_handle',
214 );
215  
216 ## no critic (ProhibitUnusedPrivateSubroutines)
217  
218 3 ▶sub _build_blastdb {
Subroutine _build_blastdb: called CC=1 SCAR=0.0
219 3 return Database->new( file => shift->database );
220 }
221  
222 3 ▶sub _build_idl_file {
Subroutine _build_idl_file: called CC=1 SCAR=0.0
223 3 return file( change_suffix( shift->new_parameter_file, '.idl' ) );
224 }
225  
226 3 ▶sub _build_fas_file {
Subroutine _build_fas_file: called CC=1 SCAR=0.0
227 3 return file( change_suffix( shift->new_parameter_file, '.fasta' ) );
228 }
229  
230 0 ▶sub _build_tax_file {
Subroutine _build_tax_file: not called CC=1 SCAR=6.9
231 0 return file( change_suffix( shift->new_parameter_file, '.tax' ) );
232 }
233  
234 3 ▶sub _build_list_file {
Subroutine _build_list_file: called CC=1 SCAR=0.0
235 3 return file( change_suffix( shift->new_parameter_file, '.list' ) );
236 } # TODO: choose better suffix: .text?
237  
238 3 ▶sub _build_json_file {
Subroutine _build_json_file: called CC=1 SCAR=0.0
239 3 return shift->new_parameter_file;
240 }
241  
242 3 ▶sub _build_new_parameter_file {
Subroutine _build_new_parameter_file: called CC=3 SCAR=11.0
243 3 my $self = shift;
244  
245 3 my $filename = $self->last_parameter_file;
246 3 my ($basename, $dir, $suf) = fileparse( $filename, '.json' );
247 3 ▶ ($basename, $dir, $suf) = fileparse( $self->file, qr{ \.[^.]* }xms )
Branch: 1 branch
BranchTrueFalseTotal
unless $basename 0 3 3
248 unless ($basename); # if first time
249 3 my @parts = split q{-}, $basename;
250 # default if it's the first time
251 3 my $file = join q{-}, @parts;
252 3 my $new_num = 1;
253  
254 3 my $num = pop @parts;
255 3 ▶ if (looks_like_number $num) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($num)) 3 0 3
256 3 $new_num = $num + 1;
257 3 $file = join q{-}, @parts;
258 }
259  
260 3 my $new_file = join q{.}, ( join q{-}, $file, $new_num ), 'json';
261  
262 3 return file( $dir, $new_file );
263 }
264  
265 3 ▶sub _build_last_parameter_file {
Subroutine _build_last_parameter_file: called CC=4 SCAR=14.5
266 3 my $self = shift;
267  
268 3 my ($file, $dir, $suf) = fileparse( $self->file, qr{ \.[^.]* }xms );
269 3 my @files = File::Find::Rule->file()
270 ->name( $file . '*.json' )
271 ->in( $self->file->dir );
272 3 my @sorted = nsort( @files );
273  
274 # because File::Find::Rule goes recursively in directories
275 # use the directory of entry file
276 3 my @finalsort;
277  
278 FINALSORT:
279 3 for my $f (@sorted) {
280 6 my @parts = split m{/}xms, $f;
281 6 ▶ if ("$parts[0]/" eq $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$parts[0]/" eq $dir) 6 0 6
282 6 push @finalsort, $f;
283 6 next FINALSORT;
284 }
285 0 ▶ push @finalsort, $f if @parts < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 2 0 0 0
286 }
287  
288 3 return file( $finalsort[-1] );
289 }
290  
291 0 ▶sub _build_avg_len {
Subroutine _build_avg_len: not called CC=1 SCAR=6.9
292 0 my $self = shift;
293  
294 # TODO: change: 10 bins eval + mean length // idem but length alignment
295 # compute average length of the top-25% hits
296 0 my $quarter = ceil( $self->count_hits / 4 );
297 0 ▶ my @lengths = $self->map_hits( sub { $_->{'len'} } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my $avg_len = sum( @lengths[ 0 .. $quarter-1 ] ) / $quarter;
299  
300 0 return $avg_len;
301 }
302  
303 4 ▶sub _build_coeffs {
Subroutine _build_coeffs: called CC=5 SCAR=16.2
304 4 my $self = shift;
305  
306 4 my %count_for;
307 my %coeffs_for;
308  
309 4 for my $hit ($self->all_hits) {
310 # TODO: use SeqId methods: $_->$method // $_->taxon_id
311 736 my $org = (split m{\|}xms, $hit->{acc})[0];
312 736 $count_for{$org}++;
313  
314 736 my $coeff_len = $hit->{len} / $hit->{qlen};
315 my $coeff_hmm
316 736 = ( $hit->{hmm_to} - $hit->{hmm_from} + 1 ) / $hit->{qlen};
317  
318 my ($index, $label) = $self->scheme
319 736 ▶ ? $self->scheme->icol( $hit->{acc} ) : (undef, undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->scheme ? : 0 736 736
320 736 ▶ unless (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 736 0 736
321 736 $index = 0;
322 736 $label = 'MISSING';
323 }
324  
325 $coeffs_for{ $hit->{acc} } = {
326 org => $org,
327 736 count => $count_for{$org},
328 max_count => undef,
329 coeff_len => sprintf("%.3f", $coeff_len),
330 align => sprintf("%.3f", $coeff_hmm),
331 tax => $label,
332 index_tax => $index,
333 };
334 }
335  
336 4 for my $hit ($self->all_hits) {
337 $coeffs_for{$hit->{acc}}{max_count}
338 736 = $count_for{$coeffs_for{$hit->{acc}}{org}};
339 }
340  
341 4 return \%coeffs_for;
342 }
343  
344 0 ▶sub _build_data_handle {
Subroutine _build_data_handle: not called CC=3 SCAR=24.8
345 0 my $self = shift;
346  
347 # open anonymous temporary file...
348 # ... and ensures immediate communication with gnuplot
349 0 my $data_handle = File::Temp->new(SUFFIX => '.dat', UNLINK => 1);
350  
351 # write pairs of -log10(evalue) / length as data points
352 0 for my $hit ($self->all_hits) {
353  
354 0 my $info_for = $self->get_coeffs( $hit->{acc} );
355 #### $info_for
356  
357 0 ▶ say {$data_handle} join "\t",
Branch: 1 branch
BranchTrueFalseTotal
$self->scheme ? : 0 0 0
358 _eval2log10( $hit->{exp} ), $hit->{len},
359 $info_for->{count}, $info_for->{align}, $info_for->{coeff_len},
360 0 ( $self->scheme ? $info_for->{index_tax} : () )
361 ;
362  
363 }
364  
365 0 return $data_handle;
366 }
367  
368 2 ▶sub _build_parameters {
Subroutine _build_parameters: called CC=2 SCAR=6.9
369 2 my $self = shift;
370  
371 2 ▶ return Parameters->load( $self->last_parameter_file->stringify )
Branch: 1 branch
BranchTrueFalseTotal
if $self->restore_last_params 1 1 2
372 if $self->restore_last_params;
373  
374 1 return Parameters->new();
375 }
376  
377 0 ▶sub _build_gnuplot_vers {
Subroutine _build_gnuplot_vers: not called CC=2 SCAR=17.9
378 0 my $self = shift;
379  
380 # determine gnuplot version
381 # TODO: doc OUM_GNUPLOT_EXEC
382 0 ▶ my $pgm = $ENV{OUM_GNUPLOT_EXEC} // 'gnuplot';
Condition0%0/2
$ENV{"OUM_GNUPLOT_EXEC"} // "gnuplot"
l!l
00
MC/DC0%0/1
$ENV{"OUM_GNUPLOT_EXEC"} // "gnuplot"
$ENV{"OUM_GNUPLOT_EXEC"}
Truth table0%0/2
A
A= $ENV{"OUM_GNUPLOT_EXEC"}
Aresult
11
00
383 0 my ($version) = qx{$pgm --version} =~ m/gnuplot \s+ (\S+)/xms;
384 0 ### gnuplot version: $version
385 0 ### gnuplot terminal: $self->gnuplot_term
386  
387 0 return version->parse($version);
388 }
389  
390 0 ▶sub _build_plot_handle {
Subroutine _build_plot_handle: not called CC=2 SCAR=17.9
391 0 my $self = shift;
392  
393 # open a gnuplot subprocess...
394 # ... and ensures that commands are immediately processed
395 # TODO: doc OUM_GNUPLOT_EXEC
396 0 ▶ my $pgm = $ENV{OUM_GNUPLOT_EXEC} // 'gnuplot';
Condition0%0/2
$ENV{"OUM_GNUPLOT_EXEC"} // "gnuplot"
l!l
00
MC/DC0%0/1
$ENV{"OUM_GNUPLOT_EXEC"} // "gnuplot"
$ENV{"OUM_GNUPLOT_EXEC"}
Truth table0%0/2
A
A= $ENV{"OUM_GNUPLOT_EXEC"}
Aresult
11
00
397 0 open my $plot_handle, '|-', $pgm;
398 0 $plot_handle->autoflush;
399  
400 0 return $plot_handle;
401 }
402  
403 ## use critic
404  
405  
406 0 ▶sub select_bounds {
Subroutine select_bounds: not called CC=1 SCAR=6.9
Pod undocumented
407 0 my $self = shift;
408  
409 # configure display to allow interactive bound selection
410 # TODO: handle a dumb terminal? this requires manually handling zooming
411 0 $self->_setup_gnuplot;
412  
413 # output help message to console
414 0 my $msg = "When satisfied with your selection, press 'Return' in the console";
415 0 chomp $msg;
416 0 my $ans = prompt $msg, -def => 'Y';
417  
418 # recover newly defined bounds from gnuplot subprocess
419 0 $self->_update_bounds;
420 0 $self->_clear_selection;
421 0 $self->_set_selection($self->_build_selection);
422 0 $self->_clear_filter;
423 0 $self->_set_filter($self->_build_filter);
424  
425 0 return;
426 }
427  
428 0 ▶sub change_filter {
Subroutine change_filter: not called CC=4 SCAR=30.0
Pod undocumented
429 0 my $self = shift;
430 0 my $ans;
431 # TODO: update wording to match main script
432 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$ans ne "N" and do { do { $ans = prompt("Would you like to change the limit for filtering the", "-menu", {"number of gene in one organism", "O", "alignment", "A", "that is OK, thanks", "N"}, ">"); my $msg = "Type your new filtering limit"; if ($ans eq "O") { my $ans_org = prompt($msg, "-n"); $self->set_max_copy($ans_org); } elsif ($ans eq "A") { my $ans_align = prompt($msg, "-n"); $self->set_min_cov($ans_align); } } } 0 0 0
433 0 $ans = prompt "Would you like to change the limit for filtering the",
434 -menu => { 'number of gene in one organism' => 'O',
435 'alignment' => 'A',
436 'that is OK, thanks' => 'N' },
437 '>';
438 0 my $msg = "Type your new filtering limit";
439  
440 0 ▶ if ($ans eq 'O') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ans eq 'O') { } 0 0 0
elsif ($ans eq 'A') { } 0 0 0
441 0 my $ans_org = prompt $msg, -n;
442 #$self->nb_org($ans_org);
443 0 $self->set_max_copy($ans_org);
444 }
445  
446 elsif ($ans eq 'A') {
447 0 my $ans_align = prompt $msg, -n;
448 #$self->align($ans_align);
449 0 $self->set_min_cov($ans_align);
450 }
451  
452 } while ($ans ne 'N');
453  
454 0 return;
455 }
456  
457 # TODO: only for bounds => should change name
458 4 ▶sub _build_selection {
Subroutine _build_selection: called CC=2 SCAR=6.9
459 4 my $self = shift;
460  
461 # ensure that a (valid) bounding-box is in use
462 4 ▶ croak 'Error: undefined hit bounding-box'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->max_len 0 4 4
463 unless defined $self->max_len;
464  
465 # dynamically filter hits based on current bounds
466 736 ▶ my @selection = $self->filter_hits( sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
467 _eval2log10( $_->{'exp'} ) >= $self->min_eval
468 && _eval2log10( $_->{'exp'} ) <= $self->max_eval
469 && $_->{'len'} >= $self->min_len
470 736 ▶ && $_->{'len'} <= $self->max_len
Branch: 1 branch
BranchTrueFalseTotal
_eval2log10($_->{"exp"}) >= $self->min_eval and _eval2log10($_->{"exp"}) <= $self->max_eval and $_->{"len"} >= $self->min_len and $_->{"len"} <= $self->max_len 260 476 736
Condition100%6/6
_eval2log10($_->{"exp"}) >= $self->min_eval and _eval2log10($_->{"exp"}) <= $self->max_eval and $_->{"len"} >= $self->min_len
!ll&&!rl&&r
370106260
_eval2log10($_->{"exp"}) >= $self->min_eval and _eval2log10($_->{"exp"}) <= $self->max_eval
!ll&&!rl&&r
27694366
MC/DC100%3/3
_eval2log10($_->{"exp"}) >= $self->min_eval and _eval2log10($_->{"exp"}) <= $self->max_eval and $_->{"len"} >= $self->min_len
_eval2log10($_->{"exp"}) >= $self->min_eval _eval2log10($_->{"exp"}) <= $self->max_eval $_->{"len"} >= $self->min_len
Truth table100%4/4
A and B and C
A= _eval2log10($_->{"exp"}) >= $self->min_evalB= _eval2log10($_->{"exp"}) <= $self->max_evalC= $_->{"len"} >= $self->min_len
ABCresult
0XX0
10X0
1100
1111
471 4 } );
472  
473 4 return \@selection;
474 }
475  
476 4 ▶sub _build_filter {
Subroutine _build_filter: called CC=4 SCAR=13.9
477 4 my $self = shift;
478  
479 4 my $nb_org = $self->max_copy;
480 4 my $align = $self->min_cov;
481 4 my %filter_for;
482  
483 4 for my $hit ($self->all_selection) {
484 260 ▶ if ( ( $self->get_coeffs($hit->{acc}) )->{align} >= $align
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_coeffs($hit->{"acc"})->{"align"} >= $align and $self->get_coeffs($hit->{"acc"})->{"count"} <= $nb_org) 249 11 260
Condition66%2/3
$self->get_coeffs($hit->{"acc"})->{"align"} >= $align and $self->get_coeffs($hit->{"acc"})->{"count"} <= $nb_org
!ll&&!rl&&r
110249
MC/DC50%1/2
$self->get_coeffs($hit->{"acc"})->{"align"} >= $align and $self->get_coeffs($hit->{"acc"})->{"count"} <= $nb_org
$self->get_coeffs($hit->{"acc"})->{"align"} >= $align $self->get_coeffs($hit->{"acc"})->{"count"} <= $nb_org
Truth table66%2/3
A and B
A= $self->get_coeffs($hit->{"acc"})->{"align"} >= $alignB= $self->get_coeffs($hit->{"acc"})->{"count"} <= $nb_org
ABresult
0X0
100
111
485 && ( $self->get_coeffs($hit->{acc}) )->{count} <= $nb_org ) {
486 249 $filter_for{ $hit->{acc} } = q{*};
487 }
488 }
489  
490 4 return \%filter_for;
491 }
492  
493 10 ▶sub list_selection {
Subroutine list_selection: called CC=4 SCAR=13.9
Pod undocumented
494 10 my $self = shift;
495 10 my $option = shift;
496  
497 # setup table of selected hits
498 10 my @heads = qw(keep accession description length evalue count max alignment ratio_length);
499 # my @heads = qw(keep accession description length evalue copy max hmm_cover hit_ratio);
500 10 ▶ push @heads, 'taxonomy' if $self->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $self->scheme 0 10 10
501 10 my $table = Text::Table->new(@heads);
502  
503 # fill-in table (defaults to 'all' hits)
504 10 my @fields = qw(count max_count align coeff_len);
505 10 ▶ push @fields, 'tax' if $self->scheme;
Branch: 1 branch
BranchTrueFalseTotal
if $self->scheme 0 10 10
506 10 my @rows = map { [
507 $self->get_filter( $_->{acc} ), $_->{acc},
508 760 @{$_}{ qw(dsc len exp) },
509 760 @{ $self->get_coeffs( $_->{acc} ) }{@fields}
510 ] } $self->all_selection;
511  
512 # optionally filter rows
513 10 ▶ @rows = grep { $_->[0] } @rows if $option eq 'keep';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "keep" 6 4 10
514  
515 # output table
516 10 $table->load(@rows);
517 10 ( my $selection = $table->rule(q{=}) . $table->title . $table->rule(q{-})
518 . $table->body . $table->rule(q{=}) ) =~ s{\ +$}{}xmsg;
519 # trim trailing whitespace for unit test
520 10 return $selection;
521 }
522  
523 3 ▶sub save_selection {
Subroutine save_selection: called CC=7 SCAR=21.0
Pod undocumented
524 3 my $self = shift;
525 # TODO: figure out how to get rid of stringify
526 3 $self->parameters->store( $self->json_file->stringify );
527  
528 3 ### save_selection and parameters: $self->json_file->stringify
529 # write file of accession (or GI) numbers
530 3 my @ids;
531  
532 # TODO: make this a map construct
533 3 for my $selec ($self->all_selection) {
534 250 ▶ if ( $self->get_filter( $selec->{acc} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_filter($selec->{"acc"})) 239 11 250
535 239 push @ids, $selec->{acc};
536 }
537 }
538  
539 3 $self->idl_file->spew( join("\n", @ids) . "\n" );
540  
541 3 $self->list_file->spew( $self->list_selection('keep') );
542  
543 # optionally write file of seqs
544 3 ▶ if ($self->extract_seqs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extract_seqs) 3 0 3
545 3 my $seqs = $self->fetch_seqs( \@ids );
546  
547 # fix ids returned by blastdbcmd
548 # TODO: consider fixing this in Bio::MUST::Drivers or in Stash
549 3 for my $seq_id ( $seqs->all_seq_ids ) {
550 0 ( my $full_id = $seq_id->full_id )
551 =~ s/lcl\| | \s unnamed \s protein \s product//xmsg;
552 0 $seq_id->_set_full_id($full_id);
553 }
554 # TODO: fix this... what is the issue?
555 #$seqs->store_fasta( $self->fas_file );
556 3 my @new_seqs = $seqs->all_seqs;
557 3 my $ali = Ali->new( seqs => \@new_seqs );
558 #my $ali = Ali->new( seqs => $seqs );
559 3 $ali->store_fasta( $self->fas_file );
560 }
561  
562 3 ▶ if ($self->extract_tax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->extract_tax) 0 3 3
563 0 ▶ my @labels = map { $self->scheme->classify($_) // "undef" } @ids;
Condition0%0/2
$self->scheme->classify($_) // "undef"
l!l
00
MC/DC0%0/1
$self->scheme->classify($_) // "undef"
$self->scheme->classify($_)
Truth table0%0/2
A
A= $self->scheme->classify($_)
Aresult
11
00
564 0 $self->tax_file->spew( join("\n", @labels) . "\n" );
565 }
566  
567 3 return;
568 }
569  
570  
571 3 ▶sub fetch_seqs {
Subroutine fetch_seqs: called CC=1 SCAR=0.0
Pod undocumented
572 3 my $self = shift;
573 3 my $ids = shift;
574  
575 3 return $self->_blastdb->blastdbcmd($ids);
576 }
577  
578 0 ▶sub _setup_gnuplot {
Subroutine _setup_gnuplot: not called CC=8 SCAR=42.8
579 0 my $self = shift;
580  
581 # fill-in command template with relevant object attributes
582  
583 0 my $template = $self->_template_gnuplot;
584  
585 0 my %longcol_for = ( O => 'organisms',
586 T => 'taxonomy',
587 A => 'alignment',
588 G => 'global / alignment');
589  
590 # get variables for organism coloration
591 0 my $org_col = $self->_org_col;
592 0 my $coloration = 'O';
593  
594 # get variables for coefficients coloration
595 0 my $coeff_col = $self->_coeff_col;
596  
597 # get variables for taxonomic coloration if asked
598 0 my $tax_col;
599  
600 0 ▶ if ($self->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scheme) 0 0 0
601 0 $tax_col = $self->_tax_col;
602 0 $coloration = 'T';
603 }
604  
605 #### data: $self->_data_file
606  
607 0 my $ans = 'Y';
608 0 my $explanation;
609  
610 COLORS:
611 0 ▶ while (uc($ans) ne 'N') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $ans ne "N") 0 0 0
612  
613 # set variables given the coloration asked
614 0 my $col_vars = $self->_set_col_variables( $coloration, $org_col,
615 $coeff_col, $tax_col );
616  
617 0 ▶ my $global = $coloration eq 'G' ? $self->max_copy : undef;
Branch: 1 branch
BranchTrueFalseTotal
$coloration eq 'G' ? : 0 0 0
618 0 ▶ my $dt = $self->gnuplot_vers >= version->parse(5) ? q{dt "-"} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->gnuplot_vers >= 'version'->parse(5) ? : 0 0 0
619  
620 0 my $tt = Template->new;
621 my $vars = {
622 term => $self->gnuplot_term,
623 report => $self->file,
624 avg_len => int( $self->_avg_len ), # no fractional length
625 data_file => $self->_data_file,
626 palette => $col_vars->{palette},
627 range => $col_vars->{color_n},
628 tic => $col_vars->{tic},
629 column => $col_vars->{column},
630 limit => $col_vars->{limit},
631 comparison => $col_vars->{comparison},
632 top => $coeff_col->{top},
633 bottom => $coeff_col->{bottom},
634 qlen => $coeff_col->{qlen},
635 0 coloration => $longcol_for{$coloration},
636 global => $global,
637 dt => $dt,
638 };
639  
640 # bug when "Undo": have to change colors twice to be correct (sometimes)
641 # TODO: fix that bug
642 0 my $cmds;
643 0 ▶ $tt->process(\$template, $vars, \$cmds)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$template, $vars, \$cmds) or croak($tt->error, "\n") 0 0 0
644 or croak $tt->error(), "\n";
645  
646 #### $cmds
647  
648 # send completed commands to gnuplot subprocess
649 0 print { $self->_plot_handle } $cmds;
650  
651 0 my $msg = "Which coloration information would you like? (If you do not want to change colors, press 'Return'.)";
652  
653 0 ▶ unless ($explanation) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($explanation) 0 0 0
654 0 $msg = <<'EOT';
655 Define the hit bounding-box using the mouse (button 3).
656 If needed, reset the zoom level by pressing the 'U' key in the plot window.
657 Press 'H' to get help for other hot keys.
658 Which coloration information would you like? (If you do not want to change colors, press 'Return'.)
659 EOT
660 0 $explanation = 1;
661 }
662  
663 0 ▶ $ans = prompt $msg,
Branch: 1 branch
BranchTrueFalseTotal
$self->scheme ? : 0 0 0
664 -def => 'N',
665 -menu => { 'Organisms' => 'O',
666 ( $self->scheme ? ( 'Taxonomy' => 'T' ) : () ),
667 'Alignment' => 'A',
668 'Global' => 'G' },
669 '>';
670  
671 0 $coloration = $ans;
672 };
673  
674 0 return;
675 }
676  
677 0 ▶sub _coeff_col {
Subroutine _coeff_col: not called CC=3 SCAR=24.8
678 0 my $self = shift;
679  
680 0 my $palette_hmm = "0 \t 'yellow', 0.5 \t 'green', 1 \t 'black'"; # 1 = perfectly aligned
681 0 my $color_n_hmm = 1;
682 0 my $tic_hmm = "\"0\" 0, \"0.2\" 0.2, \"0.4\" 0.4, \"0.6\" 0.6, \"0.8\" 0.8, \"1\" 1";
683  
684 # TODO: find another way (do it twice: here and in _build_data_handle)
685 0 my $top;
686 my $bottom;
687 0 my $qlen;
688  
689 HIT:
690 0 for my $hit ($self->all_hits) {
691 0 $top = $hit->{qlen} * 1.5;
692 0 $bottom = $hit->{qlen} * 0.5;
693 0 $qlen = $hit->{qlen};
694 0 ▶ last HIT if ($qlen);
Branch: 1 branch
BranchTrueFalseTotal
if $qlen 0 0 0
695 }
696  
697 0 my %return = (
698 color_n_hmm => $color_n_hmm,
699 palette_hmm => $palette_hmm,
700 tic_hmm => $tic_hmm,
701 top => $top,
702 bottom => $bottom,
703 qlen => $qlen
704 );
705  
706 0 return \%return;
707 }
708  
709 0 ▶sub _org_col {
Subroutine _org_col: not called CC=4 SCAR=30.0
710 0 my $self = shift;
711  
712 0 my @ids = map { $_->{acc} } $self->all_hits;
713 0 my @orgs = map { (split m{\|}xms, $_)[0] } @ids;
714 0 my %count_orgs;
715  
716 0 for my $org (@orgs) {
717 0 $count_orgs{$org}++;
718 }
719  
720 0 my $nb_org_tot = keys %count_orgs;
721 0 my $max = (sort {$a <=> $b} values %count_orgs)[$nb_org_tot-1];
722 0 my $color_n = $max; # range scale palette
723 # limit coloration: 3 (default) or more times the same organism in yellow
724 0 my $limit = $self->max_copy;
725 # if black: bug!
726 0 my $palette = "0 \t 'black', 1 \t 'red', $limit \t 'yellow', $max \t 'yellow'";
727  
728 0 ▶ if ($limit > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit > $max) 0 0 0
729 0 $palette = "0 \t 'black', 1 \t 'red', $max \t 'yellow'";
730  
731 0 ▶ if ($max == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max == 1) 0 0 0
732 0 $palette = "0 \t 'black', 1 \t 'red'";
733 }
734 }
735  
736 0 my %return = (
737 color_n => $color_n,
738 palette => $palette,
739 );
740  
741 0 return \%return;
742 }
743  
744 0 ▶sub _tax_col {
Subroutine _tax_col: not called CC=4 SCAR=30.0
745 0 my $self = shift;
746  
747 # make palette for legend
748 0 my $scheme = $self->scheme;
749 0 my $color_n = $scheme->count_colors; # range scale palette
750 0 my %color_for = $scheme->all_icols;
751 #### %color_for
752  
753 # TODO: exclude black specs?
754 # TODO: warn of dupe specs
755 my %colorcode_for = (
756 0 => '#000000', # unclassified hit = black
757 0 map { $color_for{$_} => $_ } keys %color_for
758 );
759 #### %colorcode_for
760  
761 0 my @strings;
762 0 my $inc = 0;
763 0 for my $num ( sort {$a <=> $b} keys %colorcode_for ) {
764 0 push @strings, ($num-$inc) . "\t \"" . $colorcode_for{$num} . q{"};
765 0 ▶ $inc ||= $inc + 0.5;
Condition0%0/3
$inc ||= $inc + "0.5"
l!l&&r!l&&!r
000
MC/DC0%0/2
$inc ||= $inc + "0.5"
$inc $inc + "0.5"
Truth table0%0/3
A ||= B
A= $incB= $inc + "0.5"
ABresult
1X1
011
000
766 0 push @strings, ($num+$inc) . "\t \"" . $colorcode_for{$num} . q{"};
767 }
768 #### @strings
769 0 my $palette = join q{,}, @strings;
770  
771 # make labels for legend
772 0 my @names = $scheme->all_names;
773  
774 0 my @tics;
775 0 for my $label (@names) {
776 0 my $index = $self->scheme->icol_for( $scheme->color_for($label) );
777 0 my $string = q{"} . $label . qq{" \t} . $index;
778 0 push @tics, $string;
779 }
780 0 my $tic_str = join q{,}, @tics;
781  
782 0 my %return = (
783 color_n => $color_n + 0.5,
784 palette => $palette,
785 tic => $tic_str,
786 );
787  
788 0 return \%return;
789 }
790  
791 0 ▶sub _template_gnuplot {
Subroutine _template_gnuplot: not called CC=1 SCAR=6.9
792 0 my $self = shift;
793  
794 # different template given the coloration asked
795 0 my $template = <<'EOT';
796 x = "-log10(evalue)"
797 y = "hit length"
798 [% IF print %]set terminal pdf enhanced font ",8"
799 set output "[% report %].[% suffix %]_[% coloration %].pdf"
800 [% ELSE %]set term [% term %] title "OmpaPa: [% report %]"
801 [% END %]set format "%.0f"
802 set mouse mouseformat x . ": %3.0f | " . y . ": %4.0f"
803 set size square
804 #set title "average length of top-25% hits: [% avg_len %]"
805 set title "Selected hits given hit length and evalue and with \n [% coloration %] coloration"
806 set grid x y
807 set xlabel x
808 set ylabel y
809 set arrow 1 from graph 0, first [% top %] to graph 1, first [% top %] nohead [% dt %]
810 set arrow 2 from graph 0, first [% bottom %] to graph 1, first [% bottom %] nohead [% dt %]
811 set arrow 3 from graph 0, first [% qlen %] to graph 1, first [% qlen %] nohead [% dt %] lc rgb "blue"
812 set cbrange[0:[% range %]]
813 [% IF tic %]set cbtics ([% tic %])
814 [% ELSE %]unset cbtics
815 set cbtics
816 [% END %]set palette defined ([% palette %])
817 [% IF print %][% IF global %][% IF all %]plot "[% data_file %]" using 1:2:[% column %] notitle with points pt 7 ps .2 palette
818 [% ELSE %]plot "[% data_file %]" using 1:($[% column %] [%comparison %]= [% limit %] && $3 <= [% global %] ? $2 : 1/0):[% column %] notitle with points pt 7 ps .2 palette[% END %]
819 [% ELSE %]plot "[% data_file %]" using 1:($[% column %] [%comparison %]= [% limit %] ? $2 : 1/0):[% column %] notitle with points pt 7 ps .2 palette[% END %]
820 [% ELSE %][% IF global %]plot "[% data_file %]" using 1:($[% column %] [%comparison %]= [% limit %] && $3 <= [% global %] ? $2 : 1/0):[% column %] notitle with points pt 7 ps 1 palette
821 [% ELSE %]plot "[% data_file %]" using 1:($[% column %] [%comparison %]= [% limit %] ? $2 : 1/0):[% column %] notitle with points pt 7 ps 1 palette[% END %][% END %]
822 EOT
823  
824 0 return $template;
825 }
826  
827  
828 0 ▶sub _set_col_variables {
Subroutine _set_col_variables: not called CC=5 SCAR=34.0
829 0 my $self = shift;
830 0 my $coloration = shift;
831 0 my $org_col = shift;
832 0 my $coeff_col = shift;
833 0 my $tax_col = shift;
834  
835 0 my $palette;
836 my $color_n;
837 0 my $tic;
838 0 my $column;
839 0 my $limit;
840 0 my $comparison;
841  
842 0 ▶ if (uc($coloration) eq 'O') {
Branch: 4 branches
BranchTrueFalseTotal
if (uc $coloration eq 'O') { } 0 0 0
elsif (uc $coloration eq 'T') { } 0 0 0
elsif (uc $coloration eq 'A') { } 0 0 0
elsif (uc $coloration eq 'G') { } 0 0 0
843 0 $palette = $org_col->{palette};
844 0 $color_n = $org_col->{color_n};
845 0 $tic = undef;
846 0 $column = 3;
847 0 $limit = $self->max_copy;
848 0 $comparison = "<";
849 }
850  
851 elsif (uc($coloration) eq 'T') {
852 0 $palette = $tax_col->{palette};
853 0 $color_n = $tax_col->{color_n};
854 0 $tic = $tax_col->{tic};
855 0 $column = 6;
856 0 $limit = '$6';
857 0 $comparison = "=";
858 }
859  
860 elsif (uc($coloration) eq 'A') {
861 0 $palette = $coeff_col->{palette_hmm};
862 0 $color_n = $coeff_col->{color_n_hmm};
863 0 $tic = $coeff_col->{tic_hmm};
864 0 $column = 4;
865 0 $limit = $self->min_cov;
866 0 $comparison = ">";
867 }
868  
869 elsif (uc($coloration) eq 'G') {
870 0 $palette = $coeff_col->{palette_hmm};
871 0 $color_n = $coeff_col->{color_n_hmm};
872 0 $tic = $coeff_col->{tic_hmm};
873 0 $column = 4;
874 0 $limit = $self->min_cov;
875 0 $comparison = ">";
876 }
877  
878 0 my $vars = {
879 palette => $palette,
880 color_n => $color_n,
881 tic => $tic,
882 column => $column,
883 limit => $limit,
884 comparison => $comparison,
885 };
886  
887 0 return $vars;
888 }
889  
890 0 ▶sub print_plot {
Subroutine print_plot: not called CC=6 SCAR=37.4
Pod undocumented
891 0 my $self = shift;
892 0 my $suf = shift;
893 0 my $all = shift;
894  
895 0 my $template = $self->_template_gnuplot;
896  
897 0 my $org_col = $self->_org_col;
898 0 my $coeff_col = $self->_coeff_col;
899 0 my @colorations = qw(O A G);
900  
901 0 my $tax_col;
902  
903 0 ▶ if ($self->scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->scheme) 0 0 0
904 0 $tax_col = $self->_tax_col;
905 0 push @colorations, 'T';
906 }
907  
908 0 for my $coloration (@colorations) {
909 0 my $col_vars = $self->_set_col_variables( $coloration, $org_col,
910 $coeff_col, $tax_col );
911  
912 0 ▶ my $global = $coloration eq 'G' ? $self->max_copy : undef;
Branch: 1 branch
BranchTrueFalseTotal
$coloration eq 'G' ? : 0 0 0
913 0 ▶ my $dt = $self->gnuplot_vers >= version->parse(5) ? q{dt "-"} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$self->gnuplot_vers >= 'version'->parse(5) ? : 0 0 0
914  
915 0 my ($basename, $dir) = fileparse( $self->new_parameter_file, '.json' );
916 0 my $report = join q{}, $dir, $basename;
917  
918 0 my $tt = Template->new;
919 my $vars = {
920 report => $report,
921 avg_len => int( $self->_avg_len ), # no fractional length
922 data_file => $self->_data_file,
923 palette => $col_vars->{palette},
924 range => $col_vars->{color_n},
925 tic => $col_vars->{tic},
926 column => $col_vars->{column},
927 limit => $col_vars->{limit},
928 comparison => $col_vars->{comparison},
929 top => $coeff_col->{top},
930 bottom => $coeff_col->{bottom},
931 qlen => $coeff_col->{qlen},
932 0 coloration => $coloration,
933 suffix => $suf,
934 print => 'Y',
935 global => $global,
936 all => $all,
937 dt => $dt,
938 };
939  
940 0 my $print_cmds;
941 0 ▶ $tt->process(\$template, $vars, \$print_cmds)
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$template, $vars, \$print_cmds) or croak($tt->error, "\n") 0 0 0
942 or croak $tt->error(), "\n";
943  
944 #### $print_cmds
945  
946 # send completed commands to gnuplot subprocess
947 0 print { $self->_plot_handle } $print_cmds;
948 }
949  
950 0 return;
951 }
952  
953 0 ▶sub _update_bounds {
Subroutine _update_bounds: not called CC=1 SCAR=6.9
954 0 my $self = shift;
955  
956 # ask gnuplot to export its bounds as currently defined
957 0 my $bounds = $self->store_bounds;
958 0 print { $self->_plot_handle } $bounds; # store bounds
959  
960 # restore bounds from freshly created bb_file
961 0 $self->load_bounds;
962  
963 0 return;
964 }
965  
966  
967 # private subs
968  
969 {
970 1 const my $MAXLOG10 => 308;
971 1 const my $LOG10 => log(10);
972  
973 1196 ▶ sub _eval2log10 {
Subroutine _eval2log10: called CC=3 SCAR=12.0
974 1196 my $eval = shift;
975  
976 1196 ▶ return $eval == 0 ? $MAXLOG10
Branch: 2 branches
BranchTrueFalseTotal
$eval == 0 ? : 0 1196 1196
$eval >= 1 ? : 0 1196 1196
977 : $eval >= 1 ? 0
978 : -log($eval)/$LOG10
979 ;
980 }
981 }
982  
983 1 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
984 1 1;
985  
986 __END__
« lib/Bio/MUST/Apps/OmpaPa/Blast.pm ↑ Summary lib/Bio/MUST/Apps/OmpaPa/Parameters.pm »