blib/lib/Bio/Tools/CodonOptTable.pm

filter: Statementstmt 93.3% 28 / 30 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 95.0% 38 / 40 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Bio::Tools::CodonOptTable;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Bio::Root::Root;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Bio::PrimarySeq;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Bio::SeqIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Bio::Tools::SeqStats;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Bio::Tools::CodonTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Bio::DB::GenBank;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 0 ▶use GD::Graph::bars;
Subroutine BEGIN: called CC=1 SCAR=2.6
14 use Text::Textile qw(textile);
15 use File::Slurp;
16  
17 our $VERSION = '1.05';
18  
19 use vars qw(@ISA %AMINOACID %GENETIC_CODE);
20  
21 @ISA = ( 'Bio::Root::Root', 'Bio::SeqIO', 'Bio::PrimarySeq' );
22  
23 %GENETIC_CODE = (
24 'TCA' => 'S', # Serine
25 'TCC' => 'S', # Serine
26 'TCG' => 'S', # Serine
27 'TCT' => 'S', # Serine
28 'TTC' => 'F', # Phenylalanine
29 'TTT' => 'F', # Phenylalanine
30 'TTA' => 'L', # Leucine
31 'TTG' => 'L', # Leucine
32 'TAC' => 'Y', # Tyrosine
33 'TAT' => 'Y', # Tyrosine
34 'TAA' => '*', # Stop
35 'TAG' => '*', # Stop
36 'TGC' => 'C', # Cysteine
37 'TGT' => 'C', # Cysteine
38 'TGA' => '*', # Stop
39 'TGG' => 'W', # Tryptophan
40 'CTA' => 'L', # Leucine
41 'CTC' => 'L', # Leucine
42 'CTG' => 'L', # Leucine
43 'CTT' => 'L', # Leucine
44 'CCA' => 'P', # Proline
45 'CCC' => 'P', # Proline
46 'CCG' => 'P', # Proline
47 'CCT' => 'P', # Proline
48 'CAC' => 'H', # Histidine
49 'CAT' => 'H', # Histidine
50 'CAA' => 'Q', # Glutamine
51 'CAG' => 'Q', # Glutamine
52 'CGA' => 'R', # Arginine
53 'CGC' => 'R', # Arginine
54 'CGG' => 'R', # Arginine
55 'CGT' => 'R', # Arginine
56 'ATA' => 'I', # Isoleucine
57 'ATC' => 'I', # Isoleucine
58 'ATT' => 'I', # Isoleucine
59 'ATG' => 'M', # Methionine
60 'ACA' => 'T', # Threonine
61 'ACC' => 'T', # Threonine
62 'ACG' => 'T', # Threonine
63 'ACT' => 'T', # Threonine
64 'AAC' => 'N', # Asparagine
65 'AAT' => 'N', # Asparagine
66 'AAA' => 'K', # Lysine
67 'AAG' => 'K', # Lysine
68 'AGC' => 'S', # Serine
69 'AGT' => 'S', # Serine
70 'AGA' => 'R', # Arginine
71 'AGG' => 'R', # Arginine
72 'GTA' => 'V', # Valine
73 'GTC' => 'V', # Valine
74 'GTG' => 'V', # Valine
75 'GTT' => 'V', # Valine
76 'GCA' => 'A', # Alanine
77 'GCC' => 'A', # Alanine
78 'GCG' => 'A', # Alanine
79 'GCT' => 'A', # Alanine
80 'GAC' => 'D', # Aspartic Acid
81 'GAT' => 'D', # Aspartic Acid
82 'GAA' => 'E', # Glutamic Acid
83 'GAG' => 'E', # Glutamic Acid
84 'GGA' => 'G', # Glycine
85 'GGC' => 'G', # Glycine
86 'GGG' => 'G', # Glycine
87 'GGT' => 'G', # Glycine
88 );
89  
90 %AMINOACID = (
91 'A' => 'Ala', 'R' => 'Arg', 'N' => 'Asn', 'D' => 'Asp',
92 'C' => 'Cys', 'Q' => 'Gln', 'E' => 'Glu', 'G' => 'Gly',
93 'H' => 'His', 'I' => 'Ile', 'L' => 'Leu', 'K' => 'Lys',
94 'M' => 'Met', 'F' => 'Phe', 'P' => 'Pro', 'S' => 'Ser',
95 'T' => 'Thr', 'W' => 'Trp', 'Y' => 'Tyr', 'V' => 'Val',
96 'B' => 'Asx', 'Z' => 'glx', 'X' => 'Xaa', '*' => 'Stop'
97 );
98  
99 sub new {
100 my ( $class, @args ) = @_;
101 my $self = $class->SUPER::new(@args);
102 my (
103 $seq, $id, $acc, $pid,
104 $desc, $alphabet, $given_id, $is_circular,
105 $file, $format, $ncbi_id, $genetic_code
106 )
107 = $self->_rearrange(
108 [
109 qw(
110 SEQ
111 DISPLAY_ID
112 ACCESSION_NUMBER
113 PRIMARY_ID
114 DESC
115 ALPHABET
116 ID
117 IS_CIRCULAR
118 FILE
119 FORMAT
120 NCBI_ID
121 GENETIC_CODE
122 )
123 ],
124 @args
125 );
126  
127 if ( $file && $format ) {
128 ( $seq, $id, $alphabet ) = $self->_read_localfile( $file, $format );
129 }
130 if ($ncbi_id) {
131 ( $seq, $id, $desc, $alphabet ) = $self->_read_remotefile($ncbi_id);
132 }
133  
134 $self = Bio::PrimarySeq->new(
135 -seq => $seq,
136 -id => $id,
137 -accession_number => $acc,
138 -display_id => $given_id,
139 -desc => $desc,
140 -primary_id => $pid,
141 -alphabet => $alphabet,
142 -is_circular => $is_circular
143 );
144 $self->{'genetic_code'} = $genetic_code;
145 _build_codons($self);
146 _map_codon_iupac($self);
147 return bless $self, $class;
148 }
149  
150 sub _build_codons {
151 my $self = shift;
152  
153 my $seq_stats = Bio::Tools::SeqStats->new( -seq => $self );
154 my $codons = $seq_stats->count_codons();
155 my $total_codons;
156 foreach my $codon ( keys %GENETIC_CODE ) {
157 if ( !exists $codons->{$codon} ) {
158 $codons->{$codon} = 0;
159 }
160 }
161 $self->{'codons'} = $codons;
162 $self->{'monomers_count'} = $seq_stats->count_monomers();
163 $self->{'seq_mol_weight'} = $seq_stats->get_mol_wt();
164 delete $codons->{ambiguous} if($codons->{ambiguous});
165
166 return 1;
167 }
168  
169 sub _read_remotefile {
170 my ( $self, $ncbi_id ) = @_;
171  
172 my ( $seq, $id, $desc, $alphabet );
173  
174 my $retrivefile = new Bio::DB::GenBank(
175 -retrievaltype => 'tempfile',
176 -format => 'Fasta'
177 );
178  
179 my $fetchedfile = $retrivefile->get_Stream_by_acc($ncbi_id);
180 my $seq_data = $fetchedfile->next_seq;
181  
182 $seq = $seq_data->seq;
183 $id = $seq_data->primary_id;
184 $desc = $seq_data->desc;
185 $alphabet = $seq_data->alphabet;
186  
187 return ( $seq, $id, $desc, $alphabet );
188 }
189  
190 sub _read_localfile {
191 my ( $self, $file, $format ) = @_;
192  
193 my ( $seq, $id, $alphabet );
194  
195 my $inputstream = Bio::SeqIO->new(
196 -file => $file,
197 -format => $format
198 );
199 my $input = $inputstream->next_seq();
200  
201 $seq = $input->seq();
202 $id = $input->id;
203 $alphabet = $input->alphabet;
204  
205 return ( $seq, $id, $alphabet );
206 }
207  
208 sub rscu_rac_table {
209 my $self = shift;
210  
211 my $codons = $self->{'codons'};
212 my ( @codons, $max_rscu ) = $self->_calculate_rscu();
213 my $rscu_rac = $self->_calculate_rac( @codons, $max_rscu );
214 my @sorted_codons_by_aa =
215 sort { $a->{aa_name} cmp $b->{aa_name} } @$rscu_rac;
216  
217 return \@sorted_codons_by_aa;
218 }
219  
220 sub _map_codon_iupac {
221 my $self = shift;
222  
223 my $myCodonTable = Bio::Tools::CodonTable->new();
224 my $codons = $self->{'codons'};
225 $self->{'genetic_code'} = 1 if ( !$self->{'genetic_code'} );
226  
227 $myCodonTable->id( $self->{'genetic_code'} );
228  
229 my $myCodons;
230  
231 foreach my $single_codon ( keys %$codons ) {
232 my $aa_name_abri = $myCodonTable->translate($single_codon);
233 my $aa_name = $AMINOACID{$aa_name_abri};
234 $myCodons->{$single_codon} = {
235 'frequency' => $codons->{$single_codon},
236 'aa_name' => $aa_name,
237 };
238 }
239 $self->{'codons'} = $myCodons;
240  
241 return 1;
242 }
243  
244 # Function : Calculate the RSCU(Relative Synonymous Codons Uses).
245 # Note : The formula is used in the following references.
246 # http://www.pubmedcentral.nih.gov/articlerender.fcgi?tool=pubmed&pubmedid=3547335
247  
248 sub _calculate_rscu {
249 my $self = shift;
250  
251 my $codons = $self->{'codons'};
252  
253 my ( $rscu, @myCodons, %rscu_max_table );
254  
255 foreach my $each_codon ( keys %$codons ) {
256 my $amino = $codons->{$each_codon}->{'aa_name'};
257 my $freq = $codons->{$each_codon}->{'frequency'};
258 my $count;
259 my $all_freq_aa = 0;
260 if ($amino) {
261 foreach my $goforall ( keys %$codons ) {
262 if ( $amino && ( $codons->{$goforall}->{'aa_name'} eq $amino ) )
263 {
264 $all_freq_aa += $codons->{$goforall}->{'frequency'};
265 $count++;
266 }
267 }
268 $rscu = $all_freq_aa > 0 ? $count * $freq / $all_freq_aa : 0.00;
269 }
270 if ( !defined( $rscu_max_table{$amino} )
271 || ( $rscu_max_table{$amino} < $rscu ) )
272 {
273 $rscu_max_table{$amino} = $rscu;
274 }
275 push @myCodons,
276 {
277 codon => $each_codon,
278 frequency => $freq,
279 aa_name => $amino,
280 rscu => $rscu,
281 total_aa_comb => $count,
282 all_fre_aa => $all_freq_aa,
283 };
284 }
285 return ( \@myCodons, \%rscu_max_table );
286 }
287  
288 # Function : Calculate the RAC (Relative Adaptiveness of a Codon).
289 # Note : The formula is used in the following references.
290 # http://www.pubmedcentral.nih.gov/articlerender.fcgi?tool=pubmed&pubmedid=3547335
291  
292 sub _calculate_rac {
293 my ( $self, $codons, $max_rscu ) = @_;
294 my ( $rac, @myCodons );
295  
296 foreach my $each_codon (@$codons) {
297 my $amino = $each_codon->{'aa_name'};
298 my $rscu = $each_codon->{'rscu'};
299 if ($amino) {
300 my $max = $max_rscu->{$amino};
301 $rac = $max > 0 ? $rscu / $max : 0.00;
302 push @myCodons,
303 {
304 'codon' => $each_codon->{'codon'},
305 'frequency' => $each_codon->{'frequency'},
306 'aa_name' => $amino,
307 'rscu' => sprintf( "%.2f", $rscu ),
308 'rac' => sprintf( "%.2f", $rac ),
309 };
310 }
311 }
312 return ( \@myCodons );
313 }
314  
315 # The CAI index is defined as the geometric mean of these relative adaptiveness values.
316 sub calculate_cai {
317 my ( $self, $rscu_rac ) = @_;
318  
319 my $count;
320 my $w;
321 foreach my $codon (@$rscu_rac) {
322 $w += $codon->{rac};
323 $count++;
324 }
325 my $cai = $w * ( 1 / $count );
326  
327 # A CAI of 1.0 is considered to be perfect in the desired expression organism, and a
328 # CAI of >0.9 is regarded as very good, in terms of high gene expression level.
329 return sprintf( "%.2f", $cai );
330 }
331  
332 sub prefered_codon {
333 my ( $self, $codons ) = @_;
334 my $prefered_codon;
335 for my $each_aa (@$codons) {
336 my $aa_name = $each_aa->{'aa_name'};
337 my $rscu = $each_aa->{'rscu'};
338 my $codon = $each_aa->{'codon'};
339 my $frequency = $each_aa->{'frequency'};
340  
341 if ( !defined( $prefered_codon->{$aa_name} )
342 || ( $prefered_codon->{$aa_name} < $rscu ) )
343 {
344 $prefered_codon->{$aa_name} = $codon;
345 }
346 }
347 return $prefered_codon;
348 }
349  
350 sub generate_graph {
351 my ( $self, $codons, $output_file ) = @_;
352  
353 my ( @x_axis_labels, @rscu, @rac, @x_axis_values, @codons_table,
354 @codon_freq );
355 my $y_axis_max = 8;
356 my @category_colours = qw(red dgreen);
357 my $bar_graph = new GD::Graph::bars( 1000, 500 );
358  
359 foreach my $each_aa (@$codons) {
360 if ( $each_aa->{'aa_name'} ) {
361 push( @codons_table,
362 $each_aa->{'aa_name'} . "(" . $each_aa->{'codon'} . ")" );
363 push( @codon_freq, $each_aa->{'frequency'} );
364 push( @x_axis_labels,
365 $each_aa->{'codon'} . "("
366 . $each_aa->{'frequency'} . ")" . "-"
367 . $each_aa->{'aa_name'} );
368 push( @rscu, $each_aa->{'rscu'} );
369 push( @rac, $each_aa->{'rac'} );
370 }
371 }
372  
373 my @bar_graph_table;
374 push( @bar_graph_table, \@x_axis_labels );
375 push( @bar_graph_table, \@rscu );
376 push( @bar_graph_table, \@rac );
377  
378 $bar_graph->set(
379 title =>
380 'Graph Representing : Relative Synonymous Codons Uses and Relative Adaptiveness of a Codon for '
381 . $self->display_id,
382 y_label => 'RSCU and RAC values', #y-axis label
383 y_max_value => $y_axis_max, #the max value of the y-axis
384 y_min_value => 0
385 , #the min value of y-axis, note set below 0 if negative values are required
386 y_tick_number => 20, #y-axis scale increment
387 y_label_skip => 1, #label every other y-axis marker
388 box_axis => 0, #do not draw border around graph
389 line_width => 2, #width of lines
390 legend_spacing => 5, #spacing between legend elements
391 legend_placement => 'RC', #put legend to the centre right of chart
392 dclrs => \@category_colours
393 , #reference to array of category colours for each line
394 bgclr => 'red',
395 long_ticks => 0,
396 tick_length => 3,
397 x_labels_vertical => 1,
398 ) || die "\nFailed to create line graph: $bar_graph->error()";
399  
400 $bar_graph->set_legend( 'RSCU Value', 'RAC Value' );
401  
402 my $plot = $bar_graph->plot( \@bar_graph_table );
403  
404 my $line_file = $output_file;
405 open( GPH, ">$line_file" )
406 || die("\nFailed to save graph to file: $line_file. $!");
407 binmode(GPH);
408 print GPH $plot->gif();
409 close(GPH);
410 }
411  
412 sub generate_report {
413 my ( $self, $out_file ) = @_;
414  
415 my $myCodons = $self->rscu_rac_table();
416 my $sequence_id = $self->id;
417 my $genetic_code = $self->{genetic_code};
418 my $monomers = $self->{monomers_count};
419 my $cai = $self->calculate_cai($myCodons);
420  
421 my %colors = (
422 'Ala' => '#f4f4f4', 'Arg' => '#FF99FF', 'Asn' => '#CC99CC',
423 'Asp' => '#99FFCC', 'Cys' => '#99CC99', 'Gln' => '#CCFF00',
424 'Glu' => '#FF00CC', 'Gly' => '#33FFCC', 'His' => '#66CCFF',
425 'Ile' => '#c9c9c9', 'Leu' => '#CCFFFF', 'Lys' => '#FFFFCC',
426 'Met' => '#FFFF66', 'Phe' => '#FFCC00', 'Pro' => '#F4F4F4',
427 'Ser' => '#FF99FF', 'Thr' => '#CC99CC', 'Trp' => '#99FFCC',
428 'Tyr' => '#CCFF00', 'Val' => '#33FFCC', 'Asx' => '#6666CC',
429 'glx' => '#FF00CC', 'Xaa' => '#cccccc', 'Stop' => '#f8f8f8'
430 );
431  
432 my $codons = "|%{color:red}CODON%|";
433 my $aa_name = "|%{color:red}AMINOACID%|";
434 my $counts = "|%{color:red}COUNT%|";
435 my $rscu = "|%{color:red}RSCU%|";
436 my $rac = "|%{color:red}RAC%|";
437  
438 foreach my $each_codon (@$myCodons) {
439 my $frequency = $each_codon->{frequency} || 'O';
440 my $amino_acid = $each_codon->{aa_name};
441 my $codon = $each_codon->{codon};
442 my $rscu_value = $each_codon->{rscu};
443 my $rac_value = $each_codon->{rac};
444 my $aa_color = $colors{$amino_acid};
445  
446 $codons .= "%{background:$aa_color}$codon%|";
447 $aa_name .= "%{background:$aa_color}$amino_acid%|";
448 $counts .= "%{background:$aa_color}$frequency%|";
449 $rscu .= "%{background:$aa_color}$rscu_value%|";
450 $rac .= "%{background:$aa_color}$rac_value%|";
451 }
452 my $codon_uses = "$codons\n$aa_name\n$counts\n";
453 my $rscu_uses = "$codons\n$aa_name\n$rscu\n";
454 my $rac_uses = "$codons\n$aa_name\n$rac\n";
455 my $mono_mers =
456 "|%{color:red}A%|"
457 . $monomers->{A} . "|\n"
458 . "|%{color:red}T%|"
459 . $monomers->{T} . "|\n"
460 . "|%{color:red}G%|"
461 . $monomers->{G} . "|\n"
462 . "|%{color:red}C%|"
463 . $monomers->{C} . "|\n";
464  
465 my $gc_percentage =
466 ( ( $monomers->{G} + $monomers->{C} ) /
467 ( $monomers->{A} + $monomers->{T} + $monomers->{G} + $monomers->{C} )
468 ) * 100;
469 $gc_percentage = sprintf( "%.2f", $gc_percentage );
470  
471 my $REPORT = <<EOT;
472 h1. Bio::Tools::CodonOptTable
473  
474 %{color:green}Report for $sequence_id%
475  
476 %{color:red}Codon Adaptation Index (CAI) for sequence% : $cai
477  
478 %{color:red}GC percentage for sequence% : $gc_percentage%
479  
480 %{color:red}GENETIC CODE USED% : $genetic_code "--more about genetic code--":http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi
481  
482 %{background:#336699;color:white;padding:5px}++**CODON USAGE**++%
483  
484 $codon_uses
485  
486 %{background:#336699;color:white;padding:5px}++**Relative Synonimous Codon Usage (RSCU)**++%
487  
488 $rscu_uses
489  
490 %{background:#336699;color:white;padding:5px}++**Relative Adaptiveness of Codon**++%
491  
492 $rac_uses
493  
494 %{background:#336699;color:white;padding:5px}++**Monomers**++%
495  
496 $mono_mers
497  
498 "Source code":http://search.cpan.org/~shardiwal/Bio-Tools-CodonOptTable-$VERSION/lib/Bio/Tools/CodonOptTable.pm is available.
499  
500 EOT
501  
502 my $html = textile($REPORT);
503 write_file( $out_file, $html );
504 }
505  
506 1; # End of Bio::Tools::CodonOptTable
507  
508 __END__
↑ Summary