| 1 | package Convert::Pheno::CSV; | ||||||||||
| 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 | 136 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 136 | ▶ | use feature qw(say); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 136 | ▶ | use JSON::XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 136 | ▶ | use Hash::Fold fold => { array_delimiter => ':' }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 136 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 136 | our @EXPORT = qw(do_bff2csv do_pxf2csv); | |||||||||
| 11 | |||||||||||
| 12 | #$Data::Dumper::Sortkeys = 1; | ||||||||||
| 13 | |||||||||||
| 14 | 136 | my $JSON = JSON::XS->new->canonical; | |||||||||
| 15 | |||||||||||
| 16 | ############### | ||||||||||
| 17 | ############### | ||||||||||
| 18 | # BFF2CSV # | ||||||||||
| 19 | ############### | ||||||||||
| 20 | ############### | ||||||||||
| 21 | |||||||||||
| 22 | 66 | ▶ | sub do_bff2csv { | ||||||||
|
Subroutine
do_bff2csv: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 23 | 66 | my ( $self, $bff ) = @_; | |||||||||
| 24 | |||||||||||
| 25 | # Premature return | ||||||||||
| 26 | 66 | ▶ | return unless defined($bff); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | |||||||||||
| 28 | # Flatten the hash to 1D | ||||||||||
| 29 | 66 | my $csv = fold($bff); | |||||||||
| 30 | 66 | _normalize_folded_csv_values($csv); | |||||||||
| 31 | |||||||||||
| 32 | # Return the flattened hash | ||||||||||
| 33 | 66 | return $csv; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | ############### | ||||||||||
| 37 | ############### | ||||||||||
| 38 | # PXF2CSV # | ||||||||||
| 39 | ############### | ||||||||||
| 40 | ############### | ||||||||||
| 41 | |||||||||||
| 42 | 92 | ▶ | sub do_pxf2csv { | ||||||||
|
Subroutine
do_pxf2csv: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 43 | 92 | my ( $self, $pxf ) = @_; | |||||||||
| 44 | |||||||||||
| 45 | # Premature return | ||||||||||
| 46 | 92 | ▶ | return unless defined($pxf); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | |||||||||||
| 48 | # Flatten the hash to 1D | ||||||||||
| 49 | 92 | my $csv = fold($pxf); | |||||||||
| 50 | 92 | _normalize_folded_csv_values($csv); | |||||||||
| 51 | |||||||||||
| 52 | # Return the flattened hash | ||||||||||
| 53 | 92 | return $csv; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 158 | ▶ | sub _normalize_folded_csv_values { | ||||||||
|
Subroutine
_normalize_folded_csv_values: called CC=6 SCAR=17.9
| |||||||||||
| 57 | 158 | my ($row) = @_; | |||||||||
| 58 | |||||||||||
| 59 | # CSV output cannot preserve nested Perl refs. When Hash::Fold leaves a | ||||||||||
| 60 | # boolean/object/array/hash at the leaf, convert it to a JSON literal so | ||||||||||
| 61 | # headers stay stable and the value is still inspectable by the user. | ||||||||||
| 62 | 158 | for my $key ( keys %{$row} ) { | |||||||||
| 63 | 15235 | ▶ | next unless ref $row->{$key}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 687 | my $type = ref $row->{$key}; | |||||||||
| 65 | |||||||||||
| 66 | 687 | ▶ | if ( $type =~ /::Boolean$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 679 | ▶ | $row->{$key} = $row->{$key} ? 'true' : 'false'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 679 | next; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 8 | my $encoded = eval { $JSON->encode( $row->{$key} ) }; | |||||||||
| 72 | 8 | ▶ | $row->{$key} = defined $encoded ? $encoded : q{}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 158 | return $row; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 136 | 1; | |||||||||