line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Statistics::Diversity::Shannon; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Compute the Shannon diversity |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
821
|
use Moo; |
|
1
|
|
|
|
|
8255
|
|
|
1
|
|
|
|
|
6
|
|
7
|
1
|
|
|
1
|
|
1342
|
use strictures 2; |
|
1
|
|
|
|
|
1273
|
|
|
1
|
|
|
|
|
45
|
|
8
|
1
|
|
|
1
|
|
471
|
use namespace::clean; |
|
1
|
|
|
|
|
8616
|
|
|
1
|
|
|
|
|
7
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
332
|
use List::Util qw(sum0); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
439
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has data => ( |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
isa => sub { die 'Not an array reference' unless ref($_[0]) eq 'ARRAY' }, |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has freq => ( |
21
|
|
|
|
|
|
|
is => 'ro', |
22
|
|
|
|
|
|
|
isa => sub { die 'Not an array reference' unless ref($_[0]) eq 'ARRAY' }, |
23
|
|
|
|
|
|
|
builder => 1, |
24
|
|
|
|
|
|
|
lazy => 1, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has N => ( |
28
|
|
|
|
|
|
|
is => 'ro', |
29
|
|
|
|
|
|
|
builder => 1, |
30
|
|
|
|
|
|
|
lazy => 1, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has sum => ( |
34
|
|
|
|
|
|
|
is => 'ro', |
35
|
|
|
|
|
|
|
builder => 1, |
36
|
|
|
|
|
|
|
lazy => 1, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has index => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
builder => 1, |
42
|
|
|
|
|
|
|
lazy => 1, |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has evenness => ( |
46
|
|
|
|
|
|
|
is => 'ro', |
47
|
|
|
|
|
|
|
builder => 1, |
48
|
|
|
|
|
|
|
lazy => 1, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _build_N { |
53
|
3
|
|
|
3
|
|
870
|
my $self = shift; |
54
|
3
|
50
|
|
|
|
45
|
my $n = $self->data ? scalar @{ $self->data } : $self->freq ? scalar @{ $self->freq } : 0; |
|
1
|
100
|
|
|
|
5
|
|
|
2
|
|
|
|
|
52
|
|
55
|
3
|
|
|
|
|
33
|
return $n; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _build_sum { |
59
|
3
|
|
|
3
|
|
26
|
my $self = shift; |
60
|
3
|
50
|
|
|
|
37
|
my $sum = $self->data ? sum0 @{ $self->data } : $self->freq ? sum0 @{ $self->freq } : 0; |
|
1
|
100
|
|
|
|
8
|
|
|
2
|
|
|
|
|
36
|
|
61
|
3
|
|
|
|
|
37
|
return $sum; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _build_freq { |
65
|
1
|
|
|
1
|
|
11
|
my $self = shift; |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
2
|
my @freq; |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
2
|
for my $datum ( @{ $self->data } ) { |
|
1
|
|
|
|
|
4
|
|
70
|
5
|
|
|
|
|
86
|
push @freq, $datum / $self->sum; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
20
|
return \@freq; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _build_index { |
78
|
3
|
|
|
3
|
|
590
|
my $self = shift; |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
8
|
my @index; |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
5
|
for my $datum ( @{ $self->freq } ) { |
|
3
|
|
|
|
|
50
|
|
83
|
13
|
|
|
|
|
48
|
push @index, $datum * log($datum); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
36
|
return -1 * sum0 @index; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _build_evenness { |
91
|
3
|
|
|
3
|
|
30
|
my $self = shift; |
92
|
3
|
|
|
|
|
44
|
return $self->index / log( $self->N ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |