/
protein.py
executable file
·205 lines (186 loc) · 6.14 KB
/
protein.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
#!/usr/bin/env python3
import argparse
import sys
from itertools import chain, repeat, islice
def dnaToRna(sequenceToChange):
for index, base in enumerate(sequenceToChange):
if base == "a":
sequenceToChange[index] = "u"
elif base == "t":
sequenceToChange[index] = "a"
elif base == "g":
sequenceToChange[index] = "c"
elif base == "c":
sequenceToChange[index] = "g"
elif base == "-" or base == ",":
sequenceToChange[index] = base
else:
print("Error: Sequence is incorrect, it contains:", base,
file=sys.stderr)
sys.exit(1)
return sequenceToChange
def rnaToDna(sequenceToChange):
for index, base in enumerate(sequenceToChange):
if base == "a":
sequenceToChange[index] = "t"
elif base == "u":
sequenceToChange[index] = "a"
elif base == "g":
sequenceToChange[index] = "c"
elif base == "c":
sequenceToChange[index] = "g"
elif base == "-" or base == ",":
sequenceToChange[index] = base
else:
print("Error: Sequence is incorrect, it contains:", base,
file=sys.stderr)
sys.exit(1)
return sequenceToChange
aaDict = {
'uuu': 'Phe',
'uuc': 'Phe',
'uua': 'Leu',
'uug': 'Leu',
'ucu': 'Ser',
'ucc': 'Ser',
'uca': 'Ser',
'ucg': 'Ser',
'uau': 'Tyr',
'uac': 'Tyr',
'uaa': 'STOP',
'uag': 'STOP',
'ugu': 'Cys',
'ugc': 'Cys',
'uga': 'STOP',
'ugg': 'Trp',
'cuu': 'Leu',
'cuc': 'Leu',
'cua': 'Leu',
'cug': 'Leu',
'ccu': 'Pro',
'ccc': 'Pro',
'cca': 'Pro',
'ccg': 'Pro',
'cau': 'Hls',
'cac': 'Hls',
'caa': 'Gln',
'cag': 'Gln',
'cgu': 'Arg',
'cgc': 'Arg',
'cga': 'Arg',
'cgg': 'Arg',
'auu': 'Ile',
'auc': 'Ile',
'aua': 'Ile',
'aug': 'Met',
'acu': 'Thr',
'acc': 'Thr',
'aca': 'Thr',
'acg': 'Thr',
'aau': 'Asn',
'aac': 'Asn',
'aaa': 'Lys',
'aag': 'Lys',
'agu': 'Ser',
'agc': 'Ser',
'aga': 'Arg',
'agg': 'Arg',
'guu': 'Val',
'guc': 'Val',
'gua': 'Val',
'gug': 'Val',
'gcu': 'Ala',
'gcc': 'Ala',
'gca': 'Ala',
'gcg': 'Ala',
'gau': 'Asp',
'gac': 'Asp',
'gaa': 'Glu',
'gag': 'Glu',
'ggu': 'Gly',
'ggc': 'Gly',
'gga': 'Gly',
'ggg': 'Gly'
}
def addPunctuationToList(punctuation, oldList):
return list(islice(chain.from_iterable(
zip(repeat(punctuation), oldList)), 1, None))
def groupList(numberPerGroup, oldList):
return list(zip(*(iter(oldList),) * numberPerGroup))
def rnaToAa(sequenceToChange, verbose, nowarnings):
if verbose:
print("Here's the sequence befande removing punctuation:",
sequenceToChange)
for x in sequenceToChange:
if x == ",":
sequenceToChange.remove(",")
elif x == "-":
sequenceToChange.remove("-")
if verbose:
print("Here's the sequence after removing punctuation:",
sequenceToChange)
if not nowarnings and (len(sequenceToChange) % 3):
print("Warning: Sequence is not a multiple of three",
file=sys.stderr)
sequenceToChange = groupList(3, sequenceToChange)
for index, base in enumerate(sequenceToChange):
sequenceToChange[index] = ''.join(base)
sequenceToChange[index] = aaDict[sequenceToChange[index]]
sequenceToChange = addPunctuationToList('-', sequenceToChange)
return sequenceToChange
def checkTypes(outputType, inputType, verbose):
# see codecomplete 2 for a better way of doing this using arrays
if verbose:
print("Here's the input type:", inputType)
print("Here's the output type:", outputType)
if outputType != "aa" and outputType != "mrna" and outputType != "dna":
print("Error: Not DNA, mRNA and AA. Please change your output type",
file=sys.stderr)
sys.exit(1)
if inputType != "aa" and inputType != "mrna" and inputType != "dna":
print("Error: Not DNA, mRNA and AA. Please change your input type",
file=sys.stderr)
sys.exit(1)
if inputType == "aa" and (outputType == "dna" or outputType == "mrna"):
# verbose
sys.exit(1)
def processSequence(outputType, inputType, sequence, verbose, nowarnings):
if inputType == outputType:
return ''.join(sequence).upper()
elif inputType == "dna" and outputType == "mrna":
sequence = dnaToRna(sequence)
return ''.join(sequence).upper()
elif inputType == "dna" and outputType == "aa":
sequence = dnaToRna(sequence)
sequence = rnaToAa(sequence, verbose, nowarnings)
return ''.join(sequence)
elif inputType == "mrna" and outputType == "dna":
sequence = rnaToDna(sequence)
return ''.join(sequence).upper()
elif inputType == "mrna" and outputType == "aa":
sequence = rnaToAa(sequence, verbose, nowarnings)
return ''.join(sequence)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input_type",
help="""this is the format the
input should be: DNA, mRNA or AA""")
parser.add_argument("output_type",
help="""this is the format the output should be
: DNA, mRNA or AA""")
parser.add_argument("sequence",
help="""this is a sequence of DNA, mRNA or AA with both - and , allowed
as separators but - is recomended""")
parser.add_argument("-n", "--nowarnings", action="store_true",
help="turns off warnings")
parser.add_argument("-v", "--verbose", action="store_true",
help="increases output verbosity")
args = parser.parse_args()
output_type = args.output_type.lower()
input_type = args.input_type.lower()
sequence = list(args.sequence.lower())
checkTypes(output_type, input_type, args.verbose)
print(processSequence(output_type, input_type, sequence, args.verbose,
args.nowarnings))
if __name__ == '__main__':
main()