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::BED - Object implementing methods for accessing bed formatted files (http://genome.ucsc.edu/FAQ/FAQformat#format1) |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Object that manages a bed file. |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# To initialize |
12
|
|
|
|
|
|
|
my $bed_file = GenOO::Data::File::BED->new({ |
13
|
|
|
|
|
|
|
FILE => undef, |
14
|
|
|
|
|
|
|
EXTRA_INFO => undef, |
15
|
|
|
|
|
|
|
}); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=head1 DESCRIPTION |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
This object offers methods to read a bed file line by line. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 EXAMPLES |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# Create object |
24
|
|
|
|
|
|
|
my $bed_file = GenOO::Data::File::BED->new({ |
25
|
|
|
|
|
|
|
FILE => 't/sample_data/sample.bed.gz' |
26
|
|
|
|
|
|
|
}); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Read one record at a time |
29
|
|
|
|
|
|
|
my $record = $bed_file->next_record(); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=cut |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# Let the code begin... |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
package GenOO::Data::File::BED; |
36
|
|
|
|
|
|
|
$GenOO::Data::File::BED::VERSION = '1.4.6'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
####################################################################### |
39
|
|
|
|
|
|
|
####################### Load External modules ##################### |
40
|
|
|
|
|
|
|
####################################################################### |
41
|
1
|
|
|
1
|
|
4599
|
use Modern::Perl; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
12
|
|
42
|
1
|
|
|
1
|
|
177
|
use autodie; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
14
|
|
43
|
1
|
|
|
1
|
|
4339
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
13
|
|
44
|
1
|
|
|
1
|
|
7361
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
14
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
####################################################################### |
48
|
|
|
|
|
|
|
######################### Load GenOO modules ###################### |
49
|
|
|
|
|
|
|
####################################################################### |
50
|
1
|
|
|
1
|
|
839
|
use GenOO::Data::File::BED::Record; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
1392
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
####################################################################### |
54
|
|
|
|
|
|
|
####################### Interface attributes ###################### |
55
|
|
|
|
|
|
|
####################################################################### |
56
|
|
|
|
|
|
|
has 'file' => ( |
57
|
|
|
|
|
|
|
isa => 'Maybe[Str]', |
58
|
|
|
|
|
|
|
is => 'rw', |
59
|
|
|
|
|
|
|
required => 1 |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
has 'redirect_score_to_copy_number' => ( |
63
|
|
|
|
|
|
|
traits => ['Bool'], |
64
|
|
|
|
|
|
|
is => 'rw', |
65
|
|
|
|
|
|
|
isa => 'Bool', |
66
|
|
|
|
|
|
|
default => 0, |
67
|
|
|
|
|
|
|
lazy => 1 |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
####################################################################### |
72
|
|
|
|
|
|
|
######################## Private attributes ####################### |
73
|
|
|
|
|
|
|
####################################################################### |
74
|
|
|
|
|
|
|
has '_filehandle' => ( |
75
|
|
|
|
|
|
|
is => 'ro', |
76
|
|
|
|
|
|
|
builder => '_open_filehandle', |
77
|
|
|
|
|
|
|
init_arg => undef, |
78
|
|
|
|
|
|
|
lazy => 1, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
####################################################################### |
83
|
|
|
|
|
|
|
############################## BUILD ############################## |
84
|
|
|
|
|
|
|
####################################################################### |
85
|
|
|
|
|
|
|
sub BUILD { |
86
|
20
|
|
|
20
|
0
|
40
|
my $self = shift; |
87
|
|
|
|
|
|
|
|
88
|
20
|
|
|
|
|
67
|
$self->init_header; |
89
|
20
|
|
|
|
|
63
|
$self->init_records_cache; |
90
|
20
|
|
|
|
|
54
|
$self->init_records_read_count; |
91
|
|
|
|
|
|
|
|
92
|
20
|
|
|
|
|
60
|
$self->parse_header_section; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
####################################################################### |
97
|
|
|
|
|
|
|
######################## Interface Methods ######################## |
98
|
|
|
|
|
|
|
####################################################################### |
99
|
|
|
|
|
|
|
sub records_read_count { |
100
|
5
|
|
|
5
|
0
|
1503
|
my ($self) = @_; |
101
|
5
|
|
|
|
|
33
|
return $self->{RECORDS_READ_COUNT}; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub next_record { |
105
|
54
|
|
|
54
|
0
|
1644
|
my ($self) = @_; |
106
|
|
|
|
|
|
|
|
107
|
54
|
|
|
|
|
61
|
my $record; |
108
|
54
|
100
|
|
|
|
108
|
if ($self->record_cache_not_empty) { |
109
|
9
|
|
|
|
|
35
|
$record = $self->next_record_from_cache; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
45
|
|
|
|
|
89
|
$record = $self->next_record_from_file; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
54
|
100
|
|
|
|
138
|
if (defined $record) { |
116
|
49
|
|
|
|
|
118
|
$self->increment_records_read_count; |
117
|
|
|
|
|
|
|
} |
118
|
54
|
|
|
|
|
391
|
return $record; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
####################################################################### |
123
|
|
|
|
|
|
|
####################### Private Methods ############################ |
124
|
|
|
|
|
|
|
####################################################################### |
125
|
|
|
|
|
|
|
sub set_eof_reached { |
126
|
5
|
|
|
5
|
0
|
10
|
my ($self) = @_; |
127
|
5
|
|
|
|
|
18
|
$self->{EOF} = 1; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub header { |
131
|
1
|
|
|
1
|
0
|
1511
|
my ($self) = @_; |
132
|
1
|
|
|
|
|
10
|
return $self->{HEADER}; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub records_cache { |
136
|
61
|
|
|
61
|
0
|
1706
|
my ($self) = @_; |
137
|
61
|
|
|
|
|
262
|
return $self->{RECORDS_CACHE}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub init_header { |
141
|
20
|
|
|
20
|
0
|
31
|
my ($self) = @_; |
142
|
20
|
|
|
|
|
56
|
$self->{HEADER} = {}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
sub init_records_cache { |
146
|
20
|
|
|
20
|
0
|
28
|
my ($self) = @_; |
147
|
20
|
|
|
|
|
55
|
$self->{RECORDS_CACHE} = []; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub init_records_read_count { |
151
|
20
|
|
|
20
|
0
|
41
|
my ($self) = @_; |
152
|
20
|
|
|
|
|
38
|
$self->{RECORDS_READ_COUNT} = 0; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub increment_records_read_count { |
156
|
50
|
|
|
50
|
0
|
1541
|
my ($self) = @_; |
157
|
50
|
|
|
|
|
87
|
$self->{RECORDS_READ_COUNT}++; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub parse_header_section { |
161
|
20
|
|
|
20
|
0
|
34
|
my ($self) = @_; |
162
|
|
|
|
|
|
|
|
163
|
20
|
|
|
|
|
781
|
my $filehandle = $self->_filehandle; |
164
|
20
|
|
|
|
|
642
|
while (my $line = $filehandle->getline) { |
165
|
80
|
100
|
|
|
|
7935
|
if ($self->line_looks_like_header($line)) { |
|
|
50
|
|
|
|
|
|
166
|
60
|
|
|
|
|
192
|
$self->recognize_and_store_header_line($line); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
elsif ($self->line_looks_like_record($line)) { |
169
|
|
|
|
|
|
|
# the while loop will read one line after header. Usually, this is the first record and unfortunately in zipped files we cannot go back |
170
|
20
|
|
|
|
|
84
|
my $record = $self->parse_record_line($line); |
171
|
20
|
|
|
|
|
78
|
$self->add_to_records_cache($record); |
172
|
20
|
|
|
|
|
722
|
return; |
173
|
|
|
|
|
|
|
} |
174
|
|
|
|
|
|
|
else { |
175
|
0
|
|
|
|
|
0
|
return; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# TODO fix to store "browser" and "track" lines |
181
|
|
|
|
|
|
|
sub recognize_and_store_header_line { |
182
|
60
|
|
|
60
|
0
|
1243
|
my ($self, $line) = @_; |
183
|
|
|
|
|
|
|
# if ($self->line_looks_like_version($line)) { |
184
|
|
|
|
|
|
|
# $self->parse_and_store_version_line($line); |
185
|
|
|
|
|
|
|
# } |
186
|
|
|
|
|
|
|
# else { |
187
|
|
|
|
|
|
|
# $self->parse_and_store_header_line($line); |
188
|
|
|
|
|
|
|
# } |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub add_to_records_cache { |
192
|
20
|
|
|
20
|
0
|
61
|
my ($self, $record) = @_; |
193
|
20
|
|
|
|
|
30
|
push @{$self->{RECORDS_CACHE}}, $record, |
|
20
|
|
|
|
|
66
|
|
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub next_record_from_file { |
197
|
46
|
|
|
46
|
0
|
1401
|
my ($self) = @_; |
198
|
|
|
|
|
|
|
|
199
|
46
|
|
|
|
|
1374
|
while (my $line = $self->_filehandle->getline) { |
200
|
41
|
50
|
|
|
|
1011
|
if ($self->line_looks_like_record($line)) { |
201
|
41
|
|
|
|
|
88
|
return $self->parse_record_line($line); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
else { |
204
|
0
|
0
|
|
|
|
0
|
if ($self->line_looks_like_header($line)) { |
205
|
0
|
|
|
|
|
0
|
die "Record was expected but line looks like a header - the header should have been parsed already. $line\n"; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
else { |
208
|
0
|
|
|
|
|
0
|
warn "Record was expected but line looks different. $line\n"; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
5
|
|
|
|
|
187
|
$self->set_eof_reached; |
214
|
5
|
|
|
|
|
10
|
return undef; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub next_record_from_cache { |
218
|
10
|
|
|
10
|
0
|
1321
|
my ($self) = @_; |
219
|
|
|
|
|
|
|
|
220
|
10
|
|
|
|
|
13
|
my $record = shift @{$self->{RECORDS_CACHE}}; |
|
10
|
|
|
|
|
23
|
|
221
|
10
|
50
|
|
|
|
29
|
if (defined $record) { |
222
|
10
|
|
|
|
|
34
|
return $record; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
else { |
225
|
0
|
|
|
|
|
0
|
return undef; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub parse_record_line { |
230
|
62
|
|
|
62
|
0
|
1707
|
my ($self, $line) = @_; |
231
|
|
|
|
|
|
|
|
232
|
62
|
|
|
|
|
109
|
chomp $line; |
233
|
62
|
|
|
|
|
450
|
my ($chr,$start,$stop_1,$name,$score,$strand,$thick_start,$thick_stop,$rgb,$block_count,$block_sizes,$block_starts) = split(/\t/,$line); |
234
|
|
|
|
|
|
|
|
235
|
62
|
|
|
|
|
647
|
my $data = { |
236
|
|
|
|
|
|
|
rname => $chr, |
237
|
|
|
|
|
|
|
start => $start, |
238
|
|
|
|
|
|
|
stop_1based => $stop_1, |
239
|
|
|
|
|
|
|
name => $name, |
240
|
|
|
|
|
|
|
score => $score, |
241
|
|
|
|
|
|
|
strand_symbol => $strand, |
242
|
|
|
|
|
|
|
}; |
243
|
|
|
|
|
|
|
|
244
|
62
|
100
|
|
|
|
2096
|
($data->{copy_number} = $score) if $self->redirect_score_to_copy_number; |
245
|
62
|
50
|
|
|
|
243
|
($data->{thick_start} = $thick_start) if defined $thick_start; |
246
|
62
|
50
|
|
|
|
205
|
($data->{thick_stop_1based} = $thick_stop) if defined $thick_stop; |
247
|
62
|
50
|
|
|
|
183
|
($data->{rgb} = $rgb) if defined $rgb; |
248
|
62
|
100
|
|
|
|
104
|
($data->{block_count} = $block_count) if defined $block_count; |
249
|
62
|
100
|
|
|
|
104
|
($data->{block_sizes} = [split(/,/,$block_sizes)]) if defined $block_sizes; |
250
|
62
|
100
|
|
|
|
108
|
($data->{block_starts} = [split(/,/,$block_starts)]) if defined $block_starts; |
251
|
|
|
|
|
|
|
|
252
|
62
|
|
|
|
|
2285
|
return GenOO::Data::File::BED::Record->new($data); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub line_looks_like_comment { |
256
|
1
|
|
|
1
|
0
|
1512
|
my ($self, $line) = @_; |
257
|
1
|
50
|
|
|
|
16
|
return ($line =~ /^#/) ? 1 : 0; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sub line_looks_like_header { |
261
|
83
|
|
|
83
|
0
|
1635
|
my ($self, $line) = @_; |
262
|
83
|
100
|
|
|
|
529
|
return ($line =~ /^(track|browser)/) ? 1 : 0; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub line_looks_like_record { |
266
|
65
|
|
|
65
|
0
|
1784
|
my ($self, $line) = @_; |
267
|
65
|
100
|
|
|
|
292
|
return ($line !~ /^(#|track|browser)/) ? 1 : 0; |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
sub record_cache_not_empty { |
271
|
56
|
|
|
56
|
0
|
1662
|
my ($self) = @_; |
272
|
56
|
100
|
|
|
|
147
|
return ($self->record_cache_size > 0) ? 1 : 0; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
sub record_cache_is_empty { |
276
|
2
|
|
|
2
|
0
|
1584
|
my ($self) = @_; |
277
|
2
|
100
|
|
|
|
11
|
return ($self->record_cache_size == 0) ? 1 : 0; |
278
|
|
|
|
|
|
|
} |
279
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
sub record_cache_size { |
281
|
60
|
|
|
60
|
0
|
1401
|
my ($self) = @_; |
282
|
60
|
|
|
|
|
63
|
return scalar @{$self->records_cache}; |
|
60
|
|
|
|
|
118
|
|
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
sub is_eof_reached { |
286
|
2
|
|
|
2
|
0
|
1418
|
my ($self) = @_; |
287
|
2
|
|
|
|
|
14
|
return $self->{EOF}; |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
####################################################################### |
292
|
|
|
|
|
|
|
######################### Private Methods ######################### |
293
|
|
|
|
|
|
|
####################################################################### |
294
|
|
|
|
|
|
|
sub _open_filehandle { |
295
|
20
|
|
|
20
|
|
28
|
my ($self) = @_; |
296
|
|
|
|
|
|
|
|
297
|
20
|
|
|
|
|
31
|
my $read_mode; |
298
|
|
|
|
|
|
|
my $HANDLE; |
299
|
20
|
50
|
|
|
|
612
|
if (!defined $self->file) { |
|
|
50
|
|
|
|
|
|
300
|
0
|
|
|
|
|
0
|
open ($HANDLE, '<-', $self->file); |
301
|
|
|
|
|
|
|
} |
302
|
|
|
|
|
|
|
elsif ($self->file =~ /\.gz$/) { |
303
|
20
|
50
|
|
|
|
553
|
die 'Cannot open file ' . $self->file . "\n" if ! -e $self->file; |
304
|
20
|
|
|
|
|
599
|
open($HANDLE, 'gzip -dc ' . $self->file . ' |'); |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
else { |
307
|
0
|
|
|
|
|
0
|
open ($HANDLE, '<', $self->file); |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
20
|
|
|
|
|
78306
|
return $HANDLE; |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
####################################################################### |
315
|
|
|
|
|
|
|
############################ Finalize ############################# |
316
|
|
|
|
|
|
|
####################################################################### |
317
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
1; |