forked from jimhester/fasta_utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadFastx.pm
275 lines (237 loc) · 6.87 KB
/
ReadFastx.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{
package ReadFastx;
use Class::XSAccessor getters => [qw(fh files)];
use Carp;
use FileBar;
use autodie;
our $VERSION = '0.25';
sub new {
my $class = shift;
my $self;
if (@_ == 1 && !ref $_[0]) {
$self->{files} = [$_[0]];
}
else {
$self = {@_};
}
$self = bless $self, $class;
$self->{files} = \@ARGV unless exists $self->{files};
$self->BUILD($self);
return $self;
}
sub BUILD {
my ($self) = @_;
unless ($self->{fh}) {
#automatically unzip gzip and bzip files
@{$self->{files}} = map {
s/(.*\.gz)\s*$/pigz -dc < $1|/;
s/(.*\.bz2)\s*$/pbzip2 -dc < $1|/;
$_
} @{$self->files};
#if no files read from stdin
$self->{files} = ['-'] unless @{$self->files};
}
$self->{file_itr} = 0;
$self->current_file($self->files->[0]);
}
sub current_file {
my ($self, $file) = @_;
if ($file) {
$self->_set_current_file($file);
}
else {
return exists $self->{current_file} ? $self->{current_file} : undef;
}
}
sub next_file {
my ($self) = @_;
$self->{file_itr}++;
return if $self->{file_itr} >= @{$self->files};
$self->current_file($self->files->[$self->{file_itr}]);
}
sub next_seq {
my ($self) = @_;
return &{$self->{reader}};
}
sub _set_current_file {
my ($self, $file) = @_;
open my ($fh), $file;
$self->{fh} = $fh;
if (not $self->{bar}) {
$self->{bar} = FileBar->new(current_file => $file,
files => $self->files,
fh_in => $self->fh);
}
else {
$self->{bar}->fh_in($fh);
$self->{bar}->current_file($file);
}
my $first_char = $self->_get_first($self->{fh});
$self->{reader} =
$first_char eq ">" ? sub { $self->_read_fasta }
: $first_char eq "@" ? sub { $self->_read_fastq }
: croak "Not a fasta or fastq file, $first_char is not > or @";
$self->{current_file} = $file;
}
sub _get_first{
my($self, $fh) = @_;
local $/ = \1;
my $first = <$fh>;
return $first;
}
sub _read_fasta {
my ($self) = @_;
local $/ = "\n>";
if (defined(my $record = readline $self->{fh})) {
chomp $record;
my($header, $sequence) = split qr{\n}o, $record, 2;
$sequence =~ tr/\n\r\t //d;
return ReadFastx::Fasta->new(header=>$header, sequence=>$sequence);
}
if ($self->_end_of_files()) {
return undef;
}
$self->next_file();
return $self->next_seq;
}
sub _read_fastq {
my ($self) = @_;
my $fh = $self->fh;
my ($header, $sequence, $h2, $quality);
#ugly for performance I am sorry
{
local $/ = "\n";
$header = readline $fh;
last unless $header;
chomp $header;
$/ = "\n+";
$sequence = readline $fh;
last unless $sequence;
chomp $sequence;
$/ = "\n";
$h2 = readline $fh;
last unless $h2;
$/ = "\n@";
$quality = readline $fh;
last unless $quality;
chomp $quality;
}
if (defined $header and defined $sequence and defined $quality) {
$sequence =~ tr/\n\r\t //d;
$quality =~ tr/\n\r\t //d;
return
ReadFastx::Fastq->new(header => $header,
sequence => $sequence,
quality => $quality);
}
if ($self->_end_of_files()) {
return undef;
}
$self->next_file();
return $self->next_seq;
}
sub _end_of_files {
my ($self) = @_;
return (eof $self->{fh} and $self->{file_itr} > @{$self->{files}});
}
sub close_file {
my ($self) = @_;
close $self->fh;
}
}
{
package ReadFastx::Fastx;
use Class::XSAccessor constructor => 'new', accessors => [qw(header sequence)];
sub _wrap {
my ($self, $string, $width) = @_;
if ($width) {
my $out = '';
my $cur = 0;
while ($cur < length($string)) {
$out .= substr($string, $cur, $width) . "\n";
$cur += $width;
}
return $out;
}
return ($string . "\n");
}
}
{
package ReadFastx::Fasta;
use base qw(ReadFastx::Fastx);
use Readonly;
Readonly my $PRINT_DEFAULT_FH => \*STDOUT;
sub string {
my ($self) = shift;
my $args = @_ == 1 && ref $_[0] ? $_[0] : {@_};
my $width = exists $args->{width} ? $args->{width} : undef;
return ('>' . $self->header . "\n" . $self->_wrap($self->sequence, $width));
}
sub print {
my ($self) = shift;
my $args = @_ == 1 && ref $_[0] ? $_[0] : {@_};
my $fh = exists $args->{fh} ? $args->{fh} : $PRINT_DEFAULT_FH;
my $width = exists $args->{width} ? $args->{width} : undef;
print $fh $self->string(@_);
}
}
{
package ReadFastx::Fastq;
use base qw(ReadFastx::Fastx);
use Class::XSAccessor accessors => [qw(is_phred64)];
use Readonly;
Readonly my $PRINT_DEFAULT_FH => \*STDOUT;
Readonly my $ILLUMINA_OFFSET => 64;
Readonly my $SANGER_OFFSET => 32;
sub quality_array {
my ($self, $new) = @_;
if (defined $new) {
$self->{quality_array} = $new;
delete $self->{quality};
}
elsif (not exists $self->{quality_array}) {
my $offset = $self->is_phred64 ? $ILLUMINA_OFFSET : $SANGER_OFFSET;
$self->{quality_array} = [map { $_ - $offset } unpack "c*", $self->quality];
}
return $self->{quality_array};
}
sub quality {
my ($self, $new) = @_;
if (defined $new) {
$self->{quality} = $new;
delete $self->{quality_array};
}
elsif (not exists $self->{quality}) {
my $offset = $self->is_phred64 ? $ILLUMINA_OFFSET : $SANGER_OFFSET;
$self->{quality} = pack "c*", map { $_ + $offset } @{$self->{quality_array}};
}
return $self->{quality};
}
sub quality_at {
my ($self, $position, $new) = @_;
die "quality_at must specify a position" unless defined $position;
if (defined $new) {
$self->quality_array()->[$position] = $new;
delete $self->{quality};
}
return $self->quality_array()->[$position];
}
sub string {
my ($self) = shift;
my $args = @_ == 1 && ref $_[0] ? $_[0] : {@_};
my $fh = exists $args->{fh} ? $args->{fh} : $PRINT_DEFAULT_FH;
my $offset = $self->is_phred64 ? $ILLUMINA_OFFSET : $SANGER_OFFSET;
my $width = exists $args->{width} ? $args->{width} : undef;
return ("@" . $self->header . "\n" . $self->_wrap($self->sequence, $width) . "+" . $self->header . "\n" . $self->_wrap($self->quality, $width));
}
sub print {
my ($self) = shift;
my $args = @_ == 1 && ref $_[0] ? $_[0] : {@_};
my $fh = exists $args->{fh} ? $args->{fh} : $PRINT_DEFAULT_FH;
my $offset = $self->is_phred64 ? $ILLUMINA_OFFSET : $SANGER_OFFSET;
my $width = exists $args->{width} ? $args->{width} : undef;
print $fh $self->string(@_);
}
use namespace::autoclean;
}
1;