lib/Convert/Pheno/Mapping/Shared.pm

filter: Statementstmt 90.4% 311 / 344 Branchbran 70.2% 118 / 168 Conditioncond 62.9% 95 / 151 MC/DCmcdc 36.0% 31 / 86 Subroutinesub 92.5% 50 / 54 Podpod 0.0% 0 / 28 totaltotal 72.8% 605 / 831 CC 144 SCAR 30.1
CC
144
Cov
79%
get_info
81.7
map_ontology_term
41.8
record_term_audit
23.7
CRAP 20.3
« lib/Convert/Pheno/OMOP/Vocabulary.pm ↑ Summary lib/Convert/Pheno/FHIR/Util.pm »
1 package Convert::Pheno::Mapping::Shared;
2  
3 143 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 143 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 143 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 143 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 143 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 143 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 143 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 143 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 143 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 143 ▶use DateTime::Format::ISO8601;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 143 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 143 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 143 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 143 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 143 ▶use Convert::Pheno::Audit::Terminology;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 143 ▶use Convert::Pheno::DB::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 143 ▶use Convert::Pheno::Utils::Default qw(get_defaults);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 143 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 143 ▶use open qw(:std :encoding(UTF-8));
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 143 our @EXPORT =
24 qw(map_ontology_term dotify_and_coerce_number get_current_utc_iso8601_timestamp map_iso8601_date2timestamp map_iso8601_timestamp2date get_date_component map_reference_range map_reference_range_csv map_age_range map2redcap_dict map2ohdsi convert2boolean get_age_from_date_and_birthday get_date_at_age generate_random_alphanumeric_string allocate_surrogate_integer map_operator_concept_id map_info_field map_omop_visit_occurrence convert_date_to_iso8601 validate_format get_metaData get_info merge_omop_tables convert_label_to_days finalize_term_audit record_term_audit term_audit_review);
25  
26 143 my $DEFAULT = get_defaults();
27 143 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 143 ▶use constant MAX_OMOP_INTEGER => 2_147_483_647;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 80001 ▶sub _public_ontology_entry {
Subroutine _public_ontology_entry: called CC=1 SCAR=0.0
31 80001 my ($entry) = @_;
32 80001 my %public_entry = %{$entry};
33 80001 delete $public_entry{search_resolution};
34 80001 delete $public_entry{match_source};
35 80001 delete $public_entry{search_evidence};
36 80001 return \%public_entry;
37 }
38  
39 5385 ▶sub _term_match_status {
Subroutine _term_match_status: called CC=3 SCAR=11.0
40 5385 my ($entry) = @_;
41 return 'not_found'
42 5385 ▶ if !defined $entry->{id} || $entry->{id} =~ /:NA0000\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $entry->{"id"} or $entry->{"id"} =~ /:NA0000\z/ 277 5108 5385
Condition66%2/3
not defined $entry->{"id"} or $entry->{"id"} =~ /:NA0000\z/
l!l&&r!l&&!r
02775108
MC/DC50%1/2
not defined $entry->{"id"} or $entry->{"id"} =~ /:NA0000\z/
not defined $entry->{"id"} $entry->{"id"} =~ /:NA0000\z/
Truth table66%2/3
not(A) or B
A= not defined $entry->{"id"}B= $entry->{"id"} =~ /:NA0000\z/
ABresult
1X1
011
000
43 5108 return 'matched';
44 }
45  
46 3474 ▶sub _term_decision_reason {
Subroutine _term_decision_reason: called CC=12 SCAR=26.2
47 3474 my ( $status, $resolution, $evidence ) = @_;
48 3474 ▶ return 'direct_mapping'
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "configured" or $resolution eq "direct_term" 3 3471 3474
Condition66%2/3
$status eq "configured" or $resolution eq "direct_term"
l!l&&r!l&&!r
303471
MC/DC50%1/2
$status eq "configured" or $resolution eq "direct_term"
$status eq "configured" $resolution eq "direct_term"
Truth table66%2/3
A or B
A= $status eq "configured"B= $resolution eq "direct_term"
ABresult
1X1
011
000
49 if $status eq 'configured' || $resolution eq 'direct_term';
50 3471 ▶ return 'not_searched' if $status eq 'not_searched';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "not_searched" 19 3452 3471
51 return 'score_below_threshold'
52 if $status eq 'not_found'
53 3452 ▶ && defined $evidence->{best_candidate_score};
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "not_found" and defined $evidence->{"best_candidate_score"} 1 3451 3452
Condition100%3/3
$status eq "not_found" and defined $evidence->{"best_candidate_score"}
!ll&&!rl&&r
345011
MC/DC100%2/2
$status eq "not_found" and defined $evidence->{"best_candidate_score"}
$status eq "not_found" defined $evidence->{"best_candidate_score"}
Truth table100%3/3
A and B
A= $status eq "not_found"B= defined $evidence->{"best_candidate_score"}
ABresult
0X0
100
111
54 3451 ▶ return 'no_candidate' if $status eq 'not_found';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "not_found" 1 3450 3451
55 return 'spelling_variant_accepted'
56 3450 ▶ if $status eq 'matched' && $evidence->{spelling_variant};
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "matched" and $evidence->{"spelling_variant"} 1 3449 3450
Condition66%2/3
$status eq "matched" and $evidence->{"spelling_variant"}
!ll&&!rl&&r
034491
MC/DC50%1/2
$status eq "matched" and $evidence->{"spelling_variant"}
$status eq "matched" $evidence->{"spelling_variant"}
Truth table66%2/3
A and B
A= $status eq "matched"B= $evidence->{"spelling_variant"}
ABresult
0X0
100
111
57 3449 ▶ return 'similarity_accepted' if $resolution eq 'similarity';
Branch: 1 branch
BranchTrueFalseTotal
if $resolution eq "similarity" 0 3449 3449
58 3449 ▶ return 'exact_match' if $resolution eq 'exact';
Branch: 1 branch
BranchTrueFalseTotal
if $resolution eq "exact" 3449 0 3449
59 0 ▶ return 'source_fallback' if $resolution eq 'fallback_source';
Branch: 1 branch
BranchTrueFalseTotal
if $resolution eq "fallback_source" 0 0 0
60 0 return 'resolved';
61 }
62  
63 3850 ▶sub _term_audit_config {
Subroutine _term_audit_config: called CC=9 SCAR=25.6
64 3850 my ($self) = @_;
65 my $search = $self->can('search')
66 ? $self->search
67 3850 ▶ : $self->{search};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('search') ? : 3850 0 3850
68 my $text_similarity_method = $self->can('text_similarity_method')
69 ? $self->text_similarity_method
70 3850 ▶ : $self->{text_similarity_method};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('text_similarity_method') ? : 3850 0 3850
71 my $min_text_similarity_score = $self->can('min_text_similarity_score')
72 ? $self->min_text_similarity_score
73 3850 ▶ : $self->{min_text_similarity_score};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('min_text_similarity_score') ? : 3850 0 3850
74 my $levenshtein_weight = $self->can('levenshtein_weight')
75 ? $self->levenshtein_weight
76 3850 ▶ : $self->{levenshtein_weight};
Branch: 1 branch
BranchTrueFalseTotal
$self->can('levenshtein_weight') ? : 3850 0 3850
77  
78 return {
79 3850 ▶ search => defined $search ? $search : 'exact',
Branch: 4 branches
BranchTrueFalseTotal
defined $search ? : 3850 0 3850
defined $text_similarity_method ? : 3850 0 3850
defined $min_text_similarity_score ? : 3850 0 3850
defined $levenshtein_weight ? : 3850 0 3850
80 text_similarity_method => defined $text_similarity_method
81 ? $text_similarity_method
82 : 'cosine',
83 min_text_similarity_score =>
84 defined $min_text_similarity_score
85 ? $min_text_similarity_score
86 : 0.8,
87 levenshtein_weight => defined $levenshtein_weight
88 ? $levenshtein_weight
89 : 0.1,
90 };
91 }
92  
93 81174 ▶sub record_term_audit {
Subroutine record_term_audit: called CC=21 SCAR=31.6
Pod undocumented
94 81174 my ($arg) = @_;
95 81174 my $self = $arg->{self};
96 81174 ▶ return 1 unless defined $self->{term_audit_file} && length $self->{term_audit_file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"term_audit_file"} and length $self->{"term_audit_file"} 77324 3850 81174
Condition66%2/3
defined $self->{"term_audit_file"} and length $self->{"term_audit_file"}
!ll&&!rl&&r
7732403850
MC/DC50%1/2
defined $self->{"term_audit_file"} and length $self->{"term_audit_file"}
defined $self->{"term_audit_file"} length $self->{"term_audit_file"}
Truth table66%2/3
A and B
A= defined $self->{"term_audit_file"}B= length $self->{"term_audit_file"}
ABresult
0X0
100
111
97  
98 3850 ▶ my $entry = $arg->{term} || {};
Condition50%1/2
$arg->{'term'} || {}
l!l
38500
MC/DC0%0/1
$arg->{'term'} || {}
$arg->{'term'}
Truth table50%1/2
A
A= $arg->{'term'}
Aresult
11
00
99 3850 my $config = _term_audit_config($self);
100 3850 ▶ my $lookup_column = $arg->{lookup_column} // q{};
Condition100%2/2
$arg->{"lookup_column"} // ""
l!l
382723
MC/DC100%1/1
$arg->{"lookup_column"} // ""
$arg->{"lookup_column"}
Truth table100%2/2
A
A= $arg->{"lookup_column"}
Aresult
11
00
101 3850 ▶ my $status = $arg->{match_status} // _term_match_status($entry);
Condition66%2/3
$arg->{"match_status"} // _term_match_status($entry)
l!l&&r!l&&!r
40434460
MC/DC0%0/2
$arg->{"match_status"} // _term_match_status($entry)
$arg->{"match_status"} _term_match_status($entry)
Truth table66%2/3
A // B
A= $arg->{"match_status"}B= _term_match_status($entry)
ABresult
1X1
011
000
102 my $effective_search_mode = $arg->{effective_search_mode}
103 // ( $lookup_column eq 'id' || $lookup_column eq 'concept_id'
104 ? 'exact'
105 3850 ▶ : length($lookup_column) ? $config->{search} : 'not_used' );
Branch: 2 branches
BranchTrueFalseTotal
$lookup_column eq 'id' || $lookup_column eq 'concept_id' ? : 0 25 25
length $lookup_column ? : 2 23 25
Condition50%3/6
$arg->{"effective_search_mode"} // ($lookup_column eq "id" || $lookup_column eq "concept_id" ? "exact" : (length $lookup_column ? $config->{"search"} : "not_used"))
l!l&&r!l&&!r
3825250
$lookup_column eq "id" or $lookup_column eq "concept_id"
l!l&&r!l&&!r
0025
MC/DC33%1/3
$arg->{"effective_search_mode"} // ($lookup_column eq "id" || $lookup_column eq "concept_id" ? "exact" : (length $lookup_column ? $config->{"search"} : "not_used"))
$arg->{"effective_search_mode"} $lookup_column eq "id" $lookup_column eq "concept_id"
Truth table50%2/4
A // B or C
A= $arg->{"effective_search_mode"}B= $lookup_column eq "id"C= $lookup_column eq "concept_id"
ABCresult
1XX1
01X1
0011
0000
106 my $resolution = $arg->{lookup_resolution}
107 // $entry->{search_resolution}
108 3850 ▶ // ( $status eq 'matched' ? 'exact' : 'fallback_na' );
Branch: 1 branch
BranchTrueFalseTotal
$status eq 'matched' ? : 0 0 0
Condition60%3/5
($arg->{"lookup_resolution"} // $entry->{"search_resolution"}) // ($status eq "matched" ? "exact" : "fallback_na")
l!l
38500
$arg->{"lookup_resolution"} // $entry->{"search_resolution"}
l!l&&r!l&&!r
40434460
MC/DC0%0/2
($arg->{"lookup_resolution"} // $entry->{"search_resolution"}) // ($status eq "matched" ? "exact" : "fallback_na")
$arg->{"lookup_resolution"} $entry->{"search_resolution"}
Truth table66%2/3
A // B
A= $arg->{"lookup_resolution"}B= $entry->{"search_resolution"}
ABresult
1X1
011
000
109 my $fallback_action = $arg->{fallback_action}
110 3850 ▶ // ( $status eq 'not_found' ? 'na' : 'none' );
Branch: 1 branch
BranchTrueFalseTotal
$status eq 'not_found' ? : 1 3445 3446
Condition100%2/2
$arg->{"fallback_action"} // ($status eq "not_found" ? "na" : "none")
l!l
4043446
MC/DC100%1/1
$arg->{"fallback_action"} // ($status eq "not_found" ? "na" : "none")
$arg->{"fallback_action"}
Truth table100%2/2
A
A= $arg->{"fallback_action"}
Aresult
11
00
111 3850 ▶ my $evidence = $entry->{search_evidence} || {};
Condition100%2/2
$entry->{'search_evidence'} || {}
l!l
3452398
MC/DC100%1/1
$entry->{'search_evidence'} || {}
$entry->{'search_evidence'}
Truth table100%2/2
A
A= $entry->{'search_evidence'}
Aresult
11
00
112 my $decision_reason = $arg->{decision_reason}
113 3850 ▶ // _term_decision_reason( $status, $resolution, $evidence );
Condition66%2/3
$arg->{"decision_reason"} // _term_decision_reason($status, $resolution, $evidence)
l!l&&r!l&&!r
37634740
MC/DC0%0/2
$arg->{"decision_reason"} // _term_decision_reason($status, $resolution, $evidence)
$arg->{"decision_reason"} _term_decision_reason($status, $resolution, $evidence)
Truth table66%2/3
A // B
A= $arg->{"decision_reason"}B= _term_decision_reason($status, $resolution, $evidence)
ABresult
1X1
011
000
114 3850 my %retrieval_path_name = (
115 exact_index => 'exact_lookup',
116 strict_fts => 'all_tokens',
117 relaxed_fts => 'one_token_relaxed',
118 not_used => 'not_used',
119 );
120 my $retrieval_path = exists $retrieval_path_name{ $evidence->{candidate_strategy} // q{} }
121 ? $retrieval_path_name{ $evidence->{candidate_strategy} }
122 3850 ▶ : ( $effective_search_mode eq 'not_used' ? 'not_used' : q{} );
Branch: 2 branches
BranchTrueFalseTotal
exists $retrieval_path_name{$evidence->{'candidate_strategy'} // ''} ? : 3452 398 3850
$effective_search_mode eq 'not_used' ? : 23 375 398
Condition100%2/2
$evidence->{"candidate_strategy"} // ""
l!l
3452398
MC/DC100%1/1
$evidence->{"candidate_strategy"} // ""
$evidence->{"candidate_strategy"}
Truth table100%2/2
A
A= $evidence->{"candidate_strategy"}
Aresult
11
00
123  
124 3850 my $writer = $self->{_term_audit_writer};
125 3850 ▶ unless ($writer) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($writer) 13 3837 3850
126 $writer = Convert::Pheno::Audit::Terminology->new(
127 path => $self->{term_audit_file},
128 13 config => $config,
129 );
130 13 $self->{_term_audit_writer} = $writer;
131 }
132  
133 $writer->write_row(
134 {
135 row => $self->{current_row},
136 source_record => $arg->{source_record},
137 source_field => $arg->{source_field},
138 source_value => $arg->{source_value},
139 source_label => $arg->{source_label},
140 lookup_query => $arg->{lookup_query},
141 lookup_column => $lookup_column,
142 converted_term_label => $entry->{label},
143 converted_term_id => $entry->{id},
144 ontology => $arg->{ontology},
145 configured_search_mode => $config->{search},
146 effective_search_mode => $effective_search_mode,
147 text_similarity_method => $config->{text_similarity_method},
148 min_text_similarity_score => $config->{min_text_similarity_score},
149 levenshtein_weight => $config->{levenshtein_weight},
150 match_status => $status,
151 decision_reason => $decision_reason,
152 match_source => $arg->{match_source},
153 lookup_resolution => $resolution,
154 fallback_action => $fallback_action,
155 retrieval_path => $retrieval_path,
156 best_candidate_label => $evidence->{best_candidate_label},
157 best_candidate_id => $evidence->{best_candidate_id},
158 best_candidate_score => $evidence->{best_candidate_score},
159 score_margin => $evidence->{score_margin},
160 }
161 3850 );
162  
163 3850 return 1;
164 }
165  
166 269 ▶sub finalize_term_audit {
Subroutine finalize_term_audit: called CC=2 SCAR=6.9
Pod undocumented
167 269 my ($self) = @_;
168 269 ▶ return 1 unless exists $self->{_term_audit_writer};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_term_audit_writer"} 256 13 269
169  
170 13 my $writer = delete $self->{_term_audit_writer};
171 13 $writer->close;
172 13 $self->{_term_audit_review} = $writer->review;
173 13 return 1;
174 }
175  
176 158 ▶sub term_audit_review {
Subroutine term_audit_review: called CC=1 SCAR=0.0
Pod undocumented
177 158 my ($self) = @_;
178 158 return $self->{_term_audit_review};
179 }
180  
181 #############################
182 #############################
183 # HELPER SUBS FOR MAPPING #
184 #############################
185 #############################
186  
187 81031 ▶sub map_ontology_term {
Subroutine map_ontology_term: called CC=37 SCAR=37.3
Pod undocumented
188 81031 my ($arg) = @_;
189 81031 my $query = $arg->{query};
190 81031 my $ontology = $arg->{ontology};
191 81031 my $self = $arg->{self};
192 81031 ▶ my $column = $arg->{column} // 'label';
Condition50%1/2
$arg->{"column"} // "label"
l!l
810310
MC/DC0%0/1
$arg->{"column"} // "label"
$arg->{"column"}
Truth table50%1/2
A
A= $arg->{"column"}
Aresult
11
00
193 my $effective_search =
194 $column eq 'id' || $column eq 'concept_id'
195 ? 'exact'
196 81031 ▶ : ( $arg->{search} // $self->{search} // 'exact' );
Branch: 1 branch
BranchTrueFalseTotal
$column eq 'id' || $column eq 'concept_id' ? : 506 80525 81031
Condition62%5/8
$column eq "id" or $column eq "concept_id"
l!l&&r!l&&!r
450280525
($arg->{"search"} // $self->{"search"}) // "exact"
l!l
805250
$arg->{"search"} // $self->{"search"}
l!l&&r!l&&!r
0805250
MC/DC100%2/2
$column eq "id" or $column eq "concept_id"
$column eq "id" $column eq "concept_id"
MC/DC0%0/2
($arg->{"search"} // $self->{"search"}) // "exact"
$arg->{"search"} $self->{"search"}
Truth table100%3/3
A or B
A= $column eq "id"B= $column eq "concept_id"
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $arg->{"search"}B= $self->{"search"}
ABresult
1X1
011
000
197 my $profile_enabled =
198 81031 ▶ $self && defined $self->{debug} && $self->{debug} >= 2;
Condition83%5/6
$self && defined $self->{'debug'} && $self->{'debug'} >= 2
!ll&&!rl&&r
8102452
$self and defined $self->{"debug"}
!ll&&!rl&&r
0810247
MC/DC66%2/3
$self && defined $self->{'debug'} && $self->{'debug'} >= 2
$self defined $self->{"debug"} $self->{'debug'} >= 2
Truth table75%3/4
A and B && C
A= $selfB= defined $self->{"debug"}C= $self->{'debug'} >= 2
ABCresult
0XX0
10X0
1100
1111
199  
200 81031 ▶ if ($profile_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_enabled) 2 81029 81031
201 2 $self->{db_profile}{mapping}{requests}++;
202 2 $self->{db_profile}{ontology}{$ontology}{requests}++;
203 }
204  
205 # 1) Historical guard: pure numeric label searches used to create noisy
206 # matches in generic ontology lookups, so we still short-circuit them for
207 # label-like queries. Do not apply this to explicit concept_id resolution,
208 # or OMOP fallback by concept_id will never reach the DB.
209 return $DEFAULT->{ontology_term}
210 81031 ▶ if looks_like_number($query)
Branch: 1 branch
BranchTrueFalseTotal
if &looks_like_number($query) and $column ne "concept_id" and $column ne "id" 1010 80021 81031
Condition83%5/6
&looks_like_number($query) and $column ne "concept_id" and $column ne "id"
!ll&&!rl&&r
8002101010
&looks_like_number($query) and $column ne "concept_id"
!ll&&!rl&&r
795205011010
MC/DC66%2/3
&looks_like_number($query) and $column ne "concept_id" and $column ne "id"
&looks_like_number($query) $column ne "concept_id" $column ne "id"
Truth table75%3/4
A and B and C
A= &looks_like_number($query)B= $column ne "concept_id"C= $column ne "id"
ABCresult
0XX0
10X0
1100
1111
211 && $column ne 'concept_id'
212 && $column ne 'id';
213  
214 # 2) If already an object, assume pre‑mapped
215 80021 ▶ return $query if ref $query eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $query eq "HASH" 0 80021 80021
216  
217 # Cache lookup semantics as well as the query itself. Keeping the cache on
218 # the converter prevents results leaking between API requests.
219 80021 my $column_key = $column;
220 80021 ▶ my $result_key = $arg->{require_concept_id} ? 'with_concept_id' : 'term';
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'require_concept_id'} ? : 1 80020 80021
221 my $cache =
222 ( $self->{_ontology_term_cache} ||= {} )->{$ontology}{$column_key}
223 80021 ▶ {$result_key} ||= {};
Condition100%4/4
+($self->{"_ontology_term_cache"} ||= {})->{$ontology}{$column_key}{$result_key} ||= {}
l!l
79895126
$self->{"_ontology_term_cache"} ||= {}
l!l
79910111
MC/DC100%1/1
+($self->{"_ontology_term_cache"} ||= {})->{$ontology}{$column_key}{$result_key} ||= {}
+($self->{"_ontology_term_cache"} ||= {})->{$ontology}{$column_key}{$result_key}
MC/DC100%1/1
$self->{"_ontology_term_cache"} ||= {}
$self->{"_ontology_term_cache"}
Truth table100%2/2
A
A= +($self->{"_ontology_term_cache"} ||= {})->{$ontology}{$column_key}{$result_key}
Aresult
11
00
Truth table100%2/2
A
A= $self->{"_ontology_term_cache"}
Aresult
11
00
224  
225 # 3) Fast return on cache hit
226 80021 ▶ if ( exists $cache->{$query} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache->{$query}) 78081 1940 80021
227 78081 say "Skipping searching for <$query> in <$ontology> (cached)"
228 if DEVEL_MODE;
229 78081 ▶ if ($profile_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_enabled) 1 78080 78081
230 1 $self->{db_profile}{mapping}{cache_hits}++;
231 1 $self->{db_profile}{ontology}{$ontology}{cache_hits}++;
232 my $resolution =
233 1 ▶ $cache->{$query}{search_resolution} // 'fallback_na';
Condition50%1/2
$cache->{$query}{"search_resolution"} // "fallback_na"
l!l
10
MC/DC0%0/1
$cache->{$query}{"search_resolution"} // "fallback_na"
$cache->{$query}{"search_resolution"}
Truth table50%1/2
A
A= $cache->{$query}{"search_resolution"}
Aresult
11
00
234 1 $self->{db_profile}{final_resolution}{$resolution}++;
235 $self->{db_profile}{ontology}{$ontology}{final_resolution}
236 1 {$resolution}++;
237 }
238 78081 my $cached = { %{ $cache->{$query} }, match_source => 'cache' };
239 record_term_audit(
240 {
241 self => $self,
242 source_record => $arg->{source_record},
243 source_field => $arg->{source_field},
244 source_value => $arg->{source_value} // $query,
245 source_label => $arg->{source_label} // $query,
246 lookup_query => $query,
247 lookup_column => $column,
248 ontology => $ontology,
249 term => $cached,
250 effective_search_mode => $effective_search,
251 match_source => 'cache',
252 }
253 )
254 78081 ▶ unless exists $arg->{audit} && !$arg->{audit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{"audit"} and not $arg->{"audit"} 78081 0 78081
Condition55%5/9
exists $arg->{"audit"} and not $arg->{"audit"}
!ll&&!rl&&r
7808100
$arg->{"source_value"} // $query
l!l&&r!l&&!r
29463486180
$arg->{"source_label"} // $query
l!l&&r!l&&!r
35860422210
MC/DC0%0/2
exists $arg->{"audit"} and not $arg->{"audit"}
exists $arg->{"audit"} not $arg->{"audit"}
MC/DC0%0/2
$arg->{"source_value"} // $query
$arg->{"source_value"} $query
MC/DC0%0/2
$arg->{"source_label"} // $query
$arg->{"source_label"} $query
Truth table33%1/3
A and not(B)
A= exists $arg->{"audit"}B= not $arg->{"audit"}
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $arg->{"source_value"}B= $query
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $arg->{"source_label"}B= $query
ABresult
1X1
011
000
255 return $arg->{return_metadata}
256 78081 ▶ ? $cached
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'return_metadata'} ? : 0 78081 78081
257 : _public_ontology_entry($cached);
258 }
259  
260 # 4) --ohdsi-db
261 1940 ▶ if ( $ontology eq 'ohdsi' && !$self->{ohdsi_db} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ontology eq "ohdsi" and not $self->{"ohdsi_db"}) 1 1939 1940
Condition100%3/3
$ontology eq "ohdsi" and not $self->{"ohdsi_db"}
!ll&&!rl&&r
193721
MC/DC100%2/2
$ontology eq "ohdsi" and not $self->{"ohdsi_db"}
$ontology eq "ohdsi" not $self->{"ohdsi_db"}
Truth table100%3/3
A and not(B)
A= $ontology eq "ohdsi"B= not $self->{"ohdsi_db"}
ABresult
0X0
100
111
262  
263 #If -iomop and term not found in RAM <CONCEPT> die unless --ohdsi-db
264 1 ▶ if ( $self->{method} =~ /^omop2bff/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'method'} =~ /^omop2bff/) { } 1 0 1
265 1 die "Could not find concept_id:<$query> in provided CONCEPT table. "
266 . "Use --ohdsi-db to enable Athena‑OHDSI lookup.\n";
267 }
268  
269 # Any search that involves 'ohdsi' as an ontology (e.g., mapping file)
270 else {
271 0 die "You have to use --ohdsi-db to perform Athena‑OHDSI lookups.\n";
272 }
273 }
274  
275 # 5) Perform the lookup
276 1939 say "Searching for <$query> in <$ontology>…" if DEVEL_MODE;
277 1939 ▶ if ($profile_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_enabled) 1 1938 1939
278 1 $self->{db_profile}{mapping}{db_lookups}++;
279 1 $self->{db_profile}{ontology}{$ontology}{db_lookups}++;
280 }
281 my ( $id, $label, $concept_id, $search_resolution, $search_evidence ) =
282 get_ontology_terms(
283 {
284 self => $self,
285 sth_column_ref => $self->{sth}{$ontology}{$column},
286 query => $query,
287 ontology => $ontology,
288 databases => $self->{databases},
289 column => $column,
290 search => $effective_search,
291 text_similarity_method => $self->{text_similarity_method},
292 min_text_similarity_score => $self->{min_text_similarity_score},
293 levenshtein_weight => $self->{levenshtein_weight},
294 }
295 1939 );
296 1939 ▶ if ($profile_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile_enabled) 1 1938 1939
297 1 ▶ my $resolution = $search_resolution // 'fallback_na';
Condition50%1/2
$search_resolution // "fallback_na"
l!l
10
MC/DC0%0/1
$search_resolution // "fallback_na"
$search_resolution
Truth table50%1/2
A
A= $search_resolution
Aresult
11
00
298 1 $self->{db_profile}{final_resolution}{$resolution}++;
299 $self->{db_profile}{ontology}{$ontology}{final_resolution}
300 1 {$resolution}++;
301 }
302  
303 # 6) Store in cache
304 my $entry =
305 $arg->{require_concept_id}
306 ? {
307 1939 ▶ id => $id,
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'require_concept_id'} ? : 1 1938 1939
308 label => $label,
309 concept_id => $concept_id,
310 search_resolution => $search_resolution,
311 search_evidence => $search_evidence,
312 }
313 : {
314 id => $id,
315 label => $label,
316 search_resolution => $search_resolution,
317 search_evidence => $search_evidence,
318 };
319  
320 1939 ▶ my $match_source = _term_match_status($entry) eq 'matched'
Branch: 1 branch
BranchTrueFalseTotal
_term_match_status($entry) eq 'matched' ? : 1663 276 1939
321 ? 'db'
322 : 'fallback_na';
323 1939 $entry->{match_source} = $match_source;
324 1939 $cache->{$query} = $entry;
325 record_term_audit(
326 {
327 self => $self,
328 source_record => $arg->{source_record},
329 source_field => $arg->{source_field},
330 source_value => $arg->{source_value} // $query,
331 source_label => $arg->{source_label} // $query,
332 lookup_query => $query,
333 lookup_column => $column,
334 ontology => $ontology,
335 term => $entry,
336 effective_search_mode => $effective_search,
337 match_source => $match_source,
338 }
339 )
340 1939 ▶ unless exists $arg->{audit} && !$arg->{audit};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $arg->{"audit"} and not $arg->{"audit"} 1921 18 1939
Condition66%6/9
exists $arg->{"audit"} and not $arg->{"audit"}
!ll&&!rl&&r
1921018
$arg->{"source_value"} // $query
l!l&&r!l&&!r
12187030
$arg->{"source_label"} // $query
l!l&&r!l&&!r
12187030
MC/DC50%1/2
exists $arg->{"audit"} and not $arg->{"audit"}
exists $arg->{"audit"} not $arg->{"audit"}
MC/DC0%0/2
$arg->{"source_value"} // $query
$arg->{"source_value"} $query
MC/DC0%0/2
$arg->{"source_label"} // $query
$arg->{"source_label"} $query
Truth table66%2/3
A and not(B)
A= exists $arg->{"audit"}B= not $arg->{"audit"}
ABresult
0X0
100
111
Truth table66%2/3
A // B
A= $arg->{"source_value"}B= $query
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $arg->{"source_label"}B= $query
ABresult
1X1
011
000
341  
342 return $arg->{return_metadata}
343 1939 ▶ ? { %{$entry} }
Branch: 1 branch
BranchTrueFalseTotal
$arg->{'return_metadata'} ? : 19 1920 1939
344 : _public_ontology_entry($entry);
345 }
346  
347 1365599 ▶sub dotify_and_coerce_number {
Subroutine dotify_and_coerce_number: called CC=4 SCAR=13.9
Pod undocumented
348 1365599 my $val = shift;
349  
350 # Premature return
351 1365599 ▶ return undef unless ( defined $val && $val ne '' );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 98856 1266743 1365599
Condition66%2/3
defined $val and $val ne ""
!ll&&!rl&&r
0988561266743
MC/DC50%1/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table66%2/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
352  
353 # looks_like_number does not work with commas so we must tr first
354 1266743 ( my $tr_val = $val ) =~ tr/,/./;
355  
356 #print "#$val#$tr_val#\n";
357  
358 # coercing to number $tr_val
359 1266743 ▶ return looks_like_number($tr_val)
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($tr_val) ? : 766706 500037 1266743
360 ? 0 + $tr_val
361 : $val;
362 }
363  
364 75 ▶sub get_current_utc_iso8601_timestamp {
Subroutine get_current_utc_iso8601_timestamp: called CC=1 SCAR=0.0
Pod undocumented
365  
366 # Standard modules (gmtime()===>Coordinated Universal Time(UTC))
367 # NB: The T separates the date portion from the time-of-day portion.
368 # The Z on the end means UTC (that is, an offset-from-UTC of zero hours-minutes-seconds).
369 # - The Z is pronounced “Zulu”.
370 75 my $now = time();
371 75 return strftime( '%Y-%m-%dT%H:%M:%SZ', gmtime($now) );
372 }
373  
374 21632 ▶sub map_iso8601_date2timestamp {
Subroutine map_iso8601_date2timestamp: called CC=1 SCAR=0.0
Pod undocumented
375 21632 my $iso_str = shift;
376  
377 # Parse the ISO string into a DateTime object
378 21632 $iso_str =~ s/ /T/;
379 21632 my $dt = DateTime::Format::ISO8601->parse_datetime($iso_str);
380  
381 # Format it to the standardized ISO8601 timestamp,
382 # ensuring that if no time was provided, a default is used.
383 21632 return $dt->strftime('%Y-%m-%dT%H:%M:%SZ');
384 }
385  
386 43 ▶sub map_iso8601_timestamp2date {
Subroutine map_iso8601_timestamp2date: called CC=1 SCAR=0.0
Pod undocumented
387 43 my $iso_str = shift;
388 43 $iso_str =~ s/\s+/T/;
389  
390 # split on 'T' and take the date portion
391 43 my ($date) = split /T/, $iso_str;
392 43 return $date;
393 }
394  
395 83 ▶sub get_date_component {
Subroutine get_date_component: called CC=3 SCAR=11.0
Pod undocumented
396 83 my ( $date, $component ) = @_;
397 83 ▶ $component //= 'year';
Condition50%1/2
$component //= "year"
l!l
830
MC/DC0%0/1
$component //= "year"
$component
Truth table50%1/2
A
A= $component
Aresult
11
00
398 83 $date =~ s/T.*//; # get rid of 'T00:00:00Z'
399  
400 83 my @parts = split /-/, $date;
401 83 my %indexes = ( year => 0, month => 1, day => 2 );
402  
403 # Return the requested component if valid; otherwise, warn and return the year.
404 return exists $indexes{$component}
405 ? $parts[ $indexes{$component} ]
406 83 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
exists $indexes{$component} ? : 82 1 83
407 1 warn
408 "Invalid component <$component> requested. Returning year by default.\n";
409 1 $parts[ $indexes{'year'} ];
410 };
411 }
412  
413 5570 ▶sub map_reference_range {
Subroutine map_reference_range: called CC=2 SCAR=6.9
Pod undocumented
414 5570 my $arg = shift;
415 5570 my $field = $arg->{field};
416 5570 my $redcap_dict = $arg->{redcap_dict};
417 5570 my $unit = $arg->{unit};
418 5570 my %hash = ( low => 'Text Validation Min', high => 'Text Validation Max' );
419 my $hashref = {
420 unit => $unit,
421 5570 map { $_ => undef } qw(low high)
422 }; # Initialize low,high to undef
423 5570 for my $range (qw (low high)) {
424 11140 my $meta = $redcap_dict->field_meta($field);
425 $hashref->{$range} =
426 11140 dotify_and_coerce_number( $meta->{ $hash{$range} } );
427 }
428  
429 5570 return $hashref;
430 }
431  
432 961 ▶sub map_reference_range_csv {
Subroutine map_reference_range_csv: called CC=1 SCAR=0.0
Pod undocumented
433 961 my ( $unit, $range ) = @_;
434 961 $range->{unit} = $unit;
435 961 return $range;
436 }
437  
438 920 ▶sub map_age_range {
Subroutine map_age_range: called CC=2 SCAR=6.9
Pod undocumented
439 920 my $str = shift;
440  
441 # Premature return if not range
442 920 ▶ return { age =>
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\-|\+/ 199 721 920
443 { iso8601duration => 'P' . dotify_and_coerce_number($str) . 'Y' } }
444 unless $str =~ m/\-|\+/;
445  
446 # if range
447 721 $str =~ s/\+/\-999/; # from '70+' '70-999'
448 721 my ( $start, $end ) = split /\-/, $str;
449  
450 return {
451 721 ageRange => {
452 start => {
453 iso8601duration => 'P' . dotify_and_coerce_number($start) . 'Y'
454 },
455 end =>
456 { iso8601duration => 'P' . dotify_and_coerce_number($end) . 'Y' }
457 }
458 };
459 }
460  
461 0 ▶sub map2redcap_dict {
Subroutine map2redcap_dict: not called CC=2 SCAR=17.9
Pod undocumented
462 0 my $arg = shift;
463 my ( $redcap_dict, $participant, $field, $labels ) = (
464 $arg->{redcap_dict}, $arg->{participant},
465 $arg->{field}, $arg->{labels}
466 0 );
467  
468 # Options:
469 # labels = 1
470 # _labels
471 # labels = 0
472 # 'Field Note'
473  
474 # NB: Some numeric fields will get stringified at $participant->{$field}
475 return $labels
476 ? $redcap_dict->{$field}{_labels}{ $participant->{$field} }
477 0 ▶ : $redcap_dict->{$field}{'Field Note'};
Branch: 1 branch
BranchTrueFalseTotal
$labels ? : 0 0 0
478 }
479  
480 39965 ▶sub map2ohdsi {
Subroutine map2ohdsi: called CC=2 SCAR=6.9
Pod undocumented
481 39965 my $arg = shift;
482 my ( $ohdsi_dict, $concept_id, $self ) =
483 39965 ( $arg->{ohdsi_dict}, $arg->{concept_id}, $arg->{self} );
484  
485 #######################
486 # OPTION A: <CONCEPT> #
487 #######################
488  
489 # NB1: Here we don't win any speed over using %SEEN as ...
490 # .. we are already searching in a hash
491 # NB2: $concept_id is stringified by hash
492 39965 my ( $data, $id, $label, $vocabulary ) = ( (undef) x 4 );
493 39965 ▶ if ( exists $ohdsi_dict->{$concept_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ohdsi_dict->{$concept_id}) { } 39464 501 39965
494 39464 $id = $ohdsi_dict->{$concept_id}{concept_code};
495 39464 $label = $ohdsi_dict->{$concept_id}{concept_name};
496 39464 $vocabulary = $ohdsi_dict->{$concept_id}{vocabulary_id};
497 # OMOP has local vocabulary labels such as "Type Concept" or "Meas Type".
498 # Keep the vocabulary information but make it safe for Beacon CURIE-like ids.
499 39464 $vocabulary =~ s/\s+/_/g;
500 39464 $data = { id => qq($vocabulary:$id), label => $label };
501 }
502  
503 ######################
504 # OPTION B: External #
505 ######################
506  
507 else {
508 501 $data = map_ontology_term(
509 {
510 query => $concept_id,
511 column => 'concept_id',
512 ontology => 'ohdsi',
513 self => $self
514 }
515 );
516 }
517 39964 return $data;
518 }
519  
520 363 ▶sub convert2boolean {
Subroutine convert2boolean: called CC=5 SCAR=16.3
Pod undocumented
521 363 my $val = lc(shift);
522 return
523 363 ▶ ( $val eq 'true' || $val eq 'yes' ) ? JSON::XS::true
Branch: 2 branches
BranchTrueFalseTotal
$val eq 'true' || $val eq 'yes' ? : 111 252 363
$val eq 'false' || $val eq 'no' ? : 251 1 252
Condition66%4/6
$val eq "true" or $val eq "yes"
l!l&&r!l&&!r
0111252
$val eq "false" or $val eq "no"
l!l&&r!l&&!r
02511
MC/DC50%1/2
$val eq "true" or $val eq "yes"
$val eq "true" $val eq "yes"
MC/DC50%1/2
$val eq "false" or $val eq "no"
$val eq "false" $val eq "no"
Truth table66%2/3
A or B
A= $val eq "true"B= $val eq "yes"
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $val eq "false"B= $val eq "no"
ABresult
1X1
011
000
524 : ( $val eq 'false' || $val eq 'no' ) ? JSON::XS::false
525 : undef; # unknown = undef
526  
527 }
528  
529 19027 ▶sub get_age_from_date_and_birthday {
Subroutine get_age_from_date_and_birthday: called CC=6 SCAR=18.0
Pod undocumented
530 19027 my $arg = shift;
531 19027 ▶ my $birth_date = $arg->{birth_day} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $birth_date = $arg->{"birth_day"} or return 19027 0 19027
532 19027 ▶ my $current_date = $arg->{date} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $current_date = $arg->{"date"} or return 19027 0 19027
533  
534 # Assuming both dates are in a format like "YYYY-MM-DD" (or with spaces instead of a dash separator for the birth date)
535 # Split the dates into year, month, and day.
536 19027 my ( $birth_year, $birth_month, $birth_day ) = split /[-\s]+/, $birth_date;
537 19027 my ( $current_year, $current_month, $current_day ) = split /-/,
538 $current_date;
539  
540 # Calculate age based on year difference.
541 19027 my $age = $current_year - $birth_year;
542  
543 # If the current month/day is before the birthday month/day, subtract one year.
544 19027 ▶ if ( $current_month < $birth_month
Branch: 1 branch
BranchTrueFalseTotal
if ($current_month < $birth_month or $current_month == $birth_month and $current_day < $birth_day) 8988 10039 19027
Condition100%6/6
$current_month < $birth_month or $current_month == $birth_month and $current_day < $birth_day
l!l&&r!l&&!r
7780120810039
$current_month == $birth_month and $current_day < $birth_day
!ll&&!rl&&r
886211771208
MC/DC100%3/3
$current_month < $birth_month or $current_month == $birth_month and $current_day < $birth_day
$current_month < $birth_month $current_month == $birth_month $current_day < $birth_day
Truth table100%4/4
A or B and C
A= $current_month < $birth_monthB= $current_month == $birth_monthC= $current_day < $birth_day
ABCresult
1XX1
0111
00X0
0100
545 or ( $current_month == $birth_month && $current_day < $birth_day ) )
546 {
547 8988 $age--;
548 }
549  
550 # Return the age in ISO8601 duration format (e.g. "P31Y").
551 19027 return "P${age}Y";
552 }
553  
554 96 ▶sub get_date_at_age {
Subroutine get_date_at_age: called CC=2 SCAR=7.1
Pod undocumented
555 96 my ( $duration_iso, $birthdate_iso ) = @_;
556  
557 # Parse the birth date using ISO8601 format.
558 96 my $birthdate = DateTime::Format::ISO8601->parse_datetime($birthdate_iso);
559  
560 # Here we only handle durations expressed solely in years.
561 # For a string like "P31Y", extract the number 31.
562 96 my $years;
563 96 ▶ if ( $duration_iso =~ /^P(\d+)Y/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration_iso =~ /^P(\d+)Y/) { } 96 0 96
564 96 $years = $1;
565 }
566 else {
567 0 warn
568 "Unsupported duration format: $duration_iso. Only durations in full years (P<number>Y) are supported.";
569 }
570  
571 # Create a duration object for the extracted number of years.
572 96 my $duration = DateTime::Duration->new( years => $years );
573  
574 # Add the duration to the birth date.
575 96 my $date_at_age = $birthdate->clone->add_duration($duration);
576  
577 # Return the result in ISO format (YYYY-MM-DD)
578 96 return $date_at_age->ymd;
579 }
580  
581 0 ▶sub generate_random_alphanumeric_string {
Subroutine generate_random_alphanumeric_string: not called CC=1 SCAR=6.9
Pod undocumented
582  
583 #https://www.perlmonks.org/?node_id=233023
584 return join( '',
585 0 map { ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9 )[ rand 62 ] } 0 .. shift );
586 }
587  
588 0 ▶sub map_operator_concept_id {
Subroutine map_operator_concept_id: not called CC=3 SCAR=24.8
Pod undocumented
589 0 my $arg = shift;
590 0 my $id = $arg->{operator_concept_id};
591 0 my $val = $arg->{value_as_number};
592 0 my $unit = $arg->{unit};
593  
594 # Define hash for possible values
595 0 my %operator_concept_id = ( 4172704 => 'GT', 4172756 => 'LT' );
596  
597 # 4172703 => 'EQ';
598  
599 # $hasref will be used for return
600 0 my $hashref = undef;
601  
602 # Only for GT || LT
603 0 ▶ if ( exists $operator_concept_id{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $operator_concept_id{$id}) 0 0 0
604 $hashref = {
605 unit => $unit,
606 0 map { $_ => undef } qw(low high)
607 }; # Initialize low,high to undef
608 0 ▶ if ( $operator_concept_id{$id} eq 'GT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operator_concept_id{$id} eq 'GT') { } 0 0 0
609 0 $hashref->{high} = dotify_and_coerce_number($val);
610 }
611 else {
612 0 $hashref->{low} = dotify_and_coerce_number($val);
613 }
614 }
615 0 return $hashref;
616 }
617  
618 15640 ▶sub map_omop_visit_occurrence {
Subroutine map_omop_visit_occurrence: called CC=6 SCAR=18.0
Pod undocumented
619  
620 # key eq 'visit_occurrence_id'
621 # { '85' =>
622 # {
623 # 'admitting_source_concept_id' => 0,
624 # 'admitting_source_value' => undef,
625 # 'care_site_id' => '\\N',
626 # 'discharge_to_concept_id' => 0,
627 # 'discharge_to_source_value' => undef,
628 # 'person_id' => 1,
629 # 'preceding_visit_occurrence_id' => 82,
630 # 'provider_id' => '\\N',
631 # 'visit_concept_id' => 9201,
632 # 'visit_end_date' => '1981-08-19',
633 # 'visit_end_datetime' => '1981-08-19 00:00:00',
634 # 'visit_occurrence_id' => 85,
635 # 'visit_source_concept_id' => 0,
636 # 'visit_source_value' => '7879d5b2-1af2-49a7-a801-121de124c6af',
637 # 'visit_start_date' => '1981-08-18',
638 # 'visit_start_datetime' => '1981-08-18 00:00:00',
639 # 'visit_type_concept_id' => 44818517
640 # }
641 # }
642  
643 15640 my $arg = shift;
644 15640 my $self = $arg->{self};
645 15640 my $ohdsi_dict = $arg->{ohdsi_dict};
646 15640 my $person_id = $arg->{person_id};
647 15640 my $visit_occurrence_id = $arg->{visit_occurrence_id};
648 15640 my $visit_occurrence = $self->{visit_occurrence};
649  
650 # Premature return
651 15640 ▶ return undef if $visit_occurrence_id eq '\\N'; # perlcritic Severity: 5
Branch: 1 branch
BranchTrueFalseTotal
if $visit_occurrence_id eq "\\N" 56 15584 15640
652  
653 # *** IMPORTANT ***
654 # EUNOMIA instance has mismatches between the person_id -- visit_occurrence_id
655 # For instance, person_id = 1 has only visit_occurrence_id = 85, but on tables it has:
656 # 82, 84, 42, 54, 41, 25, 76 and 81
657  
658 # warn if we don't have $visit_occurrence_id in VISIT_OCCURRENCE
659 15584 ▶ unless ( exists $visit_occurrence->{$visit_occurrence_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $visit_occurrence->{$visit_occurrence_id}) 15392 192 15584
660 15392 warn
661 "Sorry, but <visit_occurrence_id:$visit_occurrence_id> does not exist for <person_id:$person_id>\n"
662 if DEVEL_MODE;
663  
664 # Premature return
665 15392 return undef; # perlcritic Severity: 5
666 }
667  
668 # Getting pointer to the hash element
669 192 my $hashref = $visit_occurrence->{$visit_occurrence_id};
670  
671 my $concept = map2ohdsi(
672 {
673 ohdsi_dict => $ohdsi_dict,
674 concept_id => $hashref->{visit_concept_id},
675 192 self => $self
676  
677 }
678 );
679  
680 # *** IMPORTANT ***
681 # Ad hoc to avoid using --ohdsi-db while we find a solution to EUNOMIA not being self-contained
682 192 my $ad_hoc_44818517 = {
683 id => "Visit_Type:OMOP4822465",
684 label => "Visit derived from encounter on claim"
685 };
686 my $type =
687 $hashref->{visit_type_concept_id} == 44818517
688 ? $ad_hoc_44818517
689 : map2ohdsi(
690 {
691 ohdsi_dict => $ohdsi_dict,
692 concept_id => $hashref->{visit_type_concept_id},
693 192 ▶ self => $self
Branch: 1 branch
BranchTrueFalseTotal
$hashref->{'visit_type_concept_id'} == 44818517 ? : 192 0 192
694  
695 }
696 );
697 192 my $start_date = map_iso8601_date2timestamp( $hashref->{visit_start_date} );
698 192 my $end_date = map_iso8601_date2timestamp( $hashref->{visit_end_date} );
699 192 ▶ my $info =
Branch: 1 branch
BranchTrueFalseTotal
_source_info_enabled($self) ? : 192 0 192
700 _source_info_enabled($self)
701 ? { VISIT_OCCURRENCE => { OMOP_columns => $hashref } }
702 : undef;
703  
704 my $visit = {
705 id => $visit_occurrence_id,
706 concept => $concept,
707 type => $type,
708 start_date => $start_date,
709 end_date => $end_date,
710 occurrence_id => $hashref->{visit_occurrence_id}
711 192 };
712 192 ▶ $visit->{_info} = $info if defined $info;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info 192 0 192
713 192 return $visit;
714 }
715  
716 171 ▶sub convert_date_to_iso8601 {
Subroutine convert_date_to_iso8601: called CC=6 SCAR=19.4
Pod undocumented
717 171 ▶ my $date = shift // '';
Condition50%1/2
shift() // ""
l!l
1710
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
718  
719 # Trim any accidental whitespace
720 171 $date =~ s/^\s+|\s+$//g;
721  
722 # Return default if input is empty
723 171 ▶ return '1900-01-01' if $date eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $date eq "" 0 171 171
724  
725 # If already in ISO format (YYYY-MM-DD), return as-is
726 171 ▶ if ( $date =~ /^\d{4}-\d{2}-\d{2}$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^\d{4}-\d{2}-\d{2}$/) 86 85 171
727 86 return $date;
728 }
729  
730 # If dot-separated format with four-digit first element (YYYY.MM.DD)
731 85 ▶ if ( $date =~ /^(\d{4})\.(\d{2})\.(\d{2})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^(\d{4})\.(\d{2})\.(\d{2})$/) 0 85 85
732 0 return "$1-$2-$3";
733 }
734  
735 # If dot-separated format with two-digit first element (DD.MM.YYYY)
736 85 ▶ if ( $date =~ /^(\d{2})\.(\d{2})\.(\d{4})$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^(\d{2})\.(\d{2})\.(\d{4})$/) 85 0 85
737 85 return "$3-$2-$1";
738 }
739  
740 # Optionally, handle any other unexpected format gracefully
741 0 warn "Invalid date format: $date";
742 }
743  
744 0 ▶sub is_multidimensional {
Subroutine is_multidimensional: not called CC=2 SCAR=17.9
Pod undocumented
745 0 ▶ return ref shift ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref shift() ? : 0 0 0
746 }
747  
748 2206 ▶sub validate_format {
Subroutine validate_format: called CC=2 SCAR=6.9
Pod undocumented
749 2206 my ( $data, $format ) = @_;
750 return ( $format eq 'pxf' )
751 ? !!( exists $data->{subject} )
752 2206 ▶ : !( exists $data->{subject} );
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'pxf' ? : 342 1864 2206
753 }
754  
755 194 ▶sub get_info {
Subroutine get_info: called CC=15 SCAR=44.0
Pod undocumented
756 194 my $self = shift;
757  
758 # Detecting the number of logical CPUs across different OSes
759 194 my $os = $^O;
760 194 my $threadshost = 1;
761  
762 194 ▶ if ( lc($os) eq 'darwin' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $os eq 'darwin') { } 0 194 194
elsif (lc $os eq 'freebsd') { } 0 194 194
elsif ($os eq 'MSWin32') { } 0 194 194
763 0 my $out = qx{/usr/sbin/sysctl -n hw.logicalcpu 2>/dev/null};
764 0 chomp $out;
765 0 ▶ $threadshost = $1 if defined $out && $out =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out =~ /(\d+)/ 0 0 0
Condition0%0/3
defined $out and $out =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and $out =~ /(\d+)/
defined $out $out =~ /(\d+)/
Truth table0%0/3
A and B
A= defined $outB= $out =~ /(\d+)/
ABresult
0X0
100
111
766 }
767 elsif ( lc($os) eq 'freebsd' ) {
768 0 my $out = qx{sysctl -n hw.ncpu 2>/dev/null};
769 0 chomp $out;
770 0 ▶ $threadshost = $1 if defined $out && $out =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out =~ /(\d+)/ 0 0 0
Condition0%0/3
defined $out and $out =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and $out =~ /(\d+)/
defined $out $out =~ /(\d+)/
Truth table0%0/3
A and B
A= defined $outB= $out =~ /(\d+)/
ABresult
0X0
100
111
771 }
772 elsif ( $os eq 'MSWin32' ) {
773 0 my $out = $ENV{NUMBER_OF_PROCESSORS};
774 0 ▶ $threadshost = $1 if defined $out && $out =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out =~ /(\d+)/ 0 0 0
Condition0%0/3
defined $out and $out =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and $out =~ /(\d+)/
defined $out $out =~ /(\d+)/
Truth table0%0/3
A and B
A= defined $outB= $out =~ /(\d+)/
ABresult
0X0
100
111
775 }
776 else {
777 194 my $out = qx{/usr/bin/nproc 2>/dev/null};
778 194 chomp $out;
779 194 ▶ $threadshost = $1 if defined $out && $out =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out =~ /(\d+)/ 194 0 194
Condition33%1/3
defined $out and $out =~ /(\d+)/
!ll&&!rl&&r
00194
MC/DC0%0/2
defined $out and $out =~ /(\d+)/
defined $out $out =~ /(\d+)/
Truth table33%1/3
A and B
A= defined $outB= $out =~ /(\d+)/
ABresult
0X0
100
111
780 }
781  
782 194 $threadshost = 0 + $threadshost; # coercing it to be a number
783  
784 return {
785 user => $ENV{'LOGNAME'}
786 || $ENV{'USER'}
787 || $ENV{'USERNAME'}
788 || 'dummy-user',
789 username => $self->{username},
790 threadshost => $threadshost,
791 cwd => cwd,
792 id => $self->{id},
793 194 ▶ hostname => hostname,
Condition37%3/8
$ENV{'LOGNAME'} || $ENV{'USER'} || $ENV{'USERNAME'} || 'dummy-user'
l!l
0194
$ENV{"LOGNAME"} or $ENV{"USER"} or $ENV{"USERNAME"}
l!l&&r!l&&!r
00194
$ENV{"LOGNAME"} or $ENV{"USER"}
l!l&&r!l&&!r
00194
MC/DC0%0/3
$ENV{'LOGNAME'} || $ENV{'USER'} || $ENV{'USERNAME'} || 'dummy-user'
$ENV{"LOGNAME"} $ENV{"USER"} $ENV{"USERNAME"}
Truth table25%1/4
A or B or C
A= $ENV{"LOGNAME"}B= $ENV{"USER"}C= $ENV{"USERNAME"}
ABCresult
1XX1
01X1
0011
0000
794 version => $Convert::Pheno::VERSION,
795 beaconSchemaVersion => '2.0.0',
796 };
797 }
798  
799 75 ▶sub get_metaData {
Subroutine get_metaData: called CC=1 SCAR=0.0
Pod undocumented
800 75 my $self = shift;
801  
802 # Setting a few variables
803 75 my $username = $self->{username};
804  
805 # Setting resources
806 75 my $resources = [
807 {
808 id => 'icd10',
809 name =>
810 'International Statistical Classification of Diseases and Related Health Problems 10th Revision',
811 url => 'https://icd.who.int/browse10/2019/en#',
812 version => '2019',
813 namespacePrefix => 'ICD10',
814 iriPrefix => 'https://icd.who.int/browse10/2019/en#/'
815 },
816 {
817 id => 'ncit',
818 name => 'NCI Thesaurus',
819 url => 'http://purl.obolibrary.org/obo/ncit.owl',
820 version => '22.03d',
821 namespacePrefix => 'NCIT',
822 iriPrefix => 'http://purl.obolibrary.org/obo/NCIT_'
823 },
824 {
825 id => 'athena-ohdsi',
826 name => 'Athena-OHDSI',
827 url => 'https://athena.ohdsi.org',
828 version => 'v5.3.1',
829 namespacePrefix => 'OHDSI',
830 iriPrefix => 'http://www.fakeurl.com/OHDSI_'
831 },
832 {
833 id => 'hp',
834 name => 'Human Phenotype Ontology',
835 url => 'http://purl.obolibrary.org/obo/hp.owl',
836 version => '2023-04-05',
837 namespacePrefix => 'HP',
838 iriPrefix => 'http://purl.obolibrary.org/obo/HP_'
839 },
840 {
841 id => 'omim',
842 name => 'Online Mendelian Inheritance in Man',
843 url => 'https://www.omim.org',
844 version => '2023-05-22',
845 namespacePrefix => 'OMIM',
846 iriPrefix => 'http://omim.org/entry/'
847 },
848 {
849 id => 'cdisc-terminology',
850 name => 'CDISC Terminology',
851 url =>
852 'https://www.cdisc.org/standards/terminology/controlled-terminology',
853 version => '2023-01-24',
854 namespacePrefix => 'CDISC',
855 iriPrefix => 'http://www.fakeurl.com/CDISC_'
856 }
857 ];
858 return {
859 75 created => get_current_utc_iso8601_timestamp(),
860 createdBy => $username,
861 submittedBy => $username,
862 phenopacketSchemaVersion => '2.0',
863 resources => $resources,
864 externalReferences => [
865 {
866 id => 'PMID: 26262116',
867 reference =>
868 'https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4815923',
869 description =>
870 'Observational Health Data Sciences and Informatics (OHDSI): Opportunities for Observational Researchers'
871 }
872 ]
873 };
874 }
875  
876 18 ▶sub merge_omop_tables {
Subroutine merge_omop_tables: called CC=6 SCAR=17.9
Pod undocumented
877 18 my $individuals = shift; # Expect an arrayref of individual OMOP structures
878 18 ▶ die "Expected an array reference" unless ref($individuals) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $individuals eq "ARRAY" 0 18 18
879  
880 18 my %merged;
881 18 foreach my $ind (@$individuals) {
882  
883 # Ensure each individual record is a hashref.
884 70 ▶ next unless ref($ind) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ind eq "HASH" 1 69 70
885  
886 # For each table in this individual...
887 69 foreach my $table ( keys %$ind ) {
888  
889 # If the table is stored as an arrayref, merge its rows.
890 233 ▶ if ( ref( $ind->{$table} ) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ind->{$table} eq 'ARRAY') { } 164 69 233
891 164 push @{ $merged{$table} }, @{ $ind->{$table} };
892 }
893 else {
894 # If it's a single hash (one row), add it as a single element.
895 69 push @{ $merged{$table} }, $ind->{$table};
896 }
897 }
898 }
899 18 return \%merged;
900 }
901  
902 26 ▶sub convert_label_to_days {
Subroutine convert_label_to_days: called CC=5 SCAR=16.2
Pod undocumented
903 26 my ( $label, $count ) = @_;
904  
905 # return undef on missing args
906 return undef
907 26 ▶ unless defined $label && defined $count && looks_like_number($count);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label and defined $count and &looks_like_number($count) 1 25 26
Condition66%4/6
defined $label and defined $count and &looks_like_number($count)
!ll&&!rl&&r
1025
defined $label and defined $count
!ll&&!rl&&r
1025
MC/DC33%1/3
defined $label and defined $count and &looks_like_number($count)
defined $label defined $count &looks_like_number($count)
Truth table50%2/4
A and B and C
A= defined $labelB= defined $countC= &looks_like_number($count)
ABCresult
0XX0
10X0
1100
1111
908  
909 25 my $key = lc $label;
910  
911 # normalize plural to singular
912 25 $key =~ s/s$//;
913  
914 25 my %mult = (
915 day => 1,
916 week => 7,
917 month => 30,
918 year => 365,
919 );
920  
921 # lookup multiplier
922 25 my $factor = $mult{$key};
923 25 ▶ return undef unless defined $factor;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $factor 1 24 25
924  
925 24 return $factor * $count;
926 }
927  
928 16825 ▶sub allocate_surrogate_integer {
Subroutine allocate_surrogate_integer: called CC=9 SCAR=23.3
Pod undocumented
929 16825 my ( $self, $scope, $source_key ) = @_;
930 16825 ▶ die "Missing scope for surrogate integer allocation\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scope and length $scope 0 16825 16825
Condition33%1/3
defined $scope and length $scope
!ll&&!rl&&r
0016825
MC/DC0%0/2
defined $scope and length $scope
defined $scope length $scope
Truth table33%1/3
A and B
A= defined $scopeB= length $scope
ABresult
0X0
100
111
931 unless defined $scope && length $scope;
932 16825 ▶ die "Missing source key for surrogate integer allocation\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source_key and length $source_key 0 16825 16825
Condition33%1/3
defined $source_key and length $source_key
!ll&&!rl&&r
0016825
MC/DC0%0/2
defined $source_key and length $source_key
defined $source_key length $source_key
Truth table33%1/3
A and B
A= defined $source_keyB= length $source_key
ABresult
0X0
100
111
933 unless defined $source_key && length $source_key;
934  
935 16825 ▶ my $map = $self->{_surrogate_integer_map}{$scope} ||= {};
Condition100%2/2
$self->{"_surrogate_integer_map"}{$scope} ||= {}
l!l
1678738
MC/DC100%1/1
$self->{"_surrogate_integer_map"}{$scope} ||= {}
$self->{"_surrogate_integer_map"}{$scope}
Truth table100%2/2
A
A= $self->{"_surrogate_integer_map"}{$scope}
Aresult
11
00
936 16825 ▶ return $map->{$source_key} if exists $map->{$source_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $map->{$source_key} 16166 659 16825
937  
938 659 ▶ my $next = ( $self->{_surrogate_integer_counter}{$scope} ||= 0 ) + 1;
Condition100%2/2
$self->{"_surrogate_integer_counter"}{$scope} ||= 0
l!l
62138
MC/DC100%1/1
$self->{"_surrogate_integer_counter"}{$scope} ||= 0
$self->{"_surrogate_integer_counter"}{$scope}
Truth table100%2/2
A
A= $self->{"_surrogate_integer_counter"}{$scope}
Aresult
11
00
939 659 ▶ die "Surrogate integer overflow for <$scope>; OMOP integer max is <"
Branch: 1 branch
BranchTrueFalseTotal
if $next > 2147483647 0 659 659
940 . MAX_OMOP_INTEGER . ">\n"
941 if $next > MAX_OMOP_INTEGER;
942  
943 659 $self->{_surrogate_integer_counter}{$scope} = $next;
944 659 $map->{$source_key} = $next;
945 659 return $next;
946 }
947  
948 192 ▶sub _source_info_enabled {
Subroutine _source_info_enabled: called CC=2 SCAR=8.1
949 192 my ($self) = @_;
950 192 ▶ return !exists $self->{source_info} || $self->{source_info};
Condition33%1/3
!exists $self->{'source_info'} || $self->{'source_info'}
l!l&&r!l&&!r
01920
MC/DC0%0/2
!exists $self->{'source_info'} || $self->{'source_info'}
!exists $self->{'source_info'} $self->{'source_info'}
Truth table33%1/3
not(A) || B
A= !exists $self->{'source_info'}B= $self->{'source_info'}
ABresult
1X1
011
000
951 }
952  
953 143 1;
« lib/Convert/Pheno/OMOP/Vocabulary.pm ↑ Summary lib/Convert/Pheno/FHIR/Util.pm »