Bio::CodonUsage Table
SummaryIncluded librariesPackage variablesSynopsisDescriptionGeneral documentationMethods
Summary
Bio::CodonUsage::Table - for access to the Codon usage Database
at http://www.kazusa.or.jp/codon.
Package variables
No package variables defined.
Included modules
Bio::SeqUtils
Bio::Tools::CodonTable
Inherit
Bio::Root::Root
Synopsis
  use Bio::CodonUsage::Table;
  use Bio::DB::CUTG;

  ## get  a codon usage table from web database ##
  my $cdtable = Bio::DB::CUTG->new(-sp => 'Mus musculus'
                                   -gc => 1);

  ## or from local file

  my $io = Bio::CodonUsage::IO->new(-file=>"file");
  my $cdtable= $io->next_data();

  ## or create your own from your own sequences 

  ## get a Bio::PrimarySeq compliant object ##
  # $codonstats is a ref to a hash of codon name /count key-value pairs.

  my $codonstats = Bio::Tools::SeqUtils->codon_count($my_1ary_Seq_objct);

  ### the '-data' field must be specified ##
  ### the '-species' and 'genetic_code' fields are optional
  my $CUT = Bio::CodonUsage::Table->new(-data =>$codonstats,
                                        -species => 'Hsapiens_kinase');

  print "leu frequency is ", $cdtable->aa_frequency('LEU'), "\n";
  print "freqof ATG is ", $cdtable->codon_rel_frequency('ttc'), "\n";
  print "abs freq of ATG is ", $cdtable->codon_abs_frequency('ATG'), "\n";
  print "number of ATG codons is ", $cdtable->codon_count('ATG'), "\n";
  print "gc content at position 1 is ", $cdtable->get_coding_gc('1'), "\n";
  print "total CDSs for Mus musculus  is ", $cdtable->cds_count(), "\n";
Description
This class provides methods for accessing codon usage table data.
All of the methods at present are simple look-ups of the table or are
derived from simple calculations from the table. Future methods could
include measuring the codon usage of a sequence , for example, or
provide methods for examining codon usage in alignments.
Methods
BEGIN Code
newDescriptionCode
all_aa_frequenciesDescriptionCode
codon_abs_frequencyDescriptionCode
codon_rel_frequencyDescriptionCode
codon_countDescriptionCode
get_coding_gcDescriptionCode
set_coding_gcDescriptionCode
speciesDescriptionCode
genetic_codeDescriptionCode
cds_countDescriptionCode
aa_frequencyDescriptionCode
_check_codon
No description
Code
_init_from_cod
No description
Code
_init_from_aa
No description
Code
Methods description
newcode    nextTop
 Title   : new
 Usage   : my $cut = Bio::CodonUsage::Table->new(-data => $cut_hash_ref,
                                                 -species => 'H.sapiens_kinase'
                                                 -genetic_code =>1);
 Returns : a reference to a new  Bio::CodonUsage::Table object
 Args    : none or a reference to a hash of codon counts. This constructor is
           designed to be compatible with the output of
           Bio::Tools::SeqUtils::count_codons()
           Species and genetic code parameters can be entered here or via the 
           species() and genetic_code() methods separately.
all_aa_frequenciescodeprevnextTop
 Title   : all_aa_frequencies
 Usage   : my $freq = $cdtable->all_aa_frequencies();
 Returns : a reference to a hash where each key is an amino acid
           and each value is its frequency in all proteins in that
           species.
 Args    : none
codon_abs_frequencycodeprevnextTop
 Title   : codon_abs_frequency
 Usage   : my $freq = $cdtable->codon_abs_frequency('CTG');
 Purpose : To return the frequency of that codon as a percentage
           of all codons in the organism. 
 Returns : a percentage frequency
 Args    : a non-ambiguous codon string
codon_rel_frequencycodeprevnextTop
 Title   : codon_rel_frequency
 Usage   : my $freq = $cdtable->codon_rel_frequency('CTG');
 Purpose : To return the frequency of that codon as a percentage
           of codons coding for the same amino acid. E.g., ATG and TGG
           would return 100 as those codons are unique.
 Returns : a percentage frequency
 Args    : a non-ambiguous codon string
codon_countcodeprevnextTop
 Title   : codon_count
 Usage   : my $count = $cdtable->codon_count('CTG');
 Purpose : To obtain the absolute number of the codons in the
           organism. 
 Returns : an integer
 Args    : a non-ambiguous codon string
get_coding_gccodeprevnextTop
 Title   : get_coding_gc
 Usage   : my $count = $cdtable->get_coding_gc(1);
 Purpose : To return the percentage GC composition for the organism at
           codon positions 1,2 or 3, or an average for all coding sequence
          ('all').
 Returns : a number (%-age GC content) or 0 if these fields are undefined
 Args    : 1,2,3 or 'all'.
set_coding_gccodeprevnextTop
 Title   : set_coding_gc
 Usage   : my $count = $cdtable->set_coding_gc(-1=>55.78);
 Purpose : To set the percentage GC composition for the organism at
           codon positions 1,2 or 3, or an average for all coding sequence
           ('all').  
 Returns : void
 Args    : a hash where the key must be 1,2,3 or 'all' and the value the %age GC
           at that codon position..
speciescodeprevnextTop
 Title     : species
 Usage     : my $sp = $cut->species();
 Purpose   : Get/setter for species name of codon table
 Returns   : Void or species name string
 Args      : None or species name string
genetic_codecodeprevnextTop
 Title     : genetic_code
 Usage     : my $sp = $cut->genetic_code();
 Purpose   : Get/setter for genetic_code name of codon table
 Returns   : Void or genetic_code id, 1 by default
 Args      : None or genetic_code id, 1 by default if invalid argument.
cds_countcodeprevnextTop
 Title   : cds_count
 Usage   : my $count = $cdtable->cds_count();
 Purpose : To retrieve the total number of CDSs used to generate the Codon Table
           for that organism. 
 Returns : an integer
 Args    : none (if retrieving the value) or an integer( if setting ).
aa_frequencycodeprevnextTop
 Title   : aa_frequency
 Usage   : my $freq = $cdtable->aa_frequency('Leu');
 Purpose : To retrieve the frequency of an amino acid in the organism
 Returns : a percentage
 Args    : a 1 letter or 3 letter string representing the amino acid
Methods code
BEGINTop
BEGIN {
 @AA = qw(A C D E F G H I K L M N P Q R S T V W Y);
 map {$STRICTAA{$_} = undef} @AA;
}
newdescriptionprevnextTop
sub new {
	my ($class, @args) = @_;
	my $self= $class->SUPER::new(@args);
	if (@args) {
		$self->_rearrange([qw(DATA)], @args);
		shift @args; # get rid of key
my $arg = shift @args; $self->throw("need a hash reference, not a [" . ref($arg). "] reference") if ref($arg) ne 'HASH'; ### flags to detect argument type, can be either to start with ##
my $is_codon_hash = 1; my $is_Aa_hash = 1; for my $k (keys %$arg) { ## cpnvert to UC
$k =~ s/(\w+)/\U$1/; if (!exists($STRICTAA{$k}) ){ $is_Aa_hash = 0; } elsif ($k =~ /[^ATCGatcg]/) { $is_codon_hash = 0; } } if (!$is_codon_hash && !$is_Aa_hash) { $self->throw(" invalid key values in CUT hash - must be unique aa or nucleotide identifiers"); } elsif ($is_Aa_hash) { $self->_init_from_aa($arg); } elsif($is_codon_hash) { $self->_init_from_cod($arg); } while (@args) { my $key = shift @args; $key =~ s/\-(\w+)/\L($1)/; $self->$key(shift @args); } } return $self;
}
all_aa_frequenciesdescriptionprevnextTop
sub all_aa_frequencies {
	my $self = shift;
	my %aa_freqs =();
	for my $aa (keys %STRICTAA) {
		my $freq = $self->aa_frequency($aa);
		$aa_freqs{$aa} = $freq;
		}
	return\% aa_freqs;
}
codon_abs_frequencydescriptionprevnextTop
sub codon_abs_frequency {
	my ($self, $a) = @_;
	my $cod = uc $a;
	if ($self->_check_codon($cod))  {
		my $ctable =  Bio::Tools::CodonTable->new;
		$ctable->id($self->genetic_code() );
		my $aa =$Bio::SeqUtils::THREECODE {$ctable->translate($cod)};

		return $self->{'_table'}{$aa}{$cod}{'per1000'}/10 ;
} else {return 0;}
}
codon_rel_frequencydescriptionprevnextTop
sub codon_rel_frequency {
	my ($self, $a) = @_;
	my $cod = uc $a;
	if ($self->_check_codon($cod)) {
		my $ctable =  Bio::Tools::CodonTable->new;
		$ctable->id($self->genetic_code () );
		my $aa =$Bio::SeqUtils::THREECODE {$ctable->translate($cod)};
		return $self->{'_table'}{$aa}{$cod}{'rel_freq'};
	}
	else {
		return 0;
		}
}
codon_countdescriptionprevnextTop
sub codon_count {
	my $self = shift;
	if (@_) {
		my $a = shift;
		my $cod = uc $a;
		if ($self->_check_codon($cod)) {
			my $ctable =  Bio::Tools::CodonTable->new;
			$ctable->id($self->genetic_code());
			my $aa =$Bio::SeqUtils::THREECODE {$ctable->translate($cod)};
			return $self->{'_table'}{$aa}{$cod}{'abs_count'};
			}
		else {return 0;}
	}
	else {
		$self->warn(" need to give a codon sequence as a parameter ");
		return 0;
		}
}
get_coding_gcdescriptionprevnextTop
sub get_coding_gc {
	my $self  = shift;
	if (! @_) {
		$self->warn(" no parameters supplied must be  a codon position (1,2,3) or 'all'");
		return 0;
			}
	else{
		my $n = shift;
		##return request if valid ##
if ( exists($self->{'_coding_gc'}{$n} ) ) { return sprintf("%.2f", $self->{'_coding_gc'}{$n}); } ##else return 'all' value if exists
elsif (exists($self->{'_coding_gc'}{'all'} )) { $self->warn("coding gc doesn't have value for [$n], returning gc content for all CDSs"); return sprintf("%.2f", $self->{'_coding_gc'}{'all'}); } ### else return 0,
else { $self->warn("coding gc values aren't defined, returning 0"); return 0; } }#end of outer else
}
set_coding_gcdescriptionprevnextTop
sub set_coding_gc {
	my ($self, $key, $value) = @_;
	my @allowed = qw(1 2 3 all);
	$key =~ s/\-//;
	if (!grep {$key eq $_} @allowed ) {
		$self->warn ("invalid key! - must be one of [ ". (join " ", @allowed) . "]");
		return;
		}
	$self->{'_coding_gc'}{$key} = $value;
}
speciesdescriptionprevnextTop
sub species {
	my $self = shift;
	if (@_ ){
		$self->{'_species'} = shift;
		}
	return $self->{'_species'} || "unknown";
}
genetic_codedescriptionprevnextTop
sub genetic_code {
	my $self = shift;
	if (@_ ){
		my $val = shift;
		if ($val < 0 || $val >16 || $val =~ /[^\d]/ 
				|| $val ==7 || $val ==8) {
			$self->warn ("invalid genetic code - must be 1-16 but not 7 or 8,setting to default [1]");
			$self->{'_genetic_code'} = 1;
			}
		else {
			$self->{'_genetic_code'} = shift;
			}
		}
	return $self->{'_genetic_code'} || 1;
}
cds_countdescriptionprevnextTop
sub cds_count {
	my $self= shift;
	if (@_) {
		my $val = shift;
		if ($val < 0) {
			$self->warn("can't have negative count initializing to 1");
			$self->{'_cds_count'} = 0.00;
			}
		else{
			$self->{'_cds_count'} = $val;
		}
	}
	$self->warn("cds_count value is undefined, returning 0") 
		if !exists($self->{'_cds_count'});

	return $self->{'_cds_count'} || 0.00;
}
aa_frequencydescriptionprevnextTop
sub aa_frequency {
	my ($self, $a) = @_;
	## process args ##
## deal with cases ##
my $aa = lc $a; $aa =~ s/^(\w)/\U$1/; if (!exists($STRICTAA{$aa}) && !exists($Bio::SeqUtils::ONECODE{$aa}) ) { $self->warn("Invalid amino acid! must be a unique 1 letter or 3 letter identifier"); return; } #translate to 3 letter code for Ctable #
my $aa3 = $Bio::SeqUtils::THREECODE{$aa} || $aa; ## return % of all amino acids in organism ##
my $freq = 0; map {$freq += $self->{'_table'}{$aa3}{$_}{'per1000'} } keys %{$self->{'_table'}{$aa3}}; return sprintf("%.2f", $freq/10);
}
_check_codondescriptionprevnextTop
sub _check_codon {
	my ($self, $cod) = @_;
	if ($cod =~ /[^ATCG]/  || $cod !~ /\w\w\w/) {
		$self->warn(" impossible codon - must be 3 letters and just containing ATCG");
		return 0;
	}
	else {return 1;}
}
_init_from_coddescriptionprevnextTop
sub _init_from_cod {
	## make hash based on aa and then send to _init_from_aa
my ($self, $ref) = @_; my $ct = Bio::Tools::CodonTable->new(); my %aa_hash; for my $codon(keys %$ref ) { my $aa = $ct->translate($codon); $aa_hash{$aa}{$codon} = $ref->{$codon}; } $self->_init_from_aa(\%aa_hash);
}
_init_from_aadescriptionprevnextTop
sub _init_from_aa {
	my ($self, $ref) = @_;
		## abs counts  and count codons
my $total_codons = 0; my %threeletter; map{$threeletter{$Bio::SeqUtils::THREECODE{$_}} = $ref->{$_} } keys %$ref; $ref =\% threeletter; for my $aa (keys %$ref) { for my $cod(keys %{$ref->{$aa}} ) { $self->{'_table'}{$aa}{$cod}{'abs_count'} = $ref->{$aa}{$cod}; $total_codons += $ref->{$aa}{$cod}; } } ## now calculate abs codon frequencies
for my $aa (keys %$ref) { for my $cod(keys %{$ref->{$aa}} ) { $self->{'_table'}{$aa}{$cod}{'per1000'} = sprintf("%.2f",$ref->{$aa}{$cod} /$total_codons * 1000) ;
} } ## now calculate rel codon_frequencies
for my $aa (keys %$ref) { my $aa_freq = 0; map{$aa_freq += $ref->{$aa}{$_} } keys %{$ref->{$aa}}; for my $cod(keys %{$ref->{$aa}} ) { $self->{'_table'}{$aa}{$cod}{'rel_freq'}= sprintf("%.2f",$ref->{$aa}{$cod}/ $aa_freq );
} } ## now calculate gc fields
my %GC; for my $aa (keys %$ref) { for my $cod(keys %{$ref->{$aa}} ) { for my $index (qw(1 2 3) ) { if (substr ($cod, $index -1, 1) =~ /g|c/oi) { $GC{$index} += $ref->{$aa}{$cod}; } } } } my $tot = 0; map{$tot += $GC{$_}} qw(1 2 3); $self->set_coding_gc('all', $tot/(3 *$total_codons) * 100);
map{$self->set_coding_gc($_,$GC{$_}/$total_codons * 100)} qw(1 2 3);
##
return $self; } sub _gb_db { my $self = shift; return $self->{'_gd_db'} || "unknown"; } 1;
}
General documentation
SEE ALSOTop
Bio::Tools::CodonTable,
Bio::WebAgent,
Bio::CodonUsage::IO,
Bio::DB::CUTG
FEEDBACKTop
Mailing ListsTop
User feedback is an integral part of the evolution of this and other
Bioperl modules. Send your comments and suggestions preferably to one
of the Bioperl mailing lists. Your participation is much appreciated.
  bioperl-l@bioperl.org                       - General discussion
  http://bio.perl.org/MailList.html           - About the mailing lists
Reporting BugsTop
Report bugs to the Bioperl bug tracking system to help us keep track
the bugs and their resolution. Bug reports can be submitted via email
or the web:
  bioperl-bugs@bio.perl.org
  http://bugzilla.bioperl.org/
AUTHORSTop
Richard Adams, Richard.Adams@ed.ac.uk
APPENDIXTop
The rest of the documentation details each of the object
methods. Internal methods are usually preceded with a _