| 1 | # POD documentation - main docs before the code | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | GenOO::Data::File::FASTA - Object implementing methods for accessing fasta formatted files (http://genome.ucsc.edu/FAQ/FAQformat#format1) | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Object that manages a fasta file. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | # To initialize | ||||||||||||||||||||||||||||
| 12 | my $fasta_parser = GenOO::Data::File::FASTA->new( | ||||||||||||||||||||||||||||
| 13 | file => undef, | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | This object offers methods to read a fasta file line by line. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 EXAMPLES | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Create object | ||||||||||||||||||||||||||||
| 23 | my $fasta_parser = GenOO::Data::File::FASTA->new( | ||||||||||||||||||||||||||||
| 24 | file => 't/sample_data/sample.fasta.gz' | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Read one record at a time | ||||||||||||||||||||||||||||
| 28 | my $record = $fasta_parser->next_record; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Get the number of records read | ||||||||||||||||||||||||||||
| 31 | my $count = $fasta_parser->records_read_count; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Let the code begin... | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | package GenOO::Data::File::FASTA; | ||||||||||||||||||||||||||||
| 38 | 1 | $GenOO::Data::File::FASTA::VERSION = '1.4.9'; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | ####################################################################### | ||||||||||||||||||||||||||||
| 41 | ####################### Load External modules ##################### | ||||||||||||||||||||||||||||
| 42 | ####################################################################### | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ####################################################################### | ||||||||||||||||||||||||||||
| 50 | ######################### Load GenOO modules ###################### | ||||||||||||||||||||||||||||
| 51 | ####################################################################### | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use GenOO::Data::File::FASTA::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ####################################################################### | ||||||||||||||||||||||||||||
| 56 | ####################### Interface attributes ###################### | ||||||||||||||||||||||||||||
| 57 | ####################################################################### | ||||||||||||||||||||||||||||
| 58 | 1 | has 'file' => ( | |||||||||||||||||||||||||||
| 59 | isa => 'Maybe[Str]', | ||||||||||||||||||||||||||||
| 60 | is => 'rw', | ||||||||||||||||||||||||||||
| 61 | required => 1 | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | has 'records_read_count' => ( | |||||||||||||||||||||||||||
| 65 | is => 'rw', | ||||||||||||||||||||||||||||
| 66 | default => 0, | ||||||||||||||||||||||||||||
| 67 | init_arg => undef, | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | has '_filehandle' => ( | |||||||||||||||||||||||||||
| 71 | is => 'ro', | ||||||||||||||||||||||||||||
| 72 | builder => '_open_filehandle', | ||||||||||||||||||||||||||||
| 73 | init_arg => undef, | ||||||||||||||||||||||||||||
| 74 | lazy => 1, | ||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 1 | has '_stored_record_header' => ( | |||||||||||||||||||||||||||
| 78 | is => 'rw', | ||||||||||||||||||||||||||||
| 79 | clearer => '_clear_stored_record_header', | ||||||||||||||||||||||||||||
| 80 | predicate => '_has_stored_record_header', | ||||||||||||||||||||||||||||
| 81 | init_arg => undef, | ||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | has '_stored_record_sequence_parts' => ( | ||||||||||||||||||||||||||||
| 85 | traits => ['Array'], | ||||||||||||||||||||||||||||
| 86 | is => 'rw', | ||||||||||||||||||||||||||||
| 87 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 88 | 7 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | 1 | handles => { | |||||||||||||||||||||||||||
| 90 | _all_record_sequence_parts => 'elements', | ||||||||||||||||||||||||||||
| 91 | _add_record_sequence_part => 'push', | ||||||||||||||||||||||||||||
| 92 | _join_record_sequence_parts => 'join', | ||||||||||||||||||||||||||||
| 93 | _clear_record_sequence_parts => 'clear', | ||||||||||||||||||||||||||||
| 94 | }, | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | has '_eof' => ( | |||||||||||||||||||||||||||
| 98 | is => 'rw', | ||||||||||||||||||||||||||||
| 99 | predicate => '_reached_eof', | ||||||||||||||||||||||||||||
| 100 | init_arg => undef | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | ####################################################################### | ||||||||||||||||||||||||||||
| 105 | ######################## Interface Methods ######################## | ||||||||||||||||||||||||||||
| 106 | ####################################################################### | ||||||||||||||||||||||||||||
| 107 | sub next_record { | ||||||||||||||||||||||||||||
| 108 | 24 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_record: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 24 | ▶ | return undef if ($self->_reached_eof); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 22 | ▶ | while (my $line = $self->_filehandle->getline) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 78 | chomp $line; | |||||||||||||||||||||||||||
| 114 | 78 | ▶ | if (_line_looks_like_record_header($line)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 115 | 23 | ▶ | if ($self->_has_stored_record_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 20 | my $record = $self->_create_record; | |||||||||||||||||||||||||||
| 117 | 20 | $self->_stored_record_header($line); | |||||||||||||||||||||||||||
| 118 | 20 | return $record; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||||||||
| 121 | 3 | $self->_stored_record_header($line); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | elsif (_line_looks_like_sequence($line)) { | ||||||||||||||||||||||||||||
| 125 | 44 | $self->_add_record_sequence_part($line); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 2 | $self->_eof(1); | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | if ($self->_has_stored_record_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 2 | return $self->_create_record; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | return undef; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ####################################################################### | ||||||||||||||||||||||||||||
| 136 | ######################### Private methods ########################## | ||||||||||||||||||||||||||||
| 137 | ####################################################################### | ||||||||||||||||||||||||||||
| 138 | sub _open_filehandle { | ||||||||||||||||||||||||||||
| 139 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_open_filehandle: called CC=4 SCAR=15.5
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 3 | my $read_mode; | |||||||||||||||||||||||||||
| 142 | my $HANDLE; | ||||||||||||||||||||||||||||
| 143 | 3 | ▶ | if (!defined $self->file) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 144 | 0 | open ($HANDLE, '<-', $self->file); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | elsif ($self->file =~ /\.gz$/) { | ||||||||||||||||||||||||||||
| 147 | 3 | ▶ | die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 3 | open($HANDLE, 'gzip -dc ' . $self->file . ' |'); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 0 | open ($HANDLE, '<', $self->file); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 3 | return $HANDLE; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub _increment_records_read_count { | ||||||||||||||||||||||||||||
| 158 | 22 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_increment_records_read_count: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 22 | $self->records_read_count($self->records_read_count+1); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | sub _create_record { | ||||||||||||||||||||||||||||
| 163 | 22 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_create_record: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 22 | my $record = GenOO::Data::File::FASTA::Record->new( | |||||||||||||||||||||||||||
| 166 | header => $self->_stored_record_header, | ||||||||||||||||||||||||||||
| 167 | sequence => $self->_join_record_sequence_parts(''), | ||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | 22 | $self->_clear_stored_record_header; | |||||||||||||||||||||||||||
| 170 | 22 | $self->_clear_record_sequence_parts; | |||||||||||||||||||||||||||
| 171 | 22 | $self->_increment_records_read_count; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 22 | return $record; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | ####################################################################### | ||||||||||||||||||||||||||||
| 178 | ####################### Private subroutines ######################## | ||||||||||||||||||||||||||||
| 179 | ####################################################################### | ||||||||||||||||||||||||||||
| 180 | sub _line_looks_like_record_header { | ||||||||||||||||||||||||||||
| 181 | 133 | ▶ | return ($_[0] =~ /^>/) ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
_line_looks_like_record_header: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub _line_looks_like_sequence { | ||||||||||||||||||||||||||||
| 185 | 55 | ▶ | return ((!_line_looks_like_record_header($_[0])) and ($_[0] =~ /\S/)) ? 1 : 0; | ||||||||||||||||||||||||||
|
Subroutine
_line_looks_like_sequence: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
not _line_looks_like_record_header($_[0]) and $_[0] =~ /\S/u
MC/DC50%1/2
not _line_looks_like_record_header($_[0]) and $_[0] =~ /\S/u
not _line_looks_like_record_header($_[0])
$_[0] =~ /\S/u
Truth table66%2/3
not(A) and B
A= not _line_looks_like_record_header($_[0])B= $_[0] =~ /\S/u
| |||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | ####################################################################### | ||||||||||||||||||||||||||||
| 190 | ############################ Finalize ############################# | ||||||||||||||||||||||||||||
| 191 | ####################################################################### | ||||||||||||||||||||||||||||
| 192 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 1 | 1; | |||||||||||||||||||||||||||