Translate DNA sequences

from cogent3 import get_code

standard_code = get_code(1)
standard_code.translate("TTTGCAAAC")
'FAN'

Conversion to a ProteinSequence from a DnaSequence is shown in Translate a DnaSequence to protein.

Translate all six frames

from cogent3 import make_seq, get_code

standard_code = get_code(1)
seq = make_seq("ATGCTAACATAAA", moltype="dna")
translations = standard_code.sixframes(seq)
print(translations)
['MLT*', 'C*HK', 'ANI', 'FMLA', 'LC*H', 'YVS']

Find out how many stops in a frame

from cogent3 import make_seq, get_code

standard_code = get_code(1)
seq = make_seq("ATGCTAACATAAA", moltype="dna")
stops_frame1 = standard_code.get_stop_indices(seq, start=0)
stops_frame1
stop_index = stops_frame1[0]
seq[stop_index : stop_index + 3]
0
NoneTAA

3 DnaSequence

Translate a codon

from cogent3 import make_seq, get_code

standard_code = get_code(1)
standard_code["TTT"]
'F'

or get the codons for a single amino acid

standard_code["A"]
['GCT', 'GCC', 'GCA', 'GCG']

Look up the amino acid corresponding to a single codon

from cogent3 import get_code

standard_code = get_code(1)
standard_code["TTT"]
'F'

Get all the codons for one amino acid

from cogent3 import get_code

standard_code = get_code(1)
standard_code["A"]
['GCT', 'GCC', 'GCA', 'GCG']

Get all the codons for a group of amino acids

targets = ["A", "C"]
codons = [standard_code[aa] for aa in targets]
codons
flat_list = sum(codons, [])
flat_list
['GCT', 'GCC', 'GCA', 'GCG', 'TGT', 'TGC']

Converting the CodonAlphabet to codon series

from cogent3 import make_seq

my_seq = make_seq("AGTACACTGGTT", moltype="dna")
sorted(my_seq.codon_alphabet())
len(my_seq.codon_alphabet())
61

Obtaining the codons from a DnaSequence object

Use the method get_in_motif_size

from cogent3 import make_seq

my_seq = make_seq("ATGCACTGGTAA", name="my_gene", moltype="dna")
codons = my_seq.get_in_motif_size(3)
print(codons)
['ATG', 'CAC', 'TGG', 'TAA']

Translating a DNA sequence with a terminating stop codon

You can’t translate a sequence that contains a stop codon.

pep = my_seq.get_translation()
---------------------------------------------------------------------------
AlphabetError                             Traceback (most recent call last)
<ipython-input-11-78926fb55d34> in <module>
----> 1 pep = my_seq.get_translation()

~/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.8/site-packages/cogent3/core/sequence.py in get_translation(self, gc, incomplete_ok)
   1305             orig_codon = self._seq[posn : posn + 3]
   1306             try:
-> 1307                 resolved = codon_alphabet.resolve_ambiguity(orig_codon)
   1308             except AlphabetError:
   1309                 if not incomplete_ok or "-" not in orig_codon:

~/work/cogent3.github.io/cogent3.github.io/.venv/lib/python3.8/site-packages/cogent3/core/alphabet.py in resolve_ambiguity(self, ambig_motif)
    727 
    728         if not motif_set:
--> 729             raise AlphabetError(ambig_motif)
    730 
    731         return tuple(motif_set)

AlphabetError: TAA

By removing the trailing stop codon first

from cogent3 import make_seq

my_seq = make_seq("ATGCACTGGTAA", name="my_gene", moltype="dna")
seq = my_seq.trim_stop_codon()
pep = seq.get_translation()
print(pep.to_fasta())
print(type(pep))
>my_gene
MHW

<class 'cogent3.core.sequence.ProteinSequence'>

By slicing the DnaSequence first

from cogent3 import make_seq

my_seq = make_seq("CAAATGTATTAA", name="my_gene", moltype="dna")
pep = my_seq[:-3].get_translation()
print(pep.to_fasta())
>my_gene
QMY