lib/Pheno/Ranker.pm

filter: Statementstmt 97.2% 245 / 252 Branchbran 79.5% 70 / 88 Conditioncond 60.0% 9 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 95.5% 43 / 45 Podpod 0.0% 0 / 2 totaltotal 90.0% 369 / 410 CC 51 SCAR 13.9
CC
51
Cov
91%
BUILD
9.0
_load_precomputed_data
8.0
_load_reference_cohort_data
6.1
CRAP 4.0
« lib/Pheno/Ranker/Compare/Alignment.pm ↑ Summary utils/bff_pxf_simulator/bff-pxf-simulator »
1 package Pheno::Ranker;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Types::Standard qw(Int Enum Bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use File::ShareDir::ProjectDistDir qw(dist_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Pheno::Ranker::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Pheno::Ranker::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Pheno::Ranker::Metrics;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Pheno::Ranker::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Pheno::Ranker::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use Pheno::Ranker::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use Pheno::Ranker::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 our @EXPORT_OK = qw($VERSION write_json);
23  
24 # Personalize warn and die functions
25 0 ▶$SIG{__WARN__} = sub { warn BOLD YELLOW "Warn: ", @_, RESET };
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 87 ▶$SIG{__DIE__} = sub { die BOLD RED "Error: ", @_, RESET };
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 8 our $VERSION = '1.09';
29 8 ▶our $share_dir = $ENV{PHENO_RANKER_SHARE_DIR} // dist_dir('Pheno-Ranker');
Condition33%1/3
$ENV{"PHENO_RANKER_SHARE_DIR"} // dist_dir("Pheno-Ranker")
l!l&&r!l&&!r
080
MC/DC0%0/2
$ENV{"PHENO_RANKER_SHARE_DIR"} // dist_dir("Pheno-Ranker")
$ENV{"PHENO_RANKER_SHARE_DIR"} dist_dir("Pheno-Ranker")
Truth table33%1/3
A // B
A= $ENV{"PHENO_RANKER_SHARE_DIR"}B= dist_dir("Pheno-Ranker")
ABresult
1X1
011
000
30  
31 # Set development mode
32 8 ▶use constant DEVEL_MODE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 8 my $default_config_file = catfile( $share_dir, 'conf', 'config.yaml' );
35  
36 ############################################
37 # Start declaring attributes for the class #
38 ############################################
39  
40 has 'config_file' => (
41 is => 'ro',
42 isa =>
43 40 ▶ sub { die "Config file '$_[0]' is not a valid file" unless -e $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_[0] 0 40 40
44 8 default => $default_config_file,
45 );
46  
47 has config => (
48 is => 'ro',
49 lazy => 1,
50 40 ▶ default => sub { Pheno::Ranker::Config->new( file => shift->config_file ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 8 );
52  
53 has options => (
54 is => 'ro',
55 lazy => 1,
56 37 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 37 my $self = shift;
58 37 return Pheno::Ranker::Options->new(
59 config => $self->config,
60 share_dir => $share_dir,
61 );
62 },
63 8 );
64  
65 8 has sort_by => (
66 is => 'ro',
67 isa => Enum [qw(hamming jaccard)]
68 );
69  
70 8 has similarity_metric_cohort => (
71 is => 'ro',
72 isa => Enum [qw(hamming jaccard)]
73 );
74  
75 8 has matrix_format => (
76 is => 'ro',
77 isa => Enum [qw(dense mtx)]
78 );
79  
80 8 has max_out => (
81 is => 'ro',
82 isa => Int
83 );
84  
85 8 has max_number_vars => (
86 is => 'ro',
87 isa => Int
88 );
89  
90 8 has max_matrix_records_in_ram => (
91 is => 'ro',
92 isa => Int
93 );
94  
95 8 has graph_min_weight => (
96 is => 'ro',
97 );
98  
99 8 has graph_max_weight => (
100 is => 'ro',
101 );
102  
103 has hpo_file => (
104 is => 'ro',
105 0 ▶ isa => sub { die "Error <$_[0]> is not a valid file" unless -e $_[0] },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless -e $_[0] 0 0 0
106 8 );
107  
108 has poi_out_dir => (
109 is => 'ro',
110 1 ▶ isa => sub { die "<$_[0]> dir does not exist" unless -d $_[0] },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless -d $_[0] 0 1 1
111 8 );
112  
113 has [qw/include_terms exclude_terms/] => (
114 is => 'ro',
115 67 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
116 67 ▶ die "<--include_terms> and <--exclude_terms> must be an array ref\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref shift() eq "ARRAY" 0 67 67
117 unless ref shift eq 'ARRAY';
118 },
119 8 );
120  
121 8 has cli => (
122 is => 'ro',
123 isa => Bool,
124 );
125  
126 # Miscellaneous attributes
127 8 has [
128 qw/target_file weights_file out_file include_hpo_ascendants
129 retain_excluded_phenotypicFeatures align align_basename export export_basename
130 log verbose age cytoscape_json graph_stats/
131 ] => ( is => 'ro' );
132  
133 8 has [qw/append_prefixes reference_files patients_of_interest/] => ( is => 'ro' );
134  
135 8 has [qw/glob_hash_file ref_hash_file ref_binary_hash_file coverage_stats_file labels_file/]
136 => ( is => 'ro' );
137  
138 ##########################################
139 # End declaring attributes for the class #
140 ##########################################
141  
142 40 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 40 my ( $orig, $class, @args ) = @_;
144 40 my $args = $class->$orig(@args);
145 40 return Pheno::Ranker::Options->defined_constructor_args($args);
146 8 };
147  
148 40 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.0
Pod undocumented
149  
150 # BUILD: is an instance method that is called after the object has been constructed but before it is returned to the caller.
151 # BUILDARGS is a class method that is responsible for processing the arguments passed to the constructor (new) and returning a hash reference of attributes that will be used to initialize the object.
152  
153 40 my $self = shift;
154 40 ▶ $self->config->apply_to($self) unless exists $self->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"primary_key"} 40 0 40
155 37 $self->options->apply_to($self);
156  
157 # Miscellaneous checks
158 37 ▶ if ( @{ $self->{append_prefixes} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"append_prefixes"};}) 3 34 37
159 die "<--append_prefixes> requires at least 2 cohort files!\n"
160 3 ▶ unless @{ $self->{reference_files} } > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"reference_files"};} > 1 1 2 3
161 die "The number of items in <--r> and <--append-prefixes> must match!\n"
162 2 ▶ unless @{ $self->{reference_files} } == @{ $self->{append_prefixes} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"reference_files"};} == @{$self->{"append_prefixes"};} 1 1 2
163 }
164 35 ▶ if ( @{ $self->{patients_of_interest} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"patients_of_interest"};}) 2 33 35
165 die "<--patients-of-interest> must be used with <--r>\n"
166 2 ▶ unless @{ $self->{reference_files} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"reference_files"};} 1 1 2
167 }
168 34 ▶ if ( $self->{matrix_format} eq 'mtx' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"matrix_format"} eq "mtx") 1 33 34
169 die "<--matrix-format mtx> only works in cohort mode\n"
170 1 ▶ if $self->{target_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"target_file"} 0 1 1
171 }
172 }
173  
174 # ============================================================
175 # run method
176 # ============================================================
177 33 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod undocumented
178 33 my $self = shift;
179  
180 # Labels belong to this run, including when reusing the module in one process.
181 33 local %Pheno::Ranker::Compare::nomenclature;
182  
183 33 $self->_validate_output_directories;
184  
185 my (
186 31 $glob_hash, $ref_hash, $ref_binary_hash, $hash2serialize, $weight
187 );
188  
189 31 ▶ if ( $self->_has_precomputed_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_precomputed_data) { } 8 23 31
190 8 ( $glob_hash, $ref_hash, $ref_binary_hash, $hash2serialize ) =
191 $self->_load_precomputed_data;
192 }
193 else {
194 my $ref_data = $self->_load_reference_cohort_data(
195 $self->{reference_files},
196 $self->{primary_key},
197 $self->{append_prefixes}
198 23 );
199  
200 23 ▶ return 1 if $self->_maybe_write_poi($ref_data);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_maybe_write_poi($ref_data) 1 22 23
201  
202 22 $weight = validate_json( $self->{weights_file} );
203 20 $self->_maybe_load_hpo;
204  
205 my ( $coverage_stats, $glob_hash_computed, $ref_hash_computed,
206 $ref_binary_hash_computed, $hash2serialize_computed )
207 = $self->_compute_cohort_metrics(
208 $ref_data,
209 $weight,
210 $self->{primary_key},
211 $self->{target_file}
212 20 );
213  
214 19 $glob_hash = $glob_hash_computed;
215 19 $ref_hash = $ref_hash_computed;
216 19 $ref_binary_hash = $ref_binary_hash_computed;
217 19 $hash2serialize = $hash2serialize_computed;
218 }
219  
220 24 $self->_maybe_run_cohort_comparison($ref_binary_hash);
221 24 $self->_maybe_write_graph($ref_binary_hash);
222 24 $self->_maybe_process_patient(
223 {
224 weight => $weight,
225 glob_hash => $glob_hash,
226 ref_hash => $ref_hash,
227 ref_binary_hash => $ref_binary_hash,
228 },
229 \$hash2serialize
230 );
231 24 $self->_maybe_export_hashes($hash2serialize);
232  
233 24 return 1;
234 }
235  
236 33 ▶sub _validate_output_directories {
Subroutine _validate_output_directories: called CC=5 SCAR=16.1
237 33 my $self = shift;
238  
239 33 ▶ my $align_dir = defined $self->{align} ? dirname( $self->{align} ) : '.';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'align'} ? : 21 12 33
240 33 ▶ die "Directory <$align_dir> does not exist (used with --align)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $align_dir 1 32 33
241 unless -d $align_dir;
242  
243 32 ▶ my $export_dir = defined $self->{export} ? dirname( $self->{export} ) : '.';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'export'} ? : 5 27 32
244 32 ▶ die "Directory <$export_dir> does not exist (used with --export)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -d $export_dir 1 31 32
245 unless -d $export_dir;
246  
247 31 return 1;
248 }
249  
250 31 ▶sub _has_precomputed_data {
Subroutine _has_precomputed_data: called CC=4 SCAR=14.6
251 31 my $self = shift;
252 return defined $self->{glob_hash_file}
253 && defined $self->{ref_hash_file}
254 && defined $self->{ref_binary_hash_file}
255 31 ▶ && defined $self->{coverage_stats_file};
Condition66%6/9
defined $self->{'glob_hash_file'} && defined $self->{'ref_hash_file'} && defined $self->{'ref_binary_hash_file'} && defined $self->{'coverage_stats_file'}
!ll&&!rl&&r
2308
defined $self->{"glob_hash_file"} and defined $self->{"ref_hash_file"} and defined $self->{"ref_binary_hash_file"}
!ll&&!rl&&r
2308
defined $self->{"glob_hash_file"} and defined $self->{"ref_hash_file"}
!ll&&!rl&&r
2308
MC/DC25%1/4
defined $self->{'glob_hash_file'} && defined $self->{'ref_hash_file'} && defined $self->{'ref_binary_hash_file'} && defined $self->{'coverage_stats_file'}
defined $self->{"glob_hash_file"} defined $self->{"ref_hash_file"} defined $self->{"ref_binary_hash_file"} defined $self->{'coverage_stats_file'}
Truth table40%2/5
A and B and C && D
A= defined $self->{"glob_hash_file"}B= defined $self->{"ref_hash_file"}C= defined $self->{"ref_binary_hash_file"}D= defined $self->{'coverage_stats_file'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
256 }
257  
258 8 ▶sub _load_precomputed_data {
Subroutine _load_precomputed_data: called CC=8 SCAR=20.8
259 8 my $self = shift;
260  
261 8 ▶ say "Using precomputed data" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 8 8
262  
263 8 my $glob_hash = read_json( $self->{glob_hash_file} );
264 8 my $ref_hash = read_json( $self->{ref_hash_file} );
265 8 my $ref_binary_hash = read_json( $self->{ref_binary_hash_file} );
266 8 my $coverage_stats = read_json( $self->{coverage_stats_file} );
267  
268 8 ▶ if (defined $self->{labels_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"labels_file"}) 6 2 8
269 6 my $labels = read_json($self->{labels_file});
270 6 ▶ die "Precomputed labels must be a JSON object\n" unless ref($labels) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $labels eq "HASH" 1 5 6
271 5 for my $key (keys %$labels) {
272 die "Precomputed label key is absent from the global vector: $key\n"
273 8 ▶ unless exists $glob_hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $glob_hash->{$key} 1 7 8
274 die "Precomputed labels must contain defined scalar values\n"
275 7 ▶ if !defined($labels->{$key}) || ref($labels->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $labels->{$key} or ref $labels->{$key} 1 6 7
Condition66%2/3
not defined $labels->{$key} or ref $labels->{$key}
l!l&&r!l&&!r
016
MC/DC50%1/2
not defined $labels->{$key} or ref $labels->{$key}
not defined $labels->{$key} ref $labels->{$key}
Truth table66%2/3
not(A) or B
A= not defined $labels->{$key}B= ref $labels->{$key}
ABresult
1X1
011
000
276 }
277 3 %Pheno::Ranker::Compare::nomenclature = %$labels;
278 }
279  
280 5 $self->_add_attribute( 'format', $coverage_stats->{format} );
281  
282 5 my $hash2serialize = {
283 glob_hash => $glob_hash,
284 ref_hash => $ref_hash,
285 ref_binary_hash => $ref_binary_hash,
286 coverage_stats => $coverage_stats,
287 };
288  
289 5 return ( $glob_hash, $ref_hash, $ref_binary_hash, $hash2serialize );
290 }
291  
292 23 ▶sub _maybe_write_poi {
Subroutine _maybe_write_poi: called CC=2 SCAR=6.9
293 23 my ( $self, $ref_data ) = @_;
294 23 ▶ return 0 unless @{ $self->{patients_of_interest} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"patients_of_interest"};} 22 1 23
295  
296 write_poi(
297 {
298 ref_data => $ref_data,
299 poi => $self->{patients_of_interest},
300 poi_out_dir => $self->{poi_out_dir},
301 primary_key => $self->{primary_key},
302 verbose => $self->{verbose}
303 }
304 1 );
305  
306 1 return 1;
307 }
308  
309 20 ▶sub _maybe_load_hpo {
Subroutine _maybe_load_hpo: called CC=2 SCAR=10.9
310 20 my $self = shift;
311 20 ▶ return 1 unless $self->{include_hpo_ascendants};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"include_hpo_ascendants"} 20 0 20
312  
313 0 my ( $nodes, $edges ) = parse_hpo_json( read_json( $self->{hpo_file} ) );
314 0 $self->{nodes} = $nodes;
315 0 $self->{edges} = $edges;
316  
317 0 return 1;
318 }
319  
320 24 ▶sub _maybe_run_cohort_comparison {
Subroutine _maybe_run_cohort_comparison: called CC=2 SCAR=6.9
321 24 my ( $self, $ref_binary_hash ) = @_;
322 24 ▶ return 1 if $self->{target_file};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"target_file"} 7 17 24
323  
324 17 cohort_comparison( $ref_binary_hash, $self->_context );
325 17 return 1;
326 }
327  
328 24 ▶sub _maybe_write_graph {
Subroutine _maybe_write_graph: called CC=1 SCAR=0.0
329 24 my ( $self, $ref_binary_hash ) = @_;
330  
331 $self->_perform_graph_calculations(
332 $ref_binary_hash,
333 $self->{cytoscape_json},
334 $self->{graph_stats},
335 $self->{similarity_metric_cohort},
336 $self->{graph_min_weight},
337 $self->{graph_max_weight}
338 24 );
339  
340 24 return 1;
341 }
342  
343 24 ▶sub _maybe_process_patient {
Subroutine _maybe_process_patient: called CC=2 SCAR=6.9
344 24 my ( $self, $computed, $hash2serialize_ref ) = @_;
345 24 ▶ return 1 unless $self->{target_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"target_file"} 17 7 24
346  
347 $self->_process_patient_data(
348 {
349 target_file => $self->{target_file},
350 primary_key => $self->{primary_key},
351 weight => $computed->{weight},
352 glob_hash => $computed->{glob_hash},
353 ref_hash => $computed->{ref_hash},
354 ref_binary_hash => $computed->{ref_binary_hash},
355 align => $self->{align},
356 align_basename => $self->{align_basename},
357 out_file => $self->{out_file},
358 cli => $self->{cli},
359 verbose => $self->{verbose},
360 },
361 7 $hash2serialize_ref
362 );
363  
364 7 return 1;
365 }
366  
367 24 ▶sub _maybe_export_hashes {
Subroutine _maybe_export_hashes: called CC=4 SCAR=13.9
368 24 my ( $self, $hash2serialize ) = @_;
369 24 ▶ return 1 unless defined $self->{export};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"export"} 20 4 24
370  
371 # Use the same feature keys and fallback as alignment; never alter vector data.
372 $hash2serialize->{labels} = {
373 map { $_ => (exists $Pheno::Ranker::Compare::nomenclature{$_}
374 168 ▶ ? $Pheno::Ranker::Compare::nomenclature{$_}
Branch: 1 branch
BranchTrueFalseTotal
exists $Pheno::Ranker::Compare::nomenclature{$_} ? : 127 41 168
375 : Pheno::Ranker::Compare::Remap::guess_label($_)) }
376 4 keys %{$hash2serialize->{glob_hash}}
377 };
378  
379 serialize_hashes(
380 {
381 data => $hash2serialize,
382 export_basename =>
383 $self->{export} ? $self->{export} : $self->{export_basename}
384 }
385 4 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'export'} ? : 4 0 4
386  
387 4 return 1;
388 }
389  
390 # ============================================================
391 # Private method: _load_reference_cohort_data
392 # ------------------------------------------------------------
393 # Loads each reference cohort file, validates the primary_key,
394 # and then appends prefixes if needed.
395 # ============================================================
396 23 ▶sub _load_reference_cohort_data {
Subroutine _load_reference_cohort_data: called CC=6 SCAR=18.1
397 23 my ( $self, $reference_files, $primary_key, $append_prefixes ) = @_;
398  
399 # *** IMPORTANT ***
400 # $ref_data is an array array where each element is the content of the file (e.g, [] or {})
401  
402 23 my $ref_data = [];
403 23 for my $cohort_file ( @{$reference_files} ) {
404 24 ▶ die "<$cohort_file> does not exist\n" unless -f $cohort_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $cohort_file 0 24 24
405 24 my $json_data = io_yaml_or_json(
406 {
407 filepath => $cohort_file,
408 mode => 'read'
409 }
410 );
411  
412 # Check for existence of primary_key otherwise die
413 # Expected cases:
414 # - A) BFF/PXF (default config) exists primary_key('id')
415 # - B) JSON (default config) exists primary_key('id') - i.e., OpenEHR
416 # - C) JSON (external config) exists primary_key
417  
418 24 my $msg =
419 "Sorry, <$cohort_file> does not contain primary_key <$primary_key>. Are you using the right configuration file?\n";
420 24 ▶ if ( ref $json_data eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json_data eq ref []) { } 23 1 24
421 23 ▶ die $msg unless exists $json_data->[0]->{$primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json_data->[0]{$primary_key} 0 23 23
422 }
423 else {
424 1 ▶ die $msg unless exists $json_data->{$primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $json_data->{$primary_key} 0 1 1
425 }
426 24 push @$ref_data, $json_data;
427 }
428  
429 # In <inter-cohort> we join --cohorts into one but we rename the values of primary_key
430 # NB: Re-using $ref_data to save memory
431  
432 23 $ref_data = append_and_rename_primary_key(
433 {
434 ref_data => $ref_data,
435 append_prefixes => $append_prefixes,
436 primary_key => $primary_key
437 }
438 );
439 23 return $ref_data;
440 }
441  
442 # ============================================================
443 # Private method: _compute_cohort_metrics
444 # ------------------------------------------------------------
445 # Computes cohort coverage statistics, restructures the data
446 # (e.g. PXF interpretations), and then creates the global and
447 # per-individual hashes along with their one-hot encoded version.
448 # ============================================================
449 20 ▶sub _compute_cohort_metrics {
Subroutine _compute_cohort_metrics: called CC=4 SCAR=13.9
450 20 my ( $self, $ref_data, $weight, $primary_key, $target_file ) = @_;
451 20 my $export = $self->{export};
452  
453 # We have to check if we have BFF|PXF or others (unless defined at config)
454 $self->_add_attribute( 'format', check_format($ref_data) )
455 20 ▶ unless defined $self->{format};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"format"} 17 3 20
456  
457 20 my $context = $self->_context;
458 my $coverage_stats = coverage_stats(
459 $ref_data,
460 $context->{format},
461 {
462 retain_excluded_phenotypicFeatures =>
463 $context->{retain_excluded_phenotypicFeatures}
464 }
465 20 );
466 die
467 1 "--include-terms <@{$self->{include_terms}}> does not exist in the cohort(s)\n"
468 unless check_existence_of_include_terms( $coverage_stats,
469 20 ▶ $context->{include_terms} );
Branch: 1 branch
BranchTrueFalseTotal
unless check_existence_of_include_terms($coverage_stats, $context->{"include_terms"}) 1 19 20
470  
471 # Restructure PXF
472 19 restructure_pxf_interpretations( $ref_data, $context );
473  
474 # First we create:
475 # - $glob_hash => hash with all the COHORT keys possible
476 # - $ref_hash => BIG hash with all individiduals' keys "flattened"
477  
478 19 my ( $glob_hash, $ref_hash ) =
479 create_glob_and_ref_hashes( $ref_data, $weight, $context );
480  
481 # Limit the number of variables if > $self-{max_number_vars}
482 # *** IMPORTANT ***
483 # Change only performed in $glob_hash
484 19 ▶ if ( keys %$glob_hash > $context->{max_number_vars} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$glob_hash > $context->{"max_number_vars"}) 0 19 19
485 0 $glob_hash = randomize_variables( $glob_hash, $context );
486 }
487  
488 # Second we peform one-hot encoding for each individual
489 19 my $ref_binary_hash =
490 create_binary_digit_string( $export, $weight, $glob_hash, $ref_hash );
491  
492 # Hashes to be serialized to JSON if <--export>
493 19 my $hash2serialize = {
494 glob_hash => $glob_hash,
495 ref_hash => $ref_hash,
496 ref_binary_hash => $ref_binary_hash,
497 coverage_stats => $coverage_stats
498 };
499 return (
500 19 $coverage_stats, $glob_hash, $ref_hash,
501 $ref_binary_hash, $hash2serialize
502 );
503 }
504  
505 # ============================================================
506 # Private method: _process_patient_data
507 # ------------------------------------------------------------
508 # Loads patient data from the target file, validates it,
509 # restructures interpretations, and then performs the patient
510 # to cohort comparison and ranking.
511 # ============================================================
512 7 ▶sub _process_patient_data {
Subroutine _process_patient_data: called CC=6 SCAR=18.0
513 7 my ( $self, $params, $hash2serialize_ref ) = @_;
514 7 my $target_file = $params->{target_file};
515 7 my $primary_key = $params->{primary_key};
516 7 my $weight = $params->{weight};
517 7 my $glob_hash = $params->{glob_hash};
518 7 my $ref_hash = $params->{ref_hash};
519 7 my $ref_binary_hash = $params->{ref_binary_hash};
520 7 my $align = $params->{align};
521 7 my $align_basename = $params->{align_basename};
522 7 my $out_file = $params->{out_file};
523 7 my $cli = $params->{cli};
524 7 my $verbose = $params->{verbose};
525 7 my $export = $self->{export};
526 7 my $context = $self->_context;
527  
528 7 my $tar_data = array2object(
529 io_yaml_or_json( { filepath => $target_file, mode => 'read' } ) );
530  
531 # The target file has to have $_->{$primary_key} otherwise die
532  
533 die
534 "Sorry, <$target_file> does not contain primary_key <$primary_key>. Are you using the right config file?\n"
535 7 ▶ unless exists $tar_data->{$primary_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tar_data->{$primary_key} 0 7 7
536 7 restructure_pxf_interpretations( $tar_data, $context );
537  
538 # We store {primary_key} as a variable as it might be deleted from $tar_data (--exclude-terms id)
539  
540 7 my $tar_data_id = $tar_data->{$primary_key};
541 7 my $tar_hash = {
542 $tar_data_id => remap_hash(
543 {
544 hash => $tar_data,
545 weight => $weight,
546 self => $context
547 }
548 )
549 };
550  
551 # *** IMPORTANT ***
552 # The target binary is created from matches to $glob_hash
553 # Thus, it does not include variables ONLY present in TARGET
554  
555 7 my $tar_binary_hash =
556 create_binary_digit_string( $export, $weight, $glob_hash, $tar_hash );
557 my (
558 7 $results_rank, $results_align, $alignment_ascii,
559 $alignment_dataframe, $alignment_csv
560 )
561 = compare_and_rank(
562 {
563 glob_hash => $glob_hash,
564 ref_hash => $ref_hash,
565 tar_hash => $tar_hash,
566 ref_binary_hash => $ref_binary_hash,
567 tar_binary_hash => $tar_binary_hash,
568 weight => $weight,
569 self => $context
570 }
571 );
572 7 ▶ say join "\n", @$results_rank if $cli;
Branch: 1 branch
BranchTrueFalseTotal
if $cli 0 7 7
573 7 write_array2txt( { filepath => $out_file, data => $results_rank } );
574  
575 7 ▶ if ( defined $align ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align) 7 0 7
576 7 ▶ write_alignment(
Branch: 1 branch
BranchTrueFalseTotal
$align ? : 5 2 7
577 {
578 align => $align ? $align : $align_basename,
579 ascii => $alignment_ascii,
580 dataframe => $alignment_dataframe,
581 csv => $alignment_csv
582 }
583 );
584 }
585 7 $$hash2serialize_ref->{tar_hash} = $tar_hash;
586 7 $$hash2serialize_ref->{tar_binary_hash} = $tar_binary_hash;
587 7 ▶ $$hash2serialize_ref->{alignment_hash} = $results_align if defined $align;
Branch: 1 branch
BranchTrueFalseTotal
if defined $align 7 0 7
588 7 return 1;
589 }
590  
591 24 ▶sub _perform_graph_calculations {
Subroutine _perform_graph_calculations: called CC=3 SCAR=11.0
592 24 my ( $self, $ref_binary_hash, $cytoscape_json, $graph_stats,
593 $similarity_metric_cohort, $graph_min_weight, $graph_max_weight )
594 = @_;
595  
596 24 my $graph;
597 24 ▶ if ($cytoscape_json) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cytoscape_json) 2 22 24
598 $graph = binary_hash2graph(
599 {
600 ref_binary_hash => $ref_binary_hash,
601 json => $cytoscape_json,
602 metric => $similarity_metric_cohort,
603 graph_stats => 1,
604 graph_min_weight => $graph_min_weight,
605 graph_max_weight => $graph_max_weight,
606 verbose => $self->{verbose},
607 }
608 2 );
609 }
610  
611 24 ▶ if ( defined $graph_stats ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $graph_stats) 1 23 24
612 cytoscape2graph(
613 {
614 graph => $graph,
615 output => $graph_stats,
616 metric => $similarity_metric_cohort,
617 verbose => $self->{verbose},
618 }
619 1 );
620 }
621  
622 24 return $graph;
623 }
624  
625 22 ▶sub _add_attribute {
Subroutine _add_attribute: called CC=1 SCAR=0.0
626 22 my ( $self, $name, $value ) = @_;
627 22 $self->{$name} = $value;
628 22 return 1;
629 }
630  
631 44 ▶sub _context {
Subroutine _context: called CC=1 SCAR=0.0
632 44 my $self = shift;
633 44 return Pheno::Ranker::Context->from_ranker($self);
634 }
635  
636 8 1;
637  
638 =pod
639  
640 =head1 NAME
641  
642 Pheno::Ranker - A module that performs semantic similarity in PXF/BFF data structures and beyond (JSON|YAML)
643
644 =head1 SYNOPSIS
645  
646 use Pheno::Ranker;
647  
648 # Create object
649 my $ranker = Pheno::Ranker->new(
650 {
651 reference_files => ['individuals.json'],
652 out_file => 'matrix.txt'
653 }
654 );
655  
656 # Run it (output are text files)
657 $ranker->run;
658  
659 =head1 DESCRIPTION
660  
661 We recommend using the included L<command-line interface|https://metacpan.org/dist/Pheno-Ranker/view/bin/pheno-ranker>.
662  
663 For a better description, please read the following documentation:
664  
665 =over
666  
667 =item General:
668  
669 L<https://cnag-biomedical-informatics.github.io/pheno-ranker>
670  
671 =item Command-Line Interface:
672  
673 L<https://github.com/CNAG-Biomedical-Informatics/pheno-ranker#readme>
674  
675 =back
676  
677 =head1 CITATION
678  
679 The author requests that any published work that utilizes `Pheno-Ranker` includes a cite to the following reference:
680  
681 Leist, I.C. et al., (2024). Pheno-Ranker: a toolkit for comparison of phenotypic data stored in GA4GH standards and beyond. _BMC Bioinformatics_. DOI: 10.1186/s12859-024-05993-2
682  
683 =head1 AUTHOR
684  
685 Written by Manuel Rueda, PhD. Info about CNAG can be found at L<https://www.cnag.eu>.
686  
687 =head1 METHODS
688  
689 There is only method named C<run>. See above the syntax.
690  
691 For more information check the documentation:
692  
693 L<https://cnag-biomedical-informatics.github.io/pheno-ranker>
694  
695 =head1 COPYRIGHT
696  
697 This PERL file is copyrighted. See the LICENSE file included in this distribution.
698  
699 =cut
« lib/Pheno/Ranker/Compare/Alignment.pm ↑ Summary utils/bff_pxf_simulator/bff-pxf-simulator »