blib/lib/Bio/Gonzales/Util/File.pm

filter: Statementstmt 65.9% 91 / 138 Branchbran 41.2% 33 / 80 Conditioncond 31.4% 11 / 35 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 60.7% 17 / 28 Podpod 50.0% 8 / 16 totaltotal 51.4% 162 / 315 CC 55 SCAR 31.1
CC
55
Cov
53%
open_on_demand
48.6
glob_regex
42.0
is_newer
30.0
CRAP 22.5
« blib/lib/Bio/Gonzales/Align/ConsensePrimer1.pm ↑ Summary blib/script/bg_fasplit.pl »
1 package Bio::Gonzales::Util::File;
2  
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 28 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 28 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use IO::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use IO::Uncompress::Bunzip2 qw($Bunzip2Error);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use File::Which qw/which/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use Bio::Gonzales::Util::IO::Compressed;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 28 our %ZMODES = (
17 '>' => 'wb',
18 '>>' => 'ab',
19 '<', => 'rb',
20 );
21  
22 28 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 28 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
24 28 our $VERSION = '0.058'; # VERSION
25 28 our $EXTERNAL_GZ = which('gzip'); #which('pigz') // which('gzip');
26 28 our $EXTERNAL_BZIP2 = which('bzip2');
27  
28 28 @EXPORT = qw(glob_regex epath bname openod spath);
29 28 %EXPORT_TAGS = ();
30 @EXPORT_OK
31 28 = qw(expand_path slurpc basename regex_glob open_on_demand is_newer splitpath %ZMODES is_archive expand_home gonzopen);
32  
33 0 ▶sub epath { expand_path(@_) }
Subroutine epath: not called CC=1 SCAR=6.9
Pod undocumented
34  
35 sub expand_path {
36 0 ▶ my @files = @_;
Subroutine expand_path: not called CC=3 SCAR=24.8
Pod documented
37  
38 0 my @expanded;
39 0 for my $file (@files) {
40 0 push @expanded, File::Spec->rel2abs( expand_home($file) );
41 }
42 0 ▶ return wantarray ? @expanded : ( shift @expanded );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
43 }
44  
45 sub expand_home {
46 44 ▶ my $file = shift;
Subroutine expand_home: called CC=4 SCAR=23.7
Pod undocumented
47 44 $file =~ s{ ^ ~ ( [^/]* ) }
48 { $1
49 ? (getpwnam($1))[7]
50 0 ▶ : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7] )
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
Condition0%0/6
$ENV{"HOME"} or $ENV{"LOGDIR"} or (getpwuid $>)[7]
l!l&&r!l&&!r
000
$ENV{"HOME"} or $ENV{"LOGDIR"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{"HOME"} or $ENV{"LOGDIR"} or (getpwuid $>)[7]
$ENV{"HOME"} $ENV{"LOGDIR"} (getpwuid $>)[7]
Truth table0%0/4
A or B or C
A= $ENV{"HOME"}B= $ENV{"LOGDIR"}C= (getpwuid $>)[7]
ABCresult
1XX1
01X1
0011
0000
51 }ex;
52  
53 44 return $file;
54 }
55  
56 0 ▶sub regex_glob { return glob_regex(@_) }
Subroutine regex_glob: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 sub glob_regex {
59 0 ▶ my ( $dir, $re ) = @_;
Subroutine glob_regex: not called CC=6 SCAR=37.4
Pod documented
60  
61 0 $dir = expand_path($dir);
62  
63 0 ▶ opendir( my $dh, $dir ) || die "can't opendir >$dir< $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "can't opendir >$dir< $!" 0 0 0
64 0 my @res;
65 0 for ( readdir($dh) ) {
66 0 ▶ push @res, File::Spec->catfile( $dir, $_ ) if ( /$re/ && $_ !~ /^\.\.?$/ );
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ and not $_ =~ /^\.\.?$/ 0 0 0
Condition0%0/3
/$re/ and not $_ =~ /^\.\.?$/
!ll&&!rl&&r
000
MC/DC0%0/2
/$re/ and not $_ =~ /^\.\.?$/
/$re/ not $_ =~ /^\.\.?$/
Truth table0%0/3
A and not(B)
A= /$re/B= not $_ =~ /^\.\.?$/
ABresult
0X0
100
111
67 }
68 0 closedir $dh;
69 0 ▶ return wantarray ? @res : \@res;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
70 }
71  
72 sub slurpc {
73  
74 10 ▶ my ( $fh, $was_open ) = open_on_demand( $_[0], '<' );
Subroutine slurpc: called CC=3 SCAR=11.0
Pod documented
75 10 my @lines = map { s/\r\n/\n/; chomp; $_ } <$fh>;
76 10 ▶ $fh->close if ( !$was_open );
Branch: 1 branch
BranchTrueFalseTotal
unless $was_open 10 0 10
77  
78 10 ▶ return wantarray ? @lines : \@lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 8 10
79 }
80  
81 0 ▶sub bname { return basename(@_) }
Subroutine bname: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 sub basename {
84 0 ▶ my $f = shift;
Subroutine basename: not called CC=2 SCAR=17.9
Pod documented
85 0 my ( $dir, $base ) = ( File::Spec->splitpath($f) )[ 1, 2 ];
86 0 $base =~ s/\.([^.]*?)$//;
87 0 my $suffix = $1;
88  
89 0 ▶ return wantarray ? ( $dir, $base, $suffix ) : $base;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
90 }
91  
92 0 ▶sub spath { return splitpath(@_) }
Subroutine spath: not called CC=1 SCAR=6.9
Pod undocumented
93  
94 sub splitpath {
95 0 ▶ my $f = shift;
Subroutine splitpath: not called CC=1 SCAR=6.9
Pod documented
96 0 my ( $dir, $filename ) = ( File::Spec->splitpath($f) )[ 1, 2 ];
97 0 $dir =~ s![\/\\]$!!;
98 0 return ( $dir, $filename );
99 }
100  
101 0 ▶sub openod { confess "this function is deprecated, use gonzopen instead"; }
Subroutine openod: not called CC=1 SCAR=6.9
Pod documented
102  
103 4 ▶sub gonzopen { ( my $fh ) = open_on_demand(@_); return $fh; }
Subroutine gonzopen: called CC=1 SCAR=0.0
Pod undocumented
104  
105 sub open_on_demand {
106 57 ▶ my ( $src, $mode ) = @_;
Subroutine open_on_demand: called CC=21 SCAR=38.8
Pod documented
107  
108 57 ▶ confess "no file or filehandle given" unless ($src);
Branch: 1 branch
BranchTrueFalseTotal
unless $src 0 57 57
109 57 ▶ confess "no file open mode given or mode not known: $mode" unless ( $mode && exists( $ZMODES{$mode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $mode and exists $Bio::Gonzales::Util::File::ZMODES{$mode} 0 57 57
Condition33%1/3
$mode and exists $Bio::Gonzales::Util::File::ZMODES{$mode}
!ll&&!rl&&r
0057
MC/DC0%0/2
$mode and exists $Bio::Gonzales::Util::File::ZMODES{$mode}
$mode exists $Bio::Gonzales::Util::File::ZMODES{$mode}
Truth table33%1/3
A and B
A= $modeB= exists $Bio::Gonzales::Util::File::ZMODES{$mode}
ABresult
0X0
100
111
110  
111 57 my $fh;
112 my $fh_was_open;
113  
114 57 ▶ if ( $src && !ref($src) && $src eq '-' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src and not ref $src and $src eq "-") 0 57 57
Condition66%4/6
$src and not ref $src and $src eq "-"
!ll&&!rl&&r
13440
$src and not ref $src
!ll&&!rl&&r
01344
MC/DC0%0/3
$src and not ref $src and $src eq "-"
$src not ref $src $src eq "-"
Truth table50%2/4
A and not(B) and C
A= $srcB= not ref $srcC= $src eq "-"
ABCresult
0XX0
10X0
1100
1111
115 0 ▶ if ( $mode eq '<' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '<') { } 0 0 0
elsif ($mode eq '>') { } 0 0 0
116 0 $src = \*STDIN;
117 } elsif ( $mode eq '>' ) {
118 0 $src = \*STDOUT;
119 }
120 }
121  
122 57 ▶ if ( is_fh($src) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_fh($src)) { } 10 47 57
elsif (not ref $src) { } 44 3 47
123 10 $fh = $src;
124 10 $fh_was_open = 1;
125 } elsif ( !ref($src) ) {
126 44 $src = expand_home($src);
127 44 ▶ if ( $src =~ /.+?\.gz$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($src =~ /.+?\.gz$/i) { } 4 40 44
elsif ($src =~ /.+?\.bz2$/i) { } 0 40 40
128 4 ▶ if ($EXTERNAL_GZ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Bio::Gonzales::Util::File::EXTERNAL_GZ) { } 2 2 4
129 2 $fh = _pipe_z( $EXTERNAL_GZ, $src, $mode );
130 } else {
131 2 ▶ $fh = IO::Zlib->new( $src, $ZMODES{$mode} ) or die "IO::Zlib failed\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Zlib"->new($src, $Bio::Gonzales::Util::File::ZMODES{$mode}) or die "IO::Zlib failed\n" 0 2 2
132 }
133 } elsif ( $src =~ /.+?\.bz2$/i ) {
134 0 ▶ if ($EXTERNAL_BZIP2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Bio::Gonzales::Util::File::EXTERNAL_BZIP2) { } 0 0 0
135 0 $fh = _pipe_z( $EXTERNAL_BZIP2, $src, $mode );
136 } else {
137 0 ▶ $fh = IO::Uncompress::Bunzip2->new($src) or die "IO::Uncompress::Bunzip2 failed: $Bunzip2Error\n";
Branch: 1 branch
BranchTrueFalseTotal
$fh = "IO::Uncompress::Bunzip2"->new($src) or die "IO::Uncompress::Bunzip2 failed: $Bio::Gonzales::Util::File::Bunzip2Error\n" 0 0 0
138 }
139 } else {
140 40 ▶ open $fh, $mode, $src or confess "Can't open filehandle $src: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $src or confess("Can't open filehandle ${src}: $!") 0 40 40
141 }
142 } else {
143 # try to open it anyway, let's see what happens
144 # could be a reference to a scalar, supported since perl 5.10
145 3 ▶ open $fh, $mode, $src or confess "Can't open filehandle $src: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $mode, $src or confess("Can't open filehandle ${src}: $!") 0 3 3
146 }
147  
148 56 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 53 3 56
elsif ($fh_was_open) { } 0 3 3
149 53 return ( $fh, $fh_was_open );
150 } elsif ($fh_was_open) {
151 0 carp "it is not advisable to use open_on_demand\n in scalar context with previously opened filehandle";
152 }
153 3 return $fh;
154 }
155  
156 sub _pipe_z {
157 5 ▶ my ( $gz, $f, $mode ) = @_;
Subroutine _pipe_z: called CC=12 SCAR=29.8
158 5 ▶ return unless ( $gz && -x $gz );
Branch: 1 branch
BranchTrueFalseTotal
unless $gz and -x $gz 0 5 5
Condition33%1/3
$gz and -x $gz
!ll&&!rl&&r
005
MC/DC0%0/2
$gz and -x $gz
$gz -x $gz
Truth table33%1/3
A and B
A= $gzB= -x $gz
ABresult
0X0
100
111
159 5 ▶ if ( $mode eq '<' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '<') { } 0 5 5
elsif ($mode eq '>') { } 5 0 5
160 0 ▶ open my $fh, '-|', $gz, '-c', '-d', $f or die "Can't open filehandle $f: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", $gz, "-c", "-d", $f or die "Can't open filehandle ${f}: $!" 0 0 0
161 0 return $fh;
162 } elsif ( $mode eq '>' ) {
163 5 my ( $r, $w );
164 5 ▶ pipe( $r, $w ) || die "gz pipe failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe $r, $w or die "gz pipe failed: $!" 0 5 5
165 5 my $pid = fork();
166 #$SIG{PIPE} = sub { die "whoops, gz pipe broke" };
167 5 ▶ defined($pid) || die "gz fork failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $pid or die "gz fork failed: $!" 0 5 5
168 5 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 3 2 5
169 3 $r->close;
170 #return $w;
171 3 return Bio::Gonzales::Util::IO::Compressed->new( $w, $pid );
172 } else {
173 2 ▶ open( STDIN, "<&", $r ) || die "can't reopen gz STDIN: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $r or die "can't reopen gz STDIN: $!" 0 2 2
174 2 ▶ $w->close || die "can't close gz WRITER: $!";
Branch: 1 branch
BranchTrueFalseTotal
$w->close or die "can't close gz WRITER: $!" 0 2 2
175 2 ▶ open STDOUT, '>', $f or die "Can't open filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $f or die "Can't open filehandle: $!" 0 2 2
176 2 exec( $gz, '-c' );
177 }
178 }
179  
180 0 return;
181 }
182  
183 sub is_archive {
184 0 ▶ my $f = shift;
Subroutine is_archive: not called CC=3 SCAR=24.8
Pod undocumented
185  
186 0 ▶ if ( $f =~ /.+?\.gz$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($f =~ /.+?\.gz$/i) { } 0 0 0
elsif ($f =~ /.+?\.bz2$/i) { } 0 0 0
187 0 return 'gz';
188 } elsif ( $f =~ /.+?\.bz2$/i ) {
189 0 return 'bz2';
190 } else {
191 0 return;
192 }
193 }
194  
195 sub is_newer {
196 0 ▶ my ( $a, $b ) = @_;
Subroutine is_newer: not called CC=5 SCAR=34.0
Pod documented
197  
198 0 ▶ confess "$a doesn't exist"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $a 0 0 0
199 unless ( -f $a );
200 0 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if not -e $b or -e $b and (stat $a)[9] > (stat $b)[9] 0 0 0
Condition0%0/6
not -e $b or -e $b and (stat $a)[9] > (stat $b)[9]
l!l&&r!l&&!r
000
-e $b and (stat $a)[9] > (stat $b)[9]
!ll&&!rl&&r
000
MC/DC0%0/3
not -e $b or -e $b and (stat $a)[9] > (stat $b)[9]
not -e $b -e $b (stat $a)[9] > (stat $b)[9]
Truth table0%0/4
not(A) or B and C
A= not -e $bB= -e $bC= (stat $a)[9] > (stat $b)[9]
ABCresult
1XX1
0111
00X0
0100
201 if ( !-e $b || ( -e $b && ( stat $a )[9] > ( stat $b )[9] ) );
202 0 return;
203 }
204  
205 sub is_fh {
206 67 ▶ my $fh = shift;
Subroutine is_fh: called CC=5 SCAR=16.5
Pod undocumented
207  
208 67 my $reftype = Scalar::Util::reftype($fh);
209  
210 67 ▶ return 1 if ( $reftype && ( $reftype eq 'IO' or $reftype eq 'GLOB' && *{$fh}{IO} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $reftype and $reftype eq "IO" || $reftype eq "GLOB" && *{$fh;}{"IO"} 20 47 67
Condition62%5/8
$reftype and $reftype eq "IO" || $reftype eq "GLOB" && *{$fh;}{"IO"}
!ll&&!rl&&r
44023
$reftype eq "IO" or $reftype eq "GLOB" and *{$fh;}{"IO"}
l!l&&r!l&&!r
0230
$reftype eq "GLOB" and *{$fh;}{"IO"}
!ll
320
MC/DC66%2/3
$reftype and $reftype eq "IO" || $reftype eq "GLOB" && *{$fh;}{"IO"}
$reftype $reftype eq "IO" $reftype eq "GLOB"
Truth table75%3/4
A and B or C
A= $reftypeB= $reftype eq "IO"C= $reftype eq "GLOB"
ABCresult
0XX0
1000
11X1
1011
211  
212 47 return;
213 }
214  
215 28 1;
216 __END__
217  
218 =head1 NAME
219  
220 Bio::Gonzales::Util::File - Utility functions for file stuff
221  
222 =head1 SYNOPSIS
223  
224 use Bio::Gonzales::Util::File qw(glob_regex expand_path slurpc basename open_on_demand is_newer);
225  
226 =head1 DESCRIPTION
227  
228 =head1 SUBROUTINES
229  
230 =over 4
231  
232 =item B<< my ($fh, $was_already_open) = open_on_demand($filename_or_fh, $mode) >>
233  
234 =item B<< my ($fh, $was_already_open) = openod($filename_or_fh, $mode) >>
235  
236 Opens the file if C<$filename_or_fh> is a filename or returns
237 C<$filename_or_fh> if it is already a filehandle, that is opened.
238  
239 =item B<< my $fh = open_on_demand($filename, $mode) >>
240  
241 =item B<< my $fh = openod($filename, $mode) >>
242  
243 Opens the file C<$filename> and returns an handle to it.
244  
245 =item B<< $true_if_a_is_newer = is_newer($a, $b) >>
246  
247 Return true if C<$b> does not exist or C<$a> is newer than C<$b>. Dies if C<$a> does not exist.
248  
249 =item B<< ($dir, $basename, $suffix) = basename($file) >>
250  
251 =item B<< $basename = basename($file) >>
252  
253 Returns the basename of C<$file> in scalar context and the ( C<$dir>,
254 C<$basename>, C<$suffix> ) in list context. Filename example:
255  
256 /path/to/file.txt
257 scalar basename: 'file'
258 list basename: ('path/to', 'file', 'txt')
259  
260 =item B<< @lines = slurpc($file) >>
261  
262 =item B<< @expanded = expand_path(@files) >>
263  
264 =item B<< $expanded_ref = expand_path(@files) >>
265  
266 Expands F<~> in all supplied files and returns the crap.
267  
268 =item B<< @files = glob_regex($dir, $file_regex) >>
269  
270 Selects files from C<$dir> based on the supplied C<$file_regex>.
271  
272 =item B<< ($dirname, $filename) = splitpath($path) >>
273  
274 Splits a $path into directory and filename.
275  
276 =back
277  
278 =head1 SEE ALSO
279  
280 =head1 AUTHOR
281  
282 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
« blib/lib/Bio/Gonzales/Align/ConsensePrimer1.pm ↑ Summary blib/script/bg_fasplit.pl »