File Coverage

blib/lib/Bio/MUST/Core/SeqMask/Freqs.pm
Criterion Covered Total %
statement 61 61 100.0
branch 2 2 100.0
condition 3 4 75.0
subroutine 10 10 100.0
pod 1 1 100.0
total 77 78 98.7


line stmt bran cond sub pod time code
1             package Bio::MUST::Core::SeqMask::Freqs;
2             # ABSTRACT: Arbitrary frequencies for sequence sites
3             $Bio::MUST::Core::SeqMask::Freqs::VERSION = '0.212530';
4 17     17   13244 use Moose;
  17         45  
  17         136  
5 17     17   123612 use namespace::autoclean;
  17         53  
  17         159  
6              
7 17     17   1586 use autodie;
  17         43  
  17         168  
8 17     17   92606 use feature qw(say);
  17         64  
  17         1611  
9              
10 17     17   150 use List::AllUtils qw(sum);
  17         58  
  17         1283  
11              
12 17     17   126 use Bio::MUST::Core::Types;
  17         45  
  17         572  
13 17     17   130 use aliased 'Bio::MUST::Core::SeqMask::Rates';
  17         61  
  17         152  
14              
15              
16             # public hash containing freqs by sequence and site
17             # Note: this hash is actually a Tie::IxHash (see Profiles)
18             has 'freq_for_at' => (
19             traits => ['Hash'],
20             is => 'ro',
21             isa => 'HashRef[ArrayRef[Num]]',
22             required => 1,
23             handles => {
24             count_freqs_at => 'count',
25             all_freqs_at => 'values',
26             freqs_at_for => 'get',
27             all_ids => 'keys',
28             },
29             );
30              
31              
32             # private SeqMask::Rates-like object derived by averaging freqs over seqs
33             has '_mask' => (
34             is => 'ro',
35             isa => 'Bio::MUST::Core::SeqMask::Rates',
36             init_arg => undef,
37             lazy => 1,
38             builder => '_build_mask',
39             handles => {
40             mask_len => 'mask_len',
41             all_freqs => 'all_states',
42             min_freq => 'min_rate',
43             max_freq => 'max_rate',
44             bin_freqs_masks => 'bin_rates_masks',
45             freqs_mask => 'rates_mask',
46             },
47             );
48              
49              
50             # private hash containing freqs averaged over sites
51             has '_avg_freq_for' => (
52             traits => ['Hash'],
53             is => 'ro',
54             isa => 'HashRef[Num]',
55             init_arg => undef,
56             lazy => 1,
57             builder => '_build_avg_freq_for',
58             handles => {
59             avg_freq_for => 'get',
60             },
61             );
62              
63              
64             ## no critic (ProhibitUnusedPrivateSubroutines)
65              
66             sub _build_mask {
67 3     3   9 my $self = shift;
68              
69 3         8 my @mask;
70              
71             # average freqs over seqs
72 3         134 for my $freqs_at ($self->all_freqs_at) {
73 161         1176 my $i = 0;
74 161         164 $mask[$i++] += $_ for @{$freqs_at};
  161         5128  
75             }
76 3         268 my $n = $self->count_freqs_at;
77 3         456 @mask = map { $_ / $n } @mask;
  555         876  
78              
79 3         166 return Rates->new( mask => \@mask );
80             }
81              
82              
83             sub _build_avg_freq_for {
84 2     2   6 my $self = shift;
85              
86 2         5 my %avg_freq_for;
87              
88             # average freqs over sites
89 2         14 my $n = $self->mask_len;
90 2         74 for my $id ($self->all_ids) {
91 160         2504 $avg_freq_for{$id} = sum( @{ $self->freqs_at_for($id) } ) / $n;
  160         5124  
92             }
93              
94 2         139 return \%avg_freq_for;
95             }
96              
97             ## use critic
98              
99              
100              
101             sub store {
102 3     3 1 12 my $self = shift;
103 3         7 my $outfile = shift;
104 3   50     12 my $args = shift // {}; # HashRef (should not be empty...)
105              
106 3   100     19 my $reorder = $args->{reorder} // 0;
107              
108 3         22 open my $out, '>', $outfile;
109              
110             # optionally sort ids by descending average freq (over sites)
111 3         2948 my @ids = $self->all_ids;
112             @ids = sort {
113 3 100       830 $self->avg_freq_for($b) <=> $self->avg_freq_for($a)
  411         12727  
114             } @ids if $reorder;
115              
116             # output header
117 3         6 say {$out} join "\t", 'site', 'f(i,.)', @ids;
  3         64  
118              
119             # output average freqs (over sites)
120 3         9 say {$out} join "\t", 'f(.,j)', q{}, map { $self->avg_freq_for($_) } @ids;
  3         11  
  240         7462  
121              
122             # setup rows with site numbers and average freqs (over seqs)
123 3         39 my @rows = 1..$self->mask_len;
124 3         23 my @avg_freqs_at = $self->all_freqs;
125 3         800 $_ .= "\t" . shift @avg_freqs_at for @rows;
126              
127             # assemble freqs by site (one site by row)
128 3         10 for my $id (@ids) {
129 240         373 my @freqs_at = @{ $self->freqs_at_for($id) };
  240         8844  
130 240         45662 $_ .= "\t" . shift @freqs_at for @rows;
131             }
132              
133             # output freqs for all sites
134 3         14 say {$out} $_ for @rows;
  555         1907  
135              
136 3         265 return;
137             }
138              
139             __PACKAGE__->meta->make_immutable;
140             1;
141              
142             __END__
143              
144             =pod
145              
146             =head1 NAME
147              
148             Bio::MUST::Core::SeqMask::Freqs - Arbitrary frequencies for sequence sites
149              
150             =head1 VERSION
151              
152             version 0.212530
153              
154             =head1 SYNOPSIS
155              
156             # TODO
157              
158             =head1 DESCRIPTION
159              
160             # TODO
161              
162             =head1 METHODS
163              
164             =head2 store
165              
166             =head1 AUTHOR
167              
168             Denis BAURAIN <denis.baurain@uliege.be>
169              
170             =head1 COPYRIGHT AND LICENSE
171              
172             This software is copyright (c) 2013 by University of Liege / Unit of Eukaryotic Phylogenomics / Denis BAURAIN.
173              
174             This is free software; you can redistribute it and/or modify it under
175             the same terms as the Perl 5 programming language system itself.
176              
177             =cut