lib/Convert/Pheno/Source/ClinicalCDM.pm

filter: Statementstmt 99.1% 122 / 123 Branchbran 67.3% 35 / 52 Conditioncond 60.0% 15 / 25 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 81.9% 191 / 233 CC 44 SCAR 26.6
CC
44
Cov
86%
_group_patients
26.2
prepare
9.0
_first_value
7.7
CRAP 14.3
« lib/Convert/Pheno/ClinicalCDM/Util.pm ↑ Summary lib/Convert/Pheno/Source/OMOP.pm »
1 package Convert::Pheno::Source::ClinicalCDM;
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  
6 136 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 136 ▶use Convert::Pheno::Mapping::Shared qw(get_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 136 ▶use Convert::Pheno::Source::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 136 ▶use Convert::Pheno::Source::TablePackage qw(load_table_package);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 136 my %PROFILE = (
13 i2b2 => {
14 label => 'i2b2',
15 person_table => 'PATIENT_DIMENSION',
16 person_keys => [qw(PATIENT_NUM PATID PATIENT_ID)],
17 required => [qw(PATIENT_DIMENSION OBSERVATION_FACT)],
18 concept_table => 'CONCEPT_DIMENSION',
19 concept_key => 'CONCEPT_CD',
20 visit_table => 'VISIT_DIMENSION',
21 visit_keys => [qw(ENCOUNTER_NUM ENCOUNTERID VISIT_ID)],
22 patient_tables => [qw(OBSERVATION_FACT VISIT_DIMENSION)],
23 },
24 sentinel => {
25 label => 'Sentinel CDM',
26 person_table => 'DEMOGRAPHIC',
27 person_keys => [qw(PATID PATIENT_ID PATIENT_NUM)],
28 required => ['DEMOGRAPHIC'],
29 visit_table => 'ENCOUNTER',
30 visit_keys => [qw(ENCOUNTERID ENCOUNTER_NUM VISIT_ID)],
31 patient_tables => [qw(DEATH DIAGNOSIS DISPENSING ENCOUNTER INPATIENT_PHARMACY LAB_RESULT PRESCRIBING PROCEDURE VITAL_SIGNS)],
32 },
33 pcornet => {
34 label => 'PCORnet CDM',
35 person_table => 'DEMOGRAPHIC',
36 person_keys => [qw(PATID PATIENT_ID PATIENT_NUM)],
37 required => ['DEMOGRAPHIC'],
38 visit_table => 'ENCOUNTER',
39 visit_keys => [qw(ENCOUNTERID ENCOUNTER_NUM VISIT_ID)],
40 patient_tables => [qw(CONDITION DEATH DIAGNOSIS DISPENSING ENCOUNTER EXTERNAL_MEDS IMMUNIZATION LAB_RESULT_CM MED_ADMIN OBS_CLIN OBS_GEN PRESCRIBING PROCEDURES VITAL)],
41 },
42 );
43  
44 21 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
45 21 my ( $class, $converter, %arg ) = @_;
46 21 ▶ my $profile = lc( $arg{profile} // q{} );
Condition50%1/2
$arg{"profile"} // ""
l!l
210
MC/DC0%0/1
$arg{"profile"} // ""
$arg{"profile"}
Truth table50%1/2
A
A= $arg{"profile"}
Aresult
11
00
47 die "Unknown clinical CDM source profile <$profile>\n"
48 21 ▶ unless exists $PROFILE{$profile};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $PROFILE{$profile} 0 21 21
49 21 return bless {
50 converter => $converter,
51 profile => $profile,
52 }, $class;
53 }
54  
55 21 ▶sub load {
Subroutine load: called CC=3 SCAR=11.0
Pod undocumented
56 21 my ($self) = @_;
57 21 my $profile = $self->{profile};
58 21 my $config = $PROFILE{$profile};
59 21 my $package = load_table_package( $self->{converter}, $profile );
60 21 my $tables = $package->{tables};
61  
62 21 for my $required ( @{ $config->{required} } ) {
63 die "$config->{label} input requires table <$required>\n"
64 29 ▶ unless exists $tables->{$required};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tables->{$required} 0 29 29
65 }
66  
67 21 my $records = _group_patients( $tables, $profile, $config );
68 19 my %counts = map { $_ => scalar @{ $tables->{$_} } } sort keys %{$tables};
69  
70 return Convert::Pheno::Source::Result->new(
71 {
72 data => $records,
73 owned => 1,
74 artifacts => {
75 profile => $profile,
76 table_counts => \%counts,
77 table_sources => dclone( $package->{sources} ),
78 source_kind => $package->{kind},
79 },
80 }
81 19 );
82 }
83  
84 21 ▶sub prepare {
Subroutine prepare: called CC=8 SCAR=22.0
Pod undocumented
85 21 my ($self) = @_;
86 21 my $converter = $self->{converter};
87 21 my $profile = $self->{profile};
88 21 my $prepared_key = $profile . '_input_prepared';
89 21 my $source_key = '_' . $profile . '_source_data';
90 21 ▶ return 1 if $converter->{$prepared_key} && exists $converter->{data};
Branch: 1 branch
BranchTrueFalseTotal
if $converter->{$prepared_key} and exists $converter->{"data"} 0 21 21
Condition33%1/3
$converter->{$prepared_key} and exists $converter->{"data"}
!ll&&!rl&&r
2100
MC/DC0%0/2
$converter->{$prepared_key} and exists $converter->{"data"}
$converter->{$prepared_key} exists $converter->{"data"}
Truth table33%1/3
A and B
A= $converter->{$prepared_key}B= exists $converter->{"data"}
ABresult
0X0
100
111
91  
92 $converter->{$source_key} = $converter->{data}
93 21 ▶ if exists $converter->{data} && !exists $converter->{$source_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $converter->{"data"} and not exists $converter->{$source_key} 4 17 21
Condition66%2/3
exists $converter->{"data"} and not exists $converter->{$source_key}
!ll&&!rl&&r
1704
MC/DC50%1/2
exists $converter->{"data"} and not exists $converter->{$source_key}
exists $converter->{"data"} not exists $converter->{$source_key}
Truth table66%2/3
A and not(B)
A= exists $converter->{"data"}B= not exists $converter->{$source_key}
ABresult
0X0
100
111
94 $converter->{data} = $converter->{$source_key}
95 21 ▶ if !exists $converter->{data} && exists $converter->{$source_key};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $converter->{"data"} and exists $converter->{$source_key} 0 21 21
Condition66%2/3
not exists $converter->{"data"} and exists $converter->{$source_key}
!ll&&!rl&&r
4170
MC/DC0%0/2
not exists $converter->{"data"} and exists $converter->{$source_key}
not exists $converter->{"data"} exists $converter->{$source_key}
Truth table66%2/3
not(A) and B
A= not exists $converter->{"data"}B= exists $converter->{$source_key}
ABresult
0X0
100
111
96  
97 21 my $source = $self->load;
98 19 $source->apply_to($converter);
99 19 $converter->{clinical_cdm_metadata}{$profile} = {
100 table_counts => $source->artifact('table_counts'),
101 table_sources => $source->artifact('table_sources'),
102 source_kind => $source->artifact('source_kind'),
103 };
104 19 ▶ $converter->{convertPheno} ||= get_info($converter);
Condition33%1/3
$converter->{"convertPheno"} ||= get_info($converter)
l!l&&r!l&&!r
0190
MC/DC0%0/2
$converter->{"convertPheno"} ||= get_info($converter)
$converter->{"convertPheno"} get_info($converter)
Truth table33%1/3
A ||= B
A= $converter->{"convertPheno"}B= get_info($converter)
ABresult
1X1
011
000
105 19 $converter->{$prepared_key} = 1;
106 19 return 1;
107 }
108  
109 21 ▶sub _group_patients {
Subroutine _group_patients: called CC=25 SCAR=32.7
110 21 my ( $tables, $profile, $config ) = @_;
111 21 my $person_table = $config->{person_table};
112 21 my ( %record_for, @order, %concepts );
113  
114 21 ▶ if ( $config->{concept_table} && exists $tables->{ $config->{concept_table} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"concept_table"} and exists $$tables{$config->{"concept_table"}}) 5 16 21
Condition100%3/3
$config->{"concept_table"} and exists $$tables{$config->{"concept_table"}}
!ll&&!rl&&r
1335
MC/DC100%2/2
$config->{"concept_table"} and exists $$tables{$config->{"concept_table"}}
$config->{"concept_table"} exists $$tables{$config->{"concept_table"}}
Truth table100%3/3
A and B
A= $config->{"concept_table"}B= exists $$tables{$config->{"concept_table"}}
ABresult
0X0
100
111
115 5 for my $row ( @{ $tables->{ $config->{concept_table} } } ) {
116 25 my $code = _first_value( $row, $config->{concept_key} );
117 25 ▶ next unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 25 25
118 die "$config->{label} table <$config->{concept_table}> contains duplicate concept <$code>\n"
119 25 ▶ if exists $concepts{$code};
Branch: 1 branch
BranchTrueFalseTotal
if exists $concepts{$code} 0 25 25
120 25 ▶ $concepts{$code} = $row if defined $code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $code 25 0 25
121 }
122 }
123  
124 21 for my $row ( @{ $tables->{$person_table} } ) {
125 my $person_id = _required_identifier(
126 $row,
127 $config->{person_keys},
128 38 "$config->{label} $person_table",
129 );
130 die "$config->{label} table <$person_table> contains duplicate patient <$person_id>\n"
131 38 ▶ if exists $record_for{$person_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $record_for{$person_id} 0 38 38
132 38 my $record = {
133 model => $profile,
134 personId => $person_id,
135 patient => $row,
136 tables => {},
137 visits => {},
138 # Concept metadata is read-only and can be very large in i2b2.
139 # Share one index rather than cloning it once per patient.
140 concepts => \%concepts,
141 };
142 38 $record_for{$person_id} = $record;
143 38 push @order, $person_id;
144 }
145 21 ▶ die "$config->{label} table <$person_table> contains no patients\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @order 0 21 21
146 unless @order;
147  
148 21 ▶ if ( exists $tables->{ $config->{visit_table} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$tables{$config->{"visit_table"}}) 14 7 21
149 14 for my $row ( @{ $tables->{ $config->{visit_table} } } ) {
150 28 my $person_id = _row_person_id( $row, $config->{person_keys} );
151 28 ▶ die "$config->{label} table <$config->{visit_table}> row is missing a patient identifier\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $person_id 0 28 28
152 unless defined $person_id;
153 28 _require_known_patient( $profile, $config, $person_id, $config->{visit_table}, \%record_for );
154 28 my $visit_id = _first_value( $row, @{ $config->{visit_keys} } );
155 28 ▶ next unless defined $visit_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $visit_id 0 28 28
156 die "$config->{label} table <$config->{visit_table}> contains duplicate encounter <$visit_id> for patient <$person_id>\n"
157 28 ▶ if exists $record_for{$person_id}{visits}{$visit_id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $record_for{$person_id}{"visits"}{$visit_id} 0 28 28
158 28 $record_for{$person_id}{visits}{$visit_id} = $row;
159 }
160 }
161  
162 21 ▶ my %patient_scoped = map { $_ => 1 } @{ $config->{patient_tables} || [] };
Condition50%1/2
$config->{'patient_tables'} || []
l!l
210
MC/DC0%0/1
$config->{'patient_tables'} || []
$config->{'patient_tables'}
Truth table50%1/2
A
A= $config->{'patient_tables'}
Aresult
11
00
163 21 for my $table ( sort keys %{$tables} ) {
164 106 ▶ next if $table eq $person_table;
Branch: 1 branch
BranchTrueFalseTotal
if $table eq $person_table 21 85 106
165 85 ▶ next if $config->{concept_table} && $table eq $config->{concept_table};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"concept_table"} and $table eq $config->{"concept_table"} 5 80 85
Condition100%3/3
$config->{"concept_table"} and $table eq $config->{"concept_table"}
!ll&&!rl&&r
67135
MC/DC100%2/2
$config->{"concept_table"} and $table eq $config->{"concept_table"}
$config->{"concept_table"} $table eq $config->{"concept_table"}
Truth table100%3/3
A and B
A= $config->{"concept_table"}B= $table eq $config->{"concept_table"}
ABresult
0X0
100
111
166  
167 80 for my $row ( @{ $tables->{$table} } ) {
168 128 my $person_id = _row_person_id( $row, $config->{person_keys} );
169 128 ▶ if ( !defined $person_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $person_id) 1 127 128
170 die "$config->{label} table <$table> row is missing a patient identifier\n"
171 1 ▶ if $patient_scoped{$table};
Branch: 1 branch
BranchTrueFalseTotal
if $patient_scoped{$table} 1 0 1
172 0 next;
173 }
174 127 _require_known_patient( $profile, $config, $person_id, $table, \%record_for );
175 126 push @{ $record_for{$person_id}{tables}{$table} }, $row;
176 }
177 }
178  
179 19 my @records = map { $record_for{$_} } @order;
180 19 for my $index ( 0 .. $#records ) {
181 36 ▶ $records[$index]{isFirst} = $index == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$index == 0 ? : 19 17 36
182 }
183 19 return \@records;
184 }
185  
186 155 ▶sub _require_known_patient {
Subroutine _require_known_patient: called CC=2 SCAR=6.9
187 155 my ( $profile, $config, $person_id, $table, $records ) = @_;
188 die "$config->{label} table <$table> references unknown patient <$person_id>\n"
189 155 ▶ unless exists $records->{$person_id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $records->{$person_id} 1 154 155
190 154 return 1;
191 }
192  
193 38 ▶sub _required_identifier {
Subroutine _required_identifier: called CC=2 SCAR=7.0
194 38 my ( $row, $keys, $context ) = @_;
195 38 my $value = _first_value( $row, @{$keys} );
196 38 ▶ die "$context row is missing a patient identifier\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 38 38
197 unless defined $value;
198 38 return $value;
199 }
200  
201 156 ▶sub _row_person_id {
Subroutine _row_person_id: called CC=1 SCAR=0.0
202 156 my ( $row, $keys ) = @_;
203 156 return _first_value( $row, @{$keys} );
204 }
205  
206 247 ▶sub _first_value {
Subroutine _first_value: called CC=7 SCAR=20.4
207 247 my ( $row, @keys ) = @_;
208 247 for my $key (@keys) {
209 249 ▶ next unless exists $row->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $row->{$key} 3 246 249
210 246 my $value = $row->{$key};
211 246 ▶ next unless defined $value && !ref($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 0 246 246
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
00246
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
212 246 $value =~ s/^\s+|\s+$//g;
213 246 ▶ next unless length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 246 246
214 246 ▶ next if $value =~ /\A(?:NULL|\\N|\.)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:NULL|\\N|\.)\z/i 0 246 246
215 246 return $value;
216 }
217 1 return;
218 }
219  
220 136 1;
« lib/Convert/Pheno/ClinicalCDM/Util.pm ↑ Summary lib/Convert/Pheno/Source/OMOP.pm »