-
Notifications
You must be signed in to change notification settings - Fork 0
/
150310_MUST_hmmBestAll.py
executable file
·189 lines (174 loc) · 6.94 KB
/
150310_MUST_hmmBestAll.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
#!/usr/bin/env python
# script to get the best annotation out of the 5 HMM databases (KEGG, metaCyc, Swiss-Prot, Pfam and TIGR Pfam) for each gene, together with the score of the best hit and the number of hits
# takes 6 inputs: the hit files for each database (output of consolidate_hmmscan_results.pl) and the number of genes in the dataset
# written by Anna Heintz-Buschart (March 2015)
import os
import sys
import argparse
import math
parser = argparse.ArgumentParser(description='Select significant annotations from HMM-output.')
parser.add_argument('koFile', help='KEGG output files from consolidate_hmmscan_results.pl')
parser.add_argument('mcFile', help='metaCyc output files from consolidate_hmmscan_results.pl')
parser.add_argument('spFile', help='Swiss-Prot output files from consolidate_hmmscan_results.pl')
parser.add_argument('pfFile', help='Pfam output files from consolidate_hmmscan_results.pl')
parser.add_argument('tiFile', help='TIGR Pfam output files from consolidate_hmmscan_results.pl')
parser.add_argument('-g','--numberOfGenes', type=int,help='number of genes used as input to hmmer, score cut-off is calculated as log2 of this')
args = parser.parse_args()
koFile = args.koFile
mcFile = args.mcFile
spFile = args.spFile
pfFile = args.pfFile
tiFile = args.tiFile
annN = "ID"
if args.numberOfGenes:
sigVal = math.log(args.numberOfGenes,2)
outFile = "besthitsAllDB.tsv"
gene_dict = {}
hmm_file = open(koFile, "r")
header = 1
while 1:
linek = hmm_file.readline()
if linek == "":
break
if header == 1:
header = 0
else:
linek = linek.rstrip()
tabi = linek.split("\t")
if float(tabi[2]) >= sigVal:
tabid, tabgene, tabscore = "KEGG:"+tabi[0].split("_")[0], tabi[1], float(tabi[2])
if tabgene not in gene_dict:
gene_dict[tabgene] = [[], []]
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
else:
if tabscore >= gene_dict[tabgene][1][0]:
if tabscore > gene_dict[tabgene][1][0]:
gene_dict[tabgene][0].insert(0,tabid)
gene_dict[tabgene][1].insert(0,float(tabscore))
else:
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
hmm_file.close()
hmm_file = open(mcFile, "r")
header = 1
while 1:
linek = hmm_file.readline()
if linek == "":
break
if header == 1:
header = 0
else:
linek = linek.rstrip()
tabi = linek.split("\t")
if float(tabi[2]) >= sigVal:
tabid, tabgene, tabscore = "metaCyc:"+tabi[0].split("_")[0], tabi[1], float(tabi[2])
if tabgene not in gene_dict:
gene_dict[tabgene] = [[], []]
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
else:
if tabscore >= gene_dict[tabgene][1][0]:
if tabscore > gene_dict[tabgene][1][0]:
gene_dict[tabgene][0].insert(0,tabid)
gene_dict[tabgene][1].insert(0,float(tabscore))
else:
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
hmm_file.close()
hmm_file = open(spFile, "r")
header = 1
while 1:
linek = hmm_file.readline()
if linek == "":
break
if header == 1:
header = 0
else:
linek = linek.rstrip()
tabi = linek.split("\t")
if float(tabi[2]) >= sigVal:
tabid, tabgene, tabscore = "swissProt:"+tabi[0].split("_")[0], tabi[1], float(tabi[2])
if tabgene not in gene_dict:
gene_dict[tabgene] = [[], []]
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
else:
if tabscore >= gene_dict[tabgene][1][0]:
if tabscore > gene_dict[tabgene][1][0]:
gene_dict[tabgene][0].insert(0,tabid)
gene_dict[tabgene][1].insert(0,float(tabscore))
else:
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
hmm_file.close()
hmm_file = open(pfFile, "r")
header = 1
while 1:
linek = hmm_file.readline()
if linek == "":
break
if header == 1:
header = 0
else:
linek = linek.rstrip()
tabi = linek.split("\t")
if float(tabi[2]) >= sigVal:
tabid, tabgene, tabscore = "Pfam:"+tabi[0], tabi[1], float(tabi[2])
if tabgene not in gene_dict:
gene_dict[tabgene] = [[], []]
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
else:
if tabscore >= gene_dict[tabgene][1][0]:
if tabscore > gene_dict[tabgene][1][0]:
gene_dict[tabgene][0].insert(0,tabid)
gene_dict[tabgene][1].insert(0,float(tabscore))
else:
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
hmm_file.close()
hmm_file = open(tiFile, "r")
header = 1
while 1:
linek = hmm_file.readline()
if linek == "":
break
if header == 1:
header = 0
else:
linek = linek.rstrip()
tabi = linek.split("\t")
if float(tabi[2]) >= sigVal:
tabid, tabgene, tabscore = "TIGR:"+tabi[0].split("_")[0], tabi[1], float(tabi[2])
if tabgene not in gene_dict:
gene_dict[tabgene] = [[], []]
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
else:
if tabscore >= gene_dict[tabgene][1][0]:
if tabscore > gene_dict[tabgene][1][0]:
gene_dict[tabgene][0].insert(0,tabid)
gene_dict[tabgene][1].insert(0,float(tabscore))
else:
gene_dict[tabgene][0].append(tabid)
gene_dict[tabgene][1].append(float(tabscore))
hmm_file.close()
out_file = open(outFile, "w")
out_file.write("Gene\t" + annN + "\tmaxScore\thitNumber\n")
allIDs = []
for item in gene_dict:
gene = item
priIDs = []
hN = 0
score = gene_dict[item][1][0]
for IDind in range(len(gene_dict[item][0])):
if gene_dict[item][1][IDind] >= score and gene_dict[item][0][IDind] not in priIDs:
priIDs.append(gene_dict[item][0][IDind])
if gene_dict[item][0][IDind] not in allIDs:
allIDs.append(gene_dict[item][0][IDind])
IDs = ";".join(priIDs)
hN = len(priIDs)
out_file.write(gene + "\t" + IDs + "\t" + str(score) + "\t" + str(hN) + "\n")
out_file.close()
print(len(allIDs))