Barriers/Minimum.pm

filter: Statementstmt 79.6% 98 / 123 Branchbran 55.8% 19 / 34 Conditioncond 58.3% 7 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 78.5% 22 / 28 Podpod 87.5% 7 / 8 totaltotal 72.7% 155 / 213 CC 24 SCAR 16.6
CC
24
Cov
73%
__ANON__
12.4
saddle_height
12.0
ancestors
6.0
CRAP 5.2
« Barriers/Results.pm ↑ Summary Barriers.pm »
1 package Bio::RNA::Barriers::Minimum;
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 Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use namespace::autoclean;
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 q{""} => 'stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use List::MoreUtils qw(zip);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 #### Special types for attribute checking.
21 subtype 'RNAStruct' => (
22 as 'Str',
23 748 ▶ where { m{ ^ [(.)]+ $ }x },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 0 ▶ message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 "Only '(', ')', and '.' allowed in structure string, found '$_'"
26 },
27 12 );
28  
29 subtype 'DisconSaddle' => (
30 as 'Str',
31 13 ▶ where { m{ ^ ~+ $ }x },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 0 ▶ message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 "Only '~' allowed in disconnected saddle string, found '$_'"
34 },
35 12 );
36  
37 # index - index of basins ordered by energy; 1 is lowest
38 # struct - struct of lowest energy in minimums
39 # mfe - free energy of the basin's local minimum
40 # father_index - index of father basin (the basin this one is merged to)
41 # barrier_height - height of energy barrier (in kcal/mol) to minimum this
42 # one is merged to (relative to this minimum)
43 12 my @default_attribs = qw( index struct mfe father_index barrier_height);
44 12 my @default_attrib_args = (is => 'rw', required => 1);
45 12 my %default_attrib_isa
46 = &zip(\@default_attribs, [qw(Int RNAStruct Num Int Num)]);
47 has $_ => (@default_attrib_args, isa => $default_attrib_isa{$_})
48 12 foreach @default_attribs;
49  
50 # Return true iff this is the mfe basin 1.
51 0 ▶sub is_global_min {
Subroutine is_global_min: not called CC=1 SCAR=6.9
Pod documented
52 0 my $self = shift;
53 0 my $is_global_min = $self->index == 1;
54 0 return $is_global_min;
55 }
56  
57 # Optional attributes generated by Barriers options --bsize and --saddle.
58 # Descriptions in quotes are from Barriers tutorial at
59 # https://www.tbi.univie.ac.at/RNA/tutorial/#sec4_2
60 # merged_struct_count - 'numbers of structures in the basin we merge with'
61 # Given is the number of structures in the *current* basin
62 # (including merged ones) *at the time of merging*. For minimum 1,
63 # this is close to the total number of input structures (except for
64 # disconnected structures and other missing ones (???).
65 # father_struct_count - 'number of basin which we merge to'
66 # Actually, it's the number of *structures* in the basin that we
67 # merge to (father basin) *at the time of merging*.
68 # merged_basin_energy - 'free energy of the basin'
69 # This seems to be the free energy of (the partition function of)
70 # the basin---including all merged basins---at the time this basin
71 # is merged. For minimum 1, this corresponds to the ensemble free
72 # energy as far as it was enumerated by RNAsubopt (excluding
73 # disconnected structures).
74 # grad_struct_count - 'number of structures in this basin using gradient walk'
75 # This seems to be the actual number of structures only in this
76 # basin, excluding merged basins. What about --minh merging? Why
77 # doesn't this column sum up to exactly to the total number of
78 # structs if --max==Inf (some are missing)? Issues due to degenerate
79 # energies?
80 # grad_basin_energy - 'gradient basin (consisting of all structures where
81 # gradientwalk ends in the minimum)'
82 # This seems to be free energy of the basin without any merged
83 # basins. Summing up the partition functions corresponding to these
84 # energies, one obtains a free energy almost equal to the ensemble
85 # energy (up to rounding errors due to 6 digit precision).
86 12 my @bsize_attributes = qw(
87 merged_struct_count father_struct_count merged_basin_energy
88 grad_struct_count grad_basin_energy
89 );
90 12 my @opt_attributes = (@bsize_attributes, qw(saddle_struct));
91 # Define a bsize predicate only for the first bsize attribute. Ensure in
92 # BUILD that either all or none of the attributes are set.
93 my @common_attrib_args = (
94 is => 'ro',
95 lazy => 1,
96 132 ▶ default => sub { confess 'attribute undefined, did you use --bsize/--saddle?' },
Subroutine __ANON__: called CC=1 SCAR=0.0
97 12 );
98 # has $bsize_attributes[0] => (is => 'ro', predicate => 'has_bsize' );
99 # has $_ => (is => 'ro') foreach @bsize_attributes[1..$#bsize_attributes];
100 # has 'saddle_struct' => (is => 'ro', predicate => 'has_saddle_struct');
101 12 has $bsize_attributes[0] => (
102 @common_attrib_args,
103 isa => 'Num',
104 predicate => 'has_bsize',
105 writer => "_$bsize_attributes[0]", # private writer
106 );
107 has $_ => (@common_attrib_args, writer => "_$_") # private writer
108 12 foreach @bsize_attributes[1..$#bsize_attributes];
109 12 has 'saddle_struct' => (
110 @common_attrib_args,
111 isa => 'RNAStruct | DisconSaddle',
112 predicate => 'has_saddle_struct',
113 );
114  
115 # Optional reference to the father minimum.
116 12 has 'father' => (
117 is => 'rw',
118 trigger => \&_check_father,
119 # Use name 'has_father_REF' because has_father==false reads as if the
120 # min does not have a father at all.
121 # We don't need this, we always have it if we have a father.
122 # predicate => 'has_father_ref',
123 );
124  
125 526 ▶sub _check_father {
Subroutine _check_father: called CC=4 SCAR=16.1
126 526 my ($self, $father) = @_;
127 526 ▶ confess 'Need a reference to another minimum to set father attribute'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($father) and $father->isa(__PACKAGE__) 0 526 526
Condition33%1/3
builtin::blessed($father) and $father->isa(__PACKAGE__)
!ll&&!rl&&r
00526
MC/DC0%0/2
builtin::blessed($father) and $father->isa(__PACKAGE__)
builtin::blessed($father) $father->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($father)B= $father->isa(__PACKAGE__)
ABresult
0X0
100
111
128 unless blessed $father and $father->isa( __PACKAGE__ );
129 526 ▶ confess "Father's index does not match the index used during construction"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->father_index == $father->index 0 526 526
130 unless $self->father_index == $father->index;
131 }
132  
133 # Returns true iff the minimum has a father minimum it has been merged to.
134 566 ▶sub has_father {
Subroutine has_father: called CC=1 SCAR=0.0
Pod documented
135 566 my $self = shift;
136 566 my $has_father = $self->father_index > 0;
137 566 return $has_father;
138 }
139  
140  
141 # Minimum is connected to basin 1 (mfe).
142 12 has 'is_connected' => (
143 is => 'ro',
144 lazy => 1,
145 init_arg => undef, # cannot be set manually
146 builder => '_build_is_connected',
147 );
148  
149 12 ▶sub _build_is_connected {
Subroutine _build_is_connected: called CC=3 SCAR=11.0
150 12 my $self = shift;
151  
152 12 ▶ return 1 if $self->index == 1; # this is the mfe basin
Branch: 1 branch
BranchTrueFalseTotal
if $self->index == 1 1 11 12
153 11 ▶ return 0 unless $self->has_father; # basin has no father
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_father 1 10 11
154  
155 # confess 'Reference to father minimum has not been set, cannot proceed.'
156 # unless $self->has_father_ref;
157  
158 10 my $is_connected = $self->father->is_connected;
159 10 return $is_connected;
160 }
161  
162 # Parse passed line read from barriers file.
163 581 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=11 SCAR=25.2
164 581 my $orig = shift;
165 581 my $class = shift;
166  
167 581 my @args; # as passed to the constructor
168 581 ▶ if ( @_ == 1 && !ref $_[0] ) { # process line from bar file
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 581 0 581
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
00581
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
169 581 my $input_line = shift;
170 581 my @fields = split /\s+/, $input_line;
171 581 ▶ shift @fields if $fields[0] eq q{}; # drop empty first field
Branch: 1 branch
BranchTrueFalseTotal
if $fields[0] eq "" 581 0 581
172  
173 581 ▶ if (@fields < @default_attribs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields < @default_attribs) 0 581 581
174 0 confess "Input line has not enough fields: $input_line";
175 }
176  
177 # Add default args
178 581 push @args, $_ => shift @fields foreach @default_attribs;
179 # @args = map { $_ => shift @fields } @attributes;
180  
181 # Add saddle struct if present
182 581 ▶ if (@fields == 1 or @fields == @opt_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == 1 or @fields == @opt_attributes) 170 411 581
Condition100%3/3
@fields == 1 or @fields == @opt_attributes
l!l&&r!l&&!r
10268411
MC/DC100%2/2
@fields == 1 or @fields == @opt_attributes
@fields == 1 @fields == @opt_attributes
Truth table100%3/3
A or B
A= @fields == 1B= @fields == @opt_attributes
ABresult
1X1
011
000
183 170 push @args, saddle_struct => shift @fields;
184 }
185  
186 # Add bsize attributes if present
187 581 ▶ if (@fields == @bsize_attributes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields == @bsize_attributes) 247 334 581
188 247 push @args, $_ => shift @fields foreach @bsize_attributes;
189 }
190  
191 581 ▶ confess "Unrecognized number of fields on input line:\n$input_line"
Branch: 1 branch
BranchTrueFalseTotal
unless @fields == 0 0 581 581
192 unless @fields == 0; # all fields used up?
193 }
194 else {
195 0 @args = @_;
196 }
197 581 return $class->$orig(@args);
198 12 };
199  
200 578 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.3
Pod undocumented
201 578 my $self = shift;
202  
203 # Ensure presence or absence of all bsize attributes
204 578 my $defined_count = grep {defined $self->{$_}} @bsize_attributes;
205 578 ▶ confess "Need to define all or none of the --bsize attributes ",
Branch: 1 branch
BranchTrueFalseTotal
unless $defined_count == 0 or $defined_count == @bsize_attributes 0 578 578
Condition66%2/3
$defined_count == 0 or $defined_count == @bsize_attributes
l!l&&r!l&&!r
3312470
MC/DC0%0/2
$defined_count == 0 or $defined_count == @bsize_attributes
$defined_count == 0 $defined_count == @bsize_attributes
Truth table66%2/3
A or B
A= $defined_count == 0B= $defined_count == @bsize_attributes
ABresult
1X1
011
000
206 join q{, }, @bsize_attributes
207 unless $defined_count == 0 or $defined_count == @bsize_attributes;
208 }
209  
210 # Determine all ancestor minima of this minimum, i.e. this minimum's
211 # father, grand father, grand grand father etc. in this order.
212 # Returns list of all ancestors (may be empty if min is disconnected).
213 0 ▶sub ancestors {
Subroutine ancestors: not called CC=2 SCAR=17.9
Pod documented
214 0 my ($self) = @_;
215  
216 0 my $ancestor = $self;
217 0 my @ancestors;
218 0 ▶ while ($ancestor->has_father) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor->has_father) 0 0 0
219 # confess 'Need father reference to determine ancestors'
220 # unless $ancestor->has_father_ref;
221 0 push @ancestors, $ancestor->father;
222 0 $ancestor = $ancestor->father;
223 }
224  
225 0 return @ancestors;
226 }
227  
228 # Stringify minimum to equal an entry line in the barriers output file.
229 # Format strings are taken from Barriers' C source code.
230 528 ▶sub stringify {
Subroutine stringify: called CC=3 SCAR=11.0
Pod documented
231 528 my $self = shift;
232  
233 # Default attributes
234 528 my $min_string = $self->brief;
235  
236 # Add saddle struct if defined.
237 528 ▶ if ($self->has_saddle_struct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_saddle_struct) 264 264 528
238 264 $min_string .= q{ } . $self->saddle_struct;
239 }
240  
241 # Add bsize attributes if defined.
242 528 ▶ if ($self->has_bsize) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_bsize) 264 264 528
243 $min_string .= sprintf " %12ld %8ld %10.6f %8ld %10.6f",
244 264 map {$self->{$_}} @bsize_attributes;
245 }
246  
247 528 return $min_string;
248 }
249  
250 # Stringification method returning a more brief representation of the
251 # minimum containing only the index, min struct, its energy, the father's
252 # index, and the barrier height. This equals the output of Barriers if
253 # neither --bsize nor --saddle is given.
254 528 ▶sub brief {
Subroutine brief: called CC=1 SCAR=0.0
Pod documented
255 528 my $self = shift;
256  
257 # Default attributes
258 my $brief_string = sprintf "%4d %s %6.2f %4d %6.2f",
259 528 map {$self->{$_}} @default_attribs;
260  
261 528 return $brief_string;
262 }
263  
264 # ABSOLUTE energy of the lowest structure connecting this basin to another
265 # one. The barrier height, in contrast, is the RELATIVE energy of the same
266 # structure w.r.t. to the basin's (local) mfe.
267 # BEWARE: if the basin does not have a father (father == 0), then the
268 # barrier height is (as reported by Barriers) given with respect to the global exploration
269 # threshold.
270 # Since this gives unexpected results, the saddle height is set to the
271 # global mfe for basin 1, and to Inf for disconnected basins.
272 0 ▶sub saddle_height {
Subroutine saddle_height: not called CC=3 SCAR=24.8
Pod documented
273 0 my $self = shift;
274  
275 # Mfe basin is connected to itself with a barrier of 0. Other
276 # fatherless basins are disconnected and thus have an unknown saddle
277 # height -- set to Inf.
278 0 ▶ my $barrier_height = $self->has_father ? $self->barrier_height
Branch: 2 branches
BranchTrueFalseTotal
$self->has_father ? : 0 0 0
$self->is_global_min ? : 0 0 0
279 : $self->is_global_min ? 0
280 : 'Inf'
281 ;
282  
283 # Energy values from Bar file have only 2 digits precision.
284 0 my $saddle_height = sprintf "%.2f", $self->mfe + $barrier_height;
285 0 return $saddle_height;
286 }
287  
288 # Saddle height as described for saddle_height(), but with respect to the
289 # global mfe structure (basin 1).
290 0 ▶sub global_saddle_height {
Subroutine global_saddle_height: not called CC=2 SCAR=17.9
Pod documented
291 0 my $self = shift;
292  
293 # Move up in barrier tree until reachin basin 1 or realizing we are
294 # disconnected. Global saddle height is maximal encountered height.
295 0 my $ancestor = $self;
296 0 my $glob_sadd_height = $self->saddle_height;
297 0 ▶ while ($ancestor->father_index > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ancestor->father_index > 1) 0 0 0
298 0 $ancestor = $ancestor->father;
299 0 $glob_sadd_height
300 = max $glob_sadd_height, $ancestor->saddle_height;
301 }
302  
303 0 return $glob_sadd_height;
304 }
305  
306 12 __PACKAGE__->meta->make_immutable;
307  
308 12 1;
309  
310  
311 __END__
« Barriers/Results.pm ↑ Summary Barriers.pm »