lib/Pheno/Ranker/IO.pm

filter: Statementstmt 99.6% 247 / 248 Branchbran 89.0% 89 / 100 Conditioncond 92.3% 12 / 13 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 37 / 37 Podpod 0.0% 0 / 18 totaltotal 92.4% 392 / 424 CC 66 SCAR 16.2
CC
66
Cov
96%
coverage_value_is_present
10.0
poi_output_filename
8.2
restructure_pxf_interpretations
8.0
CRAP 5.1
« lib/Pheno/Ranker/Compare/Encoding.pm ↑ Summary lib/Pheno/Ranker/Graph.pm »
1 package Pheno::Ranker::IO;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use File::Spec::Functions qw(catdir catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Hash::Util qw(lock_hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use YAML::XS qw(Load LoadFile DumpFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #use Data::Dumper;
17  
18 #use Sort::Naturally qw(nsort);
19 10 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 our @EXPORT =
21 qw(serialize_hashes write_alignment io_yaml_or_json read_json read_yaml write_json write_array2txt array2object validate_json write_poi poi_output_filename coverage_stats check_existence_of_include_terms append_and_rename_primary_key restructure_pxf_interpretations);
22 10 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 #########################
25 #########################
26 # SUBROUTINES FOR I/O #
27 #########################
28 #########################
29  
30 5 ▶sub serialize_hashes {
Subroutine serialize_hashes: called CC=2 SCAR=6.9
Pod undocumented
31 5 my $arg = shift;
32 5 my $data = $arg->{data};
33 5 my $export_basename = $arg->{export_basename};
34 write_json(
35 { data => $data->{$_}, filepath => qq/$export_basename.$_.json/ } )
36 5 for keys %{$data};
37 5 return 1;
38 }
39  
40 7 ▶sub write_alignment {
Subroutine write_alignment: called CC=2 SCAR=6.9
Pod undocumented
41 7 my $arg = shift;
42 7 my $basename = $arg->{align};
43 7 my $ascii = $arg->{ascii};
44 7 my $dataframe = $arg->{dataframe};
45 7 my $csv = $arg->{csv};
46 7 my %hash = (
47 '.txt' => $ascii,
48 '.csv' => $dataframe,
49 '.target.csv' => $csv
50 );
51  
52 7 for my $key ( keys %hash ) {
53 21 my $output = $basename . $key;
54 21 write_array2txt( { filepath => $output, data => $hash{$key} } );
55 }
56 7 return 1;
57 }
58  
59 34 ▶sub io_yaml_or_json {
Subroutine io_yaml_or_json: called CC=6 SCAR=17.9
Pod undocumented
60 34 my $arg = shift;
61 34 my $file = $arg->{filepath};
62 34 my $mode = $arg->{mode};
63 34 ▶ my $data = $mode eq 'write' ? $arg->{data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'write' ? : 1 33 34
64  
65 # Check if the file is gzipped
66 34 ▶ my $is_gz = $file =~ /\.gz$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$file =~ /\.gz$/ ? : 2 32 34
67  
68 # Remove .gz for extension recognition if present
69 34 ▶ my $file_for_ext = $is_gz ? ( $file =~ s/\.gz$//r ) : $file;
Branch: 1 branch
BranchTrueFalseTotal
$is_gz ? : 2 32 34
70  
71 # Allowed extensions
72 34 my @exts = qw(.yaml .yml .json);
73  
74 # Use fileparse on the file name without the .gz suffix
75 34 my ( undef, undef, $ext ) = fileparse( $file_for_ext, @exts );
76 34 my $msg = qq(Can't recognize <$file> extension. Extensions allowed are: )
77 . join ',', @exts;
78 34 ▶ die $msg unless any { $_ eq $ext } @exts;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $ext; } , @exts) 1 33 34
79  
80 # Unify extension by removing "a" and "."
81 33 $ext =~
82 tr/a.//d; # so ".yaml" or ".yml" become "yml" and ".json" becomes "json"
83  
84 # Dispatch table for read/write operations
85 33 my $return = {
86 read => { json => \&read_json, yml => \&read_yaml },
87 write => { json => \&write_json, yml => \&write_yaml },
88 };
89  
90 # Call the appropriate function based on the mode and extension
91 return $mode eq 'read'
92 ? $return->{$mode}{$ext}->($file)
93 33 ▶ : $return->{$mode}{$ext}->( { filepath => $file, data => $data } );
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'read' ? : 32 1 33
94 }
95  
96 81 ▶sub read_json {
Subroutine read_json: called CC=3 SCAR=11.0
Pod undocumented
97 81 my $file = shift;
98 81 my $str;
99 81 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) { } 3 78 81
100 3 ▶ gunzip $file => \$str
Branch: 1 branch
BranchTrueFalseTotal
gunzip($file, \$str) or die "gunzip failed for ${file}: $Pheno::Ranker::IO::GunzipError\n" 3 0 3
101 or die "gunzip failed for $file: $GunzipError\n";
102 }
103 else {
104 78 $str = path($file)->slurp;
105 }
106 81 return decode_json($str);
107 }
108  
109 50 ▶sub read_yaml {
Subroutine read_yaml: called CC=3 SCAR=11.0
Pod undocumented
110 50 my $file = shift;
111 50 my $data;
112  
113 # Check if the file ends with .gz
114 50 ▶ if ( $file =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.gz$/) { } 1 49 50
115 1 my $yaml_str;
116 1 ▶ gunzip $file => \$yaml_str
Branch: 1 branch
BranchTrueFalseTotal
gunzip($file, \$yaml_str) or die "gunzip failed for ${file}: $Pheno::Ranker::IO::GunzipError\n" 1 0 1
117 or die "gunzip failed for $file: $GunzipError\n";
118  
119 # Decode the YAML from the string
120 1 $data = Load($yaml_str);
121 }
122 else {
123 # Directly load from the file
124 49 $data = LoadFile($file);
125 }
126 50 return $data;
127 }
128  
129 41 ▶sub write_json {
Subroutine write_json: called CC=1 SCAR=0.0
Pod undocumented
130 41 my $arg = shift;
131 41 my $file = $arg->{filepath};
132 41 my $json_data = $arg->{data};
133  
134 # Note that canonical DOES not match the order of nsort from Sort::Naturally
135 41 my $json = JSON::XS->new->utf8->canonical->pretty->encode($json_data);
136 41 path($file)->spew($json);
137 41 return 1;
138 }
139  
140 6 ▶sub write_yaml {
Subroutine write_yaml: called CC=1 SCAR=0.0
Pod undocumented
141 6 my $arg = shift;
142 6 my $file = $arg->{filepath};
143 6 my $json_data = $arg->{data};
144 6 local $YAML::XS::Boolean = 'JSON::PP';
145 6 DumpFile( $file, $json_data );
146 6 return 1;
147 }
148  
149 28 ▶sub write_array2txt {
Subroutine write_array2txt: called CC=1 SCAR=0.0
Pod undocumented
150 28 my $arg = shift;
151 28 my $file = $arg->{filepath};
152 28 my $data = $arg->{data};
153  
154 # Watch out for RAM usage!!!
155 28 path($file)->spew_utf8( join( "\n", @$data ) . "\n" );
156 28 return 1;
157 }
158  
159 2 ▶sub write_poi {
Subroutine write_poi: called CC=4 SCAR=13.9
Pod undocumented
160 2 my $arg = shift;
161 2 my $ref_data = $arg->{ref_data};
162 2 my $poi = $arg->{poi};
163 2 my $poi_out_dir = $arg->{poi_out_dir};
164 2 my $primary_key = $arg->{primary_key};
165 2 my $verbose = $arg->{verbose};
166 2 for my $name (@$poi) {
167 3 my ($match) = grep { $name eq $_->{$primary_key} } @$ref_data;
168 3 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 2 1 3
169 2 my $out = catfile( $poi_out_dir, poi_output_filename($name) );
170 2 ▶ say "Writting <$out>" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 1 1 2
171 2 write_json( { filepath => $out, data => $match } );
172 }
173 else {
174 1 warn
175 "No individual found for <$name>. Are you sure you used the right prefix?\n";
176 }
177 }
178 2 return 1;
179 }
180  
181 7 ▶sub poi_output_filename {
Subroutine poi_output_filename: called CC=8 SCAR=21.1
Pod undocumented
182 7 my ( $name, $portable ) = @_;
183 7 ▶ $portable = $^O eq 'MSWin32' unless defined $portable;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $portable 3 4 7
184  
185 7 ▶ my $filename = defined $name ? "$name" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 7 0 7
186  
187 # Always encode path separators and percent to avoid directory traversal
188 # and ambiguous percent-encoded names. Encode extra characters on Windows.
189 7 ▶ my $unsafe = $portable
Branch: 1 branch
BranchTrueFalseTotal
$portable ? : 2 5 7
190 ? qr/([<>:"\/\\|?*\x00-\x1F%])/
191 : qr/([\/\\\x00-\x1F%])/;
192 7 $filename =~ s/$unsafe/sprintf '%%%02X', ord($1)/eg;
193  
194 7 ▶ if ($portable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($portable) 2 5 7
195 0 ▶ $filename =~ s/([ .])\z/sprintf '%%%02X', ord($1)/eg
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ /[ .]\z/ and do { $filename =~ s/([ .])\z/sprintf "%%%02X", ord $1;/eg } 0 2 2
196 while $filename =~ /[ .]\z/;
197 2 ▶ $filename = "_$filename"
Branch: 1 branch
BranchTrueFalseTotal
if $filename =~ /^(?:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\z|\.)/i 1 1 2
198 if $filename =~ /^(?:CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(?:\z|\.)/i;
199 }
200  
201 7 ▶ $filename = 'poi' if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 7 7
202 7 return "$filename.json";
203 }
204  
205 9 ▶sub array2object {
Subroutine array2object: called CC=3 SCAR=11.0
Pod undocumented
206 9 my $data = shift;
207 9 ▶ if ( ref $data eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq ref []) 2 7 9
208 2 my $n = @$data;
209 2 ▶ if ( $n == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) { } 1 1 2
210 1 $data = $data->[0];
211 }
212 else {
213 1 die
214 "Sorry, your file has $n patients but only 1 patient is allowed with <-t>\n";
215 }
216 }
217 8 return $data;
218 }
219  
220 22 ▶sub validate_json {
Subroutine validate_json: called CC=5 SCAR=16.1
Pod undocumented
221 22 my $file = shift;
222 22 ▶ my $data = ( $file && -f $file ) ? read_yaml($file) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$file && -f $file ? : 5 17 22
Condition66%2/3
$file and -f $file
!ll&&!rl&&r
1705
MC/DC50%1/2
$file and -f $file
$file -f $file
Truth table66%2/3
A and B
A= $fileB= -f $file
ABresult
0X0
100
111
223  
224 # Premature return with undef if the file does not exist
225 22 ▶ return undef unless defined $data; #perlcritic severity 5
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 17 5 22
226  
227 # schema for the weights file
228 5 my $schema = {
229 '$schema' => 'http://json-schema.org/draft-07/schema#',
230 'type' => 'object',
231 'patternProperties' => {
232 '^\w+([.:\w]*\w+)?$' => {
233 'type' => 'integer',
234 },
235 },
236 'additionalProperties' => JSON::XS::false,
237 };
238  
239 # Load at runtime
240 5 require JSON::Validator;
241  
242 # Create object and load schema
243 5 my $jv = JSON::Validator->new;
244  
245 # Load schema in object
246 5 $jv->schema($schema);
247  
248 # Validate data
249 5 my @errors = $jv->validate($data);
250  
251 # Show error(s) if any + die
252 5 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 2 3 5
253 2 my $msg = join "\n", @errors;
254 2 die qq/$msg\n/;
255 }
256  
257 # Lock config data (keys+values)
258 3 lock_hash(%$data);
259  
260 # return data if ok
261 3 return $data;
262  
263 }
264  
265 23 ▶sub coverage_stats {
Subroutine coverage_stats: called CC=6 SCAR=17.9
Pod undocumented
266 23 my ( $data, $format, $arg ) = @_;
267 23 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
212
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
268 23 my $coverage = {};
269 23 my $retain_excluded = $arg->{retain_excluded_phenotypicFeatures};
270  
271 23 for my $item (@$data) {
272 573 for my $key ( keys %$item ) {
273  
274 # Initialize key in coverage with 0 if not already present
275 3823 ▶ $coverage->{$key} //= 0;
Condition100%2/2
$coverage->{$key} //= 0
l!l
3661162
MC/DC100%1/1
$coverage->{$key} //= 0
$coverage->{$key}
Truth table100%2/2
A
A= $coverage->{$key}
Aresult
11
00
276  
277 3823 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (coverage_value_is_present($key, $item->{$key}, $retain_excluded)) 3384 439 3823
278 coverage_value_is_present(
279 $key, $item->{$key}, $retain_excluded
280 )
281 )
282 {
283 3384 $coverage->{$key}++;
284 }
285 }
286 }
287 return {
288 23 format => $format,
289 cohort_size => scalar @$data,
290 coverage_terms => $coverage
291 };
292 }
293  
294 3823 ▶sub coverage_value_is_present {
Subroutine coverage_value_is_present: called CC=10 SCAR=23.0
Pod undocumented
295 3823 my ( $key, $value, $retain_excluded ) = @_;
296  
297 3823 ▶ return 0 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 3822 3823
298  
299 3822 ▶ if ( ref $value eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 972 2850 3822
300 972 ▶ return keys %{$value} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$value;} ? : 539 433 972
301 }
302  
303 2850 ▶ if ( ref $value eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1893 957 2850
304 1893 ▶ return 0 unless @{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$value;} 1 1892 1893
305  
306 1892 ▶ if ( $key eq 'phenotypicFeatures' && !$retain_excluded ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "phenotypicFeatures" and not $retain_excluded) 427 1465 1892
Condition100%3/3
$key eq "phenotypicFeatures" and not $retain_excluded
!ll&&!rl&&r
14623427
MC/DC100%2/2
$key eq "phenotypicFeatures" and not $retain_excluded
$key eq "phenotypicFeatures" not $retain_excluded
Truth table100%3/3
A and not(B)
A= $key eq "phenotypicFeatures"B= not $retain_excluded
ABresult
0X0
100
111
307 934 ▶ return any {
Subroutine __ANON__: called CC=2 SCAR=7.1
308 ref $_ ne 'HASH' || !$_->{excluded}
309 427 ▶ } @{$value};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ne "HASH" or not $_->{"excluded"} 0 934 934
310 }
311  
312 1465 return 1;
313 }
314  
315 957 ▶ return 0 if $value eq 'NA' || $value eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "NA" or $value eq "NaN" 2 955 957
Condition100%3/3
$value eq "NA" or $value eq "NaN"
l!l&&r!l&&!r
11955
MC/DC100%2/2
$value eq "NA" or $value eq "NaN"
$value eq "NA" $value eq "NaN"
Truth table100%3/3
A or B
A= $value eq "NA"B= $value eq "NaN"
ABresult
1X1
011
000
316 955 return 1;
317 }
318  
319 23 ▶sub check_existence_of_include_terms {
Subroutine check_existence_of_include_terms: called CC=2 SCAR=6.9
Pod undocumented
320 23 my ( $coverage, $include_terms ) = @_;
321  
322 # Return true if include_terms is empty
323 23 ▶ return 1 unless @$include_terms;
Branch: 1 branch
BranchTrueFalseTotal
unless @$include_terms 18 5 23
324  
325 # Check for the existence of any term in include_terms within coverage
326 # Returns true if any term exists, false otherwise
327 5 ▶ return any { exists $coverage->{coverage_terms}{$_} } @$include_terms;
Subroutine __ANON__: called CC=1 SCAR=0.0
328 }
329  
330 25 ▶sub append_and_rename_primary_key {
Subroutine append_and_rename_primary_key: called CC=7 SCAR=19.5
Pod undocumented
331 25 my $arg = shift;
332 25 my $ref_data = $arg->{ref_data};
333 25 my $append_prefixes = $arg->{append_prefixes};
334 25 my $primary_key = $arg->{primary_key};
335  
336 # Premature return if @$ref_data == 1 (only 1 cohort)
337 # *** IMPORTANT ***
338 # $ref_data->[0] can be ARRAY or HASH
339 # We force HASH to be ARRAY
340 25 ▶ return ref $ref_data->[0] eq ref {} ? [ $ref_data->[0] ] : $ref_data->[0]
Branch: 2 branches
BranchTrueFalseTotal
if @$ref_data == 1 23 2 25
ref $ref_data->[0] eq ref {} ? : 1 22 23
341 if @$ref_data == 1;
342  
343 # Count for prefixes
344 2 my $prefix_count = 1;
345  
346 # We have to load into a new array data
347 # NB: for is a bit faster than map
348 2 my $data;
349 2 for my $item (@$ref_data) {
350  
351 # Get prefix
352 4 ▶ my $prefix =
Branch: 1 branch
BranchTrueFalseTotal
$$append_prefixes[$prefix_count - 1] ? : 3 1 4
353 $append_prefixes->[ $prefix_count - 1 ]
354 ? $append_prefixes->[ $prefix_count - 1 ] . '_'
355 : 'C' . $prefix_count . '_';
356  
357 # ARRAY
358 4 my $item_count = 1;
359 4 ▶ if ( ref $item eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq ref []) { } 2 2 4
360 2 for my $individual (@$item) {
361 37 my $id = $individual->{$primary_key};
362 37 check_null_primary_key(
363 {
364 count => $item_count,
365 primary_key => $primary_key,
366 id => $id,
367 prefix => $prefix
368 }
369 );
370 37 $individual->{$primary_key} = $prefix . $id;
371 37 push @$data, $individual;
372 37 $item_count++;
373 }
374 }
375  
376 # Object
377 else {
378  
379 # Check if primary_key is defined
380 2 my $id = $item->{$primary_key};
381 2 check_null_primary_key(
382 {
383 count => $item_count,
384 primary_key => $primary_key,
385 id => $id,
386 prefix => $prefix
387 }
388 );
389 2 $item->{$primary_key} = $prefix . $id;
390 2 push @$data, $item;
391 2 $item_count++;
392 }
393 4 $prefix_count++;
394 }
395 2 return $data;
396 }
397  
398 40 ▶sub check_null_primary_key {
Subroutine check_null_primary_key: called CC=2 SCAR=6.9
Pod undocumented
399 40 my $arg = shift;
400 40 my $id = $arg->{id};
401 40 my $count = $arg->{count};
402 40 my $primary_key = $arg->{primary_key};
403 40 my $prefix = $arg->{prefix};
404 40 ▶ die
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 39 40
405 "Sorry but the JSON document ${prefix}[$count] does not have the primary_key <$primary_key> defined\n"
406 unless defined $id;
407 39 return 1;
408 }
409  
410 30 ▶sub restructure_pxf_interpretations {
Subroutine restructure_pxf_interpretations: called CC=8 SCAR=20.8
Pod undocumented
411 30 my ( $data, $self ) = @_;
412  
413 # Premature return if the format is not 'PXF'
414 30 ▶ return unless $self->{format} eq 'PXF';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"format"} eq "PXF" 18 12 30
415  
416 # Premature return if "interpretations" is excluded
417 12 ▶ return if ( grep { $_ eq 'interpretations' } @{ $self->{exclude_terms} } );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "interpretations";} @{$$self{"exclude_terms"};} 1 11 12
418  
419 say "Restructuring <interpretations> in PXFs..."
420 11 ▶ if defined $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"verbose"} 0 11 11
421  
422 # Function to restructure individual interpretation
423 4 ▶ my $restructure_interpretation = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
424 4 my $interpretation = shift;
425 4 my $disease_id = $interpretation->{diagnosis}{disease}{id};
426 my $new_interpretation = {
427 progressStatus => $interpretation->{progressStatus},
428 4 genomicInterpretations => {}
429 };
430  
431 4 foreach my $genomic_interpretation (
432 4 @{ $interpretation->{diagnosis}{genomicInterpretations} } )
433 {
434 7 my $gene_id;
435 my $interpretation_data;
436  
437 7 ▶ if ( exists $genomic_interpretation->{variantInterpretation} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $genomic_interpretation->{'variantInterpretation'}) { } 4 3 7
elsif (exists $genomic_interpretation->{'geneDescriptor'}) { } 3 0 3
438 my $variant_interpretation =
439 4 $genomic_interpretation->{variantInterpretation};
440  
441 # Check if geneContext with valueId exists
442 4 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (exists $variant_interpretation->{'variationDescriptor'}{'geneContext'}{'valueId'}) { } 2 2 4
elsif (exists $variant_interpretation->{'variationDescriptor'}{'id'}) { } 2 0 2
443 exists $variant_interpretation->{variationDescriptor}
444 {geneContext}{valueId} )
445 {
446 $gene_id = $variant_interpretation->{variationDescriptor}
447 2 {geneContext}{valueId};
448 }
449  
450 # Check if id within variationDescriptor exists as an alternative
451 elsif (
452 exists $variant_interpretation->{variationDescriptor}{id} )
453 {
454 $gene_id =
455 2 $variant_interpretation->{variationDescriptor}{id};
456 }
457  
458 4 $interpretation_data = $variant_interpretation;
459 }
460 elsif ( exists $genomic_interpretation->{geneDescriptor} ) {
461 3 $gene_id = $genomic_interpretation->{geneDescriptor}{valueId};
462 $interpretation_data =
463 3 $genomic_interpretation->{geneDescriptor};
464 }
465  
466 $new_interpretation->{genomicInterpretations}{$gene_id} = {
467 interpretationStatus =>
468 $genomic_interpretation->{interpretationStatus},
469 (
470 exists $genomic_interpretation->{variantInterpretation}
471 7 ▶ ? ( variantInterpretation => $interpretation_data )
Branch: 1 branch
BranchTrueFalseTotal
exists $genomic_interpretation->{'variantInterpretation'} ? : 4 3 7
472 : ( geneDescriptor => $interpretation_data )
473 )
474 };
475 }
476  
477 4 return ( $disease_id, $new_interpretation );
478 11 };
479  
480 # Helper function to process a data structure
481 15 ▶ my $process_data = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
482 15 my $data = shift;
483 15 ▶ return unless exists $data->{interpretations};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{"interpretations"} 11 4 15
484  
485 4 my $new_data = {};
486  
487 4 foreach my $interpretation ( @{ $data->{interpretations} } ) {
488 4 my ( $disease_id, $new_interpretation ) =
489 $restructure_interpretation->($interpretation);
490 4 $new_data->{$disease_id} = $new_interpretation;
491 }
492  
493 4 $data->{interpretations} = $new_data;
494 11 };
495  
496 # Process $data if it's an array or a single object
497 11 ▶ if ( ref($data) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 5 6 11
elsif (ref $data eq 'HASH') { } 6 0 6
498 5 foreach my $entry (@$data) {
499 9 ▶ $process_data->($entry) if ref($entry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $entry eq "HASH" 9 0 9
500 }
501 }
502 elsif ( ref($data) eq 'HASH' ) {
503 6 $process_data->($data);
504 }
505  
506 11 return 1;
507 }
« lib/Pheno/Ranker/Compare/Encoding.pm ↑ Summary lib/Pheno/Ranker/Graph.pm »