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
|
|
1162
|
use Moo; |
|
1
|
|
|
|
|
9222
|
|
|
1
|
|
|
|
|
4
|
|
7
|
1
|
|
|
1
|
|
1435
|
use strictures 2; |
|
1
|
|
|
|
|
1174
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
474
|
use namespace::clean; |
|
1
|
|
|
|
|
8180
|
|
|
1
|
|
|
|
|
11
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
291
|
use List::Util 1.30 qw(sum0); |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
389
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.0102'; |
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
|
|
894
|
my $self = shift; |
54
|
3
|
50
|
|
|
|
34
|
my $n = $self->data ? scalar @{ $self->data } : $self->freq ? scalar @{ $self->freq } : 0; |
|
1
|
100
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
55
|
3
|
|
|
|
|
27
|
return $n; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub _build_sum { |
59
|
3
|
|
|
3
|
|
21
|
my $self = shift; |
60
|
3
|
50
|
|
|
|
31
|
my $sum = $self->data ? sum0 @{ $self->data } : $self->freq ? sum0 @{ $self->freq } : 0; |
|
1
|
100
|
|
|
|
8
|
|
|
2
|
|
|
|
|
32
|
|
61
|
3
|
|
|
|
|
25
|
return $sum; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _build_freq { |
65
|
1
|
|
|
1
|
|
15
|
my $self = shift; |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
4
|
my @freq; |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
4
|
for my $datum ( @{ $self->data } ) { |
|
1
|
|
|
|
|
25
|
|
70
|
5
|
|
|
|
|
128
|
push @freq, $datum / $self->sum; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
|
|
25
|
return \@freq; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _build_index { |
78
|
3
|
|
|
3
|
|
586
|
my $self = shift; |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
|
|
5
|
my @index; |
81
|
|
|
|
|
|
|
|
82
|
3
|
|
|
|
|
6
|
for my $datum ( @{ $self->freq } ) { |
|
3
|
|
|
|
|
48
|
|
83
|
13
|
|
|
|
|
56
|
push @index, $datum * log($datum); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
42
|
return -1 * sum0 @index; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _build_evenness { |
91
|
3
|
|
|
3
|
|
19
|
my $self = shift; |
92
|
3
|
|
|
|
|
47
|
return $self->index / log( $self->N ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
__END__ |