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.212650';
4 17     17   126 use Moose;
  17         55  
  17         148  
5 17     17   122252 use namespace::autoclean;
  17         51  
  17         172  
6              
7 17     17   1557 use autodie;
  17         45  
  17         165  
8 17     17   93797 use feature qw(say);
  17         52  
  17         1627  
9              
10             # use Smart::Comments;
11              
12 17     17   139 use Statistics::Descriptive;
  17         44  
  17         513  
13 17     17   138 use Tie::IxHash;
  17         48  
  17         548  
14              
15 17     17   137 use Bio::MUST::Core::Types;
  17         51  
  17         482  
16 17     17   106 use aliased 'Bio::MUST::Core::PostPred::Composition';
  17         41  
  17         142  
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   4 my $self = shift;
54              
55 1         9 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         1935 my ($obs, @sims) = @{ $self->stats_for($id) };
  81         3280  
63 81         1645 my $stat = Statistics::Descriptive::Full->new();
64 81         5678 $stat->add_data(@sims);
65 81         13686 my $mean = $stat->mean;
66 81         482 my $sd = $stat->standard_deviation;
67              
68             # compute and store zscore
69 81         3638 my $zscore = ($obs - $mean) / $sd;
70 81         373 $zscore_for{$id} = $zscore;
71             }
72              
73 1         99 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 27 return shift->_make_test(Composition, @_);
84             }
85              
86              
87             sub _make_test {
88 1     1   3 my $class = shift;
89 1         2 my $type = shift;
90 1         2 my $alis = shift;
91              
92 1         15 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         24 for my $ali ( @{$alis} ) {
  1         4  
97              
98             # get test statistics for Ali
99 51         1972 my $test = $type->new( seqs => $ali );
100              
101             # accumulate test statistics by seq
102 4131         49934 push @{ $stats_for{$_} }, $test->stat_for($_)
103 51         2256 for $test->all_ids;
104             }
105              
106 1         55 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.212650
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