lib/Bio/MUST/Core/SeqMask/Profiles.pm

filter: Statementstmt 100% 102 / 102 Branchbran 94.4% 17 / 18 Conditioncond 90.0% 9 / 10 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 98.0% 148 / 151 CC 23 SCAR 18.3
CC
23
Cov
98%
ppred_freqs
9.0
store
8.0
ppred_profiles
8.0
CRAP 6.3
« lib/Bio/MUST/Core/Taxonomy/Category.pm ↑ Summary lib/Bio/MUST/Core/Ali.pm »
1 package Bio::MUST::Core::SeqMask::Profiles;
2 # ABSTRACT: Evolutionary profiles for sequence sites
3 18 $Bio::MUST::Core::SeqMask::Profiles::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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 extends 'Bio::MUST::Core::SeqMask';
15  
16 18 ▶use Bio::MUST::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 18 ▶use Bio::MUST::Core::Constants qw(:files);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 18 ▶use aliased 'Bio::MUST::Core::SeqMask::Freqs';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 # override superclass' Bool type
22 # Note: mask indices are as follow: [site]{full_id}{AA}
23 # mask values are AA freqs (both per seq and averaged over seqs)
24 18 has '+mask' => (
25 isa => 'ArrayRef[HashRef[HashRef[Num]]]',
26 );
27  
28 # TODO: mask non-applicable methods from superclass? (Liskov principle)
29 # TODO: move this under PostPred instead of SeqMask?
30  
31 18 const my $AVERAGE => '<:AVERAGE:>';
32 18 const my $PREC => 3;
33  
34  
35 2 ▶sub ppred_profiles {
Subroutine ppred_profiles: called CC=8 SCAR=20.8
Pod documented
36 2 my $class = shift;
37 2 my $alis = shift;
38 2 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition100%2/2
shift() // {}
l!l
11
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
39  
40 2 my $list = $args->{sim_list};
41  
42 2 my @sim_freq_at_for;
43  
44 my $regex;
45 2 my $width;
46 2 my $seq_inc;
47 2 my $avg_inc;
48  
49 # loop through Ali objects to build site profiles
50 # Note: profiles will be available both per seq and averaged over seqs
51 2 for my $ali ( @{$alis} ) {
52  
53 # extract seqs on which to compute freqs (defaults to all seqs)
54 100 ▶ my $sample = $list ? $list->filtered_ali($ali) : $ali;
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 50 50 100
55 100 my @seqs = $sample->all_seqs;
56  
57 # setup mask details based on first Ali
58 100 ▶ unless ($regex) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($regex) 2 98 100
59 2 $regex = $ali->gapmiss_regex;
60 2 $width = $ali->width;
61 2 $seq_inc = 1.0 / @{$alis};
62 2 $avg_inc = $seq_inc / @seqs;
63 }
64  
65 # loop through simulated seqs to store and average ppred state freqs
66 100 for my $seq (@seqs) {
67 4000 my $full_id = $seq->full_id;
68  
69 # store and average ppred state freq at each site for current seq
70 # Note: all missing/gap states are folded to '*'
71 4000 for my $site (0..$width-1) {
72 740000 my $sim_state = uc $seq->state_at($site);
73 740000 ▶ $sim_state = '*' if $sim_state =~ m/$regex/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $sim_state =~ /$regex/msx 0 740000 740000
74 740000 $sim_freq_at_for[$site]{$full_id}{$sim_state} += $seq_inc;
75 740000 $sim_freq_at_for[$site]{$AVERAGE}{$sim_state} += $avg_inc;
76 }
77 }
78 }
79  
80 2 return $class->new( mask => \@sim_freq_at_for );
81 }
82  
83  
84  
85 3 ▶sub ppred_freqs {
Subroutine ppred_freqs: called CC=9 SCAR=22.0
Pod documented
86 3 my $self = shift;
87 3 my $ali = shift;
88 3 ▶ my $args = shift // {}; # HashRef (should not be empty...)
Condition100%2/2
shift() // {}
l!l
21
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89  
90 3 ▶ my $by_seq = $args->{by_seq} // 0;
Condition100%2/2
$args->{"by_seq"} // 0
l!l
12
MC/DC100%1/1
$args->{"by_seq"} // 0
$args->{"by_seq"}
Truth table100%2/2
A
A= $args->{"by_seq"}
Aresult
11
00
91 3 my $list = $args->{obs_list};
92  
93 # ppred_freqs_by_seq
94 # input: f_AA(site,seq)
95 # output: f_AAobs(site,seq)
96 # => mask f_AAobs_avg(site) over seq
97 # => seq sort f_AAobs_avg(seq) over sites
98  
99 # ppred_freqs
100 # input: f_AA_avg(site) over seq
101 # output: f_AAobs(site,seq)
102 # => mask f_AAobs_avg(site) over seq
103 # => seq sort f_AAobs_avg(seq) over sites
104  
105 3 tie my %obs_freq_for_at, 'Tie::IxHash';
106  
107 # extract seqs on which to compute freqs (defaults to all seqs)
108 3 ▶ my $sample = $list ? $list->filtered_ali($ali) : $ali;
Branch: 1 branch
BranchTrueFalseTotal
$list ? : 1 2 3
109 3 my @seqs = $sample->all_seqs;
110  
111 # setup mask details
112 3 my $regex = $ali->gapmiss_regex;
113 3 my $width = $ali->width;
114  
115 # loop through real seqs to store observed state freqs
116 # Note: ppred state freqs are either by seq or averaged over seqs
117 3 for my $seq (@seqs) {
118 161 my $full_id = $seq->full_id;
119 161 ▶ my $seq_key = $by_seq ? $full_id : $AVERAGE;
Branch: 1 branch
BranchTrueFalseTotal
$by_seq ? : 80 81 161
120  
121 # store observed state freq at each site for current seq
122 # Note: sites with missing/gap state get a max freq of 1.0
123 161 for my $site (0..$width-1) {
124 29785 my $sim_freq_for = ${ $self->state_at($site) }{$seq_key};
125 29785 my $obs_state = uc $seq->state_at($site);
126 my $obs_freq = $obs_state =~ m/$regex/xms ? 1.0
127 29785 ▶ : $sim_freq_for->{$obs_state} // 0.0;
Branch: 1 branch
BranchTrueFalseTotal
$obs_state =~ /$regex/msx ? : 2663 27122 29785
Condition100%2/2
$sim_freq_for->{$obs_state} // 0
l!l
26801321
MC/DC100%1/1
$sim_freq_for->{$obs_state} // 0
$sim_freq_for->{$obs_state}
Truth table100%2/2
A
A= $sim_freq_for->{$obs_state}
Aresult
11
00
128 29785 $obs_freq_for_at{$full_id}[$site] = $obs_freq;
129 }
130 }
131  
132 3 return Freqs->new( freq_for_at => \%obs_freq_for_at );
133 }
134  
135  
136  
137 1 ▶sub store {
Subroutine store: called CC=8 SCAR=20.8
Pod documented
138 1 my $self = shift;
139 1 my $outfile = shift;
140  
141 1 open my $out, '>', $outfile;
142  
143 # output header
144 1 say {$out} join "\t", qw(site seq aa), 'f(i,j)';
145  
146 # setup loop
147 1 my @ids;
148 1 my @aas = split //xms, 'ACDEFGHIKLMNPQRSTVWY*'; # TODO: improve this
149 1 my $width = $self->mask_len;
150  
151 # loop through sites, ids and AAs to output ppred state freqs
152 1 for my $site (0..$width-1) {
153 185 my $sim_freq_for = $self->state_at($site);
154 185 ▶ @ids = sort keys %{ $sim_freq_for // {} } unless @ids;
Branch: 1 branch
BranchTrueFalseTotal
unless @ids 1 184 185
Condition50%1/2
$sim_freq_for // {}
l!l
10
MC/DC0%0/1
$sim_freq_for // {}
$sim_freq_for
Truth table50%1/2
A
A= $sim_freq_for
Aresult
11
00
155 # get ids from first state
156 ID:
157 185 for my $id (@ids) {
158 14800 ▶ next ID if $id eq $AVERAGE; # skip averaged ppred freqs
Branch: 1 branch
BranchTrueFalseTotal
if $id eq $AVERAGE 185 14615 14800
159 14615 for my $aa (@aas) {
160 306915 ▶ say {$out} join "\t", $site + 1, $id, $aa,
Branch: 1 branch
BranchTrueFalseTotal
defined $sim_freq_for->{$id}{$aa} ? : 79157 227758 306915
161 defined $sim_freq_for->{$id}{$aa}
162 306915 ? sprintf "%.${PREC}f", $sim_freq_for->{$id}{$aa} : 0;
163 } # missing AAs are set to 0 (hence the alphabet)
164 }
165 }
166  
167 1 close $out;
168  
169 1 return;
170 }
171  
172 18 __PACKAGE__->meta->make_immutable;
173 18 1;
174  
175 __END__
« lib/Bio/MUST/Core/Taxonomy/Category.pm ↑ Summary lib/Bio/MUST/Core/Ali.pm »