Skip to content

Codon Tables

A suite of codon tables to be used. We've included tables from python_codon_tables below for reference.

codon_table_to_codon_map(codon_table, deterministic=True)

This is a convenience function that takes a codon map as defined by the dictionaries above and returns a backtranslation callable that takes an AA SeqLike and returns a NT SeqLike.

By default, the SeqLike back_translate() method takes kwargs. In this simple case, we do not. The codon_table and deterministic flag are baked into the returned callable and can't be changed after definition.

:param codon_table: A nested dictionary that has keys being AAs and values codon-probabilty pairs :param deterministic: if True we return the first codon and disregard the rest. :returns: A callable that takes and returns a SeqLike

Source code in seqlike/codon_tables.py
def codon_table_to_codon_map(codon_table: dict, deterministic: bool = True) -> Callable[[SeqLike], SeqLike]:
    """This is a convenience function that takes a codon map as defined by the
    dictionaries above and returns a backtranslation callable that
    takes an AA SeqLike and returns a NT SeqLike.

    By default, the SeqLike back_translate() method takes kwargs.  In
    this simple case, we do not.  The codon_table and deterministic
    flag are baked into the returned callable and can't be changed
    after definition.

    :param codon_table: A nested dictionary that has keys being AAs
        and values codon-probabilty pairs
    :param deterministic: if True we return the first codon and disregard the rest.
    :returns: A callable that takes and returns a SeqLike
    """

    def backtranslator(seq):
        if seq._type != "AA":
            raise TypeError("Sequence must be an AA SeqLike!")
        seq_str = seq.to_str()

        nt = ""
        for aa in seq_str:
            codons, probs = zip(*codon_table[aa].items())

            # we normalize the probabilities
            # most tables are near 1.0, but issues with precision exist
            sum_prob = sum(probs)
            probs = [p / sum_prob for p in probs]

            if deterministic:
                nt += codons[0]
            else:
                nt += np.random.choice(codons, p=probs)

        new_seqlike = SeqLike(
            nt,
            id=seq.id,
            name=seq.name,
            description=seq.description,
            annotations=seq.annotations,
            dbxrefs=seq.dbxrefs,
            seq_type="dna",
            codon_map=seq.codon_map,
        )
        new_seqlike._aa_record = deepcopy(seq._aa_record)
        return new_seqlike

    return backtranslator

sort_codon_table_by_frequency(codon_table)

This is a convenience function sorts the individual codons by frequency, which is useful for obtaining a top codon codon map using the deterministic flag in codon_table_to_codon_map()

:param codon_table: A nested dictionary that has keys being AAs and values codon-probabilty pairs :returns: a new nestted dictionary

Source code in seqlike/codon_tables.py
def sort_codon_table_by_frequency(codon_table: dict) -> dict:
    """This is a convenience function sorts the individual codons by frequency,
    which is useful for obtaining a top codon codon map using the deterministic
    flag in `codon_table_to_codon_map()`

    :param codon_table: A nested dictionary that has keys being AAs
        and values codon-probabilty pairs
    :returns: a new nestted dictionary
    """
    new_codon_table = dict()
    for letter, codon_frequencies in codon_table.items():
        new_codon_table[letter] = dict(sorted(codon_frequencies.items(), key=lambda x: x[1], reverse=True))
    return new_codon_table