lib/Convert/Pheno/BFF/ToOMOP.pm

filter: Statementstmt 99.5% 231 / 232 Branchbran 86.9% 80 / 92 Conditioncond 74.4% 64 / 86 MC/DCmcdc 53.0% 26 / 49 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 2 totaltotal 87.5% 421 / 481 CC 89 SCAR 25.2
CC
89
Cov
91%
_map_person
22.2
_map_treatments
18.4
_map_measurements
15.5
CRAP 12.4
« lib/Convert/Pheno/Source/TablePackage.pm ↑ Summary lib/Convert/Pheno/CDISC/ODM/Detector.pm »
1 package Convert::Pheno::BFF::ToOMOP;
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 136 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 136 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 136 ▶use Convert::Pheno::Utils::Default qw(get_defaults);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 136 ▶use Convert::Pheno::Mapping::Shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 136 ▶use Convert::Pheno::OMOP::Vocabulary qw(resolve_standard_concept);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 136 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 136 our @EXPORT = qw(do_bff2omop);
15 136 my $DEFAULT = get_defaults();
16 136 my $MEASUREMENT_ID_COUNT = 0;
17 136 my $DRUG_EXPOSURE_ID_COUNT = 0;
18 136 my $OBSERVATION_ID_COUNT = 0;
19 136 my $CONDITION_OCCURRENCE_COUNT = 0;
20 136 my $PROCEDURE_OCCURRENCE_ID_COUNT = 0;
21  
22 # OMOP standard concept fields are domain-specific. An exact label in another
23 # domain is not interchangeable, even when its text is identical.
24 136 my %INVERSE_CONFIG = (
25 gender => { domains => ['Gender'] },
26 race => { domains => ['Race'] },
27 ethnicity => { domains => ['Ethnicity'] },
28 demographic => { domains => [ 'Race', 'Ethnicity' ] },
29 disease => { domains => ['Condition'] },
30 stage => { domains => ['Condition Status'] },
31 exposure => { domains => ['Observation'] },
32 phenotypicFeature => { domains => ['Observation'] },
33 procedure => { domains => ['Procedure'] },
34 measurement => { domains => ['Measurement'] },
35 measurementValue => {
36 domains => ['Meas Value'],
37 relationships => ['Maps to value'],
38 },
39 treatment => { domains => ['Drug'] },
40 unit => { domains => ['Unit'] },
41 route => { domains => ['Route'] },
42 );
43  
44 136 my %SOURCE_FIELDS = (
45 gender => 'sex', demographic => 'ethnicity',
46 disease => 'diseases.diseaseCode', exposure => 'exposures.exposureCode',
47 phenotypicFeature => 'phenotypicFeatures.featureType',
48 procedure => 'interventionsOrProcedures.procedureCode',
49 measurement => 'measures.assayCode', measurementValue => 'measures.measurementValue',
50 treatment => 'treatments.treatmentCode', route => 'treatments.routeOfAdministration',
51 );
52  
53 ###############
54 ###############
55 # BFF2OMOP #
56 ###############
57 ###############
58  
59 71 ▶sub do_bff2omop {
Subroutine do_bff2omop: called CC=4 SCAR=13.9
Pod undocumented
60 71 my ( $self, $bff ) = @_;
61  
62 # Premature return if no input
63 71 ▶ return unless defined($bff);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bff 1 70 71
64  
65 # Validate that input is in BFF format.
66 70 ▶ die "Input format error: Are you sure your input is not already OMOP?\n"
Branch: 1 branch
BranchTrueFalseTotal
unless validate_format($bff, "bff") 1 69 70
67 unless validate_format( $bff, 'bff' );
68  
69 # Convert ID string to ID integer
70 my $person_id = looks_like_number( $bff->{id} )
71 ? 0 + $bff->{id}
72 69 ▶ : allocate_surrogate_integer( $self, 'omop_person_id', $bff->{id} );
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($bff->{'id'}) ? : 15 54 69
73  
74 # Create a new OMOP structure.
75 # This will be a hash with keys corresponding to OMOP table names.
76 69 my $omop = {};
77  
78 # Convert individual components.
79 # Pass the precomputed $person_id to each conversion sub.
80 69 _map_person( $self, $bff, $omop, $person_id );
81 69 _map_diseases( $self, $bff, $omop, $person_id );
82 69 _map_exposures( $self, $bff, $omop, $person_id );
83 69 _map_phenotypicFeatures( $self, $bff, $omop, $person_id );
84 69 _map_interventionsOrProcedures( $self, $bff, $omop, $person_id );
85 69 _map_measurements( $self, $bff, $omop, $person_id );
86 69 _map_treatments( $self, $bff, $omop, $person_id );
87  
88 # (Optionally, additional tables such as VISIT_OCCURRENCE or OBSERVATION_PERIOD
89 # could be derived from extra info in $bff.)
90 #print Dumper $omop;
91 69 return $omop;
92 }
93  
94 ###############################################################################
95 # Private conversion subs (each inverts part of the OMOP2BFF mapping)
96 ###############################################################################
97  
98 # Convert BFF subject and info into a PERSON record.
99 71 ▶sub _map_person {
Subroutine _map_person: called CC=21 SCAR=31.0
100 71 my ( $self, $bff, $omop_ref, $person_id ) = @_;
101  
102 71 my $person;
103  
104 # Miscellanea id
105 71 $person->{person_id} = $person_id;
106 71 $person->{person_source_value} = $bff->{id};
107  
108 # Phenopacket-derived BFF stores subject metadata under info.phenopacket;
109 # older BFF producers may still place dateOfBirth directly under info.
110 71 ▶ my $info = ref( $bff->{info} ) eq 'HASH' ? $bff->{info} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $bff->{'info'} eq 'HASH' ? : 67 4 71
111 my $phenopacket_info =
112 71 ▶ ref( $info->{phenopacket} ) eq 'HASH' ? $info->{phenopacket} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $info->{'phenopacket'} eq 'HASH' ? : 37 34 71
113 my $date_of_birth =
114 71 ▶ $phenopacket_info->{dateOfBirth} // $info->{dateOfBirth};
Condition100%3/3
$phenopacket_info->{"dateOfBirth"} // $info->{"dateOfBirth"}
l!l&&r!l&&!r
171044
MC/DC100%2/2
$phenopacket_info->{"dateOfBirth"} // $info->{"dateOfBirth"}
$phenopacket_info->{"dateOfBirth"} $info->{"dateOfBirth"}
Truth table100%3/3
A // B
A= $phenopacket_info->{"dateOfBirth"}B= $info->{"dateOfBirth"}
ABresult
1X1
011
000
115  
116 # Map dateOfBirth (if available) to birth_datetime.
117 71 ▶ $person->{birth_datetime} = $date_of_birth // $DEFAULT->{timestamp};
Condition66%2/3
$date_of_birth // $DEFAULT->{"timestamp"}
l!l&&r!l&&!r
27440
MC/DC0%0/2
$date_of_birth // $DEFAULT->{"timestamp"}
$date_of_birth $DEFAULT->{"timestamp"}
Truth table66%2/3
A // B
A= $date_of_birthB= $DEFAULT->{"timestamp"}
ABresult
1X1
011
000
118  
119 # Map dateOfBirth (if available) to birth_datetime.
120 71 ▶ if ( defined($date_of_birth) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date_of_birth) { } 27 44 71
121 27 for (qw/year month day/) {
122 81 $person->{ $_ . '_of_birth' } =
123 get_date_component( $date_of_birth, $_ );
124 }
125 }
126 else {
127 44 $person->{year_of_birth} = $DEFAULT->{year};
128 }
129  
130 # Convert sex: now done via our new generic inverse_map.
131 (
132 $person->{gender_concept_id},
133 $person->{gender_source_value},
134 $person->{gender_source_concept_id}
135 ) =
136 71 inverse_map( 'gender', $bff->{sex}, 'label', $self );
137  
138 ( $person->{race_concept_id}, $person->{race_source_value},
139 $person->{race_source_concept_id} ) =
140 71 ( $DEFAULT->{concept_id}, '', $DEFAULT->{concept_id} );
141 ( $person->{ethnicity_concept_id}, $person->{ethnicity_source_value},
142 $person->{ethnicity_source_concept_id} ) =
143 71 ( $DEFAULT->{concept_id}, '', $DEFAULT->{concept_id} );
144  
145 71 ▶ if ( exists $bff->{ethnicity} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bff->{"ethnicity"}) 41 30 71
146 my ( $concept_id, $source_value, $source_concept_id, $resolution ) =
147 41 inverse_map( 'demographic', $bff->{ethnicity}, 'label', $self );
148 41 ▶ my $target = $resolution->{target_domain} // 'Race';
Condition100%2/2
$resolution->{"target_domain"} // "Race"
l!l
2714
MC/DC100%1/1
$resolution->{"target_domain"} // "Race"
$resolution->{"target_domain"}
Truth table100%2/2
A
A= $resolution->{"target_domain"}
Aresult
11
00
149 41 ▶ my $prefix = $target eq 'Ethnicity' ? 'ethnicity' : 'race';
Branch: 1 branch
BranchTrueFalseTotal
$target eq 'Ethnicity' ? : 14 27 41
150 41 $person->{ $prefix . '_concept_id' } = $concept_id;
151 41 $person->{ $prefix . '_source_value' } = $source_value;
152 41 $person->{ $prefix . '_source_concept_id' } = $source_concept_id;
153 }
154  
155 # Some synthetic OMOP datasets place country of birth in
156 # ethnicity_source_value. Retain that established fallback only when BFF
157 # did not provide a term that resolves to the OMOP Ethnicity domain.
158 71 ▶ if ( exists $bff->{geographicOrigin}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bff->{"geographicOrigin"} and not $person->{"ethnicity_concept_id"}) 10 61 71
Condition100%3/3
exists $bff->{"geographicOrigin"} and not $person->{"ethnicity_concept_id"}
!ll&&!rl&&r
53810
MC/DC100%2/2
exists $bff->{"geographicOrigin"} and not $person->{"ethnicity_concept_id"}
exists $bff->{"geographicOrigin"} not $person->{"ethnicity_concept_id"}
Truth table100%3/3
A and not(B)
A= exists $bff->{"geographicOrigin"}B= not $person->{"ethnicity_concept_id"}
ABresult
0X0
100
111
159 && !$person->{ethnicity_concept_id} )
160 {
161 # This is source-text preservation, not an ethnicity correspondence.
162 # Do not search geography in the Ethnicity domain or report it as a
163 # failed lookup. Both concept identifiers deliberately remain zero.
164 10 my $origin = $bff->{geographicOrigin};
165 10 ▶ my $id = ref($origin) eq 'HASH' ? ($origin->{id} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $origin eq 'HASH' ? : 10 0 10
Condition50%1/2
$origin->{"id"} // ""
l!l
100
MC/DC0%0/1
$origin->{"id"} // ""
$origin->{"id"}
Truth table50%1/2
A
A= $origin->{"id"}
Aresult
11
00
166 10 ▶ my $label = ref($origin) eq 'HASH' ? ($origin->{label} // '') : ($origin // '');
Branch: 1 branch
BranchTrueFalseTotal
ref $origin eq 'HASH' ? : 10 0 10
Condition25%1/4
$origin->{"label"} // ""
l!l
100
$origin // ""
l!l
00
MC/DC0%0/1
$origin->{"label"} // ""
$origin->{"label"}
MC/DC0%0/1
$origin // ""
$origin
Truth table50%1/2
A
A= $origin->{"label"}
Aresult
11
00
Truth table0%0/2
A
A= $origin
Aresult
11
00
167 10 s/^\s+|\s+$//g for ($id, $label);
168 10 ▶ my $value = length($label) ? $label : $id;
Branch: 1 branch
BranchTrueFalseTotal
length $label ? : 10 0 10
169 10 $person->{ethnicity_source_value} = $value;
170 10 $person->{ethnicity_source_concept_id} = $DEFAULT->{concept_id};
171 10 ▶ record_term_audit({
Branch: 2 branches
BranchTrueFalseTotal
if length $value 10 0 10
length $id ? : 10 0 10
172 self => $self, source_field => 'geographicOrigin',
173 source_value => length($id) ? $id : $value, source_label => $label,
174 term => {label => $value}, ontology => 'ohdsi',
175 match_status => 'preserved', decision_reason => 'source_value_preserved',
176 lookup_resolution => 'source_value_preserved', match_source => 'source',
177 fallback_action => 'concept_0',
178 }) if length($value);
179 }
180  
181 # Save the PERSON record one person per individual)
182 71 $omop_ref->{PERSON} = $person;
183  
184 }
185  
186 # Convert BFF diseases into OMOP CONDITION_OCCURRENCE rows.
187 69 ▶sub _map_diseases {
Subroutine _map_diseases: called CC=7 SCAR=19.5
188 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
189  
190 69 my @conditions;
191  
192 69 ▶ for my $disease ( @{ $bff->{diseases} // [] } ) {
Condition100%2/2
$bff->{"diseases"} // []
l!l
5910
MC/DC100%1/1
$bff->{"diseases"} // []
$bff->{"diseases"}
Truth table100%2/2
A
A= $bff->{"diseases"}
Aresult
11
00
193 127 my $cond;
194  
195 127 $cond->{condition_occurrence_id} = ++$CONDITION_OCCURRENCE_COUNT;
196  
197 # We now call our generic inverse_map:
198 (
199 $cond->{condition_concept_id},
200 $cond->{condition_source_value},
201 $cond->{condition_source_concept_id}
202 ) =
203 127 inverse_map( 'disease', $disease->{diseaseCode}, 'label', $self );
204  
205 # Convert onset (e.g., an ISO8601 duration) to a date
206 127 ▶ if ( ref($disease->{ageOfOnset}) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $disease->{'ageOfOnset'} eq 'HASH' and ref $disease->{'ageOfOnset'}{'age'} eq 'HASH' and $disease->{'ageOfOnset'}{'age'}{'iso8601duration'}) { } 87 40 127
Condition83%5/6
ref $disease->{"ageOfOnset"} eq "HASH" and ref $disease->{"ageOfOnset"}{"age"} eq "HASH" and $disease->{"ageOfOnset"}{"age"}{"iso8601duration"}
!ll&&!rl&&r
40087
ref $disease->{"ageOfOnset"} eq "HASH" and ref $disease->{"ageOfOnset"}{"age"} eq "HASH"
!ll&&!rl&&r
32887
MC/DC66%2/3
ref $disease->{"ageOfOnset"} eq "HASH" and ref $disease->{"ageOfOnset"}{"age"} eq "HASH" and $disease->{"ageOfOnset"}{"age"}{"iso8601duration"}
ref $disease->{"ageOfOnset"} eq "HASH" ref $disease->{"ageOfOnset"}{"age"} eq "HASH" $disease->{"ageOfOnset"}{"age"}{"iso8601duration"}
Truth table75%3/4
A and B and C
A= ref $disease->{"ageOfOnset"} eq "HASH"B= ref $disease->{"ageOfOnset"}{"age"} eq "HASH"C= $disease->{"ageOfOnset"}{"age"}{"iso8601duration"}
ABCresult
0XX0
10X0
1100
1111
207 && ref($disease->{ageOfOnset}{age}) eq 'HASH'
208 && $disease->{ageOfOnset}{age}{iso8601duration} ) {
209 $cond->{condition_start_date} = get_date_at_age(
210 $disease->{ageOfOnset}{age}{iso8601duration},
211 $omop_ref->{PERSON}{year_of_birth}
212 87 );
213 }
214 else {
215 40 $cond->{condition_start_date} = $DEFAULT->{date};
216 }
217  
218 # TEMPORARY SOLUTION: Setting defaults
219 # mrueda: Apr-2025
220 127 $cond->{condition_type_concept_id} = $DEFAULT->{concept_id};
221  
222 127 _attach_common( $self, $cond, $disease, $person_id );
223  
224 # Optionally map stage to condition_status_concept_id.
225 #if ( exists $disease->{stage} ) {
226 #}
227  
228 127 push @conditions, $cond;
229 }
230 69 ▶ $omop_ref->{CONDITION_OCCURRENCE} = \@conditions if @conditions;
Branch: 1 branch
BranchTrueFalseTotal
if @conditions 59 10 69
231  
232 }
233  
234 # Convert BFF exposures into OMOP OBSERVATION rows.
235 69 ▶sub _map_exposures {
Subroutine _map_exposures: called CC=5 SCAR=16.1
236 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
237  
238 69 my @observations;
239  
240 69 ▶ for my $exposure ( @{ $bff->{exposures} // [] } ) {
Condition100%2/2
$bff->{"exposures"} // []
l!l
1455
MC/DC100%1/1
$bff->{"exposures"} // []
$bff->{"exposures"}
Truth table100%2/2
A
A= $bff->{"exposures"}
Aresult
11
00
241 16 my $obs;
242  
243 16 $obs->{observation_id} = ++$OBSERVATION_ID_COUNT;
244  
245 # e.g., $exposure->{exposureCode} used in a generic mapping:
246 (
247 $obs->{observation_concept_id},
248 $obs->{observation_source_value},
249 $obs->{observation_source_concept_id}
250 ) =
251 16 inverse_map( 'exposure', $exposure->{exposureCode}, 'label', $self );
252  
253 # Date
254 16 $obs->{observation_date} = $exposure->{date};
255  
256 # BFF only accepts numeric
257 $obs->{value_as_number} =
258 16 ▶ defined $exposure->{value} ? $exposure->{value} : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $exposure->{'value'} ? : 16 0 16
259  
260 ( $obs->{unit_concept_id}, $obs->{unit_source_value} ) =
261 16 inverse_map( 'unit', $exposure->{unit}, 'label', $self, 'exposures.unit' );
262  
263 # TEMPORARY: BFF only accepts numeric
264 16 $obs->{value_as_concept_id} = '';
265 16 $obs->{value_as_string} = '';
266  
267 # TEMPORARY SOLUTION: Setting defaults
268 # mrueda: Apr-2025
269 16 $obs->{observation_type_concept_id} = $DEFAULT->{concept_id};
270  
271 16 _attach_common( $self, $obs, $exposure, $person_id );
272  
273 16 push @observations, $obs;
274 }
275 69 ▶ $omop_ref->{OBSERVATION} = \@observations if @observations;
Branch: 1 branch
BranchTrueFalseTotal
if @observations 14 55 69
276 }
277  
278 # Convert BFF phenotypicFeatures into additional OMOP OBSERVATION rows.
279 69 ▶sub _map_phenotypicFeatures {
Subroutine _map_phenotypicFeatures: called CC=9 SCAR=22.1
280 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
281  
282 69 my @observations;
283  
284 69 ▶ for my $feature ( @{ $bff->{phenotypicFeatures} // [] } ) {
Condition100%2/2
$bff->{"phenotypicFeatures"} // []
l!l
3039
MC/DC100%1/1
$bff->{"phenotypicFeatures"} // []
$bff->{"phenotypicFeatures"}
Truth table100%2/2
A
A= $bff->{"phenotypicFeatures"}
Aresult
11
00
285  
286 50 my $obs;
287  
288 next
289 50 ▶ if ( $feature->{excluded} && $feature->{excluded} == JSON::PP::true() );
Branch: 1 branch
BranchTrueFalseTotal
if $feature->{"excluded"} and $feature->{"excluded"} == JSON::PP::true() 0 50 50
Condition33%1/3
$feature->{"excluded"} and $feature->{"excluded"} == JSON::PP::true()
!ll&&!rl&&r
5000
MC/DC0%0/2
$feature->{"excluded"} and $feature->{"excluded"} == JSON::PP::true()
$feature->{"excluded"} $feature->{"excluded"} == JSON::PP::true()
Truth table33%1/3
A and B
A= $feature->{"excluded"}B= $feature->{"excluded"} == JSON::PP::true()
ABresult
0X0
100
111
290  
291 50 $obs->{observation_id} = ++$OBSERVATION_ID_COUNT;
292  
293 # e.g., $feature->{featureType} used in a generic mapping:
294 (
295 $obs->{observation_concept_id},
296 $obs->{observation_source_value},
297 $obs->{observation_source_concept_id}
298 ) =
299 inverse_map( 'phenotypicFeature', $feature->{featureType},
300 50 'label', $self );
301  
302 # Date
303 50 ▶ if ( $feature->{onset}{iso8601duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($feature->{'onset'}{'iso8601duration'}) { } 4 46 50
304 $obs->{observation_date} = get_date_at_age(
305 $feature->{onset}{iso8601duration},
306 $omop_ref->{PERSON}{year_of_birth}
307 4 );
308 }
309 else {
310 46 $obs->{observation_date} = $DEFAULT->{date};
311 }
312  
313 # BFF only accepts numeric
314 $obs->{value_as_number} =
315 50 ▶ defined $feature->{value} ? $feature->{value} : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $feature->{'value'} ? : 0 50 50
316  
317 ( $obs->{unit_concept_id}, $obs->{unit_source_value} ) =
318 50 inverse_map( 'unit', $feature->{unit}, 'label', $self, 'phenotypicFeatures.unit' );
319  
320 # TEMPORARY: BFF only accepts numeric
321 50 $obs->{value_as_concept_id} = '';
322 50 $obs->{value_as_string} = '';
323  
324 # TEMPORARY SOLUTION: Setting defaults
325 # mrueda: Apr-2025
326 50 $obs->{observation_type_concept_id} = $DEFAULT->{concept_id};
327  
328 50 _attach_common( $self, $obs, $feature, $person_id );
329  
330 50 push @observations, $obs;
331 }
332  
333 # Append these observations to any existing ones.
334 69 ▶ if (@observations) {
Branch: 1 branch
BranchTrueFalseTotal
if (@observations) 30 39 69
335 30 ▶ if ( exists $omop_ref->{OBSERVATION} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $omop_ref->{'OBSERVATION'}) { } 14 16 30
336 14 push @{ $omop_ref->{OBSERVATION} }, @observations;
337 }
338 else {
339 16 $omop_ref->{OBSERVATION} = \@observations;
340 }
341 }
342 }
343  
344 # Convert BFF interventionsOrProcedures into OMOP PROCEDURE_OCCURRENCE rows.
345 69 ▶sub _map_interventionsOrProcedures {
Subroutine _map_interventionsOrProcedures: called CC=6 SCAR=17.9
346 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
347 69 my @procedures;
348  
349 69 ▶ for my $proc ( @{ $bff->{interventionsOrProcedures} // [] } ) {
Condition100%2/2
$bff->{"interventionsOrProcedures"} // []
l!l
1851
MC/DC100%1/1
$bff->{"interventionsOrProcedures"} // []
$bff->{"interventionsOrProcedures"}
Truth table100%2/2
A
A= $bff->{"interventionsOrProcedures"}
Aresult
11
00
350  
351 42 my $procedure;
352  
353 $procedure->{procedure_occurrence_id} =
354 42 ++$PROCEDURE_OCCURRENCE_ID_COUNT;
355  
356 (
357 $procedure->{procedure_concept_id},
358 $procedure->{procedure_source_value},
359 $procedure->{procedure_source_concept_id}
360 )
361 42 = inverse_map( 'procedure', $proc->{procedureCode}, 'label', $self );
362  
363 $procedure->{procedure_date} =
364 defined $proc->{dateOfProcedure}
365 ? $proc->{dateOfProcedure}
366 42 ▶ : $DEFAULT->{date};
Branch: 1 branch
BranchTrueFalseTotal
defined $proc->{'dateOfProcedure'} ? : 31 11 42
367  
368 $procedure->{procedure_datetime} =
369 defined $proc->{dateOfProcedure}
370 ? map_iso8601_date2timestamp( $proc->{dateOfProcedure} )
371 42 ▶ : $DEFAULT->{timestamp};
Branch: 1 branch
BranchTrueFalseTotal
defined $proc->{'dateOfProcedure'} ? : 31 11 42
372  
373 # TEMPORARY SOLUTION: Setting defaults
374 # mrueda: Apr-2025
375 42 $procedure->{procedure_type_concept_id} = $DEFAULT->{concept_id};
376  
377 42 _attach_common( $self, $procedure, $proc, $person_id );
378  
379 42 push @procedures, $procedure;
380 }
381 69 ▶ $omop_ref->{PROCEDURE_OCCURRENCE} = \@procedures if @procedures;
Branch: 1 branch
BranchTrueFalseTotal
if @procedures 18 51 69
382 }
383  
384 # Convert BFF measures into OMOP MEASUREMENT rows.
385 69 ▶sub _map_measurements {
Subroutine _map_measurements: called CC=15 SCAR=27.4
386 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
387 69 my @measurements;
388  
389 69 ▶ for my $measure ( @{ $bff->{measures} // [] } ) {
Condition100%2/2
$bff->{"measures"} // []
l!l
3138
MC/DC100%1/1
$bff->{"measures"} // []
$bff->{"measures"}
Truth table100%2/2
A
A= $bff->{"measures"}
Aresult
11
00
390  
391 109 my $m;
392 109 $m->{measurement_id} = ++$MEASUREMENT_ID_COUNT;
393  
394 (
395 $m->{measurement_concept_id},
396 $m->{measurement_source_value},
397 $m->{measurement_source_concept_id}
398 ) =
399 109 inverse_map( 'measurement', $measure->{assayCode}, 'label', $self );
400 109 $m->{measurement_date} = $measure->{date};
401  
402 # Determine measurement value. Beacon permits either a quantity or an
403 # ontology term; the latter maps to OMOP value_as_concept_id.
404 109 ▶ if ( exists $measure->{measurementValue} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $measure->{'measurementValue'}) { } 108 1 109
405 108 my $value = $measure->{measurementValue};
406  
407 108 ▶ if ( ref($value) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH') { } 107 1 108
408 my $quantity =
409 exists $value->{quantity} ? $value->{quantity}
410 107 ▶ : exists $value->{value} && exists $value->{unit} ? $value
Branch: 2 branches
BranchTrueFalseTotal
exists $value->{'quantity'} ? : 101 6 107
exists $value->{'value'} && exists $value->{'unit'} ? : 0 6 6
Condition33%1/3
exists $value->{"value"} and exists $value->{"unit"}
!ll&&!rl&&r
600
MC/DC0%0/2
exists $value->{"value"} and exists $value->{"unit"}
exists $value->{"value"} exists $value->{"unit"}
Truth table33%1/3
A and B
A= exists $value->{"value"}B= exists $value->{"unit"}
ABresult
0X0
100
111
411 : undef;
412  
413 107 ▶ if ( ref($quantity) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $quantity eq 'HASH') { } 101 6 107
elsif (exists $value->{'id'}) { } 6 0 6
414 101 ▶ $m->{value_as_number} = $quantity->{value} // -1;
Condition50%1/2
$quantity->{"value"} // -1
l!l
1010
MC/DC0%0/1
$quantity->{"value"} // -1
$quantity->{"value"}
Truth table50%1/2
A
A= $quantity->{"value"}
Aresult
11
00
415 101 ▶ if ( ref( $quantity->{referenceRange} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $quantity->{"referenceRange"} eq "HASH") 88 13 101
416 88 $m->{range_low} = $quantity->{referenceRange}{low};
417 88 $m->{range_high} = $quantity->{referenceRange}{high};
418 }
419 101 ▶ if ( ref( $quantity->{unit} ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $quantity->{"unit"} eq "HASH") 101 0 101
420 (
421 $m->{unit_concept_id},
422 $m->{unit_source_value},
423 $m->{unit_source_concept_id}
424 ) =
425 101 inverse_map( 'unit', $quantity->{unit}, 'label', $self,
426 'measures.measurementValue.quantity.unit' );
427 }
428 }
429 elsif ( exists $value->{id} ) {
430 6 ( $m->{value_as_concept_id}, $m->{value_source_value} ) =
431 inverse_map( 'measurementValue', $value, 'label', $self );
432 }
433 else {
434 0 $m->{value_as_number} = -1;
435 }
436 }
437 else {
438 1 $m->{value_as_number} = $value;
439 }
440 }
441 else {
442 1 $m->{value_as_number} = -1;
443 }
444  
445 $m->{value_source_value} = $m->{value_as_number}
446 109 ▶ unless exists $m->{value_source_value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $m->{"value_source_value"} 103 6 109
447  
448 # A BFF measurement procedure describes how the assay was performed;
449 # it is not OMOP provenance and must not populate a Type Concept field.
450 109 $m->{measurement_type_concept_id} = $DEFAULT->{concept_id};
451  
452 109 _attach_common( $self, $m, $measure, $person_id );
453  
454 109 push @measurements, $m;
455 }
456 69 ▶ $omop_ref->{MEASUREMENT} = \@measurements if @measurements;
Branch: 1 branch
BranchTrueFalseTotal
if @measurements 31 38 69
457 }
458  
459 # Convert BFF treatments into OMOP DRUG_EXPOSURE rows.
460 69 ▶sub _map_treatments {
Subroutine _map_treatments: called CC=18 SCAR=29.1
461 69 my ( $self, $bff, $omop_ref, $person_id ) = @_;
462 69 my @treatments;
463  
464 69 ▶ for my $treatment ( @{ $bff->{treatments} // [] } ) {
Condition100%2/2
$bff->{"treatments"} // []
l!l
3039
MC/DC100%1/1
$bff->{"treatments"} // []
$bff->{"treatments"}
Truth table100%2/2
A
A= $bff->{"treatments"}
Aresult
11
00
465 32 my $drug;
466  
467 32 $drug->{drug_exposure_id} = ++$DRUG_EXPOSURE_ID_COUNT;
468  
469 # TEMPORARY
470 32 $drug->{drug_type_concept_id} = 0;
471  
472 (
473 $drug->{drug_concept_id},
474 $drug->{drug_source_value},
475 $drug->{drug_source_concept_id}
476 ) =
477 inverse_map( 'treatment', $treatment->{treatmentCode},
478 32 'label', $self );
479  
480 32 ▶ if ( exists $treatment->{doseIntervals}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $treatment->{'doseIntervals'} and @{$treatment->{'doseIntervals'};}) { } 22 10 32
Condition66%2/3
exists $treatment->{"doseIntervals"} and @{$treatment->{"doseIntervals"};}
!ll&&!rl&&r
10022
MC/DC50%1/2
exists $treatment->{"doseIntervals"} and @{$treatment->{"doseIntervals"};}
exists $treatment->{"doseIntervals"} @{$treatment->{"doseIntervals"};}
Truth table66%2/3
A and B
A= exists $treatment->{"doseIntervals"}B= @{$treatment->{"doseIntervals"};}
ABresult
0X0
100
111
481 22 and @{ $treatment->{doseIntervals} } )
482 {
483 22 my $dose = $treatment->{doseIntervals}[0];
484 22 ▶ $drug->{quantity} = $dose->{quantity}{value} // -1;
Condition100%2/2
$dose->{"quantity"}{"value"} // -1
l!l
211
MC/DC100%1/1
$dose->{"quantity"}{"value"} // -1
$dose->{"quantity"}{"value"}
Truth table100%2/2
A
A= $dose->{"quantity"}{"value"}
Aresult
11
00
485 22 $drug->{dose_unit_source_value} = $dose->{quantity}{unit}{label};
486  
487 }
488 else {
489 10 $drug->{quantity} = -1;
490 }
491  
492 ( $drug->{route_concept_id}, $drug->{route_source_value} ) =
493 inverse_map( 'route', $treatment->{routeOfAdministration},
494 32 'label', $self );
495  
496 32 my ( $interval_start, $interval_end );
497 32 ▶ if ( exists $treatment->{doseIntervals}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $treatment->{"doseIntervals"} and @{$treatment->{"doseIntervals"};}) 22 10 32
Condition66%2/3
exists $treatment->{"doseIntervals"} and @{$treatment->{"doseIntervals"};}
!ll&&!rl&&r
10022
MC/DC50%1/2
exists $treatment->{"doseIntervals"} and @{$treatment->{"doseIntervals"};}
exists $treatment->{"doseIntervals"} @{$treatment->{"doseIntervals"};}
Truth table66%2/3
A and B
A= exists $treatment->{"doseIntervals"}B= @{$treatment->{"doseIntervals"};}
ABresult
0X0
100
111
498 22 and @{ $treatment->{doseIntervals} } )
499 {
500 22 ▶ my $interval = $treatment->{doseIntervals}[0]{interval} // {};
Condition100%2/2
$treatment->{"doseIntervals"}[0]{"interval"} // {}
l!l
211
MC/DC100%1/1
$treatment->{"doseIntervals"}[0]{"interval"} // {}
$treatment->{"doseIntervals"}[0]{"interval"}
Truth table100%2/2
A
A= $treatment->{"doseIntervals"}[0]{"interval"}
Aresult
11
00
501 22 $interval_start = $interval->{start};
502 22 $interval_end = $interval->{end};
503 }
504  
505 # Prefer explicit dose interval dates. If only one bound is present,
506 # reuse it for both OMOP-required date fields.
507 32 ▶ if ( $interval_start || $interval_end ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($interval_start or $interval_end) { } 21 11 32
elsif (ref $treatment->{'ageOfOnset'} eq 'HASH' and ref $treatment->{'ageOfOnset'}{'age'} eq 'HASH' and $treatment->{'ageOfOnset'}{'age'}{'iso8601duration'}) { } 4 7 11
Condition66%2/3
$interval_start or $interval_end
l!l&&r!l&&!r
21011
MC/DC50%1/2
$interval_start or $interval_end
$interval_start $interval_end
Truth table66%2/3
A or B
A= $interval_startB= $interval_end
ABresult
1X1
011
000
508 21 ▶ my $resolved_start = $interval_start // $interval_end;
Condition33%1/3
$interval_start // $interval_end
l!l&&r!l&&!r
2100
MC/DC0%0/2
$interval_start // $interval_end
$interval_start $interval_end
Truth table33%1/3
A // B
A= $interval_startB= $interval_end
ABresult
1X1
011
000
509 21 ▶ my $resolved_end = $interval_end // $interval_start;
Condition66%2/3
$interval_end // $interval_start
l!l&&r!l&&!r
2010
MC/DC0%0/2
$interval_end // $interval_start
$interval_end $interval_start
Truth table66%2/3
A // B
A= $interval_endB= $interval_start
ABresult
1X1
011
000
510  
511 $drug->{drug_exposure_start_date} =
512 21 map_iso8601_timestamp2date($resolved_start);
513 $drug->{drug_exposure_end_date} =
514 21 map_iso8601_timestamp2date($resolved_end);
515 }
516 ▶ elsif ( ref($treatment->{ageOfOnset}) eq 'HASH'
Condition66%4/6
ref $treatment->{"ageOfOnset"} eq "HASH" and ref $treatment->{"ageOfOnset"}{"age"} eq "HASH" and $treatment->{"ageOfOnset"}{"age"}{"iso8601duration"}
!ll&&!rl&&r
704
ref $treatment->{"ageOfOnset"} eq "HASH" and ref $treatment->{"ageOfOnset"}{"age"} eq "HASH"
!ll&&!rl&&r
704
MC/DC33%1/3
ref $treatment->{"ageOfOnset"} eq "HASH" and ref $treatment->{"ageOfOnset"}{"age"} eq "HASH" and $treatment->{"ageOfOnset"}{"age"}{"iso8601duration"}
ref $treatment->{"ageOfOnset"} eq "HASH" ref $treatment->{"ageOfOnset"}{"age"} eq "HASH" $treatment->{"ageOfOnset"}{"age"}{"iso8601duration"}
Truth table50%2/4
A and B and C
A= ref $treatment->{"ageOfOnset"} eq "HASH"B= ref $treatment->{"ageOfOnset"}{"age"} eq "HASH"C= $treatment->{"ageOfOnset"}{"age"}{"iso8601duration"}
ABCresult
0XX0
10X0
1100
1111
517 && ref($treatment->{ageOfOnset}{age}) eq 'HASH'
518 && $treatment->{ageOfOnset}{age}{iso8601duration} ) {
519 $drug->{drug_exposure_start_date} =
520 get_date_at_age( $treatment->{ageOfOnset}{age}{iso8601duration},
521 4 $omop_ref->{PERSON}{year_of_birth} );
522  
523 # TEMPORARY
524 4 $drug->{drug_exposure_end_date} = $drug->{drug_exposure_start_date};
525 }
526 else {
527 7 $drug->{drug_exposure_start_date} = $DEFAULT->{date};
528  
529 # TEMPORARY
530 7 $drug->{drug_exposure_end_date} = $drug->{drug_exposure_start_date};
531 }
532  
533 $drug->{days_supply} =
534 $treatment->{cumulativeDose}{unit}{label}
535 ? convert_label_to_days(
536 $treatment->{cumulativeDose}{unit}{label},
537 $treatment->{cumulativeDose}{value}
538 )
539 32 ▶ : '';
Branch: 1 branch
BranchTrueFalseTotal
$treatment->{'cumulativeDose'}{'unit'}{'label'} ? : 22 10 32
540  
541 32 _attach_common( $self, $drug, $treatment, $person_id );
542  
543 32 push @treatments, $drug;
544 }
545 69 ▶ $omop_ref->{DRUG_EXPOSURE} = \@treatments if @treatments;
Branch: 1 branch
BranchTrueFalseTotal
if @treatments 30 39 69
546 }
547  
548 672 ▶sub inverse_map {
Subroutine inverse_map: called CC=5 SCAR=16.1
Pod undocumented
549 672 my ( $mapping_type, $hashref, $key, $self, $source_field ) = @_;
550  
551 672 ▶ $source_field //= $SOURCE_FIELDS{$mapping_type};
Condition100%3/3
$source_field //= $SOURCE_FIELDS{$mapping_type}
l!l&&r!l&&!r
1665051
MC/DC100%2/2
$source_field //= $SOURCE_FIELDS{$mapping_type}
$source_field $SOURCE_FIELDS{$mapping_type}
Truth table100%3/3
A //= B
A= $source_fieldB= $SOURCE_FIELDS{$mapping_type}
ABresult
1X1
011
000
552  
553 672 my $config = $INVERSE_CONFIG{$mapping_type};
554 672 ▶ unless ($config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config) 1 671 672
555 1 warn "Unknown mapping type <$mapping_type>; returning (0,'')\n";
556 1 return ( 0, '', 0 );
557 }
558  
559 671 ▶ my $term = ref($hashref) eq 'HASH'
Branch: 2 branches
BranchTrueFalseTotal
ref $hashref eq 'HASH' ? : 617 54 671
defined $hashref ? : 0 54 54
560 ? $hashref
561 : { $key => defined $hashref ? $hashref : q{} };
562 my $result = resolve_standard_concept(
563 {
564 self => $self,
565 term => $term,
566 mapping_type => $mapping_type,
567 source_field => $source_field,
568 671 %{$config},
569 }
570 );
571  
572 return (
573 $result->{concept_id},
574 $result->{source_value},
575 $result->{source_concept_id},
576 671 $result,
577 );
578 }
579  
580 376 ▶sub _attach_common {
Subroutine _attach_common: called CC=8 SCAR=20.9
581  
582 # Individuals default schema does not provice anything related to visit
583 # Taking information if Convert-Pheno set it
584  
585 376 my ( $self, $row, $ent, $pid ) = @_;
586 376 ▶ my $v = $ent->{_visit} // undef;
Condition100%2/2
$ent->{"_visit"} // undef
l!l
247129
MC/DC100%1/1
$ent->{"_visit"} // undef
$ent->{"_visit"}
Truth table100%2/2
A
A= $ent->{"_visit"}
Aresult
11
00
587 376 ▶ my $visit_occurrence_id = $v->{occurrence_id} // '';
Condition100%2/2
$v->{"occurrence_id"} // ""
l!l
247129
MC/DC100%1/1
$v->{"occurrence_id"} // ""
$v->{"occurrence_id"}
Truth table100%2/2
A
A= $v->{"occurrence_id"}
Aresult
11
00
588 376 ▶ if ( defined $visit_occurrence_id
Branch: 1 branch
BranchTrueFalseTotal
if (defined $visit_occurrence_id and $visit_occurrence_id ne "" and not &looks_like_number($visit_occurrence_id)) 19 357 376
Condition83%5/6
defined $visit_occurrence_id and $visit_occurrence_id ne "" and not &looks_like_number($visit_occurrence_id)
!ll&&!rl&&r
12922819
defined $visit_occurrence_id and $visit_occurrence_id ne ""
!ll&&!rl&&r
0129247
MC/DC66%2/3
defined $visit_occurrence_id and $visit_occurrence_id ne "" and not &looks_like_number($visit_occurrence_id)
defined $visit_occurrence_id $visit_occurrence_id ne "" not &looks_like_number($visit_occurrence_id)
Truth table75%3/4
A and B and not(C)
A= defined $visit_occurrence_idB= $visit_occurrence_id ne ""C= not &looks_like_number($visit_occurrence_id)
ABCresult
0XX0
10X0
1100
1111
589 && $visit_occurrence_id ne ''
590 && !looks_like_number($visit_occurrence_id) )
591 {
592 19 ▶ my $visit_key = $v->{composite} // $visit_occurrence_id;
Condition33%1/3
$v->{"composite"} // $visit_occurrence_id
l!l&&r!l&&!r
1900
MC/DC0%0/2
$v->{"composite"} // $visit_occurrence_id
$v->{"composite"} $visit_occurrence_id
Truth table33%1/3
A // B
A= $v->{"composite"}B= $visit_occurrence_id
ABresult
1X1
011
000
593 19 $visit_occurrence_id =
594 allocate_surrogate_integer( $self, 'omop_visit_occurrence_id', $visit_key );
595 }
596 376 $row->{visit_occurrence_id} = $visit_occurrence_id;
597 376 ▶ $row->{visit_detail_id} = $v->{detail_id} // '';
Condition100%2/2
$v->{"detail_id"} // ""
l!l
1375
MC/DC100%1/1
$v->{"detail_id"} // ""
$v->{"detail_id"}
Truth table100%2/2
A
A= $v->{"detail_id"}
Aresult
11
00
598 376 $row->{person_id} = $pid;
599 }
600  
601 136 1;
« lib/Convert/Pheno/Source/TablePackage.pm ↑ Summary lib/Convert/Pheno/CDISC/ODM/Detector.pm »