AlignDB-Codon
view release on metacpan or search on metacpan
lib/AlignDB/Codon.pm view on Meta::CPAN
$s_cnt += $sub_s_cnt;
$n_cnt += $sub_n_cnt;
}
# calculate number of synonymous/non synonymous mutations for that
# codon and add to total
if ( $pathway != 0 ) {
$syn_cnt = ( $s_cnt / $pathway );
$nsy_cnt = ( $n_cnt / $pathway );
}
} # endif $diffcnt = 3
return ( $syn_cnt, $nsy_cnt );
}
# counts the number of nucleotide differences between 2 codons
# when 1 nucleotide is different, returns this value plus the codon index
# of which nucleotide is different
# when 2 nucleotides are different, returns this value plus the codon index
# of which nucleotide is the same
# So comp_codons() knows which nucleotides to change or not to change
sub count_diffs {
my $self = shift;
my $cod1 = shift;
my $cod2 = shift;
my $cnt = 0;
my $return_pos;
my @sames; # store same base position
my @diffs; # store diff base position
if ( length $cod1 != 3 or length $cod2 != 3 ) {
Carp::confess YAML::Syck::Dump( { cod1 => $cod1, cod2 => $cod2 } ), "Codon length error\n";
return ( $cnt, $return_pos );
}
# just for 2 differences
for ( 0 .. 2 ) {
if ( substr( $cod1, $_, 1 ) ne substr( $cod2, $_, 1 ) ) {
$cnt++;
push @diffs, $_;
}
else {
push @sames, $_;
}
}
if ( $cnt == 1 ) {
$return_pos = $diffs[0];
}
elsif ( $cnt == 2 ) {
$return_pos = $sames[0];
}
return ( $cnt, $return_pos );
}
sub translate {
my $self = shift;
my $seq = shift;
my $frame = shift;
# check $frame
if ( defined $frame ) {
if ( List::MoreUtils::PP::none { $_ == $frame } ( 0 .. 2 ) ) {
confess Dump( { frame => $frame } ), "Wrong frame\n";
}
}
else {
$frame = 0;
}
if ( $frame != 0 ) {
$seq = substr( $seq, $frame ); # delete first $frame bases from $seq
}
my $offset = length($seq) - ( length($seq) % 3 );
substr( $seq, $offset, length($seq), '' ); # now $seq is 3n bp
my $peptide = "";
my $codon2aa = $self->codon2aa;
my $codon_size = 3;
for ( my $i = 0; $i < ( length($seq) - ( $codon_size - 1 ) ); $i += $codon_size ) {
my $triplet = substr( $seq, $i, $codon_size );
if ( exists $codon2aa->{$triplet} ) {
$peptide .= $codon2aa->{$triplet};
}
else {
$peptide .= 'X';
}
}
return $peptide;
}
sub is_start_codon {
my $self = shift;
my $cod = shift;
$cod = uc $cod;
$cod =~ tr/U/T/;
my $table_starts = $self->table_starts;
my $codon_idx = $self->codon_idx;
if ( exists $codon_idx->{$cod} ) {
my $aa = substr( $table_starts, $codon_idx->{$cod}, 1 );
return $aa eq "M" ? 1 : 0;
}
else {
return 0;
}
}
sub is_ter_codon {
my $self = shift;
my $cod = shift;
$cod = uc $cod;
$cod =~ tr/U/T/;
my $table_content = $self->table_content;
my $codon_idx = $self->codon_idx;
if ( exists $codon_idx->{$cod} ) {
my $aa = substr( $table_content, $codon_idx->{$cod}, 1 );
return $aa eq "*" ? 1 : 0;
}
else {
return 0;
}
}
sub _load_aa_code {
my $self = shift;
( run in 1.523 second using v1.01-cache-2.11-cpan-df04353d9ac )