[Biopython] Need help in making a back table for the CodonTable object

Andrew Dalke dalke at dalkescientific.com
Mon Sep 14 08:23:27 EDT 2009


On Sep 14, 2009, at 11:05 AM, Peter wrote:
> Anyway, the specific task of back translation of one amino acid
> to a list of codons is well defined. How about:
>
> from Bio.Data.CodonTable import unambiguous_dna_by_name
> table = unambiguous_dna_by_name["Bacterial"].forward_table
> back_table = {}
> for codon, amino in table.iteritems() :
>     try :
>         back_table[amino].append(codon)
>     except KeyError :
>         back_table[amino] = [codon]
> print back_table["L"]

Quick pointer to 'defaultdict' added in Python 2.5:

import defaultdict

back_table = collections.defaultdict(list)
for codon, amino in table.iteritems() :
     back_table[amino].append(codon)
print back_table["L"]

If you really want a dictionary at the end instead of a defaultdict,

back_table = dict(back_table)


				Andrew
				dalke at dalkescientific.com




More information about the Biopython mailing list