lib/Pheno/Ranker/Compare/Matrix.pm

filter: Statementstmt 100% 98 / 98 Branchbran 86.6% 26 / 30 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 90.6% 146 / 161 CC 26 SCAR 26.1
CC
26
Cov
94%
cohort_comparison
20.4
_write_matrix_market
7.0
BEGIN
1.0
CRAP 13.6
« lib/Pheno/Ranker/Compare/Rank.pm ↑ Summary blib/lib/auto/share/dist/Pheno-Ranker/diseases/hpo/scripts/hpo_disease_converter.pl »
1 package Pheno::Ranker::Compare::Matrix;
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 Sort::Naturally qw(nsort);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Pheno::Ranker::Metrics;
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(cohort_comparison);
14  
15 20 ▶sub cohort_comparison {
Subroutine cohort_comparison: called CC=20 SCAR=30.1
Pod undocumented
16 20 my ( $ref_binary_hash, $self ) = @_;
17 20 my $out_file = $self->{out_file};
18 20 my $similarity_metric = $self->{similarity_metric_cohort};
19 20 ▶ my $matrix_format = $self->{matrix_format} || 'dense';
Condition100%2/2
$self->{'matrix_format'} || 'dense'
l!l
182
MC/DC100%1/1
$self->{'matrix_format'} || 'dense'
$self->{'matrix_format'}
Truth table100%2/2
A
A= $self->{'matrix_format'}
Aresult
11
00
20  
21 # Define limit #items for switching to whole matrix calculation
22 20 my $max_items = $self->{max_matrix_records_in_ram};
23  
24 # Inform about the start of the comparison process
25 say "Performing COHORT comparison"
26 20 ▶ if ( $self->{debug} || $self->{verbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} or $self->{"verbose"} 1 19 20
Condition66%2/3
$self->{"debug"} or $self->{"verbose"}
l!l&&r!l&&!r
1019
MC/DC50%1/2
$self->{"debug"} or $self->{"verbose"}
$self->{"debug"} $self->{"verbose"}
Truth table66%2/3
A or B
A= $self->{"debug"}B= $self->{"verbose"}
ABresult
1X1
011
000
27  
28 # Define the subroutine to be used
29 20 my %similarity_function = (
30 'hamming' => \&hd_fast,
31 'jaccard' => \&jaccard_similarity_formatted
32 );
33  
34 # Define values for diagonal elements depending on metric
35 20 my %similarity_diagonal = (
36 'hamming' => 0,
37 'jaccard' => 1
38 );
39  
40 # Use previous hashes to define stuff
41 20 my $metric = $similarity_function{$similarity_metric};
42 20 my $similarity_diagonal = $similarity_diagonal{$similarity_metric};
43  
44 # Sorting keys of the hash
45 20 my @ids = nsort( keys %{$ref_binary_hash} );
46 20 my @strings = map { $ref_binary_hash->{$_}{binary_digit_string_weighted} } @ids;
47 20 my $num_items = scalar @ids;
48  
49 # Define $switch for going from RAM to all calculations
50 20 ▶ my $switch = $num_items > $max_items ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$num_items > $max_items ? : 3 17 20
51  
52 say "RAM efficient mode is: "
53 . ( $switch ? "on" : "off" )
54 . " (max_matrix_records_in_ram: $max_items)"
55 20 ▶ if ( $self->{debug} || $self->{verbose} );
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"debug"} or $self->{"verbose"} 1 19 20
$switch ? : 1 0 1
Condition66%2/3
$self->{"debug"} or $self->{"verbose"}
l!l&&r!l&&!r
1019
MC/DC50%1/2
$self->{"debug"} or $self->{"verbose"}
$self->{"debug"} $self->{"verbose"}
Truth table66%2/3
A or B
A= $self->{"debug"}B= $self->{"verbose"}
ABresult
1X1
011
000
56  
57 20 ▶ if ( $matrix_format eq 'mtx' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix_format eq "mtx") 2 18 20
58 _write_matrix_market(
59 {
60 out_file => $out_file,
61 ids => \@ids,
62 strings => \@strings,
63 metric => $metric,
64 similarity_diagonal => $similarity_diagonal,
65 verbose => $self->{verbose},
66 }
67 2 );
68 2 ▶ say "Matrix saved to <$out_file>" if ( $self->{debug} || $self->{verbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} or $self->{"verbose"} 0 2 2
Condition33%1/3
$self->{"debug"} or $self->{"verbose"}
l!l&&r!l&&!r
002
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
69 2 return 1;
70 }
71  
72 # Opening file for output
73 18 open( my $fh, '>:encoding(UTF-8)', $out_file );
74 18 say $fh "\t", join "\t", @ids;
75  
76 # Initialize matrix for storing similarity
77 18 my @matrix;
78  
79 # Iterate over items (I elements)
80 18 for my $i ( 0 .. $#ids ) {
81 say "Calculating <" . $ids[$i] . "> against the cohort..."
82 462 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 462 462
83 462 my $str1 = $strings[$i];
84  
85 462 my @row = ( $ids[$i] );
86  
87 # Iterate for pairwise comparisons (J elements)
88 462 for my $j ( 0 .. $#ids ) {
89 16336 my $str2 = $strings[$j];
90 16336 my $similarity;
91  
92 16336 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) { } 8 16328 16336
93  
94 # For large datasets compute upper and lower triange
95 8 ▶ $similarity =
Branch: 1 branch
BranchTrueFalseTotal
$i == $j ? : 4 4 8
96 $i == $j ? $similarity_diagonal : $metric->( $str1, $str2 );
97 }
98 else {
99 16328 ▶ if ( $i == $j ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == $j) { } 458 15870 16328
elsif ($j > $i) { } 7935 7935 15870
100  
101 # Similarity for diagonal elements
102 458 $similarity = $similarity_diagonal;
103 }
104 elsif ( $j > $i ) {
105  
106 # Compute similarity for large cohorts or upper triangle
107 7935 $similarity = $metric->( $str1, $str2 );
108 7935 $matrix[$i][$j] = $similarity;
109 }
110 else {
111 # Use precomputed similarity from lower triangle
112 7935 $similarity = $matrix[$j][$i];
113 }
114 }
115  
116 16336 push @row, $similarity;
117 }
118  
119 462 say $fh join "\t", @row;
120 }
121  
122 # Close the file handle
123 18 close $fh;
124  
125 # Inform about the completion of the matrix computation
126 18 ▶ say "Matrix saved to <$out_file>" if ( $self->{debug} || $self->{verbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} or $self->{"verbose"} 1 17 18
Condition66%2/3
$self->{"debug"} or $self->{"verbose"}
l!l&&r!l&&!r
1017
MC/DC50%1/2
$self->{"debug"} or $self->{"verbose"}
$self->{"debug"} $self->{"verbose"}
Truth table66%2/3
A or B
A= $self->{"debug"}B= $self->{"verbose"}
ABresult
1X1
011
000
127 18 return 1;
128 }
129  
130 2 ▶sub _write_matrix_market {
Subroutine _write_matrix_market: called CC=7 SCAR=19.5
131 2 my $arg = shift;
132 2 my $out_file = $arg->{out_file};
133 2 my $ids = $arg->{ids};
134 2 my $strings = $arg->{strings};
135 2 my $metric = $arg->{metric};
136 2 my $similarity_diagonal = $arg->{similarity_diagonal};
137 2 my $verbose = $arg->{verbose};
138 2 my $num_items = scalar @$ids;
139  
140 2 open( my $fh, '+>:encoding(UTF-8)', $out_file );
141 2 say $fh '%%MatrixMarket matrix coordinate real symmetric';
142 2 say $fh '% Generated by Pheno-Ranker';
143 2 say $fh '% IDs use 1-based matrix indexes: % id <index> <identifier>';
144 2 for my $i ( 0 .. $#$ids ) {
145 38 say $fh '% id ' . ( $i + 1 ) . ' ' . $ids->[$i];
146 }
147  
148 2 my $size_line_offset = tell $fh;
149 2 printf {$fh} "%20d %20d %20d\n", $num_items, $num_items, 0;
150  
151 2 my $nonzero_count = 0;
152 2 for my $i ( 0 .. $#$ids ) {
153 38 ▶ say "Calculating <" . $ids->[$i] . "> against the cohort..."
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 38 38
154 if $verbose;
155 38 my $str1 = $strings->[$i];
156  
157 # Matrix Market symmetric coordinate format stores one triangle only.
158 38 for my $j ( $i .. $#$ids ) {
159 669 ▶ my $similarity =
Branch: 1 branch
BranchTrueFalseTotal
$i == $j ? : 38 631 669
160 $i == $j ? $similarity_diagonal : $metric->( $str1, $strings->[$j] );
161 669 ▶ next if $similarity == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $similarity == 0 38 631 669
162  
163 631 printf {$fh} "%d %d %s\n", $i + 1, $j + 1, $similarity;
164 631 $nonzero_count++;
165 }
166 }
167  
168 2 seek $fh, $size_line_offset, 0;
169 2 printf {$fh} "%20d %20d %20d\n", $num_items, $num_items, $nonzero_count;
170 2 close $fh;
171  
172 2 return 1;
173 }
174  
175 8 1;
« lib/Pheno/Ranker/Compare/Rank.pm ↑ Summary blib/lib/auto/share/dist/Pheno-Ranker/diseases/hpo/scripts/hpo_disease_converter.pl »