GenOO/Data/File/FASTA.pm

filter: Statementstmt 94.8% 55 / 58 Branchbran 81.8% 18 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 89.8% 88 / 98 CC 13 SCAR 13.1
CC
13
Cov
90%
next_record
7.0
_open_filehandle
4.7
_line_looks_like_sequence
3.0
CRAP 3.7
« GenOO/Data/File/GFF/Record.pm ↑ Summary GenOO/Data/Structure/DoubleHashArray.pm »
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
BranchTrueFalseTotal
if $self->_reached_eof 2 22 24
111  
112 22 ▶ while (my $line = $self->_filehandle->getline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->_filehandle->getline) 78 2 80
113 78 chomp $line;
114 78 ▶ if (_line_looks_like_record_header($line)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_line_looks_like_record_header($line)) { } 23 55 78
elsif (_line_looks_like_sequence($line)) { } 44 11 55
115 23 ▶ if ($self->_has_stored_record_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_stored_record_header) { } 20 3 23
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
BranchTrueFalseTotal
if ($self->_has_stored_record_header) 2 0 2
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
BranchTrueFalseTotal
if (not defined $self->file) { } 0 3 3
elsif ($self->file =~ /\.gz$/u) { } 3 0 3
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
BranchTrueFalseTotal
unless -e $self->file 0 3 3
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
BranchTrueFalseTotal
$_[0] =~ /^>/u ? : 23 110 133
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
BranchTrueFalseTotal
!_line_looks_like_record_header($_[0]) && $_[0] =~ /\S/u ? : 44 11 55
Condition66%2/3
not _line_looks_like_record_header($_[0]) and $_[0] =~ /\S/u
!ll&&!rl&&r
01144
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
ABresult
0X0
100
111
186 }
187  
188  
189 #######################################################################
190 ############################ Finalize #############################
191 #######################################################################
192 1 __PACKAGE__->meta->make_immutable;
193  
194 1 1;
« GenOO/Data/File/GFF/Record.pm ↑ Summary GenOO/Data/Structure/DoubleHashArray.pm »