Hobohm.pm

filter: Statementstmt 100% 42 / 42 Branchbran 87.5% 7 / 8 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 98.4% 62 / 63 CC 8 SCAR 9.2
CC
8
Cov
98%
cluster
4.0
_similarity
3.0
_is_similar
3.0
CRAP 2.5
↑ Summary Hobohm/Types.pm »
1 package String::Cluster::Hobohm;
2 2 $String::Cluster::Hobohm::VERSION = '0.141020';
3 # ABSTRACT: Cluster strings using the Hobohm algorithm
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use String::Cluster::Hobohm::Types 'Quotient';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Text::LevenshteinXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # accept a list of strings or a closure that returns, stepwise, the
14 # strings (this prevents loading everything into memory in the case that
15 # the dataset is too big)
16  
17 # Return, for now, a structure with the indices
18 # {
19 # index1 => [ index2, index3, index4 ]
20 # ...
21 # }
22  
23 2 has similarity => ( is => 'ro', default => 0.62, isa => Quotient );
24  
25 3 ▶sub cluster {
Subroutine cluster: called CC=4 SCAR=13.9
Pod documented
26 3 my ($self, $sequences) = @_;
27 3 ▶ defined $sequences or croak "Need sequences as argument";
Branch: 1 branch
BranchTrueFalseTotal
defined $sequences or croak("Need sequences as argument") 2 1 3
28  
29 2 my @clusters;
30  
31 2 foreach my $sequence (@$sequences) {
32  
33 6 my $cluster_id = $self->_is_similar( $sequence, \@clusters );
34  
35 6 ▶ if ( defined $cluster_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cluster_id) { } 2 4 6
36 2 push @{ $clusters[ $cluster_id ] }, \$sequence;
37 }
38 else {
39 4 push @clusters, [ \$sequence ]
40 }
41 }
42  
43 2 return \@clusters;
44 }
45  
46 6 ▶sub _is_similar {
Subroutine _is_similar: called CC=3 SCAR=11.0
47 6 my ($self, $sequence, $clusters ) = @_;
48  
49 6 foreach my $i (0 .. $#$clusters) {
50 4 my $similarity = $self->_similarity(\$sequence, $clusters->[$i][0]);
51  
52 4 ▶ return $i if $similarity >= $self->similarity;
Branch: 1 branch
BranchTrueFalseTotal
if $similarity >= $self->similarity 2 2 4
53 }
54  
55 4 return;
56 }
57  
58 4 ▶sub _similarity {
Subroutine _similarity: called CC=3 SCAR=11.0
59 4 my $self = shift;
60  
61 4 my @seqs = map { $$_ } @_;
62  
63 4 my $distance = Text::LevenshteinXS::distance(@seqs);
64 4 ▶ defined $distance or croak "unable to compute distance";
Branch: 1 branch
BranchTrueFalseTotal
defined $distance or croak("unable to compute distance") 4 0 4
65  
66 4 ▶ return 1 - $distance / (length( $seqs[0] ) || 1);
Condition100%2/2
length $seqs[0] || 1
l!l
31
MC/DC100%1/1
length $seqs[0] || 1
length $seqs[0]
Truth table100%2/2
A
A= length $seqs[0]
Aresult
11
00
67 }
68  
69 2 __PACKAGE__->meta->make_immutable;
70  
71 __END__
↑ Summary Hobohm/Types.pm »