lib/Bio/MUST/Core/SeqMask/Rates.pm

filter: Statementstmt 100% 104 / 104 Branchbran 90.9% 20 / 22 Conditioncond 90.0% 18 / 20 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 96.1% 173 / 180 CC 22 SCAR 19.2
CC
22
Cov
97%
bin_rates_masks
14.0
load
6.0
delta_rates
3.0
CRAP 6.8
« lib/Bio/MUST/Core/Taxonomy/Filter.pm ↑ Summary lib/Bio/MUST/Core/Taxonomy/Category.pm »
1 package Bio::MUST::Core::SeqMask::Rates;
2 # ABSTRACT: Evolutionary rates for sequence sites
3 18 $Bio::MUST::Core::SeqMask::Rates::VERSION = '0.252040';
4 18 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # use Smart::Comments;
11  
12 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use List::AllUtils qw(each_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 18 extends 'Bio::MUST::Core::SeqMask';
18  
19 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use Bio::MUST::Core::Constants qw(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use aliased 'Bio::MUST::Core::SeqMask';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # override superclass' Bool type
25 # Note: mask indices are as follow: [site]
26 # mask values are rates
27 18 has '+mask' => (
28 isa => 'ArrayRef[Num]',
29 );
30  
31 # TODO: mask non-applicable methods from superclass? (Liskov principle)
32  
33  
34  
35 5 ▶sub min_rate {
Subroutine min_rate: called CC=1 SCAR=0.0
Pod documented
36 5 my $self = shift;
37 5 return List::AllUtils::min @{ $self->mask };
38 }
39  
40  
41  
42 5 ▶sub max_rate {
Subroutine max_rate: called CC=1 SCAR=0.0
Pod documented
43 5 my $self = shift;
44 5 return List::AllUtils::max @{ $self->mask };
45 }
46  
47  
48  
49 1 ▶sub delta_rates {
Subroutine delta_rates: called CC=3 SCAR=11.0
Pod documented
50 1 my $self = shift;
51 1 my $othr = shift;
52  
53 # check that both rates objects are the same length
54 # potential bugs could come from constant sites etc
55 1 my $s_width = $self->mask_len;
56 1 my $o_width = $othr->mask_len;
57 1 ▶ carp "[BMC] Warning: Rates widths do not match: $s_width vs. $o_width!"
Branch: 1 branch
BranchTrueFalseTotal
unless $s_width == $o_width 0 1 1
58 unless $s_width == $o_width;
59  
60 1 my @deltas;
61  
62 1 my $ea = each_arrayref [ $self->all_states ], [ $othr->all_states ];
63 1 ▶ while (my ($s_rate, $o_rate) = $ea->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$ea()) 22436 1 22437
64 22436 push @deltas, 0 + ( sprintf "%.13f", abs( $s_rate - $o_rate ) );
65 } # Note: trick to get identical results across platforms
66  
67 # TODO: check that $self->new() is really correct
68 1 return $self->new( mask => \@deltas );
69 }
70  
71  
72 # Rates-based SeqMask factory methods
73  
74  
75 # small delta for slightly increasing extreme bins
76 18 const my $DELTA => 1e-13;
77  
78 9 ▶sub bin_rates_masks {
Subroutine bin_rates_masks: called CC=14 SCAR=26.4
Pod documented
79 9 my $self = shift;
80 9 my $bin_n = shift;
81 9 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition50%1/2
shift() // {}
l!l
90
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
82  
83 9 ▶ my $percentile = $args->{percentile} // 0;
Condition100%2/2
$args->{"percentile"} // 0
l!l
54
MC/DC100%1/1
$args->{"percentile"} // 0
$args->{"percentile"}
Truth table100%2/2
A
A= $args->{"percentile"}
Aresult
11
00
84 9 ▶ my $cumulative = $args->{cumulative} // 0;
Condition100%2/2
$args->{"cumulative"} // 0
l!l
45
MC/DC100%1/1
$args->{"cumulative"} // 0
$args->{"cumulative"}
Truth table100%2/2
A
A= $args->{"cumulative"}
Aresult
11
00
85 9 ▶ my $descending = $args->{descending} // 0;
Condition100%2/2
$args->{"descending"} // 0
l!l
81
MC/DC100%1/1
$args->{"descending"} // 0
$args->{"descending"}
Truth table100%2/2
A
A= $args->{"descending"}
Aresult
11
00
86  
87 9 my @masks;
88  
89 # define bin bounds based on equal count (in terms of sites)
90 9 ▶ if ($percentile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($percentile) { } 5 4 9
91  
92 # create rates-sorted index of sites (from slow to fast)
93 my @index = sort {
94 5 $self->state_at($a) <=> $self->state_at($b)
95 } 0..$self->mask_len-1;
96  
97 # optionally reverse index: higher values mean slower rates (TIGER)
98 5 ▶ @index = reverse @index if $descending;
Branch: 1 branch
BranchTrueFalseTotal
if $descending 4 1 5
99  
100 # compute masks from index slices
101 5 my $step = ceil( @index / $bin_n );
102 ### $step
103 5 for my $i (0..$bin_n-1) {
104 32 ▶ my $min = $cumulative ? 0 : ($i * $step);
Branch: 1 branch
BranchTrueFalseTotal
$cumulative ? : 11 21 32
105 32 my $max = (($i+1) * $step - 1);
106 32 ▶ $max = $#index if $max > $#index;
Branch: 1 branch
BranchTrueFalseTotal
if $max > $#index 5 27 32
107 ### min: $min
108 ### max: $max
109 ### rates: map { $self->state_at($_) } @index[ $min..$max ]
110 32 push @masks, SeqMask->custom_mask(
111 $self->mask_len, [ @index[ $min..$max ] ]
112 );
113  
114 }
115 }
116  
117 # define bin bounds based on equal width (in terms of rates)
118 else {
119 4 my @bounds;
120  
121 # compute bin bounds from rate range
122 4 my $min = $self->min_rate;
123 4 my $max = $self->max_rate;
124 4 my $step = ($max - $min) / $bin_n;
125 4 ▶ for (my $i = $min + $step; $i <= $max; $i += $step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 40 4 44
126 40 push @bounds, $i;
127 }
128 # Note: did try to use Statistics::Descriptive with no luck
129  
130 # add lower bound for first bin...
131 # ... and small delta to first/last bins for catching min/max values
132 4 unshift @bounds, $min - $DELTA;
133 4 $bounds[-1] += $DELTA;
134  
135 # optionally reverse bounds: higher values mean slower rates (TIGER)
136 4 ▶ @bounds = reverse @bounds if $descending;
Branch: 1 branch
BranchTrueFalseTotal
if $descending 4 0 4
137 ### @bounds
138  
139 # derive masks from bin bounds
140 4 for my $i (1..$#bounds) {
141 40 ▶ push @masks, $self->rates_mask(
Branch: 1 branch
BranchTrueFalseTotal
$cumulative ? : 20 20 40
142 $cumulative ? $bounds[0] : $bounds[$i - 1], # bin min
143 $bounds[$i] # bin max
144 );
145 }
146 }
147  
148 9 return @masks;
149 }
150  
151  
152  
153 49 ▶sub rates_mask {
Subroutine rates_mask: called CC=2 SCAR=6.9
Pod documented
154 49 my $self = shift;
155 49 my $min = shift;
156 49 my $max = shift;
157  
158 # ensure min and max are correctly ordered
159 49 ($min, $max) = sort ($min, $max);
160  
161 # filter out sites not within (bin) bounds
162 my @mask = map {
163 49 (
164 45085 ▶ $self->state_at($_) > $min
Condition100%3/3
$self->state_at($_) > $min && $self->state_at($_) <= $max
!ll&&!rl&&r
147031255717825
MC/DC100%2/2
$self->state_at($_) > $min && $self->state_at($_) <= $max
$self->state_at($_) > $min $self->state_at($_) <= $max
Truth table100%3/3
A && B
A= $self->state_at($_) > $minB= $self->state_at($_) <= $max
ABresult
0X0
100
111
165 && $self->state_at($_) <= $max
166 ) + 0 # ensure proper numeric context (0 or 1)
167 } 0..$self->mask_len-1;
168  
169 49 return SeqMask->new( mask => \@mask );
170 }
171  
172  
173 # I/O methods
174  
175  
176 # TODO: avoid duplicating code with SeqMask class
177  
178 # TIGER format:
179 # 0.651325757576
180 # 0.633143939394
181 # 0.488257575758
182 # 1.0
183 # ...
184  
185 # PhyloBayes (MPI) format:
186 # 0 15.5174
187 # 1 16.4429
188 # 2 18.664
189 # 3 30.1682
190 # ...
191  
192 # PhyloBayes (serial) format:
193 # site rate std error
194 #
195 # 1 5.86191 0.207244
196 # 2 7.35053 0.350185
197 # 3 6.09158 0.24426
198 # ...
199  
200 # IQ-TREE format:
201 # # Site-specific subtitution rates determined by empirical Bayesian method
202 # # This file can be read in MS Excel or in R with command:
203 # # tab=read.table('replica-3-concat-modified.fasta.rate',header=TRUE)
204 # # Columns are tab-separated with following meaning:
205 # # Site: Alignment site ID
206 # # Rate: Posterior mean site rate weighted by posterior probability
207 # # Cat: Category with highest posterior (0=invariable, 1=slow, etc)
208 # # C_Rate: Corresponding rate of highest category
209 # Site Rate Cat C_Rate
210 # 1 0.31154 1 0.30935
211 # 2 1.03326 4 1.91612
212 # 3 1.39822 4 1.91612
213 # ...
214  
215 18 const my %COLUMN_FOR => (
216 1 => 0, # TIGER
217 2 => 1, # PhyloBayes (MPI)
218 3 => 1, # PhyloBayes (serial)
219 4 => 1, # IQ-TREE
220 );
221  
222 12 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod documented
223 12 my $class = shift;
224 12 my $infile = shift;
225  
226 12 open my $in, '<', $infile;
227  
228 12 my $mask = $class->new();
229 12 my $col;
230  
231 LINE:
232 12 ▶ while (my $line = <$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $in)) 71734 12 71746
233 71734 chomp $line;
234  
235 # skip empty lines, header line and process comment lines
236 71734 ▶ next LINE if $line =~ $EMPTY_LINE
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/ or $line =~ /^site/imsx or $mask->is_comment($line) 11 71723 71734
Condition100%6/6
$line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/ or $line =~ /^site/imsx or $mask->is_comment($line)
l!l&&r!l&&!r
3871723
$line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/ or $line =~ /^site/imsx
l!l&&r!l&&!r
1271731
MC/DC100%3/3
$line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/ or $line =~ /^site/imsx or $mask->is_comment($line)
$line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/ $line =~ /^site/imsx $mask->is_comment($line)
Truth table100%4/4
A or B or C
A= $line =~ /$Bio::MUST::Core::SeqMask::Rates::EMPTY_LINE/B= $line =~ /^site/imsxC= $mask->is_comment($line)
ABCresult
1XX1
01X1
0011
0000
237 || $line =~ m/^site/xmsi # PhyloBayes/IQ-TREE
238 || $mask->is_comment($line);
239  
240 # try to split line on whitespace
241 71723 my @fields = split /\s+/xms, $line;
242 71723 ▶ $col //= $COLUMN_FOR{ scalar @fields };
Condition66%2/3
$col //= $COLUMN_FOR{scalar @fields}
l!l&&r!l&&!r
71711120
MC/DC0%0/2
$col //= $COLUMN_FOR{scalar @fields}
$col $COLUMN_FOR{scalar @fields}
Truth table66%2/3
A //= B
A= $colB= $COLUMN_FOR{scalar @fields}
ABresult
1X1
011
000
243  
244 # store either first or second field depending on split outcome
245 # Note: we check this outcome only once for efficiency
246 71723 $mask->add_state( $fields[$col] );
247 }
248  
249 12 return $mask;
250 }
251  
252 18 __PACKAGE__->meta->make_immutable;
253 18 1;
254  
255 __END__
« lib/Bio/MUST/Core/Taxonomy/Filter.pm ↑ Summary lib/Bio/MUST/Core/Taxonomy/Category.pm »