lib/Convert/Pheno/Emit/OMOP.pm

filter: Statementstmt 95.8% 139 / 145 Branchbran 80.0% 40 / 50 Conditioncond 62.2% 28 / 45 MC/DCmcdc 32.0% 8 / 25 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 8 totaltotal 80.0% 233 / 291 CC 48 SCAR 20.4
CC
48
Cov
86%
omop_stream_targets_finalize
15.9
omop_stream_targets_write
10.1
_stream_output_path
7.3
CRAP 7.7
« lib/Convert/Pheno/Source.pm ↑ Summary lib/Convert/Pheno/ClinicalCDM/ToBFF.pm »
1 package Convert::Pheno::Emit::OMOP;
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  
8 136 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 136 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 136 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 136 ▶use Convert::Pheno::IO::Atomic qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 commit_staged_path
13 create_staged_path
14 discard_staged_path
15 136 );
16  
17 136 our @EXPORT_OK = qw(
18 dispatcher_open_stream_out
19 transform_item
20 finalize_stream_out
21 omop_dispatcher
22 omop_stream_targets_open
23 omop_stream_targets_write
24 omop_stream_targets_finalize
25 omop_streams_multiple_entities
26 );
27  
28 200 ▶sub dispatcher_open_stream_out {
Subroutine dispatcher_open_stream_out: called CC=3 SCAR=11.0
Pod undocumented
29 200 my ($self) = @_;
30 200 ▶ return unless ( $self->{method} eq 'omop2bff' && $self->{omop_cli} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"method"} eq "omop2bff" and $self->{"omop_cli"} 186 14 200
Condition100%3/3
$self->{"method"} eq "omop2bff" and $self->{"omop_cli"}
!ll&&!rl&&r
180614
MC/DC100%2/2
$self->{"method"} eq "omop2bff" and $self->{"omop_cli"}
$self->{"method"} eq "omop2bff" $self->{"omop_cli"}
Truth table100%3/3
A and B
A= $self->{"method"} eq "omop2bff"B= $self->{"omop_cli"}
ABresult
0X0
100
111
31  
32 14 my $fh = Convert::Pheno::open_filehandle( $self->{out_file}, 'a' );
33 14 say $fh "[";
34 14 return { fh => $fh, first => 1 };
35 }
36  
37 6000 ▶sub transform_item {
Subroutine transform_item: called CC=4 SCAR=14.0
Pod undocumented
38 6000 my ( $self, $method_result, $fh_out, $is_last_item, $json ) = @_;
39  
40 6000 ▶ $json //= JSON::XS->new->canonical->pretty;
Condition33%1/3
$json //= "JSON::XS"->new->canonical->pretty
l!l&&r!l&&!r
600000
MC/DC0%0/2
$json //= "JSON::XS"->new->canonical->pretty
$json "JSON::XS"->new->canonical->pretty
Truth table33%1/3
A //= B
A= $jsonB= "JSON::XS"->new->canonical->pretty
ABresult
1X1
011
000
41  
42 6000 my $out;
43  
44 6000 ▶ if ( $self->{method_ori} && $self->{method_ori} eq 'omop2pxf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'method_ori'} and $self->{'method_ori'} eq 'omop2pxf') { } 1000 5000 6000
Condition66%2/3
$self->{"method_ori"} and $self->{"method_ori"} eq "omop2pxf"
!ll&&!rl&&r
050001000
MC/DC50%1/2
$self->{"method_ori"} and $self->{"method_ori"} eq "omop2pxf"
$self->{"method_ori"} $self->{"method_ori"} eq "omop2pxf"
Truth table66%2/3
A and B
A= $self->{"method_ori"}B= $self->{"method_ori"} eq "omop2pxf"
ABresult
0X0
100
111
45 1000 my $pxf = Convert::Pheno::do_bff2pxf( $self, $method_result );
46 1000 $out = $json->encode($pxf);
47 }
48 else {
49 5000 $out = $json->encode($method_result);
50 }
51  
52 6000 chomp $out;
53 6000 print $fh_out $out;
54  
55 6000 return 1;
56 }
57  
58 12 ▶sub finalize_stream_out {
Subroutine finalize_stream_out: called CC=1 SCAR=0.0
Pod undocumented
59 12 my ($stream) = @_;
60 12 say { $stream->{fh} } "\n]";
61 12 close $stream->{fh};
62 12 return 1;
63 }
64  
65 2 ▶sub omop_dispatcher {
Subroutine omop_dispatcher: called CC=3 SCAR=11.1
Pod undocumented
66 2 my ( $self, $method_result, $json ) = @_;
67  
68 2 ▶ $json //= JSON::XS->new->canonical->pretty;
Condition33%1/3
$json //= "JSON::XS"->new->canonical->pretty
l!l&&r!l&&!r
200
MC/DC0%0/2
$json //= "JSON::XS"->new->canonical->pretty
$json "JSON::XS"->new->canonical->pretty
Truth table33%1/3
A //= B
A= $jsonB= "JSON::XS"->new->canonical->pretty
ABresult
1X1
011
000
69  
70 2 my $out;
71  
72 2 ▶ if ( $self->{method_ori} ne 'omop2pxf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'method_ori'} ne 'omop2pxf') { } 1 1 2
73 1 $out = $json->encode($method_result);
74 }
75 else {
76 1 my $pxf = Convert::Pheno::do_bff2pxf( $self, $method_result );
77 1 $out = $json->encode($pxf);
78 }
79 2 chomp $out;
80 2 return \$out;
81 }
82  
83 13899 ▶sub omop_streams_multiple_entities {
Subroutine omop_streams_multiple_entities: called CC=3 SCAR=11.3
Pod undocumented
84 13899 my ($self) = @_;
85 13899 ▶ my @entities = @{ $self->{entities} || ['individuals'] };
Condition50%1/2
$self->{'entities'} || ['individuals']
l!l
138990
MC/DC0%0/1
$self->{'entities'} || ['individuals']
$self->{'entities'}
Truth table50%1/2
A
A= $self->{'entities'}
Aresult
11
00
86 13899 ▶ return @entities != 1 || $entities[0] ne 'individuals';
Condition66%2/3
@entities != 1 || $entities[0] ne 'individuals'
l!l&&r!l&&!r
88013811
MC/DC50%1/2
@entities != 1 || $entities[0] ne 'individuals'
@entities != 1 $entities[0] ne 'individuals'
Truth table66%2/3
A || B
A= @entities != 1B= $entities[0] ne 'individuals'
ABresult
1X1
011
000
87 }
88  
89 45 ▶sub omop_stream_targets_open {
Subroutine omop_stream_targets_open: called CC=7 SCAR=19.5
Pod undocumented
90 45 my ($self) = @_;
91 return $self->{_omop_stream_targets}
92 45 ▶ if exists $self->{_omop_stream_targets};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_omop_stream_targets"} 39 6 45
93  
94 6 ▶ my @entities = @{ $self->{entities} || ['individuals'] };
Condition50%1/2
$self->{'entities'} || ['individuals']
l!l
60
MC/DC0%0/1
$self->{'entities'} || ['individuals']
$self->{'entities'}
Truth table50%1/2
A
A= $self->{'entities'}
Aresult
11
00
95 6 my %targets;
96  
97 6 my $ok = eval {
98 6 for my $entity (@entities) {
99 11 my $path = _stream_output_path( $self, $entity );
100 11 my $staged = create_staged_path($path);
101 10 my $fh = Convert::Pheno::open_filehandle( $staged, 'w' );
102 10 $targets{$entity} = {
103 fh => $fh,
104 path => $path,
105 staged => $staged,
106 };
107 }
108 5 1;
109 };
110  
111 6 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 5 6
112 1 my $error = $@;
113 1 for my $target ( values %targets ) {
114 1 ▶ eval { close $target->{fh} if defined fileno( $target->{fh} ) };
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $target->{"fh"} 1 0 1
115 1 eval { discard_staged_path( $target->{staged} ) };
116 }
117 1 die $error;
118 }
119  
120 5 $self->{_omop_stream_targets} = \%targets;
121 5 $self->{_omop_stream_seen} = {};
122 5 return $self->{_omop_stream_targets};
123 }
124  
125 41 ▶sub omop_stream_targets_write {
Subroutine omop_stream_targets_write: called CC=10 SCAR=23.1
Pod undocumented
126 41 my ( $self, $result ) = @_;
127 41 my $targets = omop_stream_targets_open($self);
128 41 my $json = JSON::XS->new->canonical;
129 41 ▶ my $seen = $self->{_omop_stream_seen} ||= {};
Condition50%1/2
$self->{"_omop_stream_seen"} ||= {}
l!l
410
MC/DC0%0/1
$self->{"_omop_stream_seen"} ||= {}
$self->{"_omop_stream_seen"}
Truth table50%1/2
A
A= $self->{"_omop_stream_seen"}
Aresult
11
00
130  
131 41 ▶ if ( blessed($result) && $result->can('entities') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->can("entities")) 37 4 41
Condition66%2/3
builtin::blessed($result) and $result->can("entities")
!ll&&!rl&&r
4037
MC/DC50%1/2
builtin::blessed($result) and $result->can("entities")
builtin::blessed($result) $result->can("entities")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->can("entities")
ABresult
0X0
100
111
132 37 for my $entity ( keys %{$targets} ) {
133 74 for my $entry ( @{ $result->entities($entity) } ) {
134 53 ▶ next if _stream_entity_entry_seen( $seen, $entity, $entry );
Branch: 1 branch
BranchTrueFalseTotal
if _stream_entity_entry_seen($seen, $entity, $entry) 3 50 53
135 50 print { $targets->{$entity}{fh} } $json->encode($entry), "\n";
136 }
137 }
138 37 return 1;
139 }
140  
141 4 ▶ return 1 unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 3 4
142 3 ▶ return 1 unless exists $targets->{individuals};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $targets->{"individuals"} 0 3 3
143 3 ▶ return 1 if _stream_entity_entry_seen( $seen, 'individuals', $result );
Branch: 1 branch
BranchTrueFalseTotal
if _stream_entity_entry_seen($seen, "individuals", $result) 1 2 3
144  
145 2 print { $targets->{individuals}{fh} } $json->encode($result), "\n";
146 2 return 1;
147 }
148  
149 6 ▶sub omop_stream_targets_finalize {
Subroutine omop_stream_targets_finalize: called CC=13 SCAR=27.7
Pod undocumented
150 6 my ( $self, $commit ) = @_;
151 6 ▶ $commit = 1 unless defined $commit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $commit 2 4 6
152 6 ▶ return 1 unless exists $self->{_omop_stream_targets};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_omop_stream_targets"} 1 5 6
153  
154 5 my @errors;
155 5 for my $entity ( keys %{ $self->{_omop_stream_targets} } ) {
156 9 my $target = $self->{_omop_stream_targets}{$entity};
157 9 my $ok = eval {
158 9 ▶ close $target->{fh} if defined fileno( $target->{fh} );
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $target->{"fh"} 9 0 9
159 9 1;
160 };
161 9 ▶ push @errors, $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 9 9
162 }
163  
164 5 ▶ $commit = 0 if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 5 5
165 5 for my $entity ( sort keys %{ $self->{_omop_stream_targets} } ) {
166 9 my $target = $self->{_omop_stream_targets}{$entity};
167 9 ▶ if ($commit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($commit) { } 7 2 9
168 7 my $ok = eval {
169 7 commit_staged_path( $target->{staged}, $target->{path} );
170 7 1;
171 };
172 7 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 7 7
173 0 push @errors, $@;
174 0 my $discarded = eval {
175 0 discard_staged_path( $target->{staged} );
176 0 1;
177 };
178 0 ▶ push @errors, $@ unless $discarded;
Branch: 1 branch
BranchTrueFalseTotal
unless $discarded 0 0 0
179 0 $commit = 0;
180 }
181 }
182 else {
183 2 my $ok = eval {
184 2 discard_staged_path( $target->{staged} );
185 2 1;
186 };
187 2 ▶ push @errors, $@ unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
188 }
189 }
190  
191 5 delete $self->{_omop_stream_targets};
192 5 delete $self->{_omop_stream_seen};
193  
194 5 ▶ die join q{}, @errors if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 5 5
195 5 return 1;
196 }
197  
198 56 ▶sub _stream_entity_entry_seen {
Subroutine _stream_entity_entry_seen: called CC=6 SCAR=18.2
199 56 my ( $seen, $entity, $entry ) = @_;
200 return 0
201 unless $entity eq 'individuals'
202 && ref($entry) eq 'HASH'
203 && exists $entry->{id}
204 56 ▶ && defined $entry->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $entity eq "individuals" and ref $entry eq "HASH" and exists $entry->{"id"} and defined $entry->{"id"} 37 19 56
Condition66%6/9
$entity eq "individuals" and ref $entry eq "HASH" and exists $entry->{"id"} and defined $entry->{"id"}
!ll&&!rl&&r
37019
$entity eq "individuals" and ref $entry eq "HASH" and exists $entry->{"id"}
!ll&&!rl&&r
37019
$entity eq "individuals" and ref $entry eq "HASH"
!ll&&!rl&&r
37019
MC/DC25%1/4
$entity eq "individuals" and ref $entry eq "HASH" and exists $entry->{"id"} and defined $entry->{"id"}
$entity eq "individuals" ref $entry eq "HASH" exists $entry->{"id"} defined $entry->{"id"}
Truth table40%2/5
A and B and C and D
A= $entity eq "individuals"B= ref $entry eq "HASH"C= exists $entry->{"id"}D= defined $entry->{"id"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
205  
206 19 ▶ return 1 if $seen->{$entity}{ $entry->{id} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen->{$entity}{$entry->{"id"}}++ 4 15 19
207 15 return 0;
208 }
209  
210 11 ▶sub _stream_output_path {
Subroutine _stream_output_path: called CC=7 SCAR=19.9
211 11 my ( $self, $entity ) = @_;
212  
213 11 ▶ if ( !omop_streams_multiple_entities($self)
Branch: 1 branch
BranchTrueFalseTotal
if (not omop_streams_multiple_entities($self) and $entity eq "individuals" and defined $self->{"out_file"} and length $self->{"out_file"}) 1 10 11
Condition66%6/9
not omop_streams_multiple_entities($self) and $entity eq "individuals" and defined $self->{"out_file"} and length $self->{"out_file"}
!ll&&!rl&&r
1001
not omop_streams_multiple_entities($self) and $entity eq "individuals" and defined $self->{"out_file"}
!ll&&!rl&&r
1001
not omop_streams_multiple_entities($self) and $entity eq "individuals"
!ll&&!rl&&r
1001
MC/DC25%1/4
not omop_streams_multiple_entities($self) and $entity eq "individuals" and defined $self->{"out_file"} and length $self->{"out_file"}
not omop_streams_multiple_entities($self) $entity eq "individuals" defined $self->{"out_file"} length $self->{"out_file"}
Truth table40%2/5
not(A) and B and C and D
A= not omop_streams_multiple_entities($self)B= $entity eq "individuals"C= defined $self->{"out_file"}D= length $self->{"out_file"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
214 && $entity eq 'individuals'
215 && defined $self->{out_file}
216 && length $self->{out_file} )
217 {
218 1 return $self->{out_file};
219 }
220  
221 10 ▶ if ( exists $self->{output_name_overrides}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"output_name_overrides"} and exists $self->{"output_name_overrides"}{$entity}) 2 8 10
Condition66%2/3
exists $self->{"output_name_overrides"} and exists $self->{"output_name_overrides"}{$entity}
!ll&&!rl&&r
082
MC/DC50%1/2
exists $self->{"output_name_overrides"} and exists $self->{"output_name_overrides"}{$entity}
exists $self->{"output_name_overrides"} exists $self->{"output_name_overrides"}{$entity}
Truth table66%2/3
A and B
A= exists $self->{"output_name_overrides"}B= exists $self->{"output_name_overrides"}{$entity}
ABresult
0X0
100
111
222 && exists $self->{output_name_overrides}{$entity} )
223 {
224 2 return $self->{output_name_overrides}{$entity};
225 }
226  
227 8 return $self->{out_dir} . q{/} . $entity . '.json';
228 }
229  
230 136 1;
« lib/Convert/Pheno/Source.pm ↑ Summary lib/Convert/Pheno/ClinicalCDM/ToBFF.pm »