Skip to content
Snippets Groups Projects
Commit 846ffc1d authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

add new implementaion of translation exercise

parent 4b29b15e
No related branches found
No related tags found
No related merge requests found
......@@ -151,7 +151,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -181,7 +181,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -211,7 +211,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -240,7 +240,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -276,7 +276,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -307,7 +307,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -336,7 +336,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -375,7 +375,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -410,7 +410,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exercice
Exercise
--------
Without executing the code in Python interpreter, can you determine what the code below print out.
......@@ -474,14 +474,48 @@ bonus
This function have to take the phase as parameter
We will start to implement a function translating nucleic sequence only in phase 1
first implementation:
We will compute how many cycle we have to do and loop with a while
.. literalinclude:: _static/code/translate.py
:linenos:
:lines: 4-50
:language: python
second implementation:
We will use a range to compute the stating position of each codon
.. literalinclude:: _static/code/translate.py
:linenos:
:lines: 50-68
:language: python
third implementation:
We use a new function in the module itertools : `batched` that provide very nice feature for us.
But this function is available **only** from `python 3.13`
.. literalinclude:: _static/code/translate.py
:linenos:
:lines: 1-4, 69-86
:language: python
Now we can implement the bonus (a translation with specifying the phase)
.. literalinclude:: _static/code/translate.py
:linenos:
:lines: 87-
:language: python
:download:`translate.py <_static/code/translate.py>` .
.. _matrix_exercise:
Exercise
--------
......
# this import is useful only for translate_3
# and batched is available only from python3.13
from itertools import batched
genetic_code = { 'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
'ttc': 'F', 'tcc': 'S', 'tac': 'Y', 'tgc': 'C',
'tta': 'L', 'tca': 'S', 'taa': '*', 'tga': '*',
......@@ -17,7 +21,9 @@ genetic_code = { 'ttt': 'F', 'tct': 'S', 'tat': 'Y', 'tgt': 'C',
}
def translate(nuc_seq, code):
"""
Trnaslate nuc_seq in phase 1 using the genetic code *code*
"""
prot_seq = ''
n = 0
# to avoid to compute len(seq)/3 at each loop
......@@ -42,8 +48,57 @@ def translate(nuc_seq, code):
# n += 3
return prot_seq
def translate2(nuc_seq, code, phase = 1):
def translate_2(nuc_seq, code):
"""
Translate nuc_seq in phase 1 using the genetic code *code*
"""
prot_seq = ''
# we have to compute the position of the last codon
# otherwise if the seq length is not a multiple of 3
# we generate a codon with 2 or one base length
# and trigger an error when trying to translate it
for pos in range(0, len(nuc_seq) // 3 * 3, 3):
codon = nuc_seq[pos: pos + 3]
try:
prot_seq += code[codon]
except KeyError as err:
raise RuntimeError(f"Unknown codon: {codon}")
return prot_seq
def translate_3(nuc_seq, code):
"""
Translate nuc_seq in phase 1 using the genetic code *code*
"""
# This implementation use a new function from the module itertools
# batched : https://docs.python.org/3/library/itertools.html#itertools.batched
# Warning batched is available from python 3.13 only
prot_seq = ''
for codon in batched(nuc_seq, 3, strict=False):
if len(codon) < 3:
# it means that we are at the end of the sequence
# there is not enough bases to create a codon
break
else:
prot_seq += code[''.join(codon)]
return prot_seq
def translate_with_phase(nuc_seq, code, phase = 1):
"""
Translate the *nuc_seq* using the genetic code *code* in one of the 6 phases.
- phase 1: compute codons from the first base odf the sequence *nuc_seq*
- phase 2: compute codons from the second base
- phase 3: compute codons from the third base
- phase -1: compute codons from the first base but from the reversed sequence
- phase -2: compute codons from the second base but from the reversed sequence
- phase -3: compute codons from the third base but from the reversed sequence
:return: the aminoacid sequence
:rtype: str
"""
if 0 < phase < 4 :
start = phase - 1
nuc_seq = nuc_seq[start:]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment