lib/Convert/Pheno/FHIR/Util.pm

filter: Statementstmt 95.1% 99 / 104 Branchbran 61.7% 42 / 68 Conditioncond 36.4% 27 / 74 MC/DCmcdc 7.3% 3 / 41 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 7 totaltotal 59.8% 183 / 306 CC 64 SCAR 27.8
CC
64
Cov
68%
codeable_concept_to_term
37.1
reference_aliases
24.6
source_term
12.0
CRAP 16.1
« lib/Convert/Pheno/Mapping/Shared.pm ↑ Summary api/perl/main.pl »
1 package Convert::Pheno::FHIR::Util;
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  
8 136 our @EXPORT_OK = qw(
9 canonical_reference
10 codeable_concept_to_term
11 coding_to_term
12 quantity_unit_to_term
13 reference_aliases
14 resolve_reference
15 source_term
16 );
17  
18 136 my %SYSTEM_PREFIX = (
19 'http://human-phenotype-ontology.org' => 'HP',
20 'http://loinc.org' => 'LOINC',
21 'http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl' => 'NCIT',
22 'http://purl.bioontology.org/ontology/ICD10' => 'ICD10',
23 'http://purl.bioontology.org/ontology/NCIT' => 'NCIT',
24 'http://purl.obolibrary.org/obo/hp.owl' => 'HP',
25 'http://snomed.info/sct' => 'SNOMEDCT',
26 'http://unitsofmeasure.org' => 'UCUM',
27 'http://www.nlm.nih.gov/research/umls/rxnorm' => 'RxNorm',
28 'http://hl7.org/fhir/sid/icd-10' => 'ICD10',
29 'http://hl7.org/fhir/sid/icd-10-cm' => 'ICD10CM',
30 'http://hl7.org/fhir/sid/icd-10-pcs' => 'ICD10PCS',
31 'urn:oid:2.16.840.1.113883.6.238' => 'CDCREC',
32 );
33  
34 611 ▶sub canonical_reference {
Subroutine canonical_reference: called CC=6 SCAR=18.8
Pod undocumented
35 611 my ($reference) = @_;
36 611 ▶ return unless defined $reference && !ref($reference);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reference and not ref $reference 0 611 611
Condition33%1/3
defined $reference and not ref $reference
!ll&&!rl&&r
00611
MC/DC0%0/2
defined $reference and not ref $reference
defined $reference not ref $reference
Truth table33%1/3
A and not(B)
A= defined $referenceB= not ref $reference
ABresult
0X0
100
111
37  
38 611 $reference =~ s/^\s+|\s+$//g;
39 611 ▶ return unless length $reference;
Branch: 1 branch
BranchTrueFalseTotal
unless length $reference 0 611 611
40 611 ▶ return $reference if $reference =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $reference =~ /^#/ 0 611 611
41  
42 611 $reference =~ s{/+_history/[^/?#]+\z}{};
43 611 ▶ if ( $reference =~ m{(?:\A|/)([A-Z][A-Za-z0-9]+)/([^/?#]+)\z} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reference =~ m[(?:\A|/)([A-Z][A-Za-z0-9]+)/([^/?#]+)\z]) 362 249 611
44 362 return "$1/$2";
45 }
46  
47 249 return $reference;
48 }
49  
50 292 ▶sub reference_aliases {
Subroutine reference_aliases: called CC=15 SCAR=32.0
Pod undocumented
51 292 my ( $resource, $full_url ) = @_;
52 292 ▶ return [] unless ref($resource) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $resource eq "HASH" 0 292 292
53  
54 292 my @aliases;
55 292 ▶ push @aliases, $full_url
Branch: 1 branch
BranchTrueFalseTotal
if defined $full_url and not ref $full_url and length $full_url 290 2 292
Condition66%4/6
defined $full_url and not ref $full_url and length $full_url
!ll&&!rl&&r
20290
defined $full_url and not ref $full_url
!ll&&!rl&&r
20290
MC/DC33%1/3
defined $full_url and not ref $full_url and length $full_url
defined $full_url not ref $full_url length $full_url
Truth table50%2/4
A and not(B) and C
A= defined $full_urlB= not ref $full_urlC= length $full_url
ABCresult
0XX0
10X0
1100
1111
56 if defined $full_url && !ref($full_url) && length $full_url;
57  
58 292 ▶ if ( defined $resource->{resourceType}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resource->{"resourceType"} and not ref $resource->{"resourceType"} and defined $resource->{"id"} and not ref $resource->{"id"} and length $resource->{"id"}) 292 0 292
Condition33%4/12
defined $resource->{"resourceType"} and not ref $resource->{"resourceType"} and defined $resource->{"id"} and not ref $resource->{"id"} and length $resource->{"id"}
!ll&&!rl&&r
00292
defined $resource->{"resourceType"} and not ref $resource->{"resourceType"} and defined $resource->{"id"} and not ref $resource->{"id"}
!ll&&!rl&&r
00292
defined $resource->{"resourceType"} and not ref $resource->{"resourceType"} and defined $resource->{"id"}
!ll&&!rl&&r
00292
defined $resource->{"resourceType"} and not ref $resource->{"resourceType"}
!ll&&!rl&&r
00292
MC/DC0%0/5
defined $resource->{"resourceType"} and not ref $resource->{"resourceType"} and defined $resource->{"id"} and not ref $resource->{"id"} and length $resource->{"id"}
defined $resource->{"resourceType"} not ref $resource->{"resourceType"} defined $resource->{"id"} not ref $resource->{"id"} length $resource->{"id"}
Truth table16%1/6
A and not(B) and C and not(D) and E
A= defined $resource->{"resourceType"}B= not ref $resource->{"resourceType"}C= defined $resource->{"id"}D= not ref $resource->{"id"}E= length $resource->{"id"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
59 && !ref( $resource->{resourceType} )
60 && defined $resource->{id}
61 && !ref( $resource->{id} )
62 && length $resource->{id} )
63 {
64 292 push @aliases, "$resource->{resourceType}/$resource->{id}";
65 }
66  
67 292 my %seen;
68 my @normalized;
69 292 for my $alias (@aliases) {
70 582 for my $candidate ( $alias, canonical_reference($alias) ) {
71 1164 ▶ next unless defined $candidate && length $candidate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $candidate and length $candidate 0 1164 1164
Condition33%1/3
defined $candidate and length $candidate
!ll&&!rl&&r
001164
MC/DC0%0/2
defined $candidate and length $candidate
defined $candidate length $candidate
Truth table33%1/3
A and B
A= defined $candidateB= length $candidate
ABresult
0X0
100
111
72 1164 ▶ push @normalized, $candidate unless $seen{$candidate}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$candidate}++ 582 582 1164
73 }
74 }
75  
76 292 return \@normalized;
77 }
78  
79 22 ▶sub resolve_reference {
Subroutine resolve_reference: called CC=6 SCAR=19.1
Pod undocumented
80 22 my ( $index, $reference ) = @_;
81 22 ▶ return unless ref($index) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $index eq "HASH" 0 22 22
82 22 ▶ return unless defined $reference && !ref($reference);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reference and not ref $reference 0 22 22
Condition33%1/3
defined $reference and not ref $reference
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $reference and not ref $reference
defined $reference not ref $reference
Truth table33%1/3
A and not(B)
A= defined $referenceB= not ref $reference
ABresult
0X0
100
111
83  
84 22 ▶ return $index->{$reference} if exists $index->{$reference};
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{$reference} 17 5 22
85 5 my $canonical = canonical_reference($reference);
86 5 ▶ return unless defined $canonical;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $canonical 0 5 5
87 5 return $index->{$canonical};
88 }
89  
90 473 ▶sub codeable_concept_to_term {
Subroutine codeable_concept_to_term: called CC=12 SCAR=36.1
Pod undocumented
91 473 my ( $concept, $fallback_namespace ) = @_;
92 473 ▶ return unless ref($concept) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $concept eq "HASH" 123 350 473
93  
94 350 ▶ for my $coding ( @{ $concept->{coding} || [] } ) {
Condition50%1/2
$concept->{'coding'} || []
l!l
3500
MC/DC0%0/1
$concept->{'coding'} || []
$concept->{'coding'}
Truth table50%1/2
A
A= $concept->{'coding'}
Aresult
11
00
95 next unless ref($coding) eq 'HASH'
96 && defined $coding->{code}
97 && !ref( $coding->{code} )
98 350 ▶ && length $coding->{code};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coding eq "HASH" and defined $coding->{"code"} and not ref $coding->{"code"} and length $coding->{"code"} 0 350 350
Condition33%3/9
ref $coding eq "HASH" and defined $coding->{"code"} and not ref $coding->{"code"} and length $coding->{"code"}
!ll&&!rl&&r
00350
ref $coding eq "HASH" and defined $coding->{"code"} and not ref $coding->{"code"}
!ll&&!rl&&r
00350
ref $coding eq "HASH" and defined $coding->{"code"}
!ll&&!rl&&r
00350
MC/DC0%0/4
ref $coding eq "HASH" and defined $coding->{"code"} and not ref $coding->{"code"} and length $coding->{"code"}
ref $coding eq "HASH" defined $coding->{"code"} not ref $coding->{"code"} length $coding->{"code"}
Truth table20%1/5
A and B and not(C) and D
A= ref $coding eq "HASH"B= defined $coding->{"code"}C= not ref $coding->{"code"}D= length $coding->{"code"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
99 350 my $term = coding_to_term( $coding, $concept->{text} );
100 350 ▶ return $term if defined $term;
Branch: 1 branch
BranchTrueFalseTotal
if defined $term 350 0 350
101 }
102  
103 0 my $text = _trim( $concept->{text} );
104 0 ▶ return unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 0 0
Condition0%0/3
defined $text and length $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
105 0 ▶ return source_term( $fallback_namespace || 'CodeableConcept', $text, $text );
Condition0%0/2
$fallback_namespace || 'CodeableConcept'
l!l
00
MC/DC0%0/1
$fallback_namespace || 'CodeableConcept'
$fallback_namespace
Truth table0%0/2
A
A= $fallback_namespace
Aresult
11
00
106 }
107  
108 406 ▶sub coding_to_term {
Subroutine coding_to_term: called CC=9 SCAR=22.4
Pod undocumented
109 406 my ( $coding, $fallback_label ) = @_;
110 406 ▶ return unless ref($coding) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coding eq "HASH" 0 406 406
111  
112 406 my $code = _trim( $coding->{code} );
113 406 my $label = _trim( $coding->{display} );
114 406 ▶ $label = _trim($fallback_label) unless defined $label && length $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label and length $label 31 375 406
Condition66%2/3
defined $label and length $label
!ll&&!rl&&r
310375
MC/DC50%1/2
defined $label and length $label
defined $label length $label
Truth table66%2/3
A and B
A= defined $labelB= length $label
ABresult
0X0
100
111
115  
116 406 ▶ return source_term( 'Coding', $label, $label )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and length $code 0 406 406
Condition33%1/3
defined $code and length $code
!ll&&!rl&&r
00406
MC/DC0%0/2
defined $code and length $code
defined $code length $code
Truth table33%1/3
A and B
A= defined $codeB= length $code
ABresult
0X0
100
111
117 unless defined $code && length $code;
118  
119 406 $code =~ s/^HP_([0-9]+)\z/HP:$1/;
120 406 my $id;
121 406 ▶ if ( $code =~ /^\w[^:]+:.+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /^\w[^:]+:.+\z/) { } 19 387 406
122 19 $id = $code;
123 }
124 else {
125 387 my $prefix = _prefix_for_system( $coding->{system} );
126 387 $id = "$prefix:$code";
127 }
128  
129 406 $id =~ s/\s+/_/g;
130 406 my $term = { id => $id };
131 406 ▶ $term->{label} = $label
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and length $label 375 31 406
Condition66%2/3
defined $label and length $label
!ll&&!rl&&r
310375
MC/DC50%1/2
defined $label and length $label
defined $label length $label
Truth table66%2/3
A and B
A= defined $labelB= length $label
ABresult
0X0
100
111
132 if defined $label && length $label;
133 406 return $term;
134 }
135  
136 56 ▶sub quantity_unit_to_term {
Subroutine quantity_unit_to_term: called CC=6 SCAR=24.1
Pod undocumented
137 56 my ($quantity) = @_;
138 56 ▶ return unless ref($quantity) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $quantity eq "HASH" 0 56 56
139  
140 56 my $code = _trim( $quantity->{code} );
141 56 my $unit = _trim( $quantity->{unit} );
142 56 my $system = _trim( $quantity->{system} );
143  
144 56 ▶ if ( defined $code && length $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and length $code) 56 0 56
Condition33%1/3
defined $code and length $code
!ll&&!rl&&r
0056
MC/DC0%0/2
defined $code and length $code
defined $code length $code
Truth table33%1/3
A and B
A= defined $codeB= length $code
ABresult
0X0
100
111
145 56 return coding_to_term(
146 {
147 code => $code,
148 display => $unit,
149 system => $system,
150 },
151 $unit,
152 );
153 }
154  
155 0 ▶ return unless defined $unit && length $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit and length $unit 0 0 0
Condition0%0/3
defined $unit and length $unit
!ll&&!rl&&r
000
MC/DC0%0/2
defined $unit and length $unit
defined $unit length $unit
Truth table0%0/3
A and B
A= defined $unitB= length $unit
ABresult
0X0
100
111
156 0 return source_term( 'Unit', $unit, $unit );
157 }
158  
159 18 ▶sub source_term {
Subroutine source_term: called CC=9 SCAR=24.8
Pod undocumented
160 18 my ( $namespace, $value, $label ) = @_;
161 18 ▶ $namespace = _trim($namespace) // 'Value';
Condition50%1/2
_trim($namespace) // "Value"
l!l
180
MC/DC0%0/1
_trim($namespace) // "Value"
_trim($namespace)
Truth table50%1/2
A
A= _trim($namespace)
Aresult
11
00
162 18 $value = _trim($value);
163 18 ▶ return unless defined $value && length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and length $value 0 18 18
Condition33%1/3
defined $value and length $value
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table33%1/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
164  
165 18 $namespace =~ s/[^A-Za-z0-9_.-]+/_/g;
166 18 ▶ $namespace = 'Value' unless length $namespace;
Branch: 1 branch
BranchTrueFalseTotal
unless length $namespace 0 18 18
167  
168 18 my $local = $value;
169 18 $local =~ s/\s+/_/g;
170 18 $local =~ s/[^A-Za-z0-9_.:\/\[\]{}+%=-]+/_/g;
171 18 $local =~ s/^_+|_+$//g;
172 18 ▶ $local = 'unknown' unless length $local;
Branch: 1 branch
BranchTrueFalseTotal
unless length $local 0 18 18
173  
174 18 my $term = { id => "FHIR:$namespace.$local" };
175 18 ▶ $term->{label} = $label
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and not ref $label and length $label 18 0 18
Condition33%2/6
defined $label and not ref $label and length $label
!ll&&!rl&&r
0018
defined $label and not ref $label
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $label and not ref $label and length $label
defined $label not ref $label length $label
Truth table25%1/4
A and not(B) and C
A= defined $labelB= not ref $labelC= length $label
ABCresult
0XX0
10X0
1100
1111
176 if defined $label && !ref($label) && length $label;
177 18 return $term;
178 }
179  
180 387 ▶sub _prefix_for_system {
Subroutine _prefix_for_system: called CC=6 SCAR=18.7
181 387 my ($system) = @_;
182 387 $system = _trim($system);
183 387 ▶ return 'FHIR' unless defined $system && length $system;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $system and length $system 0 387 387
Condition33%1/3
defined $system and length $system
!ll&&!rl&&r
00387
MC/DC0%0/2
defined $system and length $system
defined $system length $system
Truth table33%1/3
A and B
A= defined $systemB= length $system
ABresult
0X0
100
111
184  
185 387 $system =~ s{/+\z}{};
186 387 ▶ return $SYSTEM_PREFIX{$system} if exists $SYSTEM_PREFIX{$system};
Branch: 1 branch
BranchTrueFalseTotal
if exists $SYSTEM_PREFIX{$system} 364 23 387
187  
188 23 my ($tail) = $system =~ m{([^/:#]+)\z};
189 23 ▶ $tail //= 'Coding';
Condition50%1/2
$tail //= "Coding"
l!l
230
MC/DC0%0/1
$tail //= "Coding"
$tail
Truth table50%1/2
A
A= $tail
Aresult
11
00
190 23 $tail =~ s/[^A-Za-z0-9_.-]+/_/g;
191 23 ▶ $tail = 'Coding' unless length $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless length $tail 0 23 23
192 23 return "FHIR-$tail";
193 }
194  
195 1434 ▶sub _trim {
Subroutine _trim: called CC=3 SCAR=11.0
196 1434 my ($value) = @_;
197 1434 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 62 1372 1434
198 1372 ▶ return $value if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 1372 1372
199 1372 $value =~ s/^\s+|\s+$//g;
200 1372 return $value;
201 }
202  
203 136 1;
« lib/Convert/Pheno/Mapping/Shared.pm ↑ Summary api/perl/main.pl »