Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python_one_week_4_biologists_solutions
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hub-courses
python_one_week_4_biologists_solutions
Commits
846ffc1d
Commit
846ffc1d
authored
1 month ago
by
Bertrand NÉRON
Browse files
Options
Downloads
Patches
Plain Diff
add new implementaion of translation exercise
parent
4b29b15e
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
source/Dive_into_Functions.rst
+44
-10
44 additions, 10 deletions
source/Dive_into_Functions.rst
source/_static/code/translate.py
+58
-3
58 additions, 3 deletions
source/_static/code/translate.py
with
102 additions
and
13 deletions
source/Dive_into_Functions.rst
+
44
−
10
View file @
846ffc1d
...
...
@@ -151,7 +151,7 @@ help you by drawing diagram.
.. image :: _static/figs/spacer.png
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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
Exerci
c
e
Exerci
s
e
--------
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 diff is collapsed.
Click to expand it.
source/_static/code/translate.py
+
58
−
3
View file @
846ffc1d
# 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
:]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment