forked from kyclark/biofx_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution3_list_comp_slice.py
executable file
·95 lines (67 loc) · 2.79 KB
/
solution3_list_comp_slice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
""" Translate RNA to proteins """
import argparse
from typing import NamedTuple, List
class Args(NamedTuple):
""" Command-line arguments """
rna: str
# --------------------------------------------------
def get_args() -> Args:
""" Get command-line arguments """
parser = argparse.ArgumentParser(
description='Translate RNA to proteins',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('rna', type=str, metavar='RNA', help='RNA sequence')
args = parser.parse_args()
return Args(args.rna)
# --------------------------------------------------
def main() -> None:
""" Make a jazz noise here """
args = get_args()
print(translate(args.rna.upper()))
# --------------------------------------------------
def translate(rna: str) -> str:
""" Translate codon sequence """
codon_to_aa = {
'AAA': 'K', 'AAC': 'N', 'AAG': 'K', 'AAU': 'N', 'ACA': 'T',
'ACC': 'T', 'ACG': 'T', 'ACU': 'T', 'AGA': 'R', 'AGC': 'S',
'AGG': 'R', 'AGU': 'S', 'AUA': 'I', 'AUC': 'I', 'AUG': 'M',
'AUU': 'I', 'CAA': 'Q', 'CAC': 'H', 'CAG': 'Q', 'CAU': 'H',
'CCA': 'P', 'CCC': 'P', 'CCG': 'P', 'CCU': 'P', 'CGA': 'R',
'CGC': 'R', 'CGG': 'R', 'CGU': 'R', 'CUA': 'L', 'CUC': 'L',
'CUG': 'L', 'CUU': 'L', 'GAA': 'E', 'GAC': 'D', 'GAG': 'E',
'GAU': 'D', 'GCA': 'A', 'GCC': 'A', 'GCG': 'A', 'GCU': 'A',
'GGA': 'G', 'GGC': 'G', 'GGG': 'G', 'GGU': 'G', 'GUA': 'V',
'GUC': 'V', 'GUG': 'V', 'GUU': 'V', 'UAC': 'Y', 'UAU': 'Y',
'UCA': 'S', 'UCC': 'S', 'UCG': 'S', 'UCU': 'S', 'UGC': 'C',
'UGG': 'W', 'UGU': 'C', 'UUA': 'L', 'UUC': 'F', 'UUG': 'L',
'UUU': 'F', 'UAA': '*', 'UAG': '*', 'UGA': '*',
}
aa = [codon_to_aa.get(codon, '-') for codon in codons(rna)]
if '*' in aa:
aa = aa[:aa.index('*')]
return ''.join(aa)
# --------------------------------------------------
def test_translate() -> None:
""" Test translate """
assert translate('') == ''
assert translate('AUG') == 'M'
assert translate('AUGCCGUAAUCU') == 'MP'
assert translate('AUGGCCAUGGCGCCCAGAACUGAGAU'
'CAAUAGUACCCGUAUUAACGGGUGA') == 'MAMAPRTEINSTRING'
# --------------------------------------------------
def codons(seq: str) -> List[str]:
""" Extract codons from a sequence """
k = 3
return [seq[i:i + k] for i in range(0, len(seq), k)]
# --------------------------------------------------
def test_codons() -> None:
""" Test codons """
assert codons('') == []
assert codons('A') == ['A']
assert codons('ABC') == ['ABC']
assert codons('ABCDE') == ['ABC', 'DE']
assert codons('ABCDEF') == ['ABC', 'DEF']
# --------------------------------------------------
if __name__ == '__main__':
main()