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

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