lib/Bio/MUST/Core/SeqMask.pm

filter: Statementstmt 81.4% 273 / 335 Branchbran 62.5% 40 / 64 Conditioncond 65.1% 28 / 43 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 94.1% 48 / 51 Podpod 100% 25 / 25 totaltotal 78.5% 425 / 541 CC 61 SCAR 28.0
CC
61
Cov
77%
gblocks_mask
110.0
bmge_mask
56.0
ideal_mask
8.0
CRAP 16.4
« lib/Bio/MUST/Core/Taxonomy/Labeler.pm ↑ Summary script/import-itol.pl »
1 package Bio::MUST::Core::SeqMask;
2 # ABSTRACT: Sequence mask for selecting specific sites
3 # CONTRIBUTOR: Catherine COLSON <ccolson@doct.uliege.be>
4 # CONTRIBUTOR: Raphael LEONARD <rleonard@doct.uliege.be>
5 18 $Bio::MUST::Core::SeqMask::VERSION = '0.252040';
6 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # use Smart::Comments '###';
13  
14 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use IPC::System::Simple qw(system);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use List::AllUtils 0.08 qw(uniq max sum natatime first_index last_index pairmap mesh bundle_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use Path::Class qw(file);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use Bio::MUST::Core::Constants qw(:gaps :files);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use Bio::MUST::Core::Utils qw(change_suffix);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use aliased 'Bio::MUST::Core::Seq';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use aliased 'Bio::MUST::Core::Ali';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use aliased 'Bio::MUST::Core::IdList';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 with 'Bio::MUST::Core::Roles::Commentable';
28  
29  
30 # public array
31 # Note: mask methods use the 0-based C/Perl array indexing
32 # ...but block methods encode bounds using the usual 1-based indexing
33 has 'mask' => (
34 traits => ['Array'],
35 is => 'ro',
36 isa => 'ArrayRef[Bool]',
37 17 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 18 writer => '_set_mask',
39 handles => {
40 mask_len => 'count',
41 all_states => 'elements',
42 add_state => 'push',
43 set_state => 'set',
44 state_at => 'get',
45 },
46 );
47  
48  
49  
50 2 ▶sub first_site {
Subroutine first_site: called CC=1 SCAR=0.0
Pod documented
51 2 my $self = shift;
52 2 ▶ return first_index { $_ } $self->all_states;
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54  
55  
56  
57 2 ▶sub last_site {
Subroutine last_site: called CC=1 SCAR=0.0
Pod documented
58 2 my $self = shift;
59 2 ▶ return last_index { $_ } $self->all_states;
Subroutine __ANON__: called CC=1 SCAR=0.0
60 }
61  
62  
63  
64 75 ▶sub count_sites {
Subroutine count_sites: called CC=1 SCAR=0.0
Pod documented
65 75 my $self = shift;
66 75 my $count = grep { $_ } $self->all_states;
67 75 return $count;
68 }
69  
70  
71  
72 2 ▶sub coverage {
Subroutine coverage: called CC=1 SCAR=0.0
Pod documented
73 2 my $self = shift;
74 2 return $self->count_sites / $self->mask_len;
75 }
76  
77  
78  
79 48 ▶sub empty_mask {
Subroutine empty_mask: called CC=2 SCAR=6.9
Pod documented
80 48 my $class = shift;
81 48 ▶ my $len = shift // 0;
Condition100%2/2
shift() // 0
l!l
471
MC/DC100%1/1
shift() // 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
82  
83 48 return $class->new( mask => [ (0) x $len ] );
84 }
85  
86  
87  
88 33 ▶sub custom_mask {
Subroutine custom_mask: called CC=2 SCAR=6.9
Pod documented
89 33 my $class = shift;
90 33 my $len = shift;
91 33 my $sites = shift;
92  
93 33 my $mask = $class->empty_mask($len);
94 33 $mask->set_state($_, 1) for @{$sites};
95  
96 33 return $mask;
97 }
98  
99  
100  
101 40 ▶sub mark_block { ## no critic (RequireArgUnpacking)
Subroutine mark_block: called CC=1 SCAR=0.0
Pod documented
102 40 return shift->_change_block(1, @_);
103 }
104  
105  
106  
107 0 ▶sub unmark_block { ## no critic (RequireArgUnpacking)
Subroutine unmark_block: not called CC=1 SCAR=6.9
Pod documented
108 0 return shift->_change_block(0, @_);
109 }
110  
111  
112 40 ▶sub _change_block {
Subroutine _change_block: called CC=2 SCAR=6.9
113 40 my $self = shift;
114 40 my $state = shift;
115 40 my $start = shift;
116 40 my $end = shift;
117  
118 40 $self->set_state($_, $state) for $start-1 .. $end-1;
119  
120 40 return $self;
121 }
122  
123  
124  
125 14 ▶sub mask2blocks {
Subroutine mask2blocks: called CC=6 SCAR=17.9
Pod documented
126 14 my $self = shift;
127  
128 14 my @blocks;
129 14 my $start = -1;
130 14 my $site = 0;
131 14 ▶ while ($site < $self->mask_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site < $self->mask_len) 217 14 231
132  
133 # open a new block (if not yet open)
134 217 ▶ if ( $self->state_at($site) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state_at($site)) { } 123 94 217
135 123 ▶ $start = $site+1 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 40 83 123
136 }
137  
138 # close the current block (if any open)
139 else {
140 94 ▶ if ($start >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start >= 0) 34 60 94
141 34 push @blocks, [ $start, $site ];
142 34 $start = -1;
143 }
144 }
145  
146 217 $site++;
147 }
148  
149 # close last block (if still open)
150 14 ▶ push @blocks, [ $start, $site ] if $start >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start >= 0 6 8 14
151  
152 14 return \@blocks;
153 }
154  
155  
156  
157 14 ▶sub blocks2mask {
Subroutine blocks2mask: called CC=2 SCAR=6.9
Pod documented
158 14 my $class = shift;
159 14 my $blocks = shift;
160  
161 # old code: only handled non-overlapping ordered blocks
162 # my @mask;
163 # for my $block ( @{$blocks} ) {
164 # my ($start, $end) = @{$block};
165 # push @mask, (0) x ($start-@mask-1); # pad last block
166 # push @mask, (1) x ($end-$start+1); # mark new block
167 # }
168 # return \@mask;
169  
170 # Note: blocks can now overlap and come in any order
171 # this allows using SeqMask for computing seq coverages
172  
173 # setup empty mask going up to max end (second slot)
174 14 my $max = max map { $_->[1] } @{$blocks};
175 14 my $mask = $class->empty_mask($max);
176  
177 # mark all states included in each block
178 14 $mask->mark_block( @{$_} ) for @{$blocks};
179  
180 14 return $mask;
181 }
182  
183  
184 # Ali-based SeqMask factory methods
185  
186  
187 1 ▶sub neutral_mask {
Subroutine neutral_mask: called CC=1 SCAR=0.0
Pod documented
188 1 my $class = shift;
189 1 my $ali = shift;
190  
191 1 my $width = $ali->width;
192 1 return $class->new( mask => [ (1) x $width ] );
193 }
194  
195  
196  
197 3 ▶sub variable_mask {
Subroutine variable_mask: called CC=3 SCAR=11.0
Pod documented
198 3 my $class = shift;
199 3 my $ali = shift;
200  
201 # TODO: profile and optimize as ideal_mask below (if needed)
202  
203 3 my @mask;
204 3 my $width = $ali->width;
205 3 my $regex = $ali->gapmiss_regex;
206  
207 # filter out sites with only one state
208 3 ▶ for (my $site = 0; $site < $width; $site++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site < $width) 53696 3 53699
209 my $state_n = uniq # count unique states
210 432032 grep { $_ !~ m/$regex/xms } # which are valid
211 53696 map { $_->state_at($site) } # and found at that site
212 $ali->all_seqs # across all seqs
213 ;
214 53696 ▶ push @mask, $state_n > 1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$state_n > 1 ? : 35213 18483 53696
215 }
216  
217 3 return $class->new( mask => \@mask );
218 }
219  
220  
221  
222 1 ▶sub parsimony_mask {
Subroutine parsimony_mask: called CC=4 SCAR=13.9
Pod documented
223 1 my $class = shift;
224 1 my $ali = shift;
225  
226 # TODO: profile and optimize as ideal_mask below (if needed)
227  
228 1 my @mask;
229 1 my $width = $ali->width;
230 1 my $regex = $ali->gapmiss_regex;
231  
232 # filter out sites with only one state
233 1 ▶ for (my $site = 0; $site < $width; $site++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($site < $width) 26232 1 26233
234 my @states = # get states
235 209856 grep { $_ !~ m/$regex/xms } # which are valid
236 26232 map { $_->state_at($site) } # and found at that site
237 $ali->all_seqs; # across all seqs
238 ;
239 #### @states
240  
241 # check whether at least two states are seen at least twice
242 26232 my %count_for;
243 26232 $count_for{$_}++ for @states;
244 #### %count_for
245 26232 my $state_n = grep { $count_for{$_} >= 2 } keys %count_for;
246 #### $state_n
247 26232 ▶ push @mask, $state_n >= 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$state_n >= 2 ? : 7118 19114 26232
248 }
249  
250 1 return $class->new( mask => \@mask );
251 }
252  
253  
254 13 ▶sub ideal_mask {
Subroutine ideal_mask: called CC=8 SCAR=20.8
Pod documented
255 13 my $class = shift;
256 13 my $ali = shift;
257 13 ▶ my $max_res = shift // 0; # defaults to shared gaps only
Condition50%1/2
shift() // 0
l!l
130
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
258  
259 # convert fractional max_res to conservative integer (if needed)
260 13 ▶ $max_res = floor($max_res * $ali->count_seqs)
Branch: 1 branch
BranchTrueFalseTotal
if 0 < $max_res and $max_res < 1 9 4 13
Condition66%2/3
0 < $max_res and $max_res < 1
!ll&&!rl&&r
049
MC/DC50%1/2
0 < $max_res and $max_res < 1
0 < $max_res $max_res < 1
Truth table66%2/3
A and B
A= 0 < $max_resB= $max_res < 1
ABresult
0X0
100
111
261 if 0 < $max_res && $max_res < 1;
262  
263 # pick up right regex based on sequence type
264 13 my $regex = $ali->gapmiss_regex;
265  
266 # determine count of non-gap-nor-missing states for each site
267  
268 # original version: easy to understand but much too slow on large matrices
269 # because of unavoidable regex recompilation for each character state
270 # (lots of calls to CORE::regcomp in output Devel::NYTProf)
271 # my @counts;
272 # for my $seq ($ali->all_seqs) {
273 # my $site = 0;
274 # $counts[$site++] += ($_ !~ $regex) for $seq->all_states;
275 # }
276  
277 # 'all magic comes with a price' version: the /o regex modifier does speed
278 # up things but compiles the regex once for all, which leads to bugs when
279 # dealing with both nucleotide and protein alignments in the same run
280 # my @counts;
281 # for my $seq ($ali->all_seqs) {
282 # my $site = 0;
283 # $counts[$site++] += ($_ !~ m/$regex/o) for $seq->all_states;
284 # }
285  
286 # improved version: more convoluted but much faster
287 # idea: directly search in sequence strings
288 # algorithm: set maximal count for each site
289 # then: decrease corrresponding count for each gap-or-missing state
290 # my @counts = ( $ali->count_seqs ) x $ali->width;
291 # for my $seq ($ali->all_seqs) {
292 # my $str = $seq->seq;
293 # while ($str =~ m/$regex/xmsg) {
294 # $counts[ pos($str) - 1 ]--;
295 # }
296 # }
297  
298 # parallel version: quite complex and inefficient
299 # setup threading
300 # my $thread_n = 2;
301 # my $pl = Parallel::Loops->new($thread_n);
302 # my %counts;
303 # $pl->share( \%counts );
304 #
305 # # define sites ranges
306 # my $site_n = $ali->width;
307 # my $chunk = ceil( $site_n / $thread_n );
308 # my @ranges;
309 # for (my $start = 0; $start < $site_n; $start += $chunk) {
310 # my $end = min( $start + $chunk, $site_n );
311 # push @ranges, [ $start..$end-1 ];
312 # }
313 #
314 # # process ranges in parallel
315 # my @seqs = $ali->all_seqs;
316 # $pl->foreach( \@ranges, sub {
317 # for my $seq (@seqs) {
318 # $counts{$_} += $seq->state_at($_) !~ $regex for @{$_};
319 # }
320 # } );
321 #
322 # # filter out sites with at most max_res non-gap-nor-missing states
323 # my @mask = map { $counts{$_} > $max_res ? 1 : 0 }
324 # sort { $a <=> $b } keys %counts; # watch the sorting cost!
325  
326 # current version: even more complex but even more faster
327 # idea: search for (long) gaps instead of individual states
328 # algorithm: set maximal count for each site
329 # then: decrease counts for each stretch of gap-or-missing states
330 13 my @counts = ( $ali->count_seqs ) x $ali->width;
331 13 for my $seq ($ali->all_seqs) {
332 160 my $str = $seq->seq;
333 160 ▶ while ($str =~ m/($regex+)/xmsg) { # look for next gap
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /($regex+)/gmsx) 7532 160 7692
334 7532 my $end = pos($str) - 1; # compute gap start
335 7532 my $begin = $end - length($1) + 1; # and gap end
336 7532 $counts[$_]-- for $begin..$end; # decrease count over the gap
337 }
338 }
339  
340 # filter out sites with at most max_res non-gap-nor-missing states
341 13 ▶ my @mask = map { $_ > $max_res ? 1 : 0 } @counts;
Branch: 1 branch
BranchTrueFalseTotal
$_ > $max_res ? : 63822 22333 86155
342  
343 13 return $class->new( mask => \@mask );
344 }
345  
346  
347 18 my %gb_parms_for = (
348 strict => [ 0.85, 8, 10, 'N' ], # emulate original MUST settings
349 medium => [ 0.75, 5, 5, 'H' ],
350 loose => [ 0.55, 5, 5, 'H' ],
351 );
352  
353 0 ▶sub gblocks_mask {
Subroutine gblocks_mask: not called CC=10 SCAR=47.0
Pod documented
354 0 my $class = shift;
355 0 my $ali = shift;
356 0 ▶ my $mode = shift // 'strict';
Condition0%0/2
shift() // "strict"
l!l
00
MC/DC0%0/1
shift() // "strict"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
357  
358 # check Gblocks settings
359 0 ▶ if ( !defined $gb_parms_for{$mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $gb_parms_for{$mode}) 0 0 0
360 0 carp "[BMC] Warning: invalid Gblocks settings: $mode; using strict!";
361 0 $mode = 'strict';
362 }
363  
364 # create temp .fasta infile for Gblocks
365 0 my $infile = $ali->temp_fasta;
366  
367 # compute Gblocks parms depending on specified mode
368 my ($t, $b1, $b2, $b3, $b4, $b5) = (
369 ($ali->is_protein ? 'p' : 'd'), # Sequence Type
370 int($ali->count_seqs / 2) + 1, # Conserved Position
371 int($ali->count_seqs * $gb_parms_for{$mode}[0]), # Flank Position
372 0 ▶ @{ $gb_parms_for{$mode} }[1..3], # other block parms
Branch: 1 branch
BranchTrueFalseTotal
$ali->is_protein ? : 0 0 0
373 );
374 0 $b2 = max($b1, $b2); # with few seqs 0.55*n can be smaller than n/2+1
375  
376 # create Gblocks command
377 0 my $cmd = "Gblocks $infile -t=$t"
378 . " -b1=$b1 -b2=$b2 -b3=$b3 -b4=$b4 -b5=$b5"
379 . ' -s=n > /dev/null 2> /dev/null' # minimal output
380 ;
381  
382 # try to robustly execute Gblocks
383 0 my $ret_code = system( [ 1, 127 ], $cmd); # Gblocks always returns 1?!?
384 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
385 0 carp '[BMC] Warning: cannot execute Gblocks command;'
386 . ' returning neutral mask!';
387 0 return $class->neutral_mask($ali);
388 }
389 # TODO: try to bypass shell (need for absolute path to executable then)
390  
391 # parse Gblocks output to get blocks
392 0 my $outfile = $infile . '-gb.htm';
393 0 open my $out, '<', $outfile;
394  
395 0 my @blocks;
396 0 ▶ while (my $line = <$out>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $out)) 0 0 0
397  
398 # only use the 'Flanks:' line
399 0 my ($flanks) = $line =~ m/\AFlanks:\s+(.*)/xms;
400 0 ▶ if (defined $flanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flanks) 0 0 0
401  
402 # isolate numbers
403 0 chomp $flanks;
404 0 $flanks =~ s/[\[\]]//xmsg;
405 0 my @bound_pairs = split /\s+/xms, $flanks;
406  
407 # take number pairs as block bounds
408 0 my $it = natatime 2, @bound_pairs;
409 0 ▶ while ( my @bounds = $it->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bounds) = &$it()) 0 0 0
410 0 push @blocks, \@bounds;
411 }
412 }
413  
414 # ...and the 'New number of positions' line
415 # to check Gblocks didn't shrink the original Ali due to shared gaps
416 # which would left-shift all blocks bounds!
417 0 my ($width) = $line =~ m/original\s+(\d+)\s+positions/xms;
418 0 ▶ if (defined $width && $width != $ali->width) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $width and $width != $ali->width) 0 0 0
Condition0%0/3
defined $width and $width != $ali->width
!ll&&!rl&&r
000
MC/DC0%0/2
defined $width and $width != $ali->width
defined $width $width != $ali->width
Truth table0%0/3
A and B
A= defined $widthB= $width != $ali->width
ABresult
0X0
100
111
419 0 carp '[BMC] Warning: shared gaps detected; returning neutral mask!';
420 0 return $class->neutral_mask($ali);
421 }
422 }
423  
424 # unlink temp files
425 0 file( $infile)->remove;
426 0 file($outfile)->remove;
427  
428 # compute SeqMask from Gblocks blocks
429 0 return $class->blocks2mask( \@blocks );
430 }
431  
432  
433 # TODO: check interface for BMGE 2.x
434 18 my %bmge_parms_for = (
435 strict => [ 0.4, 0.05 ],
436 medium => [ 0.5, 0.20 ],
437 loose => [ 0.6, 0.40 ],
438 );
439  
440 0 ▶sub bmge_mask {
Subroutine bmge_mask: not called CC=7 SCAR=40.3
Pod documented
441 0 my $class = shift;
442 0 my $ali = shift;
443 0 ▶ my $mode = shift // 'strict';
Condition0%0/2
shift() // "strict"
l!l
00
MC/DC0%0/1
shift() // "strict"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
444  
445 # check BMGE settings
446 0 ▶ if ( !defined $bmge_parms_for{$mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bmge_parms_for{$mode}) 0 0 0
447 0 carp "[BMC] Warning: invalid BMGE settings: $mode; using strict!";
448 0 $mode = 'strict';
449 }
450  
451 # create temp .fasta infile for BMGE
452 0 my $infile = $ali->temp_fasta;
453  
454 # compute BMGE parms depending on specified mode
455 0 my ($entropy_cutoff, $gap_cutoff) = @{ $bmge_parms_for{$mode} };
456 0 ▶ my $coding_type = $ali->is_protein ? 'AA' : 'DNA';
Branch: 1 branch
BranchTrueFalseTotal
$ali->is_protein ? : 0 0 0
457 0 my $outfile = $infile . '-bmge.htm';
458  
459 # create BMGE command
460 0 my $cmd = "bmge.sh $infile"
461 . " $coding_type $entropy_cutoff $gap_cutoff $outfile > /dev/null";
462  
463 # try to robustly execute BMGE
464 0 my $ret_code = system( [ 0, 127 ], $cmd);
465 0 ▶ if ($ret_code == 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_code == 127) 0 0 0
466 0 carp '[BMC] Warning: cannot execute BMGE command;'
467 . ' returning neutral mask!';
468 0 return $class->neutral_mask($ali);
469 }
470 # TODO: try to bypass shell (need for absolute path to executable then)
471  
472 # parse BMGE output to get selected sites
473 0 open my $out, '<', $outfile;
474 0 my $len = $ali->width;
475 0 my $sites;
476  
477 LINE:
478 0 ▶ while (my $line = <$out>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $out)) 0 0 0
479 0 ▶ if ($line =~ m/\s+ selected: \s+ ([\ (0-9)]+)/xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s+ selected: \s+ ([\ (0-9)]+)/msx) 0 0 0
480 0 $sites = [ map { $_ - 1 } split /\s+/xms, $1 ];
481 0 last LINE;
482 }
483 }
484  
485 # unlink temp files
486 0 file($infile)->remove;
487 0 file($outfile)->remove;
488  
489 0 return $class->custom_mask($len, $sites);
490 }
491  
492  
493 # mask manipulation methods
494 # TODO: improve interface? $mask1 should be $self for and_mask and or_mask
495  
496  
497 1 ▶sub and_mask {
Subroutine and_mask: called CC=1 SCAR=0.0
Pod documented
498 1 my $class = shift;
499 1 my $mask1 = shift;
500 1 my $mask2 = shift;
501  
502 1 my @mask;
503 1 ▶ @mask = pairmap { ($a && $b) // 0 } mesh ( @{$mask1}, @{$mask2} );
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%5/5
$a && $b // 0
l!l
112
$a and $b
!ll&&!rl&&r
643
MC/DC100%2/2
$a && $b // 0
$a $b
Truth table100%3/3
A and B
A= $aB= $b
ABresult
111
0X0
100
504  
505 1 return $class->new( mask => \@mask );
506 }
507  
508  
509 1 ▶sub or_mask {
Subroutine or_mask: called CC=1 SCAR=0.0
Pod documented
510 1 my $class = shift;
511 1 my $mask1 = shift;
512 1 my $mask2 = shift;
513  
514 1 my @mask;
515 1 ▶ @mask = pairmap { ($a || $b) // 0 } mesh ( @{$mask1}, @{$mask2} );
Subroutine __ANON__: called CC=3 SCAR=11.0
Condition100%5/5
($a || $b) // 0
l!l
121
$a or $b
l!l&&r!l&&!r
715
MC/DC100%2/2
($a || $b) // 0
$a $b
Truth table100%3/3
A or B
A= $aB= $b
ABresult
1X1
011
000
516  
517 1 return $class->new( mask => \@mask );
518 }
519  
520  
521 13 ▶sub negative_mask {
Subroutine negative_mask: called CC=1 SCAR=0.0
Pod documented
522 13 my $self = shift;
523 13 my $ali = shift;
524  
525 # negate all states over the whole Ali width
526 # Note the '+ 0' to ensure proper numeric context (0 or 1)
527 13 my $width = $ali->width;
528 13 my @mask = map { ( not $self->state_at($_) ) + 0 } 0..$width-1;
529  
530 13 return $self->new( mask => \@mask );
531 }
532  
533  
534 1 ▶sub codon_mask { ## no critic (RequireArgUnpacking)
Subroutine codon_mask: called CC=5 SCAR=16.6
Pod documented
535 1 my $self = shift; # the critic stems from @_ below
536 1 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
10
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
537  
538 1 ▶ my $frame = $args->{frame} // 1; # defaults to frame +1
Condition50%1/2
$args->{"frame"} // 1
l!l
10
MC/DC0%0/1
$args->{"frame"} // 1
$args->{"frame"}
Truth table50%1/2
A
A= $args->{"frame"}
Aresult
11
00
539 1 ▶ my $chunk = $args->{chunk} // 3; # defaults to codon
Condition50%1/2
$args->{"chunk"} // 3
l!l
01
MC/DC0%0/1
$args->{"chunk"} // 3
$args->{"chunk"}
Truth table50%1/2
A
A= $args->{"chunk"}
Aresult
11
00
540 1 ▶ my $max = $args->{max} // 1;
Condition50%1/2
$args->{"max"} // 1
l!l
10
MC/DC0%0/1
$args->{"max"} // 1
$args->{"max"}
Truth table50%1/2
A
A= $args->{"max"}
Aresult
11
00
541  
542 1 my @states = $self->all_states;
543 1 my $offset = (abs $frame) - 1;
544 my @mask = ( (0) x $offset, # first skip frame-1 sites
545 1 ▶ map { ( ($_ > $max) + 0 ) x $chunk } bundle_by { sum @_ } $chunk,
Subroutine __ANON__: called CC=1 SCAR=0.0
546 @states[$offset..$#states] # then filter sites by chunks
547 );
548  
549 1 return $self->new( mask => [ @mask[0..$#states] ] );
550 } # truncate mask to original length
551  
552  
553 # SeqMask-based Ali factory methods
554  
555  
556 26 ▶before 'filtered_ali' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
557 ## no critic (ProtectPrivateSubs)
558 26 return Bio::MUST::Core::Ali::_premask_check( @_[1,0,2] ); # swap args
559 ## use critic
560 18 };
561  
562 26 ▶sub filtered_ali {
Subroutine filtered_ali: called CC=8 SCAR=20.8
563 26 my $self = shift;
564 26 my $ali = shift;
565 26 my $list = shift; # optional: enable masking vs. filtering
566  
567 # setup filtering or masking
568 26 ▶ my $char = $list ? 'X' : q{};
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 1 25 26
569  
570 # create new Ali object (extending header comment)
571 # TODO: allow custom comments
572 26 ▶ my $new_ali = Ali->new(
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 1 25 26
573 comments => [ $ali->all_comments,
574 'built by ' . ($list ? 'masked_ali' : 'filtered_ali')
575 ],
576 );
577  
578 SEQ:
579 26 for my $seq ($ali->all_seqs) {
580  
581 # for non-listed Seqs clone Seq to new Ali
582 # Note: when filtering all Seqs will be affected
583 577 ▶ if ( $list and not $list->is_listed($seq->full_id) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $list->is_listed($seq->full_id)) 8 569 577
Condition100%3/3
$list and not $list->is_listed($seq->full_id)
!ll&&!rl&&r
56728
MC/DC100%2/2
$list and not $list->is_listed($seq->full_id)
$list not $list->is_listed($seq->full_id)
Truth table100%3/3
A and not(B)
A= $listB= not $list->is_listed($seq->full_id)
ABresult
0X0
100
111
584 8 $new_ali->add_seq($seq->clone);
585 8 next SEQ;
586 }
587  
588 # for listed Seqs copy marked sites from original Seq...
589 # ... and either filter or mask others (set them to missing state)
590 569 my $new_seq;
591 569 my $site = 0;
592 569 for my $state ($self->all_states) {
593 1955599 ▶ $new_seq .= $state ? $seq->state_at($site) x $state : $char;
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 590995 1364604 1955599
594 1955599 $site++; # x $state is for Weights objects
595 }
596  
597 # add Seq to new Ali
598 $new_ali->add_seq(
599 569 Seq->new( seq_id => $seq->full_id, seq => $new_seq )
600 );
601 }
602  
603 26 return $new_ali;
604 }
605  
606  
607 # I/O methods
608  
609  
610 1 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod documented
611 1 my $class = shift;
612 1 my $infile = shift;
613  
614 1 open my $in, '<', $infile;
615  
616 1 my $mask = $class->new();
617  
618 LINE:
619 1 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 26234 1 26235
620 26234 chomp $line;
621  
622 # skip empty lines and process comment lines
623 26234 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line) 2 26232 26234
Condition66%2/3
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line)
l!l&&r!l&&!r
0226232
MC/DC50%1/2
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line)
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ $mask->is_comment($line)
Truth table66%2/3
A or B
A= $line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/B= $mask->is_comment($line)
ABresult
1X1
011
000
624 || $mask->is_comment($line);
625  
626 26232 $mask->add_state($line);
627 }
628  
629 1 return $mask;
630 }
631  
632  
633  
634 4 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
635 4 my $self = shift;
636 4 my $outfile = shift;
637  
638 4 open my $out, '>', $outfile;
639  
640 4 print {$out} $self->header;
641 4 say {$out} join "\n", $self->all_states;
642  
643 4 close $out;
644  
645 4 return;
646 }
647  
648  
649  
650 # sub load_bor {
651 # #~ my $self = shift;
652 # my $class = shift;
653 # my $bor = shift;
654 # my $ali = shift;
655 #
656 # #~ my $class = 'Bio::MUST::Core::SeqMask';
657 #
658 # #~ open my $in, '<', $infile;
659 # my $bor_file = $bor->stringify;
660 # #~ tie my @rows, 'Tie::File', $bor_file;
661 # #~ tie my @rows, 'Tie::File', $in;
662 # #~ my $delims = $rows[-1];
663 # #~ ### $delims
664 # my $delims;
665 # open my $bof, '<', $bor_file;
666 # while ( my $i = <$bof> ) { $delims = $i; }
667 # my @blocks = pairmap { [ $a , $b ] } ( split ' ', $delims );
668 # #~ ### @blocks
669 # my $mask = $class->blocks2mask(\@blocks)->mask;
670 # #~ ### $mask
671 # my $seq_mask = $class->new( mask => $mask);
672 # my $negative = $seq_mask->negative_mask($ali);
673 # return $negative;
674 # }
675  
676  
677  
678  
679  
680  
681 1 ▶sub store_una {
Subroutine store_una: called CC=3 SCAR=11.0
Pod documented
682 1 my $self = shift;
683 1 my $outfile = shift;
684 1 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
10
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
685  
686 # TODO: check arguments as there are no default values?
687 1 my $ali = $args->{ali};
688 1 my $id = $args->{id};
689  
690 # search for non-gap positions in reference seq
691 1 my $seq = $ali->get_seq_with_id($id);
692 1 my @pos_bases = grep { !( $seq->is_gap($_) ) } 0..($seq->seq_len-1);
693  
694 # delete gap positions in mask
695 1 my @mask_degap = @{ $self->mask }[ @pos_bases ];
696  
697 # build mask and compute blocks
698 1 my $new_mask = $self->new( mask => \@mask_degap );
699 1 my $new_blocks = $new_mask->mask2blocks;
700  
701 1 open my $out, '>', $outfile;
702  
703 1 my $filename = $ali->file;
704 1 my ($basename, $dir, $ext) = fileparse($filename, qr{\.[^.]*}xms);
705  
706 1 say {$out} "Unambigous numbering for $basename$ext based on $id";
707 1 for my $new_block ( @{$new_blocks} ) {
708 3 say {$out} join '-', @{$new_block};
709 }
710  
711 1 close $out;
712  
713 1 return;
714 }
715  
716  
717 2 ▶sub load_blocks {
Subroutine load_blocks: called CC=4 SCAR=13.9
Pod documented
718 2 my $class = shift;
719 2 my $infile = shift;
720  
721 2 open my $in, '<', $infile;
722  
723 2 my $mask = $class->new();
724 2 my @blocks;
725  
726 LINE:
727 2 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 10 2 12
728 10 chomp $line;
729  
730 # skip empty lines and process comment lines
731 10 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line) 5 5 10
Condition100%3/3
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line)
l!l&&r!l&&!r
145
MC/DC100%2/2
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ or $mask->is_comment($line)
$line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/ $mask->is_comment($line)
Truth table100%3/3
A or B
A= $line =~ /$Bio::MUST::Core::SeqMask::EMPTY_LINE/B= $mask->is_comment($line)
ABresult
1X1
011
000
732 || $mask->is_comment($line);
733  
734 # process block specifications (one or more by line)
735 5 ▶ push @blocks, pairmap { [ $a , $b ] } split /\s+/xms, $line;
Subroutine __ANON__: called CC=1 SCAR=0.0
736 }
737  
738 # build mask from blocks and replace empty mask
739 # this is needed to preserve potential comments
740 2 $mask->_set_mask( $class->blocks2mask( \@blocks )->mask );
741  
742 2 return $mask;
743 }
744  
745  
746 1 ▶sub store_blocks {
Subroutine store_blocks: called CC=2 SCAR=6.9
Pod documented
747 1 my $self = shift;
748 1 my $outfile = shift;
749  
750 1 open my $out, '>', $outfile;
751  
752 1 say {$out} "# Coordinates of blocks \n#";
753  
754 # compute blocks from mask
755 1 my $blocks = $self->mask2blocks;
756  
757 1 for my $block ( @{$blocks} ) {
758 6 say {$out} join "\t", @{$block};
759 }
760  
761 1 close $out;
762  
763 1 return;
764 }
765  
766 18 __PACKAGE__->meta->make_immutable;
767 18 1;
768  
769 __END__
« lib/Bio/MUST/Core/Taxonomy/Labeler.pm ↑ Summary script/import-itol.pl »