| 1 | package Convert::Pheno::Tabular::REDCap::Dictionary; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 138 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 138 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 138 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 138 | ▶ | use File::Basename qw(fileparse); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 138 | ▶ | use IO::Uncompress::Gunzip qw($GunzipError); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 138 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 14 | ▶ | sub from_file { | ||||||||||||||||||||||||||
|
Subroutine
from_file: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 14 | my ( $class, $filepath ) = @_; | |||||||||||||||||||||||||||
| 12 | 14 | my $separator = _default_separator_for_path($filepath); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 14 | my $key = 'Variable / Field Name'; | |||||||||||||||||||||||||||
| 15 | 14 | my $labels = 'Choices, Calculations, OR Slider Labels'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 14 | my $csv = Text::CSV_XS->new( | |||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | sep_char => $separator, | ||||||||||||||||||||||||||||
| 20 | binary => 1, | ||||||||||||||||||||||||||||
| 21 | auto_diag => 1, | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 14 | my $fh = _open_filehandle($filepath); | |||||||||||||||||||||||||||
| 26 | 14 | my $headers = $csv->getline($fh); | |||||||||||||||||||||||||||
| 27 | 14 | $csv->column_names(@$headers); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 14 | my $rows = {}; | |||||||||||||||||||||||||||
| 30 | 14 | ▶ | while ( my $row = $csv->getline_hr($fh) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4695 | $row->{_labels} = _parse_choice_labels( $row->{$labels} ); | |||||||||||||||||||||||||||
| 32 | 4695 | $rows->{ $row->{$key} } = $row; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 14 | close $fh; | |||||||||||||||||||||||||||
| 36 | 14 | return bless { rows => $rows }, $class; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 108366 | ▶ | sub field_meta { | ||||||||||||||||||||||||||
|
Subroutine
field_meta: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 108366 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 41 | 108366 | return $self->{rows}{$field}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | sub field_label { | ||||||||||||||||||||||||||
|
Subroutine
field_label: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 1 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | my $meta = $self->field_meta($field) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | return $meta->{'Field Label'}; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 5570 | ▶ | sub field_note { | ||||||||||||||||||||||||||
|
Subroutine
field_note: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | 5570 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 52 | 5570 | ▶ | my $meta = $self->field_meta($field) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 5570 | return $meta->{'Field Note'}; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 79825 | ▶ | sub choice_labels { | ||||||||||||||||||||||||||
|
Subroutine
choice_labels: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 79825 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 58 | 79825 | ▶ | my $meta = $self->field_meta($field) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 54305 | return $meta->{_labels}; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 14934 | ▶ | sub choice_label { | ||||||||||||||||||||||||||
|
Subroutine
choice_label: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 14934 | my ( $self, $field, $code ) = @_; | |||||||||||||||||||||||||||
| 64 | 14934 | ▶ | my $labels = $self->choice_labels($field) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 14934 | return $labels->{$code}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 64891 | ▶ | sub has_choice_labels { | ||||||||||||||||||||||||||
|
Subroutine
has_choice_labels: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 64891 | my ( $self, $field ) = @_; | |||||||||||||||||||||||||||
| 70 | 64891 | ▶ | return defined $self->choice_labels($field) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub as_hashref { | ||||||||||||||||||||||||||
|
Subroutine
as_hashref: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 75 | 0 | return $self->{rows}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 14 | ▶ | sub _default_separator_for_path { | ||||||||||||||||||||||||||
|
Subroutine
_default_separator_for_path: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 79 | 14 | my ($filepath) = @_; | |||||||||||||||||||||||||||
| 80 | 14 | my @exts = map { $_, $_ . '.gz' } qw(.csv .tsv .txt); | |||||||||||||||||||||||||||
| 81 | 14 | my ( undef, undef, $ext ) = fileparse( $filepath, @exts ); | |||||||||||||||||||||||||||
| 82 | 14 | ▶ | return ( $ext eq '.csv' || $ext eq '.csv.gz' ) ? ';' : "\t"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ext eq ".csv" or $ext eq ".csv.gz"
MC/DC0%0/2
$ext eq ".csv" or $ext eq ".csv.gz"
$ext eq ".csv"
$ext eq ".csv.gz"
Truth table66%2/3
A or B
A= $ext eq ".csv"B= $ext eq ".csv.gz"
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 14 | ▶ | sub _open_filehandle { | ||||||||||||||||||||||||||
|
Subroutine
_open_filehandle: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 86 | 14 | my ($filepath) = @_; | |||||||||||||||||||||||||||
| 87 | 14 | ▶ | return IO::Uncompress::Gunzip->new( $filepath, MultiStream => 1 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | if $filepath =~ /\.gz$/; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 13 | open my $fh, '<:encoding(UTF-8)', $filepath; | |||||||||||||||||||||||||||
| 91 | 13 | return $fh; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 4695 | ▶ | sub _parse_choice_labels { | ||||||||||||||||||||||||||
|
Subroutine
_parse_choice_labels: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||
| 95 | 4695 | my $value = shift; | |||||||||||||||||||||||||||
| 96 | 4695 | ▶ | return undef unless $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 11273 | my @choices = grep { length } | |||||||||||||||||||||||||||
| 99 | 2691 | map { _trim($_) } split /\s*\|\s*/, $value; # perlcritic Severity: 5 | |||||||||||||||||||||||||||
| 100 | 2691 | ▶ | return undef unless @choices; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2691 | my %labels; | |||||||||||||||||||||||||||
| 103 | 2691 | for my $choice (@choices) { | |||||||||||||||||||||||||||
| 104 | 11201 | my ( $code, $label ) = split /\s*,\s*/, $choice, 2; | |||||||||||||||||||||||||||
| 105 | 11201 | ▶ | return undef unless defined $label; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 11104 | $code = _trim($code); | |||||||||||||||||||||||||||
| 108 | 11104 | $label = _trim($label); | |||||||||||||||||||||||||||
| 109 | 11104 | ▶ | return undef unless length($code) && length($label); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $code and length $label
MC/DC0%0/2
length $code and length $label
length $code
length $label
Truth table33%1/3
A and B
A= length $codeB= length $label
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 11104 | $labels{$code} = $label; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2594 | return \%labels; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 33481 | ▶ | sub _trim { | ||||||||||||||||||||||||||
|
Subroutine
_trim: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 118 | 33481 | my $value = shift; | |||||||||||||||||||||||||||
| 119 | 33481 | ▶ | return q{} unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 33481 | $value =~ s/^\s+//; | |||||||||||||||||||||||||||
| 121 | 33481 | $value =~ s/\s+$//; | |||||||||||||||||||||||||||
| 122 | 33481 | return $value; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 138 | 1; | |||||||||||||||||||||||||||