line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
$GenOO::Data::File::FASTA::VERSION = '1.5.2'; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
####################################################################### |
41
|
|
|
|
|
|
|
####################### Load External modules ##################### |
42
|
|
|
|
|
|
|
####################################################################### |
43
|
1
|
|
|
1
|
|
5516
|
use Modern::Perl; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
32
|
|
44
|
1
|
|
|
1
|
|
343
|
use autodie; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
17
|
|
45
|
1
|
|
|
1
|
|
6562
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
46
|
1
|
|
|
1
|
|
11311
|
use namespace::autoclean; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
20
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
####################################################################### |
50
|
|
|
|
|
|
|
######################### Load GenOO modules ###################### |
51
|
|
|
|
|
|
|
####################################################################### |
52
|
1
|
|
|
1
|
|
179
|
use GenOO::Data::File::FASTA::Record; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1044
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
####################################################################### |
56
|
|
|
|
|
|
|
####################### Interface attributes ###################### |
57
|
|
|
|
|
|
|
####################################################################### |
58
|
|
|
|
|
|
|
has 'file' => ( |
59
|
|
|
|
|
|
|
isa => 'Maybe[Str]', |
60
|
|
|
|
|
|
|
is => 'rw', |
61
|
|
|
|
|
|
|
required => 1 |
62
|
|
|
|
|
|
|
); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
has 'records_read_count' => ( |
65
|
|
|
|
|
|
|
is => 'rw', |
66
|
|
|
|
|
|
|
default => 0, |
67
|
|
|
|
|
|
|
init_arg => undef, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
has '_filehandle' => ( |
71
|
|
|
|
|
|
|
is => 'ro', |
72
|
|
|
|
|
|
|
builder => '_open_filehandle', |
73
|
|
|
|
|
|
|
init_arg => undef, |
74
|
|
|
|
|
|
|
lazy => 1, |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
default => sub { [] }, |
89
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
24
|
0
|
1409
|
my ($self) = @_; |
109
|
|
|
|
|
|
|
|
110
|
24
|
100
|
|
|
|
852
|
return undef if ($self->_reached_eof); |
111
|
|
|
|
|
|
|
|
112
|
22
|
|
|
|
|
653
|
while (my $line = $self->_filehandle->getline) { |
113
|
78
|
|
|
|
|
2179
|
chomp $line; |
114
|
78
|
100
|
|
|
|
186
|
if (_line_looks_like_record_header($line)) { |
|
|
100
|
|
|
|
|
|
115
|
23
|
100
|
|
|
|
889
|
if ($self->_has_stored_record_header) { |
116
|
20
|
|
|
|
|
87
|
my $record = $self->_create_record; |
117
|
20
|
|
|
|
|
612
|
$self->_stored_record_header($line); |
118
|
20
|
|
|
|
|
237
|
return $record; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
3
|
|
|
|
|
138
|
$self->_stored_record_header($line); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif (_line_looks_like_sequence($line)) { |
125
|
44
|
|
|
|
|
1929
|
$self->_add_record_sequence_part($line); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
2
|
|
|
|
|
135
|
$self->_eof(1); |
129
|
2
|
50
|
|
|
|
79
|
if ($self->_has_stored_record_header) { |
130
|
2
|
|
|
|
|
13
|
return $self->_create_record; |
131
|
|
|
|
|
|
|
} |
132
|
0
|
|
|
|
|
0
|
return undef; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
####################################################################### |
136
|
|
|
|
|
|
|
######################### Private methods ########################## |
137
|
|
|
|
|
|
|
####################################################################### |
138
|
|
|
|
|
|
|
sub _open_filehandle { |
139
|
3
|
|
|
3
|
|
8
|
my ($self) = @_; |
140
|
|
|
|
|
|
|
|
141
|
3
|
|
|
|
|
6
|
my $read_mode; |
142
|
|
|
|
|
|
|
my $HANDLE; |
143
|
3
|
50
|
|
|
|
100
|
if (!defined $self->file) { |
|
|
50
|
|
|
|
|
|
144
|
0
|
|
|
|
|
0
|
open ($HANDLE, '<-', $self->file); |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
elsif ($self->file =~ /\.gz$/) { |
147
|
3
|
50
|
|
|
|
75
|
die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; |
148
|
3
|
|
|
|
|
100
|
open($HANDLE, 'gzip -dc ' . $self->file . ' |'); |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
else { |
151
|
0
|
|
|
|
|
0
|
open ($HANDLE, '<', $self->file); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
3
|
|
|
|
|
17320
|
return $HANDLE; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub _increment_records_read_count { |
158
|
22
|
|
|
22
|
|
60
|
my ($self) = @_; |
159
|
22
|
|
|
|
|
722
|
$self->records_read_count($self->records_read_count+1); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
sub _create_record { |
163
|
22
|
|
|
22
|
|
49
|
my ($self) = @_; |
164
|
|
|
|
|
|
|
|
165
|
22
|
|
|
|
|
731
|
my $record = GenOO::Data::File::FASTA::Record->new( |
166
|
|
|
|
|
|
|
header => $self->_stored_record_header, |
167
|
|
|
|
|
|
|
sequence => $self->_join_record_sequence_parts(''), |
168
|
|
|
|
|
|
|
); |
169
|
22
|
|
|
|
|
968
|
$self->_clear_stored_record_header; |
170
|
22
|
|
|
|
|
1080
|
$self->_clear_record_sequence_parts; |
171
|
22
|
|
|
|
|
86
|
$self->_increment_records_read_count; |
172
|
|
|
|
|
|
|
|
173
|
22
|
|
|
|
|
64
|
return $record; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
####################################################################### |
178
|
|
|
|
|
|
|
####################### Private subroutines ######################## |
179
|
|
|
|
|
|
|
####################################################################### |
180
|
|
|
|
|
|
|
sub _line_looks_like_record_header { |
181
|
133
|
100
|
|
133
|
|
1086
|
return ($_[0] =~ /^>/) ? 1 : 0; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub _line_looks_like_sequence { |
185
|
55
|
100
|
66
|
55
|
|
130
|
return ((!_line_looks_like_record_header($_[0])) and ($_[0] =~ /\S/)) ? 1 : 0; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
####################################################################### |
190
|
|
|
|
|
|
|
############################ Finalize ############################# |
191
|
|
|
|
|
|
|
####################################################################### |
192
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
1; |