lib/Pheno/Ranker/Compare/Rank.pm

filter: Statementstmt 100% 106 / 106 Branchbran 71.4% 20 / 28 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 87.9% 138 / 157 CC 20 SCAR 27.1
CC
20
Cov
91%
compare_and_rank
20.6
BEGIN
1.0
BEGIN
1.0
CRAP 15.0
« lib/Pheno/Ranker/CLI.pm ↑ Summary lib/Pheno/Ranker/Compare/Matrix.pm »
1 package Pheno::Ranker::Compare::Rank;
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 feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Pheno::Ranker::Metrics;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Pheno::Ranker::Compare::Alignment qw(create_alignment);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Pheno::Ranker::Compare::Remap qw(guess_label);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 our @EXPORT_OK = qw(compare_and_rank);
14  
15 7 ▶sub compare_and_rank {
Subroutine compare_and_rank: called CC=20 SCAR=30.2
Pod undocumented
16 7 my $arg = shift;
17 7 my $glob_hash = $arg->{glob_hash};
18 7 my $ref_hash = $arg->{ref_hash};
19 7 my $tar_hash = $arg->{tar_hash};
20 7 my $ref_binary_hash = $arg->{ref_binary_hash};
21 7 my $tar_binary_hash = $arg->{tar_binary_hash};
22 7 my $weight = $arg->{weight};
23 7 my $self = $arg->{self};
24 7 ▶ my $nomenclature = $arg->{nomenclature} || {};
Condition50%1/2
$arg->{'nomenclature'} || {}
l!l
70
MC/DC0%0/1
$arg->{'nomenclature'} || {}
$arg->{'nomenclature'}
Truth table50%1/2
A
A= $arg->{'nomenclature'}
Aresult
11
00
25 7 my $sort_by = $self->{sort_by};
26 7 my $align = $self->{align};
27 7 my $max_out = $self->{max_out};
28  
29 say "Performing COHORT(REF)-PATIENT(TAR) comparison"
30 7 ▶ if ( $self->{debug} || $self->{verbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} or $self->{"verbose"} 0 7 7
Condition33%1/3
$self->{"debug"} or $self->{"verbose"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$self->{"debug"} or $self->{"verbose"}
$self->{"debug"} $self->{"verbose"}
Truth table33%1/3
A or B
A= $self->{"debug"}B= $self->{"verbose"}
ABresult
1X1
011
000
31  
32 # Hash for compiling metrics
33 7 my $score;
34  
35 # Hash for stats
36 my $stat;
37  
38 # Load TAR binary string
39 7 my ($tar) = keys %{$tar_binary_hash};
40 my $tar_str_weighted =
41 7 $tar_binary_hash->{$tar}{binary_digit_string_weighted};
42  
43 # Load TAR number of vars
44 7 my $target_vars = keys %{ $tar_hash->{$tar} };
45  
46 7 for my $key ( keys %{$ref_binary_hash} ) { # No need to sort
47  
48 # Load REF binary string
49 my $ref_str_weighted =
50 82 $ref_binary_hash->{$key}{binary_digit_string_weighted};
51 82 ▶ say "Comparing <id:$key> --- <id:$tar>" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 82 82
52 say "REF:$ref_str_weighted\nTAR:$tar_str_weighted\n"
53 82 ▶ if ( defined $self->{debug} && $self->{debug} > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"debug"} and $self->{"debug"} > 1 0 82 82
Condition33%1/3
defined $self->{"debug"} and $self->{"debug"} > 1
!ll&&!rl&&r
8200
MC/DC0%0/2
defined $self->{"debug"} and $self->{"debug"} > 1
defined $self->{"debug"} $self->{"debug"} > 1
Truth table33%1/3
A and B
A= defined $self->{"debug"}B= $self->{"debug"} > 1
ABresult
0X0
100
111
54  
55 # Hamming
56 $score->{$key}{hamming} =
57 82 hd_fast( $ref_str_weighted, $tar_str_weighted );
58  
59 # Intersect and Jaccard
60 82 ( $score->{$key}{jaccard}, $score->{$key}{intersect} ) =
61 jaccard_similarity( $ref_str_weighted, $tar_str_weighted );
62  
63 # Load REF number of vars
64 82 $score->{$key}{reference_vars} = keys %{ $ref_hash->{$key} };
65  
66 # Add values
67 82 push @{ $stat->{hamming_data} }, $score->{$key}{hamming};
68 82 push @{ $stat->{jaccard_data} }, $score->{$key}{jaccard};
69 }
70  
71 # Stats are only computed once (no overhead)
72 7 $stat->{hamming_stats} = add_stats( $stat->{hamming_data} );
73 7 $stat->{jaccard_stats} = add_stats( $stat->{jaccard_data} );
74  
75 # Initialize a few variables
76 7 my @headers = (
77 'RANK', 'REFERENCE(ID)',
78 'TARGET(ID)', 'FORMAT',
79 'LENGTH', 'WEIGHTED',
80 'HAMMING-DISTANCE', 'DISTANCE-Z-SCORE',
81 'DISTANCE-P-VALUE', 'DISTANCE-Z-SCORE(RAND)',
82 'JACCARD-INDEX', 'JACCARD-Z-SCORE',
83 'JACCARD-P-VALUE', 'REFERENCE-VARS',
84 'TARGET-VARS', 'INTERSECT',
85 'INTERSECT-RATE(%)', 'COMPLETENESS(%)'
86 );
87 7 my $header = join "\t", @headers;
88 7 my @results = $header;
89 7 my %info;
90 7 my $length_align = length($tar_str_weighted);
91 7 ▶ my $weight_bool = $weight ? 'True' : 'False';
Branch: 1 branch
BranchTrueFalseTotal
$weight ? : 1 6 7
92 7 my @alignments_ascii;
93 my $alignment_str_csv;
94 7 my @alignments_csv = join ';',
95 qw/id ref indicator tar weight hamming-distance json-path label/;
96  
97 # The dataframe will have two header lines
98 # *** IMPORTANT ***
99 # nsort does not yield same results as canonical from JSON::XS
100 # NB: we're sorting here and in create_binary_digit_string()
101 7 my @sort_keys_glob_hash = sort keys %{$glob_hash};
102  
103 # Creating @labels array from {id,label}, or guess_label()
104 my @labels =
105 7 ▶ map { exists $nomenclature->{$_} ? $nomenclature->{$_} : guess_label($_) }
Branch: 1 branch
BranchTrueFalseTotal
exists $nomenclature->{$_} ? : 255 73 328
106 @sort_keys_glob_hash;
107  
108 # Die if #elements in arrays differ
109 7 ▶ die "Mismatch between variables and labels"
Branch: 1 branch
BranchTrueFalseTotal
if @sort_keys_glob_hash != @labels 0 7 7
110 if @sort_keys_glob_hash != @labels;
111  
112 # Labels
113 # NB: there is a comma in 'Serum Glutamic Pyruvic Transaminase, CTCAE'
114 7 my @dataframe = join ';', 'Id', @labels;
115  
116 # Variables (json path)
117 7 push @dataframe, join ';', 'Id', @sort_keys_glob_hash;
118  
119 # 0/1
120 push @dataframe, join ';', qq/T|$tar/,
121 7 ( split //, $tar_binary_hash->{$tar}{binary_digit_string} ); # Add Target
122  
123 # Sort %score by value and load results
124 7 my $count = 1;
125 7 $max_out++; # to be able to start w/ ONE
126  
127 # Start loop
128 7 for my $key (
129 sort {
130 $sort_by eq 'jaccard' #
131 ? $score->{$b}{$sort_by}
132 <=> $score->{$a}{$sort_by} # 1 to 0 (similarity)
133 : $score->{$a}{$sort_by}
134 291 ▶ <=> $score->{$b}{$sort_by} # 0 to N (distance)
Branch: 1 branch
BranchTrueFalseTotal
$sort_by eq 'jaccard' ? : 5 286 291
135 } keys %$score
136 )
137 {
138  
139 82 ▶ say "$count: Creating alignment <id:$key>" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 82 82
140  
141 # Create ASCII alignment
142 # NB: We need it here to get $n_00
143 my ( $n_00, $alignment_str_ascii, $alignment_arr_csv ) =
144 create_alignment(
145 {
146 ref_key => $key,
147 ref_str_weighted =>
148 $ref_binary_hash->{$key}{binary_digit_string_weighted},
149 82 tar_str_weighted => $tar_str_weighted,
150 glob_hash => $glob_hash,
151 sorted_keys_glob_hash => \@sort_keys_glob_hash,
152 labels => \@labels
153 }
154 );
155  
156 # *** IMPORTANT ***
157 # The LENGTH of the alignment is based on the #variables in the REF-COHORT
158 # Compute estimated av and dev for binary_string of L = length_align - n_00
159 # Corrected length_align L = length_align - n_00
160 82 my $length_align_corrected = $length_align - $n_00;
161  
162 #$estimated_average, $estimated_std_dev
163 82 ( $stat->{hamming_stats}{mean_rnd}, $stat->{hamming_stats}{sd_rnd} ) =
164 estimate_hamming_stats($length_align_corrected);
165  
166 # Compute a few stats
167 my $hamming_z_score = z_score(
168 $score->{$key}{hamming},
169 $stat->{hamming_stats}{mean},
170 $stat->{hamming_stats}{sd}
171 82 );
172 my $hamming_z_score_from_random = z_score(
173 $score->{$key}{hamming},
174 $stat->{hamming_stats}{mean_rnd},
175 $stat->{hamming_stats}{sd_rnd}
176 82 );
177  
178 #my $hamming_p_value =
179 # p_value( $score->{$key}{hamming}, $length_align_corrected );
180 82 my $hamming_p_value_from_z_score =
181 p_value_from_z_score($hamming_z_score);
182 my $jaccard_z_score = z_score(
183 $score->{$key}{jaccard},
184 $stat->{jaccard_stats}{mean},
185 $stat->{jaccard_stats}{sd}
186 82 );
187 82 my $jaccard_p_value_from_z_score =
188 p_value_from_z_score( 1 - $jaccard_z_score );
189  
190 # Compute Intersect-Rate (I/T) * 100
191 # Completeness (T/R) * 100
192 82 my $reference_vars = $score->{$key}{reference_vars};
193 82 my $intersect = $score->{$key}{intersect};
194 82 ▶ my $intersect_rate =
Branch: 1 branch
BranchTrueFalseTotal
$target_vars == 0 ? : 0 82 82
195 ( $target_vars == 0 ) ? 0 : ( $intersect / $target_vars ) * 100;
196 82 ▶ my $completeness =
Branch: 1 branch
BranchTrueFalseTotal
$reference_vars == 0 ? : 0 82 82
197 ( $reference_vars == 0 ) ? 0 : ( $intersect / $reference_vars ) * 100;
198  
199 # Create a hash with formats
200 my $format = {
201 'RANK' => { value => $count, format => undef },
202 'REFERENCE(ID)' => { value => $key, format => undef },
203 'TARGET(ID)' => { value => $tar, format => undef },
204 'FORMAT' => { value => $self->{format}, format => undef },
205 'WEIGHTED' => { value => $weight_bool, format => undef },
206 'LENGTH' => { value => $length_align_corrected, format => '%6d' },
207 'HAMMING-DISTANCE' =>
208 { value => $score->{$key}{hamming}, format => '%4d' },
209 'DISTANCE-Z-SCORE' =>
210 { value => $hamming_z_score, format => '%7.3f' },
211 'DISTANCE-P-VALUE' =>
212 { value => $hamming_p_value_from_z_score, format => '%12.7f' },
213 'DISTANCE-Z-SCORE(RAND)' =>
214 { value => $hamming_z_score_from_random, format => '%8.4f' },
215 'JACCARD-INDEX' =>
216 82 { value => $score->{$key}{jaccard}, format => '%7.3f' },
217 'JACCARD-Z-SCORE' =>
218 { value => $jaccard_z_score, format => '%7.3f' },
219 'JACCARD-P-VALUE' =>
220 { value => $jaccard_p_value_from_z_score, format => '%12.7f' },
221 'REFERENCE-VARS' => { value => $reference_vars, format => '%6d' },
222 'TARGET-VARS' => { value => $target_vars, format => '%6d' },
223 'INTERSECT' => { value => $intersect, format => '%6d' },
224 'INTERSECT-RATE(%)' =>
225 { value => $intersect_rate, format => '%8.2f' },
226 'COMPLETENESS(%)' => { value => $completeness, format => '%8.2f' }
227 };
228  
229 # Serialize results
230 my $tmp_str = join "\t", map {
231 82 defined $format->{$_}{format}
232 ? sprintf( $format->{$_}{format}, $format->{$_}{value} )
233 : $format->{$_}{value}
234 1476 ▶ } @headers;
Branch: 1 branch
BranchTrueFalseTotal
defined $format->{$_}{'format'} ? : 1066 410 1476
235 82 push @results, $tmp_str;
236  
237 # To save memory only load if --align
238 82 ▶ if ( defined $align ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $align) 82 0 82
239  
240 # Add all of the above to @alignments_ascii
241 82 my $sep = ('-') x 80;
242 82 push @alignments_ascii,
243 qq/#$header\n$tmp_str\n$sep\n$$alignment_str_ascii/;
244  
245 # Add all of the above to @alignments_csv
246 82 push @alignments_csv, @$alignment_arr_csv;
247  
248 # Add data to dataframe
249 push @dataframe, join ';', qq/R|$key/,
250 82 ( split //, $ref_binary_hash->{$key}{binary_digit_string} );
251  
252 # Add values to info
253 $info{$key} = {
254 weighted => $weight_bool eq 'True'
255 ? JSON::XS::true
256 : JSON::XS::false,
257 reference_id => $key,
258 target_id => $tar,
259 reference_binary_string =>
260 $ref_binary_hash->{$key}{binary_digit_string},
261 target_binary_string =>
262 $tar_binary_hash->{$key}{binary_digit_string},
263 reference_binary_string_weighted =>
264 $ref_binary_hash->{$key}{binary_digit_string_weighted},
265 target_binary_string_weighted =>
266 $tar_binary_hash->{$key}{binary_digit_string_weighted},
267 alignment_length => $length_align_corrected,
268 hamming_distance => $score->{$key}{hamming},
269 hamming_z_score => $hamming_z_score,
270 hamming_p_value => $hamming_p_value_from_z_score,
271 jaccard_similarity => $score->{$key}{jaccard},
272 jaccard_z_score => $jaccard_z_score,
273 jaccard_p_value => $jaccard_p_value_from_z_score,
274 jaccard_distance => 1 - $score->{$key}{jaccard},
275 format => $self->{format},
276 82 ▶ alignment => $$alignment_str_ascii,
Branch: 1 branch
BranchTrueFalseTotal
$weight_bool eq 'True' ? : 36 46 82
277 reference_vars => $reference_vars,
278 target_vars => $target_vars,
279 intersect => $intersect,
280 intersect_rate => $intersect_rate,
281 completeness => $completeness
282 };
283  
284 }
285  
286 82 $count++;
287 82 ▶ last if $count == $max_out;
Branch: 1 branch
BranchTrueFalseTotal
if $count == $max_out 7 75 82
288 }
289  
290 7 return \@results, \%info, \@alignments_ascii, \@dataframe, \@alignments_csv;
291 }
292  
293 8 1;
« lib/Pheno/Ranker/CLI.pm ↑ Summary lib/Pheno/Ranker/Compare/Matrix.pm »