| 1 | # POD documentation - main docs before the code | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | GenOO::Data::File::FASTQ - Object implementing methods for accessing fastq formatted files | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | # Object that manages a fastq file. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # To initialize | ||||||||||||||
| 12 | my $file = GenOO::Data::File::FASTQ->new( | ||||||||||||||
| 13 | file => undef, | ||||||||||||||
| 14 | ); | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||
| 17 | |||||||||||||||
| 18 | This object offers methods to read a fastq file entry by entry | ||||||||||||||
| 19 | |||||||||||||||
| 20 | =head1 EXAMPLES | ||||||||||||||
| 21 | |||||||||||||||
| 22 | # Read one entry | ||||||||||||||
| 23 | my $entry = $fastq_parser->next_record(); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | =cut | ||||||||||||||
| 26 | |||||||||||||||
| 27 | # Let the code begin... | ||||||||||||||
| 28 | |||||||||||||||
| 29 | package GenOO::Data::File::FASTQ; | ||||||||||||||
| 30 | 1 | $GenOO::Data::File::FASTQ::VERSION = '1.5.0'; | |||||||||||||
| 31 | |||||||||||||||
| 32 | ####################################################################### | ||||||||||||||
| 33 | ####################### Load External modules ##################### | ||||||||||||||
| 34 | ####################################################################### | ||||||||||||||
| 35 | 1 | ▶ | use Modern::Perl; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 36 | 1 | ▶ | use autodie; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 1 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 38 | 1 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | ####################################################################### | ||||||||||||||
| 42 | ######################### Load GenOO modules ###################### | ||||||||||||||
| 43 | ####################################################################### | ||||||||||||||
| 44 | 1 | ▶ | use GenOO::Data::File::FASTQ::Record; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | |||||||||||||||
| 47 | ####################################################################### | ||||||||||||||
| 48 | ####################### Interface attributes ###################### | ||||||||||||||
| 49 | ####################################################################### | ||||||||||||||
| 50 | 1 | has 'file' => ( | |||||||||||||
| 51 | isa => 'Maybe[Str]', | ||||||||||||||
| 52 | is => 'rw', | ||||||||||||||
| 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 | |||||||||||||||
| 68 | ####################################################################### | ||||||||||||||
| 69 | ######################## Private attributes ####################### | ||||||||||||||
| 70 | ####################################################################### | ||||||||||||||
| 71 | 1 | has '_filehandle' => ( | |||||||||||||
| 72 | is => 'ro', | ||||||||||||||
| 73 | builder => '_open_filehandle', | ||||||||||||||
| 74 | init_arg => undef, | ||||||||||||||
| 75 | lazy => 1, | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | |||||||||||||||
| 78 | |||||||||||||||
| 79 | ####################################################################### | ||||||||||||||
| 80 | ######################## Interface Methods ######################## | ||||||||||||||
| 81 | ####################################################################### | ||||||||||||||
| 82 | sub next_record { | ||||||||||||||
| 83 | 43 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
next_record: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 84 | |||||||||||||||
| 85 | 43 | my $filehandle = $self->_filehandle; | |||||||||||||
| 86 | 43 | ▶ | while (my $line = $filehandle->getline) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 42 | ▶ | if ($line =~ /^\@/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 42 | my $id = substr($line,1); | |||||||||||||
| 89 | 42 | chomp($id); | |||||||||||||
| 90 | 42 | $id =~ s/\r//g; | |||||||||||||
| 91 | 42 | my $seq = $filehandle->getline; | |||||||||||||
| 92 | 42 | chomp($seq); | |||||||||||||
| 93 | 42 | $seq =~ s/\r//g; | |||||||||||||
| 94 | 42 | $filehandle->getline; #unused line | |||||||||||||
| 95 | 42 | my $quality = $filehandle->getline; | |||||||||||||
| 96 | 42 | chomp($quality); | |||||||||||||
| 97 | 42 | $quality =~ s/\r//g; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 42 | $self->_inc_records_read_count; | |||||||||||||
| 100 | 42 | return $self->_create_record($id, $seq, $quality); | |||||||||||||
| 101 | } | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | 1 | return undef; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | ####################################################################### | ||||||||||||||
| 107 | ####################### Private Methods ############################ | ||||||||||||||
| 108 | ####################################################################### | ||||||||||||||
| 109 | sub _open_filehandle { | ||||||||||||||
| 110 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
_open_filehandle: called CC=4 SCAR=15.5
| |||||||||||||||
| 111 | |||||||||||||||
| 112 | 2 | my $read_mode; | |||||||||||||
| 113 | my $HANDLE; | ||||||||||||||
| 114 | 2 | ▶ | if (!defined $self->file) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 115 | 0 | open ($HANDLE, '<-', $self->file); | |||||||||||||
| 116 | } | ||||||||||||||
| 117 | elsif ($self->file =~ /\.gz$/) { | ||||||||||||||
| 118 | 2 | ▶ | die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 2 | open($HANDLE, 'gzip -dc ' . $self->file . ' |'); | |||||||||||||
| 120 | } | ||||||||||||||
| 121 | else { | ||||||||||||||
| 122 | 0 | open ($HANDLE, '<', $self->file); | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 2 | return $HANDLE; | |||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | sub _create_record { | ||||||||||||||
| 129 | 42 | ▶ | my ($self, $id, $seq, $quality) = @_; | ||||||||||||
|
Subroutine
_create_record: called CC=1 SCAR=0.0
| |||||||||||||||
| 130 | |||||||||||||||
| 131 | 42 | return GenOO::Data::File::FASTQ::Record->new( | |||||||||||||
| 132 | name => $id, | ||||||||||||||
| 133 | sequence => $seq, | ||||||||||||||
| 134 | quality => $quality, | ||||||||||||||
| 135 | ); | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | ####################################################################### | ||||||||||||||
| 139 | ############################ Finalize ############################# | ||||||||||||||
| 140 | ####################################################################### | ||||||||||||||
| 141 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 142 | |||||||||||||||
| 143 | 1 | 1; | |||||||||||||