| 1 | # POD documentation - main docs before the code | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | GenOO::Data::File::SAM - Object implementing methods for accessing SAM formatted files | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # Object that manages a sam file. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # To initialize | ||||||||||||||
| 12 | my $sam_file = GenOO::Data::File::SAM->new( | ||||||||||||||
| 13 | file => undef, | ||||||||||||||
| 14 | ); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||
| 18 | |||||||||||||||
| 19 | This object implements methods to read a sam file line by line. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 EXAMPLES | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # Create object | ||||||||||||||
| 24 | my $sam_file = GenOO::Data::File::SAM->new( | ||||||||||||||
| 25 | file => 't/sample_data/sample.sam.gz' | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | # Read one record at a time | ||||||||||||||
| 29 | my $sam_record = $sam_file->next_record(); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =cut | ||||||||||||||
| 32 | |||||||||||||||
| 33 | # Let the code begin... | ||||||||||||||
| 34 | |||||||||||||||
| 35 | package GenOO::Data::File::SAM; | ||||||||||||||
| 36 | 1 | $GenOO::Data::File::SAM::VERSION = '1.5.0'; | |||||||||||||
| 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 | ####################### Interface attributes ###################### | ||||||||||||||
| 49 | ####################################################################### | ||||||||||||||
| 50 | 1 | has 'file' => ( | |||||||||||||
| 51 | isa => 'Maybe[Str]', # undef value makes the parser read from STDIN | ||||||||||||||
| 52 | is => 'ro', | ||||||||||||||
| 53 | required => 1, | ||||||||||||||
| 54 | ); | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 1 | has 'records_read_count' => ( | |||||||||||||
| 57 | traits => ['Counter'], | ||||||||||||||
| 58 | is => 'ro', | ||||||||||||||
| 59 | isa => 'Num', | ||||||||||||||
| 60 | default => 0, | ||||||||||||||
| 61 | handles => { | ||||||||||||||
| 62 | _inc_records_read_count => 'inc', | ||||||||||||||
| 63 | _reset_records_read_count => 'reset', | ||||||||||||||
| 64 | }, | ||||||||||||||
| 65 | ); | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 1 | has 'records_class' => ( | |||||||||||||
| 68 | is => 'ro', | ||||||||||||||
| 69 | default => 'GenOO::Data::File::SAM::Record', | ||||||||||||||
| 70 | ); | ||||||||||||||
| 71 | |||||||||||||||
| 72 | ####################################################################### | ||||||||||||||
| 73 | ######################## Private attributes ####################### | ||||||||||||||
| 74 | ####################################################################### | ||||||||||||||
| 75 | 1 | has '_filehandle' => ( | |||||||||||||
| 76 | is => 'ro', | ||||||||||||||
| 77 | builder => '_open_filehandle', | ||||||||||||||
| 78 | init_arg => undef, | ||||||||||||||
| 79 | lazy => 1, | ||||||||||||||
| 80 | ); | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | has '_is_eof_reached' => ( | |||||||||||||
| 83 | traits => ['Bool'], | ||||||||||||||
| 84 | is => 'rw', | ||||||||||||||
| 85 | isa => 'Bool', | ||||||||||||||
| 86 | default => 0, | ||||||||||||||
| 87 | handles => { | ||||||||||||||
| 88 | _set_eof_reached => 'set', | ||||||||||||||
| 89 | _unset_eof_reached => 'unset', | ||||||||||||||
| 90 | _eof_not_reached => 'not', | ||||||||||||||
| 91 | }, | ||||||||||||||
| 92 | ); | ||||||||||||||
| 93 | |||||||||||||||
| 94 | has '_cached_header_lines' => ( | ||||||||||||||
| 95 | traits => ['Array'], | ||||||||||||||
| 96 | is => 'ro', | ||||||||||||||
| 97 | 11 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 98 | 1 | handles => { | |||||||||||||
| 99 | _all_cached_header_lines => 'elements', | ||||||||||||||
| 100 | _add_header_line_in_cache => 'push', | ||||||||||||||
| 101 | _shift_cached_header_line => 'shift', | ||||||||||||||
| 102 | _has_cached_header_lines => 'count', | ||||||||||||||
| 103 | _has_no_cached_header_lines => 'is_empty', | ||||||||||||||
| 104 | _cached_header_lines_count => 'count', | ||||||||||||||
| 105 | _clear_cached_header_lines => 'clear' | ||||||||||||||
| 106 | }, | ||||||||||||||
| 107 | ); | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 1 | has '_cached_record' => ( | |||||||||||||
| 110 | is => 'rw', | ||||||||||||||
| 111 | clearer => '_clear_cached_record', | ||||||||||||||
| 112 | predicate => '_has_cached_record', | ||||||||||||||
| 113 | ); | ||||||||||||||
| 114 | |||||||||||||||
| 115 | |||||||||||||||
| 116 | ####################################################################### | ||||||||||||||
| 117 | ############################## BUILD ############################## | ||||||||||||||
| 118 | ####################################################################### | ||||||||||||||
| 119 | sub BUILD { | ||||||||||||||
| 120 | 11 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 121 | |||||||||||||||
| 122 | 11 | eval "require ".$self->records_class; | |||||||||||||
| 123 | 11 | $self->_parse_header_section; | |||||||||||||
| 124 | } | ||||||||||||||
| 125 | |||||||||||||||
| 126 | |||||||||||||||
| 127 | ####################################################################### | ||||||||||||||
| 128 | ######################## Interface Methods ######################## | ||||||||||||||
| 129 | ####################################################################### | ||||||||||||||
| 130 | sub next_record { | ||||||||||||||
| 131 | 2939 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 132 | |||||||||||||||
| 133 | 2939 | my $record; | |||||||||||||
| 134 | 2939 | ▶ | if ($self->_has_cached_record) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | 5 | $record = $self->_cached_record; | |||||||||||||
| 136 | 5 | $self->_clear_cached_record; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | else { | ||||||||||||||
| 139 | 2934 | $record = $self->_next_record_from_file; | |||||||||||||
| 140 | } | ||||||||||||||
| 141 | |||||||||||||||
| 142 | 2939 | ▶ | $self->_inc_records_read_count if defined $record; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | |||||||||||||||
| 144 | 2939 | return $record; | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | sub header { | ||||||||||||||
| 148 | 0 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 149 | |||||||||||||||
| 150 | 0 | return join("\n", $self->_all_cached_header_lines); | |||||||||||||
| 151 | } | ||||||||||||||
| 152 | |||||||||||||||
| 153 | |||||||||||||||
| 154 | ####################################################################### | ||||||||||||||
| 155 | ######################### Private Methods ######################### | ||||||||||||||
| 156 | ####################################################################### | ||||||||||||||
| 157 | sub _parse_header_section { | ||||||||||||||
| 158 | 11 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_parse_header_section: called CC=3 SCAR=11.0
| |||||||||||||||
| 159 | |||||||||||||||
| 160 | 11 | ▶ | while (my $line = $self->_filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | 253 | ▶ | if ($line =~ /^\@/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 242 | chomp($line); | |||||||||||||
| 163 | 242 | $self->_add_header_line_in_cache($line); | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | else { | ||||||||||||||
| 166 | # When the while reads the first line after the header section | ||||||||||||||
| 167 | # we need to process it immediatelly because in zipped files we cannot go back | ||||||||||||||
| 168 | 11 | my $record = $self->_parse_record_line($line); | |||||||||||||
| 169 | 11 | $self->_cached_record($record); | |||||||||||||
| 170 | 11 | return; | |||||||||||||
| 171 | } | ||||||||||||||
| 172 | } | ||||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | sub _next_record_from_file { | ||||||||||||||
| 176 | 2935 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_next_record_from_file: called CC=2 SCAR=6.9
| |||||||||||||||
| 177 | |||||||||||||||
| 178 | 2935 | my $line = $self->_filehandle->getline; | |||||||||||||
| 179 | 2935 | ▶ | if (defined $line) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 2932 | return $self->_parse_record_line($line); | |||||||||||||
| 181 | } | ||||||||||||||
| 182 | else { | ||||||||||||||
| 183 | 3 | $self->_set_eof_reached; | |||||||||||||
| 184 | 3 | return undef; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | } | ||||||||||||||
| 187 | |||||||||||||||
| 188 | sub _parse_record_line { | ||||||||||||||
| 189 | 2944 | ▶ | my ($self,$line) = @_; | ||||||||||||
|
Subroutine
_parse_record_line: called CC=1 SCAR=0.0
| |||||||||||||||
| 190 | |||||||||||||||
| 191 | 2944 | chomp $line; | |||||||||||||
| 192 | 2944 | my @fields = split(/\t/,$line); | |||||||||||||
| 193 | 2944 | return $self->records_class->new(fields => \@fields); | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | sub _open_filehandle { | ||||||||||||||
| 197 | 11 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_open_filehandle: called CC=4 SCAR=15.5
| |||||||||||||||
| 198 | |||||||||||||||
| 199 | 11 | my $read_mode; | |||||||||||||
| 200 | my $HANDLE; | ||||||||||||||
| 201 | 11 | ▶ | if (!defined $self->file) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 202 | 0 | open ($HANDLE, '<-', $self->file); | |||||||||||||
| 203 | } | ||||||||||||||
| 204 | elsif ($self->file =~ /\.gz$/) { | ||||||||||||||
| 205 | 11 | ▶ | die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 206 | 11 | open($HANDLE, 'gzip -dc ' . $self->file . ' |'); | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | else { | ||||||||||||||
| 209 | 0 | open ($HANDLE, '<', $self->file); | |||||||||||||
| 210 | } | ||||||||||||||
| 211 | |||||||||||||||
| 212 | 11 | return $HANDLE; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | |||||||||||||||
| 215 | |||||||||||||||
| 216 | ####################################################################### | ||||||||||||||
| 217 | ############################ Finalize ############################# | ||||||||||||||
| 218 | ####################################################################### | ||||||||||||||
| 219 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 220 | |||||||||||||||
| 221 | 1 | 1; | |||||||||||||