Seq/Stream.pm

filter: Statementstmt 100% 119 / 119 Branchbran 90.3% 46 / 52 (1 uncoverable) Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 22 / 22 Podpod 100% 2 / 2 totaltotal 89.4% 194 / 218 (1 uncoverable) CC 29 SCAR 24.7
CC
29
Cov
92%
new
23.1
_guess_format
6.0
fast
2.1
CRAP 11.8
« Seq/Fetch.pm ↑ Summary Seq/Stream/FASTQ.pm »
1 package BioX::Seq::Stream;
2  
3 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use IPC::Cmd qw/can_run/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw/blessed openhandle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use BioX::Seq;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use POSIX qw/ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cwd qw/abs_path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use File::Basename qw/fileparse/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # define or search for binary locations
16 # if these are not available
17 2 ▶our $GZIP_BIN = can_run('pigz') // can_run('gzip');
Condition33%1/3
can_run("pigz") // can_run("gzip")
l!l&&r!l&&!r
200
MC/DC0%0/2
can_run("pigz") // can_run("gzip")
can_run("pigz") can_run("gzip")
Truth table33%1/3
A // B
A= can_run("pigz")B= can_run("gzip")
ABresult
1X1
011
000
18 2 ▶our $BZIP_BIN = can_run('pbzip2') // can_run('bzip2');
Condition33%1/3
can_run("pbzip2") // can_run("bzip2")
l!l&&r!l&&!r
020
MC/DC0%0/2
can_run("pbzip2") // can_run("bzip2")
can_run("pbzip2") can_run("bzip2")
Truth table33%1/3
A // B
A= can_run("pbzip2")B= can_run("bzip2")
ABresult
1X1
011
000
19 2 ▶our $ZSTD_BIN = can_run('pzstd') // can_run('zstd');
Condition33%1/3
can_run("pzstd") // can_run("zstd")
l!l&&r!l&&!r
002
MC/DC0%0/2
can_run("pzstd") // can_run("zstd")
can_run("pzstd") can_run("zstd")
Truth table33%1/3
A // B
A= can_run("pzstd")B= can_run("zstd")
ABresult
1X1
011
000
20 2 ▶our $DSRC_BIN = can_run('dsrc2') // can_run('dsrc');
Condition33%1/3
can_run("dsrc2") // can_run("dsrc")
l!l&&r!l&&!r
002
MC/DC0%0/2
can_run("dsrc2") // can_run("dsrc")
can_run("dsrc2") can_run("dsrc")
Truth table33%1/3
A // B
A= can_run("dsrc2")B= can_run("dsrc")
ABresult
1X1
011
000
21 2 our $FQZC_BIN = can_run('fqz_comp');
22 2 our $XZ_BIN = can_run('xz');
23  
24 2 ▶use constant MAGIC_GZIP => pack('C3', 0x1f, 0x8b, 0x08);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant MAGIC_DSRC => pack('C2', 0xaa, 0x02);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant MAGIC_BZIP => 'BZh';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use constant MAGIC_FQZC => '.fqz';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use constant MAGIC_BAM => pack('C4', 0x42, 0x41, 0x4d, 0x01);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use constant MAGIC_2BIT => pack('C4', 0x1a, 0x41, 0x27, 0x43);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use constant MAGIC_ZSTD => pack('C4', 0x28, 0xB5, 0x2F, 0xFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant MAGIC_XZ => pack('C6', 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 25 ▶sub new {
Subroutine new: called CC=23 SCAR=31.4
Pod documented
34  
35 25 my ($class,$fn, %args) = @_;
36  
37 25 my $self = bless {} => $class;
38  
39 # 'fast' mode turns off parser sanity-checking in places
40 25 ▶ if ($args{fast}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"fast"}) 1 24 25
41 1 $self->fast( $args{fast} );
42 }
43  
44 25 ▶ if (defined $fn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fn) { } 24 1 25
45  
46 24 my $fh = openhandle($fn); # can pass filehandle too;
47 24 ▶ if (! defined $fh) { # otherwise assume filename
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 22 2 24
48
49 #if passed a filename, try to determine if compressed
50 22 ▶ open $fh, '<', $fn or die "Error opening $fn for reading\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $fn or die "Error opening $fn for reading\n" 21 1 22
51  
52 #read first six bytes as raw
53 #this causes a memory leak as opened filehandles are not properly
54 #closed again. Should work without setting binary mode anyway.
55 #my $old_layers = join '', map {":$_"} PerlIO::get_layers($fh);
56 #binmode($fh);
57 21 read( $fh, my $magic, 6 );
58 #binmode($fh, $old_layers);
59  
60 #check for compression and open stream if found
61 21 ▶ if (substr($magic,0,3) eq MAGIC_GZIP) {
Branch: 6 branches
BranchTrueFalseTotal
if (substr($magic, 0, 3) eq "\037\213\cH") { } 5 16 21
elsif (substr($magic, 0, 3) eq 'BZh') { } 3 13 16
elsif (substr($magic, 0, 4) eq "(\265/\375") { } 2 11 13
elsif (substr($magic, 0, 2) eq "\252\cB") { } 2 9 11
elsif (substr($magic, 0, 4) eq '.fqz') { } 2 7 9
elsif (substr($magic, 0, 6) eq "\3757zXZ\000") { } 1 6 7
62 5 close $fh;
63 5 ▶ if (! defined $GZIP_BIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $BioX::Seq::Stream::GZIP_BIN) { } 1 4 5
64 # fall back on Perl-based method (but can be SLOOOOOW!)
65 1 require IO::Uncompress::Gunzip;
66 1 $fh = IO::Uncompress::Gunzip->new($fn, MultiStream => 1);
67 }
68 else {
69 4 ▶ open $fh, '-|', $GZIP_BIN, '-dc', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::GZIP_BIN, "-dc", $fn or die "Error opening gzip stream: $!\n" 3 1 4
70 or die "Error opening gzip stream: $!\n";
71 }
72 }
73 elsif (substr($magic,0,3) eq MAGIC_BZIP) {
74 3 close $fh;
75 3 ▶ if (! defined $BZIP_BIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $BioX::Seq::Stream::BZIP_BIN) { } 1 2 3
76 # fall back on Perl-based method (but can be SLOOOOOW!)
77 1 require IO::Uncompress::Bunzip2;
78 1 $fh = IO::Uncompress::Bunzip2->new($fn, MultiStream => 1);
79 }
80 else {
81 2 ▶ open $fh, '-|', $BZIP_BIN, '-dc', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::BZIP_BIN, "-dc", $fn or die "Error opening bzip2 stream: $!\n" 1 1 2
82 or die "Error opening bzip2 stream: $!\n";
83 }
84 }
85 elsif (substr($magic,0,4) eq MAGIC_ZSTD) {
86 2 ▶ die "no zstd backend found\n" if (! defined $ZSTD_BIN);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BioX::Seq::Stream::ZSTD_BIN 1 1 2
87 1 close $fh;
88 1 ▶ open $fh, '-|', $ZSTD_BIN, '-dc', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::ZSTD_BIN, "-dc", $fn or die "Error opening zstd stream: $!\n" 0 1 1
89 or die "Error opening zstd stream: $!\n";
90 }
91 elsif (substr($magic,0,2) eq MAGIC_DSRC) {
92 2 ▶ die "no dsrc backend found\n" if (! defined $DSRC_BIN);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BioX::Seq::Stream::DSRC_BIN 1 1 2
93 1 close $fh;
94 1 ▶ open $fh, '-|', $DSRC_BIN, 'd', '-s', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::DSRC_BIN, "d", "-s", $fn or die "Error opening dsrc stream: $!\n" 0 1 1
95 or die "Error opening dsrc stream: $!\n";
96 }
97 elsif (substr($magic,0,4) eq MAGIC_FQZC) {
98 2 ▶ die "no fqz backend found\n" if (! defined $FQZC_BIN);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BioX::Seq::Stream::FQZC_BIN 1 1 2
99 1 close $fh;
100 1 ▶ open $fh, '-|', $FQZC_BIN, '-d', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::FQZC_BIN, "-d", $fn or die "Error opening fqz_comp stream: $!\n" 0 1 1
101 or die "Error opening fqz_comp stream: $!\n";
102 }
103 elsif (substr($magic,0,6) eq MAGIC_XZ) {
104 1 ▶ die "no xz backend found\n" if (! defined $XZ_BIN);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $BioX::Seq::Stream::XZ_BIN 0 1 1
105 1 close $fh;
106 1 ▶ open $fh, '-|', $XZ_BIN, '-dc', $fn
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "-|", $BioX::Seq::Stream::XZ_BIN, "-dc", $fn or die "Error opening xz stream: $!\n" 1 0 1
107 or die "Error opening xz stream: $!\n";
108 }
109 else {
110 6 seek($fh,0,0);
111 }
112  
113 }
114 15 $self->{fh} = $fh;
115  
116 }
117 else {
118 1 $self->{fh} = \*STDIN;
119 }
120  
121 # handle files coming from different platforms
122 #my @layers = PerlIO::get_layers($self->{fh});
123 #binmode($self->{fh},':unix:stdio:crlf');
124  
125 16 $self->_guess_format;
126  
127 14 $self->_init;
128  
129 12 return $self;
130  
131 }
132  
133 1 ▶sub fast {
Subroutine fast: called CC=2 SCAR=7.2
Pod documented
134  
135 1 my ($self, $bool) = @_;
136 1 ▶ $self->{fast} = $bool // 1;
Condition50%1/2
$bool // 1
l!l
10
MC/DC0%0/1
$bool // 1
$bool
Truth table50%1/2
A
A= $bool
Aresult
11
00
137  
138 }
139  
140 16 ▶sub _guess_format {
Subroutine _guess_format: called CC=6 SCAR=17.9
141  
142 16 my ($self) = @_;
143  
144 # Filetype guessing must be based on first two bytes (or less)
145 # which are stored in an object buffer
146 16 my $r = (read $self->{fh}, $self->{buffer}, 2);
147 16 ▶ die "failed to read initial bytes" if ($r != 2);
Branch: 1 branch
BranchTrueFalseTotal
if $r != 2 1 15 16
148  
149 15 my $search_path = abs_path(__FILE__);
150 15 $search_path =~ s/\.pm$//i;
151 15 my @matched;
152 2 ▶ for my $module ( glob "$search_path/*.pm" ) {
Subroutine BEGIN: called CC=1 SCAR=0.0
153 45 my ($name,$path,$suff) = fileparse($module, qr/\.pm/i);
154 45 my $classname = blessed($self) . "::$name";
155 45 load $classname;
156 45 ▶ if ($classname->_check_type($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classname->_check_type($self)) 14 31 45
157 14 push @matched, $classname;
158 }
159 }
160  
161 15 ▶ die "Failed to guess filetype\n" if (scalar(@matched) < 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matched < 1 1 14 15
162 # uncoverable branch true
163 14 ▶ die "Multiple filetypes matched\n" if (scalar(@matched) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @matched > 1 0 14 14
164  
165 14 load $matched[0];
166 14 bless $self => $matched[0];
167  
168 }
169  
170  
171 2 1;
172  
173  
174 __END__
« Seq/Fetch.pm ↑ Summary Seq/Stream/FASTQ.pm »