lib/Bio/CUA/CUB/Calculator.pm

filter: Statementstmt 41.4% 99 / 239 Branchbran 34.8% 46 / 132 Conditioncond 17.9% 7 / 39 MC/DCmcdc 12.5% 3 / 24 Subroutinesub 54.5% 12 / 22 Podpod 78.5% 11 / 14 totaltotal 37.8% 178 / 470 SCAR 77.2
CC
93
Cov
37%
estimate_base_composition
210.0
_enc_factory
136.4
expect_codon_freq
110.0
CRAP 2248.1
↑ Summary lib/Bio/CUA/Summarizer.pm »
1 package Bio::CUA::CUB::Calculator;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Bio::CUA::CUB::Calculator -- A module to calculate codon usage bias
8 (CUB) indice for protein-coding sequences
9  
10 =head1 SYNOPSIS
11  
12 use Bio::CUA::CUB::Calculator;
13  
14 my $calc = Bio::CUA::CUB::Calculator->new(
15 -codon_table => 1,
16 -tAI_values => 'tai.out' # from Bio::CUA::CUB::Builder
17 );
18  
19 # calculate tAI for each sequence
20 my $io = Bio::CUA::SeqIO->new(-file => "seqs.fa");
21 or
22 my $io = Bio::CUA::SeqIO->new(-file => "seqs.fa", -format => 'fasta');
23  
24 while(my $seq = $io->next_seq)
25 {
26 my $tai = $calc->tai($seq);
27 printf("%10s: %.7f\n", $seq->id, $tai);
28 }
29  
30 =head1 DESCRIPTION
31  
32 Codon usage bias (CUB) can be represented at two levels, codon and
33 sequence. The latter is often computed as the geometric means of the
34 sequence's codons. This module caculates CUB metrics at sequence
35 level.
36  
37 Supported CUB metrics include CAI (codon adaptation index), tAI (tRNA
38 adaptation index), Fop (Frequency of optimal codons), ENC (Effective
39 Number of Codons) and their variants. See the methods below for
40 details.
41  
42 =cut
43  
44 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use parent qw/Bio::CUA::CUB/;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use Bio::CUA::CodonTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 =head1 METHODS
51  
52 =head2 new
53  
54 Title : new
55 Usage : my $calc=Bio::CUA::CUB::Calculator->new(@args);
56 Function: initialize the calculator
57 Returns : an object of this class
58 Args : a hash with following acceptable keys:
59
60 B<Mandatory options>:
61  
62 =over
63  
64 =item C<-codon_table>
65  
66 the genetic code table applied for following sequence analyses. It
67 can be specified by an integer (genetic code table id), an object of
68 L<Bio::CUA::CodonTable>, or a map-file. See the method
69 L<Bio::CUA::Summarizer/new> for details.
70  
71 =back
72  
73 B<options needed by FOP method>
74  
75 =over
76  
77 =item C<-optimal_codons>
78  
79 a file contains all the optimal codons, one codon per line. Or a
80 hashref with keys being the optimal codons
81  
82 =back
83  
84 B<options needed by CAI method>
85  
86 =over
87  
88 =item C<-CAI_values>
89  
90 a file containing CAI values for each codon, excluding 3
91 stop codons, so 61 lines with each line containing a codon and its
92 value separated by space or tab.
93 or
94 a hashref with each key being a codon and each value being CAI index
95 for the codon.
96  
97 =back
98  
99 B<options needed by tAI method>
100  
101 =over
102  
103 =item C<-tAI_values>
104  
105 similar to C<-CAI_values>, a file or a hash containing tAI value
106 for each codon.
107  
108 =back
109  
110 B<options needed by ENC method>
111  
112 =over
113  
114 =item C<-base_background>
115  
116 optional.
117 an arrayref containing base frequency of 4 bases (in the order
118 A,T,C, and G) derived from background data such as introns.
119 Or one of the following values: 'seq', 'seq3', which will lead to
120 estimating base frequencies from each analyzed sequence in whol or
121 its 3rd codon position, respectively.
122  
123 It can also be specified for each analyzed sequence with the methods
124 L</encp> and L</encp_r>
125  
126 =back
127  
128 =cut
129  
130 sub new
131 {
132 1 ▶ my ($caller, @args) = @_;
Subroutine new: called CC=11 SCAR=30.8
Pod documented
133  
134 # option -codon_table is processed in this parent class
135 1 my $self = $caller->SUPER::new(@args);
136  
137 # process all the parameters
138 1 my $hashRef = $self->_array_to_hash(\@args);
139 1 ▶ while(my ($tag, $val) = each %$hashRef)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$hashRef) 3 1 4
140 {
141 # tag 'codon_table' is now processed by parent package
142 3 ▶ if($tag =~ /^optimal/o) # optimal codons
Branch: 4 branches
BranchTrueFalseTotal
if ($tag =~ /^optimal/o) { } 0 3 3
elsif ($tag =~ /^cai/o) { } 1 2 3
elsif ($tag =~ /^tai/o) { } 1 1 2
elsif ($tag =~ /^base/o) { } 0 1 1
143 {
144 # a hash using codons as keys
145 my $optimalCodons = ref($val) eq 'HASH'?
146 0 ▶ {map { $_ => 1 } keys(%$val)} : $self->_parse_file($val,1);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'HASH' ? : 0 0 0
147 0 $self->{'_optimal_codons'} = $optimalCodons;
148 }elsif($tag =~ /^cai/o) # CAI values
149 {
150 # a hash like codon => CAI_value
151 1 ▶ my $caiValues = ref($val) eq 'HASH'?
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'HASH' ? : 0 1 1
152 $val : $self->_parse_file($val,2);
153 1 $self->{'_cai_values'} = $caiValues;
154 }elsif($tag =~ /^tai/o) # tAI values
155 {
156 # a hash like codon => tAI_value
157 1 ▶ my $taiValues = ref($val) eq 'HASH'?
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'HASH' ? : 0 1 1
158 $val : $self->_parse_file($val,2);
159 1 $self->{'_tai_values'} = $taiValues;
160 }elsif($tag =~ /^base/o) # background base composition
161 {
162 0 ▶ if(ref($val) eq 'ARRAY' or $val =~ /^seq/)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY' or $val =~ /^seq/) { } 0 0 0
Condition0%0/3
ref $val eq "ARRAY" or $val =~ /^seq/
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $val eq "ARRAY" or $val =~ /^seq/
ref $val eq "ARRAY" $val =~ /^seq/
Truth table0%0/3
A or B
A= ref $val eq "ARRAY"B= $val =~ /^seq/
ABresult
1X1
011
000
163 {
164 0 $self->{'_base_comp'} = $val;
165 }else
166 {
167 0 $self->throw("Unknown value '$val' for parameter",
168 "-base_background");
169 }
170 }else
171 {
172 # Unknown parameter '$tag', ignored
173 }
174 }
175  
176 1 $self->no_atg(1); # exclude ATG in tAI calculation
177 # check the input values
178 # 1. make sure all the sense codons have CAI or tAI values if
179 # provided
180  
181 1 return $self;
182 }
183  
184 =head1 sequence input
185  
186 all the following methods accept one of the following formats as
187 sequence input
188  
189 =over
190  
191 =item 1
192
193 string of nucleotide sequence with length of 3N,
194  
195 =item 2
196  
197 sequence object which has a method I<seq> to get the sequence string,
198  
199 =item 3
200  
201 a sequence file in fasta format
202  
203 =item 4
204  
205 reference to a codon count hash, like
206 $codons = {
207 AGC => 50,
208 GTC => 124,
209 ... ...
210 }.
211  
212 =back
213  
214 =head2 cai
215  
216 Title : cai
217 Usage : $caiValue = $self->cai($seq);
218 Function: calculate the CAI value for the sequence
219 Returns : a number, or undef if failed
220 Args : see L</"sequence input">
221 Note: codons without synonymous competitors are excluded in
222 calculation.
223  
224 =cut
225  
226 sub cai
227 {
228 13 ▶ my ($self, $seq) = @_;
Subroutine cai: called CC=1 SCAR=0.0
Pod documented
229 13 $self->_xai($seq, 'CAI');
230 }
231  
232 # the real calculator of tAI or CAI as both have the same formula
233 sub _xai
234 {
235 26 ▶ my ($self, $seq, $type) = @_;
Subroutine _xai: called CC=14 SCAR=27.7
236  
237 26 my $name;
238 my $xaiHash;
239 26 ▶ if($type =~ /cai/i)
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /cai/i) { } 13 13 26
elsif ($type =~ /tai/i) { } 13 0 13
240 {
241 13 $name = 'CAI';
242 13 $xaiHash = $self->{"_cai_values"};
243 }elsif($type =~ /tai/i)
244 {
245 13 $name = 'tAI';
246 13 $xaiHash = $self->{"_tai_values"};
247 }else
248 {
249 0 $self->throw("Unknown adaptation index type '$type'");
250 }
251 26 ▶ unless($xaiHash)
Branch: 1 branch
BranchTrueFalseTotal
unless ($xaiHash) 0 26 26
252 {
253 0 $self->warn("$name values for codons were not provided for",
254 "this analyzer, so can not calculate $name for sequences");
255 0 return undef;
256 }
257  
258 26 ▶ my $codonList = $self->get_codon_list($seq) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $codonList = $self->get_codon_list($seq) or return 0 26 26
259  
260 26 my $xai = 0;
261 26 my $seqLen = 0; # this excludes some unsuitable codons
262 # get non-degenerative codons which are excluded in CAI
263 26 my %nonDegCodons = map { $_ => 1 } $self->codons_by_degeneracy(1);
264 26 my @senseCodons = $self->codon_table->all_sense_codons;
265 26 foreach my $codon (@senseCodons)
266 {
267 1586 ▶ next unless($codonList->{$codon}); # no observation of this codon
Branch: 1 branch
BranchTrueFalseTotal
unless $codonList->{$codon} 26 1560 1586
268 # excluding non-degenerate codons for CAI calculation
269 1560 ▶ next if($nonDegCodons{$codon} and $type =~ /cai/i);
Branch: 1 branch
BranchTrueFalseTotal
if $nonDegCodons{$codon} and $type =~ /cai/i 26 1534 1560
Condition100%3/3
$nonDegCodons{$codon} and $type =~ /cai/i
!ll&&!rl&&r
15082626
MC/DC100%2/2
$nonDegCodons{$codon} and $type =~ /cai/i
$nonDegCodons{$codon} $type =~ /cai/i
Truth table100%3/3
A and B
A= $nonDegCodons{$codon}B= $type =~ /cai/i
ABresult
0X0
100
111
270 1534 ▶ unless(exists $xaiHash->{$codon})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $xaiHash->{$codon}) 13 1521 1534
271 {
272 13 ▶ $self->warn("Codon '$codon' is ignored")
Branch: 2 branches
BranchTrueFalseTotal
if $self->debug and $self->no_atg ? $codon ne "ATG" : 1 0 13 13
$self->no_atg ? : 0 0 0
Condition33%1/3
$self->debug and $self->no_atg ? $codon ne "ATG" : 1
!ll&&!rl&&r
1300
MC/DC0%0/2
$self->debug and $self->no_atg ? $codon ne "ATG" : 1
$self->debug $self->no_atg ? $codon ne "ATG" : 1
Truth table33%1/3
A and B
A= $self->debugB= $self->no_atg ? $codon ne "ATG" : 1
ABresult
0X0
100
111
273 if($self->debug and ($self->no_atg? ($codon ne 'ATG') : 1));
274 13 next;
275 }
276 1521 my $cnt = $codonList->{$codon};
277 # to overcome real number overflow, use log
278 1521 $xai += $cnt*log($xaiHash->{$codon});
279 1521 $seqLen += $cnt;
280 }
281  
282 26 ▶ return undef unless($xai); # no codons with CAI/tAI
Branch: 1 branch
BranchTrueFalseTotal
unless $xai 0 26 26
283  
284 26 $xai = exp($xai/$seqLen);
285 26 return $xai;
286 }
287  
288 =head2 fop
289  
290 Title : fop
291 Usage : $fopValue = $self->fop($seq[,$withNonDegenerate]);
292 Function: calculate the fraction of optimal codons in the sequence
293 Returns : a number, or undef if failed
294 Args : for sequence see L</"sequence input">.
295 if optional argument '$withNonDegenerate' is true, then
296 non-degenerate codons (those do not have synonymous partners) are
297 included in calculation. Default is excluding these codons.
298  
299 =cut
300  
301 sub fop
302 {
303 0 ▶ my ($self, $seq, $withNonDeg) = @_;
Subroutine fop: not called CC=8 SCAR=42.8
Pod documented
304  
305 0 ▶ my $optimalCodons = $self->{'_optimal_codons'} or
Branch: 1 branch
BranchTrueFalseTotal
my $optimalCodons = $self->{"_optimal_codons"} or $self->throw("No optimal codons associated with $self") 0 0 0
306 $self->throw("No optimal codons associated with $self");
307  
308 0 ▶ my $codonList = $self->get_codon_list($seq) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $codonList = $self->get_codon_list($seq) or return 0 0 0
309 # get non-degenerate codons
310 0 my %nonDegCodons = map { $_ => 1 } $self->codons_by_degeneracy(1);
311  
312  
313 0 my $optCnt = 0; # optimal codons
314 0 my $total = 0;
315 0 ▶ while(my ($codon, $cnt) = each %$codonList)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$codonList) 0 0 0
316 {
317 # excluding non-degenerate codons if necessary
318 0 ▶ next if(!$withNonDeg and $nonDegCodons{$codon});
Branch: 1 branch
BranchTrueFalseTotal
if not $withNonDeg and $nonDegCodons{$codon} 0 0 0
Condition0%0/3
not $withNonDeg and $nonDegCodons{$codon}
!ll&&!rl&&r
000
MC/DC0%0/2
not $withNonDeg and $nonDegCodons{$codon}
not $withNonDeg $nonDegCodons{$codon}
Truth table0%0/3
not(A) and B
A= not $withNonDegB= $nonDegCodons{$codon}
ABresult
0X0
100
111
319 0 ▶ $optCnt += $cnt if($optimalCodons->{$codon});
Branch: 1 branch
BranchTrueFalseTotal
if $optimalCodons->{$codon} 0 0 0
320 0 $total += $cnt;
321 }
322  
323 0 ▶ return $optCnt/($total || 1);
Condition0%0/2
$total || 1
l!l
00
MC/DC0%0/1
$total || 1
$total
Truth table0%0/2
A
A= $total
Aresult
11
00
324 }
325  
326 =head2 tai
327  
328 Title : tai
329 Usage : $taiValue = $self->tai($seq);
330 Function: calculate the tAI value for the sequence
331 Returns : a number, or undef if failed
332 Args : for sequence see L</"sequence input">.
333  
334 Note: codons which do not have tAI values are ignored from input
335 sequence
336  
337 =cut
338  
339 sub tai
340 {
341 13 ▶ my ($self, $seq) = @_;
Subroutine tai: called CC=1 SCAR=0.0
Pod documented
342 13 $self->_xai($seq, 'tAI');
343 }
344  
345 # an alias
346 sub tAI
347 {
348 0 ▶ my ($self, $seq) = @_;
Subroutine tAI: not called CC=1 SCAR=6.9
Pod undocumented
349 0 $self->_xai($seq, 'tAI');
350 }
351  
352 =head2 enc
353  
354 Title : enc
355 Usage : $encValue = $self->enc($seq,[$minTotal]);
356 Function: calculate ENC for the sequence using the original method
357 I<Wright, 1990, Gene>
358 Returns : a number, or undef if failed
359 Args : for sequence see L</"sequence input">.
360 Optional argument I<minTotal> specifies minimal count
361 for an amino acid; if observed count is smaller than this count, this
362 amino acid's F will not be calculated but inferred. Deafult is 5.
363  
364 Note: when the F of a redundancy group is unavailable due to lack of
365 sufficient data, it will be estimated from other groups following Wright's
366 method, that is, F3=(F2+F4)/2, and for others, F=1/r where r is the
367 degeneracy degree of that group.
368  
369 =cut
370 sub enc
371 {
372 13 ▶ my ($self, $seq, $minTotal) = @_;
Subroutine enc: called CC=1 SCAR=0.0
Pod documented
373 13 $self->_enc_factory($seq, $minTotal, 'mean');
374 }
375  
376 =head2 enc_r
377  
378 Title : enc_r
379 Usage : $encValue = $self->enc_r($seq,[$minTotal]);
380 Function: similar to the method L</enc>, except that missing F values
381 are estimated in a different way.
382 Returns : a number, or undef if failed
383 Args : for sequence see L</"sequence input">.
384 Optional argument I<minTotal> specifies minimal count
385 for an amino acid; if observed count is smaller than this count, this
386 amino acid's F will not be calculated but inferred. Deafult is 5.
387  
388 Note: for missing Fx of degeneracy class 'x', we first estimated the
389 ratio (1/Fx-1)/(x-1) by averaging the ratios of other degeneracy
390 classes with known F values. Then Fx is obtained by solving the simple
391 equation.
392  
393 =cut
394  
395 sub enc_r
396 {
397 13 ▶ my ($self, $seq, $minTotal) = @_;
Subroutine enc_r: called CC=1 SCAR=0.0
Pod documented
398 13 $self->_enc_factory($seq, $minTotal, 'equal_ratio');
399 }
400  
401 =head2 encp
402  
403 Title : encp
404 Usage : $encpValue = $self->encp($seq,[$minTotal,[$A,$T,$C,$G]]);
405 Function: calculate ENC for the sequence using the updated method
406 by Novembre I<2002, MBE>, which corrects the background nucleotide
407 composition.
408 Returns : a number, or undef if failed
409 Args : for sequence see L</"sequence input">.
410
411 Optional argument I<minTotal> specifies minimal count
412 for an amino acid; if observed count is smaller than this count, this
413 amino acid's F will not be calculated but inferred. Deafult is 5.
414  
415 another optional argument gives the background nucleotide composition
416 in the order of A,T,C,G in an array, if not provided, it will use the
417 default one provided when calling the method L</new>. If stil
418 unavailable, error occurs.
419  
420 =cut
421  
422 sub encp
423 {
424 0 ▶ my ($self, $seq, $minTotal, $baseComp) = @_;
Subroutine encp: not called CC=1 SCAR=6.9
Pod documented
425 0 $self->_enc_factory($seq, $minTotal, 'mean', 1, $baseComp);
426 }
427  
428 =head2 encp_r
429  
430 Title : encp_r
431 Usage : $encpValue =
432 $self->encp_r($seq,[$minTotal,[$A,$T,$C,$G]]);
433 Function: similar to the method L</encp>, except that missing F values
434 are estimated using a different way.
435 Returns : a number, or undef if failed
436 Args : for sequence see L</"sequence input">.
437
438 Optional argument I<minTotal> specifies minimal count
439 for an amino acid; if observed count is smaller than this count, this
440 amino acid's F will not be calculated but inferred. Deafult is 5.
441  
442 another optional argument gives the background nucleotide composition
443 in the order of A,T,C,G in an array, if not provided, it will use the
444 default one provided when calling the method L</new>. If stil
445 unavailable, error occurs.
446  
447 Note: for missing Fx of degeneracy class 'x', we first estimated the
448 ratio (1/Fx-1)/(x-1) by averaging the ratios of other degeneracy
449 classes with known F values. Then Fx is obtained by solving the simple
450 equation.
451  
452 =cut
453  
454 sub encp_r
455 {
456 0 ▶ my ($self, $seq, $minTotal, $baseComp) = @_;
Subroutine encp_r: not called CC=1 SCAR=6.9
Pod documented
457 0 $self->_enc_factory($seq, $minTotal, 'equal_ratio', 1, $baseComp);
458 }
459  
460 # real function calculate different versions of ENC
461 # parameters explanation
462 # seq: sequence string, sequence object, sequence file, or hash
463 # reference to codon list
464 # correctBaseComp: if true, correct background base composition using
465 # Novembre's method
466 # F_EstimateMethod: how to estimate average F for a certain redundancy
467 # class if that class does not have observed data so can't be
468 # calculated; 'mean' is for Wright's method, and 'equal_ratio' for
469 # Zhenguo's method. The latter assumes a similar (1/F[r])/r for each
470 # redundancy class with redundancy degree 'r'
471 # baseComposition: optional, a reference to an array containing
472 # background nucleotide composition. If provided, it overides the
473 # values set when method L</new> was called.
474 sub _enc_factory
475 {
476 26 ▶ my ($self, $seq, $minTotal, $F_EstimateMethod, $correctBaseComp, $baseComposition) = @_;
Subroutine _enc_factory: called CC=32 SCAR=49.2
477  
478 26 ▶ $minTotal = 5 unless(defined $minTotal); # the minumum count of residule for a given amino
Branch: 1 branch
BranchTrueFalseTotal
unless defined $minTotal 0 26 26
479 # acid for it to be included in F calculation
480
481 # a hash ref, codon => counts
482 26 ▶ my $codonList = $self->get_codon_list($seq) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $codonList = $self->get_codon_list($seq) or return 0 26 26
483 26 ▶ my $seqId = (ref($seq) and $seq->can('id'))? $seq->id : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $seq && $seq->can('id') ? : 26 0 26
Condition33%1/3
ref $seq and $seq->can("id")
!ll&&!rl&&r
0026
MC/DC0%0/2
ref $seq and $seq->can("id")
ref $seq $seq->can("id")
Truth table33%1/3
A and B
A= ref $seqB= $seq->can("id")
ABresult
0X0
100
111
484  
485 # determine expected codon frequency if necessary
486 26 my $expectedCodonFreq;
487 # determine base compositions now
488 26 ▶ if($correctBaseComp)
Branch: 1 branch
BranchTrueFalseTotal
if ($correctBaseComp) 0 26 26
489 {
490 0 ▶ if(!defined($baseComposition)) # not provided for this sequence
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $baseComposition) 0 0 0
491 {
492 0 my $defaultBaseComp = $self->base_composition;
493 0 ▶ unless($defaultBaseComp)
Branch: 1 branch
BranchTrueFalseTotal
unless ($defaultBaseComp) 0 0 0
494 {
495 0 $self->warn("No default base composition for seq",
496 " '$seqId', so no GC-corrected ENC");
497 0 return undef;
498 }
499 0 ▶ if($defaultBaseComp eq 'seq')
Branch: 2 branches
BranchTrueFalseTotal
if ($defaultBaseComp eq 'seq') { } 0 0 0
elsif ($defaultBaseComp eq 'seq3') { } 0 0 0
500 {
501 0 $baseComposition =
502 $self->estimate_base_composition($codonList);
503 }elsif($defaultBaseComp eq 'seq3')
504 {
505 0 $baseComposition =
506 $self->estimate_base_composition($codonList,3);
507 }else
508 {
509 0 $baseComposition = $defaultBaseComp;
510 }
511 } # otherwise sequence-specific base-composition is provided
512 # here
513  
514 # codon frequency may not be estimated due to invalid
515 # compositions
516 $expectedCodonFreq =
517 0 ▶ $self->expect_codon_freq($baseComposition)
Branch: 1 branch
BranchTrueFalseTotal
if $baseComposition 0 0 0
518 if($baseComposition);
519 0 ▶ return undef unless($expectedCodonFreq);
Branch: 1 branch
BranchTrueFalseTotal
unless $expectedCodonFreq 0 0 0
520 }
521  
522  
523 # now let us calculate F for each redundancy class
524 # determined by codon table, containing all amino acid classes
525 26 my $AARedundancyClasses = $self->aa_degeneracy_classes; #
526 26 my %FavgByClass; # record the average F from each class
527 26 ▶ while(my ($redundancy, $AAHash) = each %$AARedundancyClasses)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$AARedundancyClasses) 130 26 156
528 {
529 # number of observed AA types in this class
530 130 my $numAAInClass = 0; # number of amino acid species in this class
531 130 my $Fsum = 0;
532 130 ▶ while(my ($AA, $codonArray) = each %$AAHash)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$AAHash) 494 104 598
533 {
534 494 ▶ if($redundancy == 1) # this class has only one codon
Branch: 1 branch
BranchTrueFalseTotal
if ($redundancy == 1) 26 468 494
535 {
536 26 $numAAInClass = scalar(keys %$AAHash);
537 26 $Fsum = $numAAInClass; # each AA contribute 1
538 26 last;
539 }
540 # total count of observed residules for this AA
541 468 my $AAcnt = 0;
542 468 foreach (@$codonArray)
543 {
544 # check the codon exists in this seq
545 1534 ▶ next unless(exists $codonList->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $codonList->{$_} 26 1508 1534
546 1508 $AAcnt += $codonList->{$_};
547 }
548
549 # skip if occurence of this amino acid is less than the
550 # minimal threshold
551 468 ▶ next if($AAcnt < $minTotal or $AAcnt < 2);
Branch: 1 branch
BranchTrueFalseTotal
if $AAcnt < $minTotal or $AAcnt < 2 4 464 468
Condition66%2/3
$AAcnt < $minTotal or $AAcnt < 2
l!l&&r!l&&!r
40464
MC/DC50%1/2
$AAcnt < $minTotal or $AAcnt < 2
$AAcnt < $minTotal $AAcnt < 2
Truth table66%2/3
A or B
A= $AAcnt < $minTotalB= $AAcnt < 2
ABresult
1X1
011
000
552  
553 # now calculate F for this AA species
554 464 ▶ if($correctBaseComp) # correct base composition
Branch: 1 branch
BranchTrueFalseTotal
if ($correctBaseComp) { } 0 464 464
555 {
556 0 my $chisq = 0;
557 # get the freq of codons of this amino acids
558 0 my $totalFreq = 0;
559 0 foreach (@$codonArray)
560 {
561 0 $totalFreq += $expectedCodonFreq->{$_};
562 }
563 0 foreach (@$codonArray)
564 {
565 # set unobserved codons to 0
566 0 ▶ my $codonCnt = $codonList->{$_} || 0;
Condition0%0/2
$codonList->{$_} || 0
l!l
00
MC/DC0%0/1
$codonList->{$_} || 0
$codonList->{$_}
Truth table0%0/2
A
A= $codonList->{$_}
Aresult
11
00
567 my $expectedFreq =
568 0 $expectedCodonFreq->{$_}/$totalFreq;
569 0 $chisq += ($codonCnt/$AAcnt -
570 $expectedFreq)**2/$expectedFreq;
571 }
572 0 $chisq *= $AAcnt; # don't forget multiply this
573 0 $Fsum += ($chisq + $AAcnt -
574 $redundancy)/($redundancy*($AAcnt-1));
575 }else # no correction, use old Wright method
576 {
577 464 my $pSquareSum = 0;
578 464 foreach (@$codonArray)
579 {
580 1526 my $codonCnt = $codonList->{$_};
581 1526 ▶ next unless($codonCnt);
Branch: 1 branch
BranchTrueFalseTotal
unless $codonCnt 22 1504 1526
582 1504 $pSquareSum += ($codonCnt/$AAcnt)**2;
583 }
584 464 $Fsum += ($AAcnt*$pSquareSum -1)/($AAcnt-1);
585 }
586 # increase the number of AA species in this class
587 464 $numAAInClass++;
588 }
589 # check whether all AA species are ignored or not observed
590 130 ▶ if($numAAInClass > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($numAAInClass > 0) 130 0 130
591 {
592 # note, in some special cases, Fsum == 0 even though
593 # $numAAInClass >0, for example for a 6-fold amino acid,
594 # if each of its codon is observed only once, it would
595 # result in Faa = 0. so we need add restriction on this
596 130 ▶ $FavgByClass{$redundancy} = $Fsum/$numAAInClass if($Fsum >
Branch: 1 branch
BranchTrueFalseTotal
if $Fsum > 0 130 0 130
597 0);
598 } # otherwise no data
599 }
600  
601 # estimate missing redundancy classes due to no observation of
602 # that class's AAs, and get the final Nc values
603 26 my $enc = 0;
604 26 ▶ while(my ($redundancy, $AAHash) = each %$AARedundancyClasses)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$AARedundancyClasses) 130 26 156
605 {
606 # the number of AA species in this class, determined by the
607 # codon table, not the input seq
608 130 my $AAcntInClass = scalar(keys %$AAHash);
609 130 ▶ if(exists $FavgByClass{$redundancy})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $FavgByClass{$redundancy}) 130 0 130
610 {
611 die "$redundancy, $AAcntInClass in seq '$seqId':$!"
612 130 ▶ unless($FavgByClass{$redundancy});
Branch: 1 branch
BranchTrueFalseTotal
unless $FavgByClass{$redundancy} 0 130 130
613 130 $enc += $AAcntInClass/$FavgByClass{$redundancy};
614 130 next;
615 }
616  
617 # otherwise this class was not observed
618 0 ▶ my $equalRatio = $F_EstimateMethod eq 'mean'? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$F_EstimateMethod eq 'mean' ? : 0 0 0
619 0 my $estimatedFavg = _estimate_F(\%FavgByClass, $redundancy,
620 $equalRatio);
621 0 ▶ unless($estimatedFavg)
Branch: 1 branch
BranchTrueFalseTotal
unless ($estimatedFavg) 0 0 0
622 {
623 0 $self->warn("Cannot estimate average F for class with",
624 "redundancy=$redundancy in sequence $seqId, ",
625 "probably no known F values for any class");
626 0 return undef;
627 }
628 0 $enc += $AAcntInClass/$estimatedFavg;
629 }
630  
631 26 return $enc;
632 }
633  
634 # estimate F average
635 sub _estimate_F
636 {
637 0 ▶ my ($knownF,$redundancy,$equalRatio) = @_;
Subroutine _estimate_F: not called CC=9 SCAR=45.0
638  
639 0 ▶ return 1 if($redundancy == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $redundancy == 1 0 0 0
640  
641 0 ▶ if($equalRatio) # get the mean (1/Fr-1)/(r-1)
Branch: 1 branch
BranchTrueFalseTotal
if ($equalRatio) { } 0 0 0
642 {
643 0 my $ratioSum;
644 0 my $cnt = 0; # number of known Fs
645 0 ▶ while(my ($r, $F) = each %$knownF)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$knownF) 0 0 0
646 {
647 0 ▶ next if $r < 2; # excluding class of redundancy==1
Branch: 1 branch
BranchTrueFalseTotal
if $r < 2 0 0 0
648 0 $ratioSum += (1/$F-1)/($r-1);
649 0 $cnt++;
650 }
651  
652 0 ▶ if( $cnt > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 0) { } 0 0 0
653 {
654 0 my $Fx = 1/($ratioSum/$cnt*($redundancy-1)+1);
655 0 return $Fx;
656 }else # no known F for any class with redundancy > 1
657 {
658 0 return undef;
659 }
660  
661 }else # otherwise use Wright's method
662 {
663 0 ▶ if($redundancy == 3)
Branch: 1 branch
BranchTrueFalseTotal
if ($redundancy == 3) { } 0 0 0
664 {
665 0 ▶ my $F2 = $knownF->{2} || 1/2; # class 2
Condition0%0/2
$knownF->{'2'} || 0.5
l!l
00
MC/DC0%0/1
$knownF->{'2'} || 0.5
$knownF->{'2'}
Truth table0%0/2
A
A= $knownF->{'2'}
Aresult
11
00
666 0 ▶ my $F4 = $knownF->{4} || 1/4; # class 4
Condition0%0/2
$knownF->{'4'} || 0.25
l!l
00
MC/DC0%0/1
$knownF->{'4'} || 0.25
$knownF->{'4'}
Truth table0%0/2
A
A= $knownF->{'4'}
Aresult
11
00
667 0 return ($F2 + $F4)/2;
668 }else
669 {
670 0 return 1/$redundancy; # assuming no bias
671 }
672 }
673  
674 }
675  
676 # get the default base compostion of this object
677 sub base_composition
678 {
679 0 ▶ my $self = shift;
Subroutine base_composition: not called CC=1 SCAR=6.9
Pod undocumented
680  
681 0 return $self->{'_base_comp'};
682 }
683  
684 =head2 estimate_base_composition
685  
686 Title : estimate_base_composition
687 Usage : @baseComp = $self->estimate_base_composition($seq,[$pos])
688 Function: estimate base compositions in the sequence
689 Returns : an array of numbers in the order of A,T,C,G, or its
690 reference if in the scalar context
691 Args : a sequence string or a reference of hash containing codons
692 and their counts (eg., AGG => 30), and optionally an integer; the integer
693 specifies which codon position's nucleotide will be used instead of
694 all three codon positions.
695  
696 =cut
697  
698 sub estimate_base_composition
699 {
700 0 ▶ my ($self, $seq, $pos) = @_;
Subroutine estimate_base_composition: not called CC=14 SCAR=53.5
Pod documented
701  
702 0 my %bases;
703 # check if input is a codon list
704 my $codonList;
705 0 ▶ if(ref($seq) eq 'HASH') # a codon list
Branch: 1 branch
BranchTrueFalseTotal
if (ref $seq eq 'HASH') { } 0 0 0
706 {
707 0 $codonList = $seq;
708 }else # a sequence string or object
709 {
710 0 $seq = $self->_get_seq_str($seq);
711 }
712  
713 0 ▶ if($pos)
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) { } 0 0 0
714 {
715 0 ▶ $self->throw("Only 1, 2, or 3 are acceptable for pos,",
Branch: 1 branch
BranchTrueFalseTotal
unless $pos > 0 and $pos < 4 0 0 0
Condition0%0/3
$pos > 0 and $pos < 4
!ll&&!rl&&r
000
MC/DC0%0/2
$pos > 0 and $pos < 4
$pos > 0 $pos < 4
Truth table0%0/3
A and B
A= $pos > 0B= $pos < 4
ABresult
0X0
100
111
716 "'$pos' is not valid here") unless($pos > 0 and $pos < 4);
717 0 ▶ if($codonList) # input is a codon list
Branch: 1 branch
BranchTrueFalseTotal
if ($codonList) { } 0 0 0
718 {
719 0 my $base;
720 0 ▶ while(my ($codon, $cnt) = each %$codonList)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$codonList) 0 0 0
721 {
722 0 $base = substr($codon, $pos-1,1);
723 0 $bases{$base} += $cnt;
724 }
725 }else # a sequence
726 {
727 0 my $seqLen = length($seq);
728 0 my $accuLen = $pos - 1;
729 0 my $period = 3; # a codon length
730 0 my $base;
731 0 ▶ while($accuLen < $seqLen)
Branch: 1 branch
BranchTrueFalseTotal
if ($accuLen < $seqLen) 0 0 0
732 {
733 0 $base = substr($seq,$accuLen,1);
734 0 $bases{$base}++;
735 0 $accuLen += $period;
736 }
737 }
738 }else # all nucleotides
739 {
740 0 ▶ if($codonList) # input is a codon list
Branch: 1 branch
BranchTrueFalseTotal
if ($codonList) { } 0 0 0
741 {
742 0 ▶ while(my ($codon, $cnt) = each %$codonList)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$codonList) 0 0 0
743 {
744 0 map { $bases{$_} += $cnt } split('', $codon);
745 }
746 }else
747 {
748 0 map { $bases{$_}++ } split('',$seq);
749 }
750 }
751  
752 0 my $total = 0;
753 0 my @comp;
754 0 foreach ($self->bases) # only consider A,T,C,G
755 {
756 0 ▶ $total += $bases{$_} || 0;
Condition0%0/2
$bases{$_} || 0
l!l
00
MC/DC0%0/1
$bases{$_} || 0
$bases{$_}
Truth table0%0/2
A
A= $bases{$_}
Aresult
11
00
757 0 ▶ push @comp, $bases{$_} || 0;
Condition0%0/2
$bases{$_} || 0
l!l
00
MC/DC0%0/1
$bases{$_} || 0
$bases{$_}
Truth table0%0/2
A
A= $bases{$_}
Aresult
11
00
758 }
759 0 @comp = map { $_/$total } @comp;
760  
761 0 ▶ return wantarray? @comp : \@comp;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
762 }
763  
764 =head2 gc_fraction
765  
766 Title : gc_fraction
767 Usage : $frac = $self->gc_fraction($seq,[$pos])
768 Function: get fraction of GC content in the sequence
769 Returns : a floating number between 0 and 1.
770 Args : a sequence string or a reference of hash containing codons
771 and their counts (eg., AGG => 30), and optionally an integer; the integer
772 specifies which codon position's nucleotide will be used for
773 calculation (i.e., 1, 2, or 3), instead of all three positions.
774  
775 =cut
776  
777 sub gc_frac
778 {
779 0 ▶ my ($self, @args) = @_;
Subroutine gc_frac: not called CC=1 SCAR=6.9
Pod undocumented
780 0 $self->gc_fraction(@args);
781 }
782  
783 sub gc_fraction
784 {
785 0 ▶ my ($self, @args) = @_;
Subroutine gc_fraction: not called CC=2 SCAR=17.9
Pod documented
786  
787 0 my @composition = $self->estimate_base_composition(@args);
788 0 my @bases = $self->bases;
789 0 my @indice = grep { $bases[$_] =~ /[GC]/ } 0..$#bases;
790  
791 0 my $frac = 0;
792 0 foreach (@indice)
793 {
794 0 $frac += $composition[$_];
795 }
796  
797 0 return $frac;
798 }
799  
800 =head2 expect_codon_freq
801  
802 Title : expect_codon_freq
803 Usage : $codonFreq = $self->expect_codon_freq($base_composition)
804 Function: return the expected frequency of codons
805 Returns : reference to a hash in which codon is hash key, and
806 fraction is hash value
807 Args : reference to an array of base compositions in the order of
808 [A, T, C, G], represented as either counts or fractions
809  
810 =cut
811  
812 sub expect_codon_freq
813 {
814 0 ▶ my ($self, $baseComp) = @_;
Subroutine expect_codon_freq: not called CC=10 SCAR=47.0
Pod documented
815  
816 0 ▶ unless($baseComp and ref($baseComp) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
unless ($baseComp and ref $baseComp eq "ARRAY") 0 0 0
Condition0%0/3
$baseComp and ref $baseComp eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$baseComp and ref $baseComp eq "ARRAY"
$baseComp ref $baseComp eq "ARRAY"
Truth table0%0/3
A and B
A= $baseCompB= ref $baseComp eq "ARRAY"
ABresult
0X0
100
111
817 {
818 0 $self->warn("Invalid base composition '$baseComp'",
819 " for expect_codon_freq, which should be an array reference");
820 0 return undef;
821 }
822  
823 0 my @bases = $self->bases;
824 0 my $compSum = 0; # used to normalize in case they are not summed to 1
825 0 my $zeroCnt = 0; # count of zero values
826 0 foreach (0..3)
827 {
828 0 ▶ $zeroCnt++ if($baseComp->[$_] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $baseComp->[$_] == 0 0 0 0
829 0 $compSum += $baseComp->[$_];
830 }
831  
832 # set zero value a pseudo count, depending on the provided values
833 # are fractions or counts
834 0 ▶ my $pseudoCnt = $compSum > 2? 1 : 1/100;
Branch: 1 branch
BranchTrueFalseTotal
$compSum > 2 ? : 0 0 0
835 0 $compSum += $pseudoCnt * $zeroCnt;
836 0 ▶ my %freq = map { $bases[$_] => ($baseComp->[$_] || $pseudoCnt)/$compSum } 0..3;
Condition0%0/3
$baseComp->[$_] || $pseudoCnt
l!l&&r!l&&!r
000
MC/DC0%0/2
$baseComp->[$_] || $pseudoCnt
$baseComp->[$_] $pseudoCnt
Truth table0%0/3
A || B
A= $baseComp->[$_]B= $pseudoCnt
ABresult
1X1
011
000
837 0 my %result;
838 0 foreach my $b1 (@bases)
839 {
840 0 foreach my $b2 (@bases)
841 {
842 0 foreach my $b3 (@bases)
843 {
844 0 my $codon = $b1.$b2.$b3;
845 0 $result{$codon} = $freq{$b1}*$freq{$b2}*$freq{$b3};
846 }
847 }
848 }
849  
850 0 return \%result;
851 }
852  
853 =head1 AUTHOR
854  
855 Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >>
856  
857 =head1 BUGS
858  
859 Please report any bugs or feature requests to C<bug-bio-cua at rt.cpan.org>, or through
860 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. I will be notified, and then you'll
861 automatically be notified of progress on your bug as I make changes.
862  
863  
864 =head1 SUPPORT
865  
866 You can find documentation for this module with the perldoc command.
867  
868 perldoc Bio::CUA::CUB::Calculator
869  
870  
871 You can also look for information at:
872  
873 =over 4
874  
875 =item * RT: CPAN's request tracker (report bugs here)
876  
877 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA>
878  
879 =item * AnnoCPAN: Annotated CPAN documentation
880  
881 L<http://annocpan.org/dist/Bio-CUA>
882  
883 =item * CPAN Ratings
884  
885 L<http://cpanratings.perl.org/d/Bio-CUA>
886  
887 =item * Search CPAN
888  
889 L<http://search.cpan.org/dist/Bio-CUA/>
890  
891 =back
892  
893  
894 =head1 ACKNOWLEDGEMENTS
895  
896  
897 =head1 LICENSE AND COPYRIGHT
898  
899 Copyright 2015 Zhenguo Zhang.
900  
901 This program is free software: you can redistribute it and/or modify
902 it under the terms of the GNU General Public License as published by
903 the Free Software Foundation, either version 3 of the License, or
904 (at your option) any later version.
905  
906 This program is distributed in the hope that it will be useful,
907 but WITHOUT ANY WARRANTY; without even the implied warranty of
908 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
909 GNU General Public License for more details.
910  
911 You should have received a copy of the GNU General Public License
912 along with this program. If not, see L<http://www.gnu.org/licenses/>.
913  
914  
915 =cut
916  
917 2 1; # End of Bio::CUA::CUB::Calculator
↑ Summary lib/Bio/CUA/Summarizer.pm »