Skip to main content
The Bio.Data.CodonTable module provides genetic code tables from the NCBI, used for translating nucleotide sequences to proteins.

Overview

Biopython includes all NCBI genetic code tables, supporting both DNA and RNA sequences, with ambiguous and unambiguous variants.
from Bio.Data import CodonTable

# Standard genetic code (Table 1)
standard_table = CodonTable.standard_dna_table
print(standard_table.forward_table['ATG'])  # 'M'
print(standard_table.stop_codons)           # ['TAA', 'TAG', 'TGA']

# Access by ID
table = CodonTable.unambiguous_dna_by_id[1]

# Access by name
table = CodonTable.unambiguous_dna_by_name['Standard']

CodonTable Class

Base class representing a codon table or genetic code.

Attributes

  • forward_table - dict[str, str] - Maps codons to amino acids
  • back_table - dict[str, str] - Maps amino acids to codons (for back-translation)
  • start_codons - list[str] - List of valid start codons
  • stop_codons - list[str] - List of stop codons
  • nucleotide_alphabet - str | None - Nucleotide alphabet (DNA/RNA)
  • protein_alphabet - str | None - Protein alphabet

Methods

__init__()

CodonTable(
    nucleotide_alphabet: str | None = None,
    protein_alphabet: str | None = None,
    forward_table: dict[str, str] = {},
    back_table: dict[str, str] = {},
    start_codons: list[str] = [],
    stop_codons: list[str] = []
) -> None
Initialize a codon table.

__str__()

Returns a formatted text representation of the codon table.
print(CodonTable.standard_dna_table)
# Table 1 Standard, SGC0
# 
#   |  T      |  C      |  A      |  G      |
# --+---------+---------+---------+---------+--
# T | TTT F   | TCT S   | TAT Y   | TGT C   | T
# T | TTC F   | TCC S   | TAC Y   | TGC C   | C
# ...

NCBICodonTable Classes

Specialized classes for NCBI genetic code tables.

NCBICodonTable

Base class for generic nucleotide sequences (accepts both DNA and RNA). Attributes:
  • id - int - NCBI genetic code ID
  • names - list[str] - List of names for this table

NCBICodonTableDNA

Codon table for unambiguous DNA sequences. Attributes:
  • nucleotide_alphabet - Set to IUPACData.unambiguous_dna_letters

NCBICodonTableRNA

Codon table for unambiguous RNA sequences. Attributes:
  • nucleotide_alphabet - Set to IUPACData.unambiguous_rna_letters

AmbiguousCodonTable

Codon table supporting ambiguous nucleotide codes.
from Bio.Data import CodonTable

table = CodonTable.ambiguous_dna_by_id[1]
print(table.forward_table['ATG'])  # 'M'
print(table.forward_table['ATR'])  # 'M' (R = A or G, so ATR = ATA or ATG)

Table Dictionaries

Tables are accessible via dictionaries by ID or name:

By ID

  • unambiguous_dna_by_id - DNA tables (unambiguous)
  • unambiguous_rna_by_id - RNA tables (unambiguous)
  • generic_by_id - Generic nucleotide tables (DNA or RNA)
  • ambiguous_dna_by_id - DNA tables with ambiguity codes
  • ambiguous_rna_by_id - RNA tables with ambiguity codes
  • ambiguous_generic_by_id - Generic tables with ambiguity codes

By Name

  • unambiguous_dna_by_name - DNA tables by name
  • unambiguous_rna_by_name - RNA tables by name
  • generic_by_name - Generic tables by name
  • ambiguous_dna_by_name - Ambiguous DNA tables by name
  • ambiguous_rna_by_name - Ambiguous RNA tables by name
  • ambiguous_generic_by_name - Ambiguous generic tables by name

Standard Tables

  • standard_dna_table - The standard genetic code (ID 1) for DNA
  • standard_rna_table - The standard genetic code (ID 1) for RNA

Common Genetic Codes

Table 1: Standard

table = CodonTable.unambiguous_dna_by_id[1]
# Names: ['Standard', 'SGC0']
# Start codons: ['TTG', 'CTG', 'ATG']
# Stop codons: ['TAA', 'TAG', 'TGA']

Table 2: Vertebrate Mitochondrial

table = CodonTable.unambiguous_dna_by_id[2]
# Start codons: ['ATT', 'ATC', 'ATA', 'ATG', 'GTG']
# Stop codons: ['TAA', 'TAG', 'AGA', 'AGG']
# TGA codes for Trp (W) instead of stop
# ATA codes for Met (M) instead of Ile

Table 11: Bacterial, Archaeal and Plant Plastid

table = CodonTable.unambiguous_dna_by_id[11]
# Start codons: ['TTG', 'CTG', 'ATT', 'ATC', 'ATA', 'ATG', 'GTG']
# Stop codons: ['TAA', 'TAG', 'TGA']

Exceptions

TranslationError

from Bio.Data.CodonTable import TranslationError
Raised when translation-specific errors occur, such as:
  • Invalid codon encountered
  • Ambiguous codon with no unique translation

Example Usage

from Bio.Data import CodonTable

# Get the standard genetic code
table = CodonTable.standard_dna_table

# Translate a codon
codon = 'ATG'
amino_acid = table.forward_table[codon]
print(f"{codon} -> {amino_acid}")  # 'ATG -> M'

# Check if codon is a start codon
if codon in table.start_codons:
    print(f"{codon} is a start codon")

# Check if codon is a stop codon
if codon in table.stop_codons:
    print(f"{codon} is a stop codon")

# Use mitochondrial code
mito_table = CodonTable.unambiguous_dna_by_id[2]
print(mito_table.forward_table['TGA'])  # 'W' (Trp in mito code)

# Work with ambiguous codons
ambig_table = CodonTable.ambiguous_dna_by_id[1]
try:
    aa = ambig_table.forward_table['NNN']  # N = any base
except CodonTable.TranslationError:
    print("No unique amino acid for this codon")

# List all available tables
for table_id in sorted(CodonTable.unambiguous_dna_by_id.keys()):
    table = CodonTable.unambiguous_dna_by_id[table_id]
    print(f"Table {table_id}: {table.names[0]}")

See Also

Build docs developers (and LLMs) love