Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub-courses
python_one_week_4_biologists_solutions
Commits
ab31e37f
Verified
Commit
ab31e37f
authored
Mar 18, 2019
by
Bertrand NÉRON
Browse files
fix translate code
in translate use // intead of / due to python3 in translate2 use translate DRY
parent
e0f44684
Pipeline
#10453
passed with stages
in 22 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
source/_static/code/translate.py
View file @
ab31e37f
genetic_code
=
{
'ttt'
:
'F'
,
'tct'
:
'S'
,
'tat'
:
'Y'
,
'tgt'
:
'C'
,
genetic_code
=
{
'ttt'
:
'F'
,
'tct'
:
'S'
,
'tat'
:
'Y'
,
'tgt'
:
'C'
,
'ttc'
:
'F'
,
'tcc'
:
'S'
,
'tac'
:
'Y'
,
'tgc'
:
'C'
,
'tta'
:
'L'
,
'tca'
:
'S'
,
'taa'
:
'*'
,
'tga'
:
'*'
,
'ttg'
:
'L'
,
'tcg'
:
'S'
,
'tag'
:
'*'
,
'tgg'
:
'W'
,
...
...
@@ -23,7 +23,11 @@ def translate(nuc_seq, code):
# to avoid to compute len(seq)/3 at each loop
# I compute it once and use a reference
# it could be expensive if the sequence is very long.
cycle
=
len
(
nuc_seq
)
/
3
# another way to determine the end of looping
# stop_iteration = len(nuc_seq)
# while (start + 2) < stop_iteration:
cycle
=
len
(
nuc_seq
)
//
3
while
n
<
cycle
:
start
=
n
*
3
end
=
start
+
3
...
...
@@ -34,23 +38,18 @@ def translate(nuc_seq, code):
else
:
raise
RuntimeError
(
"unknow codon: "
+
codon
)
n
+=
1
# if use the other looping solution
# n += 3
return
prot_seq
def
translate2
(
nuc_seq
,
code
,
phase
=
1
):
prot_seq
=
''
if
0
<
phase
<
4
:
start
=
phase
-
1
nuc_seq
=
nuc_seq
[
start
:]
elif
-
4
<
phase
<
0
:
start
=
-
phase
-
1
nuc_seq
=
nuc_seq
[::
-
1
]
# an other way to determine the end of looping
stop_iteration
=
len
(
nuc_seq
)
while
(
start
+
2
)
<
stop_iteration
:
end
=
start
+
3
codon
=
nuc_seq
[
start
:
end
].
lower
()
if
codon
in
code
:
prot_seq
+=
code
[
codon
]
else
:
raise
RuntimeError
(
"unknow codon"
)
start
+=
3
nuc_seq
=
nuc_seq
[
start
:]
prot_seq
=
translate
(
nuc_seq
,
code
)
return
prot_seq
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment