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.212670';
4 17     17   135 use Moose;
  17         47  
  17         144  
5 17     17   124716 use namespace::autoclean;
  17         49  
  17         213  
6              
7 17     17   1823 use autodie;
  17         45  
  17         164  
8 17     17   93894 use feature qw(say);
  17         69  
  17         1633  
9              
10             # use Smart::Comments;
11              
12 17     17   129 use Statistics::Descriptive;
  17         69  
  17         587  
13 17     17   134 use Tie::IxHash;
  17         40  
  17         593  
14              
15 17     17   115 use Bio::MUST::Core::Types;
  17         48  
  17         517  
16 17     17   120 use aliased 'Bio::MUST::Core::PostPred::Composition';
  17         38  
  17         157  
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         8 tie my %zscore_for, 'Tie::IxHash';
56              
57             # loop through all ids (including $GLOBAL id if any)
58 1         58 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         1806 my ($obs, @sims) = @{ $self->stats_for($id) };
  81         3225  
63 81         1237 my $stat = Statistics::Descriptive::Full->new();
64 81         5516 $stat->add_data(@sims);
65 81         13287 my $mean = $stat->mean;
66 81         848 my $sd = $stat->standard_deviation;
67              
68             # compute and store zscore
69 81         3346 my $zscore = ($obs - $mean) / $sd;
70 81         328 $zscore_for{$id} = $zscore;
71             }
72              
73 1         70 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 31 return shift->_make_test(Composition, @_);
84             }
85              
86              
87             sub _make_test {
88 1     1   2 my $class = shift;
89 1         3 my $type = shift;
90 1         3 my $alis = shift;
91              
92 1         11 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         17 for my $ali ( @{$alis} ) {
  1         2  
97              
98             # get test statistics for Ali
99 51         1974 my $test = $type->new( seqs => $ali );
100              
101             # accumulate test statistics by seq
102 4131         48577 push @{ $stats_for{$_} }, $test->stat_for($_)
103 51         2133 for $test->all_ids;
104             }
105              
106 1         38 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.212670
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