| 1 | package Convert::Pheno::Source::TablePackage; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 136 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 136 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 136 | ▶ | use Encode qw(decode FB_CROAK); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 136 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 136 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 136 | ▶ | use File::Find qw(find); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 136 | ▶ | use IO::Uncompress::Gunzip qw(gunzip $GunzipError); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 136 | ▶ | use IO::Uncompress::Unzip qw($UnzipError); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 136 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 136 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 136 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 136 | our @EXPORT_OK = qw(load_table_package normalize_table_name); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 21 | ▶ | sub load_table_package { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_table_package: called CC=19 SCAR=34.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 21 | my ( $converter, $profile ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 21 | ▶ | die "A table-package profile is required\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $profile and not ref $profile and length $profile
defined $profile and not ref $profile
MC/DC0%0/3
defined $profile and not ref $profile and length $profile
defined $profile
not ref $profile
length $profile
Truth table25%1/4
A and not(B) and C
A= defined $profileB= not ref $profileC= length $profile
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | unless defined $profile && !ref($profile) && length $profile; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | return _memory_package( $converter->{data}, $profile ) | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 21 | ▶ | if exists $converter->{data}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 17 | ▶ | my @inputs = @{ $converter->{in_files} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$converter->{'in_files'} || []
MC/DC0%0/1
$converter->{'in_files'} || []
$converter->{'in_files'}
Truth table50%1/2
A
A= $converter->{'in_files'}
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | push @inputs, $converter->{in_file} | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 17 | ▶ | if !@inputs && defined $converter->{in_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not @inputs and defined $converter->{"in_file"}
MC/DC0%0/2
not @inputs and defined $converter->{"in_file"}
not @inputs
defined $converter->{"in_file"}
Truth table33%1/3
not(A) and B
A= not @inputsB= defined $converter->{"in_file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 17 | ▶ | die "$profile input requires table files, a directory, or a ZIP package\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | unless @inputs; | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 17 | my @entries; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 17 | for my $input (@inputs) { | |||||||||||||||||||||||||||||||||||||||||
| 34 | 20 | ▶ | die "$profile input path is missing\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $input and not ref $input and length $input
defined $input and not ref $input
MC/DC0%0/3
defined $input and not ref $input and length $input
defined $input
not ref $input
length $input
Truth table25%1/4
A and not(B) and C
A= defined $inputB= not ref $inputC= length $input
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | unless defined $input && !ref($input) && length $input; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 20 | ▶ | if ( -d $input ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 13 | push @entries, @{ _directory_entries($input) }; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 13 | next; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | if ( -f $input && $input =~ /\.zip\z/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
-f $input and $input =~ /\.zip\z/i
MC/DC50%1/2
-f $input and $input =~ /\.zip\z/i
-f $input
$input =~ /\.zip\z/i
Truth table66%2/3
A and B
A= -f $inputB= $input =~ /\.zip\z/i
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | push @entries, @{ _zip_entries($input) }; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | if ( -f $input && _is_table_filename($input) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-f $input and _is_table_filename($input)
MC/DC0%0/2
-f $input and _is_table_filename($input)
-f $input
_is_table_filename($input)
Truth table33%1/3
A and B
A= -f $inputB= _is_table_filename($input)
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | push @entries, _file_entry($input); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | die "$profile input <$input> must be a CSV/TSV table, directory, or ZIP package\n"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 17 | ▶ | die "$profile input does not contain any CSV or TSV table files\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | unless @entries; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 17 | return _parse_entries( \@entries, $profile ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 106 | ▶ | sub normalize_table_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalize_table_name: called CC=7 SCAR=20.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 106 | my ( $name, $profile ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 106 | ▶ | $name = basename( defined $name ? $name : q{} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 106 | $name =~ s/\.gz\z//i; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 106 | $name =~ s/\.(?:csv|tsv|txt)\z//i; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 106 | $name = uc $name; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 106 | $name =~ s/[^A-Z0-9]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 106 | $name =~ s/^_+|_+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 106 | ▶ | my $profile_prefix = uc($profile // q{}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$profile // ""
MC/DC0%0/1
$profile // ""
$profile
Truth table50%1/2
A
A= $profile
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 106 | $profile_prefix =~ s/[^A-Z0-9]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 106 | ▶ | $name =~ s/^(?:${profile_prefix}|SCDM|CDM)_// if length $profile_prefix; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 106 | my %alias = ( | |||||||||||||||||||||||||||||||||||||||||
| 70 | i2b2 => { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | PATIENT => 'PATIENT_DIMENSION', | ||||||||||||||||||||||||||||||||||||||||||
| 72 | VISIT => 'VISIT_DIMENSION', | ||||||||||||||||||||||||||||||||||||||||||
| 73 | CONCEPT => 'CONCEPT_DIMENSION', | ||||||||||||||||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | sentinel => { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | PROCEDURES => 'PROCEDURE', | ||||||||||||||||||||||||||||||||||||||||||
| 77 | LAB_RESULT_CM => 'LAB_RESULT', | ||||||||||||||||||||||||||||||||||||||||||
| 78 | VITAL => 'VITAL_SIGNS', | ||||||||||||||||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | pcornet => { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | PROCEDURE => 'PROCEDURES', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | LAB_RESULT => 'LAB_RESULT_CM', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | VITAL_SIGNS => 'VITAL', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 106 | ▶ | my $key = lc($profile // q{}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$profile // ""
MC/DC0%0/1
$profile // ""
$profile
Truth table50%1/2
A
A= $profile
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | return $alias{$key}{$name} | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 106 | ▶ | if exists $alias{$key} && exists $alias{$key}{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $alias{$key} and exists $alias{$key}{$name}
MC/DC0%0/2
exists $alias{$key} and exists $alias{$key}{$name}
exists $alias{$key}
exists $alias{$key}{$name}
Truth table33%1/3
A and B
A= exists $alias{$key}B= exists $alias{$key}{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 106 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | sub _memory_package { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_memory_package: called CC=8 SCAR=21.9
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | my ( $data, $profile ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 94 | my $tables = ref($data) eq 'HASH' && ref( $data->{tables} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||
| 95 | ? $data->{tables} | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | ▶ | : $data; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $data eq "HASH" and ref $data->{"tables"} eq "HASH"
MC/DC0%0/2
ref $data eq "HASH" and ref $data->{"tables"} eq "HASH"
ref $data eq "HASH"
ref $data->{"tables"} eq "HASH"
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= ref $data->{"tables"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | die "$profile in-memory input must be an object keyed by table name\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | unless ref($tables) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | my ( %normalized, %sources ); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | for my $input_name ( sort keys %{$tables} ) { | |||||||||||||||||||||||||||||||||||||||||
| 102 | 8 | my $table = normalize_table_name( $input_name, $profile ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | die "$profile input contains table <$table> more than once\n" | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 8 | ▶ | if exists $normalized{$table}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | my $rows = $tables->{$input_name}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 8 | ▶ | die "$profile table <$table> must contain an array of row objects\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | unless ref($rows) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 108 | $normalized{$table} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 109 | map { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 6 | ▶ | die "$profile table <$table> must contain only row objects\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | unless ref($_) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 6 | _normalize_row( dclone($_), $table ) | |||||||||||||||||||||||||||||||||||||||||
| 113 | 8 | } @{$rows} | |||||||||||||||||||||||||||||||||||||||||
| 114 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | $sources{$table} = ["memory:$input_name"]; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | tables => \%normalized, | |||||||||||||||||||||||||||||||||||||||||
| 120 | sources => \%sources, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | kind => 'memory', | ||||||||||||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 13 | ▶ | sub _directory_entries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_directory_entries: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 13 | my ($directory) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 13 | my @files; | |||||||||||||||||||||||||||||||||||||||||
| 128 | find( | ||||||||||||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | no_chdir => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 114 | ▶ | wanted => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 114 | ▶ | push @files, $File::Find::name | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-f $File::Find::name and _is_table_filename($File::Find::name)
MC/DC100%2/2
-f $File::Find::name and _is_table_filename($File::Find::name)
-f $File::Find::name
_is_table_filename($File::Find::name)
Truth table100%3/3
A and B
A= -f $File::Find::nameB= _is_table_filename($File::Find::name)
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | if -f $File::Find::name | ||||||||||||||||||||||||||||||||||||||||||
| 134 | && _is_table_filename($File::Find::name); | ||||||||||||||||||||||||||||||||||||||||||
| 135 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 136 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 13 | $directory, | |||||||||||||||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 13 | return [ map { _file_entry($_) } sort @files ]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 94 | ▶ | sub _file_entry { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_entry: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 94 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | name => basename($file), | ||||||||||||||||||||||||||||||||||||||||||
| 146 | source => $file, | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 94 | ▶ | read => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 94 | ▶ | return _read_gzip($file) if $file =~ /\.gz\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 94 | return path($file)->slurp_raw; | |||||||||||||||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 94 | }; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 1 | ▶ | sub _zip_entries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_zip_entries: called CC=11 SCAR=27.2
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | my ($zip_file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | my @entries; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | my $zip = IO::Uncompress::Unzip->new($zip_file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | or die "Cannot open ZIP package <$zip_file>: $UnzipError\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 161 | 4 | my $header = $zip->getHeaderInfo; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 4 | my $name = $header->{Name}; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | ▶ | if ( defined $name && $name !~ m{/\z} && _is_table_filename($name) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $name and not $name =~ m[/\z] and _is_table_filename($name)
defined $name and not $name =~ m[/\z]
MC/DC0%0/3
defined $name and not $name =~ m[/\z] and _is_table_filename($name)
defined $name
not $name =~ m[/\z]
_is_table_filename($name)
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not $name =~ m[/\z]C= _is_table_filename($name)
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | my $bytes = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 4 | my $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 167 | 8 | my $read = $zip->read($buffer); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 8 | ▶ | die "Cannot read ZIP entry <$name>: $UnzipError\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $read or $read < 0
MC/DC0%0/2
not defined $read or $read < 0
not defined $read
$read < 0
Truth table33%1/3
not(A) or B
A= not defined $readB= $read < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | if !defined $read || $read < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 8 | ▶ | last if $read == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | $bytes .= $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | ▶ | if ( $name =~ /\.gz\z/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $uncompressed = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | gunzip \$bytes => \$uncompressed | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | or die "Cannot decompress ZIP entry <$name>: $GunzipError\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | $bytes = $uncompressed; | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | push @entries, { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | name => basename($name), | ||||||||||||||||||||||||||||||||||||||||||
| 181 | source => "$zip_file:$name", | ||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | ▶ | read => sub { return $bytes }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | ▶ | last unless $zip->nextStream; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | close $zip; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | return \@entries; | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 17 | ▶ | sub _parse_entries { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_entries: called CC=6 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 17 | my ( $entries, $profile ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 17 | my ( %tables, %headers, %sources ); | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 17 | for my $entry ( @{$entries} ) { | |||||||||||||||||||||||||||||||||||||||||
| 196 | 98 | my $table = normalize_table_name( $entry->{name}, $profile ); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 98 | ▶ | next unless length $table; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 98 | my $bytes = $entry->{read}->(); | |||||||||||||||||||||||||||||||||||||||||
| 199 | 98 | my $text = eval { decode( 'UTF-8', $bytes, FB_CROAK ) }; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 98 | ▶ | if ( my $error = $@ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | chomp $error; | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | die "$profile table <$entry->{source}> is not valid UTF-8: $error\n"; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | my ( $rows, $table_headers ) = _parse_table_text( | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 98 | $text, $entry->{name}, $entry->{source}, $table, | |||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 98 | ▶ | if ( exists $headers{$table} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | die "$profile table <$table> has inconsistent headers across input files\n" | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | unless join( "\x1f", @{ $headers{$table} } ) eq | |||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | join( "\x1f", @{$table_headers} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 98 | $headers{$table} = $table_headers; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 98 | push @{ $tables{$table} }, @{$rows}; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 98 | push @{ $sources{$table} }, $entry->{source}; | |||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 221 | 17 | tables => \%tables, | |||||||||||||||||||||||||||||||||||||||||
| 222 | sources => \%sources, | ||||||||||||||||||||||||||||||||||||||||||
| 223 | kind => 'files', | ||||||||||||||||||||||||||||||||||||||||||
| 224 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 98 | ▶ | sub _parse_table_text { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_table_text: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 98 | my ( $text, $name, $source, $table ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 98 | my $separator = _separator_for( $name, $text ); | |||||||||||||||||||||||||||||||||||||||||
| 230 | 98 | my $csv = Text::CSV_XS->new( | |||||||||||||||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||||||||||||||||
| 232 | binary => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 233 | auto_diag => 2, | ||||||||||||||||||||||||||||||||||||||||||
| 234 | sep_char => $separator, | ||||||||||||||||||||||||||||||||||||||||||
| 235 | empty_is_undef => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 236 | blank_is_undef => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 237 | allow_whitespace => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 240 | 98 | ▶ | open my $fh, '<', \$text | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | or die "Cannot read in-memory table <$source>: $!\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 242 | 98 | my $raw_headers = $csv->getline($fh); | |||||||||||||||||||||||||||||||||||||||||
| 243 | 98 | ▶ | die "Table <$source> has no header row\n" unless $raw_headers; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 98 | my @headers = map { _normalize_column($_) } @{$raw_headers}; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 98 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 98 | for my $header (@headers) { | |||||||||||||||||||||||||||||||||||||||||
| 248 | 538 | ▶ | die "Table <$source> contains an empty column name\n" unless length $header; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | die "Table <$source> contains duplicate normalized column <$header>\n" | ||||||||||||||||||||||||||||||||||||||||||
| 250 | 538 | ▶ | if $seen{$header}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 98 | $csv->column_names(@headers); | |||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | 98 | my @rows; | |||||||||||||||||||||||||||||||||||||||||
| 255 | 98 | ▶ | while ( my $row = $csv->getline_hr($fh) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 185 | push @rows, _normalize_row( $row, $table ); | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 98 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 98 | return ( \@rows, \@headers ); | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 191 | ▶ | sub _normalize_row { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_row: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 191 | my ( $row, $table ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 191 | my %normalized; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 191 | for my $raw_key ( keys %{$row} ) { | |||||||||||||||||||||||||||||||||||||||||
| 266 | 1040 | my $key = _normalize_column($raw_key); | |||||||||||||||||||||||||||||||||||||||||
| 267 | die "Table <$table> contains duplicate normalized column <$key>\n" | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 1040 | ▶ | if exists $normalized{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 1040 | my $value = $row->{$raw_key}; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 1040 | ▶ | die "Table <$table> column <$key> must contain scalar values\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | if ref($value); | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 1040 | ▶ | if ( defined $value ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | 1040 | $value =~ s/^\x{FEFF}//; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 1040 | $value =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 1040 | $normalized{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 191 | return \%normalized; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | 1578 | ▶ | sub _normalize_column { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_column: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | 1578 | my ($column) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 283 | 1578 | ▶ | $column = defined $column ? $column : q{}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 284 | 1578 | $column =~ s/^\x{FEFF}//; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 1578 | $column =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 1578 | $column = uc $column; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 1578 | $column =~ s/[^A-Z0-9]+/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 288 | 1578 | $column =~ s/^_+|_+$//g; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 1578 | return $column; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 98 | ▶ | sub _separator_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_separator_for: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | 98 | my ( $name, $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 294 | 98 | ▶ | return "\t" if $name =~ /\.tsv(?:\.gz)?\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 98 | my ($line) = $text =~ /\A(?:\x{FEFF})?([^\r\n]*)/; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 98 | my @candidate = ( ',', "\t", ';', '|' ); | |||||||||||||||||||||||||||||||||||||||||
| 298 | 98 | my $best = ','; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 98 | my $count = -1; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 98 | for my $separator (@candidate) { | |||||||||||||||||||||||||||||||||||||||||
| 301 | 392 | ▶ | my $found = () = ( $line // q{} ) =~ /\Q$separator\E/g; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$line // ""
MC/DC0%0/1
$line // ""
$line
Truth table50%1/2
A
A= $line
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 392 | ▶ | if ( $found > $count ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 303 | 98 | $best = $separator; | |||||||||||||||||||||||||||||||||||||||||
| 304 | 98 | $count = $found; | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||
| 307 | 98 | return $best; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | sub _read_gzip { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_gzip: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | ▶ | my $gz = IO::Uncompress::Gunzip->new($file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | or die "Cannot open gzip table <$file>: $GunzipError\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | my $bytes = q{}; | |||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | my $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my $read = $gz->read($buffer); | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | die "Cannot read gzip table <$file>: $GunzipError\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $read or $read < 0
MC/DC0%0/2
not defined $read or $read < 0
not defined $read
$read < 0
Truth table0%0/3
not(A) or B
A= not defined $readB= $read < 0
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | if !defined $read || $read < 0; | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | last if $read == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | $bytes .= $buffer; | |||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | close $gz; | |||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 111 | ▶ | sub _is_table_filename { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_table_filename: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | 111 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 111 | ▶ | return defined $file && $file =~ /\.(?:csv|tsv|txt)(?:\.gz)?\z/i; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $file && $file =~ /\.(?:csv|tsv|txt)(?:\.gz)?\z/i
MC/DC50%1/2
defined $file && $file =~ /\.(?:csv|tsv|txt)(?:\.gz)?\z/i
defined $file
$file =~ /\.(?:csv|tsv|txt)(?:\.gz)?\z/i
Truth table66%2/3
A && B
A= defined $fileB= $file =~ /\.(?:csv|tsv|txt)(?:\.gz)?\z/i
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 136 | 1; | |||||||||||||||||||||||||||||||||||||||||