Barriers/RateMatrix.pm

filter: Statementstmt 78.4% 182 / 232 Branchbran 44.8% 35 / 78 Conditioncond 40.7% 11 / 27 MC/DCmcdc 5.8% 1 / 17 Subroutinesub 78.3% 29 / 37 Podpod 92.8% 13 / 14 totaltotal 66.9% 271 / 405 CC 66 SCAR 26.5
CC
66
Cov
68%
read_text_rate_matrix
34.3
connected_states
30.0
remove_states
30.0
CRAP 14.1
↑ Summary Barriers/Results.pm »
1 package Bio::RNA::Barriers::RateMatrix;
2 12 our $VERSION = '0.03';
3  
4 12 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use Moose::Util::TypeConstraints qw(enum subtype as where message);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 12 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Scalar::Util qw( reftype looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use List::Util qw( all uniqnum );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 enum __PACKAGE__ . 'RateMatrixType', [qw(TXT BIN)];
19  
20 # Natural number type directly from the Moose docs.
21 subtype 'PosInt',
22 as 'Int',
23 0 ▶ where { $_ > 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 ▶ message { "The number you provided, $_, was not a positive number" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
25  
26 12 has 'file_name' => (
27 is => 'ro',
28 isa => 'Str',
29 predicate => 'has_file_name',
30 );
31 12 has 'file_type' => (
32 is => 'rw',
33 isa => __PACKAGE__ . 'RateMatrixType',
34 required => 1,
35 );
36 12 has '_file_handle' => (
37 is => 'ro',
38 isa => 'FileHandle',
39 init_arg => 'file_handle',
40 lazy => 1,
41 builder => '_build_file_handle',
42 );
43 # Splice rate matrix directly when reading the data. This can read big
44 # matrices when only keeping a few entries.
45 12 has 'splice_on_parsing' => (
46 is => 'ro',
47 isa => 'ArrayRef[PosInt]',
48 predicate => 'was_spliced_on_parsing',
49 );
50 12 has '_data' => (is => 'ro', lazy => 1, builder => '_build_data');
51  
52 11 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
53 11 my $self = shift;
54  
55 # Enforce data is read from handle immediately despite laziness.
56 11 $self->dim;
57 }
58  
59 # Read the actual rate data from the input file and construct the
60 # matrix from it.
61 11 ▶sub _build_data {
Subroutine _build_data: called CC=3 SCAR=11.2
62 11 my $self = shift;
63  
64 11 my $rate_matrix;
65 11 ▶ if ($self->file_type eq 'TXT') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->file_type eq 'TXT') { } 6 5 11
elsif ($self->file_type eq 'BIN') { } 5 0 5
66 6 $rate_matrix = __PACKAGE__->read_text_rate_matrix(
67 $self->_file_handle,
68 $self->splice_on_parsing,
69 );
70 }
71 elsif ($self->file_type eq 'BIN') {
72 5 $rate_matrix = __PACKAGE__->read_bin_rate_matrix(
73 $self->_file_handle,
74 $self->splice_on_parsing,
75 );
76 }
77 else {
78 0 confess "Unknown file type, that's a bug...";
79 }
80  
81 9 return $rate_matrix;
82 }
83  
84 # Class method. Reads a rate matrix in text format from the passed file
85 # handle and constructs a matrix (2-dim array) from it. Returns a
86 # reference to the constructed rate matrix.
87 # Arguments:
88 # input_matrix_fh: file handle to text file containing rate matrix
89 # splice_to: ORDERED set of states which are to be kept. The other
90 # states are pruned from the matrix on-the-fly while parsing.
91 # This saves time and memory.
92 6 ▶sub read_text_rate_matrix {
Subroutine read_text_rate_matrix: called CC=18 SCAR=35.4
Pod documented
93 6 my ($class, $input_matrix_fh, $splice_to_ref) = @_;
94  
95 # During parsing, splice the selected rows / columns. Make 0-based.
96 6 ▶ my @splice_to_rows = @{ $splice_to_ref // [] }; # 1-based, modified
Condition50%1/2
$splice_to_ref // []
l!l
06
MC/DC0%0/1
$splice_to_ref // []
$splice_to_ref
Truth table50%1/2
A
A= $splice_to_ref
Aresult
11
00
97 0 my @splice_to_cols = map {$_ - 1} @splice_to_rows; # 0-based indices
98  
99 6 my (@rate_matrix, $matrix_dim);
100 6 ▶ ROW: while (defined (my $line = <$input_matrix_fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input_matrix_fh)) 16 5 21
101 16 ▶ if (defined $splice_to_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $splice_to_ref) 0 16 16
102 0 ▶ last unless @splice_to_rows; # we're done!
Branch: 1 branch
BranchTrueFalseTotal
unless @splice_to_rows 0 0 0
103 0 ▶ next ROW if $. != $splice_to_rows[0]; # this row is not kept
Branch: 1 branch
BranchTrueFalseTotal
if $. != $splice_to_rows[0] 0 0 0
104 0 shift @splice_to_rows; # remove the leading index
105 }
106  
107 16 my @row = split q{ }, $line; # awk-style splitting
108 # Since the diagonal element may be more or less anything, we need
109 # to check it separately (e.g. to not choke on BHGbuilder output).
110 16 my @row_no_diag = @row[0..($.-2), ($.)..$#row]; # $. is 1-based
111 confess 'Input file contains non-numeric or negative input on ',
112 "line $.:\n$line"
113 unless looks_like_number $row[$.-1] # diag elem can be <0
114 16 ▶ and all {looks_like_number $_ and $_ >= 0} @row_no_diag;
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 2 branches
BranchTrueFalseTotal
unless &looks_like_number($row[$. - 1]) and &all(sub { &looks_like_number($_) and $_ >= 0; } , @row_no_diag) 1 15 16
&looks_like_number($_) and $_ >= 0 30 0 30
Condition66%2/3
&looks_like_number($row[$. - 1]) and &all(sub { &looks_like_number($_) and $_ >= 0; } , @row_no_diag)
!ll&&!rl&&r
1015
MC/DC50%1/2
&looks_like_number($row[$. - 1]) and &all(sub { &looks_like_number($_) and $_ >= 0; } , @row_no_diag)
&looks_like_number($row[$. - 1]) &all(sub { &looks_like_number($_) and $_ >= 0; } , @row_no_diag)
Truth table66%2/3
A and B
A= &looks_like_number($row[$. - 1])B= &all(sub { &looks_like_number($_) and $_ >= 0; } , @row_no_diag)
ABresult
0X0
100
111
115  
116 # Check that element count is equal in all rows.
117 15 ▶ $matrix_dim //= @row; # first-time init
Condition66%2/3
$matrix_dim //= @row
l!l&&r!l&&!r
1050
MC/DC0%0/2
$matrix_dim //= @row
$matrix_dim @row
Truth table66%2/3
A //= B
A= $matrix_dimB= @row
ABresult
1X1
011
000
118 15 ▶ confess 'Lines of input file have varying number of elements'
Branch: 1 branch
BranchTrueFalseTotal
unless $matrix_dim == @row 0 15 15
119 unless $matrix_dim == @row;
120  
121 15 ▶ @row = @row[@splice_to_cols] if defined $splice_to_ref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $splice_to_ref 0 15 15
122 15 push @rate_matrix, \@row;
123 15 ▶ confess 'Input file contains more lines than there are columns'
Branch: 1 branch
BranchTrueFalseTotal
if @rate_matrix > $matrix_dim 0 15 15
124 if @rate_matrix > $matrix_dim;
125 }
126 5 ▶ confess 'End of file reached before finding all states requested by ',
Branch: 1 branch
BranchTrueFalseTotal
if defined $splice_to_ref and @splice_to_rows > 0 0 5 5
Condition33%1/3
defined $splice_to_ref and @splice_to_rows > 0
!ll&&!rl&&r
500
MC/DC0%0/2
defined $splice_to_ref and @splice_to_rows > 0
defined $splice_to_ref @splice_to_rows > 0
Truth table33%1/3
A and B
A= defined $splice_to_refB= @splice_to_rows > 0
ABresult
0X0
100
111
127 'splicing operation'
128 if defined $splice_to_ref and @splice_to_rows > 0;
129 confess 'Requested splicing of non-contained state'
130 0 ▶ unless all {$_ < $matrix_dim} @splice_to_cols;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $_ < $matrix_dim; } , @splice_to_cols) 0 5 5
131 5 ▶ confess 'Input file is empty'
Branch: 1 branch
BranchTrueFalseTotal
unless @rate_matrix 0 5 5
132 unless @rate_matrix;
133 # Adjust dimension if splicing was applied.
134 5 ▶ confess 'Input file contains less lines than there are columns'
Branch: 2 branches
BranchTrueFalseTotal
if @rate_matrix < (defined $splice_to_ref ? @splice_to_cols : $matrix_dim) 0 5 5
defined $splice_to_ref ? : 0 5 5
135 if @rate_matrix < (defined $splice_to_ref ? @splice_to_cols
136 : $matrix_dim );
137  
138 5 return \@rate_matrix;
139 }
140  
141 4 ▶sub _transpose_matrix {
Subroutine _transpose_matrix: called CC=4 SCAR=13.9
142 4 my ($class, $matrix_ref) = @_;
143  
144 # Determine dimnensions
145 4 my $max_row = @$matrix_ref - 1;
146 4 ▶ return unless $max_row >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $max_row >= 0 0 4 4
147 4 my $max_col = @{ $matrix_ref->[0] } - 1; # check elems of first row
148  
149 # Swap values
150 4 for my $row (0..$max_row) {
151 12 for my $col (($row+1)..$max_col) {
152 12 my $temp = $matrix_ref->[$row][$col];
153 12 $matrix_ref->[$row][$col] = $matrix_ref->[$col][$row];
154 12 $matrix_ref->[$col][$row] = $temp;
155 }
156 }
157 }
158  
159 # Class method. Reads a rate matrix in binary format from the passed file
160 # handle and constructs a matrix (2-dim array) from it. Returns a
161 # reference to the constructed rate matrix.
162 5 ▶sub read_bin_rate_matrix {
Subroutine read_bin_rate_matrix: called CC=14 SCAR=30.1
Pod documented
163 5 my ($class, $input_matrix_fh, $splice_to_ref) = @_;
164  
165 # During parsing, splice the selected rows / columns. Make 0-based.
166 5 ▶ my @splice_to_cols = @{ $splice_to_ref // [] }; # 1-based, modified
Condition50%1/2
$splice_to_ref // []
l!l
05
MC/DC0%0/1
$splice_to_ref // []
$splice_to_ref
Truth table50%1/2
A
A= $splice_to_ref
Aresult
11
00
167 0 my @splice_to_rows = map {$_ - 1} @splice_to_cols; # 0-based indices
168  
169 # Set read mode to binary
170 5 binmode $input_matrix_fh;
171  
172 ##### Read out matrix dimension
173 12 ▶ my $size_of_int = do {use Config; $Config{intsize}};
Subroutine BEGIN: called CC=1 SCAR=0.0
174 5 my $read_count
175 = read($input_matrix_fh, my $raw_matrix_dim, $size_of_int);
176 5 ▶ confess "Could not read dimension from file, ",
Branch: 1 branch
BranchTrueFalseTotal
if $read_count != $size_of_int 0 5 5
177 "expected $size_of_int bytes, got $read_count"
178 if $read_count != $size_of_int;
179  
180 5 my $matrix_dim = unpack 'i', $raw_matrix_dim; # unpack integer
181  
182 confess 'Requested splicing of non-contained state'
183 0 ▶ unless all {$_ < $matrix_dim} @splice_to_rows;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { $_ < $matrix_dim; } , @splice_to_rows) 0 5 5
184  
185 ##### Read rate matrix
186 5 my @rate_matrix;
187 12 ▶ my $size_of_double = do {use Config; $Config{doublesize}};
Subroutine BEGIN: called CC=1 SCAR=0.0
188 5 my $bytes_per_column = $size_of_double * $matrix_dim;
189 5 COL: for my $i (1..$matrix_dim) {
190 # Each column consists of n=matrix_dim doubles.
191 13 $read_count
192 = read($input_matrix_fh, my $raw_column, $bytes_per_column);
193 13 ▶ confess "Could not read column $i of file, ",
Branch: 1 branch
BranchTrueFalseTotal
if $read_count != $bytes_per_column 1 12 13
194 "expected $bytes_per_column bytes, got $read_count"
195 if $read_count != $bytes_per_column;
196  
197 # Skip column if splicing and column not requested.
198 12 ▶ if (defined $splice_to_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $splice_to_ref) 0 12 12
199 0 ▶ last unless @splice_to_cols; # we're done!
Branch: 1 branch
BranchTrueFalseTotal
unless @splice_to_cols 0 0 0
200 0 ▶ next COL if $i != $splice_to_cols[0]; # this col is not kept
Branch: 1 branch
BranchTrueFalseTotal
if $i != $splice_to_cols[0] 0 0 0
201 0 shift @splice_to_cols; # remove the leading index
202 }
203  
204 # Decode raw doubles.
205 12 my @matrix_column = unpack "d$matrix_dim", $raw_column;
206  
207 # Splice parsed column if requested.
208 12 ▶ @matrix_column = @matrix_column[@splice_to_rows]
Branch: 1 branch
BranchTrueFalseTotal
if defined $splice_to_ref 0 12 12
209 if defined $splice_to_ref;
210  
211 12 push @rate_matrix, \@matrix_column;
212 }
213 4 ▶ confess 'End of file reached before finding all states requested by ',
Branch: 1 branch
BranchTrueFalseTotal
if defined $splice_to_ref and @splice_to_cols > 0 0 4 4
Condition33%1/3
defined $splice_to_ref and @splice_to_cols > 0
!ll&&!rl&&r
400
MC/DC0%0/2
defined $splice_to_ref and @splice_to_cols > 0
defined $splice_to_ref @splice_to_cols > 0
Truth table33%1/3
A and B
A= defined $splice_to_refB= @splice_to_cols > 0
ABresult
0X0
100
111
214 'splicing operation'
215 if defined $splice_to_ref and @splice_to_cols > 0;
216 4 ▶ confess 'Read data as suggested by dimension, but end of file ',
Branch: 1 branch
BranchTrueFalseTotal
unless defined $splice_to_ref or eof $input_matrix_fh 0 4 4
Condition33%1/3
defined $splice_to_ref or eof $input_matrix_fh
l!l&&r!l&&!r
040
MC/DC0%0/2
defined $splice_to_ref or eof $input_matrix_fh
defined $splice_to_ref eof $input_matrix_fh
Truth table33%1/3
A or B
A= defined $splice_to_refB= eof $input_matrix_fh
ABresult
1X1
011
000
217 'not reached'
218 unless defined $splice_to_ref or eof $input_matrix_fh;
219  
220 # For whatever reasons, binary rates are stored column-wise instead of
221 # row-wise. Transpose to fix that.
222 4 __PACKAGE__->_transpose_matrix(\@rate_matrix);
223  
224 4 return \@rate_matrix;
225 }
226  
227 6 ▶sub _build_file_handle {
Subroutine _build_file_handle: called CC=2 SCAR=7.0
228 6 my $self = shift;
229  
230 6 ▶ confess 'File required if no file handle is passed'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file_name 0 6 6
231 unless $self->has_file_name;
232  
233 6 open my $handle, '<', $self->file_name;
234 6 return $handle;
235 }
236  
237 # Get the dimension (= number of rows = number of columns) of the matrix.
238 54 ▶sub dim {
Subroutine dim: called CC=1 SCAR=0.0
Pod documented
239 54 my $self = shift;
240  
241 54 my $dimension = @{ $self->_data };
242 52 return $dimension;
243 }
244  
245 # Get the rate from state i to state j. States are 1-based (first state =
246 # state 1) just as in the results file.
247 9 ▶sub rate_from_to {
Subroutine rate_from_to: called CC=3 SCAR=11.3
Pod documented
248 9 my ($self, $from_state, $to_state) = @_;
249  
250 # Check states are within bounds
251 9 ▶ confess "from_state $from_state is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_state_is_in_bounds($from_state) 0 9 9
252 unless $self->_state_is_in_bounds($from_state);
253 9 ▶ confess "to_state $to_state is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_state_is_in_bounds($to_state) 0 9 9
254 unless $self->_state_is_in_bounds($to_state);
255  
256 # Retrieve rate.
257 9 my $rate = $self->_data->[$from_state-1][$to_state-1];
258 9 return $rate;
259 }
260  
261 # Check whether given state is contained in the rate matrix.
262 26 ▶sub _state_is_in_bounds {
Subroutine _state_is_in_bounds: called CC=2 SCAR=7.6
263 26 my ($self, $state) = @_;
264  
265 26 ▶ my $is_in_bounds = ($state >= 1 && $state <= $self->dim);
Condition33%1/3
$state >= 1 && $state <= $self->dim
!ll&&!rl&&r
0026
MC/DC0%0/2
$state >= 1 && $state <= $self->dim
$state >= 1 $state <= $self->dim
Truth table33%1/3
A && B
A= $state >= 1B= $state <= $self->dim
ABresult
0X0
100
111
266 26 return $is_in_bounds;
267 }
268  
269 # Returns a sorted list of all states connected to the (mfe) state 1.
270 # Assumes a symmetric transition matrix (only checks path *from* state 1
271 # *to* the other states). Quadratic runtime.
272 0 ▶sub connected_states {
Subroutine connected_states: not called CC=5 SCAR=34.0
Pod documented
273 0 my ($self) = @_;
274  
275 # Starting at state 1, perform a traversal of the transition graph and
276 # remember all nodes seen.
277 0 my $dim = $self->dim;
278 0 my @cue = (1);
279 0 my %connected = (1 => 1); # state 1 is connected
280 0 ▶ while (my $i = shift @cue) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = shift @cue) 0 0 0
281 0 foreach my $j (1..$dim) {
282 0 ▶ next if $connected{$j} or $self->rate_from_to($i, $j) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $connected{$j} or $self->rate_from_to($i, $j) <= 0 0 0 0
Condition0%0/3
$connected{$j} or $self->rate_from_to($i, $j) <= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$connected{$j} or $self->rate_from_to($i, $j) <= 0
$connected{$j} $self->rate_from_to($i, $j) <= 0
Truth table0%0/3
A or B
A= $connected{$j}B= $self->rate_from_to($i, $j) <= 0
ABresult
1X1
011
000
283 0 $connected{$j} = 1; # j is connected to 1 via i
284 0 push @cue, $j;
285 }
286 }
287  
288 # Sort in linear time.
289 0 my @sorted_connected = grep {$connected{$_}} 1..$dim;
290 0 return @sorted_connected;
291 }
292  
293 # Only keep the states connected to the mfe (as determined by
294 # connected_states()). Returns a list of all connected (and thus preserved)
295 # minima.
296 0 ▶sub keep_connected {
Subroutine keep_connected: not called CC=2 SCAR=17.9
Pod documented
297 0 my ($self) = @_;
298 0 my @connected_indices = map {$_ - 1} $self->connected_states;
299 0 ▶ return map {$_ + 1} @connected_indices # none removed.
Branch: 1 branch
BranchTrueFalseTotal
if $self->dim == @connected_indices 0 0 0
300 if $self->dim == @connected_indices;
301  
302 0 $self->_splice_indices(\@connected_indices);
303  
304 0 return map {$_ + 1} @connected_indices; # turn into states again
305 }
306  
307 # Remove all but the passed states from this rate matrix. States are
308 # 1-based (first state = state 1) just as in the results file.
309 6 ▶sub keep_states {
Subroutine keep_states: called CC=5 SCAR=16.1
Pod documented
310 6 my ($self, @states_to_keep) = @_;
311  
312 # We need a sorted, unique list.
313 6 @states_to_keep = uniqnum sort {$a <=> $b} @states_to_keep;
314  
315 # Check whether states are within bounds.
316 6 foreach my $state (@states_to_keep) {
317 8 ▶ confess "State $state is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_state_is_in_bounds($state) 0 8 8
318 unless $self->_state_is_in_bounds($state);
319 }
320  
321 6 ▶ return if @states_to_keep == $self->dim; # keep all == no op
Branch: 1 branch
BranchTrueFalseTotal
if @states_to_keep == $self->dim 2 4 6
322  
323 4 $_-- foreach @states_to_keep; # states are now 0-based
324 4 $self->_splice_indices(\@states_to_keep);
325  
326 4 return $self;
327 }
328  
329 # Only keep the passed states and reorder them as in the passed list. In
330 # particular, the same state can be passed multiple times and will then be
331 # deep-copied.
332 # Arguments:
333 # states: Ordered list of states defining the resulting matrix. May
334 # contain duplicates.
335 0 ▶sub splice {
Subroutine splice: not called CC=4 SCAR=30.0
Pod documented
336 0 my ($self, @states) = @_;
337  
338 # Check whether states are within bounds.
339 0 foreach my $state (@states) {
340 0 ▶ confess "State $state is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_state_is_in_bounds($state) 0 0 0
341 unless $self->_state_is_in_bounds($state);
342 }
343  
344 0 $_-- foreach @states; # states are now 0-based
345 0 $self->_splice_indices(\@states);
346  
347 0 return $self;
348 }
349  
350 # Internal version which performs no boundary checks and assumes REFERENCE
351 # to state list.
352 4 ▶sub _splice_indices {
Subroutine _splice_indices: called CC=5 SCAR=16.1
353 4 my ($self, $kept_indices_ref) = @_;
354  
355 4 my $matrix_ref = $self->_data;
356  
357 # If no entries are kept, make matrix empty.
358 4 ▶ if (@$kept_indices_ref == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$kept_indices_ref == 0) 2 2 4
359 2 @$matrix_ref = ();
360 2 return;
361 }
362  
363 # Splice the matrix.
364 # WARNING: This makes a shallow copy of the rows if the same index is
365 # passed more than once (e.g. from splice()).
366 2 @$matrix_ref = @{$matrix_ref}[@$kept_indices_ref]; # rows
367  
368 # Deep-copy duplicated rows (if any).
369 2 my %row_seen;
370 2 foreach my $row (@$matrix_ref) {
371 2 ▶ $row = [@$row] if $row_seen{$row}; # deep-copy array
Branch: 1 branch
BranchTrueFalseTotal
if $row_seen{$row} 0 2 2
372 2 $row_seen{$row} = 1;
373 }
374 2 @$_ = @{$_}[@$kept_indices_ref] # columns
375 2 foreach @$matrix_ref;
376  
377 2 return $self;
378 }
379  
380 # Remove the passed states from this rate matrix. States are 1-based
381 # (first state = state 1) just as in the results file.
382 0 ▶sub remove_states {
Subroutine remove_states: not called CC=5 SCAR=34.0
Pod documented
383 0 my ($self, @states_to_remove) = @_;
384  
385 0 ▶ return unless @states_to_remove; # removing no states at all
Branch: 1 branch
BranchTrueFalseTotal
unless @states_to_remove 0 0 0
386  
387 # Check states are within bounds.
388 0 foreach my $state (@states_to_remove) {
389 0 ▶ confess "State $state is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_state_is_in_bounds($state) 0 0 0
390 unless $self->_state_is_in_bounds($state);
391 }
392  
393 # Invert state list via look-up hash.
394 0 my %states_to_remove = map {$_ => 1} @states_to_remove;
395 my @states_to_keep
396 0 = grep {not $states_to_remove{$_}} 1..$self->dim;
397  
398 # Let _keep_indices() do the work.
399 0 $_-- foreach @states_to_keep; # states are now 0-based
400 0 $self->_splice_indices(\@states_to_keep);
401  
402 0 return $self;
403 }
404  
405 # Print this matrix as text, either to the passed handle, or to STDOUT.
406 6 ▶sub print_as_text {
Subroutine print_as_text: called CC=4 SCAR=13.9
Pod documented
407 6 my ($self, $text_matrix_out_fh) = @_;
408 6 ▶ $text_matrix_out_fh //= \*STDOUT; # write to STDOUT by default
Condition50%1/2
$text_matrix_out_fh //= \*STDOUT
l!l
60
MC/DC0%0/1
$text_matrix_out_fh //= \*STDOUT
$text_matrix_out_fh
Truth table50%1/2
A
A= $text_matrix_out_fh
Aresult
11
00
409  
410 6 my $rate_format = '%10.4g '; # as in Barriers code
411  
412 6 foreach my $row (@{ $self->_data }) {
413 18 printf {$text_matrix_out_fh} $rate_format, $_ foreach @$row;
414 18 print {$text_matrix_out_fh} "\n";
415 }
416 }
417  
418 # Print this matrix as binary data, either to the passed handle or to
419 # STDOUT. Data format: matrix dimension as integer, then column by column
420 # as double.
421 1 ▶sub print_as_bin {
Subroutine print_as_bin: called CC=3 SCAR=11.0
Pod documented
422 1 my ($self, $rate_matrix_out_fh ) = @_;
423  
424 1 my $rate_matrix_ref = $self->_data;
425  
426 # Set write mode to binary
427 1 binmode $rate_matrix_out_fh;
428  
429 ##### Print out matrix dimension
430 1 my $matrix_dim = @$rate_matrix_ref;
431 1 my $packed_dim = pack 'i', $matrix_dim; # machine representation, int
432 1 print {$rate_matrix_out_fh} $packed_dim;
433  
434 ##### Print columns of rate matrix
435 # For whatever reasons, binary rates are stored column-wise instead of
436 # row-wise (Treekin works with the transposed matrix and this way it's
437 # easier to slurp the entire file. Treekin transposes the text rates
438 # during reading).
439 #_transpose_matrix $rate_matrix_ref;
440 1 foreach my $col (0..($matrix_dim-1)) {
441 3 foreach my $row (0..($matrix_dim-1)) {
442 # Pack rate as double
443 9 my $packed_rate = pack 'd', $rate_matrix_ref->[$row][$col];
444 9 print {$rate_matrix_out_fh} $packed_rate;
445 }
446 # my $column = map {$_->[$i]} @$rate_matrix_ref;
447 # my $packed_column = pack "d$matrix_dim", @column;
448 }
449 }
450  
451 # Return string containing binary the representation of the matrix (cf.
452 # print_as_bin).
453 1 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
454 1 my $self = shift;
455  
456 # Use print function and capture matrix in a string.
457 1 my $matrix_string;
458 1 open my $matrix_string_fh, '>', \$matrix_string;
459 1 $self->print_as_bin($matrix_string_fh);
460  
461 1 return $matrix_string;
462 }
463  
464 # Returns a string containing the text representation of the matrix. The
465 # overloaded double-quote operator calls this method.
466 6 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
467 6 my $self = shift;
468  
469 # Use print function and capture matrix in a string. Empty matrices
470 # give an empty string (not undef).
471 6 my $matrix_string = q{};
472 6 open my $matrix_string_fh, '>', \$matrix_string;
473 6 $self->print_as_text($matrix_string_fh);
474  
475 6 return $matrix_string;
476 }
477  
478 12 1;
479  
480  
481 __END__
↑ Summary Barriers/Results.pm »