Bio/RNA/Treekin/PopulationDataRecord.pm

filter: Statementstmt 75.5% 68 / 90 Branchbran 50.0% 7 / 14 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 80.0% 16 / 20 Podpod 100% 7 / 7 totaltotal 69.8% 102 / 146 CC 12 SCAR 11.8
CC
12
Cov
69%
transform
12.0
__ANON__
6.0
of_min
3.3
CRAP 3.3
« Bio/RNA/Treekin/Record.pm ↑ Summary IO/File/RecordStream.pm »
1 # Bio/RNA/Treekin/PopulationDataRecord.pm
2 package Bio::RNA::Treekin::PopulationDataRecord;
3 4 our $VERSION = '0.05';
4  
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use MooseX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Scalar::Util qw(reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use List::Util qw(max all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 has 'time' => (is => 'ro', required => 1);
20  
21 4 has '_populations' => (
22 is => 'ro',
23 required => 1,
24 init_arg => 'populations',
25 );
26  
27 # Return a deep copy of this object.
28 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
29 0 my $self = shift;
30 0 my $clone = __PACKAGE__->new(
31 time => $self->time,
32 populations => [ $self->populations ],
33 );
34 0 return $clone;
35 }
36  
37 # Return number of minima for which there is population data.
38 321 ▶sub min_count {
Subroutine min_count: called CC=1 SCAR=0.0
Pod documented
39 321 my $self = shift;
40 321 my $min_count = @{ $self->_populations }; # number of data points
41  
42 321 return $min_count;
43 }
44  
45 # Use to adjust the min count, e.g. when the passed data array was
46 # constructed before the number of minima was known. It may not be
47 # shrinked as data might be lost.
48 4 ▶sub set_min_count {
Subroutine set_min_count: called CC=3 SCAR=11.0
Pod documented
49 4 my ($self, $new_min_count) = @_;
50  
51 4 my $current_min_count = @{ $self->_populations };
52 4 ▶ confess 'Can only increase min_count'
Branch: 1 branch
BranchTrueFalseTotal
if $current_min_count > $new_min_count 0 4 4
53 if $current_min_count > $new_min_count;
54  
55 # Set additional states to population of 0.
56 4 for my $i ( $current_min_count..($new_min_count-1) ) {
57 14 $self->_populations->[$i] = 0.;
58 }
59  
60 4 return;
61 }
62  
63 # Return populations of all mins. Use of_min() instead to get the
64 # population of a specific min.
65 # Returns a list of all minima's populations.
66 0 ▶sub populations {
Subroutine populations: not called CC=1 SCAR=6.9
Pod documented
67 0 my $self = shift;
68 0 return @{ $self->_populations };
69 }
70  
71 # Get population for the given minimum.
72 306 ▶sub of_min {
Subroutine of_min: called CC=3 SCAR=12.0
Pod documented
73 306 my ($self, $min) = @_;
74  
75 306 ▶ confess "Minimum $min is out of bounds"
Branch: 1 branch
BranchTrueFalseTotal
if $min < 1 or $min > $self->min_count 0 306 306
Condition33%1/3
$min < 1 or $min > $self->min_count
l!l&&r!l&&!r
00306
MC/DC0%0/2
$min < 1 or $min > $self->min_count
$min < 1 $min > $self->min_count
Truth table33%1/3
A or B
A= $min < 1B= $min > $self->min_count
ABresult
1X1
011
000
76 if $min < 1 or $min > $self->min_count;
77  
78 # Minimum 1 is the first one (index 0)
79 306 my $population = $self->_populations->[$min-1];
80 306 return $population;
81 }
82  
83 # Transform (reorder and resize) the population data according to a given
84 # mapping and resize to a given minimum count. All minima that are not
85 # mapped to a new position are discarded (replaced by zero).
86 # NOTE: Ensure that no two minima are mapped to the same position or crap
87 # will happen.
88 # Arguments:
89 # maps_to_min_ref: Hash ref that specifies for each kept minimum (key) to
90 # which new minimum (value) it is supposed to be mapped.
91 # new_min_count: New size (number of mins) of the record after the
92 # transformation. Defaults to the maximum value of maps_to_min_ref.
93 # Void.
94 0 ▶sub transform {
Subroutine transform: not called CC=3 SCAR=24.8
Pod documented
95 0 my ($self, $maps_to_min_ref, $new_min_count) = @_;
96  
97 # If not explicitely given, use max value a min was mapped to as min count.
98 0 ▶ $new_min_count //= max values %$maps_to_min_ref;
Condition0%0/3
$new_min_count //= &max(values %$maps_to_min_ref)
l!l&&r!l&&!r
000
MC/DC0%0/2
$new_min_count //= &max(values %$maps_to_min_ref)
$new_min_count &max(values %$maps_to_min_ref)
Truth table0%0/3
A //= B
A= $new_min_countB= &max(values %$maps_to_min_ref)
ABresult
1X1
011
000
99  
100 0 my @new_pops = (0) x $new_min_count; # new array with right size
101 0 my @source_mins = grep { defined $maps_to_min_ref->{$_} }
102 1..$self->min_count; # filter unmapped
103 0 my @source_indices = map { $_ - 1 } @source_mins;
104 0 my @target_indices = map { $maps_to_min_ref->{$_} - 1 } @source_mins;
105  
106 # Sanity check.
107 confess "Cannot reorder as some minima are not mapped correctly"
108 0 ▶ unless all {$_ >= 0 and $_ < $new_min_count} @target_indices;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
unless &all(sub { $_ >= 0 and $_ < $new_min_count; } , @target_indices) 0 0 0
$_ >= 0 and $_ < $new_min_count 0 0 0
109  
110 # Copy population data to the correct positions and overwrite original.
111 0 @new_pops[@target_indices] = @{$self->_populations}[@source_indices];
112 0 @{ $self->_populations } = @new_pops;
113  
114 0 return;
115 }
116  
117 161 ▶sub _parse_population_data_line {
Subroutine _parse_population_data_line: called CC=3 SCAR=11.0
118 161 my ($self, $population_data_line) = @_;
119  
120 161 my ($time, @populations) = split /\s+/, $population_data_line;
121  
122 # Sanity checks.
123 161 ▶ confess "No population data found in line:\n$population_data_line\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @populations 1 160 161
124 unless @populations;
125 160 ▶ confess "Time value '$time' is not a number"
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($time) 0 160 160
126 unless looks_like_number $time;
127 # For the sake of performance, we do not test the numberness of the data.
128  
129 # Pack args for constructor.
130 160 my @args = (
131 time => $time,
132 populations => \@populations,
133 );
134  
135 160 return @args;
136 }
137  
138 168 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
139 168 my $orig = shift;
140 168 my $class = shift;
141  
142 168 ▶ return $class->$orig(@_) if @_ != 1 or reftype $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 or builtin::reftype($_[0]) 7 161 168
Condition66%2/3
@_ != 1 or builtin::reftype($_[0])
l!l&&r!l&&!r
70161
MC/DC50%1/2
@_ != 1 or builtin::reftype($_[0])
@_ != 1 builtin::reftype($_[0])
Truth table66%2/3
A or B
A= @_ != 1B= builtin::reftype($_[0])
ABresult
1X1
011
000
143  
144 # We have a population data line here.
145 161 my $population_data_line = shift;
146 my @args
147 161 = $class->_parse_population_data_line($population_data_line);
148  
149 160 return $class->$orig(@args);
150 4 };
151  
152 # Convert this data record back to a line as found in the Treekin file.
153 132 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
154 132 my $self = shift;
155  
156 # Format data like treekin C code
157 132 my $self_as_string = sprintf "%22.20e ", $self->time;
158 $self_as_string
159 132 .= join q{ }, map {sprintf "%e", $_} @{ $self->_populations };
160  
161 # There seems to be a trailing space in the treekin C code
162 # (printf "%e ") but there is none in the treekin simulator output.
163 # $self_as_string .= q{ };
164  
165 132 return $self_as_string;
166 }
167  
168 4 __PACKAGE__->meta->make_immutable;
169  
170  
171 4 1; # End of Bio::RNA::Treekin::PopulationDataRecord
172  
173 __END__
« Bio/RNA/Treekin/Record.pm ↑ Summary IO/File/RecordStream.pm »