-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrg-cog.py
executable file
·101 lines (79 loc) · 2.52 KB
/
mrg-cog.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
### This Code merges COG annotation files###
## Elham Karimi, Algarve uiversity, Faro, Portugal, September2017 ##
###[email protected]; [email protected];###
#fuctions
def read_line(ind, data_in): # read line with ind
for line in data_in:
cog, count, dsa, dsb, dsc = line.split('\t')
if ind in cog:
return [cog, count, dsa, dsb, dsc]
def write_header(file, no): # write header for output file
file.write('#COG')
file.write('\t')
for i in range(1, no + 1):
file.write('count' + str(i))
file.write('\t')
file.write('description')
file.write('\t')
file.write('class')
file.write('\t')
file.write('class description')
file.write('\n')
def file_len(fname): # no of line in a file
with open(fname) as f:
for i, l in enumerate(f):
pass
return int(l[3:7])
# main
import sys
no = int(sys.argv[1])
data_out = open('file_out.txt','w')
data_out.truncate()
write_header(data_out, no);
no_list = []
for i in range(1, no + 1):
no_list.append(file_len(str(i) + '.extension'))
no_max = max(no_list) + 1
for k in range(1, no_max):
#print '{0:.2f}'.format(k/5662*100)
ind = 'COG' + '{0:0=4d}'.format(k)
# write COG
flag = False
for i in range(1, no + 1):
line = read_line(ind, open(str(i) + '.extension', 'r'))
if line:
if not flag:
data_out.write(ind)
data_out.write('\t')
flag = True
else:
pass
# write counts
if flag:
for i in range(1, no + 1):
line = read_line(ind, open(str(i) + '.extension', 'r'))
#print(line)
if line:
#print(1)
data_out.write(line[1])
data_out.write('\t')
else:
#print(2)
data_out.write('0')
data_out.write('\t')
# write description
flag = False
for i in range(1, no + 1):
line = read_line(ind, open(str(i) + '.extension', 'r'))
if line:
if not flag:
data_out.write(line[2])
data_out.write('\t')
data_out.write(line[3])
data_out.write('\t')
data_out.write(line[4])
flag = True
else:
pass
data_out.close()
#########################################################