lib/Convert/Pheno/OMOP/Vocabulary.pm

filter: Statementstmt 97.8% 229 / 234 Branchbran 78.8% 93 / 118 Conditioncond 55.8% 48 / 86 MC/DCmcdc 23.4% 11 / 47 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 2 totaltotal 79.4% 409 / 515 CC 97 SCAR 30.3
CC
97
Cov
84%
resolve_standard_concept
47.9
_score_similarity_candidates
9.6
_lookup_similar_standard_label
9.1
CRAP 20.7
« lib/Convert/Pheno/Source/CBioPortal.pm ↑ Summary lib/Convert/Pheno/Mapping/Shared.pm »
1 package Convert::Pheno::OMOP::Vocabulary;
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 Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 136 ▶use Convert::Pheno::DB::SQLite ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 136 ▶use Convert::Pheno::Mapping::Shared qw(record_term_audit);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 136 our @EXPORT_OK = qw(resolve_standard_concept);
11  
12 23 ▶sub prepare_terminology_mapping {
Subroutine prepare_terminology_mapping: called CC=5 SCAR=16.4
Pod undocumented
13 23 my ($self) = @_;
14 23 delete $self->{_omop_terminology};
15 23 delete $self->{_ohdsi_standard_concept_cache};
16 23 ▶ return unless defined $self->{mapping_file} && length $self->{mapping_file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"mapping_file"} and length $self->{"mapping_file"} 21 2 23
Condition66%2/3
defined $self->{"mapping_file"} and length $self->{"mapping_file"}
!ll&&!rl&&r
2102
MC/DC50%1/2
defined $self->{"mapping_file"} and length $self->{"mapping_file"}
defined $self->{"mapping_file"} length $self->{"mapping_file"}
Truth table66%2/3
A and B
A= defined $self->{"mapping_file"}B= length $self->{"mapping_file"}
ABresult
0X0
100
111
17  
18 2 require Convert::Pheno::IO::CSVHandler;
19 2 require Convert::Pheno::Mapping::Compiler;
20 2 require File::ShareDir::ProjectDistDir;
21 2 require File::Spec;
22 my $mapping = Convert::Pheno::IO::CSVHandler::read_mapping_file({
23 mapping_file => $self->{mapping_file},
24 schema_file => $self->{schema_file} // File::Spec->catfile(
25 File::ShareDir::ProjectDistDir::dist_dir('Convert-Pheno'),
26 'schema', 'mapping-v2.json'),
27 2 ▶ self_validate_schema => $self->{self_validate_schema} || 0,
Condition40%2/5
$self->{"schema_file"} // "File::Spec"->catfile(File::ShareDir::ProjectDistDir::dist_dir("Convert-Pheno"), "schema", "mapping-v2.json")
l!l&&r!l&&!r
200
$self->{'self_validate_schema'} || 0
l!l
02
MC/DC0%0/2
$self->{"schema_file"} // "File::Spec"->catfile(File::ShareDir::ProjectDistDir::dist_dir("Convert-Pheno"), "schema", "mapping-v2.json")
$self->{"schema_file"} "File::Spec"->catfile(File::ShareDir::ProjectDistDir::dist_dir("Convert-Pheno"), "schema", "mapping-v2.json")
MC/DC0%0/1
$self->{'self_validate_schema'} || 0
$self->{'self_validate_schema'}
Truth table33%1/3
A // B
A= $self->{"schema_file"}B= "File::Spec"->catfile(File::ShareDir::ProjectDistDir::dist_dir("Convert-Pheno"), "schema", "mapping-v2.json")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'self_validate_schema'}
Aresult
11
00
28 });
29 2 my $compiled = Convert::Pheno::Mapping::Compiler::compile_mapping(
30 $mapping, source_profile => 'bff');
31 2 $self->{_omop_terminology} = $compiled->{terminology};
32 }
33  
34 136 my %VOCABULARY_ALIAS = (
35 SNOMEDCT => 'SNOMED',
36 RXNORM => 'RxNorm',
37 RXNORMEXTENSION => 'RxNorm Extension',
38 ICD10CM => 'ICD10CM',
39 ICD9CM => 'ICD9CM',
40 OMOPEXTENSION => 'OMOP Extension',
41 );
42  
43 686 ▶sub resolve_standard_concept {
Subroutine resolve_standard_concept: called CC=45 SCAR=38.7
Pod undocumented
44 686 my ($arg) = @_;
45 my $self = $arg->{self}
46 686 ▶ or die "OHDSI concept resolution requires a converter instance\n";
Branch: 1 branch
BranchTrueFalseTotal
my $self = $arg->{"self"} or die "OHDSI concept resolution requires a converter instance\n" 686 0 686
47 my $term = ref( $arg->{term} ) eq 'HASH'
48 ? $arg->{term}
49 686 ▶ : { label => $arg->{term} };
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'term'} eq 'HASH' ? : 686 0 686
50 my @domains = ref( $arg->{domains} ) eq 'ARRAY'
51 671 ? @{ $arg->{domains} }
52 686 ▶ : ( $arg->{domain} );
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'domains'} eq 'ARRAY' ? : 671 15 686
53 686 ▶ @domains = grep { defined && length } @domains;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
00726
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
54 686 ▶ die "OHDSI concept resolution requires an expected OMOP domain\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @domains 0 686 686
55 unless @domains;
56  
57 my @relationships = ref( $arg->{relationships} ) eq 'ARRAY'
58 5 ▶ ? @{ $arg->{relationships} }
Condition50%1/2
$arg->{"relationship"} // "Maps to"
l!l
0681
MC/DC0%0/1
$arg->{"relationship"} // "Maps to"
$arg->{"relationship"}
Truth table50%1/2
A
A= $arg->{"relationship"}
Aresult
11
00
59 686 ▶ : ( $arg->{relationship} // 'Maps to' );
Branch: 1 branch
BranchTrueFalseTotal
ref $arg->{'relationships'} eq 'ARRAY' ? : 5 681 686
60 686 my $source_id = _trim( $term->{id} );
61 686 my $source_label = _trim( $term->{label} );
62 686 ▶ my $source_value = length($source_label) ? $source_label : $source_id;
Branch: 1 branch
BranchTrueFalseTotal
length $source_label ? : 632 54 686
63  
64 686 ▶ return _empty_result($source_value)
Branch: 1 branch
BranchTrueFalseTotal
unless length $source_id or length $source_label 54 632 686
Condition100%3/3
length $source_id or length $source_label
l!l&&r!l&&!r
629354
MC/DC100%2/2
length $source_id or length $source_label
length $source_id length $source_label
Truth table100%3/3
A or B
A= length $source_idB= length $source_label
ABresult
1X1
011
000
65 unless length($source_id) || length($source_label);
66  
67 632 my ($original_id, $original_label) = ($source_id, $source_label);
68 632 ▶ my $rule = $self->{_omop_terminology}{ $arg->{source_field} // q{} };
Condition100%2/2
$arg->{"source_field"} // ""
l!l
6239
MC/DC100%1/1
$arg->{"source_field"} // ""
$arg->{"source_field"}
Truth table100%2/2
A
A= $arg->{"source_field"}
Aresult
11
00
69 632 my $mapped = 0;
70 632 ▶ if ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule) 10 622 632
71 10 my $query = $rule->{query};
72 10 ▶ my $key = $query->{from} eq 'id' ? $source_id : $source_label;
Branch: 1 branch
BranchTrueFalseTotal
$query->{'from'} eq 'id' ? : 7 3 10
73 10 ▶ if (exists $query->{aliases}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $query->{"aliases"}{$key}) 10 0 10
74 10 my $alias = _trim($query->{aliases}{$key});
75 10 ▶ die "Empty OMOP query alias for <$arg->{source_field}>\n" unless length $alias;
Branch: 1 branch
BranchTrueFalseTotal
unless length $alias 0 10 10
76 # A reviewed identifier must resolve as an identifier. Do not rescue
77 # a wrong-domain or missing ID using the source label instead.
78 10 ▶ ($source_id, $source_label) = ($query->{column} // 'label') eq 'id'
Branch: 1 branch
BranchTrueFalseTotal
($query->{'column'} // 'label') eq 'id' ? : 7 3 10
Condition100%2/2
$query->{"column"} // "label"
l!l
73
MC/DC100%1/1
$query->{"column"} // "label"
$query->{"column"}
Truth table100%2/2
A
A= $query->{"column"}
Aresult
11
00
79 ? ($alias, q{}) : (q{}, $alias);
80 10 $mapped = 1;
81 }
82 }
83  
84 632 my $search = _setting( $self, 'search', 'exact' );
85 my $cache_key = join "\x1e",
86 $arg->{mapping_type} // q{}, join( q{|}, @domains ),
87 join( q{|}, @relationships ), $source_id, $source_label, $search,
88 632 ▶ $original_id, $original_label, $mapped, $arg->{source_field} // q{};
Condition75%3/4
$arg->{"mapping_type"} // ""
l!l
6320
$arg->{"source_field"} // ""
l!l
6239
MC/DC0%0/1
$arg->{"mapping_type"} // ""
$arg->{"mapping_type"}
MC/DC100%1/1
$arg->{"source_field"} // ""
$arg->{"source_field"}
Truth table50%1/2
A
A= $arg->{"mapping_type"}
Aresult
11
00
Truth table100%2/2
A
A= $arg->{"source_field"}
Aresult
11
00
89 632 ▶ my $cache = $self->{_ohdsi_standard_concept_cache} ||= {};
Condition100%2/2
$self->{"_ohdsi_standard_concept_cache"} ||= {}
l!l
60725
MC/DC100%1/1
$self->{"_ohdsi_standard_concept_cache"} ||= {}
$self->{"_ohdsi_standard_concept_cache"}
Truth table100%2/2
A
A= $self->{"_ohdsi_standard_concept_cache"}
Aresult
11
00
90 632 ▶ if ( exists $cache->{$cache_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cache->{$cache_key}) 406 226 632
91 406 my $cached = { %{ $cache->{$cache_key} }, match_source => 'cache' };
92 406 _record_audit( $self, $arg, $cached );
93 406 return _public_result($cached);
94 }
95  
96 226 my $result;
97 my $source_concept;
98 226 ▶ if ( length $source_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $source_id) 221 5 226
99 221 my $source_candidates = _lookup_identifier( $self, $source_id );
100 221 ▶ if ( @{$source_candidates} > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$source_candidates;} > 1) { } 0 221 221
elsif (@{$source_candidates;} == 1) { } 88 133 221
101 0 $result = _failure_result(
102 source_value => $source_value,
103 source_id => $source_id,
104 source_label => $source_label,
105 decision_reason => 'identifier_ambiguous',
106 lookup_query => $source_id,
107 lookup_column => 'id',
108 );
109 }
110 221 elsif ( @{$source_candidates} == 1 ) {
111 88 $source_concept = $source_candidates->[0];
112 88 ▶ if ( _is_eligible_standard( $source_concept, \@domains ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_eligible_standard($source_concept, \@domains)) { } 76 12 88
113 $result = _success_result(
114 target => $source_concept,
115 source_concept_id => $source_concept->{concept_id},
116 76 source_value => $source_value,
117 source_id => $source_id,
118 source_label => $source_label,
119 resolution => 'identifier_standard',
120 decision_reason => 'identifier_standard',
121 lookup_query => $source_id,
122 lookup_column => 'id',
123 );
124 }
125 else {
126 my $targets = _lookup_mapped_targets(
127 $self, $source_concept->{concept_id},
128 12 \@relationships, \@domains
129 );
130 12 ▶ if ( @{$targets} == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$targets;} == 1) { } 7 5 12
elsif (@{$targets;} > 1) { } 0 5 5
131 $result = _success_result(
132 target => $targets->[0],
133 source_concept_id => $source_concept->{concept_id},
134 7 source_value => $source_value,
135 source_id => $source_id,
136 source_label => $source_label,
137 resolution => 'maps_to_standard',
138 decision_reason => 'identifier_maps_to_standard',
139 lookup_query => $source_id,
140 lookup_column => 'id',
141 );
142 }
143 5 elsif ( @{$targets} > 1 ) {
144 $result = _failure_result(
145 source_value => $source_value,
146 source_id => $source_id,
147 source_label => $source_label,
148 source_concept_id => $source_concept->{concept_id},
149 0 decision_reason => 'mapping_ambiguous',
150 lookup_query => $source_id,
151 lookup_column => 'id',
152 );
153 }
154 }
155 }
156 }
157  
158 226 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 143 83 226
159 my $source_concept_id = $source_concept
160 ? $source_concept->{concept_id}
161 143 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
$source_concept ? : 5 138 143
162 143 ▶ if ( length $source_label ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $source_label) 140 3 143
163 140 my $label_candidates =
164 _lookup_standard_label( $self, $source_label, \@domains );
165 140 ▶ if ( @{$label_candidates} == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (@{$label_candidates;} == 1) { } 53 87 140
elsif (@{$label_candidates;} > 1) { } 4 83 87
elsif ($search eq 'mixed' or $search eq 'fuzzy') { } 1 82 83
166 53 $result = _success_result(
167 target => $label_candidates->[0],
168 source_concept_id => $source_concept_id,
169 source_value => $source_value,
170 source_id => $source_id,
171 source_label => $source_label,
172 resolution => 'domain_label_exact',
173 decision_reason => 'domain_label_exact',
174 lookup_query => $source_label,
175 lookup_column => 'label',
176 );
177 }
178 87 elsif ( @{$label_candidates} > 1 ) {
179 4 $result = _failure_result(
180 source_value => $source_value,
181 source_id => $source_id,
182 source_label => $source_label,
183 source_concept_id => $source_concept_id,
184 decision_reason => 'label_ambiguous',
185 lookup_query => $source_label,
186 lookup_column => 'label',
187 );
188 }
189 ▶ elsif ( $search eq 'mixed' || $search eq 'fuzzy' ) {
Condition66%2/3
$search eq "mixed" or $search eq "fuzzy"
l!l&&r!l&&!r
0182
MC/DC50%1/2
$search eq "mixed" or $search eq "fuzzy"
$search eq "mixed" $search eq "fuzzy"
Truth table66%2/3
A or B
A= $search eq "mixed"B= $search eq "fuzzy"
ABresult
1X1
011
000
190 1 my $similar = _lookup_similar_standard_label(
191 $self, $source_label, \@domains, $search
192 );
193 1 ▶ if ( $similar->{target} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($similar->{'target'}) { } 1 0 1
elsif ($similar->{'ambiguous'}) { } 0 0 0
194 $result = _success_result(
195 target => $similar->{target},
196 source_concept_id => $source_concept_id,
197 source_value => $source_value,
198 source_id => $source_id,
199 source_label => $source_label,
200 resolution => 'domain_label_similarity',
201 decision_reason => 'domain_label_similarity',
202 lookup_query => $source_label,
203 lookup_column => 'label',
204 search_evidence => $similar->{search_evidence},
205 1 );
206 }
207 elsif ( $similar->{ambiguous} ) {
208 $result = _failure_result(
209 source_value => $source_value,
210 source_id => $source_id,
211 source_label => $source_label,
212 source_concept_id => $source_concept_id,
213 decision_reason => 'similarity_ambiguous',
214 lookup_query => $source_label,
215 lookup_column => 'label',
216 search_evidence => $similar->{search_evidence},
217 0 );
218 }
219 }
220 }
221 }
222  
223 $result ||= _failure_result(
224 source_value => $source_value,
225 source_id => $source_id,
226 source_label => $source_label,
227 226 ▶ source_concept_id => $source_concept ? $source_concept->{concept_id} : 0,
Branch: 3 branches
BranchTrueFalseTotal
$source_concept ? : 3 82 85
length $source_label ? : 82 3 85
length $source_label ? : 82 3 85
Condition66%2/3
$result ||= _failure_result("source_value", $source_value, "source_id", $source_id, "source_label", $source_label, "source_concept_id", $source_concept ? $source_concept->{"concept_id"} : 0, "decision_reason", "no_standard_concept", "lookup_query", length $source_label ? $source_label : $source_id, "lookup_column", length $source_label ? "label" : "id")
l!l&&r!l&&!r
141850
MC/DC0%0/2
$result ||= _failure_result("source_value", $source_value, "source_id", $source_id, "source_label", $source_label, "source_concept_id", $source_concept ? $source_concept->{"concept_id"} : 0, "decision_reason", "no_standard_concept", "lookup_query", length $source_label ? $source_label : $source_id, "lookup_column", length $source_label ? "label" : "id")
$result _failure_result("source_value", $source_value, "source_id", $source_id, "source_label", $source_label, "source_concept_id", $source_concept ? $source_concept->{"concept_id"} : 0, "decision_reason", "no_standard_concept", "lookup_query", length $source_label ? $source_label : $source_id, "lookup_column", length $source_label ? "label" : "id")
Truth table66%2/3
A ||= B
A= $resultB= _failure_result("source_value", $source_value, "source_id", $source_id, "source_label", $source_label, "source_concept_id", $source_concept ? $source_concept->{"concept_id"} : 0, "decision_reason", "no_standard_concept", "lookup_query", length $source_label ? $source_label : $source_id, "lookup_column", length $source_label ? "label" : "id")
ABresult
1X1
011
000
228 decision_reason => 'no_standard_concept',
229 lookup_query => length($source_label) ? $source_label : $source_id,
230 lookup_column => length($source_label) ? 'label' : 'id',
231 );
232  
233 226 ▶ $result->{match_source} = $result->{matched} ? 'db' : 'fallback_na';
Branch: 1 branch
BranchTrueFalseTotal
$result->{'matched'} ? : 137 89 226
234 226 ▶ if ($mapped) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mapped) 10 216 226
235 # The query belongs to the mapping, not to the source record. Preserve
236 # the actual BFF provenance in OMOP *_source_* columns and in the audit.
237 10 ▶ my $original = length($original_id) ? _lookup_identifier($self, $original_id) : [];
Branch: 1 branch
BranchTrueFalseTotal
length $original_id ? : 9 1 10
238 10 ▶ $result->{source_concept_id} = @{$original} == 1 ? 0 + $original->[0]{concept_id} : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$original;} == 1 ? : 1 9 10
239 10 $result->{source_id} = $original_id;
240 10 $result->{source_label} = $original_label;
241 10 $result->{source_value} = $source_value;
242 10 $result->{decision_reason} = 'mapped_' . $result->{decision_reason};
243 10 $result->{mapping_applied} = 1;
244 }
245 226 $cache->{$cache_key} = { %{$result} };
246 226 _record_audit( $self, $arg, $result );
247 226 return _public_result($result);
248 }
249  
250 230 ▶sub _lookup_identifier {
Subroutine _lookup_identifier: called CC=8 SCAR=21.9
251 230 my ( $self, $curie ) = @_;
252 230 ▶ return [] if $curie =~ /:NA0000\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $curie =~ /:NA0000\z/i 0 230 230
253  
254 230 my ( $prefix, $code ) = split /:/, $curie, 2;
255 230 ▶ return [] unless defined $code && length $prefix && length $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and length $prefix and length $code 0 230 230
Condition33%2/6
defined $code and length $prefix and length $code
!ll&&!rl&&r
00230
defined $code and length $prefix
!ll&&!rl&&r
00230
MC/DC0%0/3
defined $code and length $prefix and length $code
defined $code length $prefix length $code
Truth table25%1/4
A and B and C
A= defined $codeB= length $prefixC= length $code
ABCresult
0XX0
10X0
1100
1111
256  
257 230 ▶ if ( uc($prefix) eq 'OHDSI' && $code =~ /\A\d+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $prefix eq "OHDSI" and $code =~ /\A\d+\z/) 3 227 230
Condition66%2/3
uc $prefix eq "OHDSI" and $code =~ /\A\d+\z/
!ll&&!rl&&r
22703
MC/DC50%1/2
uc $prefix eq "OHDSI" and $code =~ /\A\d+\z/
uc $prefix eq "OHDSI" $code =~ /\A\d+\z/
Truth table66%2/3
A and B
A= uc $prefix eq "OHDSI"B= $code =~ /\A\d+\z/
ABresult
0X0
100
111
258 3 my $rows = _select_rows(
259 _statement(
260 $self,
261 concept_id => 'SELECT * FROM OHDSI_table WHERE concept_id = ?'
262 ),
263 0 + $code
264 );
265 3 return _deduplicate_concepts($rows);
266 }
267  
268 227 my @vocabularies = _vocabulary_candidates($prefix);
269 227 my @rows;
270 227 my $sth = _statement(
271 $self,
272 vocabulary_code =>
273 'SELECT * FROM OHDSI_table '
274 . 'WHERE vocabulary_id = ? COLLATE NOCASE AND id = ? COLLATE NOCASE'
275 );
276 227 for my $vocabulary (@vocabularies) {
277 238 push @rows, @{ _select_rows( $sth, $vocabulary, $code ) };
278 }
279 227 return _deduplicate_concepts(\@rows);
280 }
281  
282 12 ▶sub _lookup_mapped_targets {
Subroutine _lookup_mapped_targets: called CC=2 SCAR=6.9
283 12 my ( $self, $source_concept_id, $relationships, $domains ) = @_;
284 12 my $sth = _statement(
285 $self,
286 maps_to =>
287 'SELECT t.* FROM OHDSI_maps_to AS m '
288 . 'JOIN OHDSI_table AS t ON t.concept_id = m.target_concept_id '
289 . 'WHERE m.source_concept_id = ? AND m.relationship_id = ? '
290 . q{AND m.invalid_reason = '' AND t.standard_concept = 'S' }
291 . q{AND t.invalid_reason = ''}
292 );
293  
294 12 my @rows;
295 12 for my $relationship ( @{$relationships} ) {
296 push @rows,
297 8 grep { _domain_matches( $_->{domain_id}, $domains ) }
298 12 @{ _select_rows( $sth, $source_concept_id, $relationship ) };
299 }
300 12 return _deduplicate_concepts(\@rows);
301 }
302  
303 140 ▶sub _lookup_standard_label {
Subroutine _lookup_standard_label: called CC=2 SCAR=6.9
304 140 my ( $self, $label, $domains ) = @_;
305 140 my $sth = _statement(
306 $self,
307 standard_label =>
308 'SELECT * FROM OHDSI_table '
309 . q{WHERE domain_id = ? COLLATE NOCASE AND standard_concept = 'S' }
310 . q{AND label = ? COLLATE NOCASE AND invalid_reason = ''}
311 );
312  
313 140 my @rows;
314 140 for my $domain ( @{$domains} ) {
315 159 push @rows, @{ _select_rows( $sth, $domain, $label ) };
316 }
317 140 return _deduplicate_concepts(\@rows);
318 }
319  
320 1 ▶sub _lookup_similar_standard_label {
Subroutine _lookup_similar_standard_label: called CC=8 SCAR=22.1
321 1 my ( $self, $label, $domains, $search ) = @_;
322 1 my $domain_placeholders = join q{,}, ('?') x @{$domains};
323 1 my $base_sql =
324 'SELECT t.label, t.id, t.concept_id, t.vocabulary_id '
325 . 'FROM OHDSI_fts JOIN OHDSI_table AS t '
326 . 'ON t.concept_id = CAST(OHDSI_fts.concept_id AS INTEGER) '
327 . 'WHERE OHDSI_fts.label MATCH ? '
328 . q{AND t.standard_concept = 'S' AND t.invalid_reason = '' }
329 . "AND t.domain_id COLLATE NOCASE IN ($domain_placeholders)";
330 1 my $normalized = Convert::Pheno::DB::SQLite::prune_problematic_chars(
331 $label, 'full_text_search'
332 );
333 1 my $strict_query =
334 Convert::Pheno::DB::SQLite::build_strict_fts_query($label);
335 1 ▶ return { search_evidence => _search_evidence( undef, 'strict_fts' ) }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $strict_query 0 1 1
336 unless defined $strict_query;
337  
338 my $strict = _score_similarity_candidates(
339 $self,
340 1 statement_name => 'standard_fts_' . @{$domains},
341 sql => $base_sql,
342 query => $strict_query,
343 scoring_query => $normalized,
344 domains => $domains,
345 search => $search,
346 strategy => 'strict_fts',
347 );
348 1 ▶ return $strict if $strict->{target} || $strict->{ambiguous};
Branch: 1 branch
BranchTrueFalseTotal
if $strict->{"target"} or $strict->{"ambiguous"} 0 1 1
Condition33%1/3
$strict->{"target"} or $strict->{"ambiguous"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$strict->{"target"} or $strict->{"ambiguous"}
$strict->{"target"} $strict->{"ambiguous"}
Truth table33%1/3
A or B
A= $strict->{"target"}B= $strict->{"ambiguous"}
ABresult
1X1
011
000
349 1 ▶ return $strict unless $search eq 'fuzzy';
Branch: 1 branch
BranchTrueFalseTotal
unless $search eq "fuzzy" 0 1 1
350 1 ▶ return $strict if ( $strict->{search_evidence}{candidates_evaluated} // 0 ) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($strict->{"search_evidence"}{"candidates_evaluated"} // 0) > 0 0 1 1
Condition50%1/2
$strict->{"search_evidence"}{"candidates_evaluated"} // 0
l!l
10
MC/DC0%0/1
$strict->{"search_evidence"}{"candidates_evaluated"} // 0
$strict->{"search_evidence"}{"candidates_evaluated"}
Truth table50%1/2
A
A= $strict->{"search_evidence"}{"candidates_evaluated"}
Aresult
11
00
351  
352 1 my $relaxed_query =
353 Convert::Pheno::DB::SQLite::build_relaxed_fts_query($label);
354 1 ▶ return $strict unless defined $relaxed_query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $relaxed_query 0 1 1
355  
356 return _score_similarity_candidates(
357 $self,
358 1 statement_name => 'standard_relaxed_fts_' . @{$domains},
359 sql => $base_sql . ' ORDER BY bm25(OHDSI_fts) LIMIT 200',
360 query => $relaxed_query,
361 scoring_query => $normalized,
362 domains => $domains,
363 search => $search,
364 strategy => 'relaxed_fts',
365 allow_spelling_variant => 1,
366 );
367 }
368  
369 2 ▶sub _score_similarity_candidates {
Subroutine _score_similarity_candidates: called CC=8 SCAR=22.6
370 2 my ( $self, %arg ) = @_;
371 2 my $sth = _statement( $self, $arg{statement_name}, $arg{sql} );
372 2 $sth->execute( $arg{query}, @{ $arg{domains} } );
373  
374 my %score_arg = (
375 sth => $sth,
376 query => $arg{scoring_query},
377 2 ontology => 'ohdsi',
378 id_column => 1,
379 label_column => 0,
380 concept_id_column => 2,
381 text_similarity_method => _setting( $self, 'text_similarity_method', 'cosine' ),
382 min_text_similarity_score => _setting( $self, 'min_text_similarity_score', 0.8 ),
383 levenshtein_weight => _setting( $self, 'levenshtein_weight', 0.1 ),
384 self => $self,
385 );
386 my ( $id, undef, $concept_id, $stats ) =
387 $arg{search} eq 'mixed'
388 ? Convert::Pheno::DB::SQLite::similarity_match(\%score_arg)
389 : Convert::Pheno::DB::SQLite::composite_similarity_match(
390 { %score_arg, allow_spelling_variant => $arg{allow_spelling_variant} }
391 2 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$arg{'search'} eq 'mixed' ? : 0 2 2
392 2 $sth->finish;
393  
394 2 my $evidence = _search_evidence( $stats, $arg{strategy} );
395 2 my $min_score = $score_arg{min_text_similarity_score};
396 my $ambiguous = defined $id
397 && defined $stats->{runner_up_score}
398 && $stats->{runner_up_score} >= $min_score
399 2 ▶ && ( $stats->{score_margin} // 0 ) <= 0;
Condition36%4/11
defined $id && defined $stats->{'runner_up_score'} && $stats->{'runner_up_score'} >= $min_score && ($stats->{'score_margin'} // 0) <= 0
!ll&&!rl&&r
200
defined $id and defined $stats->{"runner_up_score"} and $stats->{"runner_up_score"} >= $min_score
!ll&&!rl&&r
200
defined $id and defined $stats->{"runner_up_score"}
!ll&&!rl&&r
110
$stats->{"score_margin"} // 0
l!l
00
MC/DC0%0/4
defined $id && defined $stats->{'runner_up_score'} && $stats->{'runner_up_score'} >= $min_score && ($stats->{'score_margin'} // 0) <= 0
defined $id defined $stats->{"runner_up_score"} $stats->{"runner_up_score"} >= $min_score $stats->{"score_margin"}
Truth table40%2/5
A and B and C && D
A= defined $idB= defined $stats->{"runner_up_score"}C= $stats->{"runner_up_score"} >= $min_scoreD= $stats->{"score_margin"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
400 2 ▶ return { ambiguous => 1, search_evidence => $evidence } if $ambiguous;
Branch: 1 branch
BranchTrueFalseTotal
if $ambiguous 0 2 2
401 2 ▶ return { search_evidence => $evidence } unless defined $concept_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $concept_id 1 1 2
402  
403 1 my $target = _lookup_concept_id( $self, $concept_id );
404 return {
405 1 target => $target,
406 search_evidence => $evidence,
407 };
408 }
409  
410 1 ▶sub _lookup_concept_id {
Subroutine _lookup_concept_id: called CC=1 SCAR=0.0
411 1 my ( $self, $concept_id ) = @_;
412 1 my $rows = _select_rows(
413 _statement(
414 $self,
415 concept_id => 'SELECT * FROM OHDSI_table WHERE concept_id = ?'
416 ),
417 $concept_id
418 );
419 1 return $rows->[0];
420 }
421  
422 385 ▶sub _statement {
Subroutine _statement: called CC=3 SCAR=11.4
423 385 my ( $self, $name, $sql ) = @_;
424 my $dbh = $self->{dbh}{ohdsi}
425 385 ▶ or die "Athena-OHDSI lookup requires an open ohdsi.db connection\n";
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->{"dbh"}{"ohdsi"} or die "Athena-OHDSI lookup requires an open ohdsi.db connection\n" 385 0 385
426 385 ▶ return $self->{sth}{ohdsi}{standard_concept}{$name} ||=
Condition66%2/3
$self->{"sth"}{"ohdsi"}{"standard_concept"}{$name} ||= $dbh->prepare($sql)
l!l&&r!l&&!r
328570
MC/DC0%0/2
$self->{"sth"}{"ohdsi"}{"standard_concept"}{$name} ||= $dbh->prepare($sql)
$self->{"sth"}{"ohdsi"}{"standard_concept"}{$name} $dbh->prepare($sql)
Truth table66%2/3
A ||= B
A= $self->{"sth"}{"ohdsi"}{"standard_concept"}{$name}B= $dbh->prepare($sql)
ABresult
1X1
011
000
427 $dbh->prepare($sql);
428 }
429  
430 413 ▶sub _select_rows {
Subroutine _select_rows: called CC=1 SCAR=0.0
431 413 my ( $sth, @bind ) = @_;
432 413 $sth->execute(@bind);
433 413 my $rows = $sth->fetchall_arrayref( {} );
434 413 $sth->finish;
435 413 return $rows;
436 }
437  
438 227 ▶sub _vocabulary_candidates {
Subroutine _vocabulary_candidates: called CC=5 SCAR=17.1
439 227 my ($prefix) = @_;
440 227 my $normalized = uc($prefix);
441 227 $normalized =~ s/[^A-Z0-9]+//g;
442  
443 227 my @candidates;
444 push @candidates, $VOCABULARY_ALIAS{$normalized}
445 227 ▶ if exists $VOCABULARY_ALIAS{$normalized};
Branch: 1 branch
BranchTrueFalseTotal
if exists $VOCABULARY_ALIAS{$normalized} 23 204 227
446 227 push @candidates, $prefix;
447 227 ▶ if ( $prefix =~ /_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /_/) 0 227 227
448 0 ( my $with_spaces = $prefix ) =~ tr/_/ /;
449 0 push @candidates, $with_spaces;
450 }
451  
452 227 my %seen;
453 227 ▶ return grep { defined && length && !$seen{lc $_}++ } @candidates;
Condition50%3/6
defined $_ && length $_ && !$seen{lc $_}++
!ll&&!rl&&r
012238
defined $_ and length $_
!ll&&!rl&&r
00250
MC/DC33%1/3
defined $_ && length $_ && !$seen{lc $_}++
defined $_ length $_ !$seen{lc $_}++
Truth table50%2/4
A and B && not(C)
A= defined $_B= length $_C= !$seen{lc $_}++
ABCresult
0XX0
10X0
1100
1111
454 }
455  
456 88 ▶sub _is_eligible_standard {
Subroutine _is_eligible_standard: called CC=5 SCAR=16.8
457 88 my ( $concept, $domains ) = @_;
458 88 ▶ return 0 unless ( $concept->{standard_concept} // q{} ) eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
unless ($concept->{"standard_concept"} // "") eq "S" 9 79 88
Condition50%1/2
$concept->{"standard_concept"} // ""
l!l
880
MC/DC0%0/1
$concept->{"standard_concept"} // ""
$concept->{"standard_concept"}
Truth table50%1/2
A
A= $concept->{"standard_concept"}
Aresult
11
00
459 79 ▶ return 0 if length( $concept->{invalid_reason} // q{} );
Branch: 1 branch
BranchTrueFalseTotal
if length($concept->{"invalid_reason"} // "") 0 79 79
Condition50%1/2
$concept->{"invalid_reason"} // ""
l!l
790
MC/DC0%0/1
$concept->{"invalid_reason"} // ""
$concept->{"invalid_reason"}
Truth table50%1/2
A
A= $concept->{"invalid_reason"}
Aresult
11
00
460 79 return _domain_matches( $concept->{domain_id}, $domains );
461 }
462  
463 87 ▶sub _domain_matches {
Subroutine _domain_matches: called CC=2 SCAR=7.0
464 87 my ( $domain, $domains ) = @_;
465 87 ▶ return 0 unless defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 0 87 87
466 87 return scalar grep { lc($domain) eq lc($_) } @{$domains};
467 }
468  
469 382 ▶sub _deduplicate_concepts {
Subroutine _deduplicate_concepts: called CC=1 SCAR=0.0
470 382 my ($rows) = @_;
471 382 my %seen;
472 382 return [ grep { !$seen{ $_->{concept_id} }++ } @{$rows} ];
473 }
474  
475 137 ▶sub _success_result {
Subroutine _success_result: called CC=2 SCAR=7.2
476 137 my (%arg) = @_;
477 return {
478 matched => 1,
479 concept_id => 0 + $arg{target}{concept_id},
480 source_concept_id => 0 + ( $arg{source_concept_id} // 0 ),
481 source_value => $arg{source_value},
482 source_id => $arg{source_id},
483 source_label => $arg{source_label},
484 target_id => _concept_curie( $arg{target} ),
485 target_label => $arg{target}{label},
486 target_domain => $arg{target}{domain_id},
487 resolution => $arg{resolution},
488 decision_reason => $arg{decision_reason},
489 lookup_query => $arg{lookup_query},
490 lookup_column => $arg{lookup_column},
491 search_evidence => $arg{search_evidence},
492 137 ▶ };
Condition50%1/2
$arg{"source_concept_id"} // 0
l!l
1370
MC/DC0%0/1
$arg{"source_concept_id"} // 0
$arg{"source_concept_id"}
Truth table50%1/2
A
A= $arg{"source_concept_id"}
Aresult
11
00
493 }
494  
495 89 ▶sub _failure_result {
Subroutine _failure_result: called CC=2 SCAR=7.2
496 89 my (%arg) = @_;
497 return {
498 matched => 0,
499 concept_id => 0,
500 source_concept_id => 0 + ( $arg{source_concept_id} // 0 ),
501 source_value => $arg{source_value},
502 source_id => $arg{source_id},
503 source_label => $arg{source_label},
504 target_id => 'OHDSI:NA0000',
505 target_label => 'No matching standard concept',
506 resolution => 'fallback_concept_0',
507 decision_reason => $arg{decision_reason},
508 lookup_query => $arg{lookup_query},
509 lookup_column => $arg{lookup_column},
510 search_evidence => $arg{search_evidence},
511 89 ▶ };
Condition50%1/2
$arg{"source_concept_id"} // 0
l!l
890
MC/DC0%0/1
$arg{"source_concept_id"} // 0
$arg{"source_concept_id"}
Truth table50%1/2
A
A= $arg{"source_concept_id"}
Aresult
11
00
512 }
513  
514 54 ▶sub _empty_result {
Subroutine _empty_result: called CC=1 SCAR=0.0
515 54 my ($source_value) = @_;
516 return {
517 54 concept_id => 0,
518 source_concept_id => 0,
519 source_value => $source_value,
520 matched => 0,
521 resolution => 'not_searched',
522 };
523 }
524  
525 632 ▶sub _public_result {
Subroutine _public_result: called CC=1 SCAR=0.0
526 632 my ($result) = @_;
527 632 my %public = %{$result};
528 632 delete @public{qw(lookup_query lookup_column search_evidence match_source)};
529 632 return \%public;
530 }
531  
532 632 ▶sub _record_audit {
Subroutine _record_audit: called CC=6 SCAR=17.9
533 632 my ( $self, $arg, $result ) = @_;
534 record_term_audit(
535 {
536 self => $self,
537 source_field => $result->{mapping_applied}
538 ? $arg->{source_field} : $arg->{mapping_type},
539 source_value => $result->{source_id} || $result->{source_value},
540 source_label => $result->{source_label},
541 lookup_query => $result->{lookup_query},
542 lookup_column => $result->{lookup_column},
543 ontology => 'ohdsi',
544 term => {
545 id => $result->{target_id},
546 label => $result->{target_label},
547 search_resolution => $result->{resolution},
548 search_evidence => $result->{search_evidence},
549 },
550 effective_search_mode => $result->{lookup_column} eq 'label'
551 ? _setting( $self, 'search', 'exact' )
552 : 'exact',
553 match_status => $result->{matched} ? 'matched' : 'not_found',
554 decision_reason => $result->{decision_reason},
555 match_source => $result->{match_source},
556 lookup_resolution => $result->{resolution},
557 632 ▶ fallback_action => $result->{matched} ? 'none' : 'concept_0',
Branch: 4 branches
BranchTrueFalseTotal
$result->{'mapping_applied'} ? : 10 622 632
$result->{'lookup_column'} eq 'label' ? : 534 98 632
$result->{'matched'} ? : 222 410 632
$result->{'matched'} ? : 222 410 632
Condition66%2/3
$result->{'source_id'} || $result->{'source_value'}
l!l&&r!l&&!r
62930
MC/DC0%0/2
$result->{'source_id'} || $result->{'source_value'}
$result->{'source_id'} $result->{'source_value'}
Truth table66%2/3
A || B
A= $result->{'source_id'}B= $result->{'source_value'}
ABresult
1X1
011
000
558 }
559 );
560 }
561  
562 2 ▶sub _search_evidence {
Subroutine _search_evidence: called CC=4 SCAR=14.6
563 2 my ( $stats, $strategy ) = @_;
564 2 ▶ $stats ||= {};
Condition50%1/2
$stats ||= {}
l!l
20
MC/DC0%0/1
$stats ||= {}
$stats
Truth table50%1/2
A
A= $stats
Aresult
11
00
565 return {
566 candidate_strategy => $strategy,
567 candidates_evaluated => $stats->{candidate_rows} // 0,
568 eligible_candidates => $stats->{shortlisted_candidates} // 0,
569 10 map { $_ => $stats->{$_} }
570 2 ▶ grep { exists $stats->{$_} }
Condition50%2/4
$stats->{"candidate_rows"} // 0
l!l
20
$stats->{"shortlisted_candidates"} // 0
l!l
20
MC/DC0%0/1
$stats->{"candidate_rows"} // 0
$stats->{"candidate_rows"}
MC/DC0%0/1
$stats->{"shortlisted_candidates"} // 0
$stats->{"shortlisted_candidates"}
Truth table50%1/2
A
A= $stats->{"candidate_rows"}
Aresult
11
00
Truth table50%1/2
A
A= $stats->{"shortlisted_candidates"}
Aresult
11
00
571 qw(best_candidate_label best_candidate_id best_candidate_score token_similarity base_token_similarity normalized_levenshtein spelling_variant spelling_query_token spelling_candidate_token spelling_token_similarity runner_up_label runner_up_id runner_up_score score_margin),
572 };
573 }
574  
575 137 ▶sub _concept_curie {
Subroutine _concept_curie: called CC=1 SCAR=0.0
576 137 my ($concept) = @_;
577 137 my $prefix = $concept->{vocabulary_id};
578 137 $prefix =~ s/\s+/_/g;
579 137 return $prefix . ':' . $concept->{id};
580 }
581  
582 1172 ▶sub _setting {
Subroutine _setting: called CC=3 SCAR=11.7
583 1172 my ( $self, $name, $default ) = @_;
584 1172 ▶ my $value = $self->can($name) ? $self->$name : $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
$self->can($name) ? : 1172 0 1172
585 1172 ▶ return defined $value ? $value : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1172 0 1172
586 }
587  
588 1382 ▶sub _trim {
Subroutine _trim: called CC=3 SCAR=11.0
589 1382 my ($value) = @_;
590 1382 ▶ return q{} unless defined $value && !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 57 1325 1382
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
5701325
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
591 1325 $value =~ s/\A\s+//;
592 1325 $value =~ s/\s+\z//;
593 1325 return $value;
594 }
595  
596 136 1;
« lib/Convert/Pheno/Source/CBioPortal.pm ↑ Summary lib/Convert/Pheno/Mapping/Shared.pm »