Bio/RNA/Treekin/Record.pm

filter: Statementstmt 74.2% 141 / 190 Branchbran 71.1% 37 / 52 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 71.4% 25 / 35 Podpod 92.8% 13 / 14 totaltotal 72.9% 218 / 299 CC 36 SCAR 15.8
CC
36
Cov
73%
splice_mins
12.0
append_pop_data
12.0
_parse_init_population_from_cmd
8.0
CRAP 4.9
↑ Summary Bio/RNA/Treekin/PopulationDataRecord.pm »
1 # Bio/RNA/Treekin/Record.pm
2  
3 # Stores a data from a single row of the Treekin file, i.e. the populations of
4 # all minima at a given time point.
5 package Bio::RNA::Treekin::Record;
6 4 our $VERSION = '0.05';
7  
8 4 ▶use v5.14; # required for non-destructive subst m///r
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Scalar::Util qw(reftype openhandle);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use List::Util qw(first pairmap max uniqnum all);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Bio::RNA::Treekin::PopulationDataRecord;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 4 has '_population_data' => (
27 is => 'ro',
28 required => 1,
29 init_arg => 'population_data',
30 );
31  
32 4 has 'date' => (is => 'ro', required => 1);
33 4 has 'sequence' => (is => 'ro', required => 1);
34 4 has 'method' => (is => 'ro', required => 1);
35 4 has 'start_time' => (is => 'ro', required => 1);
36 4 has 'stop_time' => (is => 'ro', required => 1);
37 4 has 'temperature' => (is => 'ro', required => 1);
38 4 has 'basename' => (is => 'ro', required => 1);
39 4 has 'time_increment' => (is => 'ro', required => 1);
40 4 has 'degeneracy' => (is => 'ro', required => 1);
41 4 has 'absorbing_state' => (is => 'ro', required => 1);
42 4 has 'states_limit' => (is => 'ro', required => 1);
43  
44 # Add optional attributes including predicate.
45 has $_ => (
46 is => 'ro',
47 required => 0,
48 predicate => "has_$_",
49 )
50 4 foreach qw(
51 info
52 init_population
53 rates_file
54 file_index
55 cmd
56 of_iterations
57 );
58  
59 # Get number of population data rows stored.
60 2 ▶sub population_data_count {
Subroutine population_data_count: called CC=1 SCAR=0.0
Pod documented
61 2 my ($self) = @_;
62  
63 2 my $data_count = @{ $self->_population_data };
64 2 return $data_count;
65 }
66  
67 # Number of states / minima in this simulation.
68 # Get number of mins in the first population record; it should be the
69 # same for all records.
70 15 ▶sub min_count {
Subroutine min_count: called CC=2 SCAR=7.0
Pod documented
71 15 my $self = shift;
72  
73 15 my $first_pop = $self->population(0);
74 15 ▶ confess 'min_count: no population data present'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_pop 0 15 15
75 unless defined $first_pop;
76  
77 15 my $min_count = $first_pop->min_count;
78  
79 15 return $min_count;
80 }
81  
82 # Return a list of all minima, i. e. 1..n, where n is the total number of
83 # minima.
84 0 ▶sub mins {
Subroutine mins: not called CC=1 SCAR=6.9
Pod documented
85 0 my ($self) = @_;
86 0 my @mins = 1..$self->min_count;
87  
88 0 return @mins;
89 }
90  
91 # Keep only the population data for the selected minima, remove all other.
92 # Will NOT rescale populations, so they may no longer sum up to 1.
93 # Arguments:
94 # mins: List of mins to keep. Will be sorted and uniq'ed (cf. splice()).
95 # Returns the return value of splice().
96 0 ▶sub keep_mins {
Subroutine keep_mins: not called CC=1 SCAR=6.9
Pod documented
97 0 my ($self, @kept_mins) = @_;
98 0 @kept_mins = uniqnum sort {$a <=> $b} @kept_mins; # sort / uniq'ify
99 0 return $self->splice_mins(@kept_mins);
100 }
101  
102 # Keep only the population data for the selected minima, remove all other.
103 # May duplicate and re-order.
104 # mins: List of mins to keep. Will be used as is.
105 # Returns itself.
106 0 ▶sub splice_mins {
Subroutine splice_mins: not called CC=3 SCAR=24.8
Pod documented
107 0 my ($self, @kept_mins) = @_;
108  
109 0 my $min_count = $self->min_count;
110 confess 'Cannot splice, minimum out of bounds'
111 0 ▶ unless all {$_ >= 1 and $_ <= $min_count} @kept_mins;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
unless &all(sub { $_ >= 1 and $_ <= $min_count; } , @kept_mins) 0 0 0
$_ >= 1 and $_ <= $min_count 0 0 0
112  
113 # Directly update raw population data here instead of doing tons of
114 # calls passing the same min array.
115 0 my @kept_indices = map {$_ - 1} @kept_mins;
116 0 for my $pop_data (@{$self->_population_data}) { # each point in time
117 0 my $raw_pop_data = $pop_data->_populations;
118 0 @{$raw_pop_data} = @{$raw_pop_data}[@kept_indices];
119 }
120  
121 0 return $self;
122 }
123  
124 # Get the maximal population for the given minimum over all time points.
125 0 ▶sub max_pop_of_min {
Subroutine max_pop_of_min: not called CC=2 SCAR=17.9
Pod documented
126 0 my ($self, $min) = @_;
127 0 my $max_pop = '-Inf';
128 0 for my $pop_data (@{$self->_population_data}) { # each point in time
129 0 $max_pop = max $max_pop, $pop_data->of_min($min); # update max
130 }
131 0 return $max_pop;
132 }
133  
134 # For a given minimum, return all population values in chronological
135 # order.
136 # Arguments:
137 # min: Minimum for which to collect the population data.
138 # Returns a list of population values in chronological order.
139 0 ▶sub pops_of_min {
Subroutine pops_of_min: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($self, $min) = @_;
141  
142 0 my @pops_of_min = map { $_->of_min($min) } $self->populations;
143  
144 0 return @pops_of_min;
145 }
146  
147 # Final population data record, i.e. the result of the simulation.
148 0 ▶sub final_population {
Subroutine final_population: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($self) = @_;
150  
151 0 my $final_population_data
152 = $self->population($self->population_data_count - 1);
153  
154 0 return $final_population_data;
155 }
156  
157 # Get the i-th population data record (0-based indexing).
158 52 ▶sub population {
Subroutine population: called CC=1 SCAR=0.0
Pod documented
159 52 my ($self, $i) = @_;
160  
161 52 my $population_record = $self->_population_data->[$i];
162 52 return $population_record;
163 }
164  
165 # Return all population data records.
166 0 ▶sub populations {
Subroutine populations: not called CC=1 SCAR=6.9
Pod documented
167 0 return @{ $_[0]->_population_data };
168 }
169  
170 # Add a new minimum with all-zero entries. Data can then be appended to
171 # this new min.
172 # Returns the index of the new minimum.
173 0 ▶sub add_min {
Subroutine add_min: not called CC=2 SCAR=17.9
Pod documented
174 0 my $self = shift;
175 0 my $new_min_count = $self->min_count + 1;
176  
177 # Increase the min count of all population data records by one.
178 $_->set_min_count($new_min_count)
179 0 foreach @{ $self->_population_data }, $self->init_population;
180  
181 0 return $new_min_count; # count == highest index
182 }
183  
184 # Given a list of population data records, append them to the population data
185 # of this record. The columns of the added data can be re-arranged on the fly by
186 # providing a mapping (hash ref) giving for each minimum in the population
187 # data to be added (key) a minimum in the current population data
188 # (value) to which the new minimum should be swapped. If no data is provided
189 # for some minimum of this record, its population is set to zero in the
190 # newly added entries.
191 # Arguments:
192 # pop_data_ref: ref to the array of population data to be added
193 # append_to_min_ref:
194 # hash ref describing which mininum in pop_data_ref (key)
195 # should be mapped to which minimum in this record (value)
196 # The passed population data objects are modified.
197 0 ▶sub append_pop_data {
Subroutine append_pop_data: not called CC=3 SCAR=24.8
Pod documented
198 0 my ($self, $pop_data_ref, $append_to_min_ref) = @_;
199  
200 0 ▶ if (defined $append_to_min_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $append_to_min_ref) 0 0 0
201 0 my $min_count = $self->min_count;
202 0 $_->transform($append_to_min_ref, $min_count) foreach @$pop_data_ref;
203 }
204  
205 0 push @{ $self->_population_data }, @$pop_data_ref;
206  
207 0 return;
208 }
209  
210 # Decode a single header line into a key and a value, which are returned.
211 150 ▶sub _get_header_line_key_value {
Subroutine _get_header_line_key_value: called CC=2 SCAR=7.0
212 150 my ($class, $header_line) = @_;
213  
214 # key and value separated by first ':' (match non-greedy!)
215 150 my ($key, $value) = $header_line =~ m{ ^ ( .+? ) : [ ] ( .* ) $ }x;
216  
217 150 ▶ confess "Invalid key in header line:\n$header_line"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 150 150
218 unless defined $key;
219  
220 # Convert key to lower case and replace spaces by underscores.
221 150 $key = (lc $key) =~ s/\s+/_/gr;
222  
223 150 return ($key, $value);
224 }
225  
226 # Decode the initial population from the Treekin command line. The
227 # population is given as multiple --p0 a=x switches, where a is the state
228 # index and x is the fraction of population initially present in this
229 # state.
230 # Arguments:
231 # command: the command line string used to call treekin
232 # Returns a hash ref containing the initial population of each state a at
233 # position a (1-based).
234 7 ▶sub _parse_init_population_from_cmd {
Subroutine _parse_init_population_from_cmd: called CC=8 SCAR=20.8
235 7 my ($class, $command) = @_;
236  
237 7 my @command_parts = split /\s+/, $command;
238  
239 # Extract the initial population strings given as (multiple) arguments
240 # --p0 to Treekin from the Treekin command.
241 7 my @init_population_strings;
242 7 ▶ while (@command_parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@command_parts) 116 7 123
243 116 ▶ if (shift @command_parts eq '--p0') {
Branch: 1 branch
BranchTrueFalseTotal
if (shift @command_parts eq "--p0") 32 84 116
244 # Next value should be a population value.
245 32 ▶ confess 'No argument following a --p0 switch'
Branch: 1 branch
BranchTrueFalseTotal
unless @command_parts 0 32 32
246 unless @command_parts;
247 32 push @init_population_strings, shift @command_parts;
248 }
249 }
250  
251 # Store population of state i in index i-1.
252 7 my @init_population;
253 7 foreach my $init_population_string (@init_population_strings) {
254 32 my ($state, $population) = split /=/, $init_population_string;
255 32 $init_population[$state-1] = $population;
256 }
257  
258 # If no population was specified on the cmd line, init 100% in state 1
259 7 ▶ $init_population[0] = 1 unless @init_population_strings;
Branch: 1 branch
BranchTrueFalseTotal
unless @init_population_strings 3 4 7
260  
261 # Set undefined states to zero.
262 7 ▶ $_ //= 0. foreach @init_population;
Condition50%1/2
$_ //= 0
l!l
350
MC/DC0%0/1
$_ //= 0
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
263  
264 7 my $init_population_record
265 = Bio::RNA::Treekin::PopulationDataRecord->new(
266 time => 0,
267 populations => \@init_population,
268 );
269 7 return $init_population_record;
270 }
271  
272 10 ▶sub _parse_header_lines {
Subroutine _parse_header_lines: called CC=4 SCAR=13.9
273 10 my ($class, $header_lines_ref) = @_;
274  
275 10 my @header_args;
276 10 foreach my $line (@$header_lines_ref) {
277 150 my ($key, $value) = $class->_get_header_line_key_value($line);
278  
279 # Implement special handling for certain keys.
280 150 ▶ if ($key eq 'rates_file') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'rates_file') { } 7 143 150
elsif ($key eq 'cmd') { } 7 136 143
281 # remove (#index) from file name and store the value
282 7 my ($file_name, $file_index)
283 = $value =~ m{ ^ (.+) [ ] [(] [#] (\d+) [)] $ }x;
284 7 push @header_args, (
285 rates_file => $file_name,
286 file_index => $file_index,
287 );
288 }
289 elsif ($key eq 'cmd') {
290 # Extract initial population from Treekin command.
291 7 my $init_population_ref
292 = $class->_parse_init_population_from_cmd($value);
293 7 push @header_args, (
294 cmd => $value,
295 init_population => $init_population_ref,
296 );
297 }
298 else {
299 # For the rest, just push key and value as constructor args.
300 136 push @header_args, ($key => $value);
301 }
302 }
303 10 return @header_args;
304 }
305  
306  
307 # Read all lines from the given handle and separate it into header lines
308 # and data lines.
309 10 ▶sub _read_record_lines {
Subroutine _read_record_lines: called CC=7 SCAR=20.0
310 10 my ($class, $record_handle) = @_;
311  
312 # Separate lines into header and population data. All header lines
313 # begin with a '# ' (remove it!)
314 # Note: Newer versions of treekin also add header info *below* data lines.
315 10 my ($current_line, @header_lines, @population_data_lines);
316 10 ▶ while (defined ($current_line = <$record_handle>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($current_line = readline $record_handle)) 342 10 352
317 342 ▶ next if $current_line =~ /^@/ # drop xmgrace annotations
Branch: 1 branch
BranchTrueFalseTotal
if $current_line =~ /^@/u or $current_line =~ / ^ \s* $ /ux 0 342 342
Condition33%1/3
$current_line =~ /^@/u or $current_line =~ / ^ \s* $ /ux
l!l&&r!l&&!r
00342
MC/DC0%0/2
$current_line =~ /^@/u or $current_line =~ / ^ \s* $ /ux
$current_line =~ /^@/u $current_line =~ / ^ \s* $ /ux
Truth table33%1/3
A or B
A= $current_line =~ /^@/uB= $current_line =~ / ^ \s* $ /ux
ABresult
1X1
011
000
318 or $current_line =~ m{ ^ \s* $ }x; # or empty lines
319  
320 # Header lines start with '# ', remove it.
321 342 ▶ if ($current_line =~ s/^# //) { # header line
Branch: 1 branch
BranchTrueFalseTotal
if ($current_line =~ s/^# //u) { } 150 192 342
322 150 push @header_lines, $current_line;
323 }
324 else { # data line
325 192 push @population_data_lines, $current_line;
326 }
327 }
328  
329 # Sanity checks.
330 10 ▶ confess 'No header lines found in Treekin file'
Branch: 1 branch
BranchTrueFalseTotal
unless @header_lines 0 10 10
331 unless @header_lines;
332 10 chomp @header_lines;
333  
334 10 ▶ confess 'No population data lines found in Treekin file'
Branch: 1 branch
BranchTrueFalseTotal
unless @population_data_lines 0 10 10
335 unless @population_data_lines;
336 10 chomp @population_data_lines;
337  
338 10 return \@header_lines, \@population_data_lines;
339 }
340  
341 10 ▶sub _parse_population_data_lines {
Subroutine _parse_population_data_lines: called CC=1 SCAR=0.0
342 10 my ($class, $population_data_lines_ref) = @_;
343  
344 my @population_data
345 10 = map { Bio::RNA::Treekin::PopulationDataRecord->new($_) }
346 @$population_data_lines_ref
347 ;
348  
349 9 return (population_data => \@population_data);
350 }
351  
352 10 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
353 10 my $orig = shift;
354 10 my $class = shift;
355  
356 # Call original constructor if passed more than one arg.
357 10 ▶ return $class->$orig(@_) unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 10 10
358  
359 # Retrive file handle or pass on hash ref to constructor.
360 10 my $record_handle;
361 10 ▶ if (reftype $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($_[0])) { } 9 1 10
362 9 ▶ if (reftype $_[0] eq reftype {}) { # arg hash passed,
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($_[0]) eq builtin::reftype({})) { } 0 9 9
elsif (builtin::reftype($_[0]) eq builtin::reftype(\*STDIN)) { } 9 0 9
363 0 return $class->$orig(@_); # pass on as is
364 }
365 elsif (reftype $_[0] eq reftype \*STDIN) { # file handle passed
366 9 $record_handle = shift;
367 }
368 else {
369 0 croak 'Invalid ref type passed to constructor';
370 }
371 }
372 else { # file name passed
373 1 my $record_file = shift;
374 1 open $record_handle, '<', $record_file;
375 }
376  
377 # Read in file.
378 10 my ($header_lines_ref, $population_data_lines_ref)
379 = $class->_read_record_lines($record_handle);
380  
381 # Parse file.
382 10 my @header_args = $class->_parse_header_lines($header_lines_ref);
383 my @data_args
384 10 = $class->_parse_population_data_lines($population_data_lines_ref);
385  
386 9 my %args = (@header_args, @data_args);
387 9 return $class->$orig(\%args);
388 4 };
389  
390 9 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
391 9 my $self = shift;
392  
393 # Force construction despite laziness.
394 9 $self->min_count;
395  
396 # Adjust min count of initial population as it was not known when
397 # initial values were extracted from Treekin cmd.
398 9 ▶ $self->init_population->set_min_count( $self->min_count )
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_init_population 4 5 9
399 if $self->has_init_population;
400 }
401  
402 8 ▶sub stringify {
Subroutine stringify: called CC=6 SCAR=17.9
Pod documented
403 8 my $self = shift;
404  
405 # Format header line value of rates file entry.
406 6 ▶ my $make_rates_file_val = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
407 6 $self->rates_file . ' (#' . $self->file_index . ')';
408 8 };
409  
410 # Header
411 8 ▶ my @header_entries = (
Branch: 3 branches
BranchTrueFalseTotal
$self->has_rates_file ? : 6 2 8
$self->has_info ? : 2 6 8
$self->has_cmd ? : 6 2 8
412 $self->has_rates_file ? ('Rates file' => $make_rates_file_val->()) : (),
413 $self->has_info ? ('Info' => $self->info) : (),
414 $self->has_cmd ? ('Cmd' => $self->cmd) : (),
415 'Date' => $self->date,
416 'Sequence' => $self->sequence,
417 'Method' => $self->method,
418 'Start time' => $self->start_time,
419 'Stop time' => $self->stop_time,
420 'Temperature' => $self->temperature,
421 'Basename' => $self->basename,
422 'Time increment' => $self->time_increment,
423 'Degeneracy' => $self->degeneracy,
424 'Absorbing state' => $self->absorbing_state,
425 'States limit' => $self->states_limit,
426 );
427  
428 8 ▶ my $header_str = join "\n", pairmap { "# $a: $b" } @header_entries;
Subroutine __ANON__: called CC=1 SCAR=0.0
429  
430 # Population data
431 my $population_str
432 8 = join "\n", map { "$_" } @{ $self->_population_data };
433  
434 # Footer (new Treekin versions only).
435 8 ▶ my $footer_str = $self->has_of_iterations
Branch: 1 branch
BranchTrueFalseTotal
$self->has_of_iterations ? : 1 7 8
436 ? '# of iterations: ' . $self->of_iterations
437 : q{};
438  
439 8 my $self_as_str = $header_str . "\n" . $population_str;
440 8 ▶ $self_as_str .= "\n" . $footer_str if $footer_str;
Branch: 1 branch
BranchTrueFalseTotal
if $footer_str 1 7 8
441  
442 8 return $self_as_str;
443 }
444  
445 4 __PACKAGE__->meta->make_immutable;
446  
447 4 1; # End of Bio::RNA::Treekin::Record
448  
449  
450 __END__
↑ Summary Bio/RNA/Treekin/PopulationDataRecord.pm »