lib/Pheno/Ranker/CLI.pm

filter: Statementstmt 80.8% 97 / 120 Branchbran 57.1% 32 / 56 Conditioncond 34.0% 16 / 47 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 85.0% 17 / 20 Podpod 0.0% 0 / 8 totaltotal 59.0% 163 / 276 CC 45 SCAR 30.0
CC
45
Cov
65%
logical_cpu_count
42.0
parse_args
33.6
write_log
30.0
CRAP 20.2
↑ Summary lib/Pheno/Ranker/Compare/Rank.pm »
1 package Pheno::Ranker::CLI;
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 8 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Pheno::Ranker::Version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 my $VERSION = $Pheno::Ranker::Version::VERSION;
17  
18 8 Getopt::Long::Configure('no_ignore_case');
19  
20 5 ▶sub run {
Subroutine run: called CC=5 SCAR=17.3
Pod undocumented
21 5 my ( $class, @argv ) = @_;
22 5 ▶ my $self = ref $class ? $class : $class->new( pod_file => $0 );
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 5 0 5
23  
24 5 my $data = $self->parse_args(@argv);
25  
26 2 ▶ say BOLD CYAN program_header($VERSION), RESET if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 2 2
27  
28 2 require Pheno::Ranker;
29 2 my $ranker = Pheno::Ranker->new($data);
30 2 $ranker->run;
31  
32 write_log( $data->{log} ? $data->{log} : 'pheno-ranker-log.json', $data, $VERSION )
33 2 ▶ if defined $data->{log};
Branch: 2 branches
BranchTrueFalseTotal
if defined $data->{"log"} 0 2 2
$data->{'log'} ? : 0 0 0
34  
35 2 return 0;
36 }
37  
38 7 ▶sub new {
Subroutine new: called CC=2 SCAR=8.1
Pod undocumented
39 7 my ( $class, %arg ) = @_;
40 7 ▶ return bless { pod_file => $arg{pod_file} || $0 }, $class;
Condition33%1/3
$arg{'pod_file'} || $0
l!l&&r!l&&!r
700
MC/DC0%0/2
$arg{'pod_file'} || $0
$arg{'pod_file'} $0
Truth table33%1/3
A || B
A= $arg{'pod_file'}B= $0
ABresult
1X1
011
000
41 }
42  
43 16 ▶sub parse_args {
Subroutine parse_args: called CC=24 SCAR=35.1
Pod undocumented
44 16 my ( $self, @argv ) = @_;
45 16 ▶ $self = __PACKAGE__->new unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 16 16
46  
47 16 my $out_file_cohort = 'matrix.txt';
48 16 my $out_file_patient = 'rank.txt';
49 16 my $out_file_graph = 'graph.json';
50 16 my $out_file_graph_stats = 'graph_stats.txt';
51 16 my $export_basename = 'export';
52 16 my $align_basename = 'alignment';
53 16 my $color = 1;
54 16 my $age = 0;
55 16 my $cli = 1;
56  
57 GetOptionsFromArray(
58 \@argv,
59 'reference|r=s{1,}' => \my @reference_files,
60 'target|t=s' => \my $target_file,
61 'weights|w=s' => \my $weights_file,
62 'append-prefixes=s{1,}' => \my @append_prefixes,
63 'out-file|o=s' => \my $out_file_arg,
64 'max-out:i' => \my $max_out,
65 'max-number-vars:i' => \my $max_number_vars,
66 'include-hpo-ascendants' => \my $include_hpo_ascendants,
67 'export|e:s' => \my $export,
68 'align|a:s' => \my $align,
69 'cytoscape-json:s' => \my $cytoscape_json,
70 'graph-stats:s' => \my $graph_stats,
71 'graph-min-weight=f' => \my $graph_min_weight,
72 'graph-max-weight=f' => \my $graph_max_weight,
73 'sort-by=s' => \my $sort_by,
74 'similarity-metric-cohort=s' => \my $similarity_metric_cohort,
75 'matrix-format=s' => \my $matrix_format,
76 'patients-of-interest|poi=s{1,}' => \my @patients_of_interest,
77 'poi-out-dir=s' => \my $poi_out_dir,
78 'include-terms=s{1,11}' => \my @include_terms,
79 'exclude-terms=s{1,11}' => \my @exclude_terms,
80 'retain-excluded-phenotypicFeatures' => \my $retain_excluded_phenotypicFeatures,
81 'prp|precomputed-ref-prefix=s' => \my $precomputed_ref_prefix,
82 'max-matrix-records-in-ram=i' => \my $max_matrix_records_in_ram,
83 'config=s' => \my $config_file,
84 'age!' => \$age,
85 'help|?' => \my $help,
86 'log:s' => \my $log,
87 'man' => \my $man,
88 'debug=i' => \my $debug,
89 'verbose|v' => \my $verbose,
90 'color!' => \$color,
91 1 ▶ 'version|V' => sub { say "$0 Version $VERSION"; exit; }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 16 ▶ ) or $self->_pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray(\@argv, "reference|r=s{1,}", \my @reference_files, "target|t=s", \my $target_file, "weights|w=s", \my $weights_file, "append-prefixes=s{1,}", \my @append_prefixes, "out-file|o=s", \my $out_file_arg, "max-out:i", \my $max_out, "max-number-vars:i", \my $max_number_vars, "include-hpo-ascendants", \my $include_hpo_ascendants, "export|e:s", \my $export, "align|a:s", \my $align, "cytoscape-json:s", \my $cytoscape_json, "graph-stats:s", \my $graph_stats, "graph-min-weight=f", \my $graph_min_weight, "graph-max-weight=f", \my $graph_max_weight, "sort-by=s", \my $sort_by, "similarity-metric-cohort=s", \my $similarity_metric_cohort, "matrix-format=s", \my $matrix_format, "patients-of-interest|poi=s{1,}", \my @patients_of_interest, "poi-out-dir=s", \my $poi_out_dir, "include-terms=s{1,11}", \my @include_terms, "exclude-terms=s{1,11}", \my @exclude_terms, "retain-excluded-phenotypicFeatures", \my $retain_excluded_phenotypicFeatures, "prp|precomputed-ref-prefix=s", \my $precomputed_ref_prefix, "max-matrix-records-in-ram=i", \my $max_matrix_records_in_ram, "config=s", \my $config_file, "age!", \$age, "help|?", \my $help, "log:s", \my $log, "man", \my $man, "debug=i", \my $debug, "verbose|v", \my $verbose, "color!", \$color, "version|V", sub { say "$0 Version $VERSION"; exit; } ) or $self->_pod2usage(2) 15 0 15
93  
94 15 ▶ $self->_pod2usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 14 15
95 14 ▶ if ($man) {
Branch: 1 branch
BranchTrueFalseTotal
if ($man) 1 13 14
96 1 say "--man is deprecated. Please use --help or see the full documentation:";
97 1 say "https://cnag-biomedical-informatics.github.io/pheno-ranker/usage/";
98 1 exit 0;
99 }
100  
101 $self->_pod2usage(
102 13 ▶ -message => "Please specify a reference-cohort(s) with <--r>\n",
Branch: 1 branch
BranchTrueFalseTotal
unless @reference_files or $precomputed_ref_prefix 0 13 13
Condition66%2/3
@reference_files or $precomputed_ref_prefix
l!l&&r!l&&!r
580
MC/DC0%0/2
@reference_files or $precomputed_ref_prefix
@reference_files $precomputed_ref_prefix
Truth table66%2/3
A or B
A= @reference_filesB= $precomputed_ref_prefix
ABresult
1X1
011
000
103 -exitval => 1
104 ) unless ( @reference_files || $precomputed_ref_prefix );
105  
106 13 ▶ $self->_pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
if defined $graph_stats and not defined $cytoscape_json 0 13 13
Condition66%2/3
defined $graph_stats and not defined $cytoscape_json
!ll&&!rl&&r
1210
MC/DC0%0/2
defined $graph_stats and not defined $cytoscape_json
defined $graph_stats not defined $cytoscape_json
Truth table66%2/3
A and not(B)
A= defined $graph_statsB= not defined $cytoscape_json
ABresult
0X0
100
111
107 -message => "<--graph-stats> only works in conjunction with <--cytoscape-json>\n",
108 -exitval => 1
109 ) if ( defined $graph_stats && !defined $cytoscape_json );
110  
111 13 ▶ $self->_pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
if defined $weights_file and not -f $weights_file 0 13 13
Condition33%1/3
defined $weights_file and not -f $weights_file
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $weights_file and not -f $weights_file
defined $weights_file not -f $weights_file
Truth table33%1/3
A and not(B)
A= defined $weights_fileB= not -f $weights_file
ABresult
0X0
100
111
112 -message => "Weights file <$weights_file> does not exist\n",
113 -exitval => 1
114 ) if ( defined $weights_file && !-f $weights_file );
115  
116 13 ▶ my $out_file = $out_file_arg
Branch: 2 branches
BranchTrueFalseTotal
$target_file ? : 1 3 4
defined $matrix_format && $matrix_format eq 'mtx' ? : 1 2 3
Condition66%4/6
$out_file_arg // ($target_file ? $out_file_patient : (defined $matrix_format && $matrix_format eq "mtx" ? "matrix.mtx" : $out_file_cohort))
l!l&&r!l&&!r
940
defined $matrix_format and $matrix_format eq "mtx"
!ll&&!rl&&r
201
MC/DC0%0/2
$out_file_arg // ($target_file ? $out_file_patient : (defined $matrix_format && $matrix_format eq "mtx" ? "matrix.mtx" : $out_file_cohort))
$out_file_arg ($target_file ? $out_file_patient : (defined $matrix_format && $matrix_format eq "mtx" ? "matrix.mtx" : $out_file_cohort))
MC/DC50%1/2
defined $matrix_format and $matrix_format eq "mtx"
defined $matrix_format $matrix_format eq "mtx"
Truth table66%2/3
A // B
A= $out_file_argB= ($target_file ? $out_file_patient : (defined $matrix_format && $matrix_format eq "mtx" ? "matrix.mtx" : $out_file_cohort))
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= defined $matrix_formatB= $matrix_format eq "mtx"
ABresult
0X0
100
111
117 // (
118 $target_file ? $out_file_patient
119 : ( defined $matrix_format && $matrix_format eq 'mtx' )
120 ? 'matrix.mtx'
121 : $out_file_cohort
122 );
123  
124 my (
125 13 $glob_hash_file, $ref_hash_file,
126 $ref_binary_hash_file, $coverage_stats_file, $labels_file
127 );
128 13 ▶ if ( defined $precomputed_ref_prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $precomputed_ref_prefix) 8 5 13
129 8 ▶ my $has_incompatible_options =
Condition33%5/15
@reference_files || @append_prefixes || $age || defined $include_hpo_ascendants || defined $retain_excluded_phenotypicFeatures || defined $weights_file
l!l&&r!l&&!r
008
@reference_files or @append_prefixes or $age or defined $include_hpo_ascendants or defined $retain_excluded_phenotypicFeatures
l!l&&r!l&&!r
008
@reference_files or @append_prefixes or $age or defined $include_hpo_ascendants
l!l&&r!l&&!r
008
@reference_files or @append_prefixes or $age
l!l&&r!l&&!r
008
@reference_files or @append_prefixes
l!l&&r!l&&!r
008
MC/DC0%0/6
@reference_files || @append_prefixes || $age || defined $include_hpo_ascendants || defined $retain_excluded_phenotypicFeatures || defined $weights_file
@reference_files @append_prefixes $age defined $include_hpo_ascendants defined $retain_excluded_phenotypicFeatures defined $weights_file
Truth table14%1/7
A or B or C or D or E || F
A= @reference_filesB= @append_prefixesC= $ageD= defined $include_hpo_ascendantsE= defined $retain_excluded_phenotypicFeaturesF= defined $weights_file
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
130 @reference_files
131 || @append_prefixes
132 || $age
133 || defined $include_hpo_ascendants
134 || defined $retain_excluded_phenotypicFeatures
135 || defined $weights_file;
136  
137 8 my @incompatible_flags = (
138 '--reference', '--age',
139 '--hpo-ascendants', '--retain-excluded-phenotypicFeatures',
140 '--weights', '--append-prefixes'
141 );
142  
143 8 ▶ if ($has_incompatible_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_incompatible_options) 0 8 8
144 0 my $flags_str = join( "\n", @incompatible_flags );
145 0 $self->_pod2usage(
146 -message =>
147 "Sorry, but the options\n$flags_str\nare incompatible with --prp <$precomputed_ref_prefix>\n",
148 -exitval => 1,
149 );
150 }
151  
152 8 $glob_hash_file = resolve_file( $precomputed_ref_prefix . '.glob_hash.json' );
153 8 $ref_hash_file = resolve_file( $precomputed_ref_prefix . '.ref_hash.json' );
154 8 $ref_binary_hash_file = resolve_file( $precomputed_ref_prefix . '.ref_binary_hash.json' );
155 8 $coverage_stats_file = resolve_file( $precomputed_ref_prefix . '.coverage_stats.json' );
156 8 my $candidate = resolve_file( $precomputed_ref_prefix . '.labels.json' );
157 8 ▶ $labels_file = $candidate if -e $candidate;
Branch: 1 branch
BranchTrueFalseTotal
if -e $candidate 6 2 8
158 }
159  
160 handle_option(
161 \$cytoscape_json,
162 "<--cytoscape-json> only works in cohort-mode",
163 $target_file, $out_file_graph, $self->{pod_file}
164 13 );
165 handle_option(
166 \$graph_stats,
167 "<--graph-stats> only works in cohort-mode",
168 $target_file, $out_file_graph_stats, $self->{pod_file}
169 13 );
170  
171 13 ▶ $ENV{'ANSI_COLORS_DISABLED'} = 1 unless $color;
Branch: 1 branch
BranchTrueFalseTotal
unless $color 0 13 13
172  
173 13 my $data = {
174 reference_files => \@reference_files,
175 target_file => $target_file,
176 weights_file => $weights_file,
177 include_hpo_ascendants => $include_hpo_ascendants,
178 align => $align,
179 align_basename => $align_basename,
180 export => $export,
181 export_basename => $export_basename,
182 out_file => $out_file,
183 cytoscape_json => $cytoscape_json,
184 graph_stats => $graph_stats,
185 graph_min_weight => $graph_min_weight,
186 graph_max_weight => $graph_max_weight,
187 max_out => $max_out,
188 max_number_vars => $max_number_vars,
189 sort_by => $sort_by,
190 similarity_metric_cohort => $similarity_metric_cohort,
191 matrix_format => $matrix_format,
192 patients_of_interest => \@patients_of_interest,
193 poi_out_dir => $poi_out_dir,
194 include_terms => \@include_terms,
195 exclude_terms => \@exclude_terms,
196 retain_excluded_phenotypicFeatures => $retain_excluded_phenotypicFeatures,
197 precomputed_ref_prefix => $precomputed_ref_prefix,
198 max_matrix_records_in_ram => $max_matrix_records_in_ram,
199 glob_hash_file => $glob_hash_file,
200 ref_hash_file => $ref_hash_file,
201 ref_binary_hash_file => $ref_binary_hash_file,
202 coverage_stats_file => $coverage_stats_file,
203 labels_file => $labels_file,
204 config_file => $config_file,
205 age => $age,
206 cli => $cli,
207 append_prefixes => \@append_prefixes,
208 log => $log,
209 debug => $debug,
210 verbose => $verbose
211 };
212  
213 13 require Pheno::Ranker::Options;
214 13 return Pheno::Ranker::Options->defined_constructor_args($data);
215 }
216  
217 26 ▶sub handle_option {
Subroutine handle_option: called CC=4 SCAR=13.9
Pod undocumented
218 26 my ( $option_ref, $message, $target_file, $default, $pod_file ) = @_;
219 26 ▶ if ( defined $$option_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$option_ref) 4 22 26
220 4 ▶ pod2usage( -input => $pod_file, -message => $message, -exitval => 1 )
Branch: 1 branch
BranchTrueFalseTotal
if $target_file 0 4 4
221 if $target_file;
222 4 ▶ $$option_ref = $$option_ref ? $$option_ref : $default;
Branch: 1 branch
BranchTrueFalseTotal
$$option_ref ? : 3 1 4
223 }
224 26 return 1;
225 }
226  
227 40 ▶sub resolve_file {
Subroutine resolve_file: called CC=3 SCAR=11.0
Pod undocumented
228 40 my $base = shift;
229 40 ▶ return $base if -e $base;
Branch: 1 branch
BranchTrueFalseTotal
if -e $base 33 7 40
230  
231 7 my $gz = $base . '.gz';
232 7 ▶ return $gz if -e $gz;
Branch: 1 branch
BranchTrueFalseTotal
if -e $gz 2 5 7
233  
234 5 return $base;
235 }
236  
237 0 ▶sub logical_cpu_count {
Subroutine logical_cpu_count: not called CC=6 SCAR=37.4
Pod undocumented
238 0 my $os = $^O;
239 0 my $out;
240  
241 0 ▶ if ( lc($os) eq 'darwin' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $os eq 'darwin') { } 0 0 0
elsif (lc $os eq 'freebsd') { } 0 0 0
elsif ($os eq 'MSWin32') { } 0 0 0
242 0 $out = qx{sysctl -n hw.logicalcpu 2>/dev/null};
243 }
244 elsif ( lc($os) eq 'freebsd' ) {
245 0 $out = qx{sysctl -n hw.ncpu 2>/dev/null};
246 }
247 elsif ( $os eq 'MSWin32' ) {
248 0 $out = $ENV{NUMBER_OF_PROCESSORS};
249 }
250 else {
251 0 $out = qx{/usr/bin/nproc 2>/dev/null};
252 }
253  
254 0 ▶ return $1 if defined $out && $out =~ /(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $out and $out =~ /(\d+)/ 0 0 0
Condition0%0/3
defined $out and $out =~ /(\d+)/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $out and $out =~ /(\d+)/
defined $out $out =~ /(\d+)/
Truth table0%0/3
A and B
A= defined $outB= $out =~ /(\d+)/
ABresult
0X0
100
111
255 0 return 1;
256 }
257  
258 0 ▶sub write_log {
Subroutine write_log: not called CC=5 SCAR=34.0
Pod undocumented
259 0 my ( $log, $data, $VERSION ) = @_;
260  
261 0 my $threadshost = logical_cpu_count();
262 0 $threadshost = 0 + $threadshost;
263  
264 my $info = {
265 date => ( strftime "%a %b %e %H:%M:%S %Y", localtime ),
266 threadshost => $threadshost,
267 hostname => hostname,
268 id => time . substr( "00000$$", -5 ),
269 version => $VERSION,
270 user => $ENV{'LOGNAME'}
271 || $ENV{'USER'}
272 0 ▶ || $ENV{'USERNAME'}
Condition0%0/8
$ENV{'LOGNAME'} || $ENV{'USER'} || $ENV{'USERNAME'} || 'dummy-user'
l!l
00
$ENV{"LOGNAME"} or $ENV{"USER"} or $ENV{"USERNAME"}
l!l&&r!l&&!r
000
$ENV{"LOGNAME"} or $ENV{"USER"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'LOGNAME'} || $ENV{'USER'} || $ENV{'USERNAME'} || 'dummy-user'
$ENV{"LOGNAME"} $ENV{"USER"} $ENV{"USERNAME"}
Truth table0%0/4
A or B or C
A= $ENV{"LOGNAME"}B= $ENV{"USER"}C= $ENV{"USERNAME"}
ABCresult
1XX1
01X1
0011
0000
273 || 'dummy-user'
274 };
275  
276 0 ▶ say BOLD GREEN "Writing <$log> file\n", RESET if $data->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"verbose"} 0 0 0
277 0 require Pheno::Ranker::IO;
278 0 Pheno::Ranker::IO::write_json(
279 {
280 filepath => $log,
281 data => { info => $info, data => $data }
282 }
283 );
284  
285 0 return 1;
286 }
287  
288 0 ▶sub program_header {
Subroutine program_header: not called CC=1 SCAR=6.9
Pod undocumented
289 0 my $VERSION = shift;
290 0 my $str = <<EOF;
291 ****************************************
292 * Rank against cohort(s) (BFF/PXF) *
293 * - PHENO-RANKER - *
294 * Version: $VERSION *
295 * (C) 2023-2025 Manuel Rueda, PhD *
296 * The Artistic License 2.0 *
297 ****************************************
298 EOF
299 0 return $str;
300 }
301  
302 1 ▶sub _pod2usage {
Subroutine _pod2usage: called CC=3 SCAR=13.3
303 1 my ( $self, @arg ) = @_;
304 1 ▶ if ( @arg == 1 && !ref $arg[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg == 1 and not ref $arg[0]) 1 0 1
Condition33%1/3
@arg == 1 and not ref $arg[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@arg == 1 and not ref $arg[0]
@arg == 1 not ref $arg[0]
Truth table33%1/3
A and not(B)
A= @arg == 1B= not ref $arg[0]
ABresult
0X0
100
111
305 1 return pod2usage( -input => $self->{pod_file}, -exitval => $arg[0] );
306 }
307 0 return pod2usage( -input => $self->{pod_file}, @arg );
308 }
309  
310 8 1;
↑ Summary lib/Pheno/Ranker/Compare/Rank.pm »