blib/lib/Bio/Gonzales/Util/Math/kNN/via/Distances.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 100% 6 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 97.3% 72 / 74 CC 5 SCAR 5.1
CC
5
Cov
98%
calc
3.0
_vote
2.0
_distance_between
2.0
CRAP 1.7
« blib/lib/Bio/Gonzales/Align/Jalview.pm ↑ Summary blib/lib/Bio/Gonzales/Util/Cerial.pm »
1 package Bio::Gonzales::Util::Math::kNN::via::Distances;
2  
3 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw/max min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::MoreUtils qw/indexes/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our $VERSION = '0.058'; # VERSION
13  
14 =head1 NAME
15  
16 Bio::Gonzales::Util::Math::kNN::via::Distances - Calculate kNN clusterings from already calculated distances
17  
18 =head1 SYNOPSIS
19  
20 use Bio::Gonzales::Util::Math::kNN::via::Distances;
21  
22 my $k = Bio::Gonzales::Util::Math::kNN::via::Distances->new(
23 distances => [ [1], [ 2, 3 ], ... ],
24 groups => [ 'group1 row1', 'group row2', undef, 'group row4' ]
25 );
26 my $result = $k->calc(1);
27  
28 =head1 DESCRIPTION
29  
30 =head1 METHODS
31  
32 =head2 Bio::Gonzales::Util::Math::kNN::via::Distances->new(...)
33  
34 =over 4
35  
36 =item distances
37  
38 Distances in lower triangular form (array of arrays).
39  
40 =item groups
41  
42 Groups in array, undef for "test" rows.
43  
44 =back
45  
46 =cut
47  
48 #array of arrays (distances in lower triangular matrix form)
49 1 has distances => ( is => 'rw', required => 1 );
50 #array of groupnames with undef for test
51 1 has groups => ( is => 'rw', required => 1 );
52  
53 sub calc {
54 1 ▶ my ( $self, $k ) = @_;
Subroutine calc: called CC=3 SCAR=11.0
Pod undocumented
55  
56 1 ▶ my @training_idx = indexes {$_} @{ $self->groups };
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 say STDERR "Training idx: " . Dumper( \@training_idx );
58  
59 1 my @lies_in_group;
60 1 ▶ for ( my $i = 0; $i < @{ $self->distances }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->distances;}) 4 1 5
61 4 my $d = $self->distances->[$i];
62  
63 # here we have a training 'row', so skip it
64 4 ▶ if ( $self->groups->[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->groups->[$i]) 2 2 4
65 2 $lies_in_group[$i] = undef;
66 2 next;
67 }
68  
69 # get index of $k distances from training set
70 my @k_nearest
71 2 = ( sort { $self->_distance_between( $a, $i ) <=> $self->_distance_between( $b, $i ) } @training_idx )
72 [ 0 .. ( $k - 1 ) ];
73 2 say STDERR "$i -> " . Dumper( \@k_nearest );
74  
75 2 $lies_in_group[$i] = $self->_vote( \@k_nearest );
76 }
77 1 return \@lies_in_group;
78 }
79  
80 sub _distance_between {
81 4 ▶ my ( $self, $i, $a ) = @_;
Subroutine _distance_between: called CC=2 SCAR=6.9
82  
83 4 my $dist;
84 4 ▶ if ( $a > $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a > $i) { } 1 3 4
85 1 $dist = $self->distances->[$a][$i];
86 } else {
87 3 $dist = $self->distances->[$i][$a];
88 }
89  
90 4 say STDERR "($i, $a) => $dist";
91 4 return $dist;
92 }
93  
94 sub _vote {
95 2 ▶ my ( $self, $k_nearest ) = @_;
Subroutine _vote: called CC=2 SCAR=7.0
96  
97 2 my %votes;
98 2 for my $idx ( @{$k_nearest} ) {
99 2 $votes{ $self->groups->[$idx] }++;
100 }
101  
102 2 my $group = ( sort { $votes{$b} <=> $votes{$a} } keys %votes )[0];
103  
104 2 return $group;
105 }
106  
107 1 1;
108 __END__
109  
110 =head1 SEE ALSO
111  
112 =head1 AUTHOR
113  
114 jw bargsten, C<< <joachim.bargsten at wur.nl> >>
115  
116 =cut
« blib/lib/Bio/Gonzales/Align/Jalview.pm ↑ Summary blib/lib/Bio/Gonzales/Util/Cerial.pm »