| 1 | # POD documentation - main docs before the code | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | GenOO::Data::File::GFF - Object implementing methods for accessing GFF formatted files (http://www.sanger.ac.uk/resources/software/gff/spec.html) | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # Object that manages a gff file. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # To initialize | ||||||||||||||
| 12 | my $gff_file = GenOO::Data::File::GFF->new( | ||||||||||||||
| 13 | file => undef, | ||||||||||||||
| 14 | ); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||
| 18 | |||||||||||||||
| 19 | This object offers methods to read a gff file line by line. | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 EXAMPLES | ||||||||||||||
| 22 | |||||||||||||||
| 23 | # Create object | ||||||||||||||
| 24 | my $gff_file = GenOO::Data::File::GFF->new( | ||||||||||||||
| 25 | file => 't/sample_data/sample.gff.gz' | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | # Read one record at a time | ||||||||||||||
| 29 | my $record = $gff_file->next_record(); | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =cut | ||||||||||||||
| 32 | |||||||||||||||
| 33 | # Let the code begin... | ||||||||||||||
| 34 | |||||||||||||||
| 35 | |||||||||||||||
| 36 | package GenOO::Data::File::GFF; | ||||||||||||||
| 37 | 1 | $GenOO::Data::File::GFF::VERSION = '1.5.0'; | |||||||||||||
| 38 | |||||||||||||||
| 39 | ####################################################################### | ||||||||||||||
| 40 | ####################### Load External modules ##################### | ||||||||||||||
| 41 | ####################################################################### | ||||||||||||||
| 42 | 1 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 1 | ▶ | use autodie; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 46 | |||||||||||||||
| 47 | |||||||||||||||
| 48 | ####################################################################### | ||||||||||||||
| 49 | ######################### Load GenOO modules ###################### | ||||||||||||||
| 50 | ####################################################################### | ||||||||||||||
| 51 | 1 | ▶ | use GenOO::Data::File::GFF::Record; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 52 | |||||||||||||||
| 53 | |||||||||||||||
| 54 | ####################################################################### | ||||||||||||||
| 55 | ####################### Interface attributes ###################### | ||||||||||||||
| 56 | ####################################################################### | ||||||||||||||
| 57 | 1 | has 'file' => ( | |||||||||||||
| 58 | isa => 'Maybe[Str]', | ||||||||||||||
| 59 | is => 'rw', | ||||||||||||||
| 60 | required => 1 | ||||||||||||||
| 61 | ); | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 1 | has 'records_read_count' => ( | |||||||||||||
| 64 | traits => ['Counter'], | ||||||||||||||
| 65 | is => 'ro', | ||||||||||||||
| 66 | isa => 'Num', | ||||||||||||||
| 67 | default => 0, | ||||||||||||||
| 68 | handles => { | ||||||||||||||
| 69 | _inc_records_read_count => 'inc', | ||||||||||||||
| 70 | _reset_records_read_count => 'reset', | ||||||||||||||
| 71 | }, | ||||||||||||||
| 72 | ); | ||||||||||||||
| 73 | |||||||||||||||
| 74 | |||||||||||||||
| 75 | ####################################################################### | ||||||||||||||
| 76 | ######################## Private attributes ####################### | ||||||||||||||
| 77 | ####################################################################### | ||||||||||||||
| 78 | 1 | has '_filehandle' => ( | |||||||||||||
| 79 | is => 'ro', | ||||||||||||||
| 80 | builder => '_open_filehandle', | ||||||||||||||
| 81 | init_arg => undef, | ||||||||||||||
| 82 | lazy => 1, | ||||||||||||||
| 83 | ); | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 1 | has '_eof_reached' => ( | |||||||||||||
| 86 | is => 'rw', | ||||||||||||||
| 87 | default => 0, | ||||||||||||||
| 88 | init_arg => undef, | ||||||||||||||
| 89 | lazy => 1, | ||||||||||||||
| 90 | ); | ||||||||||||||
| 91 | |||||||||||||||
| 92 | has '_header' => ( | ||||||||||||||
| 93 | is => 'ro', | ||||||||||||||
| 94 | 16 | ▶ | default => sub {{}}, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 95 | 1 | init_arg => undef, | |||||||||||||
| 96 | lazy => 1, | ||||||||||||||
| 97 | ); | ||||||||||||||
| 98 | |||||||||||||||
| 99 | has '_cached_records' => ( | ||||||||||||||
| 100 | traits => ['Array'], | ||||||||||||||
| 101 | is => 'ro', | ||||||||||||||
| 102 | isa => 'ArrayRef[GenOO::Data::File::GFF::Record]', | ||||||||||||||
| 103 | 31 | ▶ | default => sub { [] }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 104 | 1 | handles => { | |||||||||||||
| 105 | _all_cached_records => 'elements', | ||||||||||||||
| 106 | _add_record_in_cache => 'push', | ||||||||||||||
| 107 | _shift_cached_record => 'shift', | ||||||||||||||
| 108 | _has_cached_records => 'count', | ||||||||||||||
| 109 | _has_no_cached_records => 'is_empty', | ||||||||||||||
| 110 | }, | ||||||||||||||
| 111 | ); | ||||||||||||||
| 112 | |||||||||||||||
| 113 | |||||||||||||||
| 114 | ####################################################################### | ||||||||||||||
| 115 | ############################### BUILD ############################# | ||||||||||||||
| 116 | ####################################################################### | ||||||||||||||
| 117 | sub BUILD { | ||||||||||||||
| 118 | 31 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 119 | |||||||||||||||
| 120 | 31 | $self->_parse_header_section; | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | |||||||||||||||
| 123 | ####################################################################### | ||||||||||||||
| 124 | ######################## Interface Methods ######################## | ||||||||||||||
| 125 | ####################################################################### | ||||||||||||||
| 126 | sub next_record { | ||||||||||||||
| 127 | 10950 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | 10950 | my $record; | |||||||||||||
| 130 | 10950 | ▶ | if ($self->_has_cached_records) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | 20 | $record = $self->_shift_cached_record; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | else { | ||||||||||||||
| 134 | 10930 | $record = $self->_next_record_from_file; | |||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 10950 | ▶ | if (defined $record) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | 10933 | $self->_inc_records_read_count; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | 10950 | return $record; | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | sub version { | ||||||||||||||
| 144 | 1 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 145 | 1 | return $self->_header->{VERSION}; | |||||||||||||
| 146 | } | ||||||||||||||
| 147 | |||||||||||||||
| 148 | |||||||||||||||
| 149 | ####################################################################### | ||||||||||||||
| 150 | ######################### Private Methods ######################### | ||||||||||||||
| 151 | ####################################################################### | ||||||||||||||
| 152 | sub _parse_header_section { | ||||||||||||||
| 153 | 31 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_parse_header_section: called CC=4 SCAR=13.9
| |||||||||||||||
| 154 | |||||||||||||||
| 155 | 31 | my $filehandle = $self->_filehandle; | |||||||||||||
| 156 | 31 | ▶ | while (my $line = $filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 143 | ▶ | if ($self->_line_looks_like_header($line)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 158 | 32 | $self->_recognize_and_store_header_line($line); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | elsif ($self->_line_looks_like_record($line)) { | ||||||||||||||
| 161 | # When the while reads the first line after the header section | ||||||||||||||
| 162 | # we need to process it immediatelly because in zipped files we cannot go back | ||||||||||||||
| 163 | 31 | my $record = $self->_parse_record_line($line); | |||||||||||||
| 164 | 31 | $self->_add_record_in_cache($record); | |||||||||||||
| 165 | 31 | return; | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | } | ||||||||||||||
| 168 | } | ||||||||||||||
| 169 | |||||||||||||||
| 170 | sub _next_record_from_file { | ||||||||||||||
| 171 | 10931 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_next_record_from_file: called CC=4 SCAR=15.0
| |||||||||||||||
| 172 | |||||||||||||||
| 173 | 10931 | ▶ | while (my $line = $self->_filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 10914 | ▶ | if ($self->_line_looks_like_record($line)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 175 | 10914 | return $self->_parse_record_line($line); | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | elsif ($self->_line_looks_like_header) { | ||||||||||||||
| 178 | 0 | die "A record was expected but line looks like a header - the header should have been parsed already. $line\n"; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | } | ||||||||||||||
| 181 | |||||||||||||||
| 182 | 17 | $self->_eof_reached(1); # When you reach this point the file has finished | |||||||||||||
| 183 | 17 | return undef; | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | sub _parse_record_line { | ||||||||||||||
| 187 | 10946 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_parse_record_line: called CC=2 SCAR=6.9
| |||||||||||||||
| 188 | |||||||||||||||
| 189 | 10946 | chomp $line; | |||||||||||||
| 190 | 10946 | $line =~ s/(#.+)$//; | |||||||||||||
| 191 | 10946 | my $comment_string = $1; | |||||||||||||
| 192 | 10946 | my ($seqname, $source, $feature, $start, $end, $score, $strand, $frame, $attributes_string) = split(/\t/,$line); | |||||||||||||
| 193 | 10946 | my @attributes = split(/;\s*/,$attributes_string); | |||||||||||||
| 194 | 10946 | my %attributes_hash; | |||||||||||||
| 195 | 10946 | foreach my $attribute (@attributes) { | |||||||||||||
| 196 | 21892 | $attribute =~ /(.+)[=|\s]"(.+)"/; | |||||||||||||
| 197 | 21892 | $attributes_hash{$1} = $2; | |||||||||||||
| 198 | } | ||||||||||||||
| 199 | |||||||||||||||
| 200 | 10946 | return GenOO::Data::File::GFF::Record->new({ | |||||||||||||
| 201 | seqname => $seqname, | ||||||||||||||
| 202 | source => $source, | ||||||||||||||
| 203 | feature => $feature, | ||||||||||||||
| 204 | start_1_based => $start, # 1-based | ||||||||||||||
| 205 | stop_1_based => $end, # 1-based | ||||||||||||||
| 206 | score => $score, | ||||||||||||||
| 207 | strand => $strand, | ||||||||||||||
| 208 | frame => $frame, | ||||||||||||||
| 209 | attributes => \%attributes_hash, | ||||||||||||||
| 210 | comment => $comment_string, | ||||||||||||||
| 211 | }); | ||||||||||||||
| 212 | } | ||||||||||||||
| 213 | |||||||||||||||
| 214 | sub _recognize_and_store_header_line { | ||||||||||||||
| 215 | 32 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_recognize_and_store_header_line: called CC=2 SCAR=6.9
| |||||||||||||||
| 216 | |||||||||||||||
| 217 | 32 | ▶ | if ($self->_line_looks_like_version($line)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | 16 | $self->_parse_line_and_store_version($line); | |||||||||||||
| 219 | } | ||||||||||||||
| 220 | else { | ||||||||||||||
| 221 | 16 | $self->_parse_and_store_generic_header_line($line); | |||||||||||||
| 222 | } | ||||||||||||||
| 223 | } | ||||||||||||||
| 224 | |||||||||||||||
| 225 | sub _parse_line_and_store_version { | ||||||||||||||
| 226 | 16 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_parse_line_and_store_version: called CC=1 SCAR=0.0
| |||||||||||||||
| 227 | |||||||||||||||
| 228 | 16 | my $version = (split(/\s+/,$line))[1]; | |||||||||||||
| 229 | 16 | $self->_header->{VERSION} = $version; | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | |||||||||||||||
| 232 | sub _parse_and_store_generic_header_line { | ||||||||||||||
| 233 | 16 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_parse_and_store_generic_header_line: called CC=1 SCAR=0.0
| |||||||||||||||
| 234 | |||||||||||||||
| 235 | 16 | my ($key, @values) = split(/\s+/,$line); | |||||||||||||
| 236 | 16 | $self->_header->{$key} = join(' ', @values); | |||||||||||||
| 237 | } | ||||||||||||||
| 238 | |||||||||||||||
| 239 | sub _line_looks_like_header { | ||||||||||||||
| 240 | 145 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_line_looks_like_header: called CC=2 SCAR=6.9
| |||||||||||||||
| 241 | 145 | ▶ | return ($line =~ /^#{2}/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 242 | } | ||||||||||||||
| 243 | |||||||||||||||
| 244 | sub _line_looks_like_record { | ||||||||||||||
| 245 | 11027 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_line_looks_like_record: called CC=2 SCAR=6.9
| |||||||||||||||
| 246 | 11027 | ▶ | return ($line !~ /^#/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | } | ||||||||||||||
| 248 | |||||||||||||||
| 249 | sub _line_looks_like_version { | ||||||||||||||
| 250 | 34 | ▶ | my ($self, $line) = @_; | ||||||||||||
|
Subroutine
_line_looks_like_version: called CC=2 SCAR=6.9
| |||||||||||||||
| 251 | 34 | ▶ | return ($line =~ /^##gff-version/) ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 252 | } | ||||||||||||||
| 253 | |||||||||||||||
| 254 | |||||||||||||||
| 255 | ####################################################################### | ||||||||||||||
| 256 | ######################### Private Methods ######################### | ||||||||||||||
| 257 | ####################################################################### | ||||||||||||||
| 258 | sub _open_filehandle { | ||||||||||||||
| 259 | 31 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_open_filehandle: called CC=4 SCAR=15.5
| |||||||||||||||
| 260 | |||||||||||||||
| 261 | 31 | my $read_mode; | |||||||||||||
| 262 | my $HANDLE; | ||||||||||||||
| 263 | 31 | ▶ | if (!defined $self->file) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 264 | 0 | open ($HANDLE, '<-', $self->file); | |||||||||||||
| 265 | } | ||||||||||||||
| 266 | elsif ($self->file =~ /\.gz$/) { | ||||||||||||||
| 267 | 31 | ▶ | die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 268 | 31 | open($HANDLE, 'gzip -dc ' . $self->file . ' |'); | |||||||||||||
| 269 | } | ||||||||||||||
| 270 | else { | ||||||||||||||
| 271 | 0 | open ($HANDLE, '<', $self->file); | |||||||||||||
| 272 | } | ||||||||||||||
| 273 | |||||||||||||||
| 274 | 31 | return $HANDLE; | |||||||||||||
| 275 | } | ||||||||||||||
| 276 | |||||||||||||||
| 277 | |||||||||||||||
| 278 | ####################################################################### | ||||||||||||||
| 279 | ############################ Finalize ############################# | ||||||||||||||
| 280 | ####################################################################### | ||||||||||||||
| 281 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 282 | |||||||||||||||
| 283 | 1 | 1; | |||||||||||||