blib/lib/Bio/Gonzales/Matrix/IO.pm

filter: Statementstmt 52.1% 156 / 299 Branchbran 31.4% 66 / 210 Conditioncond 29.6% 54 / 182 MC/DCmcdc 10.0% 10 / 100 Subroutinesub 69.2% 18 / 26 Podpod 36.3% 4 / 11 totaltotal 37.2% 308 / 828 CC 176 SCAR 51.2
CC
176
Cov
40%
xlsx_spew
380.0
mspew
316.3
lspew
182.0
CRAP 166.9
« blib/lib/Bio/Gonzales/Seq.pm ↑ Summary blib/lib/Bio/Gonzales/Domain/Identification/HMMER.pm »
1 package Bio::Gonzales::Matrix::IO;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use List::MoreUtils qw/uniq zip/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Bio::Gonzales::Util qw/flatten/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Text::CSV_XS qw/csv/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Bio::Gonzales::Matrix::Util qw/uniq_rows/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 ▶use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Bio::Gonzales::Util::File qw/open_on_demand slurpc expand_home/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 9 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
20 9 our $VERSION = '0.062'; # VERSION
21  
22 9 @EXPORT = qw(mslurp mspew lslurp miterate lspew dict_slurp dict_spew xcsv_slurp miterate_hash);
23 9 %EXPORT_TAGS = ();
24 9 @EXPORT_OK = qw(lspew xlsx_slurp xlsx_spew);
25 9 my $COMMENT_RE = qr/^\s*#/;
26  
27 sub dict_slurp {
28 8 ▶ my ( $src, $cc ) = @_;
Subroutine dict_slurp: called CC=43 SCAR=50.3
Pod documented
29 croak "you have not specified key_idx"
30 8 ▶ unless ( $cc && exists( $cc->{key_idx} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $cc and exists $cc->{"key_idx"} 0 8 8
Condition33%1/3
$cc and exists $cc->{"key_idx"}
!ll&&!rl&&r
008
MC/DC0%0/2
$cc and exists $cc->{"key_idx"}
$cc exists $cc->{"key_idx"}
Truth table33%1/3
A and B
A= $ccB= exists $cc->{"key_idx"}
ABresult
0X0
100
111
31  
32 8 ▶ $cc //= {};
Condition50%1/2
$cc //= {}
l!l
80
MC/DC0%0/1
$cc //= {}
$cc
Truth table50%1/2
A
A= $cc
Aresult
11
00
33 8 my %c = (
34 sep => qr/\t/,
35 header => undef,
36 skip => -1,
37 comment => $COMMENT_RE,
38 key_idx => 0,
39 record_filter => undef,
40 commented_header => undef,
41 concat_keys => 1,
42 sort_keys => 0,
43 strict => 0,
44 %$cc
45 );
46  
47 8 ▶ $c{header} //= $c{commented_header};
Condition33%1/3
$c{"header"} //= $c{"commented_header"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$c{"header"} //= $c{"commented_header"}
$c{"header"} $c{"commented_header"}
Truth table33%1/3
A //= B
A= $c{"header"}B= $c{"commented_header"}
ABresult
1X1
011
000
48  
49 8 my $is_strict = $c{strict};
50 8 my $record_filter = $c{record_filter};
51  
52 # concatenate keys to a big string
53 8 my @kidcs;
54 8 ▶ if ( $c{concat_keys} || !ref( $c{key_idx} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{'concat_keys'} or not ref $c{'key_idx'}) { } 8 0 8
Condition33%1/3
$c{"concat_keys"} or not ref $c{"key_idx"}
l!l&&r!l&&!r
800
MC/DC0%0/2
$c{"concat_keys"} or not ref $c{"key_idx"}
$c{"concat_keys"} not ref $c{"key_idx"}
Truth table33%1/3
A or not(B)
A= $c{"concat_keys"}B= not ref $c{"key_idx"}
ABresult
1X1
011
000
55 8 @kidcs = ( $c{key_idx} );
56 } else {
57 # or treat them separately
58 0 @kidcs = @{ $c{key_idx} };
59 }
60  
61 8 my $vidx = $c{val_idx};
62 # make an array from it
63  
64 8 ▶ my $uniq = $c{uniq} // $c{uniq_vals} // $c{unique} // 0;
Condition75%6/8
(($c{"uniq"} // $c{"uniq_vals"}) // $c{"unique"}) // 0
l!l
62
($c{"uniq"} // $c{"uniq_vals"}) // $c{"unique"}
l!l&&r!l&&!r
602
$c{"uniq"} // $c{"uniq_vals"}
l!l&&r!l&&!r
602
MC/DC33%1/3
(($c{"uniq"} // $c{"uniq_vals"}) // $c{"unique"}) // 0
$c{"uniq"} $c{"uniq_vals"} $c{"unique"}
Truth table50%2/4
A // B // C
A= $c{"uniq"}B= $c{"uniq_vals"}C= $c{"unique"}
ABCresult
1XX1
01X1
0011
0000
65  
66 8 my ( $fh, $fh_was_open ) = open_on_demand( $src, '<' );
67  
68 8 my @header;
69 8 ▶ if ( $c{header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{"header"}) 0 8 8
70 0 ▶ while ( my $raw_row = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $raw_row = readline $fh)) 0 0 0
71 0 ▶ if ( $c{comment} && $raw_row =~ /$c{comment}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{"comment"} and $raw_row =~ /$c{"comment"}/) 0 0 0
Condition0%0/3
$c{"comment"} and $raw_row =~ /$c{"comment"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$c{"comment"} and $raw_row =~ /$c{"comment"}/
$c{"comment"} $raw_row =~ /$c{"comment"}/
Truth table0%0/3
A and B
A= $c{"comment"}B= $raw_row =~ /$c{"comment"}/
ABresult
0X0
100
111
72 0 ▶ if ( $c{commented_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{'commented_header'}) { } 0 0 0
73 0 $raw_row =~ s/$c{comment}//;
74 } else {
75 0 next;
76 }
77 }
78 0 $raw_row =~ s/\r$//;
79 0 chomp $raw_row;
80 0 @header = split /$c{sep}/, $raw_row;
81 0 ▶ @header = ref $vidx ? @header[@$vidx] : ( $header[$vidx] ) if ( defined $vidx && $vidx ne 'all' );
Branch: 2 branches
BranchTrueFalseTotal
if defined $vidx and $vidx ne "all" 0 0 0
ref $vidx ? : 0 0 0
Condition0%0/3
defined $vidx and $vidx ne "all"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $vidx and $vidx ne "all"
defined $vidx $vidx ne "all"
Truth table0%0/3
A and B
A= defined $vidxB= $vidx ne "all"
ABresult
0X0
100
111
82 0 last;
83 }
84 }
85  
86 8 my %map;
87 8 my $lnum = 0;
88 8 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 40 7 47
89 40 ▶ next if ( $lnum++ <= $c{skip} );
Branch: 1 branch
BranchTrueFalseTotal
if $lnum++ <= $c{"skip"} 0 40 40
90 40 ▶ next if ( $c{comment} && /$c{comment}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"comment"} and /$c{"comment"}/ 0 40 40
Condition33%1/3
$c{"comment"} and /$c{"comment"}/
!ll&&!rl&&r
0400
MC/DC0%0/2
$c{"comment"} and /$c{"comment"}/
$c{"comment"} /$c{"comment"}/
Truth table33%1/3
A and B
A= $c{"comment"}B= /$c{"comment"}/
ABresult
0X0
100
111
91 40 s/\r$//;
92 40 chomp;
93 40 ▶ next if (/^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 40 40
94  
95 40 ▶ next if ( $record_filter && !$record_filter->($_) );
Branch: 1 branch
BranchTrueFalseTotal
if $record_filter and not &$record_filter($_) 1 39 40
Condition100%3/3
$record_filter and not &$record_filter($_)
!ll&&!rl&&r
3811
MC/DC100%2/2
$record_filter and not &$record_filter($_)
$record_filter not &$record_filter($_)
Truth table100%3/3
A and not(B)
A= $record_filterB= not &$record_filter($_)
ABresult
0X0
100
111
96  
97 39 my @r = split /$c{sep}/;
98  
99 39 for my $kidx (@kidcs) {
100  
101 39 ▶ my @k = ( ref $kidx ? @r[@$kidx] : $r[$kidx] );
Branch: 1 branch
BranchTrueFalseTotal
ref $kidx ? : 0 39 39
102 39 ▶ @k = map { $_ // '' } @k;
Condition50%1/2
$_ // ""
l!l
390
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
103 39 ▶ @k = sort @k if ( $c{sort_keys} );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"sort_keys"} 0 39 39
104 39 ▶ my $k = join( $;, @k ) // '';
Condition50%1/2
join($;, @k) // ""
l!l
390
MC/DC0%0/1
join($;, @k) // ""
join($;, @k)
Truth table50%1/2
A
A= join($;, @k)
Aresult
11
00
105  
106 39 ▶ if ( $uniq && !defined($vidx) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($uniq and not defined $vidx) { } 0 39 39
elsif (not defined $vidx) { } 0 39 39
elsif ($uniq) { } 13 26 39
Condition66%2/3
$uniq and not defined $vidx
!ll&&!rl&&r
26130
MC/DC0%0/2
$uniq and not defined $vidx
$uniq not defined $vidx
Truth table66%2/3
A and not(B)
A= $uniqB= not defined $vidx
ABresult
0X0
100
111
107 0 $map{$k} = 1;
108 } elsif ( not defined $vidx ) {
109 0 $map{$k}++;
110 } elsif ($uniq) {
111 13 ▶ confess "strict mode: two times the same key $k" if ( $is_strict && defined( $map{$k} ) );
Branch: 1 branch
BranchTrueFalseTotal
if $is_strict and defined $map{$k} 1 12 13
Condition100%3/3
$is_strict and defined $map{$k}
!ll&&!rl&&r
841
MC/DC100%2/2
$is_strict and defined $map{$k}
$is_strict defined $map{$k}
Truth table100%3/3
A and B
A= $is_strictB= defined $map{$k}
ABresult
0X0
100
111
112 12 ▶ $map{$k} = ( ref $vidx ? [ @r[@$vidx] ] : ( $vidx eq 'all' ? \@r : $r[$vidx] ) );
Branch: 2 branches
BranchTrueFalseTotal
ref $vidx ? : 3 9 12
$vidx eq 'all' ? : 0 9 9
113 } else {
114 26 ▶ $map{$k} //= [];
Condition100%2/2
$map{$k} //= []
l!l
224
MC/DC100%1/1
$map{$k} //= []
$map{$k}
Truth table100%2/2
A
A= $map{$k}
Aresult
11
00
115 26 ▶ push @{ $map{$k} }, ( ref $vidx ? [ @r[@$vidx] ] : ( $vidx eq 'all' ? \@r : $r[$vidx] ) );
Branch: 2 branches
BranchTrueFalseTotal
ref $vidx ? : 2 24 26
$vidx eq 'all' ? : 0 24 24
116 }
117 }
118 }
119  
120 7 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 2 5 7
121 7 ▶ return wantarray ? ( \%map, \@header ) : \%map;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 7 7
122 }
123  
124 sub dict_spew {
125 0 ▶ my ( $dest, $m, $c ) = @_;
Subroutine dict_spew: not called CC=8 SCAR=42.8
Pod undocumented
126  
127 0 ▶ my $uniq = $c->{uniq} // $c->{uniq_vals} // 0;
Condition0%0/5
($c->{"uniq"} // $c->{"uniq_vals"}) // 0
l!l
00
$c->{"uniq"} // $c->{"uniq_vals"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($c->{"uniq"} // $c->{"uniq_vals"}) // 0
$c->{"uniq"} $c->{"uniq_vals"}
Truth table0%0/3
A // B
A= $c->{"uniq"}B= $c->{"uniq_vals"}
ABresult
1X1
011
000
128  
129 0 my @flat;
130 0 ▶ while ( my ( $k, $vv ) = each %$m ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$m) 0 0 0
131 # $v => [ a, b, c] or $v => a or $v => [ [ a, b ], [c, d], ...]
132 0 ▶ $vv = [$vv] unless ( ref $vv );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vv 0 0 0
133 0 ▶ my $vals = [ map { ref $_ ? $_ : [$_] } @$vv ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
134 0 ▶ $vals = uniq_rows($vals) if ($uniq);
Branch: 1 branch
BranchTrueFalseTotal
if $uniq 0 0 0
135 0 for my $v (@$vals) {
136 0 push @flat, [ $k, @$v ];
137 }
138 }
139 0 return mspew( $dest, \@flat, $c );
140 }
141  
142 sub mslurp {
143 2 ▶ my ( $src, $cc ) = @_;
Subroutine mslurp: called CC=27 SCAR=46.5
Pod documented
144 2 my @m;
145  
146 2 my ( $fh, $fh_was_open ) = open_on_demand( $src, '<' );
147  
148 2 ▶ $cc //= {};
Condition100%2/2
$cc //= {}
l!l
11
MC/DC100%1/1
$cc //= {}
$cc
Truth table100%2/2
A
A= $cc
Aresult
11
00
149 2 my %c = (
150 sep => qr/\t/,
151 header => 0,
152 skip => -1,
153 row_names => 0,
154 comment => $COMMENT_RE,
155 commented_header => undef,
156 record_filter => undef,
157 col_idx => undef,
158 %$cc
159 );
160  
161 2 my $record_filter = $c{record_filter};
162  
163 2 my @col_idx;
164 2 ▶ @col_idx = @{ $c{col_idx} } if ( $c{col_idx} && ref $c{col_idx} eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"col_idx"} and ref $c{"col_idx"} eq "ARRAY" 1 1 2
Condition66%2/3
$c{"col_idx"} and ref $c{"col_idx"} eq "ARRAY"
!ll&&!rl&&r
101
MC/DC50%1/2
$c{"col_idx"} and ref $c{"col_idx"} eq "ARRAY"
$c{"col_idx"} ref $c{"col_idx"} eq "ARRAY"
Truth table66%2/3
A and B
A= $c{"col_idx"}B= ref $c{"col_idx"} eq "ARRAY"
ABresult
0X0
100
111
165 2 my @header;
166 my @row_names;
167  
168 2 ▶ if ( $c{header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{"header"}) 0 2 2
169 0 ▶ while ( my $raw_row = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $raw_row = readline $fh)) 0 0 0
170 0 ▶ if ( $c{comment} && $raw_row =~ /$c{comment}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{"comment"} and $raw_row =~ /$c{"comment"}/) 0 0 0
Condition0%0/3
$c{"comment"} and $raw_row =~ /$c{"comment"}/
!ll&&!rl&&r
000
MC/DC0%0/2
$c{"comment"} and $raw_row =~ /$c{"comment"}/
$c{"comment"} $raw_row =~ /$c{"comment"}/
Truth table0%0/3
A and B
A= $c{"comment"}B= $raw_row =~ /$c{"comment"}/
ABresult
0X0
100
111
171 0 ▶ if ( $c{commented_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c{'commented_header'}) { } 0 0 0
172 0 $raw_row =~ s/$c{comment}//;
173 } else {
174 0 next;
175 }
176 }
177  
178 0 $raw_row =~ s/\r\n/\n/;
179 0 chomp $raw_row;
180 0 @header = split /$c{sep}/, $raw_row;
181 0 last;
182 }
183 }
184  
185 2 my $lnum = 0;
186 2 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 4 2 6
187 4 ▶ next if ( $lnum++ <= $c{skip} );
Branch: 1 branch
BranchTrueFalseTotal
if $lnum++ <= $c{"skip"} 0 4 4
188 4 ▶ next if ( $c{comment} && /$c{comment}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"comment"} and /$c{"comment"}/ 0 4 4
Condition33%1/3
$c{"comment"} and /$c{"comment"}/
!ll&&!rl&&r
040
MC/DC0%0/2
$c{"comment"} and /$c{"comment"}/
$c{"comment"} /$c{"comment"}/
Truth table33%1/3
A and B
A= $c{"comment"}B= /$c{"comment"}/
ABresult
0X0
100
111
189 4 s/\r\n/\n/;
190 4 chomp;
191 4 ▶ next if (/^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 4 4
192  
193 4 ▶ next if ( $record_filter && !$record_filter->($_) );
Branch: 1 branch
BranchTrueFalseTotal
if $record_filter and not &$record_filter($_) 0 4 4
Condition33%1/3
$record_filter and not &$record_filter($_)
!ll&&!rl&&r
400
MC/DC0%0/2
$record_filter and not &$record_filter($_)
$record_filter not &$record_filter($_)
Truth table33%1/3
A and not(B)
A= $record_filterB= not &$record_filter($_)
ABresult
0X0
100
111
194  
195 4 my @row = split /$c{sep}/;
196  
197 4 ▶ push @row_names, shift @row if ( $c{row_names} );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"row_names"} 0 4 4
198  
199 4 ▶ push @m, ( @col_idx ? [ @row[@col_idx] ] : \@row );
Branch: 1 branch
BranchTrueFalseTotal
@col_idx ? : 2 2 4
200 }
201 2 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 2 2
202  
203 #remove first empty element of a header if same number of elements as first matrix element.
204 2 ▶ shift @header if ( $c{row_names} && $c{header} && @m > 0 && @{ $m[0] } == @header && !$header[0] );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"row_names"} and $c{"header"} and @m > 0 and @{$m[0];} == @header and not $header[0] 0 2 2
Condition33%4/12
$c{"row_names"} and $c{"header"} and @m > 0 and @{$m[0];} == @header and not $header[0]
!ll&&!rl&&r
200
$c{"row_names"} and $c{"header"} and @m > 0 and @{$m[0];} == @header
!ll&&!rl&&r
200
$c{"row_names"} and $c{"header"} and @m > 0
!ll&&!rl&&r
200
$c{"row_names"} and $c{"header"}
!ll&&!rl&&r
200
MC/DC0%0/5
$c{"row_names"} and $c{"header"} and @m > 0 and @{$m[0];} == @header and not $header[0]
$c{"row_names"} $c{"header"} @m > 0 @{$m[0];} == @header not $header[0]
Truth table16%1/6
A and B and C and D and not(E)
A= $c{"row_names"}B= $c{"header"}C= @m > 0D= @{$m[0];} == @headerE= not $header[0]
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
205  
206 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
207 0 ▶ return ( \@m, ( @header ? \@header : undef ), ( @row_names ? \@row_names : undef ) );
Branch: 2 branches
BranchTrueFalseTotal
@header ? : 0 0 0
@row_names ? : 0 0 0
208 } else {
209 2 return \@m;
210 }
211 }
212  
213 sub xcsv_slurp {
214 0 ▶ my ( $src, $c ) = @_;
Subroutine xcsv_slurp: not called CC=11 SCAR=48.8
Pod undocumented
215  
216 0 my ( $fh, $fh_was_open ) = open_on_demand( $src, '<' );
217  
218 0 ▶ my $data = do { local $/; <$fh> }
Branch: 1 branch
BranchTrueFalseTotal
my $data = do { local $/; readline $fh } or confess("No data to analyze\n") 0 0 0
219 or confess "No data to analyze\n";
220 0 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 0 0
221  
222 $c->{sep} //=
223 0 ▶ $data =~ m/["\d],["\d,]/ ? ","
Branch: 6 branches
BranchTrueFalseTotal
$data =~ /["\d],["\d,]/ ? : 0 0 0
$data =~ /["\d];["\d;]/ ? : 0 0 0
$data =~ /["\d]\t["\d]/ ? : 0 0 0
$data =~ /\w,[\w,]/ ? : 0 0 0
$data =~ /\w;[\w;]/ ? : 0 0 0
$data =~ /\w\t[\w]/ ? : 0 0 0
Condition0%0/3
$c->{"sep"} //= $data =~ /["\d],["\d,]/ ? "," : ($data =~ /["\d];["\d;]/ ? ";" : ($data =~ /["\d]\t["\d]/ ? "\t" : ($data =~ /\w,[\w,]/ ? "," : ($data =~ /\w;[\w;]/ ? ";" : ($data =~ /\w\t[\w]/ ? "\t" : ",")))))
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"sep"} //= $data =~ /["\d],["\d,]/ ? "," : ($data =~ /["\d];["\d;]/ ? ";" : ($data =~ /["\d]\t["\d]/ ? "\t" : ($data =~ /\w,[\w,]/ ? "," : ($data =~ /\w;[\w;]/ ? ";" : ($data =~ /\w\t[\w]/ ? "\t" : ",")))))
$c->{"sep"} $data =~ /["\d],["\d,]/ ? "," : ($data =~ /["\d];["\d;]/ ? ";" : ($data =~ /["\d]\t["\d]/ ? "\t" : ($data =~ /\w,[\w,]/ ? "," : ($data =~ /\w;[\w;]/ ? ";" : ($data =~ /\w\t[\w]/ ? "\t" : ",")))))
Truth table0%0/3
A //= B
A= $c->{"sep"}B= $data =~ /["\d],["\d,]/ ? "," : ($data =~ /["\d];["\d;]/ ? ";" : ($data =~ /["\d]\t["\d]/ ? "\t" : ($data =~ /\w,[\w,]/ ? "," : ($data =~ /\w;[\w;]/ ? ";" : ($data =~ /\w\t[\w]/ ? "\t" : ",")))))
ABresult
1X1
011
000
224 : $data =~ m/["\d];["\d;]/ ? ";"
225 : $data =~ m/["\d]\t["\d]/ ? "\t"
226 :
227 # If neither, then for unquoted strings
228 $data =~ m/\w,[\w,]/ ? ","
229 : $data =~ m/\w;[\w;]/ ? ";"
230 : $data =~ m/\w\t[\w]/ ? "\t"
231 : ",";
232 0 ▶ open my $dfh, '<', \$data or die "Can't open filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $dfh, "<", \$data or die "Can't open filehandle: $!" 0 0 0
233 0 my $aoa = csv( in => $dfh, sep_char => $c->{sep}, quote_char => '"', escape_char => '"' );
234 0 close $dfh;
235 0 return $aoa;
236 }
237  
238 sub miterate {
239 2 ▶ my ( $src, $cc ) = @_;
Subroutine miterate: called CC=2 SCAR=7.0
Pod undocumented
240 2 my ( $fh, $fh_was_open ) = open_on_demand( $src, '<' );
241  
242 2 ▶ $cc //= {};
Condition100%2/2
$cc //= {}
l!l
11
MC/DC100%1/1
$cc //= {}
$cc
Truth table100%2/2
A
A= $cc
Aresult
11
00
243 2 my %c = (
244 sep => qr/\t/,
245 skip => 0,
246 comment => $COMMENT_RE,
247 record_filter => undef,
248 %$cc
249 );
250  
251 2 my $record_filter = $c{record_filter};
252  
253 return sub {
254 5 ▶ while (<$fh>) {
Subroutine __ANON__: called CC=9 SCAR=22.4
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 8 2 10
255  
256 8 ▶ next if ( --$c{skip} >= 0 );
Branch: 1 branch
BranchTrueFalseTotal
if --$c{"skip"} >= 0 1 7 8
257  
258 7 ▶ next if ( $c{comment} && /$c{comment}/ );
Branch: 1 branch
BranchTrueFalseTotal
if $c{"comment"} and /$c{"comment"}/ 2 5 7
Condition66%2/3
$c{"comment"} and /$c{"comment"}/
!ll&&!rl&&r
052
MC/DC50%1/2
$c{"comment"} and /$c{"comment"}/
$c{"comment"} /$c{"comment"}/
Truth table66%2/3
A and B
A= $c{"comment"}B= /$c{"comment"}/
ABresult
0X0
100
111
259 5 s/\r\n/\n/;
260 5 chomp;
261 5 ▶ next if (/^\s*$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 2 3 5
262 3 ▶ next if ( $record_filter && !$record_filter->($_) );
Branch: 1 branch
BranchTrueFalseTotal
if $record_filter and not &$record_filter($_) 0 3 3
Condition33%1/3
$record_filter and not &$record_filter($_)
!ll&&!rl&&r
300
MC/DC0%0/2
$record_filter and not &$record_filter($_)
$record_filter not &$record_filter($_)
Truth table33%1/3
A and not(B)
A= $record_filterB= not &$record_filter($_)
ABresult
0X0
100
111
263  
264 3 my @row = split /$c{sep}/;
265 3 return \@row;
266  
267 }
268 2 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 2 2
269 2 return;
270 2 };
271 }
272  
273 sub miterate_hash {
274 0 ▶ my $mit = miterate(@_);
Subroutine miterate_hash: not called CC=1 SCAR=6.9
Pod undocumented
275  
276 0 my $header = $mit->();
277  
278 return sub {
279 0 ▶ my ( $from_idx, $to_idx ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
280  
281 0 my $row = $mit->();
282 0 ▶ return unless ($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
283  
284 0 ▶ if ( defined($from_idx) && defined($to_idx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from_idx and defined $to_idx) { } 0 0 0
Condition0%0/3
defined $from_idx and defined $to_idx
!ll&&!rl&&r
000
MC/DC0%0/2
defined $from_idx and defined $to_idx
defined $from_idx defined $to_idx
Truth table0%0/3
A and B
A= defined $from_idxB= defined $to_idx
ABresult
0X0
100
111
285 0 my @h = @{$header}[ $from_idx .. $to_idx ];
286 0 my @r = @{$row}[ $from_idx .. $to_idx ];
287 0 return { zip @h, @r };
288 } else {
289 0 return { zip @$header, @$row };
290 }
291  
292 0 }, $header;
293 }
294  
295 sub lspew {
296 0 ▶ my ( $dest, $l, $c ) = @_;
Subroutine lspew: not called CC=13 SCAR=52.0
Pod documented
297 0 ▶ my $delim = $c->{sep} // $c->{delim} // "\t";
Condition0%0/5
($c->{"sep"} // $c->{"delim"}) // "\t"
l!l
00
$c->{"sep"} // $c->{"delim"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($c->{"sep"} // $c->{"delim"}) // "\t"
$c->{"sep"} $c->{"delim"}
Truth table0%0/3
A // B
A= $c->{"sep"}B= $c->{"delim"}
ABresult
1X1
011
000
298 0 ▶ my $header = $c->{header} // $c->{ids};
Condition0%0/3
$c->{"header"} // $c->{"ids"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"header"} // $c->{"ids"}
$c->{"header"} $c->{"ids"}
Truth table0%0/3
A // B
A= $c->{"header"}B= $c->{"ids"}
ABresult
1X1
011
000
299 0 my ( $fh, $fh_was_open ) = open_on_demand( $dest, '>' );
300  
301 0 ▶ say $fh join( $delim, @$header ) if ( $header && @$header > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $header and @$header > 0 0 0 0
Condition0%0/3
$header and @$header > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$header and @$header > 0
$header @$header > 0
Truth table0%0/3
A and B
A= $headerB= @$header > 0
ABresult
0X0
100
111
302  
303 0 ▶ if ( ref $l eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $l eq 'HASH') { } 0 0 0
elsif (ref $l eq 'ARRAY') { } 0 0 0
304 0 ▶ while ( my ( $k, $v ) = each %$l ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$l) 0 0 0
305 0 ▶ if ( ref $v eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 0 0 0
306 0 say $fh join $delim, ( $k, @$v );
307 } else {
308 0 say $fh join $delim, ( $k, $v );
309 }
310 }
311 } elsif ( ref $l eq 'ARRAY' ) {
312 0 for my $v (@$l) {
313 0 ▶ if ( ref $v eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 0 0 0
314 0 say $fh join $delim, @$v;
315 } else {
316 0 say $fh $v;
317 }
318 }
319  
320 } else {
321 0 confess "need a reference for the list argument";
322 }
323 0 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 0 0
324  
325 0 return;
326 }
327  
328 sub lslurp {
329 0 ▶ my ($file) = @_;
Subroutine lslurp: not called CC=1 SCAR=6.9
Pod undocumented
330  
331 0 my @lines = slurpc($file);
332 0 return \@lines;
333 }
334  
335 sub mspew {
336 1 ▶ my ( $dest, $m, $c ) = @_;
Subroutine mspew: called CC=37 SCAR=57.6
Pod documented
337  
338 1 ▶ confess "no matrix, you need to supply a matrix of the form [ [ 1,2,3 ], [ 4,5,6 ], ... ]"
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 1 1
339 unless ($m);
340  
341 1 ▶ my $header = $c->{header} // $c->{ids};
Condition33%1/3
$c->{"header"} // $c->{"ids"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$c->{"header"} // $c->{"ids"}
$c->{"header"} $c->{"ids"}
Truth table33%1/3
A // B
A= $c->{"header"}B= $c->{"ids"}
ABresult
1X1
011
000
342 1 ▶ my $rownames = $c->{row_names} // $c->{rownames};
Condition33%1/3
$c->{"row_names"} // $c->{"rownames"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$c->{"row_names"} // $c->{"rownames"}
$c->{"row_names"} $c->{"rownames"}
Truth table33%1/3
A // B
A= $c->{"row_names"}B= $c->{"rownames"}
ABresult
1X1
011
000
343 1 my $square = $c->{square};
344 1 ▶ my $fill_rows = $c->{fill_rows} // 1;
Condition50%1/2
$c->{"fill_rows"} // 1
l!l
01
MC/DC0%0/1
$c->{"fill_rows"} // 1
$c->{"fill_rows"}
Truth table50%1/2
A
A= $c->{"fill_rows"}
Aresult
11
00
345 1 ▶ my $sep = $c->{sep} // "\t";
Condition50%1/2
$c->{"sep"} // "\t"
l!l
01
MC/DC0%0/1
$c->{"sep"} // "\t"
$c->{"sep"}
Truth table50%1/2
A
A= $c->{"sep"}
Aresult
11
00
346 1 ▶ my $na_value = $c->{missing} // $c->{na_value} // 'NA';
Condition40%2/5
($c->{"missing"} // $c->{"na_value"}) // "NA"
l!l
01
$c->{"missing"} // $c->{"na_value"}
l!l&&r!l&&!r
001
MC/DC0%0/2
($c->{"missing"} // $c->{"na_value"}) // "NA"
$c->{"missing"} $c->{"na_value"}
Truth table33%1/3
A // B
A= $c->{"missing"}B= $c->{"na_value"}
ABresult
1X1
011
000
347 1 my $quote_is_on = $c->{quote};
348  
349 # get the number of rows
350 1 my $num_rows = scalar @$m;
351 # find the longest column
352 1 ▶ my $num_cols = max - 1, map { defined $_ ? scalar @$_ : -1 } @$m;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
353  
354 # if the header is longer, it defines the number of cols
355 1 ▶ $num_cols = max $num_cols, scalar @$header if ( $header && @$header > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $header and @$header > 0 1 0 1
Condition33%1/3
$header and @$header > 0
!ll&&!rl&&r
001
MC/DC0%0/2
$header and @$header > 0
$header @$header > 0
Truth table33%1/3
A and B
A= $headerB= @$header > 0
ABresult
0X0
100
111
356  
357 1 ▶ $rownames = $header
Branch: 1 branch
BranchTrueFalseTotal
if $header and @$header > 0 and $rownames and not ref $rownames and @$header >= @$m 0 1 1
Condition33%4/12
$header and @$header > 0 and $rownames and not ref $rownames and @$header >= @$m
!ll&&!rl&&r
100
$header and @$header > 0 and $rownames and not ref $rownames
!ll&&!rl&&r
100
$header and @$header > 0 and $rownames
!ll&&!rl&&r
010
$header and @$header > 0
!ll&&!rl&&r
001
MC/DC0%0/5
$header and @$header > 0 and $rownames and not ref $rownames and @$header >= @$m
$header @$header > 0 $rownames not ref $rownames @$header >= @$m
Truth table16%1/6
A and B and C and not(D) and E
A= $headerB= @$header > 0C= $rownamesD= not ref $rownamesE= @$header >= @$m
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
358 if ( $header && @$header > 0 && $rownames && !ref $rownames && @$header >= @$m );
359  
360 # adjust num rows if rownames are longer than the
361 1 ▶ $num_rows = scalar @$rownames if ( ref $rownames eq 'ARRAY' && @$rownames > $num_rows );
Branch: 1 branch
BranchTrueFalseTotal
if ref $rownames eq "ARRAY" and @$rownames > $num_rows 0 1 1
Condition33%1/3
ref $rownames eq "ARRAY" and @$rownames > $num_rows
!ll&&!rl&&r
100
MC/DC0%0/2
ref $rownames eq "ARRAY" and @$rownames > $num_rows
ref $rownames eq "ARRAY" @$rownames > $num_rows
Truth table33%1/3
A and B
A= ref $rownames eq "ARRAY"B= @$rownames > $num_rows
ABresult
0X0
100
111
362  
363 1 ▶ if ($square) {
Branch: 1 branch
BranchTrueFalseTotal
if ($square) 0 1 1
364 0 ▶ $num_rows = $num_cols if ( $num_cols > $num_rows );
Branch: 1 branch
BranchTrueFalseTotal
if $num_cols > $num_rows 0 0 0
365 0 ▶ $num_cols = $num_rows if ( $num_rows > $num_cols );
Branch: 1 branch
BranchTrueFalseTotal
if $num_rows > $num_cols 0 0 0
366 #add one for the id in the first row
367 0 ▶ $num_cols++ if ($rownames);
Branch: 1 branch
BranchTrueFalseTotal
if $rownames 0 0 0
368 }
369  
370 1 ▶ confess "error with rownames: not an array"
Branch: 1 branch
BranchTrueFalseTotal
if $rownames and not ref $rownames eq "ARRAY" && @$rownames >= @$m 0 1 1
Condition16%1/6
$rownames and not ref $rownames eq "ARRAY" && @$rownames >= @$m
!ll&&!rl&&r
100
ref $rownames eq 'ARRAY' && @$rownames >= @$m
!ll&&!rl&&r
000
MC/DC0%0/3
$rownames and not ref $rownames eq "ARRAY" && @$rownames >= @$m
$rownames ref $rownames eq 'ARRAY' @$rownames >= @$m
Truth table25%1/4
A and not(B && C)
A= $rownamesB= ref $rownames eq 'ARRAY'C= @$rownames >= @$m
ABCresult
0XX0
1110
10X1
1101
371 if ( $rownames && !( ref $rownames eq 'ARRAY' && @$rownames >= @$m ) );
372 1 ▶ confess "no matrix" unless ( defined $m );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 1 1
373  
374 1 my ( $fh, $fh_was_open ) = open_on_demand( $dest, '>' );
375  
376 #print header if we have header
377 1 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 1 0 1
378 1 my @qhead = @{ _quote( $header, $quote_is_on, $na_value ) };
379 1 ▶ unshift @qhead, '' if ( $rownames && !$c->{r_style_header} );
Branch: 1 branch
BranchTrueFalseTotal
if $rownames and not $c->{"r_style_header"} 0 1 1
Condition33%1/3
$rownames and not $c->{"r_style_header"}
!ll&&!rl&&r
100
MC/DC0%0/2
$rownames and not $c->{"r_style_header"}
$rownames not $c->{"r_style_header"}
Truth table33%1/3
A and not(B)
A= $rownamesB= not $c->{"r_style_header"}
ABresult
0X0
100
111
380 1 say $fh join $sep, @qhead;
381 }
382  
383 #iterate through rows
384 1 ▶ for ( my $i = 0; $i < $num_rows; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_rows) 0 1 1
385 0 my @r;
386 #add rowname as first column if desired
387 0 ▶ push @r, ( $rownames->[$i] // "no_name" ) if ($rownames);
Branch: 1 branch
BranchTrueFalseTotal
if $rownames 0 0 0
Condition0%0/2
$rownames->[$i] // "no_name"
l!l
00
MC/DC0%0/1
$rownames->[$i] // "no_name"
$rownames->[$i]
Truth table0%0/2
A
A= $rownames->[$i]
Aresult
11
00
388 #add the values
389 0 ▶ push @r, @{ $m->[$i] // [] } if ( $i < @$m );
Branch: 1 branch
BranchTrueFalseTotal
if $i < @$m 0 0 0
Condition0%0/2
$m->[$i] // []
l!l
00
MC/DC0%0/1
$m->[$i] // []
$m->[$i]
Truth table0%0/2
A
A= $m->[$i]
Aresult
11
00
390 #fill the square if desired
391 0 ▶ if ( $square || $fill_rows ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($square or $fill_rows) 0 0 0
Condition0%0/3
$square or $fill_rows
l!l&&r!l&&!r
000
MC/DC0%0/2
$square or $fill_rows
$square $fill_rows
Truth table0%0/3
A or B
A= $squareB= $fill_rows
ABresult
1X1
011
000
392 0 my $missing = $num_cols - @r;
393 0 push @r, (undef) x $missing;
394 }
395 #print row
396 0 say $fh join $sep, @{ _quote( \@r, $quote_is_on, $na_value ) };
397 }
398 1 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 1 1
399 1 return;
400 }
401  
402 sub _quote {
403 1 ▶ my ( $f, $q, $na ) = @_;
Subroutine _quote: called CC=6 SCAR=22.9
404 1 ▶ $na = "$na" if ( $q && defined $na );
Branch: 1 branch
BranchTrueFalseTotal
if $q and defined $na 0 1 1
Condition33%1/3
$q and defined $na
!ll&&!rl&&r
100
MC/DC0%0/2
$q and defined $na
$q defined $na
Truth table33%1/3
A and B
A= $qB= defined $na
ABresult
0X0
100
111
405 my @fields = map {
406 1 ▶ if ( !defined ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_) { } 0 2 2
elsif (not $q or /^\d+$/) { } 2 0 2
407 0 $na;
408 ▶ } elsif ( !$q || /^\d+$/ ) {
Condition33%1/3
not $q or /^\d+$/
l!l&&r!l&&!r
200
MC/DC0%0/2
not $q or /^\d+$/
not $q /^\d+$/
Truth table33%1/3
not(A) or B
A= not $qB= /^\d+$/
ABresult
1X1
011
000
409 2 $_;
410 } else {
411 0 ( my $str = $_ ) =~ s/"/""/g;
412 0 qq{"$str"};
413 }
414 } @$f;
415 1 return \@fields;
416 }
417  
418 sub xlsx_spew {
419 0 ▶ my ( $dest, $m, $c ) = @_;
Subroutine xlsx_spew: not called CC=19 SCAR=59.4
Pod undocumented
420 #eval "use Excel::Writer::XLSX";
421 #die "could not load Excel::Writer::XLSX $@" if ($@);
422 0 ▶ eval "use Excel::Writer::XLSX; 1" or confess "could not load Excel::Writer::XLSX";
Branch: 1 branch
BranchTrueFalseTotal
eval "use Excel::Writer::XLSX; 1" or confess("could not load Excel::Writer::XLSX") 0 0 0
423  
424 0 ▶ my $header = $c->{header} // $c->{ids};
Condition0%0/3
$c->{"header"} // $c->{"ids"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"header"} // $c->{"ids"}
$c->{"header"} $c->{"ids"}
Truth table0%0/3
A // B
A= $c->{"header"}B= $c->{"ids"}
ABresult
1X1
011
000
425 0 ▶ my $rownames = $c->{row_names} // $c->{rownames};
Condition0%0/3
$c->{"row_names"} // $c->{"rownames"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->{"row_names"} // $c->{"rownames"}
$c->{"row_names"} $c->{"rownames"}
Truth table0%0/3
A // B
A= $c->{"row_names"}B= $c->{"rownames"}
ABresult
1X1
011
000
426 0 ▶ my $sep = $c->{sep} // "\t";
Condition0%0/2
$c->{"sep"} // "\t"
l!l
00
MC/DC0%0/1
$c->{"sep"} // "\t"
$c->{"sep"}
Truth table0%0/2
A
A= $c->{"sep"}
Aresult
11
00
427 0 ▶ my $na_value = $c->{missing} // $c->{na_value} // 'NA';
Condition0%0/5
($c->{"missing"} // $c->{"na_value"}) // "NA"
l!l
00
$c->{"missing"} // $c->{"na_value"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($c->{"missing"} // $c->{"na_value"}) // "NA"
$c->{"missing"} $c->{"na_value"}
Truth table0%0/3
A // B
A= $c->{"missing"}B= $c->{"na_value"}
ABresult
1X1
011
000
428  
429 0 ▶ $rownames = $header
Branch: 1 branch
BranchTrueFalseTotal
if $header and @$header > 0 and $rownames and not ref $rownames and @$header == @$m 0 0 0
Condition0%0/12
$header and @$header > 0 and $rownames and not ref $rownames and @$header == @$m
!ll&&!rl&&r
000
$header and @$header > 0 and $rownames and not ref $rownames
!ll&&!rl&&r
000
$header and @$header > 0 and $rownames
!ll&&!rl&&r
000
$header and @$header > 0
!ll&&!rl&&r
000
MC/DC0%0/5
$header and @$header > 0 and $rownames and not ref $rownames and @$header == @$m
$header @$header > 0 $rownames not ref $rownames @$header == @$m
Truth table0%0/6
A and B and C and not(D) and E
A= $headerB= @$header > 0C= $rownamesD= not ref $rownamesE= @$header == @$m
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
430 if ( $header && @$header > 0 && $rownames && !ref $rownames && @$header == @$m );
431  
432 0 my @table;
433 #print header if we have header
434 0 ▶ if ($header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header) 0 0 0
435 0 my @qhead = @$header;
436 0 ▶ unshift @qhead, '' if ($rownames);
Branch: 1 branch
BranchTrueFalseTotal
if $rownames 0 0 0
437 0 push @table, \@qhead;
438 }
439  
440 #iterate through rows
441 0 ▶ for ( my $i = 0; $i < @$m; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$m) 0 0 0
442 0 my @r;
443 #add rowname as first column if desired
444 0 ▶ push @r, $rownames->[$i] if ($rownames);
Branch: 1 branch
BranchTrueFalseTotal
if $rownames 0 0 0
445 #add the values
446 0 push @r, @{ $m->[$i] };
447 #fill the square if desired
448  
449 #print row
450 0 ▶ push @table, [ map { $_ // $na_value } @r ];
Condition0%0/3
$_ // $na_value
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ // $na_value
$_ $na_value
Truth table0%0/3
A // B
A= $_B= $na_value
ABresult
1X1
011
000
451 }
452  
453 0 my ( $fh, $fh_was_open ) = open_on_demand( $dest, '>' );
454  
455 0 my $workbook = Excel::Writer::XLSX->new($fh);
456 0 my $worksheet = $workbook->add_worksheet();
457 0 ▶ $worksheet->keep_leading_zeros() if ( $c->{keep_leading_zeros} );
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"keep_leading_zeros"} 0 0 0
458 0 $worksheet->write_col( 'A1', \@table );
459 0 $workbook->close;
460 0 ▶ $fh->close unless ($fh_was_open);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh_was_open 0 0 0
461 0 return;
462 }
463  
464 sub xlsx_slurp {
465 0 ▶ my ( $src, $cc ) = @_;
Subroutine xlsx_slurp: not called CC=8 SCAR=42.8
Pod undocumented
466 #my @m;
467 #my ( $fh, $fh_was_open ) = open_on_demand( $src, '<' );
468  
469 0 ▶ eval "use Spreadsheet::ParseXLSX; 1" or confess "could not load Spreadsheet::ParseXLSX";
Branch: 1 branch
BranchTrueFalseTotal
eval "use Spreadsheet::ParseXLSX; 1" or confess("could not load Spreadsheet::ParseXLSX") 0 0 0
470  
471 0 ▶ $src = expand_home($src) if ( !ref($src) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $src 0 0 0
472  
473 0 my $parser = Spreadsheet::ParseXLSX->new;
474 0 my $workbook = $parser->parse($src);
475 0 ▶ if ( !defined $workbook ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $workbook) 0 0 0
476 0 confess $parser->error(), ".\n";
477 }
478  
479 0 my @ws;
480 0 for my $worksheet ( $workbook->worksheets() ) {
481 0 my @w;
482 0 my ( $row_min, $row_max ) = $worksheet->row_range();
483 0 my ( $col_min, $col_max ) = $worksheet->col_range();
484 0 ▶ for ( my $i = $row_min; $i <= $row_max; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $row_max) 0 0 0
485 0 my @r;
486 0 ▶ for ( my $j = $col_min; $j <= $col_max; $j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $col_max) 0 0 0
487 0 my $e = $worksheet->get_cell( $i, $j );
488 0 ▶ push @r, ( defined($e) ? $e->unformatted : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $e ? : 0 0 0
489 }
490 0 push @w, \@r;
491 }
492 0 push @ws, \@w;
493 }
494 0 return \@ws;
495 }
496  
497 9 1;
498  
499 __END__
500  
501 =head1 NAME
502  
503 Bio::Gonzales::Matrix::IO - Library for simple matrix IO
504  
505  
506 =head1 SYNOPSIS
507  
508 use Bio::Gonzales::Matrix::IO qw(lspew mslurp lslurp mspew);
509  
510 =head1 DESCRIPTION
511  
512 Provides functions for common matrix/list IO.
513  
514 =head1 SUBROUTINES
515  
516 =over 4
517  
518 =item B<< dict_slurp($filename, \%options) >>
519  
520 %options = (
521 sep => qr/\t/,
522 header => 0,
523 skip => -1,
524 comment => qr/^\s*#/,
525 key_idx => 0,
526 val_idx => undef,
527 uniq => 0,
528 record_filter => undef,
529 concat_keys => 1,
530 strict => 0
531 );
532  
533 Setups:
534  
535 =over 4
536  
537 =item uniq = 1 && no val_idx => read in key_idx as hash and set values to 1
538  
539 =item uniq = 0 && no val_idx => read in key_idx as hash and set values to the count of keys
540  
541 =item uniq = 1 && val_idx => read into ( key => [ @values ], ...)
542  
543 =item uniq = 0 && val_idx => read into ( key => [ [ @values ], [ @more_values ] ], ...)
544  
545 =item concat_keys
546  
547 Concatenate the keys by C<< $; >>. If set to 0, key columns are taken in a
548 serial fashion and are merged to one big column.
549  
550 =item uniq = 1 && strict = 1 => confess if two times the same key occurs in the data.
551  
552 =back
553  
554 If key_idx is an array, the keys columns are joined by C<$;> to build the hash key.
555  
556  
557  
558 =item B<< mspew($filename, \@matrix, \%options) >>
559  
560 =item B<< mspew($filehandle, \@matrix, \%options) >>
561  
562 Save the values in C<@matrix> to a C<$filename> or C<$filehandle>. C<@matrix>
563 is an array of arrayrefs:
564  
565 @matrix = (
566 [ l11, l12, l13 ],
567 [ l21, l22, l23 ],
568 [ l31, l32, l33 ]
569 );
570  
571 Options:
572  
573 =over 4
574  
575 =item header / ids
576  
577 Supply a header. Same as
578  
579 mspew($file, [ \@header, @matrix ])
580  
581 =item row_names
582  
583 Supply row names or if not an array but true, use the header as row names
584  
585 mspew( $file, $matrix, { row_names => 1 } ); #use header
586 mspew( $file, $matrix, { row_names => [ 'row1', '...', 'rown' ] } ); #use supplied row names
587  
588  
589 =item fill_missing_cols
590  
591 If a row has less columns than the longest row of the matrix, fill it up with empty strings.
592  
593 =item na_value
594  
595 Use this value in case undefined values are found. Default is 'NA'.
596  
597 =item sep
598  
599 Set a separator for the output file
600  
601 =item square (default 1)
602  
603 Add empty columns to fill up to a square.
604  
605 =back
606  
607 =item B<< $matrix_ref = mslurp($file, \%config) >>
608  
609 =item B<< ($matrix_ref, $header_ref, $row_names_ref) = mslurp($file, \%config) >>
610  
611 Reads in the contents of C<$file> and puts it in a array of arrayrefs.
612  
613 You can set the delimiter via the configuration by supplying C<< { sep => qr/\t/ } >> as config hash.
614  
615 Further options with defaults:
616  
617 %config = (
618 sep => qr/\t/, # set column separator
619 header => 0, # parse header
620 skip => 0, # skip the first N lines (without header)
621 row_names => 0, # parse row names
622 comment => qr/^\s*#/ # the comment character
623 record_filter => undef # set a function to filter records
624 );
625
626 =item B<< lspew($fh_or_filename, $list, $config_options) >>
627  
628 spews out a list of values to a file. It can handle filenames and filehandles,
629 but if you supply a handle, you have to close it on your own. The C<$list> can
630 be a
631  
632 =over 4
633  
634 =item hash ref of array refs
635  
636 results in
637 keya avalue0 avalue1
638 keyb bvalue0 bvalue1
639 ...
640  
641 =item hash ref
642  
643 results in
644 keya valuea
645 keyb valueb
646 ...
647  
648 =item array ref
649  
650 results in
651 value0
652 value1
653 ...
654  
655 =back
656  
657 C<$config_options> is a hash ref. It can take the options:
658  
659 $config_options = {
660 delim => "\t",
661 };
662  
663  
664 =back
665  
666 =head1 SEE ALSO
667  
668 =head1 AUTHOR
669  
670 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
671  
672 =cut
« blib/lib/Bio/Gonzales/Seq.pm ↑ Summary blib/lib/Bio/Gonzales/Domain/Identification/HMMER.pm »