lib/Convert/Pheno/BFF/ToPXF.pm

filter: Statementstmt 91.5% 216 / 236 Branchbran 76.9% 140 / 182 Conditioncond 51.1% 46 / 90 MC/DCmcdc 30.6% 15 / 49 Subroutinesub 96.7% 30 / 31 Podpod 0.0% 0 / 2 totaltotal 75.7% 447 / 590 CC 128 SCAR 27.2
CC
128
Cov
79%
_normalize_evidence_for_pxf
56.0
_map_evidence_to_pxf
49.8
_map_sex
28.1
CRAP 15.2
« api/perl/main.pl ↑ Summary lib/Convert/Pheno/ClinicalCDM/Util.pm »
1 package Convert::Pheno::BFF::ToPXF;
2  
3 136 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 136 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 136 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 136 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 136 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 136 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 136 ▶use Convert::Pheno::Utils::Default qw(get_defaults);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 136 ▶use Convert::Pheno::Mapping::Shared;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 136 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 136 our @EXPORT = qw(do_bff2pxf);
13  
14 136 my $DEFAULT = get_defaults();
15  
16 #############
17 #############
18 # BFF2PXF #
19 #############
20 #############
21  
22 1793 ▶sub do_bff2pxf {
Subroutine do_bff2pxf: called CC=3 SCAR=11.0
Pod undocumented
23 1793 my ( $self, $bff ) = @_;
24  
25 # Premature return
26 1793 ▶ return unless defined($bff);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bff 0 1793 1793
27  
28 # Validate format
29 1793 ▶ die "Are you sure that your input is not already a pxf?\n"
Branch: 1 branch
BranchTrueFalseTotal
unless validate_format($bff, "bff") 0 1793 1793
30 unless validate_format( $bff, 'bff' );
31  
32 #########################################
33 # START MAPPING TO PHENOPACKET V2 TERMS #
34 #########################################
35  
36 # Initiate PXF structure
37 1793 my $pxf = {};
38  
39 # Map id
40 1793 _map_id( $self, $bff, $pxf );
41  
42 # Map subject
43 1793 _map_subject( $self, $bff, $pxf );
44  
45 # ===================
46 # phenotypicFeatures
47 # ===================
48 1793 _map_phenotypic_features( $self, $bff, $pxf );
49  
50 # ============
51 # measurements
52 # ============
53 1793 _map_measurements( $self, $bff, $pxf );
54  
55 # ========
56 # diseases
57 # ========
58 1793 _map_diseases( $self, $bff, $pxf );
59  
60 # ===============
61 # medicalActions
62 # ===============
63 1793 _map_medical_actions( $self, $bff, $pxf );
64  
65 # =================================================
66 # Phenopacket payload preserved under info.phenopacket
67 # =================================================
68 1793 _map_phenopacket_payload( $self, $bff, $pxf );
69  
70 # =====
71 # files
72 # =====
73 # (Not mapped, see original code comments)
74  
75 # =========
76 # metaData
77 # =========
78 1793 _map_metaData( $self, $bff, $pxf );
79  
80 # =========
81 # exposures
82 # =========
83  
84 # Phenopackets v2 does not define an "exposures" field in the Phenopacket schema.
85 #
86 # Sept 2023 -> pxf-tools error:
87 # Message type "org.phenopackets.schema.v2.Phenopacket" has no field named "exposures".
88 # Available fields: ['id', 'subject', 'phenotypicFeatures', 'measurements', 'biosamples',
89 # 'interpretations', 'diseases', 'medicalActions', 'files', 'metaData']
90 #
91 # Explanation provided by @gsfk (issue #4): Exposure was proposed for v2 but not
92 # adopted and later removed (ga4gh/phenopacket-schema@cb8bf58). Some documentation
93 # still incorrectly lists it.
94 #
95 # Do not map exposures; this block is legacy.
96 # Tracking: https://github.com/CNAG-Biomedical-Informatics/convert-pheno/issues/4
97 # Observed Sept 2023; reconfirmed Feb 2026
98  
99 # $pxf->{exposures} =
100 #
101 # [
102 # map {
103 # {
104 # type => $_->{exposureCode},
105 # occurrence => { timestamp => $_->{date} }
106 # }
107 # } @{ $bff->{exposures} }
108 # ]
109 # if exists $bff->{exposures};
110  
111 #######################################
112 # END MAPPING TO PHENOPACKET V2 TERMS #
113 #######################################
114  
115 1793 _strip_private_keys($pxf);
116  
117 1793 return $pxf;
118 }
119  
120 1793 ▶sub _map_id {
Subroutine _map_id: called CC=2 SCAR=7.2
121 1793 my ( $self, $bff, $pxf ) = @_;
122  
123 # ==
124 # id
125 # ==
126 $pxf->{id} =
127 $self->{test}
128 ? undef
129 1793 ▶ : 'phenopacket_id.' . generate_random_alphanumeric_string(8);
Branch: 1 branch
BranchTrueFalseTotal
$self->{'test'} ? : 1793 0 1793
130 }
131  
132 1793 ▶sub _map_subject {
Subroutine _map_subject: called CC=4 SCAR=13.9
133 1793 my ( $self, $bff, $pxf ) = @_;
134  
135 1793 my $vitalStatus = _resolve_vitalStatus( $self, $bff );
136  
137 # =======
138 # subject
139 # =======
140 $pxf->{subject} = {
141 id => $bff->{id},
142 1793 vitalStatus => $vitalStatus,
143 };
144  
145 1793 my $sex = _map_sex( $bff->{sex} );
146 1793 ▶ $pxf->{subject}{sex} = $sex if defined $sex;
Branch: 1 branch
BranchTrueFalseTotal
if defined $sex 1793 0 1793
147  
148 1793 my $dateOfBirth = _get_phenopacket_info_term( $bff, 'dateOfBirth' );
149 1793 ▶ $pxf->{subject}{dateOfBirth} = $dateOfBirth if defined $dateOfBirth;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dateOfBirth 1528 265 1793
150  
151 # karyotypicSex
152 $pxf->{subject}{karyotypicSex} = $bff->{karyotypicSex}
153 1793 ▶ if exists $bff->{karyotypicSex};
Branch: 1 branch
BranchTrueFalseTotal
if exists $bff->{"karyotypicSex"} 13 1780 1793
154 }
155  
156 1793 ▶sub _map_phenotypic_features {
Subroutine _map_phenotypic_features: called CC=9 SCAR=22.0
157 1793 my ( $self, $bff, $pxf ) = @_;
158  
159 # ===================
160 # phenotypicFeatures
161 # ===================
162 # Assign transformed 'phenotypicFeatures' to $pxf, only if it's defined in $bff
163 $pxf->{phenotypicFeatures} = [
164 map {
165 1330 my $feature = _clone_data($_);
166 1330 my %mapped;
167  
168 $mapped{type} = _clone_data( $feature->{featureType} )
169 1330 ▶ if exists $feature->{featureType};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"featureType"} 1330 0 1330
170 $mapped{excluded} =
171 exists $feature->{excluded}
172 ? $feature->{excluded}
173 1330 ▶ : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
exists $feature->{'excluded'} ? : 1089 241 1330
174 $mapped{severity} = _clone_data( $feature->{severity} )
175 1330 ▶ if exists $feature->{severity};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"severity"} 7 1323 1330
176 $mapped{modifiers} = _clone_data( $feature->{modifiers} )
177 1330 ▶ if exists $feature->{modifiers};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"modifiers"} 9 1321 1330
178 $mapped{evidence} = _map_evidence_to_pxf( $feature->{evidence} )
179 1330 ▶ if exists $feature->{evidence};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"evidence"} 19 1311 1330
180 $mapped{onset} = _map_time_element_to_pxf( $feature->{onset} )
181 1330 ▶ if exists $feature->{onset};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"onset"} 318 1012 1330
182 $mapped{resolution} = _map_time_element_to_pxf( $feature->{resolution} )
183 1330 ▶ if exists $feature->{resolution};
Branch: 1 branch
BranchTrueFalseTotal
if exists $feature->{"resolution"} 6 1324 1330
184  
185 1330 \%mapped;
186 373 } @{ $bff->{phenotypicFeatures} }
187 ]
188 1793 ▶ if defined $bff->{phenotypicFeatures};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bff->{"phenotypicFeatures"} 373 1420 1793
189 }
190  
191 1793 ▶sub _map_measurements {
Subroutine _map_measurements: called CC=10 SCAR=23.0
192 1793 my ( $self, $bff, $pxf ) = @_;
193  
194 # ============
195 # measurements
196 # ============
197 1793 ▶ if ( defined $bff->{measures} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bff->{"measures"}) 358 1435 1793
198 358 $pxf->{measurements} = []; # Initialize as an empty array reference
199  
200 358 for my $measure ( @{ $bff->{measures} } ) {
201 2887 my $measurementValue = _map_measurement_value_to_pxf( $measure->{measurementValue} );
202 my $hasTypedQuantities =
203 ref($measurementValue) eq 'HASH'
204 2887 ▶ && exists $measurementValue->{typedQuantities} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $measurementValue eq 'HASH' && exists $measurementValue->{'typedQuantities'} ? : 4 2883 2887
Condition66%2/3
ref $measurementValue eq "HASH" and exists $measurementValue->{"typedQuantities"}
!ll&&!rl&&r
028834
MC/DC50%1/2
ref $measurementValue eq "HASH" and exists $measurementValue->{"typedQuantities"}
ref $measurementValue eq "HASH" exists $measurementValue->{"typedQuantities"}
Truth table66%2/3
A and B
A= ref $measurementValue eq "HASH"B= exists $measurementValue->{"typedQuantities"}
ABresult
0X0
100
111
205  
206 # Construct the hash
207 2887 my $result = { assay => _clone_data( $measure->{assayCode} ) };
208  
209 # Add the complexValue key if typedQuantities was found
210 2887 ▶ if ($hasTypedQuantities) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasTypedQuantities) { } 4 2883 2887
211 4 $result->{complexValue} = $measurementValue;
212 }
213 else {
214 2883 $result->{value} = $measurementValue;
215 }
216 $result->{timeObserved} = _map_time_element_to_pxf( $measure->{observationMoment} )
217 2887 ▶ if exists $measure->{observationMoment};
Branch: 1 branch
BranchTrueFalseTotal
if exists $measure->{"observationMoment"} 332 2555 2887
218 $result->{timeObserved} = { timestamp => map_iso8601_date2timestamp( $measure->{date} ) }
219 if !exists $result->{timeObserved}
220 2887 ▶ && exists $measure->{date};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $result->{"timeObserved"} and exists $measure->{"date"} 2547 340 2887
Condition100%3/3
not exists $result->{"timeObserved"} and exists $measure->{"date"}
!ll&&!rl&&r
33282547
MC/DC100%2/2
not exists $result->{"timeObserved"} and exists $measure->{"date"}
not exists $result->{"timeObserved"} exists $measure->{"date"}
Truth table100%3/3
not(A) and B
A= not exists $result->{"timeObserved"}B= exists $measure->{"date"}
ABresult
0X0
100
111
221 $result->{procedure} = map_procedures( $measure->{procedure} )
222 2887 ▶ if defined $measure->{procedure};
Branch: 1 branch
BranchTrueFalseTotal
if defined $measure->{"procedure"} 2703 184 2887
223  
224 # Push the resulting hash onto the pxf measurements array
225 2887 push @{ $pxf->{measurements} }, $result;
226 }
227 }
228 }
229  
230 1793 ▶sub _map_diseases {
Subroutine _map_diseases: called CC=10 SCAR=23.0
231 1793 my ( $self, $bff, $pxf ) = @_;
232 1793 ▶ my $diseases = $bff->{diseases} || [];
Condition100%2/2
$bff->{'diseases'} || []
l!l
4791314
MC/DC100%1/1
$bff->{'diseases'} || []
$bff->{'diseases'}
Truth table100%2/2
A
A= $bff->{'diseases'}
Aresult
11
00
233  
234 # ========
235 # diseases
236 # ========
237 $pxf->{diseases} = [
238 map {
239 1830 my $disease = _clone_data($_);
240 1830 my %mapped;
241  
242 $mapped{term} = _clone_data( $disease->{diseaseCode} )
243 1830 ▶ if exists $disease->{diseaseCode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"diseaseCode"} 1830 0 1830
244 $mapped{excluded} = $disease->{excluded}
245 1830 ▶ if exists $disease->{excluded};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"excluded"} 12 1818 1830
246 $mapped{onset} = _map_time_element_to_pxf( $disease->{ageOfOnset} )
247 1830 ▶ if exists $disease->{ageOfOnset};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"ageOfOnset"} 1766 64 1830
248 $mapped{resolution} = _map_time_element_to_pxf( $disease->{resolution} )
249 1830 ▶ if exists $disease->{resolution};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"resolution"} 3 1827 1830
250 $mapped{diseaseStage} = _clone_data( $disease->{diseaseStage} )
251 1830 ▶ if exists $disease->{diseaseStage};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"diseaseStage"} 2 1828 1830
252 $mapped{clinicalTnmFinding} = _clone_data( $disease->{clinicalTnmFinding} )
253 1830 ▶ if exists $disease->{clinicalTnmFinding};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"clinicalTnmFinding"} 3 1827 1830
254 $mapped{primarySite} = _clone_data( $disease->{primarySite} )
255 1830 ▶ if exists $disease->{primarySite};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"primarySite"} 2 1828 1830
256 $mapped{laterality} = _clone_data( $disease->{laterality} )
257 1830 ▶ if exists $disease->{laterality};
Branch: 1 branch
BranchTrueFalseTotal
if exists $disease->{"laterality"} 0 1830 1830
258  
259 1830 \%mapped;
260 1793 } @{$diseases}
261 ];
262 }
263  
264 1793 ▶sub _map_medical_actions {
Subroutine _map_medical_actions: called CC=8 SCAR=20.8
265 1793 my ( $self, $bff, $pxf ) = @_;
266  
267 # ===============
268 # medicalActions
269 # ===============
270  
271 # **** procedures ****
272 my @procedures = map_procedures( $bff->{interventionsOrProcedures} )
273 1793 ▶ if defined $bff->{interventionsOrProcedures};
Branch: 1 branch
BranchTrueFalseTotal
if defined $bff->{"interventionsOrProcedures"} 480 1313 1793
274  
275 # **** treatments ****
276 my @treatments = map {
277 878 my $treatment = _clone_data($_);
278 878 my %mapped;
279 $mapped{agent} = _clone_data( $treatment->{treatmentCode} )
280 878 ▶ if exists $treatment->{treatmentCode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $treatment->{"treatmentCode"} 878 0 878
281 $mapped{routeOfAdministration} =
282 _clone_data( $treatment->{routeOfAdministration} )
283 878 ▶ if exists $treatment->{routeOfAdministration};
Branch: 1 branch
BranchTrueFalseTotal
if exists $treatment->{"routeOfAdministration"} 871 7 878
284 $mapped{doseIntervals} = _clone_data( $treatment->{doseIntervals} )
285 878 ▶ if exists $treatment->{doseIntervals};
Branch: 1 branch
BranchTrueFalseTotal
if exists $treatment->{"doseIntervals"} 860 18 878
286 {
287 878 treatment => {
288 %mapped,
289 }
290 }
291 1793 ▶ } @{ $bff->{treatments} // [] };
Condition100%2/2
$bff->{"treatments"} // []
l!l
3011492
MC/DC100%1/1
$bff->{"treatments"} // []
$bff->{"treatments"}
Truth table100%2/2
A
A= $bff->{"treatments"}
Aresult
11
00
292  
293 # Load
294 1793 ▶ push @{ $pxf->{medicalActions} }, @procedures if @procedures;
Branch: 1 branch
BranchTrueFalseTotal
if @procedures 480 1313 1793
295 1793 ▶ push @{ $pxf->{medicalActions} }, @treatments if @treatments;
Branch: 1 branch
BranchTrueFalseTotal
if @treatments 301 1492 1793
296 }
297  
298 1793 ▶sub _map_phenopacket_payload {
Subroutine _map_phenopacket_payload: called CC=3 SCAR=11.0
299 1793 my ( $self, $bff, $pxf ) = @_;
300  
301 1793 for my $term (qw(biosamples interpretations genes variants files pedigree)) {
302 10758 my $value = _get_phenopacket_info_term( $bff, $term );
303 10758 ▶ $pxf->{$term} = _clone_data($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 42 10716 10758
304 }
305 }
306  
307 1793 ▶sub _map_metaData {
Subroutine _map_metaData: called CC=3 SCAR=14.2
308 1793 my ( $self, $bff, $pxf ) = @_;
309  
310 # =========
311 # metaData
312 # =========
313 # Depending on the origion (redcap) , _info and resources may exist
314 $pxf->{metaData} =
315 $self->{test} ? undef
316 1793 ▶ : defined( _get_phenopacket_info_term( $bff, 'metaData' ) )
Branch: 2 branches
BranchTrueFalseTotal
$self->{'test'} ? : 1793 0 1793
defined _get_phenopacket_info_term($bff, 'metaData') ? : 0 0 0
317 ? _clone_data( _get_phenopacket_info_term( $bff, 'metaData' ) )
318 : get_metaData($self);
319 }
320  
321 #----------------------------------------------------------------------
322 # Helper subs
323 #----------------------------------------------------------------------
324  
325 14344 ▶sub _get_phenopacket_info_term {
Subroutine _get_phenopacket_info_term: called CC=7 SCAR=19.5
326 14344 my ( $bff, $term ) = @_;
327  
328 14344 ▶ return unless exists $bff->{info} && ref( $bff->{info} ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless exists $bff->{"info"} and ref $bff->{"info"} eq "HASH" 16 14328 14344
Condition66%2/3
exists $bff->{"info"} and ref $bff->{"info"} eq "HASH"
!ll&&!rl&&r
16014328
MC/DC50%1/2
exists $bff->{"info"} and ref $bff->{"info"} eq "HASH"
exists $bff->{"info"} ref $bff->{"info"} eq "HASH"
Truth table66%2/3
A and B
A= exists $bff->{"info"}B= ref $bff->{"info"} eq "HASH"
ABresult
0X0
100
111
329  
330 return $bff->{info}{phenopacket}{$term}
331 if exists $bff->{info}{phenopacket}
332 && ref( $bff->{info}{phenopacket} ) eq 'HASH'
333 14328 ▶ && exists $bff->{info}{phenopacket}{$term};
Branch: 1 branch
BranchTrueFalseTotal
if exists $bff->{"info"}{"phenopacket"} and ref $bff->{"info"}{"phenopacket"} eq "HASH" and exists $bff->{"info"}{"phenopacket"}{$term} 71 14257 14328
Condition83%5/6
exists $bff->{"info"}{"phenopacket"} and ref $bff->{"info"}{"phenopacket"} eq "HASH" and exists $bff->{"info"}{"phenopacket"}{$term}
!ll&&!rl&&r
1391234571
exists $bff->{"info"}{"phenopacket"} and ref $bff->{"info"}{"phenopacket"} eq "HASH"
!ll&&!rl&&r
139120416
MC/DC66%2/3
exists $bff->{"info"}{"phenopacket"} and ref $bff->{"info"}{"phenopacket"} eq "HASH" and exists $bff->{"info"}{"phenopacket"}{$term}
exists $bff->{"info"}{"phenopacket"} ref $bff->{"info"}{"phenopacket"} eq "HASH" exists $bff->{"info"}{"phenopacket"}{$term}
Truth table75%3/4
A and B and C
A= exists $bff->{"info"}{"phenopacket"}B= ref $bff->{"info"}{"phenopacket"} eq "HASH"C= exists $bff->{"info"}{"phenopacket"}{$term}
ABCresult
0XX0
10X0
1100
1111
334  
335 14257 ▶ return $bff->{info}{$term} if exists $bff->{info}{$term};
Branch: 1 branch
BranchTrueFalseTotal
if exists $bff->{"info"}{$term} 1509 12748 14257
336  
337 12748 return;
338 }
339  
340 1793 ▶sub _resolve_vitalStatus {
Subroutine _resolve_vitalStatus: called CC=3 SCAR=11.0
341 1793 my ( $self, $bff ) = @_;
342  
343 1793 my $vitalStatus = _get_phenopacket_info_term( $bff, 'vitalStatus' );
344 1793 ▶ return _clone_data($vitalStatus) if defined $vitalStatus;
Branch: 1 branch
BranchTrueFalseTotal
if defined $vitalStatus 10 1783 1793
345  
346 1783 ▶ return { status => $self->{default_vital_status} || 'ALIVE' };
Condition100%2/2
$self->{'default_vital_status'} || 'ALIVE'
l!l
111772
MC/DC100%1/1
$self->{'default_vital_status'} || 'ALIVE'
$self->{'default_vital_status'}
Truth table100%2/2
A
A= $self->{'default_vital_status'}
Aresult
11
00
347 }
348  
349 217603 ▶sub _clone_data {
Subroutine _clone_data: called CC=7 SCAR=19.5
350 217603 my ($data) = @_;
351 217603 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 6108 211495 217603
352  
353 211495 ▶ if ( ref($data) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 76128 135367 211495
354 76128 return { map { $_ => _clone_data( $data->{$_} ) } keys %{$data} };
355 }
356  
357 135367 ▶ if ( ref($data) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 1956 133411 135367
358 1956 return [ map { _clone_data($_) } @{$data} ];
359 }
360  
361 133411 ▶ if ( blessed($data) && blessed($data) eq 'JSON::PP::Boolean' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data) and builtin::blessed($data) eq "JSON::PP::Boolean") 1254 132157 133411
Condition66%2/3
builtin::blessed($data) and builtin::blessed($data) eq "JSON::PP::Boolean"
!ll&&!rl&&r
13215701254
MC/DC50%1/2
builtin::blessed($data) and builtin::blessed($data) eq "JSON::PP::Boolean"
builtin::blessed($data) builtin::blessed($data) eq "JSON::PP::Boolean"
Truth table66%2/3
A and B
A= builtin::blessed($data)B= builtin::blessed($data) eq "JSON::PP::Boolean"
ABresult
0X0
100
111
362 1254 ▶ return $data ? JSON::PP::true() : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
$data ? : 898 356 1254
363 }
364  
365 132157 return $data;
366 }
367  
368 153608 ▶sub _strip_private_keys {
Subroutine _strip_private_keys: called CC=7 SCAR=19.6
369 153608 my ($data) = @_;
370 153608 ▶ return unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 3829 149779 153608
371  
372 149779 ▶ if ( ref($data) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 70853 78926 149779
373 70853 for my $key ( keys %{$data} ) {
374 141242 ▶ if ( $key =~ /^_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_/) 0 141242 141242
375 0 delete $data->{$key};
376 0 next;
377 }
378 141242 _strip_private_keys( $data->{$key} );
379 }
380 70853 return;
381 }
382  
383 78926 ▶ if ( ref($data) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 4086 74840 78926
384 4086 _strip_private_keys($_) for @{$data};
385 4086 return;
386 }
387  
388 74840 return;
389 }
390  
391 1793 ▶sub _map_sex {
Subroutine _map_sex: called CC=16 SCAR=33.4
392 1793 my ($sex) = @_;
393 1793 ▶ return 'UNKNOWN_SEX' unless defined $sex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sex 3 1790 1793
394  
395 1790 ▶ my $id = ref($sex) eq 'HASH' ? $sex->{id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $sex eq 'HASH' ? : 1790 0 1790
396 1790 ▶ my $label = ref($sex) eq 'HASH' ? $sex->{label} : $sex;
Branch: 1 branch
BranchTrueFalseTotal
ref $sex eq 'HASH' ? : 1790 0 1790
397  
398 1790 ▶ return 'MALE' if defined $id && $id eq 'NCIT:C20197';
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id eq "NCIT:C20197" 895 895 1790
Condition66%2/3
defined $id and $id eq "NCIT:C20197"
!ll&&!rl&&r
0895895
MC/DC50%1/2
defined $id and $id eq "NCIT:C20197"
defined $id $id eq "NCIT:C20197"
Truth table66%2/3
A and B
A= defined $idB= $id eq "NCIT:C20197"
ABresult
0X0
100
111
399 895 ▶ return 'FEMALE' if defined $id && $id eq 'NCIT:C16576';
Branch: 1 branch
BranchTrueFalseTotal
if defined $id and $id eq "NCIT:C16576" 875 20 895
Condition66%2/3
defined $id and $id eq "NCIT:C16576"
!ll&&!rl&&r
020875
MC/DC50%1/2
defined $id and $id eq "NCIT:C16576"
defined $id $id eq "NCIT:C16576"
Truth table66%2/3
A and B
A= defined $idB= $id eq "NCIT:C16576"
ABresult
0X0
100
111
400  
401 20 ▶ return 'MALE' if defined $label && $label =~ /^male$/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and $label =~ /^male$/i 0 20 20
Condition33%1/3
defined $label and $label =~ /^male$/i
!ll&&!rl&&r
0200
MC/DC0%0/2
defined $label and $label =~ /^male$/i
defined $label $label =~ /^male$/i
Truth table33%1/3
A and B
A= defined $labelB= $label =~ /^male$/i
ABresult
0X0
100
111
402 20 ▶ return 'FEMALE' if defined $label && $label =~ /^female$/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and $label =~ /^female$/i 0 20 20
Condition33%1/3
defined $label and $label =~ /^female$/i
!ll&&!rl&&r
0200
MC/DC0%0/2
defined $label and $label =~ /^female$/i
defined $label $label =~ /^female$/i
Truth table33%1/3
A and B
A= defined $labelB= $label =~ /^female$/i
ABresult
0X0
100
111
403 20 ▶ return 'OTHER_SEX' if defined $label && $label =~ /^other(?:_sex)?$/i;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and $label =~ /^other(?:_sex)?$/i 0 20 20
Condition33%1/3
defined $label and $label =~ /^other(?:_sex)?$/i
!ll&&!rl&&r
0200
MC/DC0%0/2
defined $label and $label =~ /^other(?:_sex)?$/i
defined $label $label =~ /^other(?:_sex)?$/i
Truth table33%1/3
A and B
A= defined $labelB= $label =~ /^other(?:_sex)?$/i
ABresult
0X0
100
111
404 20 ▶ return 'UNKNOWN_SEX' if defined $label
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i 20 0 20
Condition33%1/3
defined $label and $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $label and $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i
defined $label $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i
Truth table33%1/3
A and B
A= defined $labelB= $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i
ABresult
0X0
100
111
405 && $label =~ /^(?:unknown(?:[_ ]sex)?|not available)$/i;
406  
407 0 return 'UNKNOWN_SEX';
408 }
409  
410 5860 ▶sub _looks_like_pxf_time_element {
Subroutine _looks_like_pxf_time_element: called CC=2 SCAR=6.9
411 5860 my ($time) = @_;
412 5860 ▶ return 0 unless ref($time) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $time eq "HASH" 7 5853 5860
413  
414 5853 return scalar grep { exists $time->{$_} }
415 qw(age ageRange gestationalAge interval ontologyClass timestamp);
416 }
417  
418 5860 ▶sub _map_time_element_to_pxf {
Subroutine _map_time_element_to_pxf: called CC=14 SCAR=28.1
419 5860 my ($time) = @_;
420 5860 ▶ return unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 5860 5860
421  
422 5860 ▶ return _clone_data($time) if _looks_like_pxf_time_element($time);
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_pxf_time_element($time) 5522 338 5860
423  
424 338 ▶ if ( !ref($time) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $time) 7 331 338
425 return {
426 7 ▶ timestamp => $time =~ /^\d{4}-\d{2}-\d{2}$/
Branch: 1 branch
BranchTrueFalseTotal
$time =~ /^\d{4}-\d{2}-\d{2}$/ ? : 0 7 7
427 ? map_iso8601_date2timestamp($time)
428 : $time,
429 };
430 }
431  
432 331 ▶ return _clone_data($time) unless ref($time) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $time eq "HASH" 0 331 331
433  
434 331 ▶ if ( exists $time->{iso8601duration} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $time->{"iso8601duration"}) 300 31 331
435 300 return { age => _clone_data($time) };
436 }
437  
438 31 ▶ if ( exists $time->{weeks} || exists $time->{days} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $time->{"weeks"} or exists $time->{"days"}) 9 22 31
Condition66%2/3
exists $time->{"weeks"} or exists $time->{"days"}
l!l&&r!l&&!r
9022
MC/DC50%1/2
exists $time->{"weeks"} or exists $time->{"days"}
exists $time->{"weeks"} exists $time->{"days"}
Truth table66%2/3
A or B
A= exists $time->{"weeks"}B= exists $time->{"days"}
ABresult
1X1
011
000
439 9 return { gestationalAge => _clone_data($time) };
440 }
441  
442 22 ▶ if ( exists $time->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $time->{"id"}) 18 4 22
443 18 return { ontologyClass => _clone_data($time) };
444 }
445  
446 4 ▶ if ( exists $time->{start} && exists $time->{end} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $time->{"start"} and exists $time->{"end"}) 4 0 4
Condition33%1/3
exists $time->{"start"} and exists $time->{"end"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $time->{"start"} and exists $time->{"end"}
exists $time->{"start"} exists $time->{"end"}
Truth table33%1/3
A and B
A= exists $time->{"start"}B= exists $time->{"end"}
ABresult
0X0
100
111
447 4 my $start = $time->{start};
448 4 my $end = $time->{end};
449 return {
450 4 ▶ ref($start) eq 'HASH' || ref($end) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $start eq 'HASH' || ref $end eq 'HASH' ? : 0 4 4
Condition33%1/3
ref $start eq "HASH" or ref $end eq "HASH"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $start eq "HASH" or ref $end eq "HASH"
ref $start eq "HASH" ref $end eq "HASH"
Truth table33%1/3
A or B
A= ref $start eq "HASH"B= ref $end eq "HASH"
ABresult
1X1
011
000
451 ? ( ageRange => _clone_data($time) )
452 : ( interval => _clone_data($time) )
453 };
454 }
455  
456 0 return _clone_data($time);
457 }
458  
459 0 ▶sub _normalize_evidence_for_pxf {
Subroutine _normalize_evidence_for_pxf: not called CC=7 SCAR=40.3
460 0 my ($evidence) = @_;
461 0 ▶ return unless ref($evidence) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $evidence eq "HASH" 0 0 0
462  
463 0 my $mapped = _clone_data($evidence);
464 0 ▶ delete $mapped->{info} if exists $mapped->{info};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mapped->{"info"} 0 0 0
465  
466 0 ▶ if ( exists $mapped->{reference}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mapped->{"reference"} and ref $mapped->{"reference"} eq "HASH" and exists $mapped->{"reference"}{"notes"} and not exists $mapped->{"reference"}{"description"}) 0 0 0
Condition0%0/9
exists $mapped->{"reference"} and ref $mapped->{"reference"} eq "HASH" and exists $mapped->{"reference"}{"notes"} and not exists $mapped->{"reference"}{"description"}
!ll&&!rl&&r
000
exists $mapped->{"reference"} and ref $mapped->{"reference"} eq "HASH" and exists $mapped->{"reference"}{"notes"}
!ll&&!rl&&r
000
exists $mapped->{"reference"} and ref $mapped->{"reference"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
exists $mapped->{"reference"} and ref $mapped->{"reference"} eq "HASH" and exists $mapped->{"reference"}{"notes"} and not exists $mapped->{"reference"}{"description"}
exists $mapped->{"reference"} ref $mapped->{"reference"} eq "HASH" exists $mapped->{"reference"}{"notes"} not exists $mapped->{"reference"}{"description"}
Truth table0%0/5
A and B and C and not(D)
A= exists $mapped->{"reference"}B= ref $mapped->{"reference"} eq "HASH"C= exists $mapped->{"reference"}{"notes"}D= not exists $mapped->{"reference"}{"description"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
467 && ref( $mapped->{reference} ) eq 'HASH'
468 && exists $mapped->{reference}{notes}
469 && !exists $mapped->{reference}{description} )
470 {
471 0 $mapped->{reference}{description} = delete $mapped->{reference}{notes};
472 }
473  
474 0 return $mapped;
475 }
476  
477 19 ▶sub _map_evidence_to_pxf {
Subroutine _map_evidence_to_pxf: called CC=11 SCAR=39.1
478 19 my ($evidence) = @_;
479 19 ▶ return unless defined $evidence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $evidence 0 19 19
480  
481 19 ▶ if ( ref($evidence) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"} and ref $evidence->{"info"}{"phenopacket"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"}{"evidence"} and ref $evidence->{"info"}{"phenopacket"}{"evidence"} eq "ARRAY") 19 0 19
Condition33%6/18
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"} and ref $evidence->{"info"}{"phenopacket"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"}{"evidence"} and ref $evidence->{"info"}{"phenopacket"}{"evidence"} eq "ARRAY"
!ll&&!rl&&r
0019
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"} and ref $evidence->{"info"}{"phenopacket"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"}{"evidence"}
!ll&&!rl&&r
0019
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"} and ref $evidence->{"info"}{"phenopacket"} eq "HASH"
!ll&&!rl&&r
0019
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"}
!ll&&!rl&&r
0019
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH"
!ll&&!rl&&r
0019
ref $evidence eq "HASH" and exists $evidence->{"info"}
!ll&&!rl&&r
0019
MC/DC0%0/7
ref $evidence eq "HASH" and exists $evidence->{"info"} and ref $evidence->{"info"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"} and ref $evidence->{"info"}{"phenopacket"} eq "HASH" and exists $evidence->{"info"}{"phenopacket"}{"evidence"} and ref $evidence->{"info"}{"phenopacket"}{"evidence"} eq "ARRAY"
ref $evidence eq "HASH" exists $evidence->{"info"} ref $evidence->{"info"} eq "HASH" exists $evidence->{"info"}{"phenopacket"} ref $evidence->{"info"}{"phenopacket"} eq "HASH" exists $evidence->{"info"}{"phenopacket"}{"evidence"} ref $evidence->{"info"}{"phenopacket"}{"evidence"} eq "ARRAY"
Truth table12%1/8
A and B and C and D and E and F and G
A= ref $evidence eq "HASH"B= exists $evidence->{"info"}C= ref $evidence->{"info"} eq "HASH"D= exists $evidence->{"info"}{"phenopacket"}E= ref $evidence->{"info"}{"phenopacket"} eq "HASH"F= exists $evidence->{"info"}{"phenopacket"}{"evidence"}G= ref $evidence->{"info"}{"phenopacket"}{"evidence"} eq "ARRAY"
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XXX0
11110XX0
111110X0
11111100
11111111
482 && exists $evidence->{info}
483 && ref( $evidence->{info} ) eq 'HASH'
484 && exists $evidence->{info}{phenopacket}
485 && ref( $evidence->{info}{phenopacket} ) eq 'HASH'
486 && exists $evidence->{info}{phenopacket}{evidence}
487 && ref( $evidence->{info}{phenopacket}{evidence} ) eq 'ARRAY' )
488 {
489 19 return _clone_data( $evidence->{info}{phenopacket}{evidence} );
490 }
491  
492 0 ▶ if ( ref($evidence) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $evidence eq "ARRAY") 0 0 0
493 return [
494 0 map { _normalize_evidence_for_pxf($_) }
495 0 grep { defined $_ }
496 0 map { _normalize_evidence_for_pxf($_) } @{$evidence}
497 ];
498 }
499  
500 0 my $mapped = _normalize_evidence_for_pxf($evidence);
501 0 ▶ return defined $mapped ? [$mapped] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $mapped ? : 0 0 0
502 }
503  
504 2887 ▶sub _map_measurement_value_to_pxf {
Subroutine _map_measurement_value_to_pxf: called CC=12 SCAR=26.2
505 2887 my ($value) = @_;
506 2887 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2887 2887
507  
508 2887 my $mapped = _clone_data($value);
509 2887 ▶ return $mapped unless ref($mapped) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mapped eq "HASH" 0 2887 2887
510  
511 2887 ▶ if ( exists $mapped->{typedQuantities}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mapped->{"typedQuantities"} and ref $mapped->{"typedQuantities"} eq "ARRAY") 4 2883 2887
Condition66%2/3
exists $mapped->{"typedQuantities"} and ref $mapped->{"typedQuantities"} eq "ARRAY"
!ll&&!rl&&r
288304
MC/DC50%1/2
exists $mapped->{"typedQuantities"} and ref $mapped->{"typedQuantities"} eq "ARRAY"
exists $mapped->{"typedQuantities"} ref $mapped->{"typedQuantities"} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $mapped->{"typedQuantities"}B= ref $mapped->{"typedQuantities"} eq "ARRAY"
ABresult
0X0
100
111
512 && ref( $mapped->{typedQuantities} ) eq 'ARRAY' )
513 {
514 4 for my $typedQuantity ( @{ $mapped->{typedQuantities} } ) {
515 5 ▶ next unless ref($typedQuantity) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $typedQuantity eq "HASH" 0 5 5
516 $typedQuantity->{type} = delete $typedQuantity->{quantityType}
517 5 ▶ if exists $typedQuantity->{quantityType};
Branch: 1 branch
BranchTrueFalseTotal
if exists $typedQuantity->{"quantityType"} 5 0 5
518 }
519 }
520  
521 # Phenopackets Value is a oneof wrapper. Beacon represents categorical
522 # values directly as ontology terms, so restore the ontologyClass arm
523 # rather than emitting id/label beside quantity.
524 return { ontologyClass => $mapped }
525 if exists $mapped->{id}
526 && !exists $mapped->{quantity}
527 && !exists $mapped->{ontologyClass}
528 2887 ▶ && !exists $mapped->{typedQuantities};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mapped->{"id"} and not exists $mapped->{"quantity"} and not exists $mapped->{"ontologyClass"} and not exists $mapped->{"typedQuantities"} 8 2879 2887
Condition66%6/9
exists $mapped->{"id"} and not exists $mapped->{"quantity"} and not exists $mapped->{"ontologyClass"} and not exists $mapped->{"typedQuantities"}
!ll&&!rl&&r
287908
exists $mapped->{"id"} and not exists $mapped->{"quantity"} and not exists $mapped->{"ontologyClass"}
!ll&&!rl&&r
287908
exists $mapped->{"id"} and not exists $mapped->{"quantity"}
!ll&&!rl&&r
287908
MC/DC25%1/4
exists $mapped->{"id"} and not exists $mapped->{"quantity"} and not exists $mapped->{"ontologyClass"} and not exists $mapped->{"typedQuantities"}
exists $mapped->{"id"} not exists $mapped->{"quantity"} not exists $mapped->{"ontologyClass"} not exists $mapped->{"typedQuantities"}
Truth table40%2/5
A and not(B) and not(C) and not(D)
A= exists $mapped->{"id"}B= not exists $mapped->{"quantity"}C= not exists $mapped->{"ontologyClass"}D= not exists $mapped->{"typedQuantities"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
529  
530 2879 return $mapped;
531 }
532  
533 5879 ▶sub _map_procedure_performed {
Subroutine _map_procedure_performed: called CC=4 SCAR=13.9
534 5879 my ($procedure) = @_;
535 5879 ▶ return unless ref($procedure) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $procedure eq "HASH" 0 5879 5879
536  
537 return _map_time_element_to_pxf( $procedure->{ageAtProcedure} )
538 5879 ▶ if exists $procedure->{ageAtProcedure};
Branch: 1 branch
BranchTrueFalseTotal
if exists $procedure->{"ageAtProcedure"} 3435 2444 5879
539  
540 return { timestamp => map_iso8601_date2timestamp( $procedure->{dateOfProcedure} ) }
541 2444 ▶ if exists $procedure->{dateOfProcedure};
Branch: 1 branch
BranchTrueFalseTotal
if exists $procedure->{"dateOfProcedure"} 14 2430 2444
542  
543 2430 return;
544 }
545  
546 3183 ▶sub map_procedures {
Subroutine map_procedures: called CC=3 SCAR=11.0
Pod undocumented
547 3183 my $data = shift;
548  
549 # Helper to apply mapping logic to a single item
550 5879 ▶ my $map_item = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
551 5879 my $item = shift;
552 5879 my %mapped;
553 $mapped{bodySite} = _clone_data( $item->{bodySite} )
554 5879 ▶ if exists $item->{bodySite};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"bodySite"} 3439 2440 5879
555 $mapped{code} = _clone_data( $item->{procedureCode} )
556 5879 ▶ if exists $item->{procedureCode};
Branch: 1 branch
BranchTrueFalseTotal
if exists $item->{"procedureCode"} 5879 0 5879
557  
558 5879 my $performed = _map_procedure_performed($item);
559 5879 ▶ $mapped{performed} = $performed if defined $performed;
Branch: 1 branch
BranchTrueFalseTotal
if defined $performed 3449 2430 5879
560  
561 5879 return \%mapped;
562 3183 };
563  
564 # Check if the input is an array reference
565 3183 ▶ if ( ref $data eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 480 2703 3183
elsif (ref $data eq 'HASH') { } 2703 0 2703
566 480 return map { { procedure => $map_item->($_) } } @$data;
567 }
568  
569 # Otherwise, assume it's a single hash reference
570 elsif ( ref $data eq 'HASH' ) {
571 2703 return $map_item->($data); # Return mapped single object
572 }
573 else {
574 0 die
575 "Invalid input type: expected an array reference or hash reference.";
576 }
577 }
578  
579 136 1;
« api/perl/main.pl ↑ Summary lib/Convert/Pheno/ClinicalCDM/Util.pm »