GenOO/RegionCollection/Type/DoubleHashArray.pm

filter: Statementstmt 77.9% 92 / 118 Branchbran 39.2% 11 / 28 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 30 / 36 Podpod 0.0% 0 / 17 totaltotal 65.6% 134 / 204 CC 16 SCAR 23.0
CC
16
Cov
70%
foreach_overlapping_record_do
56.0
foreach_contained_record_do
9.0
all_records
2.0
CRAP 10.0
« GenOO/Data/File/SAM/CigarAndMDZ.pm ↑ Summary GenOO/Module/Search/Binary.pm »
1 # POD documentation - main docs before the code
2  
3 =head1 NAME
4  
5 GenOO::RegionCollection::Type::DoubleHashArray - Object for a collection of GenOO::Region objects, with features
6  
7 =head1 SYNOPSIS
8  
9 # Object that manages a collection of GenOO::Region objects.
10  
11 # To initialize
12 my $locus_collection = GenOO::RegionCollection::DoubleHashArray->new({
13 name => undef,
14 species => undef,
15 description => undef,
16 extra => undef,
17 });
18  
19  
20 =head1 DESCRIPTION
21  
22 The primary data structure of this object is a 2D hash whose primary key
23 is the strand and its secondary key is the reference sequence name. Each
24 such pair of keys correspond to an array reference which stores objects of
25 the class L<GenOO::Region> sorted by start position.
26  
27 =head1 EXAMPLES
28  
29 # Get the records contained in a specific region
30 my @recs = $region_collection->records_contained_in_region(
31 1, 'chr3', 127726308, 127792250);
32  
33 # Get the longest record
34 my $longest_record = $region_collection->longest_record;
35  
36 =cut
37  
38 # Let the code begin...
39  
40 package GenOO::RegionCollection::Type::DoubleHashArray;
41 1 $GenOO::RegionCollection::Type::DoubleHashArray::VERSION = '1.5.0';
42  
43 #######################################################################
44 ####################### Load External modules #####################
45 #######################################################################
46 1 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52 #######################################################################
53 ######################### Load GenOO modules ######################
54 #######################################################################
55 1 ▶use GenOO::Module::Search::Binary;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use GenOO::Data::Structure::DoubleHashArray;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58  
59 #######################################################################
60 ####################### Interface attributes ######################
61 #######################################################################
62 1 has 'name' => (
63 isa => 'Str',
64 is => 'rw'
65 );
66  
67 1 has 'species' => (
68 isa => 'Str',
69 is => 'rw'
70 );
71  
72 1 has 'description' => (
73 isa => 'Str',
74 is => 'rw'
75 );
76  
77 1 has 'longest_record' => (
78 is => 'ro',
79 builder => '_find_longest_record',
80 clearer => '_clear_longest_record',
81 init_arg => undef,
82 lazy => 1,
83 );
84  
85 1 has 'extra' => (
86 is => 'rw'
87 );
88  
89  
90 #######################################################################
91 ######################## Private attributes #######################
92 #######################################################################
93 1 has '_container' => (
94 is => 'ro',
95 builder => '_build_container',
96 init_arg => undef,
97 lazy => 1
98 );
99  
100 #######################################################################
101 ########################## Consumed Roles #########################
102 #######################################################################
103 1 with 'GenOO::RegionCollection';
104  
105  
106 #######################################################################
107 ######################## Interface Methods ########################
108 #######################################################################
109 sub add_record {
110 1768 ▶ my ($self, $record) = @_;
Subroutine add_record: called CC=1 SCAR=0.0
Pod undocumented
111 1768 $self->_container->add_entry($record->strand, $record->rname, $record);
112 1768 $self->_reset;
113 }
114  
115 sub all_records {
116 6 ▶ my ($self) = @_;
Subroutine all_records: called CC=2 SCAR=6.9
Pod undocumented
117  
118 6 my @all_records;
119 $self->foreach_record_do(sub {
120 158 ▶ push @all_records, $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
121 6 });
122  
123 6 ▶ return wantarray ? @all_records : \@all_records;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 2 6
124 }
125  
126 sub foreach_record_do {
127 12 ▶ my ($self, $block) = @_;
Subroutine foreach_record_do: called CC=1 SCAR=0.0
Pod undocumented
128 12 $self->_container->foreach_entry_do($block);
129 }
130  
131 sub foreach_record_on_rname_do {
132 1 ▶ my ($self, $rname, $block) = @_;
Subroutine foreach_record_on_rname_do: called CC=1 SCAR=0.0
Pod undocumented
133 1 $self->_container->foreach_entry_on_secondary_key_do($rname, $block);
134 }
135  
136 sub records_count {
137 14 ▶ my ($self) = @_;
Subroutine records_count: called CC=1 SCAR=0.0
Pod undocumented
138 14 return $self->_container->entries_count;
139 }
140  
141 sub strands {
142 1 ▶ my ($self) = @_;
Subroutine strands: called CC=1 SCAR=0.0
Pod undocumented
143 1 return $self->_container->primary_keys();
144 }
145  
146 sub rnames_for_strand {
147 2 ▶ my ($self, $strand) = @_;
Subroutine rnames_for_strand: called CC=1 SCAR=0.0
Pod undocumented
148 2 return $self->_container->secondary_keys_for_primary_key($strand);
149 }
150  
151 sub rnames_for_all_strands {
152 1 ▶ my ($self) = @_;
Subroutine rnames_for_all_strands: called CC=1 SCAR=0.0
Pod undocumented
153 1 return $self->_container->secondary_keys_for_all_primary_keys();
154 }
155  
156 sub longest_record_length {
157 1 ▶ my ($self) = @_;
Subroutine longest_record_length: called CC=1 SCAR=0.0
Pod undocumented
158 1 return $self->longest_record->length;
159 }
160  
161 sub is_empty {
162 1 ▶ my ($self) = @_;
Subroutine is_empty: called CC=1 SCAR=0.0
Pod undocumented
163 1 return $self->_container->is_empty;
164 }
165  
166 sub is_not_empty {
167 1 ▶ my ($self) = @_;
Subroutine is_not_empty: called CC=1 SCAR=0.0
Pod undocumented
168 1 return $self->_container->is_not_empty;
169 }
170  
171 sub foreach_contained_record_do {
172 3 ▶ my ($self, $strand, $chr, $start, $stop, $block) = @_;
Subroutine foreach_contained_record_do: called CC=8 SCAR=22.0
Pod undocumented
173  
174 3 $self->_container->sort_entries;
175  
176 # Get a reference on the array containing the records of the specified strand and rname
177 3 ▶ my $records_ref = $self->_records_ref_for_strand_and_rname($strand, $chr) or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $records_ref = $self->_records_ref_for_strand_and_rname($strand, $chr) or return () 0 3 3
178  
179 # Get index of record whose start is closest but greater than the start of the region.
180 my $index = GenOO::Module::Search::Binary->binary_search_for_value_greater_or_equal(
181 3 ▶ $start, $records_ref, sub {return $_[0]->start});
Subroutine __ANON__: called CC=1 SCAR=0.0
182 3 ▶ return 0 if !defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 3 3
183  
184 # Scan records downstream of index for overlaps
185 3 ▶ while ($index < @$records_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$records_ref) 6 3 9
186 6 my $record = $records_ref->[$index];
187 6 ▶ if ($record->stop <= $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->stop <= $stop) 6 0 6
188 6 my $return = $block->($record);
189 6 ▶ last if defined $return and $return eq 'break_loop';
Branch: 1 branch
BranchTrueFalseTotal
if defined $return and $return eq "break_loop" 0 6 6
Condition33%1/3
defined $return and $return eq "break_loop"
!ll&&!rl&&r
060
MC/DC0%0/2
defined $return and $return eq "break_loop"
defined $return $return eq "break_loop"
Truth table33%1/3
A and B
A= defined $returnB= $return eq "break_loop"
ABresult
0X0
100
111
190 }
191 6 ▶ last if $record->start > $stop; # no chance to find overlap after this
Branch: 1 branch
BranchTrueFalseTotal
if $record->start > $stop 0 6 6
192 6 $index++;
193 }
194 }
195  
196 sub records_contained_in_region {
197 2 ▶ my ($self, $strand, $chr, $start, $stop) = @_;
Subroutine records_contained_in_region: called CC=1 SCAR=0.0
Pod undocumented
198  
199 2 my @records;
200 $self->foreach_contained_record_do($strand, $chr, $start, $stop, sub {
201 4 ▶ push @records, $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
202 2 });
203  
204 2 return @records;
205 }
206  
207 sub total_copy_number {
208 1 ▶ my ($self, $block) = @_;
Subroutine total_copy_number: called CC=1 SCAR=0.0
Pod undocumented
209  
210 1 my $total_copy_number = 0;
211 1 ▶ $self->foreach_record_do( sub {$total_copy_number += $_[0]->copy_number} );
Subroutine __ANON__: called CC=1 SCAR=0.0
212  
213 1 return $total_copy_number;
214 }
215  
216 sub total_copy_number_for_records_contained_in_region {
217 0 ▶ my ($self, $strand, $rname, $start, $stop) = @_;
Subroutine total_copy_number_for_records_contained_in_region: not called CC=1 SCAR=6.9
Pod undocumented
218  
219 0 my $total_copy_number = 0;
220 $self->foreach_contained_record_do($strand, $rname, $start, $stop, sub {
221 0 ▶ $total_copy_number += $_[0]->copy_number
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 0 });
223  
224 0 return $total_copy_number;
225 }
226  
227 #######################################################################
228 ######################### Private methods ##########################
229 #######################################################################
230 sub _build_container {
231 return GenOO::Data::Structure::DoubleHashArray->new(
232 22 ▶ sorting_code_block => sub {return $_[0]->start <=> $_[1]->start}
Subroutine __ANON__: called CC=1 SCAR=0.0
233 43 ▶ );
Subroutine _build_container: called CC=1 SCAR=0.0
234 }
235  
236 sub _find_longest_record {
237 3 ▶ my ($self) = @_;
Subroutine _find_longest_record: called CC=1 SCAR=0.0
238  
239 3 my $longest_record;
240 3 my $longest_record_length = 0;
241 $self->foreach_record_do(
242 sub {
243 37 ▶ my ($record) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
244  
245 37 ▶ if ($record->length > $longest_record_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->length > $longest_record_length) 13 24 37
246 13 $longest_record_length = $record->length;
247 13 $longest_record = $record;
248 }
249 }
250 3 );
251  
252 3 return $longest_record;
253 }
254  
255 sub _records_ref_for_strand_and_rname {
256 4 ▶ my ($self, $strand, $chr) = @_;
Subroutine _records_ref_for_strand_and_rname: called CC=1 SCAR=0.0
257 4 return $self->_container->entries_ref_for_keys($strand, $chr);
258 }
259  
260 sub _reset {
261 1768 ▶ my ($self) = @_;
Subroutine _reset: called CC=1 SCAR=0.0
262 1768 $self->_clear_longest_record;
263 }
264  
265  
266 #######################################################################
267 ####################### Deprecated methods #########################
268 #######################################################################
269 sub foreach_overlapping_record_do {
270 0 ▶ my ($self, $strand, $chr, $start, $stop, $block) = @_;
Subroutine foreach_overlapping_record_do: not called CC=7 SCAR=40.3
Pod undocumented
271  
272 0 warn 'Deprecated use of "foreach_overlapping_record_do". '.
273 'Use "foreach_contained_record_do" instead.' . "\n";
274  
275 0 $self->_container->sort_entries;
276  
277 # Get a reference on the array containing the records of the specified strand and rname
278 0 ▶ my $records_ref = $self->_records_ref_for_strand_and_rname($strand, $chr) or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $records_ref = $self->_records_ref_for_strand_and_rname($strand, $chr) or return () 0 0 0
279  
280 # Find the closest but greater value to a target value. Target value is defined such that
281 # any records with smaller values are impossible to overlap with the requested region
282 # This allows us to search only from that point onwards for overlap
283 0 my $target_value = $start - $self->longest_record->length;
284 my $index = GenOO::Module::Search::Binary->binary_search_for_value_greater_or_equal(
285 $target_value, $records_ref,
286 sub {
287 0 ▶ return $_[0]->start
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 }
289 0 );
290  
291 # If a value close and greater than the target value exists scan downstream for overlaps
292 0 ▶ if (defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) 0 0 0
293 0 ▶ while ($index < @$records_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @$records_ref) 0 0 0
294 0 my $record = $records_ref->[$index];
295 0 ▶ if ($record->start <= $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->start <= $stop) { } 0 0 0
296 0 ▶ if ($start <= $record->stop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start <= $record->stop) 0 0 0
297 0 ▶ last if $block->($record) eq 'break_loop';
Branch: 1 branch
BranchTrueFalseTotal
if &$block($record) eq "break_loop" 0 0 0
298 }
299 }
300 else {
301 0 last; # No chance to find overlap from now on
302 }
303 0 $index++;
304 }
305 }
306 }
307  
308 sub records_overlapping_region {
309 0 ▶ my ($self, $strand, $chr, $start, $stop) = @_;
Subroutine records_overlapping_region: not called CC=1 SCAR=6.9
Pod undocumented
310  
311 0 warn 'Deprecated use of "records_overlapping_region". '.
312 'Use "records_contained_in_region" instead.' . "\n";
313  
314 0 my @overlapping_records;
315 $self->foreach_overlapping_record_do($strand, $chr, $start, $stop, sub {
316 0 ▶ push @overlapping_records, $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
317 0 });
318  
319 0 return @overlapping_records;
320 }
321  
322  
323  
324 1 __PACKAGE__->meta->make_immutable;
325 1 1;
« GenOO/Data/File/SAM/CigarAndMDZ.pm ↑ Summary GenOO/Module/Search/Binary.pm »