| 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 is the strand | ||||||||||
| 23 | and its secondary key is the reference sequence name. Each such pair of keys correspond to an | ||||||||||
| 24 | array reference which stores objects of the class L<GenOO::Region> sorted by start position. | ||||||||||
| 25 | |||||||||||
| 26 | =head1 EXAMPLES | ||||||||||
| 27 | |||||||||||
| 28 | # Print records in FASTA format | ||||||||||
| 29 | $locus_collection->print("FASTA",'STDOUT',"/data1/data/UCSC/hg19/chromosomes/"); | ||||||||||
| 30 | |||||||||||
| 31 | # ditto | ||||||||||
| 32 | $locus_collection->print_in_fasta_format('STDOUT',"/data1/data/UCSC/hg19/chromosomes/"); | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | # Let the code begin... | ||||||||||
| 37 | |||||||||||
| 38 | package GenOO::RegionCollection::Type::DoubleHashArray; | ||||||||||
| 39 | 1 | $GenOO::RegionCollection::Type::DoubleHashArray::VERSION = '1.4.9'; | |||||||||
| 40 | |||||||||||
| 41 | ####################################################################### | ||||||||||
| 42 | ####################### Load External modules ##################### | ||||||||||
| 43 | ####################################################################### | ||||||||||
| 44 | 1 | ▶ | use Modern::Perl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 1 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | ####################################################################### | ||||||||||
| 51 | ######################### Load GenOO modules ###################### | ||||||||||
| 52 | ####################################################################### | ||||||||||
| 53 | 1 | ▶ | use GenOO::Module::Search::Binary; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 54 | 1 | ▶ | use GenOO::Data::Structure::DoubleHashArray; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 55 | |||||||||||
| 56 | |||||||||||
| 57 | ####################################################################### | ||||||||||
| 58 | ####################### Interface attributes ###################### | ||||||||||
| 59 | ####################################################################### | ||||||||||
| 60 | 1 | has 'name' => ( | |||||||||
| 61 | isa => 'Str', | ||||||||||
| 62 | is => 'rw' | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | 1 | has 'species' => ( | |||||||||
| 66 | isa => 'Str', | ||||||||||
| 67 | is => 'rw' | ||||||||||
| 68 | ); | ||||||||||
| 69 | |||||||||||
| 70 | 1 | has 'description' => ( | |||||||||
| 71 | isa => 'Str', | ||||||||||
| 72 | is => 'rw' | ||||||||||
| 73 | ); | ||||||||||
| 74 | |||||||||||
| 75 | 1 | has 'longest_record' => ( | |||||||||
| 76 | is => 'ro', | ||||||||||
| 77 | builder => '_find_longest_record', | ||||||||||
| 78 | clearer => '_clear_longest_record', | ||||||||||
| 79 | init_arg => undef, | ||||||||||
| 80 | lazy => 1, | ||||||||||
| 81 | ); | ||||||||||
| 82 | |||||||||||
| 83 | 1 | has 'extra' => ( | |||||||||
| 84 | is => 'rw' | ||||||||||
| 85 | ); | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | ####################################################################### | ||||||||||
| 89 | ######################## Private attributes ####################### | ||||||||||
| 90 | ####################################################################### | ||||||||||
| 91 | 1 | has '_container' => ( | |||||||||
| 92 | is => 'ro', | ||||||||||
| 93 | builder => '_build_container', | ||||||||||
| 94 | init_arg => undef, | ||||||||||
| 95 | lazy => 1 | ||||||||||
| 96 | ); | ||||||||||
| 97 | |||||||||||
| 98 | ####################################################################### | ||||||||||
| 99 | ########################## Consumed Roles ######################### | ||||||||||
| 100 | ####################################################################### | ||||||||||
| 101 | 1 | with 'GenOO::RegionCollection'; | |||||||||
| 102 | |||||||||||
| 103 | |||||||||||
| 104 | ####################################################################### | ||||||||||
| 105 | ######################## Interface Methods ######################## | ||||||||||
| 106 | ####################################################################### | ||||||||||
| 107 | sub add_record { | ||||||||||
| 108 | 1768 | ▶ | my ($self, $record) = @_; | ||||||||
|
Subroutine
add_record: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 109 | 1768 | $self->_container->add_entry($record->strand, $record->rname, $record); | |||||||||
| 110 | 1768 | $self->_reset; | |||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | sub all_records { | ||||||||||
| 114 | 6 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
all_records: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 115 | |||||||||||
| 116 | 6 | my @all_records; | |||||||||
| 117 | $self->foreach_record_do(sub { | ||||||||||
| 118 | 158 | ▶ | push @all_records, $_[0]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 119 | 6 | }); | |||||||||
| 120 | |||||||||||
| 121 | 6 | ▶ | return wantarray ? @all_records : \@all_records; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | sub foreach_record_do { | ||||||||||
| 125 | 14 | ▶ | my ($self, $block) = @_; | ||||||||
|
Subroutine
foreach_record_do: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 126 | 14 | $self->_container->foreach_entry_do($block); | |||||||||
| 127 | } | ||||||||||
| 128 | |||||||||||
| 129 | sub foreach_record_on_rname_do { | ||||||||||
| 130 | 1 | ▶ | my ($self, $rname, $block) = @_; | ||||||||
|
Subroutine
foreach_record_on_rname_do: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 131 | 1 | $self->_container->foreach_entry_on_secondary_key_do($rname, $block); | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | sub records_count { | ||||||||||
| 135 | 14 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
records_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 136 | 14 | return $self->_container->entries_count; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | sub strands { | ||||||||||
| 140 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
strands: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 141 | 1 | return $self->_container->primary_keys(); | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | sub rnames_for_strand { | ||||||||||
| 145 | 2 | ▶ | my ($self, $strand) = @_; | ||||||||
|
Subroutine
rnames_for_strand: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 146 | 2 | return $self->_container->secondary_keys_for_primary_key($strand); | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | sub rnames_for_all_strands { | ||||||||||
| 150 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
rnames_for_all_strands: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 151 | 1 | return $self->_container->secondary_keys_for_all_primary_keys(); | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | sub longest_record_length { | ||||||||||
| 155 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
longest_record_length: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 156 | 1 | return $self->longest_record->length; | |||||||||
| 157 | } | ||||||||||
| 158 | |||||||||||
| 159 | sub is_empty { | ||||||||||
| 160 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 161 | 1 | return $self->_container->is_empty; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | sub is_not_empty { | ||||||||||
| 165 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_not_empty: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 166 | 1 | return $self->_container->is_not_empty; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | sub foreach_overlapping_record_do { | ||||||||||
| 170 | 3 | ▶ | my ($self, $strand, $chr, $start, $stop, $block) = @_; | ||||||||
|
Subroutine
foreach_overlapping_record_do: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||
| 171 | |||||||||||
| 172 | 3 | $self->_container->sort_entries; | |||||||||
| 173 | |||||||||||
| 174 | # Get a reference on the array containing the records of the specified strand and rname | ||||||||||
| 175 | 3 | ▶ | my $records_ref = $self->_records_ref_for_strand_and_rname($strand, $chr) or return (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | |||||||||||
| 177 | # Find the closest but greater value to a target value. Target value is defined such that | ||||||||||
| 178 | # any records with smaller values are impossible to overlap with the requested region | ||||||||||
| 179 | # This allows us to search only from that point onwards for overlap | ||||||||||
| 180 | 3 | my $target_value = $start - $self->longest_record->length; | |||||||||
| 181 | my $index = GenOO::Module::Search::Binary->binary_search_for_value_greater_or_equal( | ||||||||||
| 182 | $target_value, $records_ref, | ||||||||||
| 183 | sub { | ||||||||||
| 184 | 15 | ▶ | return $_[0]->start | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 185 | } | ||||||||||
| 186 | 3 | ); | |||||||||
| 187 | |||||||||||
| 188 | # If a value close and greater than the target value exists scan downstream for overlaps | ||||||||||
| 189 | 3 | ▶ | if (defined $index) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 3 | ▶ | while ($index < @$records_ref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 9 | my $record = $records_ref->[$index]; | |||||||||
| 192 | 9 | ▶ | if ($record->start <= $stop) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 193 | 9 | ▶ | if ($start <= $record->stop) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 8 | ▶ | last if $block->($record) eq 'break_loop'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 195 | } | ||||||||||
| 196 | } | ||||||||||
| 197 | else { | ||||||||||
| 198 | 0 | last; # No chance to find overlap from now on | |||||||||
| 199 | } | ||||||||||
| 200 | 9 | $index++; | |||||||||
| 201 | } | ||||||||||
| 202 | } | ||||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | sub records_overlapping_region { | ||||||||||
| 206 | 2 | ▶ | my ($self, $strand, $chr, $start, $stop) = @_; | ||||||||
|
Subroutine
records_overlapping_region: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 207 | |||||||||||
| 208 | 2 | my @overlapping_records; | |||||||||
| 209 | $self->foreach_overlapping_record_do($strand, $chr, $start, $stop, | ||||||||||
| 210 | sub { | ||||||||||
| 211 | 5 | ▶ | my ($record) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 212 | 5 | push @overlapping_records, $record; | |||||||||
| 213 | } | ||||||||||
| 214 | 2 | ); | |||||||||
| 215 | |||||||||||
| 216 | 2 | return @overlapping_records; | |||||||||
| 217 | } | ||||||||||
| 218 | |||||||||||
| 219 | sub total_copy_number { | ||||||||||
| 220 | 1 | ▶ | my ($self, $block) = @_; | ||||||||
|
Subroutine
total_copy_number: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 221 | |||||||||||
| 222 | 1 | my $total_copy_number = 0; | |||||||||
| 223 | 1 | ▶ | $self->foreach_record_do( sub {$total_copy_number += $_[0]->copy_number} ); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 224 | |||||||||||
| 225 | 1 | return $total_copy_number; | |||||||||
| 226 | } | ||||||||||
| 227 | |||||||||||
| 228 | sub total_copy_number_for_records_contained_in_region { | ||||||||||
| 229 | 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
| |||||||||||
| 230 | |||||||||||
| 231 | 0 | my $total_copy_number = 0; | |||||||||
| 232 | 0 | ▶ | $self->foreach_overlapping_record_do( $strand, $rname, $start, $stop, sub {$total_copy_number += $_[0]->copy_number} ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 233 | |||||||||||
| 234 | 0 | return $total_copy_number; | |||||||||
| 235 | } | ||||||||||
| 236 | |||||||||||
| 237 | ####################################################################### | ||||||||||
| 238 | ######################### Private methods ########################## | ||||||||||
| 239 | ####################################################################### | ||||||||||
| 240 | sub _build_container { | ||||||||||
| 241 | return GenOO::Data::Structure::DoubleHashArray->new( | ||||||||||
| 242 | 22 | ▶ | sorting_code_block => sub {return $_[0]->start <=> $_[1]->start} | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 243 | 43 | ▶ | ); | ||||||||
|
Subroutine
_build_container: called CC=1 SCAR=0.0
| |||||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | sub _find_longest_record { | ||||||||||
| 247 | 5 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_find_longest_record: called CC=1 SCAR=0.0
| |||||||||||
| 248 | |||||||||||
| 249 | 5 | my $longest_record; | |||||||||
| 250 | 5 | my $longest_record_length = 0; | |||||||||
| 251 | $self->foreach_record_do( | ||||||||||
| 252 | sub { | ||||||||||
| 253 | 61 | ▶ | my ($record) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 254 | |||||||||||
| 255 | 61 | ▶ | if ($record->length > $longest_record_length) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 256 | 13 | $longest_record_length = $record->length; | |||||||||
| 257 | 13 | $longest_record = $record; | |||||||||
| 258 | } | ||||||||||
| 259 | } | ||||||||||
| 260 | 5 | ); | |||||||||
| 261 | |||||||||||
| 262 | 5 | return $longest_record; | |||||||||
| 263 | } | ||||||||||
| 264 | |||||||||||
| 265 | sub _records_ref_for_strand_and_rname { | ||||||||||
| 266 | 4 | ▶ | my ($self, $strand, $chr) = @_; | ||||||||
|
Subroutine
_records_ref_for_strand_and_rname: called CC=1 SCAR=0.0
| |||||||||||
| 267 | 4 | return $self->_container->entries_ref_for_keys($strand, $chr); | |||||||||
| 268 | } | ||||||||||
| 269 | |||||||||||
| 270 | sub _reset { | ||||||||||
| 271 | 1768 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_reset: called CC=1 SCAR=0.0
| |||||||||||
| 272 | 1768 | $self->_clear_longest_record; | |||||||||
| 273 | } | ||||||||||
| 274 | |||||||||||
| 275 | |||||||||||
| 276 | ####################################################################### | ||||||||||
| 277 | ############################ Finalize ############################# | ||||||||||
| 278 | ####################################################################### | ||||||||||
| 279 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 280 | 1 | 1; | |||||||||