-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrevTransAll.py
executable file
·234 lines (209 loc) · 5.19 KB
/
revTransAll.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/python
import sys
import os
import getopt
from itertools import product
def main():
params = parseArgs()
codon_table = dict()
if params.code is None:
codon_table = get_standard_code()
else:
codon_table = read_code_file(params.code)
#print(codon_table)
nucs = dict()
current=None
curr_index=1
for aa in read_fasta(params.input):
if current is None:
current = aa[0]
elif current != aa[0]:
current = aa[0]
curr_index = 1
for trans in get_all_revtrans(aa[1], codon_table):
header=str(aa[0]) + "_translation-" + str(curr_index)
nucs[header] = trans
curr_index +=1
write_fasta(params.out, nucs)
#generator function
def get_all_revtrans(aa, code):
possibilities = list()
for pos in aa:
possibilities.append(list(code[pos.upper()]))
for nuc in product(*possibilities):
yield("".join(nuc))
def read_code_file(file):
d = dict()
if os.path.exists(file):
with open(file, 'r') as fh:
try:
num=0
ret = dict()
for line in fh:
line = line.strip()
if not line:
continue
num += 1
if num == 1:
continue
arr = line.split()
if arr[0] not in d:
d[arr[0].upper()] = list()
d[arr[0].upper()].append(arr[1].upper())
return(d)
except IOError:
print("Could not read file ",file)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%file)
return(d)
def get_standard_code():
d = {
'*' : ['TAA','TAG','TGA'],
'A' : ['GCA','GCC','GCG','GCT'],
'C' : ['TGC','TGT'],
'D' : ['GAC','GAT'],
'E' : ['GAA','GAG'],
'F' : ['TTC'],
'G' : ['GGA','GGC','GGG','GGT'],
'H' : ['CAC','CAT'],
'I' : ['ATA','ATC','ATT'],
'K' : ['AAA','AAG'],
'L' : ['CTA','CTC','CTG','CTT','TTA','TTG'],
'M' : ['ATG'],
'N' : ['AAC','AAT'],
'P' : ['CCA','CCC','CCG','CCT'],
'Q' : ['CAA','CAG'],
'R' : ['AGA','AGG','CGA','CGC','CGG','CGT'],
'S' : ['AGC','AGT','TCA','TCC','TCG','TCT'],
'T' : ['ACA','ACC','ACG','ACT'],
'V' : ['GTA','GTC','GTG','GTT'],
'W' : ['TGG'],
'Y' : ['TAC','TAT']
}
return(d)
def write_fasta(f, aln):
with open(f, 'w') as fh:
try:
for samp in aln.keys():
ol = ">" + str(samp) + "\n" + str(aln[samp]) + "\n"
fh.write(ol)
except IOError as e:
print("Could not read file %s: %s"%(f,e))
sys.exit(1)
except Exception as e:
print("Unexpected error reading file %s: %s"%(f,e))
sys.exit(1)
finally:
fh.close()
def read_fasta(fas):
if os.path.exists(fas):
with open(fas, 'r') as fh:
try:
contig = ""
seq = ""
for line in fh:
line = line.strip()
if not line:
continue
#print(line)
if line[0] == ">": #Found a header line
#If we already loaded a contig, yield that contig and
#start loading a new one
if contig:
yield([contig,seq]) #yield
contig = "" #reset contig and seq
seq = ""
split_line = line.split()
contig = (split_line[0].replace(">",""))
else:
seq += line
#Iyield last sequence, if it has both a header and sequence
if contig and seq:
yield([contig,seq])
except IOError:
print("Could not read file ",fas)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%fas)
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'hi:c:o:', \
["help", "in=", "code=", "out="])
except getopt.GetoptError as err:
print(err)
self.display_help("\nExiting because getopt returned non-zero exit status.")
#Default values for params
#Input params
self.input=None
self.code=None
self.out="out.fas"
#First pass to see if help menu was called
for o, a in options:
if o in ("-h", "-help", "--help"):
self.display_help("Exiting because help menu was called.")
#Second pass to set all args.
for opt, arg_raw in options:
arg = arg_raw.replace(" ","")
arg = arg.strip()
opt = opt.replace("-","")
#print(opt,arg)
if opt == "h" or opt == "help":
continue
elif opt=="i" or opt=="in":
self.input = arg
elif opt=="c" or opt=="code":
self.code = arg
elif opt=="out" or opt=="o":
self.out = arg
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.input:
self.display_help("No input provided.")
if not self.code:
self.display_help("No code provided. Using default.")
def display_help(self, message=None):
if message is not None:
print()
print (message)
print ("\nrevTransAll.py\n")
print("Author: Tyler Chafin")
print ("Contact: [email protected]")
print ("Description: Gives all possible reverse translations for a amino acid sequence")
print("""
-i,--in : Input file name (FASTA format)
format:
>my_sequence
MFLIMVVFPTTAASVMMVMMV...
-c,--code : Tab-delimited codon table
format:
F TTT
F TTC
F TTA
F TTG
L CTT
...
...
<NOTE: If none supplied, will use 'standard' code>
-o,--out : Output file name (default=out.fas)
format:
>my_sequence_translation-1
ATGATGAT...
>my_sequence_translation-2
ATGATCAT...
...
...
""")
print()
sys.exit()
#Call main function
if __name__ == '__main__':
main()