| 1 | package Bio::MUST::Core::PostPred::Composition; | ||||||||||||
| 2 | # ABSTRACT: Posterior predictive test for compositional bias | ||||||||||||
| 3 | 18 | $Bio::MUST::Core::PostPred::Composition::VERSION = '0.252040'; | |||||||||||
| 4 | 18 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 18 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 18 | ▶ | use autodie; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 18 | ▶ | use feature qw(say); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | # use Smart::Comments; | ||||||||||||
| 11 | |||||||||||||
| 12 | 18 | ▶ | use List::AllUtils qw(sum); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 18 | ▶ | use Tie::IxHash; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 18 | ▶ | use Bio::MUST::Core::Types; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | 18 | has 'seqs' => ( | |||||||||||
| 19 | is => 'ro', | ||||||||||||
| 20 | isa => 'Bio::MUST::Core::Ali', | ||||||||||||
| 21 | required => 1, | ||||||||||||
| 22 | coerce => 1, | ||||||||||||
| 23 | handles => [ | ||||||||||||
| 24 | qw(gapmiss_regex all_seqs) | ||||||||||||
| 25 | ], | ||||||||||||
| 26 | ); | ||||||||||||
| 27 | |||||||||||||
| 28 | # TODO: consider a role if more tests are implemented | ||||||||||||
| 29 | |||||||||||||
| 30 | # private hash containing compositional biases | ||||||||||||
| 31 | # Note: this hash is actually a Tie::IxHash (see builder) | ||||||||||||
| 32 | 18 | has '_stat_for' => ( | |||||||||||
| 33 | traits => ['Hash'], | ||||||||||||
| 34 | is => 'ro', | ||||||||||||
| 35 | isa => 'HashRef[Num]', | ||||||||||||
| 36 | init_arg => undef, | ||||||||||||
| 37 | lazy => 1, | ||||||||||||
| 38 | builder => '_build_stat_for', | ||||||||||||
| 39 | handles => { | ||||||||||||
| 40 | all_ids => 'keys', | ||||||||||||
| 41 | stat_for => 'get', | ||||||||||||
| 42 | }, | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||
| 46 | |||||||||||||
| 47 | 51 | ▶ | sub _build_stat_for { | ||||||||||
|
Subroutine
_build_stat_for: called CC=9 SCAR=22.0
| |||||||||||||
| 48 | 51 | my $self = shift; | |||||||||||
| 49 | |||||||||||||
| 50 | 51 | my %glb_freq_for; | |||||||||||
| 51 | 51 | tie my %seq_freq_for, 'Tie::IxHash'; | |||||||||||
| 52 | |||||||||||||
| 53 | 51 | my $regex = $self->gapmiss_regex; | |||||||||||
| 54 | 51 | my $glb_tot = 0; | |||||||||||
| 55 | |||||||||||||
| 56 | # loop through seqs to store state freqs | ||||||||||||
| 57 | 51 | for my $seq ($self->all_seqs) { | |||||||||||
| 58 | 4029 | my %freq_for; | |||||||||||
| 59 | 4029 | my $seq_tot = 0; | |||||||||||
| 60 | |||||||||||||
| 61 | STATE: | ||||||||||||
| 62 | 4029 | for my $state ($seq->all_states) { | |||||||||||
| 63 | 745365 | $state = uc $state; | |||||||||||
| 64 | |||||||||||||
| 65 | # skip missing/gap states | ||||||||||||
| 66 | # Note: This is different from what we do in SeqMask::Profiles | ||||||||||||
| 67 | # so as to avoid these states to decrease regular state freqs | ||||||||||||
| 68 | 745365 | ▶ | next STATE if $state =~ m/$regex/xms; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | |||||||||||||
| 70 | # store state occurrences both for current seq and globally | ||||||||||||
| 71 | 744035 | $glb_freq_for{$state}++; | |||||||||||
| 72 | 744035 | $freq_for{$state}++; | |||||||||||
| 73 | 744035 | $glb_tot++; | |||||||||||
| 74 | 744035 | $seq_tot++; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | # convert occurrences to freqs for current seq | ||||||||||||
| 78 | 4029 | $freq_for{$_} /= $seq_tot for keys %freq_for; | |||||||||||
| 79 | |||||||||||||
| 80 | # store freqs for current seq | ||||||||||||
| 81 | 4029 | $seq_freq_for{ $seq->full_id } = \%freq_for; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | # convert global occurrences to freqs | ||||||||||||
| 85 | 51 | $glb_freq_for{$_} /= $glb_tot for keys %glb_freq_for; | |||||||||||
| 86 | |||||||||||||
| 87 | 51 | tie my %bias_for, 'Tie::IxHash'; | |||||||||||
| 88 | |||||||||||||
| 89 | # compute bias for each seq | ||||||||||||
| 90 | # according to Blanquart and Lartillot 2008 | ||||||||||||
| 91 | 51 | for my $id (keys %seq_freq_for) { | |||||||||||
| 92 | 4029 | ▶ | while (my ($aa, $freq) = each %glb_freq_for) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 79237 | ▶ | $bias_for{$id} += ( ($seq_freq_for{$id}{$aa} // 0) - $freq ) ** 2; | ||||||||||
|
Condition100%2/2
$seq_freq_for{$id}{$aa} // 0
MC/DC100%1/1
$seq_freq_for{$id}{$aa} // 0
$seq_freq_for{$id}{$aa}
Truth table100%2/2
A
A= $seq_freq_for{$id}{$aa}
| |||||||||||||
| 94 | } | ||||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | # compute global biases | ||||||||||||
| 98 | 51 | $bias_for{'GLOBALMAX'} = List::AllUtils::max( values %bias_for ); | |||||||||||
| 99 | 51 | $bias_for{'GLOBALMEAN'} = sum( values %bias_for ) / keys %bias_for; | |||||||||||
| 100 | |||||||||||||
| 101 | 51 | return \%bias_for; | |||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | ## use critic | ||||||||||||
| 105 | |||||||||||||
| 106 | 18 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 107 | 18 | 1; | |||||||||||
| 108 | |||||||||||||
| 109 | __END__ | ||||||||||||