File Coverage

blib/lib/Bio/MUST/Core/PostPred.pm
Criterion Covered Total %
statement 47 47 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 59 59 100.0


line stmt bran cond sub pod time code
1             package Bio::MUST::Core::PostPred;
2             # ABSTRACT: Posterior predictive tests for sequences
3             $Bio::MUST::Core::PostPred::VERSION = '0.212530';
4 17     17   143 use Moose;
  17         55  
  17         155  
5 17     17   123819 use namespace::autoclean;
  17         52  
  17         192  
6              
7 17     17   1741 use autodie;
  17         52  
  17         210  
8 17     17   95541 use feature qw(say);
  17         51  
  17         1742  
9              
10             # use Smart::Comments;
11              
12 17     17   173 use Statistics::Descriptive;
  17         60  
  17         642  
13 17     17   117 use Tie::IxHash;
  17         42  
  17         601  
14              
15 17     17   151 use Bio::MUST::Core::Types;
  17         61  
  17         610  
16 17     17   106 use aliased 'Bio::MUST::Core::PostPred::Composition';
  17         64  
  17         172  
17              
18              
19             # private hash containing test statistics by sequence (and globally)
20             # Note: this hash is actually a Tie::IxHash (see factory methods)
21             has '_stats_for' => (
22             traits => ['Hash'],
23             is => 'ro',
24             isa => 'HashRef[ArrayRef[Num]]',
25             required => 1,
26             handles => {
27             all_ids => 'keys',
28             stats_for => 'get',
29             },
30             );
31              
32              
33             # private hash containing test Z-scores by sequence (and globally)
34             # Note: this hash is actually a Tie::IxHash (see builder)
35             has '_zscore_for' => (
36             traits => ['Hash'],
37             is => 'ro',
38             isa => 'HashRef[Num]',
39             init_arg => undef,
40             lazy => 1,
41             builder => '_build_zscore_for',
42             handles => {
43             zscore_for => 'get',
44             },
45             );
46              
47             ## no critic (ProhibitUnusedPrivateSubroutines)
48              
49             # TODO: switch to BUILD to compute p-values as well
50             # TODO: explore other Statistics:: modules on CPAN
51              
52             sub _build_zscore_for {
53 1     1   3 my $self = shift;
54              
55 1         11 tie my %zscore_for, 'Tie::IxHash';
56              
57             # loop through all ids (including $GLOBAL id if any)
58 1         64 for my $id ($self->all_ids) {
59              
60             # compute std-dev from simulated stat dist
61             # Note: the first value MUST BE the observed test stat
62 81         1974 my ($obs, @sims) = @{ $self->stats_for($id) };
  81         3315  
63 81         1733 my $stat = Statistics::Descriptive::Full->new();
64 81         5694 $stat->add_data(@sims);
65 81         13615 my $mean = $stat->mean;
66 81         512 my $sd = $stat->standard_deviation;
67              
68             # compute and store zscore
69 81         3421 my $zscore = ($obs - $mean) / $sd;
70 81         370 $zscore_for{$id} = $zscore;
71             }
72              
73 1         94 return \%zscore_for;
74             }
75              
76             ## use critic
77              
78              
79             # PostPred factory methods
80              
81              
82             sub comp_test { ## no critic (RequireArgUnpacking)
83 1     1 1 45 return shift->_make_test(Composition, @_);
84             }
85              
86              
87             sub _make_test {
88 1     1   3 my $class = shift;
89 1         3 my $type = shift;
90 1         4 my $alis = shift;
91              
92 1         16 tie my %stats_for, 'Tie::IxHash';
93              
94             # loop through Ali objects to compute the required test statistic
95             # Note: the first Ali MUST BE the real one and the others the simulations
96 1         25 for my $ali ( @{$alis} ) {
  1         4  
97              
98             # get test statistics for Ali
99 51         2059 my $test = $type->new( seqs => $ali );
100              
101             # accumulate test statistics by seq
102 4131         49325 push @{ $stats_for{$_} }, $test->stat_for($_)
103 51         2148 for $test->all_ids;
104             }
105              
106 1         48 return $class->new( _stats_for => \%stats_for );
107             }
108              
109             __PACKAGE__->meta->make_immutable;
110             1;
111              
112             __END__
113              
114             =pod
115              
116             =head1 NAME
117              
118             Bio::MUST::Core::PostPred - Posterior predictive tests for sequences
119              
120             =head1 VERSION
121              
122             version 0.212530
123              
124             =head1 SYNOPSIS
125              
126             # TODO
127              
128             =head1 DESCRIPTION
129              
130             # TODO
131              
132             =head1 METHODS
133              
134             =head2 comp_test
135              
136             =head1 AUTHOR
137              
138             Denis BAURAIN <denis.baurain@uliege.be>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut