blib/lib/Bio/Gonzales/Stat/Util.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 149 CC 11 SCAR 27.2
CC
11
Cov
0%
hist_text
42.0
nstat
20.0
gen_number_formatter
6.0
CRAP 15.1
1 package Bio::Gonzales::Stat::Util;
2 use warnings;
3 use strict;
4 use Carp;
5 use Statistics::Descriptive;
6 use Number::Format qw/format_number :vars/;
7 use POSIX qw/ceil/;
8 use List::Util qw/sum/;
9  
10 use 5.010;
11  
12 use base 'Exporter';
13 our ( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
14 our $VERSION = '0.062'; # VERSION
15  
16 @EXPORT = qw();
17 %EXPORT_TAGS = ();
18 @EXPORT_OK = qw(hist_text nstat);
19  
20 my $nfmt_full = new Number::Format(
21 -thousands_sep => ',',
22 -decimal_point => '.',
23 -decimal_fill => 1
24 );
25  
26 my $nfmt_nice = new Number::Format(
27 -thousands_sep => ',',
28 -decimal_point => '.',
29 -decimal_fill => 0
30 );
31  
32 sub log10 {
33 my $n = shift;
34 return log($n) / log(10);
35 }
36  
37 sub hist_text {
38 my $v = shift;
39 my $c = shift;
40 my $stat = Statistics::Descriptive::Full->new();
41  
42 my $nf_nice = gen_number_formatter( $nfmt_nice, $c );
43 my $nf_full = gen_number_formatter( $nfmt_full, $c );
44  
45 if ( $c->{log10} ) {
46 $stat->add_data( map { log10($_) } @$v );
47 } else {
48 $stat->add_data(@$v);
49 }
50 my $nclass = $c->{breaks} || nclass_sturges($stat);
51  
52 my $fd = $stat->frequency_distribution_ref( $nclass + 1 );
53  
54 my $max_len = 60;
55 my $count = $stat->count;
56  
57 my @keys = sort { $a <=> $b } keys %$fd;
58  
59 my @res;
60 my $longest_interval = -1;
61 my $longest_cnt = -1;
62 for my $i ( 0 .. $#keys ) {
63 my $last = $i == 0 ? "-inf" : $keys[ $i - 1 ];
64 my $curr = $keys[$i];
65 my $kcnt = $fd->{$curr};
66 next if ( $kcnt == 0 && $c->{skip_empty} );
67 my $len = sprintf( "%.0f", ( $kcnt * $max_len / $count ) );
68 push @res, [ $nf_full->($curr), "$kcnt", "#" x $len ];
69 $longest_interval = length( $res[-1][0] ) if ( length( $res[-1][0] ) > $longest_interval );
70 $longest_cnt = length( $res[-1][1] ) if ( length( $res[-1][1] ) > $longest_cnt );
71 }
72  
73 my $res;
74 $res .= "median: " . $nf_nice->( $stat->median, 2, 0 ) . "\n";
75 $res .= "count: " . $nfmt_nice->format_number( $stat->count, 0, 0 ) . "\n";
76 $res .= "5num: "
77 . join( " ",
78 $nf_nice->( $stat->quantile(0) ),
79 $nf_nice->( $stat->quantile(1) ),
80 '>' . $nf_nice->( $stat->quantile(2) ) . '<',
81 $nf_nice->( $stat->quantile(3) ),
82 $nf_nice->( $stat->quantile(4) ),
83 ) . "\n";
84 $res .= "\n";
85 for my $r (@res) {
86 $res .= sprintf( "<= %${longest_interval}s (%${longest_cnt}s) %s\n", @$r );
87 }
88 return $res;
89 }
90  
91 sub gen_number_formatter {
92 my $nf = shift;
93 my $c = shift;
94 return sub {
95 my ( $value, @rest ) = @_;
96  
97 $value = 10**$value if ( $c->{log10} );
98 return $nf->format_number( $value, @rest );
99 };
100 }
101  
102 sub nclass_fd {
103 my ($stat) = @_;
104 my $iqr = $stat->quantile(3) - $stat->quantile(1);
105 $iqr = 1 unless ($iqr);
106 return ceil( $stat->sample_range / ( ( $stat->count()**( -1 / 3 ) ) * 2 * $iqr ) );
107 }
108  
109 sub nclass_sturges {
110 my ($stat) = @_;
111 return ceil( log( $stat->count ) / log(2) + 1 );
112 }
113  
114 sub nstat {
115 my $frac = shift;
116 my $values = shift;
117 my $min_size = shift;
118  
119 my $total;
120 if ($min_size) {
121 $total = sum grep { $_ >= $min_size } @$values;
122 } else {
123 $total = sum @$values;
124 }
125  
126 my $sum = 0;
127 my $n = 0;
128 for ( sort { $b <=> $a } @$values ) {
129 $sum += $_;
130 $n++;
131 if ( $sum >= $total * $frac ) {
132 return wantarray ? ( $_, $n, $total ) : $_;
133 }
134 }
135 return;
136 }