| 1 | package Pheno::Ranker::Metrics; | ||||||||||||||||||||||||||||
| 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 Data::Dumper; | ||||||||||||||||||||||||||||
| 9 | 8 | ▶ | use Math::CDF qw(pnorm pbinom); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Statistics::Descriptive; | ||||||||||||||||||||||||||
|
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 = | |||||||||||||||||||||||||||
| 14 | qw(hd_fast jaccard_similarity jaccard_similarity_formatted estimate_hamming_stats z_score p_value_from_z_score _p_value add_stats); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use constant DEVEL_MODE => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 19 | 8 | require Config; | |||||||||||||||||||||||||||
| 20 | 8 | require File::HomeDir; | |||||||||||||||||||||||||||
| 21 | 8 | require File::Path; | |||||||||||||||||||||||||||
| 22 | 8 | require File::Spec::Functions; | |||||||||||||||||||||||||||
| 23 | 8 | require Inline; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # Define a hidden directory in the user's home for Inline's compiled code. | ||||||||||||||||||||||||||||
| 26 | # Include the distribution, Perl version, and architecture to avoid reusing | ||||||||||||||||||||||||||||
| 27 | # cached Inline objects compiled for a different Perl ABI. | ||||||||||||||||||||||||||||
| 28 | my $inline_dir = $ENV{PHENO_RANKER_INLINE_DIR} // File::Spec::Functions::catdir( | ||||||||||||||||||||||||||||
| 29 | File::HomeDir->my_home, | ||||||||||||||||||||||||||||
| 30 | '.Inline', | ||||||||||||||||||||||||||||
| 31 | 'Pheno-Ranker', | ||||||||||||||||||||||||||||
| 32 | "perl-$Config::Config{version}", | ||||||||||||||||||||||||||||
| 33 | $Config::Config{archname} | ||||||||||||||||||||||||||||
| 34 | 8 | ▶ | ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{"PHENO_RANKER_INLINE_DIR"} // File::Spec::Functions::catdir("File::HomeDir"->my_home, ".Inline", "Pheno-Ranker", qq[perl-$Config::Config{"version"}], $Config::Config{"archname"})
MC/DC0%0/2
$ENV{"PHENO_RANKER_INLINE_DIR"} // File::Spec::Functions::catdir("File::HomeDir"->my_home, ".Inline", "Pheno-Ranker", qq[perl-$Config::Config{"version"}], $Config::Config{"archname"})
$ENV{"PHENO_RANKER_INLINE_DIR"}
File::Spec::Functions::catdir("File::HomeDir"->my_home, ".Inline", "Pheno-Ranker", qq[perl-$Config::Config{"version"}], $Config::Config{"archname"})
Truth table33%1/3
A // B
A= $ENV{"PHENO_RANKER_INLINE_DIR"}B= File::Spec::Functions::catdir("File::HomeDir"->my_home, ".Inline", "Pheno-Ranker", qq[perl-$Config::Config{"version"}], $Config::Config{"archname"})
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | File::Path::make_path($inline_dir) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | unless -d $inline_dir; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Configure Inline C before the compile-time Inline binding below. | ||||||||||||||||||||||||||||
| 40 | 8 | Inline->import( C => Config => directory => $inline_dir ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Inline C implementation using XS style (old-style syntax) | ||||||||||||||||||||||||||||
| 44 | 8 | ▶ | use Inline C => <<'END_C'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | #include "EXTERN.h" | ||||||||||||||||||||||||||||
| 46 | #include "perl.h" | ||||||||||||||||||||||||||||
| 47 | #include "XSUB.h" | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | SV* c_jaccard_similarity(char* str1, char* str2, int length) { | ||||||||||||||||||||||||||||
| 50 | int union_count = 0, intersection = 0, i; | ||||||||||||||||||||||||||||
| 51 | for(i = 0; i < length; i++){ | ||||||||||||||||||||||||||||
| 52 | if(str1[i] == '1' || str2[i] == '1'){ | ||||||||||||||||||||||||||||
| 53 | union_count++; | ||||||||||||||||||||||||||||
| 54 | if(str1[i] == '1' && str2[i] == '1') | ||||||||||||||||||||||||||||
| 55 | intersection++; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | double similarity = union_count ? ((double)intersection) / union_count : 0.0; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | /* Create a new array (AV) and push the two results */ | ||||||||||||||||||||||||||||
| 61 | AV* av = newAV(); | ||||||||||||||||||||||||||||
| 62 | av_push(av, newSVnv(similarity)); /* push similarity (double) */ | ||||||||||||||||||||||||||||
| 63 | av_push(av, newSViv(intersection)); /* push intersection (int) */ | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | /* Wrap the array in a reference and increment the reference count. | ||||||||||||||||||||||||||||
| 66 | Do not call sv_2mortal on the resulting RV. */ | ||||||||||||||||||||||||||||
| 67 | SV* rv = newRV_inc((SV*)av); | ||||||||||||||||||||||||||||
| 68 | return rv; | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | /* | ||||||||||||||||||||||||||||
| 72 | This function computes the Hamming distance between two strings, | ||||||||||||||||||||||||||||
| 73 | assuming they are both composed of '0' and '1' characters and have equal length. | ||||||||||||||||||||||||||||
| 74 | It is defined as c_hd_fast to avoid a naming conflict with the Perl wrapper. | ||||||||||||||||||||||||||||
| 75 | */ | ||||||||||||||||||||||||||||
| 76 | int c_hd_fast(char* s1, char* s2, int len) { | ||||||||||||||||||||||||||||
| 77 | int diff = 0; | ||||||||||||||||||||||||||||
| 78 | int i; | ||||||||||||||||||||||||||||
| 79 | for(i = 0; i < len; i++){ | ||||||||||||||||||||||||||||
| 80 | if (s1[i] != s2[i]) | ||||||||||||||||||||||||||||
| 81 | diff++; | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | return diff; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | END_C | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | ########### | ||||||||||||||||||||||||||||
| 88 | # HAMMING # | ||||||||||||||||||||||||||||
| 89 | ########### | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Perl wrapper: calls the Inline C function "c_hd_fast" | ||||||||||||||||||||||||||||
| 92 | 9290 | ▶ | sub hd_fast { | ||||||||||||||||||||||||||
|
Subroutine
hd_fast: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 9290 | my ( $s1, $s2 ) = @_; | |||||||||||||||||||||||||||
| 94 | 9290 | ▶ | die "Strings must be the same length" if length($s1) != length($s2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 9289 | return c_hd_fast( $s1, $s2, length($s1) ); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Original | ||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub _hd_fast { | ||||||||||||||||||||||||||
|
Subroutine
_hd_fast: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Hamming Distance | ||||||||||||||||||||||||||||
| 102 | 1 | return ( $_[0] ^ $_[1] ) =~ tr/\001-\255//; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | ########### | ||||||||||||||||||||||||||||
| 106 | # JACCARD # | ||||||||||||||||||||||||||||
| 107 | ########### | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Perl wrapper: calls the Inline C function "c_jaccard_similarity" | ||||||||||||||||||||||||||||
| 110 | 715 | ▶ | sub jaccard_similarity { | ||||||||||||||||||||||||||
|
Subroutine
jaccard_similarity: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 715 | my ( $str1, $str2 ) = @_; | |||||||||||||||||||||||||||
| 112 | 715 | my $len = length($str1); | |||||||||||||||||||||||||||
| 113 | 715 | ▶ | die "Strings must be of equal length" if $len != length($str2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | my ( $jaccard, $intersection ) = | ||||||||||||||||||||||||||||
| 115 | 714 | @{ c_jaccard_similarity( $str1, $str2, $len ) }; | |||||||||||||||||||||||||||
| 116 | 714 | return ( $jaccard, $intersection ); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Original (using vec) | ||||||||||||||||||||||||||||
| 120 | 2 | ▶ | sub _jaccard_similarity { | ||||||||||||||||||||||||||
|
Subroutine
_jaccard_similarity: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 121 | 2 | my ( $str1, $str2 ) = @_; | |||||||||||||||||||||||||||
| 122 | 2 | my $len = length($str1); | |||||||||||||||||||||||||||
| 123 | 2 | ▶ | die "Strings must be of equal length" if $len != length($str2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 2 | my ( $intersection, $union ) = ( 0, 0 ); | |||||||||||||||||||||||||||
| 125 | 2 | for my $i ( 0 .. $len - 1 ) { | |||||||||||||||||||||||||||
| 126 | 8 | my $b1 = vec( $str1, $i, 8 ); | |||||||||||||||||||||||||||
| 127 | 8 | my $b2 = vec( $str2, $i, 8 ); | |||||||||||||||||||||||||||
| 128 | 8 | ▶ | if ( $b1 == ord('1') || $b2 == ord('1') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b1 == 49 or $b2 == 49
MC/DC100%2/2
$b1 == 49 or $b2 == 49
$b1 == 49
$b2 == 49
Truth table100%3/3
A or B
A= $b1 == 49B= $b2 == 49
| |||||||||||||||||||||||||||||
| 129 | 3 | $union++; | |||||||||||||||||||||||||||
| 130 | 3 | ▶ | $intersection++ if ( $b1 == ord('1') && $b2 == ord('1') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$b1 == 49 and $b2 == 49
MC/DC100%2/2
$b1 == 49 and $b2 == 49
$b1 == 49
$b2 == 49
Truth table100%3/3
A and B
A= $b1 == 49B= $b2 == 49
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 2 | ▶ | return $union == 0 ? ( 0, 0 ) : ( $intersection / $union, $intersection ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 631 | ▶ | sub jaccard_similarity_formatted { | ||||||||||||||||||||||||||
|
Subroutine
jaccard_similarity_formatted: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # *** IMPORTANT **** | ||||||||||||||||||||||||||||
| 139 | # mrueda Dec-27-23 | ||||||||||||||||||||||||||||
| 140 | # Direct formatting in jaccard_similarity adds minor overhead (verified by testing), | ||||||||||||||||||||||||||||
| 141 | # but prevents errors on some CPAN FreeBSD architectures. | ||||||||||||||||||||||||||||
| 142 | 631 | my ( $result, undef ) = jaccard_similarity(@_); | |||||||||||||||||||||||||||
| 143 | 631 | return sprintf( "%.6f", $result ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | ######### | ||||||||||||||||||||||||||||
| 147 | # STATS # | ||||||||||||||||||||||||||||
| 148 | ######### | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 83 | ▶ | sub estimate_hamming_stats { | ||||||||||||||||||||||||||
|
Subroutine
estimate_hamming_stats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Estimate Hamming stats using a binomial distribution model. Assumes each bit position | ||||||||||||||||||||||||||||
| 153 | # in the binary strings has an independent 50% chance of mismatch, to calculate | ||||||||||||||||||||||||||||
| 154 | # the mean and standard deviation of the Hamming distance. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 83 | my $length = shift; | |||||||||||||||||||||||||||
| 157 | 83 | my $probability_mismatch = 0.5; | |||||||||||||||||||||||||||
| 158 | 83 | my $estimated_average = $length * $probability_mismatch; | |||||||||||||||||||||||||||
| 159 | 83 | my $estimated_std_dev = | |||||||||||||||||||||||||||
| 160 | sqrt( $length * $probability_mismatch * ( 1 - $probability_mismatch ) ); | ||||||||||||||||||||||||||||
| 161 | 83 | return $estimated_average, $estimated_std_dev; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 248 | ▶ | sub z_score { | ||||||||||||||||||||||||||
|
Subroutine
z_score: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | 248 | my ( $observed_value, $expected_value, $std_dev ) = @_; | |||||||||||||||||||||||||||
| 166 | 248 | ▶ | return 0 if $std_dev == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 247 | return ( $observed_value - $expected_value ) / $std_dev; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 165 | ▶ | sub p_value_from_z_score { | ||||||||||||||||||||||||||
|
Subroutine
p_value_from_z_score: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 165 | return pnorm(shift) # One-tailed test | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | #sub _p_value { | ||||||||||||||||||||||||||||
| 175 | # my ( $hamming_distance, $string_length ) = @_; | ||||||||||||||||||||||||||||
| 176 | # my $probability_mismatch = 0.5; | ||||||||||||||||||||||||||||
| 177 | # return 2 * (1 - pbinom($hamming_distance - 1, $string_length, $probability_mismatch)) | ||||||||||||||||||||||||||||
| 178 | #} | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 15 | ▶ | sub add_stats { | ||||||||||||||||||||||||||
|
Subroutine
add_stats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 181 | 15 | my $array = shift; | |||||||||||||||||||||||||||
| 182 | 15 | my %hash_out; | |||||||||||||||||||||||||||
| 183 | 15 | my $stat = Statistics::Descriptive::Full->new(); | |||||||||||||||||||||||||||
| 184 | 15 | $stat->add_data($array); | |||||||||||||||||||||||||||
| 185 | 15 | $hash_out{mean} = $stat->mean(); | |||||||||||||||||||||||||||
| 186 | 15 | $hash_out{sd} = $stat->standard_deviation(); | |||||||||||||||||||||||||||
| 187 | 15 | $hash_out{count} = $stat->count(); | |||||||||||||||||||||||||||
| 188 | 15 | $hash_out{per25} = $stat->percentile(25); | |||||||||||||||||||||||||||
| 189 | 15 | $hash_out{per75} = $stat->percentile(75); | |||||||||||||||||||||||||||
| 190 | 15 | $hash_out{min} = $stat->min(); | |||||||||||||||||||||||||||
| 191 | 15 | $hash_out{max} = $stat->max(); | |||||||||||||||||||||||||||
| 192 | 15 | $hash_out{median} = $stat->median(); | |||||||||||||||||||||||||||
| 193 | 15 | $hash_out{sum} = $stat->sum(); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 15 | return \%hash_out; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | 8 | 1; | |||||||||||||||||||||||||||