lib/Bio/CUA.pm

filter: Statementstmt 65.6% 65 / 99 Branchbran 47.2% 17 / 36 Conditioncond 15.3% 2 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 76.4% 13 / 17 Podpod 50.0% 3 / 6 totaltotal 55.8% 100 / 179 SCAR 43.9
CC
26
Cov
57%
throw
12.0
warn
12.0
_format_text
12.0
CRAP 80.7
« lib/Bio/CUA/CUB/Builder.pm ↑ Summary lib/Bio/CUA/SeqIO.pm »
1 package Bio::CUA;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # some global variables
9 6 our $VERSION = '1.03';
10 6 my $sep = "\t";
11 #my @openFHs; # all file handles opened by this class
12  
13 =pod
14  
15 =head1 NAME
16  
17 Bio::CUA - Codon Usage Analyzer.
18  
19 =head1 VERSION
20  
21 Version 1.03
22  
23 =head1 SYNOPSIS
24  
25 This is the root class for the whole distribution of
26 L<http://search.cpan.org/dist/Bio-CUA/>,
27 providing some routine methods used by all classs in the
28 distribution L<http://search.cpan.org/dist/Bio-CUA/>. Users should not use this class
29 directly. Please start with its child classes such as
30 L<Bio::CUA::Summarizer>, L<Bio::CUA::CUB::Builder>.
31  
32 =head1 DESCRIPTION
33  
34 The aim of this distribution is to provide comprehensive and flexible
35 tools to analyze codon usage bias (CUB) and relevant problems, so that
36 users can speed up the genetic research by taking advantage of this
37 convenience.
38  
39 One amino acid can be encoded by more than one synonymous codon, and
40 synonymous codons are unevenly used. For example, some codons are used
41 more often than other synonymous ones in highly expressed genes (I<Sharp
42 and Li 1987>). To measure the unevenness of codon usage, multiple
43 metrics of codon usage bias have been developed, such as Fop
44 (Frequency of optimal codons), CAI (Codon Adaptation Index), tAI (tRNA
45 Adaptation Index), and ENC (Effective Number of Codons). The causes of
46 CUB phenomena are complicated, including, mutational bias, selection on
47 translational efficiency or accurancy. CUB is one fundamental concept
48 in genetics.
49  
50 So far, no software exists to compute all the above CUB metrics, and
51 more importantly parameters of CUB calculations are often fixed in
52 software, so one can only analyze genes in a limited list of species
53 and one can not incorporate its own parameters such as sequences of
54 highly expressed genes in a tissue.
55  
56 This package mainly solves these two problems. We also extend some
57 methods, such as GC-content corrected ENC, background-data normalized
58 CAI, etc. See the relevant methods in CUB classes for more details.
59  
60 =head1 METHODS
61  
62 =cut
63  
64 sub new
65 {
66 119 ▶ my ($caller, @args) = @_;
Subroutine new: called CC=3 SCAR=11.2
Pod undocumented
67 119 my $self = {};
68 119 ▶ my $class = ref($caller)? ref($caller) : $caller;
Branch: 1 branch
BranchTrueFalseTotal
ref $caller ? : 0 119 119
69  
70 119 bless $self, $class;
71 119 my $hashRef = $self->_array_to_hash(\@args);
72  
73 # only process its own argument
74 119 ▶ $self->debug(1) if($hashRef->{'debug'});
Branch: 1 branch
BranchTrueFalseTotal
if $hashRef->{"debug"} 0 119 119
75  
76 119 return $self;
77 }
78  
79 # store and retrieve tag values
80 sub get_tag
81 {
82 312 ▶ my ($self, $tag) = @_;
Subroutine get_tag: called CC=1 SCAR=0.0
Pod undocumented
83 312 return $self->{'_tags'}->{$tag};
84 }
85  
86 sub set_tag
87 {
88 323 ▶ my ($self, $tag, $val) = @_;
Subroutine set_tag: called CC=1 SCAR=0.0
Pod undocumented
89 323 $self->{'_tags'}->{$tag} = $val;
90 }
91  
92 =head2 debug
93  
94 Title : debug
95 Usage : $true_of_false=$self->debug([$bool]);
96 Function: get/set the boolean value.
97 Returns : 0 as false, 1 as true
98 Args : optional. 0 or 1 for false and true, respectively.
99  
100 =cut
101  
102 sub debug
103 {
104 74 ▶ my ($self, $val) = @_;
Subroutine debug: called CC=2 SCAR=7.1
Pod documented
105 74 ▶ $self->set_tag('debug', $val) if($val);
Branch: 1 branch
BranchTrueFalseTotal
if $val 0 74 74
106 74 return $self->get_tag('debug');
107 }
108  
109  
110 =head2 throw
111  
112 Title : throw
113 Usage : $self->throw("Some fatal errors");
114 Function: stop and report when fatal errors in formatted message
115 Returns : None
116 Args : error message
117  
118 =cut
119  
120 # simplified version
121 sub throw
122 {
123 0 ▶ my ($self, @args) = @_;
Subroutine throw: not called CC=3 SCAR=24.8
Pod documented
124 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
125 0 ▶ $class = ' '.$class if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
126 0 my $title = "------------- EXCEPTION$class -------------";
127 0 my $footer = ('-' x length($title))."\n";
128 #my $text = join("\n", @args);
129 0 my $text = _format_text(join(' ',@args));
130 0 croak "\n$title\n", "MSG: $text\n", $footer, "\n";
131 }
132  
133 =head2 warn
134  
135 Title : warn
136 Usage : $self->warn("Please pay attention here")
137 Function: report warning message when something looks not good
138 Returns : None
139 Args : warning messages.
140  
141 =cut
142  
143 sub warn
144 {
145 0 ▶ my ($self, @args) = @_;
Subroutine warn: not called CC=3 SCAR=24.8
Pod documented
146  
147 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
148 0 ▶ $class = ' '.$class if $class;
Branch: 1 branch
BranchTrueFalseTotal
if $class 0 0 0
149 0 my $title = "------------- WARNING$class -------------";
150 0 my $footer = ('-' x length($title))."\n";
151 0 my $text = _format_text(join(' ',@args));
152 #my $text = join("\n", @args);
153 0 carp "\n$title\n", "MSG: $text\n", $footer, "\n";
154 }
155  
156 # format the text into blocks with same line length
157 sub _format_text
158 {
159 0 ▶ my ($text, $lineLen) = @_;
Subroutine _format_text: not called CC=3 SCAR=24.8
160  
161 0 ▶ $lineLen ||= 60;
Condition0%0/2
$lineLen ||= 60
l!l
00
MC/DC0%0/1
$lineLen ||= 60
$lineLen
Truth table0%0/2
A
A= $lineLen
Aresult
11
00
162 0 chomp($text);
163 0 my $result = '';
164  
165 0 my @blocks = split /\n/, $text;
166  
167 0 foreach my $b (@blocks)
168 {
169 0 my $newB = _break_into_lines($b, $lineLen);
170 0 $result .= $newB;
171 }
172 0 return $result;
173 }
174  
175 sub _break_into_lines
176 {
177 0 ▶ my ($text, $size) = @_;
Subroutine _break_into_lines: not called CC=3 SCAR=24.8
178  
179 0 my $lines = '';
180 0 my $textLen = length($text);
181  
182 0 my $accuLen = 0;
183 0 ▶ while($accuLen < $textLen)
Branch: 1 branch
BranchTrueFalseTotal
if ($accuLen < $textLen) 0 0 0
184 {
185 0 ▶ my $lineLen = $accuLen + $size > $textLen? $textLen - $accuLen
Branch: 1 branch
BranchTrueFalseTotal
$accuLen + $size > $textLen ? : 0 0 0
186 : $size;
187 0 my $l = substr($text,$accuLen,$lineLen);
188 0 $accuLen += $lineLen;
189 0 $lines .= $l."\n";
190 }
191  
192 0 return $lines;
193 }
194  
195 # return hash ref by reading into an array ref
196 sub _array_to_hash
197 {
198 240 ▶ my ($self,$arrayRef,$nc) = @_;
Subroutine _array_to_hash: called CC=5 SCAR=16.3
199  
200 240 ▶ $self->throw("parameter '$arrayRef' to _array_to_hash is not an array reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayRef eq "ARRAY" 0 240 240
201 unless(ref($arrayRef) eq 'ARRAY');
202  
203 240 my %hash;
204  
205 240 ▶ $self->throw("Odd number of elements are in the array fed to",
Branch: 1 branch
BranchTrueFalseTotal
unless $arrayRef->$#* % 2 0 240 240
206 "_array_to_hash, check the array $arrayRef")
207 unless($#$arrayRef % 2);
208  
209 240 ▶ for(my $i = 0; $i < $#$arrayRef; $i += 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $arrayRef->$#*) 674 240 914
210 {
211 674 my $k = $arrayRef->[$i];
212 674 $k =~ s/^\-*//; # removing leading '-'
213 674 ▶ $k = lc($k) unless($nc);
Branch: 1 branch
BranchTrueFalseTotal
unless $nc 674 0 674
214 674 $hash{$k} = $arrayRef->[$i+1];
215 }
216  
217 240 return \%hash;
218 }
219  
220  
221 # write out hash to an outfile
222 sub _write_out_hash
223 {
224 2 ▶ my ($self, $outFile, $hashRef) = @_;
Subroutine _write_out_hash: called CC=3 SCAR=11.0
225  
226 2 my $fh;
227 2 ▶ open($fh, "> $outFile") or die "Can not open $outFile:$!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "> $outFile" or die "Can not open ${outFile}:$!" 0 2 2
228 # let's sort the hash so that every time the same order is
229 # produced
230 2 my @sortedKeys = sort keys(%$hashRef);
231 2 foreach my $k (@sortedKeys)
232 {
233 119 print $fh join($sep, $k, $hashRef->{$k}),"\n";
234 }
235 2 close $fh;
236  
237 2 return 1;
238 }
239  
240 # open a file and return its file handle
241 sub _open_file
242 {
243 15 ▶ my ($self, $file, $mode) = @_;
Subroutine _open_file: called CC=3 SCAR=11.3
244  
245 15 ▶ $mode ||= ' ';
Condition50%1/2
$mode ||= " "
l!l
015
MC/DC0%0/1
$mode ||= " "
$mode
Truth table50%1/2
A
A= $mode
Aresult
11
00
246  
247 15 my $fh;
248 15 ▶ open($fh, "$mode $file") or $self->throw("can not open $file:$!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "$mode $file" or $self->throw("can not open ${file}:$!") 0 15 15
249 #push @openFHs, $fh;
250 15 return $fh;
251 }
252  
253 # parse the first $num fields of input file, and use the vaule at the
254 # first column as key
255 sub _parse_file
256 {
257 2 ▶ my ($self, $file, $num) = @_;
Subroutine _parse_file: called CC=6 SCAR=18.8
258  
259 2 my %hash;
260 2 my $fh = $self->_open_file($file);
261 2 ▶ while(<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 119 2 121
262 {
263 119 ▶ next if /^#/ or /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or /^\s*$/ 0 119 119
Condition33%1/3
/^#/ or /^\s*$/
l!l&&r!l&&!r
00119
MC/DC0%0/2
/^#/ or /^\s*$/
/^#/ /^\s*$/
Truth table33%1/3
A or B
A= /^#/B= /^\s*$/
ABresult
1X1
011
000
264 119 chomp;
265 119 s/^\s+//; # remove leading blanks
266 119 my @fields = split /\s+/;
267 119 ▶ if($num > 1)
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 1) { } 119 0 119
268 {
269 119 ▶ $hash{uc($fields[0])} = $num > 2?
Branch: 1 branch
BranchTrueFalseTotal
$num > 2 ? : 0 119 119
270 [@fields[1..($num-1)]] : $fields[1];
271 }else
272 {
273 0 $hash{uc($fields[0])}++;
274 }
275 }
276 2 close $fh;
277 2 return \%hash;
278 }
279  
280 # this method is called when object of this or child classes is being
281 # destroyed
282 # close the file handle if the object has one
283 sub DESTROY
284 {
285 119 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
286 #print $self;
287 119 ▶ close $self->{'_fh'} if(exists $self->{'_fh'});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_fh"} 8 111 119
288 # $self->SUPER::DESTROY(@_);
289 }
290  
291 =head1 AUTHOR
292  
293 Zhenguo Zhang, C<< <zhangz.sci at gmail.com> >>
294  
295 =head1 BUGS
296  
297 Please report any bugs or feature requests to C<bug-bio-cua at
298 rt.cpan.org> or through the web interface at
299 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-CUA>. I will be
300 notified, and then you'll automatically be notified of progress on
301 your bug as I make changes.
302  
303 =cut
304  
305 =head1 SUPPORT
306  
307 You can find documentation for this class with the perldoc command.
308  
309 perldoc Bio::CUA
310  
311 You can also look for information at:
312  
313 =over 4
314  
315 =item * RT: CPAN's request tracker (report bugs here)
316  
317 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-CUA>
318  
319 =item * AnnoCPAN: Annotated CPAN documentation
320  
321 L<http://annocpan.org/dist/Bio-CUA>
322  
323 =item * CPAN Ratings
324  
325 L<http://cpanratings.perl.org/d/Bio-CUA>
326  
327 =item * Search CPAN
328  
329 L<http://search.cpan.org/dist/Bio-CUA/>
330  
331 =back
332  
333  
334 =head1 ACKNOWLEDGEMENTS
335  
336  
337 =head1 LICENSE AND COPYRIGHT
338  
339 Copyright 2015 Zhenguo Zhang.
340  
341 This program is free software: you can redistribute it and/or modify
342 it under the terms of the GNU General Public License as published by
343 the Free Software Foundation, either version 3 of the License, or
344 (at your option) any later version.
345  
346 This program is distributed in the hope that it will be useful,
347 but WITHOUT ANY WARRANTY; without even the implied warranty of
348 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
349 GNU General Public License for more details.
350  
351 You should have received a copy of the GNU General Public License
352 along with this program. If not, see L<http://www.gnu.org/licenses/>.
353  
354  
355 =cut
356  
357 6 1; # End of Bio::CUA
358  
« lib/Bio/CUA/CUB/Builder.pm ↑ Summary lib/Bio/CUA/SeqIO.pm »