« blib/lib/auto/share/dist/Pheno-Ranker/diseases/hpo/scripts/hpo_disease_converter.pl
↑ Summary
lib/Pheno/Ranker/Compare/Remap.pm »
| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # A utility from Pheno-Ranker to convert a CSV to: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # 1 - Input file (JSON array of objects) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # 2 - Configuration file (needed for Pheno-Ranker) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Last Modified: Mar/21/2024 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # $VERSION taken from Pheno::Ranker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # Copyright (C) 2023-2024 Manuel Rueda - CNAG (manuel.rueda@cnag.eu) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # License: Artistic License 2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # If this program helps you in your research, please cite. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | use Getopt::Long qw(:config no_ignore_case); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use Pod::Usage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ### Main ##### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 5 | process_csv(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ############## | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | exit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 5 | ▶ | sub process_csv { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_csv: called CC=16 SCAR=32.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 5 | my $VERSION = '0.09'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 5 | my $array_sep = '\|'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Reading arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 'input|i=s' => \my $input, # string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 'primary-key-name=s' => \my $primary_key_name, # string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 'generate-primary-key' => \my $generate_primary_key, # flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 'separator|sep=s' => \my $sep, # str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 'array-separator=s' => \$array_sep, # str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 'output-dir=s' => \my $output_dir, # str | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 'help|?' => \my $help, # flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 'man' => \my $man, # flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 'debug=i' => \my $debug, # integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 'verbose|' => \my $verbose, # flag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | 'version|V' => sub { print "$0 Version $VERSION\n"; exit; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | ) or pod2usage(2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | pod2usage(1) if $help; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | pod2usage( -verbose => 2, -exitval => 0 ) if $man; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
$input and $input =~ /\.(csv|tsv)$/ and -f $input and -r _
$input and $input =~ /\.(csv|tsv)$/ and -f $input
$input and $input =~ /\.(csv|tsv)$/
MC/DC0%0/4
$input and $input =~ /\.(csv|tsv)$/ and -f $input and -r _
$input
$input =~ /\.(csv|tsv)$/
-f $input
-r _
Truth table20%1/5
A and B and C and D
A= $inputB= $input =~ /\.(csv|tsv)$/C= -f $inputD= -r _
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | -message => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | "Please specify a readable CSV|TSV input with --i <file.csv>\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ) unless ( $input && $input =~ m/\.(csv|tsv)$/ && -f $input && -r _ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 5 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$generate_primary_key and not $primary_key_name
MC/DC0%0/2
$generate_primary_key and not $primary_key_name
$generate_primary_key
not $primary_key_name
Truth table66%2/3
A and not(B)
A= $generate_primary_keyB= not $primary_key_name
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | -message => "Please specify a --primary-key-name <my_string>\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ) if ( $generate_primary_key && !$primary_key_name ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $output_dir and not -d $output_dir
MC/DC0%0/2
defined $output_dir and not -d $output_dir
defined $output_dir
not -d $output_dir
Truth table66%2/3
A and not(B)
A= defined $output_dirB= not -d $output_dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | -message => "Please specify a valid directory for --output-dir\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ) if ( defined $output_dir && !-d $output_dir ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $output_dir and not -w _
MC/DC0%0/2
defined $output_dir and not -w _
defined $output_dir
not -w _
Truth table66%2/3
A and not(B)
A= defined $output_dirB= not -w _
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | -message => "Please specify a writable directory for --output-dir\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ) if ( defined $output_dir && !-w _ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sep and length $sep != 1
MC/DC100%2/2
defined $sep and length $sep != 1
defined $sep
length $sep != 1
Truth table100%3/3
A and B
A= defined $sepB= length $sep != 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | -message => "Please specify a single-character --separator\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | ) if ( defined $sep && length($sep) != 1 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 4 | validate_array_separator($array_sep); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # Create object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 3 | my $csv = CSV2PhenoRanker->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | input => $input, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | primary_key_name => $primary_key_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | generate_primary_key => $generate_primary_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | output_dir => $output_dir, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | sep => $sep, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | array_sep => $array_sep, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | debug => $debug, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | verbose => $verbose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | # Run method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | $csv->run; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | sub validate_array_separator { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_array_separator: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | my $array_sep = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $array_sep and length $array_sep
MC/DC0%0/2
defined $array_sep and length $array_sep
defined $array_sep
length $array_sep
Truth table33%1/3
A and B
A= defined $array_sepB= length $array_sep
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | -message => "Please specify a non-empty --array-separator\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ) unless defined $array_sep && length($array_sep); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 4 | my $ok = eval { qr/$array_sep/; 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | pod2usage( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | -message => "Please specify a valid regular expression for --array-separator\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | -exitval => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ) unless $ok; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | package CSV2PhenoRanker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 5 | ▶ | use autodie; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 5 | ▶ | use feature qw(say); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 5 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | use File::Spec::Functions qw(catdir catfile); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 5 | ▶ | use YAML::XS qw(LoadFile DumpFile); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 5 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 5 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | my ( $class, $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 3 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | # Read the input file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | my ( $data, $arrays, $non_arrays ) = $self->read_csv(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | # Define output directory | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | my ( $name, $path, undef ) = fileparse( $self->{input}, qr/\.[^.]*/ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | ▶ | my $output_dir = $self->{output_dir} // $path; # Use defined-or operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"output_dir"} // $path
MC/DC0%0/2
$self->{"output_dir"} // $path
$self->{"output_dir"}
$path
Truth table33%1/3
A // B
A= $self->{"output_dir"}B= $path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | # Write data as JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | my $json_file = catfile( $output_dir, qq/${name}.json/ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | say "Writing <$json_file> " if $self->{verbose}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | write_json( { filepath => $json_file, data => $data } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | # Load the configuration file data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | my $config = $self->create_config( $arrays, $non_arrays ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | # Write the configuration file as YAML | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | my $yaml_file = catfile( $output_dir, qq/${name}_config.yaml/ ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | ▶ | say "Writing <$yaml_file> " if $self->{verbose}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | write_yaml( { filepath => $yaml_file, data => $config } ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | sub write_json { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_json: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | my $file = $arg->{filepath}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 1 | my $json_data = $arg->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | # Note that canonical DOES not match the order of nsort from Sort::Naturally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | my $json = JSON::XS->new->utf8->canonical->pretty->encode($json_data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 1 | path($file)->spew($json); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub write_yaml { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_yaml: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | my $arg = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | my $file = $arg->{filepath}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | my $json_data = $arg->{data}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | local $YAML::XS::Boolean = 'JSON::PP'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | DumpFile( $file, $json_data ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | sub read_csv { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_csv: called CC=15 SCAR=28.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | my $input = $self->{input}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 3 | my $primary_key_name = $self->{primary_key_name}; # has to be non-array | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 3 | my $generate_primary_key = $self->{generate_primary_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 3 | my $sep = $self->{sep}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | my $array_sep = $self->{array_sep}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 3 | my $array_sep_qr = qr/$array_sep/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | # Create a Text::CSV object with semicolon as the separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 3 | my $csv = Text::CSV_XS->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | binary => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | sep_char => define_separator( $input, $sep ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | auto_diag => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | # Open filehandle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 3 | open my $fh, '<:encoding(utf-8)', $input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # Parse the CSV data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | my $headers = $csv->getline($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | die "Input file <$input> does not contain a header row\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 3 | ▶ | unless $headers && @{$headers}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$headers and @{$headers;}
MC/DC0%0/2
$headers and @{$headers;}
$headers
@{$headers;}
Truth table33%1/3
A and B
A= $headersB= @{$headers;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # Get rid of problematic characters on headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 3 | $_ =~ tr/()//d for @$headers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | validate_headers($headers); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | # Add $primary_key_name to headers if $generate_primary_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | ▶ | if ($generate_primary_key) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | # Check that primary_key_name does not exist | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | die | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | "Primary key <$primary_key_name> already exists. Are you sure you need the <--generate-primary-key> flag?\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | ▶ | if ( grep { $_ eq $primary_key_name } @$headers ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | # Make it last element of the array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 1 | ▶ | push @$headers, $primary_key_name if $generate_primary_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | ##################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | # START READING CSV # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | ##################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | my ( @rows, %array, %non_array ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | my $count = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 1 | ▶ | say "Start reading <$input>" if $self->{verbose}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 1 | ▶ | while ( my $row = $csv->getline($fh) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | # Print if verbose | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | say "Reading row $count..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | ▶ | if ( $self->{verbose} && $count % 1_000 == 0 ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"verbose"} and $count % 1000 == 0
MC/DC0%0/2
$self->{"verbose"} and $count % 1000 == 0
$self->{"verbose"}
$count % 1000 == 0
Truth table33%1/3
A and B
A= $self->{"verbose"}B= $count % 1000 == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | # Add id if $generate_primary_key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 3 | ▶ | push @$row, 'PR_' . sprintf( "%08d", $count ) if $generate_primary_key; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | # Load data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | my %data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 3 | @data{@$headers} = @$row; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | # *** IMPORTANT *** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | # Columns can consist of arrays or strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | # Here we load all as strings and we re-format array fields a posteriori | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 3 | for my $key ( keys %data ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # Check array/non-array based on regex | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 12 | ▶ | $array{$key}++ if $data{$key} =~ $array_sep_qr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 3 | push @rows, \%data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 3 | $count++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | close $fh; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | ▶ | say "Reading <$input> completed!" if $self->{verbose}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | ################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | # END READING CSV # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | ################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | # Load array/non-array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | my @array = keys %array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # Filter the original array to exclude elements found in the @array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 1 | my @non_array = grep { !$array{$_} } @$headers; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # Re-arrange array variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | split_array_fields( \@rows, \@array, $array_sep ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | return ( \@rows, \@array, \@non_array ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | sub validate_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_headers: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | my $headers = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 3 | for my $header ( @{$headers} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | die "Input file contains an empty header after normalization\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $header and length $header
MC/DC50%1/2
defined $header and length $header
defined $header
length $header
Truth table66%2/3
A and B
A= defined $headerB= length $header
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | unless defined $header && length $header; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | die "Input file contains duplicated header <$header> after normalization\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 5 | ▶ | if $seen{$header}++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | ▶ | sub split_array_fields { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_array_fields: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | my ( $rows, $array, $array_sep ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | # Split array fields (comma-separated values) into an array_ref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # Modify the original data structure directly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 1 | for my $row (@$rows) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 3 | for my $key (@$array) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 3 | $row->{$key} = [ split /$array_sep/, $row->{$key} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | ▶ | sub create_config { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create_config: called CC=6 SCAR=19.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 1 | my ( $self, $array, $non_array ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | my $primary_key_name = $self->{primary_key_name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | my @arrays = sort @$array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | my @non_arrays = sort @$non_array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | # Convert arrays to hashes for quick look up | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | my %arrays_hash = map { $_ => 1 } @arrays; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | my %non_arrays_hash = map { $_ => 1 } @non_arrays; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Set primary key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | ▶ | if ( defined $primary_key_name ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | die "Primary-key <$primary_key_name> not found\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | unless ( exists $arrays_hash{$primary_key_name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | || exists $non_arrays_hash{$primary_key_name} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $arrays_hash{$primary_key_name} or exists $non_arrays_hash{$primary_key_name}
MC/DC0%0/2
exists $arrays_hash{$primary_key_name} or exists $non_arrays_hash{$primary_key_name}
exists $arrays_hash{$primary_key_name}
exists $non_arrays_hash{$primary_key_name}
Truth table33%1/3
A or B
A= exists $arrays_hash{$primary_key_name}B= exists $non_arrays_hash{$primary_key_name}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | ▶ | $primary_key_name = exists $non_arrays_hash{id} ? 'id' : $non_arrays[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Default for non-arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 1 | my $config = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | format => 'CSV', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | primary_key => $primary_key_name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | allowed_terms => [@non_arrays] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | # Update for arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | ▶ | if ( scalar @arrays ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # NB: Can't use $array more than once in the hash ref below. Need to deref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | $config->{indexed_terms} = [@arrays]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # @non_arrays, @arrays are sorted, but if we merge them we need to re-sort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | $config->{allowed_terms} = [ sort @non_arrays, @arrays ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | $config->{identity_paths} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | CSV => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 1 | my $val = { $_ => $_ }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 1 | $val | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } @arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 1 | return $config; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 3 | ▶ | sub define_separator { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
define_separator: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 3 | my ( $filepath, $sep ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | # Define split record separator from file extension | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 3 | my @exts = qw(.csv .tsv); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 3 | my ( $undef, undef, $ext ) = fileparse( $filepath, @exts ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # Defining separator character | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 3 | ▶ | my $separator = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | defined($sep) ? $sep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | : $ext eq '.csv' ? ',' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | : # Use comma for csv files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | $ext eq '.tsv' ? "\t" : # Use tab for tsv files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | ','; # Default to comma if no extension match or $sep is undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # Return separator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 3 | return $separator; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | csv2pheno-ranker: A script to convert a CSV to an input suitable for Pheno-Ranker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | csv2pheno-ranker -i <input.csv> [-options] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | Arguments: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | -i, --input <input.csv> CSV file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | -generate-primary-key Generates a primary key if absent. Use --primary-key-name to set its name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | -primary-key-name <name> Sets the name for the primary key. Must be a single, non-array field | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | -sep, --separator <char> Delimiter for CSV fields [auto: comma for .csv, tab for .tsv] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | -array-separator <char> Delimiter for nested arrays [|] (e.g., --array-separator ';' for semicolons) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | -output-dir <directory> Specify the directory where output files will be stored. If not specified, outputs will be placed in the same directory as the input file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | Generic Options: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | -debug <level> Print debugging (from 1 to 5, being 5 max) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | -h, --help Brief help message | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | -man Full documentation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | -v, --verbose Verbosity on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | -V, --version Print version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | Numerous tools exist for CSV to JSON conversion, but our focus here was on creating JSON specifically for C<Pheno-Ranker>. The script supports both basic CSV files and complex, comma-separated CSV files with nested fields, ensuring seamless C<Pheno-Ranker> integration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | The script will create both a JSON file and the configuration file for C<Pheno-Ranker>. Then, you can run C<Pheno-Ranker> as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | $ pheno-ranker -r my_csv.json --config --my_csv_config.yaml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | Note that we load all data in memory before dumping the JSON file. If you have a huge CSV (e.g.,>5M rows) please use a computer that has enough RAM. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | =head1 SUMMARY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | A script to convert a CSV to an input suitable for C<Pheno-Ranker> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | =head1 INSTALLATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | (only needed if you did not install C<Pheno-Ranker>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | $ cpanm --installdeps . | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | =head3 System requirements | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | * Ideally a Debian-based distribution (Ubuntu or Mint), but any other (e.g., CentOs, OpenSuse) should do as well. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | * Perl 5 (>= 5.10 core; installed by default in most Linux distributions). Check the version with "perl -v" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | * 1GB of RAM. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | * 1 core (it only uses one core per job). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | * At least 1GB HDD. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | =head1 HOW TO RUN CSV2PHENO-RANKER | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | The software requires a CSV file as the input and operates with default settings. By default, both the C<JSON> file and the configuration file will be created in the same directory as the input file, and will share the same basename. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | If you have columns with nested values make sure that you use C<--array-separator> to define the delimiting character (default is "|"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | If you want to change some parameters please take a look to the synopsis. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | B<Examples:> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | $ ./csv2pheno-ranker -i example.csv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | $ ./csv2pheno-ranker -i example.csv --generate-primary-key --primary-key-name ID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | $ ./csv2pheno-ranker -i example.csv --generate-primary-key --primary-key-name ID --output-dir /my-path --sep ';' --array-separator ',' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 COMMON ERRORS AND SOLUTIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | * Error message: Foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | Solution: Bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | * Error message: Foo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | Solution: Bar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | Written by Manuel Rueda, PhD. Info about CNAG can be found at L<https://www.cnag.eu>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | This Perl file is copyrighted. See the LICENSE file included in this distribution. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||