CohortExplorer/Command/Query/Compare.pm

filter: Statementstmt 6.5% 14 / 213 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 40.0% 4 / 10 Podpod 100% 6 / 6 totaltotal 6.0% 24 / 396 CC 89 SCAR 69.5
CC
89
Cov
4%
create_query_params
1980.0
process_result
462.0
process_table
240.0
CRAP 1043.9
↑ Summary CohortExplorer/Application.pm »
1 package CohortExplorer::Command::Query::Compare;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 0.14;
7  
8 1 ▶use base qw(CohortExplorer::Command::Query);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use CLI::Framework::Exceptions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #-------
12 # Command is only available to longitudinal datasources
13 0 ▶sub usage_text {
Subroutine usage_text: not called CC=1 SCAR=6.9
Pod documented
14 0 q\
15 compare [--out|o=<directory>] [--export|e=<table>] [--export-all|a] [--save-command|s] [--stats|S] [--cond|c=<cond>]
16 [variable] : compare entities across visits with/without conditions on variables
17  
18  
19 NOTES
20 The variables entity_id and visit (if applicable) must not be provided as arguments as they are already part of the
21 query-set. However, the user can impose conditions on both variables.
22  
23 Other variables in arguments/cond (option) must be referenced as <table>.<variable> or <visit>.<table>.<variable>
24 where visit can be vAny, vLast, v1, v2, v3 ... vMax. Here vMax is the maximum visit number for which data is
25 available.
26  
27 Conditions can be imposed using the operators: =, !=, >, <, >=, <=, between, not_between, like, not_like, ilike, in,
28 not_in, regexp and not_regexp. The keyword undef can be used to specify null.
29  
30 When condition is imposed on variable with no prefix such as vAny, vLast, v1, v2 and v3 the command assumes the
31 condition applies to all visits of the variable.
32  
33 The directory specified in 'out' option must have RWX enabled (i.e. chmod 777) for CohortExplorer.
34  
35  
36 EXAMPLES
37 compare --out=/home/user/exports --stats --save-command --cond=v1.CER.Score='>, 20' v1.SC.Date
38  
39 compare --out=/home/user/exports --export=CER --cond=SD.Sex='=, Male' v1.CER.Score v3.DIS.Status
40
41 compare --out=/home/user/exports --export=CER --cond=v2.CER.Score'!=, undef' vLast.DIS.Status
42  
43 compare -o/home/user/exports -Ssa -c vLast.CER.Score='in, 25, 30, 40' DIS.Status
44  
45 compare -o/home/user/exports -eCER -eSD -c vLast.CER.Score='between, 25, 30' DIS.Status
46  
47 \;
48 }
49  
50 0 ▶sub get_valid_variables {
Subroutine get_valid_variables: not called CC=1 SCAR=6.9
Pod documented
51 0 my ($self) = @_;
52 0 my $ds = $self->cache->get('cache')->{datasource};
53 0 return [ 'entity_id', keys %{ $ds->variable_info },
54 0 @{ $ds->visit_variables } ];
55 }
56  
57 0 ▶sub create_query_params {
Subroutine create_query_params: not called CC=44 SCAR=75.9
Pod documented
58 0 my ( $self, $opts, @args ) = @_;
59 0 my ( $ds, $csv ) = @{ $self->cache->get('cache') }{qw/datasource csv/};
60 0 my $visit_info = $ds->visit_info;
61 0 my $dialect = $ds->dialect;
62 0 my $struct = $ds->entity_structure;
63 0 my %map = @{ $struct->{-columns} };
64 0 ▶ my $aliase_in_having = $dialect->aliase_in_having || undef;
Condition0%0/2
$dialect->aliase_in_having || undef
l!l
00
MC/DC0%0/1
$dialect->aliase_in_having || undef
$dialect->aliase_in_having
Truth table0%0/2
A
A= $dialect->aliase_in_having
Aresult
11
00
65 my $visit = $dialect->aggregate( "DISTINCT $map{visit} "
66 0 ▶ . ( $struct->{-order_by} ? " ORDER BY $struct->{-order_by} " : '' ), '@@' );
Branch: 1 branch
BranchTrueFalseTotal
$struct->{'-order_by'} ? : 0 0 0
67
68 0 my ( @vars, %param );
69 # Extract all variables from args/cond (option) except
70 # entity_id and visit as they are dealt separately
71 0 my $visit_regex = join( '|', map { $visit_info->{$_}{name} } keys %$visit_info );
72
73 0 for my $v ( @args, keys %{ $opts->{cond} } ) {
74 0 $v =~ s/^(?:vLast|vAny|$visit_regex)\.//;
75 0 ▶ if ( !grep ( $_ eq $v, ( 'entity_id', 'visit', @vars ) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_ eq $v), ("entity_id", "visit", @vars))) 0 0 0
76 0 push @vars, $v;
77 }
78 }
79
80 0 for my $var (@vars) {
81 ##---- BUILD 'WHERE' FOR TABLES AND VARIABLES ----##
82 0 my ( $t, $v ) = @{ $ds->variable_info->{$var} }{qw/table variable/};
83 0 my $table_type = $ds->table_info->{$t}{__type__};
84
85 0 push @{ $param{$table_type}{-where}{ $map{table} }{-in} }, $t;
86 0 push @{ $param{$table_type}{-where}{ $map{variable} }{-in} }, $v;
87
88 my $col_sql = 'CAST( NULLIF( '
89 . $dialect->aggregate(
90 (
91 ( $table_type eq 'static' ? 'DISTINCT' : '' )
92 . " CASE WHEN CONCAT( $map{table}, '.', $map{variable} ) "
93 . (
94 $table_type eq 'static' ? " = '$var' " : " = '$var' AND $map{visit} = ? " )
95 . " THEN TRIM( $map{value} ) ELSE NULL END "
96 )
97 )
98 . ", '' ) AS "
99 0 ▶ . $ds->variable_info->{$var}{__type__} . ' )';
Branch: 2 branches
BranchTrueFalseTotal
$table_type eq 'static' ? : 0 0 0
$table_type eq 'static' ? : 0 0 0
100
101 0 ▶ if ( $table_type eq 'static' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_type eq 'static') { } 0 0 0
102 0 push @{ $param{$table_type}{-columns} }, $var => $col_sql;
103 }
104
105 else {
106 # Each column corresponds to one visit variable
107 0 for my $i ( keys %$visit_info ) {
108 0 ( my $vv = $col_sql ) =~ s/\?/'$i'/;
109 0 push @{ $param{$table_type}{-columns} },
110 "$visit_info->{$i}{name}.$var" => $vv;
111 }
112 }
113
114 ##---- BUILD 'HAVING' FOR CONDITIONS ON VARIABLES AND VISIT VARIABLES ----##
115 0 ▶ if ( $table_type eq 'static' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_type eq 'static') { } 0 0 0
116 0 ▶ if ( $opts->{cond}
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"cond"} and $opts->{"cond"}{$var} and $csv->parse($opts->{"cond"}{$var})) 0 0 0
Condition0%0/6
$opts->{"cond"} and $opts->{"cond"}{$var} and $csv->parse($opts->{"cond"}{$var})
!ll&&!rl&&r
000
$opts->{"cond"} and $opts->{"cond"}{$var}
!ll&&!rl&&r
000
MC/DC0%0/3
$opts->{"cond"} and $opts->{"cond"}{$var} and $csv->parse($opts->{"cond"}{$var})
$opts->{"cond"} $opts->{"cond"}{$var} $csv->parse($opts->{"cond"}{$var})
Truth table0%0/4
A and B and C
A= $opts->{"cond"}B= $opts->{"cond"}{$var}C= $csv->parse($opts->{"cond"}{$var})
ABCresult
0XX0
10X0
1100
1111
117 && $opts->{cond}{$var}
118 && $csv->parse( $opts->{cond}{$var} ) )
119 {
120  
121 # Set condition on static variable (i.e. variable from static table)
122 0 my ( $opr, @conds ) = grep ( s/^\s*|\s*$//g, $csv->fields );
123 0 ▶ my $alias = $aliase_in_having ? "`$var`" : $col_sql;
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
124 0 ▶ $param{$table_type}{-having}{$alias} =
Condition0%0/3
$dialect->$opr || $opr
l!l&&r!l&&!r
000
MC/DC0%0/2
$dialect->$opr || $opr
$dialect->$opr $opr
Truth table0%0/3
A || B
A= $dialect->$oprB= $opr
ABresult
1X1
011
000
125 { ( $dialect->$opr || $opr ) => \@conds };
126 }
127 }
128
129 else {
130  
131 # Build conditions for visit variables e.g. v1.var, vLast.var, vAny.var etc.
132 # Values inside array references are joined as 'OR' and hashes as 'AND'
133 my @visit_vars =
134 0 grep( /^((vAny|vLast|$visit_regex)\.$var|$var)$/, keys %{ $opts->{cond} } );
135
136 0 tie my %vv_col, 'Tie::IxHash', @{ $param{$table_type}{-columns} };
137
138 0 for my $vv (@visit_vars) {
139 0 my (@conds, $opr);
140 0 ▶ if ( $csv->parse( $opts->{cond}{$vv} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->parse($opts->{"cond"}{$vv})) 0 0 0
141 0 ( $opr, @conds ) = grep ( s/^\s*|\s*$//g, $csv->fields );
142 0 ▶ $opr = $dialect->$opr || $opr;
Condition0%0/3
$dialect->$opr || $opr
l!l&&r!l&&!r
000
MC/DC0%0/2
$dialect->$opr || $opr
$dialect->$opr $opr
Truth table0%0/3
A || B
A= $dialect->$oprB= $opr
ABresult
1X1
011
000
143 }
144 # Last visits (i.e. vLast) for entities are not known in advance so practically any
145 # visit can be the last visit for any entity
146 0 ▶ if ( $vv =~ /^vLast\.$var$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($vv =~ /^vLast\.$var$/) { } 0 0 0
elsif ($vv =~ /^vAny\.$var$/) { } 0 0 0
elsif ($vv =~ /^(?:$visit_regex)\.$var$/) { } 0 0 0
147 0 ▶ if ( defined $param{$table_type}{-having}{-or} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{$table_type}{'-having'}{'-or'}) { } 0 0 0
148 # Get all visit representations of the variable from %vv_col
149 0 my @c = grep { $_ =~ /\.$var$/ } keys %vv_col;
150
151 0 for ( 0 .. $#{ $param{$table_type}{-having}{-or} } ) {
152 0 ▶ my $alias = $aliase_in_having ? "`$c[$_]`" : $vv_col{ $c[$_] };
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
153 0 ${ $param{$table_type}{-having}{-or} }[$_]->{$alias} =
154 { $opr => \@conds };
155 }
156 }
157
158 else {
159 0 ▶ my $lv = $aliase_in_having ? '`vLast`' : $dialect->substring( $visit, '@@', -1 );
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
160 0 my @lvc;
161
162 0 for ( keys %$visit_info ) {
163 0 my $vc = "$visit_info->{$_}{name}.$var";
164 push @lvc,
165 {
166 $lv => { -ident => "'$_'" },
167 (
168 $aliase_in_having
169 ? "`$vc`"
170 0 ▶ : $vv_col{"$vc"}
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
171 ) => { $opr => \@conds }
172 };
173 }
174 0 $param{$table_type}{-having}{-or} = \@lvc;
175 }
176 }
177  
178 # vAny includes all visit variables joined as 'OR'
179 elsif ( $vv =~ /^vAny\.$var$/ ) {
180 0 my @avc;
181
182 0 for ( keys %$visit_info ) {
183 0 my $vc = "$visit_info->{$_}{name}.$var";
184 0 ▶ my $alias = $aliase_in_having ? "`$vc`" : $vv_col{"$vc"};
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
185 0 push @avc, $alias => { $opr => \@conds };
186 }
187
188 0 ▶ if ( defined $param{$table_type}{-having}{-and} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{$table_type}{'-having'}{'-and'}) { } 0 0 0
189 0 push @{ $param{$table_type}{-having}{-and} }, \@avc;
190 }
191 else {
192 0 $param{$table_type}{-having}{-and} = [ { -or => \@avc } ];
193 }
194 }
195  
196 # Individual visits (v1.var, v2.var, v3.var etc.)
197 elsif ( $vv =~ /^(?:$visit_regex)\.$var$/ ) {
198 my ( $vv_opr, @vv_conds ) = grep ( s/^\s*|\s*$//g, $csv->fields )
199 0 ▶ if $csv->parse( $opts->{cond}{$vv} );
Branch: 1 branch
BranchTrueFalseTotal
if $csv->parse($opts->{"cond"}{$vv}) 0 0 0
200
201 0 ▶ my $alias = $aliase_in_having ? "`$vv`" : $vv_col{$vv};
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
202
203 0 ▶ if ( $param{$table_type}{-having}{$alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param{$table_type}{'-having'}{$alias}) { } 0 0 0
204 0 ▶ $param{$table_type}{-having}{$alias} = [
Condition0%0/3
$dialect->$vv_opr || $vv_opr
l!l&&r!l&&!r
000
MC/DC0%0/2
$dialect->$vv_opr || $vv_opr
$dialect->$vv_opr $vv_opr
Truth table0%0/3
A || B
A= $dialect->$vv_oprB= $vv_opr
ABresult
1X1
011
000
205 -and => { $opr => \@conds },
206 [
207 -or => { ( $dialect->$vv_opr || $vv_opr ) => \@vv_conds },
208 { '=', undef }
209 ]
210 ];
211 }
212 else {
213 0 $param{$table_type}{-having}{$alias} =
214 { $opr => \@conds };
215 }
216 }
217  
218 # When condition is imposed on a variable (with no prefix v1, v2, vLast, vAny)
219 # assume condition applies to all visits of the variable (i.e. 'AND' case)
220 else {
221 0 for ( keys %$visit_info ) {
222 0 my $vc = "$visit_info->{$_}{name}.$var";
223 0 ▶ my $alias = $aliase_in_having ? "`$vc`" : $vv_col{"$vc"};
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
224 0 $param{$table_type}{-having}{$alias} =
225 [ { $opr => \@conds }, { '=', undef } ];
226 }
227 }
228 }
229 }
230 }
231
232 ##---- ADD IDENTIFIER, VISIT AND GROUP BY COLUMNS ----##
233 0 for ( keys %param ) {
234 0 ▶ if ( $_ eq 'dynamic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "dynamic") 0 0 0
235 0 unshift @{ $param{$_}{-columns} },
236 (
237 'vFirst' => $dialect->substring( $visit, '@@', 1 ),
238 'vLast' => $dialect->substring( $visit, '@@', -1 ),
239 'visit' => $visit
240 );
241 }
242
243 ###--- PARAMETERS COMMON TO BOTH STATIC AND DYNAMIC TABLE TYPES ---###
244 0 unshift @{ $param{$_}{-columns} }, entity_id => $map{entity_id};
245
246 0 ▶ if ( $opts->{cond} && $opts->{cond}{entity_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"cond"} and $opts->{"cond"}{"entity_id"}) 0 0 0
Condition0%0/3
$opts->{"cond"} and $opts->{"cond"}{"entity_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts->{"cond"} and $opts->{"cond"}{"entity_id"}
$opts->{"cond"} $opts->{"cond"}{"entity_id"}
Truth table0%0/3
A and B
A= $opts->{"cond"}B= $opts->{"cond"}{"entity_id"}
ABresult
0X0
100
111
247 # Set condition on entity_id
248 0 ▶ my $alias = $aliase_in_having ? 'entity_id' : $map{entity_id};
Branch: 1 branch
BranchTrueFalseTotal
$aliase_in_having ? : 0 0 0
249 0 my ( $opr, @conds ) = split /\s*,\s*/, $opts->{cond}{entity_id};
250 0 ▶ $param{$_}{-having}{$alias} = { ( $dialect->$opr || $opr ) => \@conds };
Condition0%0/3
$dialect->$opr || $opr
l!l&&r!l&&!r
000
MC/DC0%0/2
$dialect->$opr || $opr
$dialect->$opr $opr
Truth table0%0/3
A || B
A= $dialect->$oprB= $opr
ABresult
1X1
011
000
251 }
252
253 0 $param{$_}{-from} = $struct->{-from};
254
255 $param{$_}{-where} =
256 $struct->{-where}
257 0 ? { %{ $param{$_}{-where} }, %{ $struct->{-where} } }
258 0 ▶ : $param{$_}{-where};
Branch: 1 branch
BranchTrueFalseTotal
$struct->{'-where'} ? : 0 0 0
259
260 0 $param{$_}{-group_by} = 'entity_id';
261  
262 # Make sure condition on 'tables' has no duplicate placeholders
263 $param{$_}{-where}{ $map{table} }{-in} = [
264 0 keys %{ { map { $_ => 1 } @{ $param{$_}{-where}{ $map{table} }{-in} } } } ];
265 }
266 0 return \%param;
267 }
268  
269 0 ▶sub process_result {
Subroutine process_result: not called CC=21 SCAR=61.4
Pod documented
270 0 my ( $self, $opts, $rs, $dir, @args ) = @_;
271 0 my ( $ds, $csv ) = @{ $self->cache->get('cache') }{qw/datasource csv/};
272 0 my $visit_info = $ds->visit_info;
273 0 my $var_info = $ds->variable_info;
274 0 my $table_info = $ds->table_info;
275  
276 # Header of the csv must pay attention to args and variables on which the condition is imposed
277 # Extract visit specific variables from the result-set based on the variables provided as args/cond (option).
278 # Say, variables in args/cond variables are v1.var and vLast.var but as the result-set contains all visits of
279 # the variable 'var' so discard v2.var and v3.var and select v1.var and the equivalent vLast.var
280 0 ▶ my $index = $rs->[0][3] && $rs->[0][3] eq 'visit' ? 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rs->[0][3] && $rs->[0][3] eq 'visit' ? : 0 0 0
Condition0%0/3
$rs->[0][3] and $rs->[0][3] eq "visit"
!ll&&!rl&&r
000
MC/DC0%0/2
$rs->[0][3] and $rs->[0][3] eq "visit"
$rs->[0][3] $rs->[0][3] eq "visit"
Truth table0%0/3
A and B
A= $rs->[0][3]B= $rs->[0][3] eq "visit"
ABresult
0X0
100
111
281 0 ▶ my @cols = $index == 0 ? qw/entity_id/ : qw/entity_id vFirst vLast visit/;
Branch: 1 branch
BranchTrueFalseTotal
$index == 0 ? : 0 0 0
282 0 my @args_cond_vars = grep ( $_ ne 'entity_id', @args, keys %{ $opts->{cond} } );
283
284 0 for my $var ( @args_cond_vars ) {
285 0 ▶ if ( ( $var_info->{$var} && $table_info->{$var_info->{$var}{table}}{__type__} eq 'dynamic' ) || $var =~ /^vAny\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_info->{$var} and $$table_info{$var_info->{$var}{'table'}}{'__type__'} eq 'dynamic' or $var =~ /^vAny\./) { } 0 0 0
Condition0%0/6
$var_info->{$var} and $$table_info{$var_info->{$var}{"table"}}{"__type__"} eq "dynamic" or $var =~ /^vAny\./
l!l&&r!l&&!r
000
$var_info->{$var} and $$table_info{$var_info->{$var}{"table"}}{"__type__"} eq "dynamic"
!ll&&!rl&&r
000
MC/DC0%0/3
$var_info->{$var} and $$table_info{$var_info->{$var}{"table"}}{"__type__"} eq "dynamic" or $var =~ /^vAny\./
$var_info->{$var} $$table_info{$var_info->{$var}{"table"}}{"__type__"} eq "dynamic" $var =~ /^vAny\./
Truth table0%0/5
A and B or C
A= $var_info->{$var}B= $$table_info{$var_info->{$var}{"table"}}{"__type__"} eq "dynamic"C= $var =~ /^vAny\./
ABCresult
11X1
0X11
1011
0X00
1000
286 0 ▶ my $v = $var =~ /^vAny\.(.+)$/ ? $1 : $var;
Branch: 1 branch
BranchTrueFalseTotal
$var =~ /^vAny\.(.+)$/ ? : 0 0 0
287 # Avoid repeating visit variables
288 # variable 'var' without prefix or with prefix 'vAny' corresponds
289 # to all visits of 'var'
290 0 for my $visit ( keys %$visit_info ) {
291 0 ▶ if ( !grep ( $_ eq "$visit_info->{$visit}{name}.$v", @cols ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_ eq qq[$visit_info->{$visit}{"name"}.$v]), @cols)) 0 0 0
292 0 push @cols, "$visit_info->{$visit}{name}.$v";
293 }
294 }
295 }
296 # Static and visit specific variables (e.g. v1.Var, v2.Var, vLast.Var )
297 else {
298 0 ▶ if ( !grep ( $_ eq $var, @cols ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_ eq $var), @cols)) 0 0 0
299 0 push @cols, $var;
300 }
301 }
302 }
303
304 0 my @rs_entity;
305 0 my $file = File::Spec->catfile( $dir, 'QueryOutput.csv' );
306 0 ▶ my $fh = FileHandle->new("> $file")
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new("> $file") or throw_cmd_run_exception("error", "Failed to open file: $!") 0 0 0
307 or throw_cmd_run_exception( error => "Failed to open file: $!" );
308
309 0 ▶ $csv->print( $fh, \@cols )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, \@cols) or throw_cmd_run_exception("error", $csv->error_diag) 0 0 0
310 or throw_cmd_run_exception( error => $csv->error_diag );
311
312 0 for my $r ( 1 .. $#$rs ) {
313 0 push @rs_entity, $rs->[$r][0];
314 0 my @vals;
315 0 for my $c (@cols) {
316 0 ▶ if ( $c eq 'visit' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c eq 'visit') { } 0 0 0
elsif ($c eq 'vFirst') { } 0 0 0
elsif ($c eq 'vLast') { } 0 0 0
elsif ($c =~ /^vLast\.(.+)$/) { } 0 0 0
317 0 ( my $val = $rs->[$r][$index] ) =~ s/\@\@/, /g;
318 0 push @vals, $val;
319 }
320 elsif ( $c eq 'vFirst' ) {
321 0 push @vals, $rs->[$r][1];
322 }
323 elsif ( $c eq 'vLast' ) {
324 0 push @vals, $rs->[$r][2];
325 }
326 elsif ( $c =~ /^vLast\.(.+)$/ ) {
327 my ($pos) = grep ( $rs->[0][$_] eq "$visit_info->{$vals[2]}{name}.$1",
328 0 0 .. $#{ $rs->[0] } );
329 0 push @vals, $rs->[$r][$pos];
330 }
331 else {
332 0 my ($pos) = grep ( $rs->[0][$_] eq $c, 0 .. $#{ $rs->[0] } );
333 0 push @vals, $rs->[$r][$pos];
334 }
335 }
336 0 ▶ $csv->print( $fh, \@vals )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, \@vals) or throw_cmd_run_exception("error", $csv->error_diag) 0 0 0
337 or throw_cmd_run_exception( error => $csv->error_diag );
338 }
339 0 $fh->close;
340 0 return \@rs_entity;
341 }
342  
343 0 ▶sub process_table {
Subroutine process_table: not called CC=15 SCAR=54.8
Pod documented
344 0 my ( $self, $table, $ts, $dir, $rs_entity ) = @_;
345 0 my ( $ds, $csv ) = @{ $self->cache->get('cache') }{qw/datasource csv/};
346 0 my $table_info = $ds->table_info;
347 0 my $var_info = $ds->variable_info;
348 0 my $visit_info = $ds->visit_info;
349 0 my ( @vars, %data );
350 0 ▶ if ( $table_info->{$table}{__type__} eq 'static' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_info->{$table}{'__type__'} eq 'static') { } 0 0 0
351 0 for ( keys %$var_info ) {
352 0 ▶ if ( $var_info->{$_}{table} eq $table ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_info->{$_}{"table"} eq $table) 0 0 0
353 0 push @vars, $var_info->{$_}{variable};
354 }
355 }
356 }
357 else {
358  
359 # All visits applicable to the table
360 0 for my $v ( keys %$visit_info ) {
361 0 ▶ if ( grep ( $_ eq $table, @{ $visit_info->{$v}{tables} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep(($_ eq $table), @{$$visit_info{$v}{"tables"};})) 0 0 0
362 0 for ( keys %$var_info ) {
363 0 ▶ if ( $var_info->{$_}{table} eq $table ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_info->{$_}{"table"} eq $table) 0 0 0
364 0 push @vars, "$visit_info->{$v}{name}.$var_info->{$_}{variable}";
365 }
366 }
367 }
368 }
369 }
370
371 0 for (@$ts) {
372 # For static tables in longitudinal datasources table data comprise of
373 # entity_id and values of all table variables and in dynamic tables
374 # (longitudinal datasources only) it contains an additional column visit
375 0 ▶ if ( $table_info->{$table}{__type__} eq 'static' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($table_info->{$table}{'__type__'} eq 'static') { } 0 0 0
376 0 $data{ $_->[0] }{ $_->[1] } = $_->[2];
377 }
378 else {
379 0 $data{ $_->[0] }{"$visit_info->{$_->[3]}{name}.$_->[1]"} = $_->[2];
380 }
381 }
382  
383 # Write table data
384 0 my $file = File::Spec->catfile( $dir, 'data.csv' );
385 0 ▶ my $untaint = $1 if $file =~ /^(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /^(.+)$/ 0 0 0
386 0 ▶ my $fh = FileHandle->new("> $untaint")
Branch: 1 branch
BranchTrueFalseTotal
my $fh = "FileHandle"->new("> $untaint") or throw_cmd_run_exception("error", "Failed to open file: $!") 0 0 0
387 or throw_cmd_run_exception( error => "Failed to open file: $!" );
388
389 0 my @cols = ( qw/entity_id/, @vars );
390 0 ▶ $csv->print( $fh, \@cols )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, \@cols) or throw_cmd_run_exception("error", $csv->error_diag) 0 0 0
391 or throw_cmd_run_exception( error => $csv->error_diag );
392  
393 # Write data for entities present in the result set
394 0 for my $entity (@$rs_entity) {
395 0 my @vals = ( $entity, map { $data{$entity}{$_} } @vars );
396 0 ▶ $csv->print( $fh, \@vals )
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, \@vals) or throw_cmd_run_exception("error", $csv->error_diag) 0 0 0
397 or throw_cmd_run_exception( error => $csv->error_diag );
398 }
399 0 $fh->close;
400 }
401  
402 0 ▶sub create_dataset {
Subroutine create_dataset: not called CC=12 SCAR=50.5
Pod documented
403 0 my ( $self, $rs ) = @_;
404 0 my $visit_info = $self->cache->get('cache')->{datasource}->visit_info;
405 0 ▶ my $index = $rs->[0][3] && $rs->[0][3] eq 'visit' ? 3 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rs->[0][3] && $rs->[0][3] eq 'visit' ? : 0 0 0
Condition0%0/3
$rs->[0][3] and $rs->[0][3] eq "visit"
!ll&&!rl&&r
000
MC/DC0%0/2
$rs->[0][3] and $rs->[0][3] eq "visit"
$rs->[0][3] $rs->[0][3] eq "visit"
Truth table0%0/3
A and B
A= $rs->[0][3]B= $rs->[0][3] eq "visit"
ABresult
0X0
100
111
406 0 my ( @vars, %data );
407  
408 # Remove visit suffix vAny, vLast, v1, v2 etc. from variables
409 # in the result-set (i.e. args/cond (option))
410 0 my $regex = 'vLast|' . join( '|', map { $visit_info->{$_}{name} } keys %$visit_info );
411
412 0 for my $v ( @{ $rs->[0] }[ $index + 1 .. $#{ $rs->[0] } ] ) {
413 0 $v =~ s/^(?:$regex)\.//;
414 0 ▶ if ( !grep ( $_ eq $v, @vars ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep(($_ eq $v), @vars)) 0 0 0
415 0 push @vars, $v;
416 }
417 }
418  
419 # Create dataset for calculating summary statistics
420 0 for my $r ( 1 .. $#$rs ) {
421 0 for my $v (@vars) {
422 0 my @ds;
423 0 for my $i ( $index + 1 .. $#{ $rs->[0] } ) {
424 0 ▶ push @ds, $rs->[0][$i] =~ /$v$/ ? $rs->[$r][$i] || () : ()
Branch: 1 branch
BranchTrueFalseTotal
$rs->[0][$i] =~ /$v$/ ? : 0 0 0
Condition0%0/3
$rs->[$r][$i] or ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$rs->[$r][$i] or ()
$rs->[$r][$i] ()
Truth table0%0/3
A or B
A= $rs->[$r][$i]B= ()
ABresult
1X1
011
000
425 }
426 0 $data{ $rs->[$r][0] }{$v} = \@ds;
427
428 0 ▶ $data{ $rs->[$r][0] }{'visit'} = [ split '@@', $rs->[$r][$index] ]
Branch: 1 branch
BranchTrueFalseTotal
if $index == 3 0 0 0
429 if $index == 3;
430 }
431
432 }
433 0 ▶ return ( \%data, 1, ( $index == 0 ? qw/entity_id/ : qw/entity_id visit/ ),
Branch: 1 branch
BranchTrueFalseTotal
$index == 0 ? : 0 0 0
434 @vars );
435 }
436  
437 #-------
438 1 1;
439 __END__
↑ Summary CohortExplorer/Application.pm »