lib/Convert/Pheno/Sink/FileSet.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 87.5% 14 / 16 Conditioncond 81.2% 13 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 4 totaltotal 88.9% 129 / 145 CC 17 SCAR 13.1
CC
17
Cov
95%
write_result
8.0
new
5.8
resolve_entity_output_file
3.0
CRAP 3.7
« api/perl/json_bridge.pl ↑ Summary lib/Convert/Pheno/Source/CDISC/ODM.pm »
1 package Convert::Pheno::Sink::FileSet;
2  
3 102 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 102 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 102 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 102 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 102 ▶use Convert::Pheno::IO::Atomic qw(write_atomically);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 102 ▶use Convert::Pheno::IO::CSVHandler qw(get_headers write_csv);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 102 ▶use Convert::Pheno::IO::FileIO qw(io_yaml_or_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 102 ▶use Convert::Pheno::OMOP::Definitions qw($omop_headers);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 102 ▶use Convert::Pheno::Operations qw(conversion_spec);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 102 our @EXPORT_OK = qw(
16 resolve_entity_output_file
17 resolve_omop_table_output_file
18 );
19  
20 74 ▶sub new {
Subroutine new: called CC=5 SCAR=17.5
Pod undocumented
21 74 my ( $class, $arg ) = @_;
22 74 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
740
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
23 die "FileSet sink requires a conversion request hash\n"
24 74 ▶ unless ref( $arg->{request} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg->{"request"} eq "HASH" 0 74 74
25  
26 return bless {
27 request => $arg->{request},
28 out_file => $arg->{out_file},
29 0 ▶ before_write => $arg->{before_write} || sub { return 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 39 ▶ on_write => $arg->{on_write} || sub { return 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 74 ▶ }, $class;
Condition66%4/6
$arg->{'before_write'} || sub { return 1; }
l!l&&r!l&&!r
51230
$arg->{'on_write'} || sub { return 1; }
l!l&&r!l&&!r
51230
MC/DC0%0/2
$arg->{'before_write'} || sub { return 1; }
$arg->{'before_write'} sub { return 1; }
MC/DC0%0/2
$arg->{'on_write'} || sub { return 1; }
$arg->{'on_write'} sub { return 1; }
Truth table66%2/3
A || B
A= $arg->{'before_write'}B= sub { return 1; }
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= $arg->{'on_write'}B= sub { return 1; }
ABresult
1X1
011
000
32 }
33  
34 74 ▶sub write_result {
Subroutine write_result: called CC=8 SCAR=20.8
Pod undocumented
35 74 my ( $self, %arg ) = @_;
36 74 my $request = $self->{request};
37  
38 74 ▶ if ( $arg{bundle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg{"bundle"}) 24 50 74
39 24 for my $entity ( @{ $request->{entities} } ) {
40 46 my $path = resolve_entity_output_file( $request, $entity );
41 46 $self->{on_write}->($path);
42 46 _write_structured( $path, $arg{bundle}->entities($entity) );
43 }
44 24 return 1;
45 }
46  
47 50 my $data = $arg{data};
48 50 ▶ return 1 unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 7 43 50
49  
50 my $spec = conversion_spec( $request->{method} )
51 43 ▶ or die "Unsupported conversion <$request->{method}> in output sink\n";
Branch: 1 branch
BranchTrueFalseTotal
my $spec = conversion_spec($request->{"method"}) or die qq[Unsupported conversion <$request->{"method"}> in output sink\n] 43 0 43
52  
53 43 ▶ if ( $spec->{target} eq 'csv' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"target"} eq "csv") 5 38 43
54 5 my $path = $self->{out_file};
55 5 $self->{on_write}->($path);
56 write_atomically(
57 $path,
58 5 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
59 5 my ($staged) = @_;
60 write_csv(
61 {
62 5 ▶ sep => $request->{sep} // ';',
Condition100%2/2
$request->{"sep"} // ";"
l!l
14
MC/DC100%1/1
$request->{"sep"} // ";"
$request->{"sep"}
Truth table100%2/2
A
A= $request->{"sep"}
Aresult
11
00
63 filepath => $staged,
64 headers => get_headers($data),
65 data => $data,
66 }
67 );
68 }
69 5 );
70 5 return 1;
71 }
72  
73 38 ▶ if ( $spec->{target} eq 'omop' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"target"} eq "omop") 4 34 38
74 4 for my $table ( sort keys %{$data} ) {
75 13 my $path = resolve_omop_table_output_file( $request, $table );
76 13 $self->{before_write}->($path);
77 13 $self->{on_write}->($path);
78 13 my @headers = @{ $omop_headers->{$table} };
79 write_atomically(
80 $path,
81 13 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 13 my ($staged) = @_;
83 write_csv(
84 {
85 sep => ';',
86 filepath => $staged,
87 headers => \@headers,
88 13 data => $data->{$table},
89 }
90 );
91 }
92 13 );
93 }
94 4 return 1;
95 }
96  
97 34 my $path = $self->{out_file};
98 34 $self->{on_write}->($path);
99 34 _write_structured( $path, $data );
100 34 return 1;
101 }
102  
103 64 ▶sub resolve_entity_output_file {
Subroutine resolve_entity_output_file: called CC=3 SCAR=11.0
Pod undocumented
104 64 my ( $request, $entity ) = @_;
105 return $request->{output_name_overrides}{$entity}
106 if exists $request->{output_name_overrides}
107 64 ▶ && exists $request->{output_name_overrides}{$entity};
Branch: 1 branch
BranchTrueFalseTotal
if exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$entity} 5 59 64
Condition100%3/3
exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$entity}
!ll&&!rl&&r
5725
MC/DC100%2/2
exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$entity}
exists $request->{"output_name_overrides"} exists $request->{"output_name_overrides"}{$entity}
Truth table100%3/3
A and B
A= exists $request->{"output_name_overrides"}B= exists $request->{"output_name_overrides"}{$entity}
ABresult
0X0
100
111
108 59 return catfile( $request->{out_dir}, $entity . '.json' );
109 }
110  
111 14 ▶sub resolve_omop_table_output_file {
Subroutine resolve_omop_table_output_file: called CC=3 SCAR=11.0
Pod undocumented
112 14 my ( $request, $table ) = @_;
113 return $request->{output_name_overrides}{$table}
114 if exists $request->{output_name_overrides}
115 14 ▶ && exists $request->{output_name_overrides}{$table};
Branch: 1 branch
BranchTrueFalseTotal
if exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$table} 1 13 14
Condition100%3/3
exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$table}
!ll&&!rl&&r
1031
MC/DC100%2/2
exists $request->{"output_name_overrides"} and exists $request->{"output_name_overrides"}{$table}
exists $request->{"output_name_overrides"} exists $request->{"output_name_overrides"}{$table}
Truth table100%3/3
A and B
A= exists $request->{"output_name_overrides"}B= exists $request->{"output_name_overrides"}{$table}
ABresult
0X0
100
111
116 13 return catfile( $request->{out_dir}, $table . '.csv' );
117 }
118  
119 80 ▶sub _write_structured {
Subroutine _write_structured: called CC=1 SCAR=0.0
120 80 my ( $path, $data ) = @_;
121 return write_atomically(
122 $path,
123 80 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
124 80 my ($staged) = @_;
125 80 io_yaml_or_json(
126 {
127 filepath => $staged,
128 mode => 'write',
129 data => $data,
130 }
131 );
132 }
133 80 );
134 }
135  
136 102 1;
« api/perl/json_bridge.pl ↑ Summary lib/Convert/Pheno/Source/CDISC/ODM.pm »