-
Notifications
You must be signed in to change notification settings - Fork 0
/
dna2aa.py
214 lines (170 loc) · 7.88 KB
/
dna2aa.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 23 7:41:44 2015
@author: Mikalai Drabovich
"""
import unittest
import sys
class dna2aa(object):
rna_codon_table = \
{"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
"UCU":"S", "UCC":"S", "UCA":"S", "UCG":"S",
"UAU":"Y", "UAC":"Y", "UAA":".", "UAG":".",
"UGU":"C", "UGC":"C", "UGA":".", "UGG":"W",
"CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
"CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
"CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
"CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
"AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
"ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
"AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
"AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
"GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
"GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
"GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
"GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G"}
def convert(self,rna):
subseqs = []
result = []
startCodon = 'AUG'
stopCodon0 = 'UAA'
stopCodon1 = 'UAG'
stopCodon2 = 'UGA'
start_index = 0
total_chars = len(rna)
while start_index<total_chars-2:
if rna[start_index:start_index+3] == startCodon:
triplets = [rna[i:i+3] for i in range(start_index, len(rna), 3)]
k=0
while k<len(triplets):
if triplets[k]==stopCodon0 or triplets[k]==stopCodon1 or triplets[k]==stopCodon2:
break
else:
k=k+1
temp_str = ''
if k>=len(triplets):
# try new partitioning into triplets, startting from start_index + 1
start_index = start_index + 1
continue
# case when string between start and stop contains noise:
hasNoise = False
numStarts = 0
for triplet in triplets[0:k+1]:
try:
if triplet == startCodon:
numStarts = numStarts + 1
if numStarts>1:
hasNoise = True
break
temp_str = temp_str + self.rna_codon_table[triplet]
except KeyError:
hasNoise = True
break
if hasNoise:
start_index = start_index + 1
continue
# so, all the triplets has been successfully translated!
result.append(temp_str)
start_index = start_index + 3*(k+1)
subseqs.append(''.join(triplets[0:k+1]))
else:
start_index=start_index+1
return (subseqs, result) if len(result)>0 else (None, None)
def dna2rna(self, dna):
return ''.join(c if c!='T' else 'U' for c in dna)
def rna2dna(self, rna):
return ''.join(c if c!='U' else 'T' for c in rna)
class dna2aaTest(unittest.TestCase):
"""Unit tests for some typical and some edge cases """
def test_typical_example(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA"),['MTACFFSVAA.'])
def test_typical_example_doubled(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGAAUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA"),['MTACFFSVAA.','MTACFFSVAA.'])
def test_typical_example_doubled_with_noise(self):
converter = dna2aa()
self.assertEqual(converter.convert("---SOME-NOISE---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---SOME-NOISE---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---SOME-NOISE---"),['MTACFFSVAA.','MTACFFSVAA.'])
def test_typical_example_doubled_pseudo_stops(self):
converter = dna2aa()
self.assertEqual(converter.convert("---PSEUDO-STOPS:UAAUGAUAG---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---PSEUDO-STOPS:UAAUGAUAG---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---PSEUDO-STOPS:UAAUGAUAG---"),['MTACFFSVAA.','MTACFFSVAA.'])
def test_typical_example_doubled_pseudo_starts(self):
converter = dna2aa()
self.assertEqual(converter.convert("---PSEUDO-STARTS:AUGAUGAUG---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---PSEUDO-STARTS:AUGAUGAUG---AUGACGGCUUGUUUCUUUUCUGUGGCUGCGUGA---PSEUDO-STARTS:AUGAUGAUG---"),['MTACFFSVAA.','MTACFFSVAA.'])
def test_choosing_the_nearest_stop1(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGUAAUAA"),['M.'])
def test_choosing_the_nearest_stop2(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGAUGUAAUAA"),['M.'])
def test_noise(self):
converter = dna2aa()
self.assertEqual(converter.convert("A"), (None, None))
self.assertEqual(converter.convert("AA"),(None, None))
self.assertEqual(converter.convert("AAAAAA"),(None, None))
self.assertEqual(converter.convert("AAAAAAAAA"),(None, None))
def test_only_start_stop(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGUAA"),['M.'])
self.assertEqual(converter.convert("AUGUGA"),['M.'])
self.assertEqual(converter.convert("AUGUAG"),['M.'])
def test_start_noise_stopUAA(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGAUAA"),(None, None))
self.assertEqual(converter.convert("AUGAAUAA"),(None, None))
self.assertEqual(converter.convert("AUGABCUAA"),(None, None))
self.assertEqual(converter.convert("AUGUUUAUAA"),(None, None))
self.assertEqual(converter.convert("AUGXXXUAA"),(None, None))
def test_start_noise_stopUGA(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGAUGA"),(None, None))
self.assertEqual(converter.convert("AUGAAUGA"),(None, None))
self.assertEqual(converter.convert("AUGABCUGA"),(None, None))
self.assertEqual(converter.convert("AUGUUUAUGA"),(None, None))
self.assertEqual(converter.convert("AUGXXXUGA"),(None, None))
def test_start_noise_stopUAG(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGAUAG"),(None, None))
self.assertEqual(converter.convert("AUGAAUAG"),(None, None))
self.assertEqual(converter.convert("AUGABCUAG"),(None, None))
self.assertEqual(converter.convert("AUGUUUAUAG"),(None, None))
self.assertEqual(converter.convert("AUGXXXUAG"),(None, None))
# --- checking all codon table ... ---
def test_start_UUU_stop(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGUUUUAA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUGA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUAG"),['MF.'])
def test_start_UUC_stop(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGUUUUAA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUGA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUAG"),['MF.'])
'''
... here we would test all codon map - 60 + 4 entries ...
'''
def test_start_GGG_stop(self):
converter = dna2aa()
self.assertEqual(converter.convert("AUGUUUUAA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUGA"),['MF.'])
self.assertEqual(converter.convert("AUGUUUUAG"),['MF.'])
# --- end of checking codon table ... ---
if __name__ == '__main__':
#unittest.main()
if len(sys.argv)<3:
print "Usage:\n --rna dna.txt : converts DNA to RNA; \n --genes dna.txt : finds sequence of amino acids which can be produced from the input DNA sequence"
else:
with open(sys.argv[2], 'r') as f:
contents = f.read()
converter = dna2aa()
if sys.argv[1]=='--rna':
print converter.dna2rna(contents)
elif sys.argv[1]=='--genes':
result = converter.convert(converter.dna2rna(contents))
subseq = result[0]
aminoacids = result[1]
print "%d protein(s) can be produced from this sequence:\n" % (len(result[0]))
for k in range(0,len(subseq)):
print "DNA: %s" % (converter.rna2dna(subseq[k]))
print "RNA: %s" % (subseq[k])
print "Amino acid sequence: %s\n" % (aminoacids[k])