Barriers/Results.pm

filter: Statementstmt 61.5% 101 / 164 Branchbran 46.6% 14 / 30 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 70.0% 21 / 30 Podpod 100% 16 / 16 totaltotal 58.3% 157 / 269 CC 29 SCAR 21.0
CC
29
Cov
57%
update_connected
20.0
_update_ancestors
20.0
path
20.0
CRAP 8.2
« Barriers/RateMatrix.pm ↑ Summary Barriers/Minimum.pm »
1 package Bio::RNA::Barriers::Results;
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 12 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use File::Spec::Functions qw(catpath splitpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 has [qw( seq file_name )] => (
17 is => 'ro',
18 required => 1,
19 );
20  
21 12 has _mins => (
22 is => 'ro',
23 required => 1,
24 init_arg => 'mins'
25 );
26  
27 has $_ => (
28 is => 'ro',
29 predicate => "has_$_",
30 12 ) foreach qw(_volume _directory);
31  
32  
33 12 ▶use overload q{""} => 'stringify';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # Allow various calling styles of the constructor:
36 # new(barriers_handle): pass only file handle to read data from, file_name
37 # will be undef.
38 # new(barriers_handle, barriers_file_path): read data from handle and set
39 # file name from a given path
40 # new( barriers_file_path): open handle and read data from the passed
41 # path, set file name accordingly
42 # new(hash_ref): manual construction from required attributes
43 30 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=8 SCAR=24.0
44 30 my $orig = shift;
45 30 my $class = shift;
46  
47 30 ▶ return $class->$orig(@_) if @_ == 0 or @_ > 2; # no special handling
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 or @_ > 2 0 30 30
Condition33%1/3
@_ == 0 or @_ > 2
l!l&&r!l&&!r
0030
MC/DC0%0/2
@_ == 0 or @_ > 2
@_ == 0 @_ > 2
Truth table33%1/3
A or B
A= @_ == 0B= @_ > 2
ABresult
1X1
011
000
48  
49 # Read Barriers file from handle and set constructor arguments.
50 30 my $barriers_fh;
51 my @args;
52 30 ▶ if (@_ == 1 and not reftype $_[0]) { # file path given
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and not builtin::reftype($_[0])) { } 0 30 30
elsif (builtin::reftype($_[0]) eq builtin::reftype(\*STDIN)) { } 30 0 30
Condition66%2/3
@_ == 1 and not builtin::reftype($_[0])
!ll&&!rl&&r
2280
MC/DC0%0/2
@_ == 1 and not builtin::reftype($_[0])
@_ == 1 not builtin::reftype($_[0])
Truth table66%2/3
A and not(B)
A= @_ == 1B= not builtin::reftype($_[0])
ABresult
0X0
100
111
53 0 my $file_path = shift;
54 0 my ($volume, $directory, $file_name) = splitpath $file_path;
55 0 push @args, (
56 _volume => $volume,
57 _directory => $directory,
58 file_name => $file_name,
59 );
60  
61 0 open $barriers_fh, '<', $file_path;
62 }
63 elsif (reftype $_[0] eq reftype \*STDIN) { # file handle given
64 30 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 2 28 30
65 # Check if second arg may be a file name
66 2 ▶ return $class->$orig(@_) if reftype $_[1]; # it's no file name
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($_[1]) 0 2 2
67 2 push @args, (file_name => $_[1]);
68 }
69 else {
70 28 push @args, (file_name => undef); # no file name given
71 }
72 30 $barriers_fh = shift;
73 }
74 else {
75 0 return $class->$orig(@_);
76 }
77  
78 # Parse data from handle and create arguments.
79 30 push @args, $class->_read_barriers_file($barriers_fh);
80 27 return $class->$orig(@args);
81 12 };
82  
83 30 ▶sub _read_barriers_file {
Subroutine _read_barriers_file: called CC=8 SCAR=21.1
84 30 my ($self, $barriers_fh) = @_;
85 30 my %args;
86  
87 # Read sequence from first line
88 30 my $sequence = do {
89 30 my $line = <$barriers_fh>;
90 30 ▶ confess 'Could not read sequence from Barriers file handle'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 30 30
91 unless defined $line;
92 30 chomp $line;
93 30 $line =~ s/^\s*//r; # trim leading space
94 };
95 30 $args{seq} = $sequence;
96  
97 # Continue reading minima
98 30 my ($father, @mins);
99 30 ▶ while (defined (my $line = <$barriers_fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $barriers_fh)) 581 27 608
100 581 my $min = Bio::RNA::Barriers::Minimum->new($line);
101 578 push @mins, $min;
102 }
103  
104 27 ▶ confess 'No minima found in Barriers file'
Branch: 1 branch
BranchTrueFalseTotal
unless @mins 0 27 27
105 unless @mins;
106  
107 # Set references to father mins (if any). This needs to be done after
108 # minima construction because, if several minima share the same
109 # energy, a father may have a higher index than the doughter min (and
110 # thus not have been parsed when parsing the doughter).
111 27 foreach my $min (@mins) {
112 555 ▶ if ($min->has_father) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min->has_father) 526 29 555
113 526 my $father = $mins[$min->father_index - 1];
114 526 ▶ confess 'Inconsistent father indexing in Barriers file'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $father and $father->index == $min->father_index 0 526 526
Condition33%1/3
ref $father and $father->index == $min->father_index
!ll&&!rl&&r
00526
MC/DC0%0/2
ref $father and $father->index == $min->father_index
ref $father $father->index == $min->father_index
Truth table33%1/3
A and B
A= ref $fatherB= $father->index == $min->father_index
ABresult
0X0
100
111
115 unless ref $father
116 and $father->index == $min->father_index;
117 526 $min->father($father);
118 }
119 }
120  
121 27 $args{mins} = \@mins;
122  
123 27 return %args;
124 }
125  
126 # Dereferenced list of all minima. Select individual minima using
127 # get_min[s]().
128 405 ▶sub mins {
Subroutine mins: called CC=1 SCAR=0.0
Pod documented
129 405 my $self = shift;
130 405 return @{ $self->_mins };
131 }
132  
133 372 ▶sub get_mins {
Subroutine get_mins: called CC=4 SCAR=14.5
Pod documented
134 372 my ($self, @min_indices) = @_;
135  
136 372 foreach my $min_index (@min_indices) {
137 372 ▶ confess "There is no minimum $min_index in this file"
Branch: 1 branch
BranchTrueFalseTotal
if $min_index < 1 or $min_index > $self->mins 0 372 372
Condition33%1/3
$min_index < 1 or $min_index > $self->mins
l!l&&r!l&&!r
00372
MC/DC0%0/2
$min_index < 1 or $min_index > $self->mins
$min_index < 1 $min_index > $self->mins
Truth table33%1/3
A or B
A= $min_index < 1B= $min_index > $self->mins
ABresult
1X1
011
000
138 if $min_index < 1 or $min_index > $self->mins;
139 372 $min_index--; # Perl is 0-based, barriers 1-based
140 }
141  
142 372 my @mins = @{$self->_mins}[@min_indices]; # array slice
143 372 return @mins;
144 }
145  
146 372 ▶sub get_min {
Subroutine get_min: called CC=1 SCAR=0.0
Pod documented
147 372 my ($self, $min_index) = @_;
148 372 my ($min) = $self->get_mins($min_index);
149 372 return $min;
150 }
151  
152 8 ▶sub get_global_min {
Subroutine get_global_min: called CC=1 SCAR=0.0
Pod documented
153 8 my ($self) = @_;
154 8 my $mfe_min = $self->get_min(1); # basin 1 == mfe basin
155 8 return $mfe_min;
156 }
157  
158 # Get the global minimum free energy.
159 4 ▶sub mfe {
Subroutine mfe: called CC=1 SCAR=0.0
Pod documented
160 4 my ($self) = @_;
161 4 my $mfe = $self->get_global_min->mfe;
162 4 return $mfe;
163 }
164  
165 # Get the delta_E of the landscape, i. e. the energy difference between the
166 # (global) minimum free energy and and the highest energy encountered.
167 4 ▶sub delta_energy {
Subroutine delta_energy: called CC=1 SCAR=0.0
Pod documented
168 4 my ($self) = @_;
169 # Barrier height of the mfe basin is the explored energy bandwidth
170 4 my $delta_energy = $self->get_global_min->barrier_height;
171 4 return $delta_energy;
172 }
173  
174 # Get the maximum energy of any structure in the landscape.
175 2 ▶sub max_energy {
Subroutine max_energy: called CC=1 SCAR=0.0
Pod documented
176 2 my ($self) = @_;
177 # Energy values from Bar file have only 2 digits precision.
178 2 my $max_energy = sprintf "%.2f", $self->mfe + $self->delta_energy;
179 2 return $max_energy;
180 }
181  
182 9 ▶sub min_count {
Subroutine min_count: called CC=1 SCAR=0.0
Pod documented
183 9 my $self = shift;
184 9 my $min_count = $self->mins;
185 9 return $min_count;
186 }
187  
188 # List of all minima connected to the mfe minimum (min 1).
189 0 ▶sub connected_mins {
Subroutine connected_mins: not called CC=1 SCAR=6.9
Pod documented
190 0 my $self = shift;
191 0 my @connected_mins = grep {$_->is_connected} $self->mins;
192 0 return @connected_mins;
193 }
194  
195 # List of indices of all connected minima (cf. connected_mins()).
196 0 ▶sub connected_indices {
Subroutine connected_indices: not called CC=1 SCAR=6.9
Pod documented
197 0 my $self = shift;
198 0 my @connected_indices = map {$_->index} $self->connected_mins;
199 0 return @connected_indices;
200 }
201  
202 # Re-index minima after deleting some of them.
203 0 ▶sub _reindex {
Subroutine _reindex: not called CC=3 SCAR=24.8
204 0 my $self = shift;
205  
206 0 my $i = 1;
207 0 my @mins = $self->mins;
208  
209 # Update min indices.
210 0 $_->index($i++) foreach @mins;
211  
212 # Update father indices.
213 0 shift @mins; # min 1 is orphan
214 0 $_->father_index($_->father->index) foreach @mins;
215  
216 0 return;
217 }
218  
219 # Keep only connected minima and remove all others. The indices are
220 # remapped to 1..k for k kept minima.
221 # Returns (old) indices of all kept minima.
222 0 ▶sub keep_connected {
Subroutine keep_connected: not called CC=1 SCAR=6.9
Pod documented
223 0 my $self = shift;
224  
225 0 my @connected_indices = $self->connected_indices;
226 0 my @connected_mins = $self->connected_mins;
227  
228 # Update minima list
229 0 @{ $self->_mins } = @connected_mins;
230 0 $self->_reindex;
231  
232 0 return @connected_indices;
233 }
234  
235 # Given an ordered list of indices of all connected minima (as returned by
236 # RateMatrix::keep_connected()), delete all other minima and update their
237 # ancesters' basin size information accordingly.
238 # Arguments:
239 # ordered_connected_indices: ordered list of indices of (all???)
240 # connected minima.
241 0 ▶sub update_connected {
Subroutine update_connected: not called CC=4 SCAR=30.0
Pod documented
242 0 my ($self, @ordered_connected_indices) = @_;
243  
244 # Go through all mins and check whether they're next in the connected
245 # (==kept) index list. If not, add to removal list.
246 0 my @connected_mins = $self->get_mins(@ordered_connected_indices);
247 0 my @removed_indices;
248 0 for my $min_index (1..$self->min_count) {
249 0 ▶ unless (@ordered_connected_indices) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ordered_connected_indices) 0 0 0
250 # No exclusions left, add rest and stop
251 0 push @removed_indices, $min_index..$self->min_count;
252 0 last;
253 }
254 0 ▶ if ($min_index == $ordered_connected_indices[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_index == $ordered_connected_indices[0]) 0 0 0
255 0 shift @ordered_connected_indices;
256 0 next;
257 }
258 0 push @removed_indices, $min_index; # min is deleted
259 }
260  
261 0 my @removed_mins = $self->get_mins(@removed_indices);
262 0 $self->_update_ancestors(@removed_mins);
263 0 @{ $self->_mins } = @connected_mins;
264 0 $self->_reindex;
265  
266 0 return;
267 }
268  
269 # Pass a list of ORDERED deleted minima and update their ancestors' bsize
270 # attributes.
271 0 ▶sub _update_ancestors {
Subroutine _update_ancestors: not called CC=4 SCAR=30.0
272 0 my ($self, @removed_mins) = @_;
273  
274 # If the bsize attributes are present, update the basin energy of all
275 # (grand)* father basins (substract energy of this one).
276 # The minima need to be processed in reversed order because, if an
277 # ancester of a removed min is also removed, its merged basin energy
278 # includes the basin energy of its child, and thus this contribution
279 # would be substracted multiple times from older ancesters. In
280 # reversed order, the child contribution is first substracted from the
281 # ancestors, and then the contribution of the removed ancestors does
282 # not include the child anymore.
283  
284 0 ▶ return unless $self->has_bsize;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_bsize 0 0 0
285  
286 0 foreach my $removed_min (reverse @removed_mins) {
287 0 foreach my $ancestor_min ($removed_min->ancestors) {
288 0 $ancestor_min->_merged_basin_energy( # private writer
289 $ancestor_min->merged_basin_energy
290 - $removed_min->grad_basin_energy
291 );
292 }
293 }
294  
295 0 return;
296 }
297  
298 # Keep only the first k mins. If there are only k or less mins, do
299 # nothing.
300 # WARNING: THIS CAN DISCONNECT THE LANDSCAPE! The bar file will still look
301 # connected, however, modifying the rate matrix accordingly can lead to
302 # non-ergodicity (e.g. when basin 3 merged to 2, 2/3 merged to 1 because
303 # of a possible transition from 3 to 1, and basin 3 is then removed).
304 # To cope with that, call RateMatrix::keep_connected() and, with its
305 # return value, Results::update_connected() again.
306 # Arguments:
307 # max_min_count: number of minima to keep.
308 # Returns a list of all removed mins (may be empty).
309 0 ▶sub keep_first_mins {
Subroutine keep_first_mins: not called CC=1 SCAR=6.9
Pod documented
310 0 my ($self, $max_min_count) = @_;
311  
312 0 my @removed_mins = splice @{ $self->_mins }, $max_min_count;
313 0 $self->_update_ancestors(@removed_mins);
314  
315  
316 0 return @removed_mins;
317 }
318  
319  
320 # Check whether the stored minima contain information about the basin
321 # sizes as computed by Barriers' --bsize switch. Checks only the mfe
322 # basin (since all or neither min should have this information).
323 0 ▶sub has_bsize {
Subroutine has_bsize: not called CC=1 SCAR=6.9
Pod documented
324 0 my ($self) = @_;
325 0 my $has_bsize = $self->get_global_min->has_bsize;
326 0 return $has_bsize;
327 }
328  
329  
330 # Construct the file path to this Barriers file. Works only if it was
331 # actually parsed from a file (of course...).
332 # Returns the path as a string.
333 0 ▶sub path {
Subroutine path: not called CC=4 SCAR=30.0
Pod documented
334 0 my $self = shift;
335 0 ▶ confess 'Cannot construct path, missing volume, directory or file name'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->file_name and $self->has_volume and $self->has_directory 0 0 0
Condition0%0/6
defined $self->file_name and $self->has_volume and $self->has_directory
!ll&&!rl&&r
000
defined $self->file_name and $self->has_volume
!ll&&!rl&&r
000
MC/DC0%0/3
defined $self->file_name and $self->has_volume and $self->has_directory
defined $self->file_name $self->has_volume $self->has_directory
Truth table0%0/4
A and B and C
A= defined $self->file_nameB= $self->has_volumeC= $self->has_directory
ABCresult
0XX0
10X0
1100
1111
336 unless defined $self->file_name
337 and $self->has_volume
338 and $self->has_directory
339 ;
340 0 my ($volume, $dir, $file_name)
341 = ($self->_volume, $self->_directory, $self->file_name);
342 0 my $path = catpath($volume, $dir, $file_name);
343 0 return $path;
344 }
345  
346  
347 # Convert back to Barriers file.
348 12 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
349 12 my $self = shift;
350  
351 12 my $header = (q{ } x 5) . $self->seq;
352 12 my @lines = ($header, map { $_->stringify } $self->mins);
353  
354 12 return join "\n", @lines;
355 }
356  
357 12 __PACKAGE__->meta->make_immutable;
358  
359 12 1; # End of Bio::RNA::Barriers::Results
360  
361  
362 __END__
« Barriers/RateMatrix.pm ↑ Summary Barriers/Minimum.pm »