| 1 | # | ||
| 2 | # bioperl module for FAST::Bio::Tools::CodonTable | ||
| 3 | # | ||
| 4 | # Please direct questions and support issues to <bioperl-l@bioperl.org> | ||
| 5 | # | ||
| 6 | # Cared for by Heikki Lehvaslaiho <heikki-at-bioperl-dot-org> | ||
| 7 | # | ||
| 8 | # Copyright Heikki Lehvaslaiho | ||
| 9 | # | ||
| 10 | # You may distribute this module under the same terms as perl itself | ||
| 11 | |||
| 12 | # POD documentation - main docs before the code | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | FAST::Bio::Tools::CodonTable - Codon table object | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | # This is a read-only class for all known codon tables. The IDs are | ||
| 21 | # the ones used by nucleotide sequence databases. All common IUPAC | ||
| 22 | # ambiguity codes for DNA, RNA and amino acids are recognized. | ||
| 23 | |||
| 24 | use FAST::Bio::Tools::CodonTable; | ||
| 25 | |||
| 26 | # defaults to ID 1 "Standard" | ||
| 27 | $myCodonTable = FAST::Bio::Tools::CodonTable->new(); | ||
| 28 | $myCodonTable2 = FAST::Bio::Tools::CodonTable->new( -id => 3 ); | ||
| 29 | |||
| 30 | # change codon table | ||
| 31 | $myCodonTable->id(5); | ||
| 32 | |||
| 33 | # examine codon table | ||
| 34 | print join (' ', "The name of the codon table no.", $myCodonTable->id(4), | ||
| 35 | "is:", $myCodonTable->name(), "\n"); | ||
| 36 | |||
| 37 | # print possible codon tables | ||
| 38 | $tables = FAST::Bio::Tools::CodonTable->tables; | ||
| 39 | while ( ($id,$name) = each %{$tables} ) { | ||
| 40 | print "$id = $name\n"; | ||
| 41 | } | ||
| 42 | |||
| 43 | # translate a codon | ||
| 44 | $aa = $myCodonTable->translate('ACU'); | ||
| 45 | $aa = $myCodonTable->translate('act'); | ||
| 46 | $aa = $myCodonTable->translate('ytr'); | ||
| 47 | |||
| 48 | # reverse translate an amino acid | ||
| 49 | @codons = $myCodonTable->revtranslate('A'); | ||
| 50 | @codons = $myCodonTable->revtranslate('Ser'); | ||
| 51 | @codons = $myCodonTable->revtranslate('Glx'); | ||
| 52 | @codons = $myCodonTable->revtranslate('cYS', 'rna'); | ||
| 53 | |||
| 54 | # reverse translate an entire amino acid sequence into a IUPAC | ||
| 55 | # nucleotide string | ||
| 56 | |||
| 57 | my $seqobj = FAST::Bio::PrimarySeq->new(-seq => 'FHGERHEL'); | ||
| 58 | my $iupac_str = $myCodonTable->reverse_translate_all($seqobj); | ||
| 59 | |||
| 60 | # boolean tests | ||
| 61 | print "Is a start\n" if $myCodonTable->is_start_codon('ATG'); | ||
| 62 | print "Is a terminator\n" if $myCodonTable->is_ter_codon('tar'); | ||
| 63 | print "Is a unknown\n" if $myCodonTable->is_unknown_codon('JTG'); | ||
| 64 | |||
| 65 | =head1 DESCRIPTION | ||
| 66 | |||
| 67 | Codon tables are also called translation tables or genetic codes | ||
| 68 | since that is what they represent. A bit more complete picture | ||
| 69 | of the full complexity of codon usage in various taxonomic groups | ||
| 70 | is presented at the NCBI Genetic Codes Home page. | ||
| 71 | |||
| 72 | CodonTable is a BioPerl class that knows all current translation | ||
| 73 | tables that are used by primary nucleotide sequence databases | ||
| 74 | (GenBank, EMBL and DDBJ). It provides methods to output information | ||
| 75 | about tables and relationships between codons and amino acids. | ||
| 76 | |||
| 77 | This class and its methods recognized all common IUPAC ambiguity codes | ||
| 78 | for DNA, RNA and animo acids. The translation method follows the | ||
| 79 | conventions in EMBL and TREMBL databases. | ||
| 80 | |||
| 81 | It is a nuisance to separate RNA and cDNA representations of nucleic | ||
| 82 | acid transcripts. The CodonTable object accepts codons of both type as | ||
| 83 | input and allows the user to set the mode for output when reverse | ||
| 84 | translating. Its default for output is DNA. | ||
| 85 | |||
| 86 | Note: | ||
| 87 | |||
| 88 | This class deals primarily with individual codons and amino | ||
| 89 | acids. However in the interest of speed you can L<translate> | ||
| 90 | longer sequence, too. The full complexity of protein translation | ||
| 91 | is tackled by L<FAST::Bio::PrimarySeqI::translate>. | ||
| 92 | |||
| 93 | |||
| 94 | The amino acid codes are IUPAC recommendations for common amino acids: | ||
| 95 | |||
| 96 | A Ala Alanine | ||
| 97 | R Arg Arginine | ||
| 98 | N Asn Asparagine | ||
| 99 | D Asp Aspartic acid | ||
| 100 | C Cys Cysteine | ||
| 101 | Q Gln Glutamine | ||
| 102 | E Glu Glutamic acid | ||
| 103 | G Gly Glycine | ||
| 104 | H His Histidine | ||
| 105 | I Ile Isoleucine | ||
| 106 | L Leu Leucine | ||
| 107 | K Lys Lysine | ||
| 108 | M Met Methionine | ||
| 109 | F Phe Phenylalanine | ||
| 110 | P Pro Proline | ||
| 111 | O Pyl Pyrrolysine (22nd amino acid) | ||
| 112 | U Sec Selenocysteine (21st amino acid) | ||
| 113 | S Ser Serine | ||
| 114 | T Thr Threonine | ||
| 115 | W Trp Tryptophan | ||
| 116 | Y Tyr Tyrosine | ||
| 117 | V Val Valine | ||
| 118 | B Asx Aspartic acid or Asparagine | ||
| 119 | Z Glx Glutamine or Glutamic acid | ||
| 120 | J Xle Isoleucine or Valine (mass spec ambiguity) | ||
| 121 | X Xaa Any or unknown amino acid | ||
| 122 | |||
| 123 | |||
| 124 | It is worth noting that, "Bacterial" codon table no. 11 produces an | ||
| 125 | polypeptide that is, confusingly, identical to the standard one. The | ||
| 126 | only differences are in available initiator codons. | ||
| 127 | |||
| 128 | |||
| 129 | NCBI Genetic Codes home page: | ||
| 130 | http://www.ncbi.nlm.nih.gov/Taxonomy/Utils/wprintgc.cgi?mode=c | ||
| 131 | |||
| 132 | EBI Translation Table Viewer: | ||
| 133 | http://www.ebi.ac.uk/cgi-bin/mutations/trtables.cgi | ||
| 134 | |||
| 135 | Amended ASN.1 version with ids 16 and 21 is at: | ||
| 136 | ftp://ftp.ebi.ac.uk/pub/databases/geneticcode/ | ||
| 137 | |||
| 138 | Thanks to Matteo diTomasso for the original Perl implementation | ||
| 139 | of these tables. | ||
| 140 | |||
| 141 | =head1 FEEDBACK | ||
| 142 | |||
| 143 | =head2 Mailing Lists | ||
| 144 | |||
| 145 | User feedback is an integral part of the evolution of this and other | ||
| 146 | Bioperl modules. Send your comments and suggestions preferably to the | ||
| 147 | Bioperl mailing lists Your participation is much appreciated. | ||
| 148 | |||
| 149 | bioperl-l@bioperl.org - General discussion | ||
| 150 | http://bioperl.org/wiki/Mailing_lists - About the mailing lists | ||
| 151 | |||
| 152 | =head2 Support | ||
| 153 | |||
| 154 | Please direct usage questions or support issues to the mailing list: | ||
| 155 | |||
| 156 | I<bioperl-l@bioperl.org> | ||
| 157 | |||
| 158 | rather than to the module maintainer directly. Many experienced and | ||
| 159 | reponsive experts will be able look at the problem and quickly | ||
| 160 | address it. Please include a thorough description of the problem | ||
| 161 | with code and data examples if at all possible. | ||
| 162 | |||
| 163 | =head2 Reporting Bugs | ||
| 164 | |||
| 165 | Report bugs to the Bioperl bug tracking system to help us keep track | ||
| 166 | the bugs and their resolution. Bug reports can be submitted via the | ||
| 167 | web: | ||
| 168 | |||
| 169 | https://redmine.open-bio.org/projects/bioperl/ | ||
| 170 | |||
| 171 | =head1 AUTHOR - Heikki Lehvaslaiho | ||
| 172 | |||
| 173 | Email: heikki-at-bioperl-dot-org | ||
| 174 | |||
| 175 | =head1 APPENDIX | ||
| 176 | |||
| 177 | The rest of the documentation details each of the object | ||
| 178 | methods. Internal methods are usually preceded with a _ | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | # Let the code begin... | ||
| 183 | |||
| 184 | package FAST::Bio::Tools::CodonTable; | ||
| 185 | use vars qw(@NAMES @TABLES @STARTS $TRCOL $CODONS %IUPAC_DNA $CODONGAP $GAP | ||
| 186 | %IUPAC_AA %THREELETTERSYMBOLS $VALID_PROTEIN $TERMINATOR); | ||
| 187 | use strict; | ||
| 188 | |||
| 189 | # Object preamble - inherits from FAST::Bio::Root::Root | ||
| 190 | use FAST::Bio::Tools::IUPAC; | ||
| 191 | use FAST::Bio::SeqUtils; | ||
| 192 | |||
| 193 | use base qw(FAST::Bio::Root::Root); | ||
| 194 | |||
| 195 | |||
| 196 | # first set internal values for all translation tables | ||
| 197 | |||
| 198 | BEGIN { | ||
| 199 | use constant CODONSIZE => 3; | ||
| 200 | $GAP = '-'; | ||
| 201 | $CODONGAP = $GAP x CODONSIZE; | ||
| 202 | |||
| 203 | @NAMES = #id | ||
| 204 | ( | ||
| 205 | 'Standard', #1 | ||
| 206 | 'Vertebrate Mitochondrial',#2 | ||
| 207 | 'Yeast Mitochondrial',# 3 | ||
| 208 | 'Mold, Protozoan, and CoelenterateMitochondrial and Mycoplasma/Spiroplasma',#4 | ||
| 209 | 'Invertebrate Mitochondrial',#5 | ||
| 210 | 'Ciliate, Dasycladacean and Hexamita Nuclear',# 6 | ||
| 211 | '', '', | ||
| 212 | 'Echinoderm Mitochondrial',#9 | ||
| 213 | 'Euplotid Nuclear',#10 | ||
| 214 | '"Bacterial"',# 11 | ||
| 215 | 'Alternative Yeast Nuclear',# 12 | ||
| 216 | 'Ascidian Mitochondrial',# 13 | ||
| 217 | 'Flatworm Mitochondrial',# 14 | ||
| 218 | 'Blepharisma Nuclear',# 15 | ||
| 219 | 'Chlorophycean Mitochondrial',# 16 | ||
| 220 | '', '', '', '', | ||
| 221 | 'Trematode Mitochondrial',# 21 | ||
| 222 | 'Scenedesmus obliquus Mitochondrial', #22 | ||
| 223 | 'Thraustochytrium Mitochondrial' #23 | ||
| 224 | ); | ||
| 225 | |||
| 226 | @TABLES = | ||
| 227 | qw( | ||
| 228 | FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 229 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSS**VVVVAAAADDEEGGGG | ||
| 230 | FFLLSSSSYY**CCWWTTTTPPPPHHQQRRRRIIMMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 231 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 232 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSSSSVVVVAAAADDEEGGGG | ||
| 233 | FFLLSSSSYYQQCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 234 | '' '' | ||
| 235 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNNKSSSSVVVVAAAADDEEGGGG | ||
| 236 | FFLLSSSSYY**CCCWLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 237 | FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 238 | FFLLSSSSYY**CC*WLLLSPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 239 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNKKSSGGVVVVAAAADDEEGGGG | ||
| 240 | FFLLSSSSYYY*CCWWLLLLPPPPHHQQRRRRIIIMTTTTNNNKSSSSVVVVAAAADDEEGGGG | ||
| 241 | FFLLSSSSYY*QCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 242 | FFLLSSSSYY*LCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 243 | '' '' '' '' | ||
| 244 | FFLLSSSSYY**CCWWLLLLPPPPHHQQRRRRIIMMTTTTNNNKSSSSVVVVAAAADDEEGGGG | ||
| 245 | FFLLSS*SYY*LCC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 246 | FF*LSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG | ||
| 247 | ); | ||
| 248 | |||
| 249 | # (bases used for these tables, for reference) | ||
| 250 | # 1 TTTTTTTTTTTTTTTTCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAGGGGGGGGGGGGGGGG | ||
| 251 | # 2 TTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGGTTTTCCCCAAAAGGGG | ||
| 252 | # 3 TCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAGTCAG | ||
| 253 | |||
| 254 | @STARTS = | ||
| 255 | qw( | ||
| 256 | ---M---------------M---------------M---------------------------- | ||
| 257 | --------------------------------MMMM---------------M------------ | ||
| 258 | ----------------------------------MM---------------------------- | ||
| 259 | --MM---------------M------------MMMM---------------M------------ | ||
| 260 | ---M----------------------------MMMM---------------M------------ | ||
| 261 | -----------------------------------M---------------------------- | ||
| 262 | '' '' | ||
| 263 | -----------------------------------M---------------------------- | ||
| 264 | -----------------------------------M---------------------------- | ||
| 265 | ---M---------------M------------MMMM---------------M------------ | ||
| 266 | -------------------M---------------M---------------------------- | ||
| 267 | -----------------------------------M---------------------------- | ||
| 268 | -----------------------------------M---------------------------- | ||
| 269 | -----------------------------------M---------------------------- | ||
| 270 | -----------------------------------M---------------------------- | ||
| 271 | '' '' '' '' | ||
| 272 | -----------------------------------M---------------M------------ | ||
| 273 | -----------------------------------M---------------------------- | ||
| 274 | --------------------------------M--M---------------M------------ | ||
| 275 | ); | ||
| 276 | |||
| 277 | my @nucs = qw(t c a g); | ||
| 278 | my $x = 0; | ||
| 279 | ($CODONS, $TRCOL) = ({}, {}); | ||
| 280 | for my $i (@nucs) { | ||
| 281 | for my $j (@nucs) { | ||
| 282 | for my $k (@nucs) { | ||
| 283 | my $codon = "$i$j$k"; | ||
| 284 | $CODONS->{$codon} = $x; | ||
| 285 | $TRCOL->{$x} = $codon; | ||
| 286 | $x++; | ||
| 287 | } | ||
| 288 | } | ||
| 289 | } | ||
| 290 | %IUPAC_DNA = FAST::Bio::Tools::IUPAC->iupac_iub(); | ||
| 291 | %IUPAC_AA = FAST::Bio::Tools::IUPAC->iupac_iup(); | ||
| 292 | %THREELETTERSYMBOLS = FAST::Bio::SeqUtils->valid_aa(2); | ||
| 293 | $VALID_PROTEIN = '['.join('',FAST::Bio::SeqUtils->valid_aa(0)).']'; | ||
| 294 | $TERMINATOR = '*'; | ||
| 295 | } | ||
| 296 | |||
| 297 | sub new { | ||
| 298 | my($class,@args) = @_; | ||
| 299 | my $self = $class->SUPER::new(@args); | ||
| 300 | |||
| 301 | my($id) = | ||
| 302 | $self->_rearrange([qw(ID | ||
| 303 | )], | ||
| 304 | @args); | ||
| 305 | |||
| 306 | $id = 1 if ( ! $id ); | ||
| 307 | $id && $self->id($id); | ||
| 308 | return $self; # success - we hope! | ||
| 309 | } | ||
| 310 | |||
| 311 | =head2 id | ||
| 312 | |||
| 313 | Title : id | ||
| 314 | Usage : $obj->id(3); $id_integer = $obj->id(); | ||
| 315 | Function: Sets or returns the id of the translation table. IDs are | ||
| 316 | integers from 1 to 15, excluding 7 and 8 which have been | ||
| 317 | removed as redundant. If an invalid ID is given the method | ||
| 318 | returns 0, false. | ||
| 319 | Example : | ||
| 320 | Returns : value of id, a scalar, 0 if not a valid | ||
| 321 | Args : newvalue (optional) | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | sub id{ | ||
| 326 | my ($self,$value) = @_; | ||
| 327 | if( defined $value) { | ||
| 328 | if ( !(defined $TABLES[$value-1]) or $TABLES[$value-1] eq '') { | ||
| 329 | $self->warn("Not a valid codon table ID [$value] "); | ||
| 330 | $value = 0; | ||
| 331 | } | ||
| 332 | $self->{'id'} = $value; | ||
| 333 | } | ||
| 334 | return $self->{'id'}; | ||
| 335 | } | ||
| 336 | |||
| 337 | =head2 name | ||
| 338 | |||
| 339 | Title : name | ||
| 340 | Usage : $obj->name() | ||
| 341 | Function: returns the descriptive name of the translation table | ||
| 342 | Example : | ||
| 343 | Returns : A string | ||
| 344 | Args : None | ||
| 345 | |||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | sub name{ | ||
| 350 | my ($self) = @_; | ||
| 351 | |||
| 352 | my ($id) = $self->{'id'}; | ||
| 353 | return $NAMES[$id-1]; | ||
| 354 | } | ||
| 355 | |||
| 356 | =head2 tables | ||
| 357 | |||
| 358 | Title : tables | ||
| 359 | Usage : $obj->tables() or FAST::Bio::Tools::CodonTable->tables() | ||
| 360 | Function: returns a hash reference where each key is a valid codon | ||
| 361 | table id() number, and each value is the corresponding | ||
| 362 | codon table name() string | ||
| 363 | Example : | ||
| 364 | Returns : A hashref | ||
| 365 | Args : None | ||
| 366 | |||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub tables{ | ||
| 371 | my %tables; | ||
| 372 | for my $id (1 .. @NAMES) { | ||
| 373 | my $name = $NAMES[$id-1]; | ||
| 374 | $tables{$id} = $name if $name; | ||
| 375 | } | ||
| 376 | return \%tables; | ||
| 377 | } | ||
| 378 | |||
| 379 | =head2 translate | ||
| 380 | |||
| 381 | Title : translate | ||
| 382 | Usage : $obj->translate('YTR') | ||
| 383 | Function: Returns a string of one letter amino acid codes from | ||
| 384 | nucleotide sequence input. The imput can be of any length. | ||
| 385 | |||
| 386 | Returns 'X' for unknown codons and codons that code for | ||
| 387 | more than one amino acid. Returns an empty string if input | ||
| 388 | is not three characters long. Exceptions for these are: | ||
| 389 | |||
| 390 | - IUPAC amino acid code B for Aspartic Acid and | ||
| 391 | Asparagine, is used. | ||
| 392 | - IUPAC amino acid code Z for Glutamic Acid, Glutamine is | ||
| 393 | used. | ||
| 394 | - if the codon is two nucleotides long and if by adding | ||
| 395 | an a third character 'N', it codes for a single amino | ||
| 396 | acid (with exceptions above), return that, otherwise | ||
| 397 | return empty string. | ||
| 398 | |||
| 399 | Returns empty string for other input strings that are not | ||
| 400 | three characters long. | ||
| 401 | |||
| 402 | Example : | ||
| 403 | Returns : a string of one letter ambiguous IUPAC amino acid codes | ||
| 404 | Args : ambiguous IUPAC nucleotide string | ||
| 405 | |||
| 406 | |||
| 407 | =cut | ||
| 408 | |||
| 409 | sub translate { | ||
| 410 | my ($self, $seq, $complete_codon) = @_; | ||
| 411 | $self->throw("Calling translate without a seq argument!") unless defined $seq; | ||
| 412 | return '' unless $seq; | ||
| 413 | |||
| 414 | my $id = $self->id; | ||
| 415 | my ($partial) = 0; | ||
| 416 | $partial = 2 if length($seq) % CODONSIZE == 2; | ||
| 417 | |||
| 418 | $seq = lc $seq; | ||
| 419 | $seq =~ tr/u/t/; | ||
| 420 | my $protein = ""; | ||
| 421 | if ($seq =~ /[^actg]/ ) { #ambiguous chars | ||
| 422 | for (my $i = 0; $i < (length($seq) - (CODONSIZE-1)); $i+= CODONSIZE) { | ||
| 423 | my $triplet = substr($seq, $i, CODONSIZE); | ||
| 424 | if( $triplet eq $CODONGAP ) { | ||
| 425 | $protein .= $GAP; | ||
| 426 | } elsif (exists $CODONS->{$triplet}) { | ||
| 427 | $protein .= substr($TABLES[$id-1], | ||
| 428 | $CODONS->{$triplet},1); | ||
| 429 | } else { | ||
| 430 | $protein .= $self->_translate_ambiguous_codon($triplet); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | } else { # simple, strict translation | ||
| 434 | for (my $i = 0; $i < (length($seq) - (CODONSIZE -1)); $i+=CODONSIZE) { | ||
| 435 | my $triplet = substr($seq, $i, CODONSIZE); | ||
| 436 | if( $triplet eq $CODONGAP ) { | ||
| 437 | $protein .= $GAP; | ||
| 438 | } if (exists $CODONS->{$triplet}) { | ||
| 439 | $protein .= substr($TABLES[$id-1], $CODONS->{$triplet}, 1); | ||
| 440 | } else { | ||
| 441 | $protein .= 'X'; | ||
| 442 | } | ||
| 443 | } | ||
| 444 | } | ||
| 445 | if ($partial == 2 && $complete_codon) { # 2 overhanging nucleotides | ||
| 446 | my $triplet = substr($seq, ($partial -4)). "n"; | ||
| 447 | if( $triplet eq $CODONGAP ) { | ||
| 448 | $protein .= $GAP; | ||
| 449 | } elsif (exists $CODONS->{$triplet}) { | ||
| 450 | my $aa = substr($TABLES[$id-1], $CODONS->{$triplet},1); | ||
| 451 | $protein .= $aa; | ||
| 452 | } else { | ||
| 453 | $protein .= $self->_translate_ambiguous_codon($triplet, $partial); | ||
| 454 | } | ||
| 455 | } | ||
| 456 | return $protein; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub _translate_ambiguous_codon { | ||
| 460 | my ($self, $triplet, $partial) = @_; | ||
| 461 | $partial ||= 0; | ||
| 462 | my $id = $self->id; | ||
| 463 | my $aa; | ||
| 464 | my @codons = $self->unambiguous_codons($triplet); | ||
| 465 | my %aas =(); | ||
| 466 | foreach my $codon (@codons) { | ||
| 467 | $aas{substr($TABLES[$id-1],$CODONS->{$codon},1)} = 1; | ||
| 468 | } | ||
| 469 | my $count = scalar keys %aas; | ||
| 470 | if ( $count == 1 ) { | ||
| 471 | $aa = (keys %aas)[0]; | ||
| 472 | } | ||
| 473 | elsif ( $count == 2 ) { | ||
| 474 | if ($aas{'D'} and $aas{'N'}) { | ||
| 475 | $aa = 'B'; | ||
| 476 | } | ||
| 477 | elsif ($aas{'E'} and $aas{'Q'}) { | ||
| 478 | $aa = 'Z'; | ||
| 479 | } else { | ||
| 480 | $partial ? ($aa = '') : ($aa = 'X'); | ||
| 481 | } | ||
| 482 | } else { | ||
| 483 | $partial ? ($aa = '') : ($aa = 'X'); | ||
| 484 | } | ||
| 485 | return $aa; | ||
| 486 | } | ||
| 487 | |||
| 488 | =head2 translate_strict | ||
| 489 | |||
| 490 | Title : translate_strict | ||
| 491 | Usage : $obj->translate_strict('ACT') | ||
| 492 | Function: returns one letter amino acid code for a codon input | ||
| 493 | |||
| 494 | Fast and simple translation. User is responsible to resolve | ||
| 495 | ambiguous nucleotide codes before calling this | ||
| 496 | method. Returns 'X' for unknown codons and an empty string | ||
| 497 | for input strings that are not three characters long. | ||
| 498 | |||
| 499 | It is not recommended to use this method in a production | ||
| 500 | environment. Use method translate, instead. | ||
| 501 | |||
| 502 | Example : | ||
| 503 | Returns : A string | ||
| 504 | Args : a codon = a three nucleotide character string | ||
| 505 | |||
| 506 | |||
| 507 | =cut | ||
| 508 | |||
| 509 | sub translate_strict{ | ||
| 510 | my ($self, $value) = @_; | ||
| 511 | my $id = $self->{'id'}; | ||
| 512 | |||
| 513 | $value = lc $value; | ||
| 514 | $value =~ tr/u/t/; | ||
| 515 | |||
| 516 | return '' unless length $value == 3; | ||
| 517 | |||
| 518 | return 'X' unless defined $CODONS->{$value}; | ||
| 519 | |||
| 520 | return substr( $TABLES[$id-1], $CODONS->{$value}, 1 ); | ||
| 521 | } | ||
| 522 | |||
| 523 | =head2 revtranslate | ||
| 524 | |||
| 525 | Title : revtranslate | ||
| 526 | Usage : $obj->revtranslate('G') | ||
| 527 | Function: returns codons for an amino acid | ||
| 528 | |||
| 529 | Returns an empty string for unknown amino acid | ||
| 530 | codes. Ambiguous IUPAC codes Asx,B, (Asp,D; Asn,N) and | ||
| 531 | Glx,Z (Glu,E; Gln,Q) are resolved. Both single and three | ||
| 532 | letter amino acid codes are accepted. '*' and 'Ter' are | ||
| 533 | used for terminator. | ||
| 534 | |||
| 535 | By default, the output codons are shown in DNA. If the | ||
| 536 | output is needed in RNA (tr/t/u/), add a second argument | ||
| 537 | 'RNA'. | ||
| 538 | |||
| 539 | Example : $obj->revtranslate('Gly', 'RNA') | ||
| 540 | Returns : An array of three lower case letter strings i.e. codons | ||
| 541 | Args : amino acid, 'RNA' | ||
| 542 | |||
| 543 | =cut | ||
| 544 | |||
| 545 | sub revtranslate { | ||
| 546 | my ($self, $value, $coding) = @_; | ||
| 547 | my @codons; | ||
| 548 | |||
| 549 | if (length($value) == 3 ) { | ||
| 550 | $value = lc $value; | ||
| 551 | $value = ucfirst $value; | ||
| 552 | $value = $THREELETTERSYMBOLS{$value}; | ||
| 553 | } | ||
| 554 | if ( defined $value and $value =~ /$VALID_PROTEIN/ | ||
| 555 | and length($value) == 1 ) { | ||
| 556 | my $id = $self->{'id'}; | ||
| 557 | |||
| 558 | $value = uc $value; | ||
| 559 | my @aas = @{$IUPAC_AA{$value}}; | ||
| 560 | foreach my $aa (@aas) { | ||
| 561 | #print $aa, " -2\n"; | ||
| 562 | $aa = '\*' if $aa eq '*'; | ||
| 563 | while ($TABLES[$id-1] =~ m/$aa/g) { | ||
| 564 | my $p = pos $TABLES[$id-1]; | ||
| 565 | push (@codons, $TRCOL->{--$p}); | ||
| 566 | } | ||
| 567 | } | ||
| 568 | } | ||
| 569 | |||
| 570 | if ($coding and uc ($coding) eq 'RNA') { | ||
| 571 | for my $i (0..$#codons) { | ||
| 572 | $codons[$i] =~ tr/t/u/; | ||
| 573 | } | ||
| 574 | } | ||
| 575 | |||
| 576 | return @codons; | ||
| 577 | } | ||
| 578 | |||
| 579 | =head2 reverse_translate_all | ||
| 580 | |||
| 581 | Title : reverse_translate_all | ||
| 582 | Usage : my $iup_str = $cttable->reverse_translate_all($seq_object) | ||
| 583 | my $iup_str = $cttable->reverse_translate_all($seq_object, | ||
| 584 | $cutable, | ||
| 585 | 15); | ||
| 586 | Function: reverse translates a protein sequence into IUPAC nucleotide | ||
| 587 | sequence. An 'X' in the protein sequence is converted to 'NNN' | ||
| 588 | in the nucleotide sequence. | ||
| 589 | Returns : a string | ||
| 590 | Args : a FAST::Bio::PrimarySeqI compatible object (mandatory) | ||
| 591 | a FAST::Bio::CodonUsage::Table object and a threshold if only | ||
| 592 | codons with a relative frequency above the threshold are | ||
| 593 | to be considered. | ||
| 594 | =cut | ||
| 595 | |||
| 596 | sub reverse_translate_all { | ||
| 597 | |||
| 598 | my ($self, $obj, $cut, $threshold) = @_; | ||
| 599 | |||
| 600 | ## check args are OK | ||
| 601 | |||
| 602 | if (!$obj || !$obj->isa('FAST::Bio::PrimarySeqI')){ | ||
| 603 | $self->throw(" I need a FAST::Bio::PrimarySeqI object, not a [". | ||
| 604 | ref($obj) . "]"); | ||
| 605 | } | ||
| 606 | if($obj->alphabet ne 'protein') { | ||
| 607 | $self->throw("Cannot reverse translate, need an amino acid sequence .". | ||
| 608 | "This sequence is of type [" . $obj->alphabet ."]"); | ||
| 609 | } | ||
| 610 | my @data; | ||
| 611 | my @seq = split '', $obj->seq; | ||
| 612 | |||
| 613 | ## if we're not supplying a codon usage table... | ||
| 614 | if( !$cut && !$threshold) { | ||
| 615 | ## get lists of possible codons for each aa. | ||
| 616 | for my $aa (@seq) { | ||
| 617 | if ($aa =~ /x/i) { | ||
| 618 | push @data, (['NNN']); | ||
| 619 | }else { | ||
| 620 | my @cods = $self->revtranslate($aa); | ||
| 621 | push @data, \@cods; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | }else{ | ||
| 625 | #else we are supplying a codon usage table, we just want common codons | ||
| 626 | #check args first. | ||
| 627 | if(!$cut->isa('FAST::Bio::CodonUsage::Table')) { | ||
| 628 | $self->throw("I need a FAST::Bio::CodonUsage::Table object, not a [". | ||
| 629 | ref($cut). "]."); | ||
| 630 | } | ||
| 631 | my $cod_ref = $cut->probable_codons($threshold); | ||
| 632 | for my $aa (@seq) { | ||
| 633 | if ($aa =~ /x/i) { | ||
| 634 | push @data, (['NNN']); | ||
| 635 | next; | ||
| 636 | } | ||
| 637 | push @data, $cod_ref->{$aa}; | ||
| 638 | } | ||
| 639 | } | ||
| 640 | |||
| 641 | return $self->_make_iupac_string(\@data); | ||
| 642 | |||
| 643 | } | ||
| 644 | |||
| 645 | =head2 reverse_translate_best | ||
| 646 | |||
| 647 | Title : reverse_translate_best | ||
| 648 | Usage : my $str = $cttable->reverse_translate_best($seq_object,$cutable); | ||
| 649 | Function: Reverse translates a protein sequence into plain nucleotide | ||
| 650 | sequence (GATC), uses the most common codon for each amino acid | ||
| 651 | Returns : A string | ||
| 652 | Args : A FAST::Bio::PrimarySeqI compatible object and a FAST::Bio::CodonUsage::Table object | ||
| 653 | |||
| 654 | =cut | ||
| 655 | |||
| 656 | sub reverse_translate_best { | ||
| 657 | |||
| 658 | my ($self, $obj, $cut) = @_; | ||
| 659 | |||
| 660 | if (!$obj || !$obj->isa('FAST::Bio::PrimarySeqI')){ | ||
| 661 | $self->throw(" I need a FAST::Bio::PrimarySeqI object, not a [". | ||
| 662 | ref($obj) . "]"); | ||
| 663 | } | ||
| 664 | if ($obj->alphabet ne 'protein') { | ||
| 665 | $self->throw("Cannot reverse translate, need an amino acid sequence .". | ||
| 666 | "This sequence is of type [" . $obj->alphabet ."]"); | ||
| 667 | } | ||
| 668 | if ( !$cut | !$cut->isa('FAST::Bio::CodonUsage::Table')) { | ||
| 669 | $self->throw("I need a FAST::Bio::CodonUsage::Table object, not a [". | ||
| 670 | ref($cut). "]."); | ||
| 671 | } | ||
| 672 | |||
| 673 | my $str = ''; | ||
| 674 | my @seq = split '', $obj->seq; | ||
| 675 | |||
| 676 | my $cod_ref = $cut->most_common_codons(); | ||
| 677 | |||
| 678 | for my $aa ( @seq ) { | ||
| 679 | if ($aa =~ /x/i) { | ||
| 680 | $str .= 'NNN'; | ||
| 681 | next; | ||
| 682 | } | ||
| 683 | if ( defined $cod_ref->{$aa} ) { | ||
| 684 | $str .= $cod_ref->{$aa}; | ||
| 685 | } else { | ||
| 686 | $self->throw("Input sequence contains invalid character: $aa"); | ||
| 687 | } | ||
| 688 | } | ||
| 689 | $str; | ||
| 690 | } | ||
| 691 | |||
| 692 | =head2 is_start_codon | ||
| 693 | |||
| 694 | Title : is_start_codon | ||
| 695 | Usage : $obj->is_start_codon('ATG') | ||
| 696 | Function: returns true (1) for all codons that can be used as a | ||
| 697 | translation start, false (0) for others. | ||
| 698 | Example : $myCodonTable->is_start_codon('ATG') | ||
| 699 | Returns : boolean | ||
| 700 | Args : codon | ||
| 701 | |||
| 702 | =cut | ||
| 703 | |||
| 704 | sub is_start_codon{ | ||
| 705 | shift->_codon_is( shift, \@STARTS, 'M' ); | ||
| 706 | } | ||
| 707 | |||
| 708 | =head2 is_ter_codon | ||
| 709 | |||
| 710 | Title : is_ter_codon | ||
| 711 | Usage : $obj->is_ter_codon('GAA') | ||
| 712 | Function: returns true (1) for all codons that can be used as a | ||
| 713 | translation tarminator, false (0) for others. | ||
| 714 | Example : $myCodonTable->is_ter_codon('ATG') | ||
| 715 | Returns : boolean | ||
| 716 | Args : codon | ||
| 717 | |||
| 718 | =cut | ||
| 719 | |||
| 720 | sub is_ter_codon{ | ||
| 721 | shift->_codon_is( shift, \@TABLES, $TERMINATOR ); | ||
| 722 | } | ||
| 723 | |||
| 724 | # desc: compares the passed value with a single entry in the given | ||
| 725 | # codon table | ||
| 726 | # args: a value (typically a three-char string like 'atg'), | ||
| 727 | # a reference to the appropriate set of codon tables, | ||
| 728 | # a single-character value to check for at the position in the | ||
| 729 | # given codon table | ||
| 730 | # ret: boolean, true if the given codon table contains the $key at the | ||
| 731 | # position corresponding to $value | ||
| 732 | sub _codon_is { | ||
| 733 | my ($self, $value, $table, $key ) = @_; | ||
| 734 | |||
| 735 | return 0 unless length $value == 3; | ||
| 736 | |||
| 737 | $value = lc $value; | ||
| 738 | $value =~ tr/u/t/; | ||
| 739 | |||
| 740 | my $id = $self->{'id'}; | ||
| 741 | for my $c ( $self->unambiguous_codons($value) ) { | ||
| 742 | my $m = substr( $table->[$id-1], $CODONS->{$c}, 1 ); | ||
| 743 | return 0 unless $m eq $key; | ||
| 744 | } | ||
| 745 | return 1; | ||
| 746 | } | ||
| 747 | |||
| 748 | =head2 is_unknown_codon | ||
| 749 | |||
| 750 | Title : is_unknown_codon | ||
| 751 | Usage : $obj->is_unknown_codon('GAJ') | ||
| 752 | Function: returns false (0) for all codons that are valid, | ||
| 753 | true (1) for others. | ||
| 754 | Example : $myCodonTable->is_unknown_codon('NTG') | ||
| 755 | Returns : boolean | ||
| 756 | Args : codon | ||
| 757 | |||
| 758 | |||
| 759 | =cut | ||
| 760 | |||
| 761 | sub is_unknown_codon{ | ||
| 762 | my ($self, $value) = @_; | ||
| 763 | $value = lc $value; | ||
| 764 | $value =~ tr/u/t/; | ||
| 765 | return 1 unless $self->unambiguous_codons($value); | ||
| 766 | return 0; | ||
| 767 | } | ||
| 768 | |||
| 769 | =head2 unambiguous_codons | ||
| 770 | |||
| 771 | Title : unambiguous_codons | ||
| 772 | Usage : @codons = $self->unambiguous_codons('ACN') | ||
| 773 | Returns : array of strings (one-letter unambiguous amino acid codes) | ||
| 774 | Args : a codon = a three IUPAC nucleotide character string | ||
| 775 | |||
| 776 | =cut | ||
| 777 | |||
| 778 | sub unambiguous_codons{ | ||
| 779 | my ($self,$value) = @_; | ||
| 780 | my @nts = map { $IUPAC_DNA{uc $_} } split(//, $value); | ||
| 781 | |||
| 782 | my @codons; | ||
| 783 | for my $i ( @{$nts[0]} ) { | ||
| 784 | for my $j ( @{$nts[1]} ) { | ||
| 785 | for my $k ( @{$nts[2]} ) { | ||
| 786 | push @codons, lc "$i$j$k"; | ||
| 787 | }}} | ||
| 788 | return @codons; | ||
| 789 | } | ||
| 790 | |||
| 791 | =head2 _unambiquous_codons | ||
| 792 | |||
| 793 | deprecated, now an alias for unambiguous_codons | ||
| 794 | |||
| 795 | =cut | ||
| 796 | |||
| 797 | sub _unambiquous_codons { | ||
| 798 | unambiguous_codons( undef, @_ ); | ||
| 799 | } | ||
| 800 | |||
| 801 | =head2 add_table | ||
| 802 | |||
| 803 | Title : add_table | ||
| 804 | Usage : $newid = $ct->add_table($name, $table, $starts) | ||
| 805 | Function: Add a custom Codon Table into the object. | ||
| 806 | Know what you are doing, only the length of | ||
| 807 | the argument strings is checked! | ||
| 808 | Returns : the id of the new codon table | ||
| 809 | Args : name, a string, optional (can be empty) | ||
| 810 | table, a string of 64 characters | ||
| 811 | startcodons, a string of 64 characters, defaults to standard | ||
| 812 | |||
| 813 | =cut | ||
| 814 | |||
| 815 | sub add_table { | ||
| 816 | my ($self, $name, $table, $starts) = @_; | ||
| 817 | |||
| 818 | $name ||= 'Custom'. scalar @NAMES + 1; | ||
| 819 | $starts ||= $STARTS[0]; | ||
| 820 | $self->throw('Suspect input!') | ||
| 821 | unless length($table) == 64 and length($starts) == 64; | ||
| 822 | |||
| 823 | push @NAMES, $name; | ||
| 824 | push @TABLES, $table; | ||
| 825 | push @STARTS, $starts; | ||
| 826 | |||
| 827 | return scalar @NAMES; | ||
| 828 | |||
| 829 | } | ||
| 830 | |||
| 831 | sub _make_iupac_string { | ||
| 832 | |||
| 833 | my ($self, $cod_ref) = @_; | ||
| 834 | if(ref($cod_ref) ne 'ARRAY') { | ||
| 835 | $self->throw(" I need a reference to a list of references to codons, ". | ||
| 836 | " not a [". ref($cod_ref) . "]."); | ||
| 837 | } | ||
| 838 | my %iupac_hash = FAST::Bio::Tools::IUPAC->iupac_rev_iub(); | ||
| 839 | my $iupac_string = ''; ## the string to be returned | ||
| 840 | for my $aa (@$cod_ref) { | ||
| 841 | |||
| 842 | ## scan through codon positions, record the differing values, | ||
| 843 | # then look up in the iub hash | ||
| 844 | for my $index(0..2) { | ||
| 845 | my %h; | ||
| 846 | map { my $k = substr($_,$index,1); | ||
| 847 | $h{$k} = undef;} @$aa; | ||
| 848 | my $lookup_key = join '', sort{$a cmp $b}keys %h; | ||
| 849 | |||
| 850 | ## extend string | ||
| 851 | $iupac_string .= $iupac_hash{uc$lookup_key}; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | return $iupac_string; | ||
| 855 | |||
| 856 | } | ||
| 857 | |||
| 858 | |||
| 859 | 1; |