| 1 | package Bio::MUST::Core::SeqMask::Freqs; | ||||||||||||
| 2 | # ABSTRACT: Arbitrary frequencies for sequence sites | ||||||||||||
| 3 | 18 | $Bio::MUST::Core::SeqMask::Freqs::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 | 18 | ▶ | use Const::Fast; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 18 | ▶ | use List::AllUtils qw(sum); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 18 | ▶ | use Bio::MUST::Core::Types; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 18 | ▶ | use aliased 'Bio::MUST::Core::SeqMask::Rates'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | |||||||||||||
| 17 | # public hash containing freqs by sequence and site | ||||||||||||
| 18 | # Note: this hash is actually a Tie::IxHash (see Profiles) | ||||||||||||
| 19 | 18 | has 'freq_for_at' => ( | |||||||||||
| 20 | traits => ['Hash'], | ||||||||||||
| 21 | is => 'ro', | ||||||||||||
| 22 | isa => 'HashRef[ArrayRef[Num]]', | ||||||||||||
| 23 | required => 1, | ||||||||||||
| 24 | handles => { | ||||||||||||
| 25 | count_freqs_at => 'count', | ||||||||||||
| 26 | all_freqs_at => 'values', | ||||||||||||
| 27 | freqs_at_for => 'get', | ||||||||||||
| 28 | all_ids => 'keys', | ||||||||||||
| 29 | }, | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | |||||||||||||
| 33 | # private SeqMask::Rates-like object derived by averaging freqs over seqs | ||||||||||||
| 34 | 18 | has '_mask' => ( | |||||||||||
| 35 | is => 'ro', | ||||||||||||
| 36 | isa => 'Bio::MUST::Core::SeqMask::Rates', | ||||||||||||
| 37 | init_arg => undef, | ||||||||||||
| 38 | lazy => 1, | ||||||||||||
| 39 | builder => '_build_mask', | ||||||||||||
| 40 | handles => { | ||||||||||||
| 41 | mask_len => 'mask_len', | ||||||||||||
| 42 | all_freqs => 'all_states', | ||||||||||||
| 43 | min_freq => 'min_rate', | ||||||||||||
| 44 | max_freq => 'max_rate', | ||||||||||||
| 45 | bin_freqs_masks => 'bin_rates_masks', | ||||||||||||
| 46 | freqs_mask => 'rates_mask', | ||||||||||||
| 47 | }, | ||||||||||||
| 48 | ); | ||||||||||||
| 49 | |||||||||||||
| 50 | |||||||||||||
| 51 | # private hash containing freqs averaged over sites | ||||||||||||
| 52 | 18 | has '_avg_freq_for' => ( | |||||||||||
| 53 | traits => ['Hash'], | ||||||||||||
| 54 | is => 'ro', | ||||||||||||
| 55 | isa => 'HashRef[Num]', | ||||||||||||
| 56 | init_arg => undef, | ||||||||||||
| 57 | lazy => 1, | ||||||||||||
| 58 | builder => '_build_avg_freq_for', | ||||||||||||
| 59 | handles => { | ||||||||||||
| 60 | avg_freq_for => 'get', | ||||||||||||
| 61 | }, | ||||||||||||
| 62 | ); | ||||||||||||
| 63 | |||||||||||||
| 64 | 18 | const my $PREC => 3; | |||||||||||
| 65 | |||||||||||||
| 66 | ## no critic (ProhibitUnusedPrivateSubroutines) | ||||||||||||
| 67 | |||||||||||||
| 68 | 3 | ▶ | sub _build_mask { | ||||||||||
|
Subroutine
_build_mask: called CC=3 SCAR=11.0
| |||||||||||||
| 69 | 3 | my $self = shift; | |||||||||||
| 70 | |||||||||||||
| 71 | 3 | my @mask; | |||||||||||
| 72 | |||||||||||||
| 73 | # average freqs over seqs | ||||||||||||
| 74 | 3 | for my $freqs_at ($self->all_freqs_at) { | |||||||||||
| 75 | 161 | my $i = 0; | |||||||||||
| 76 | 161 | $mask[$i++] += $_ for @{$freqs_at}; | |||||||||||
| 77 | } | ||||||||||||
| 78 | 3 | my $n = $self->count_freqs_at; | |||||||||||
| 79 | 3 | @mask = map { $_ / $n } @mask; | |||||||||||
| 80 | |||||||||||||
| 81 | 3 | return Rates->new( mask => \@mask ); | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | |||||||||||||
| 85 | 2 | ▶ | sub _build_avg_freq_for { | ||||||||||
|
Subroutine
_build_avg_freq_for: called CC=2 SCAR=6.9
| |||||||||||||
| 86 | 2 | my $self = shift; | |||||||||||
| 87 | |||||||||||||
| 88 | 2 | my %avg_freq_for; | |||||||||||
| 89 | |||||||||||||
| 90 | # average freqs over sites | ||||||||||||
| 91 | 2 | my $n = $self->mask_len; | |||||||||||
| 92 | 2 | for my $id ($self->all_ids) { | |||||||||||
| 93 | 160 | $avg_freq_for{$id} = sum( @{ $self->freqs_at_for($id) } ) / $n; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 2 | return \%avg_freq_for; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | ## use critic | ||||||||||||
| 100 | |||||||||||||
| 101 | |||||||||||||
| 102 | |||||||||||||
| 103 | 3 | ▶ | sub store { | ||||||||||
|
Subroutine
store: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||
| 104 | 3 | my $self = shift; | |||||||||||
| 105 | 3 | my $outfile = shift; | |||||||||||
| 106 | 3 | ▶ | my $args = shift // {}; # HashRef (should not be empty...) | ||||||||||
|
Condition50%1/2
shift() // {}
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 107 | |||||||||||||
| 108 | 3 | ▶ | my $reorder = $args->{reorder} // 0; | ||||||||||
|
Condition100%2/2
$args->{"reorder"} // 0
MC/DC100%1/1
$args->{"reorder"} // 0
$args->{"reorder"}
Truth table100%2/2
A
A= $args->{"reorder"}
| |||||||||||||
| 109 | |||||||||||||
| 110 | 3 | open my $out, '>', $outfile; | |||||||||||
| 111 | |||||||||||||
| 112 | # optionally sort ids by descending average freq (over sites) | ||||||||||||
| 113 | 3 | my @ids = $self->all_ids; | |||||||||||
| 114 | @ids = sort { | ||||||||||||
| 115 | 3 | ▶ | $self->avg_freq_for($b) <=> $self->avg_freq_for($a) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 116 | } @ids if $reorder; | ||||||||||||
| 117 | |||||||||||||
| 118 | # output header | ||||||||||||
| 119 | 3 | say {$out} join "\t", 'site', 'f(i,.)', @ids; | |||||||||||
| 120 | |||||||||||||
| 121 | # output average freqs (over sites) | ||||||||||||
| 122 | 3 | say {$out} join "\t", 'f(.,j)', q{}, | |||||||||||
| 123 | 3 | map { sprintf "%.${PREC}f", $self->avg_freq_for($_) } @ids; | |||||||||||
| 124 | |||||||||||||
| 125 | # setup rows with site numbers and average freqs (over seqs) | ||||||||||||
| 126 | 3 | my @rows = 1..$self->mask_len; | |||||||||||
| 127 | 3 | my @avg_freqs_at = map { sprintf "%.${PREC}f", $_ } $self->all_freqs; | |||||||||||
| 128 | 3 | $_ .= "\t" . shift @avg_freqs_at for @rows; | |||||||||||
| 129 | |||||||||||||
| 130 | # assemble freqs by site (one site by row) | ||||||||||||
| 131 | 3 | for my $id (@ids) { | |||||||||||
| 132 | my @freqs_at | ||||||||||||
| 133 | 240 | = map { sprintf "%.${PREC}f", $_ } @{ $self->freqs_at_for($id) }; | |||||||||||
| 134 | 240 | $_ .= "\t" . shift @freqs_at for @rows; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | # output freqs for all sites | ||||||||||||
| 138 | 3 | say {$out} $_ for @rows; | |||||||||||
| 139 | |||||||||||||
| 140 | 3 | close $out; | |||||||||||
| 141 | |||||||||||||
| 142 | 3 | return; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | 18 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 146 | 18 | 1; | |||||||||||
| 147 | |||||||||||||
| 148 | __END__ | ||||||||||||