-
Notifications
You must be signed in to change notification settings - Fork 2
/
orf.py
147 lines (126 loc) · 4.13 KB
/
orf.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
import string
VALID_CHARS = set([c for c in string.ascii_letters] + [c for c in string.digits])
class DNA():
def __init__(self, name='', info='', content=None):
self.name = name
self.info = info
self.content = content
def __repr__(self):
return "name: {}\ninfo: {}\ncontent: {}".format(self.name, self.info, self.content)
def add_new_DNA(dna_list, line):
assert line[0] == '>'
first_space_idx = line.find(' ')
if first_space_idx != -1:
dna_name = line[1:first_space_idx]
dna_info = line[first_space_idx:].strip()
else:
dna_name = line[1:]
dna_info = ''
dna_list.append(DNA(name=dna_name, info=dna_info, content=[]))
def add_line_to_DNA(cur_DNA, line):
for x in line:
if x in VALID_CHARS:
cur_DNA.content.append(x)
elif x == ' ':
continue
else:
raise Exception()
def parse_FASTA(file):
"""
Basic state machine for parsing.
0 = Expecting '>' or empty line.
1 = Expecting valid string for current DNA or empty line.
2 = Got at least one string for current DNA. Ready for new DNA
or continue current DNA.
"""
state = 0
dna_list = []
for line in file:
line = line.strip()
if state == 0:
if line[0] == '>':
add_new_DNA(dna_list, line)
state = 1
elif line == '':
continue
else:
raise Exception()
elif state == 1:
add_line_to_DNA(dna_list[-1], line)
state = 2
elif state == 2:
if line[0] == '>':
add_new_DNA(dna_list, line)
state = 1
else:
add_line_to_DNA(dna_list[-1], line)
else:
raise Exception()
file.seek(0)
return dna_list
DNA_CODON_MAP = {
"TTT": 'F', "TTC": 'F', "TTA": 'L', "TTG": 'L',
"TCT": 'S', "TCC": 'S', "TCA": 'S', "TCG": 'S',
"TAT": 'Y', "TAC": 'Y', "TAA": 'Stop', "TAG": 'Stop',
"TGT": 'C', "TGC": 'C', "TGA": 'Stop', "TGG": 'W',
"CTT": 'L', "CTC": 'L', "CTA": 'L', "CTG": 'L',
"CCT": 'P', "CCC": 'P', "CCA": 'P', "CCG": 'P',
"CAT": 'H', "CAC": 'H', "CAA": 'Q', "CAG": 'Q',
"CGT": 'R', "CGC": 'R', "CGA": 'R', "CGG": 'R',
"ATT": 'I', "ATC": 'I', "ATA": 'I', "ATG": 'M',
"ACT": 'T', "ACC": 'T', "ACA": 'T', "ACG": 'T',
"AAT": 'N', "AAC": 'N', "AAA": 'K', "AAG": 'K',
"AGT": 'S', "AGC": 'S', "AGA": 'R', "AGG": 'R',
"GTT": 'V', "GTC": 'V', "GTA": 'V', "GTG": 'V',
"GCT": 'A', "GCC": 'A', "GCA": 'A', "GCG": 'A',
"GAT": 'D', "GAC": 'D', "GAA": 'E', "GAG": 'E',
"GGT": 'G', "GGC": 'G', "GGA": 'G', "GGG": 'G'
}
DNA_START_CODON = "ATG"
DNA_STOP_CODONS = [key for key, val in DNA_CODON_MAP.items() if val == "Stop"]
DNA_COMPLEMENTS = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}
RNA_CODON_MAP = {
"UUU": 'F', "UUC": 'F', "UUA": 'L', "UUG": 'L',
"UCU": 'S', "UCC": 'S', "UCA": 'S', "UCG": 'S',
"UAU": 'Y', "UAC": 'Y', "UAA": 'Stop', "UAG": 'Stop',
"UGU": 'C', "UGC": 'C', "UGA": 'Stop', "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'
}
RNA_START_CODON = "AUG"
RNA_STOP_CODONS = [key for key, val in RNA_CODON_MAP.items() if val == "Stop"]
RNA_COMPLEMENTS = {'A': 'U', 'U': 'A', 'C': 'G', 'G': 'C'}
############
# GO HERE! #
############
with open("rosalind_orf.txt") as file:
dna_list = parse_FASTA(file)
dna = ''.join(dna_list[0].content)
dna_rc = ''.join(DNA_COMPLEMENTS[c] for c in reversed(dna))
def find_proteins(s):
starts = []
for i in range(len(s)-2):
if s[i:i+3] == DNA_START_CODON:
starts.append(i)
proteins = []
for start in starts:
cur_protein = []
for i in range(start, len(s)-(len(s)-start)%3, 3):
aa = DNA_CODON_MAP[s[i:i+3]]
if aa == "Stop":
proteins.append(''.join(cur_protein))
break
cur_protein.append(aa)
return proteins
for p in set(find_proteins(dna) + find_proteins(dna_rc)):
print(p)