| 1 | # POD documentation - main docs before the code | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | GenOO::Data::File::BED - Object implementing methods for accessing bed formatted files (http://genome.ucsc.edu/FAQ/FAQformat#format1) | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # Object that manages a bed file. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # To initialize | ||||||||||||||
| 12 | my $bed_file = GenOO::Data::File::BED->new({ | ||||||||||||||
| 13 | FILE => undef, | ||||||||||||||
| 14 | EXTRA_INFO => undef, | ||||||||||||||
| 15 | }); | ||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||
| 18 | |||||||||||||||
| 19 | This object offers methods to read a bed file line by line. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 EXAMPLES | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # Create object | ||||||||||||||
| 24 | my $bed_file = GenOO::Data::File::BED->new({ | ||||||||||||||
| 25 | FILE => 't/sample_data/sample.bed.gz' | ||||||||||||||
| 26 | }); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | # Read one record at a time | ||||||||||||||
| 29 | my $record = $bed_file->next_record(); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =cut | ||||||||||||||
| 32 | |||||||||||||||
| 33 | # Let the code begin... | ||||||||||||||
| 34 | |||||||||||||||
| 35 | package GenOO::Data::File::BED; | ||||||||||||||
| 36 | 1 | $GenOO::Data::File::BED::VERSION = '1.4.9'; | |||||||||||||
| 37 | |||||||||||||||
| 38 | ####################################################################### | ||||||||||||||
| 39 | ####################### Load External modules ##################### | ||||||||||||||
| 40 | ####################################################################### | ||||||||||||||
| 41 | 1 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 42 | 1 | ▶ | use autodie; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | ####################################################################### | ||||||||||||||
| 48 | ######################### Load GenOO modules ###################### | ||||||||||||||
| 49 | ####################################################################### | ||||||||||||||
| 50 | 1 | ▶ | use GenOO::Data::File::BED::Record; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | |||||||||||||||
| 52 | |||||||||||||||
| 53 | ####################################################################### | ||||||||||||||
| 54 | ####################### Interface attributes ###################### | ||||||||||||||
| 55 | ####################################################################### | ||||||||||||||
| 56 | 1 | has 'file' => ( | |||||||||||||
| 57 | isa => 'Maybe[Str]', | ||||||||||||||
| 58 | is => 'rw', | ||||||||||||||
| 59 | required => 1 | ||||||||||||||
| 60 | ); | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 1 | has 'redirect_score_to_copy_number' => ( | |||||||||||||
| 63 | traits => ['Bool'], | ||||||||||||||
| 64 | is => 'rw', | ||||||||||||||
| 65 | isa => 'Bool', | ||||||||||||||
| 66 | default => 0, | ||||||||||||||
| 67 | lazy => 1 | ||||||||||||||
| 68 | ); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | |||||||||||||||
| 71 | ####################################################################### | ||||||||||||||
| 72 | ######################## Private attributes ####################### | ||||||||||||||
| 73 | ####################################################################### | ||||||||||||||
| 74 | 1 | has '_filehandle' => ( | |||||||||||||
| 75 | is => 'ro', | ||||||||||||||
| 76 | builder => '_open_filehandle', | ||||||||||||||
| 77 | init_arg => undef, | ||||||||||||||
| 78 | lazy => 1, | ||||||||||||||
| 79 | ); | ||||||||||||||
| 80 | |||||||||||||||
| 81 | |||||||||||||||
| 82 | ####################################################################### | ||||||||||||||
| 83 | ############################## BUILD ############################## | ||||||||||||||
| 84 | ####################################################################### | ||||||||||||||
| 85 | sub BUILD { | ||||||||||||||
| 86 | 20 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | 20 | $self->init_header; | |||||||||||||
| 89 | 20 | $self->init_records_cache; | |||||||||||||
| 90 | 20 | $self->init_records_read_count; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 20 | $self->parse_header_section; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | |||||||||||||||
| 96 | ####################################################################### | ||||||||||||||
| 97 | ######################## Interface Methods ######################## | ||||||||||||||
| 98 | ####################################################################### | ||||||||||||||
| 99 | sub records_read_count { | ||||||||||||||
| 100 | 5 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
records_read_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 101 | 5 | return $self->{RECORDS_READ_COUNT}; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | sub next_record { | ||||||||||||||
| 105 | 54 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 54 | my $record; | |||||||||||||
| 108 | 54 | ▶ | if ($self->record_cache_not_empty) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 9 | $record = $self->next_record_from_cache; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | else { | ||||||||||||||
| 112 | 45 | $record = $self->next_record_from_file; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 54 | ▶ | if (defined $record) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 49 | $self->increment_records_read_count; | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | 54 | return $record; | |||||||||||||
| 119 | } | ||||||||||||||
| 120 | |||||||||||||||
| 121 | |||||||||||||||
| 122 | ####################################################################### | ||||||||||||||
| 123 | ####################### Private Methods ############################ | ||||||||||||||
| 124 | ####################################################################### | ||||||||||||||
| 125 | sub set_eof_reached { | ||||||||||||||
| 126 | 5 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
set_eof_reached: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 127 | 5 | $self->{EOF} = 1; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | sub header { | ||||||||||||||
| 131 | 1 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 132 | 1 | return $self->{HEADER}; | |||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | sub records_cache { | ||||||||||||||
| 136 | 61 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
records_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 137 | 61 | return $self->{RECORDS_CACHE}; | |||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | sub init_header { | ||||||||||||||
| 141 | 20 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
init_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 142 | 20 | $self->{HEADER} = {}; | |||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | sub init_records_cache { | ||||||||||||||
| 146 | 20 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
init_records_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 147 | 20 | $self->{RECORDS_CACHE} = []; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | sub init_records_read_count { | ||||||||||||||
| 151 | 20 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
init_records_read_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 152 | 20 | $self->{RECORDS_READ_COUNT} = 0; | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | |||||||||||||||
| 155 | sub increment_records_read_count { | ||||||||||||||
| 156 | 50 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
increment_records_read_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 157 | 50 | $self->{RECORDS_READ_COUNT}++; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | sub parse_header_section { | ||||||||||||||
| 161 | 20 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
parse_header_section: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||
| 162 | |||||||||||||||
| 163 | 20 | my $filehandle = $self->_filehandle; | |||||||||||||
| 164 | 20 | ▶ | while (my $line = $filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 80 | ▶ | if ($self->line_looks_like_header($line)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 166 | 60 | $self->recognize_and_store_header_line($line); | |||||||||||||
| 167 | } | ||||||||||||||
| 168 | elsif ($self->line_looks_like_record($line)) { | ||||||||||||||
| 169 | # the while loop will read one line after header. Usually, this is the first record and unfortunately in zipped files we cannot go back | ||||||||||||||
| 170 | 20 | my $record = $self->parse_record_line($line); | |||||||||||||
| 171 | 20 | $self->add_to_records_cache($record); | |||||||||||||
| 172 | 20 | return; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | else { | ||||||||||||||
| 175 | 0 | return; | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | } | ||||||||||||||
| 178 | } | ||||||||||||||
| 179 | |||||||||||||||
| 180 | # TODO fix to store "browser" and "track" lines | ||||||||||||||
| 181 | sub recognize_and_store_header_line { | ||||||||||||||
| 182 | 60 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
recognize_and_store_header_line: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 183 | # if ($self->line_looks_like_version($line)) { | ||||||||||||||
| 184 | # $self->parse_and_store_version_line($line); | ||||||||||||||
| 185 | # } | ||||||||||||||
| 186 | # else { | ||||||||||||||
| 187 | # $self->parse_and_store_header_line($line); | ||||||||||||||
| 188 | # } | ||||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | sub add_to_records_cache { | ||||||||||||||
| 192 | 20 | ▶ | my ($self, $record) = @_; | ||||||||||||
|
Subroutine
add_to_records_cache: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 193 | 20 | push @{$self->{RECORDS_CACHE}}, $record, | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | sub next_record_from_file { | ||||||||||||||
| 197 | 46 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record_from_file: called CC=4 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 198 | |||||||||||||||
| 199 | 46 | ▶ | while (my $line = $self->_filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 41 | ▶ | if ($self->line_looks_like_record($line)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 201 | 41 | return $self->parse_record_line($line); | |||||||||||||
| 202 | } | ||||||||||||||
| 203 | else { | ||||||||||||||
| 204 | 0 | ▶ | if ($self->line_looks_like_header($line)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | 0 | die "Record was expected but line looks like a header - the header should have been parsed already. $line\n"; | |||||||||||||
| 206 | } | ||||||||||||||
| 207 | else { | ||||||||||||||
| 208 | 0 | warn "Record was expected but line looks different. $line\n"; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | } | ||||||||||||||
| 211 | } | ||||||||||||||
| 212 | |||||||||||||||
| 213 | 5 | $self->set_eof_reached; | |||||||||||||
| 214 | 5 | return undef; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | sub next_record_from_cache { | ||||||||||||||
| 218 | 10 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record_from_cache: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 219 | |||||||||||||||
| 220 | 10 | my $record = shift @{$self->{RECORDS_CACHE}}; | |||||||||||||
| 221 | 10 | ▶ | if (defined $record) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 222 | 10 | return $record; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | else { | ||||||||||||||
| 225 | 0 | return undef; | |||||||||||||
| 226 | } | ||||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | sub parse_record_line { | ||||||||||||||
| 230 | 62 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
parse_record_line: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||
| 231 | |||||||||||||||
| 232 | 62 | chomp $line; | |||||||||||||
| 233 | 62 | my ($chr,$start,$stop_1,$name,$score,$strand,$thick_start,$thick_stop,$rgb,$block_count,$block_sizes,$block_starts) = split(/\t/,$line); | |||||||||||||
| 234 | |||||||||||||||
| 235 | 62 | my $data = { | |||||||||||||
| 236 | rname => $chr, | ||||||||||||||
| 237 | start => $start, | ||||||||||||||
| 238 | stop_1based => $stop_1, | ||||||||||||||
| 239 | name => $name, | ||||||||||||||
| 240 | score => $score, | ||||||||||||||
| 241 | strand_symbol => $strand, | ||||||||||||||
| 242 | }; | ||||||||||||||
| 243 | |||||||||||||||
| 244 | 62 | ▶ | ($data->{copy_number} = $score) if $self->redirect_score_to_copy_number; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 245 | 62 | ▶ | ($data->{thick_start} = $thick_start) if defined $thick_start; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 246 | 62 | ▶ | ($data->{thick_stop_1based} = $thick_stop) if defined $thick_stop; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | 62 | ▶ | ($data->{rgb} = $rgb) if defined $rgb; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 248 | 62 | ▶ | ($data->{block_count} = $block_count) if defined $block_count; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 249 | 62 | ▶ | ($data->{block_sizes} = [split(/,/,$block_sizes)]) if defined $block_sizes; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 250 | 62 | ▶ | ($data->{block_starts} = [split(/,/,$block_starts)]) if defined $block_starts; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 251 | |||||||||||||||
| 252 | 62 | return GenOO::Data::File::BED::Record->new($data); | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | |||||||||||||||
| 255 | sub line_looks_like_comment { | ||||||||||||||
| 256 | 1 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
line_looks_like_comment: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||
| 257 | 1 | ▶ | return ($line =~ /^#/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 258 | } | ||||||||||||||
| 259 | |||||||||||||||
| 260 | sub line_looks_like_header { | ||||||||||||||
| 261 | 83 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
line_looks_like_header: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 262 | 83 | ▶ | return ($line =~ /^(track|browser)/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 263 | } | ||||||||||||||
| 264 | |||||||||||||||
| 265 | sub line_looks_like_record { | ||||||||||||||
| 266 | 65 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
line_looks_like_record: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 267 | 65 | ▶ | return ($line !~ /^(#|track|browser)/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 268 | } | ||||||||||||||
| 269 | |||||||||||||||
| 270 | sub record_cache_not_empty { | ||||||||||||||
| 271 | 56 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
record_cache_not_empty: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 272 | 56 | ▶ | return ($self->record_cache_size > 0) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 273 | } | ||||||||||||||
| 274 | |||||||||||||||
| 275 | sub record_cache_is_empty { | ||||||||||||||
| 276 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
record_cache_is_empty: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 277 | 2 | ▶ | return ($self->record_cache_size == 0) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 278 | } | ||||||||||||||
| 279 | |||||||||||||||
| 280 | sub record_cache_size { | ||||||||||||||
| 281 | 60 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
record_cache_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 282 | 60 | return scalar @{$self->records_cache}; | |||||||||||||
| 283 | } | ||||||||||||||
| 284 | |||||||||||||||
| 285 | sub is_eof_reached { | ||||||||||||||
| 286 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
is_eof_reached: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 287 | 2 | return $self->{EOF}; | |||||||||||||
| 288 | } | ||||||||||||||
| 289 | |||||||||||||||
| 290 | |||||||||||||||
| 291 | ####################################################################### | ||||||||||||||
| 292 | ######################### Private Methods ######################### | ||||||||||||||
| 293 | ####################################################################### | ||||||||||||||
| 294 | sub _open_filehandle { | ||||||||||||||
| 295 | 20 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_open_filehandle: called CC=4 SCAR=15.5
| |||||||||||||||
| 296 | |||||||||||||||
| 297 | 20 | my $read_mode; | |||||||||||||
| 298 | my $HANDLE; | ||||||||||||||
| 299 | 20 | ▶ | if (!defined $self->file) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 300 | 0 | open ($HANDLE, '<-', $self->file); | |||||||||||||
| 301 | } | ||||||||||||||
| 302 | elsif ($self->file =~ /\.gz$/) { | ||||||||||||||
| 303 | 20 | ▶ | die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 304 | 20 | open($HANDLE, 'gzip -dc ' . $self->file . ' |'); | |||||||||||||
| 305 | } | ||||||||||||||
| 306 | else { | ||||||||||||||
| 307 | 0 | open ($HANDLE, '<', $self->file); | |||||||||||||
| 308 | } | ||||||||||||||
| 309 | |||||||||||||||
| 310 | 20 | return $HANDLE; | |||||||||||||
| 311 | } | ||||||||||||||
| 312 | |||||||||||||||
| 313 | |||||||||||||||
| 314 | ####################################################################### | ||||||||||||||
| 315 | ############################ Finalize ############################# | ||||||||||||||
| 316 | ####################################################################### | ||||||||||||||
| 317 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 318 | |||||||||||||||
| 319 | 1 | 1; | |||||||||||||