-
Notifications
You must be signed in to change notification settings - Fork 4
/
find_longest_frame.py
executable file
·72 lines (59 loc) · 1.74 KB
/
find_longest_frame.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
import sys, getopt
import os
import operator
def find_frames(filename):
with open(filename) as f:
print (filename)
frames = []
frame = []
for line in f.readlines():
if line[0] == '>':
frames.append(''.join(frame))
frame = []
else:
frame.append(line.strip('\n'))
frames.append(''.join(frame))
frames.pop(0)
return frames
def find_longest_frame(frames):
frames_dict = dict()
for frame in frames:
start = False
count = 0
for letter in frame:
if letter == 'M' and start == False:
start = True
count = 0
elif letter == '-' and start == True:
frames_dict[frame] = count
break
elif start == True:
count += 1
sorted_frames_dict = sorted(frames_dict.items(), key = operator.itemgetter(1))
longest_frame = sorted_frames_dict[-1][0].strip('-')
#longest_frame = sorted_frames_dict.keys()[-1]
return longest_frame
def write_longest_frame(frame, inputdir, filename):
with open(os.path.join(inputdir, filename) + '.fasta', 'w') as f:
f.write(frame)
def main(argv):
inputdir = ''
try:
opts, args = getopt.getopt(argv, "hi:", ["input-dir="])
except getopt.GetoptError:
print ('Usage: python3 find_longest_frame.py --input-dir <inputdirectory>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('Usage: python3 find_longest_frame.py --input-dir <inputdirectory>')
if opt in ("-i", "--input-dir"):
inputdir = arg
print ('Input directory is ', inputdir)
for filename in os.listdir(inputdir):
if '.frame' in filename:
frames = find_frames(os.path.join(inputdir, filename))
longest_frame = find_longest_frame(frames)
gene_name = filename.split('.')[0].strip('_')
write_longest_frame(longest_frame, inputdir ,gene_name)
if __name__ == "__main__":
main(sys.argv[1:])