FASTX-Reader
view release on metacpan or search on metacpan
lib/FASTX/Reader.pm view on Meta::CPAN
package FASTX::Reader;
use 5.012;
use warnings;
use Carp qw(confess);
use Data::Dumper;
use PerlIO::encoding;
$Data::Dumper::Sortkeys = 1;
use FASTX::Seq;
use File::Basename;
$FASTX::Reader::VERSION = '1.12.0';
require Exporter;
our @ISA = qw(Exporter);
#ABSTRACT: A simple module to parse FASTA and FASTQ files, supporting compressed files and paired-ends.
use constant GZIP_SIGNATURE => pack('C3', 0x1f, 0x8b, 0x08);
sub new {
# Instantiate object
my $class = shift @_;
my $self = bless {} => $class;
my $args = {};
# Named parameters: undefined $_[0] will read STDIN!
if (defined $_[0] and substr($_[0], 0, 1) eq '-') {
my %data = @_;
# Try parsing
for my $i (keys %data) {
if ($i =~ /^-(file|filename)/i) {
$args->{filename} = $data{$i};
} elsif ($i =~ /^-(loadseqs)/i) {
$args->{loadseqs} = $data{$i};
} else {
confess "[FASTX::Reader]: Unknown parameter `$i`\n";
}
}
} else {
# Legacy instantiation
($args) = @_;
}
if (defined $args->{loadseqs}) {
if ($args->{loadseqs} eq 'name' or $args->{loadseqs} eq 'names' ) {
$args->{loadseqs} = 'name';
} elsif ($args->{loadseqs} eq 'seq' or $args->{loadseqs} eq 'seqs' or $args->{loadseqs} eq "1") {
$args->{loadseqs} = 'seq';
} elsif ($args->{loadseqs} eq 'records') {
$args->{loadseqs} = 'records';
} else {
confess("attribute <loadseqs> should be 'name' or 'seq' to specify the key of the hash.");
}
}
$self->{filename} = $args->{filename};
$self->{loadseqs} = $args->{loadseqs};
$self->{aux} = [undef];
$self->{compressed} = 0;
$self->{fh} = undef;
# Check if a filename was provided and not {{STDIN}}
# uncoverable branch false
if ( defined $self->{filename} and -d $self->{filename} ) {
confess __PACKAGE__, " Directory provide where file was expected\n";
}
if (defined $self->{filename} and $self->{filename} ne '{{STDIN}}') {
open my $initial_fh, '<', $self->{filename} or confess "Unable to read file ", $self->{filename}, "\n";
read( $initial_fh, my $magic_byte, 4 );
close $initial_fh;
# See: __BioX::Seq::Stream__ for GZIP (and other) compressed file reader
if (substr($magic_byte,0,3) eq GZIP_SIGNATURE) {
$self->{compressed} = 1;
our $GZIP_BIN = _which('pigz', 'gzip');
#close $fh;
if (! defined $GZIP_BIN) {
$self->{decompressor} = 'IO::Uncompress::Gunzip';
( run in 2.852 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )