-
Notifications
You must be signed in to change notification settings - Fork 0
/
150415_bestHmmAveCovParse.py
executable file
·224 lines (208 loc) · 8.41 KB
/
150415_bestHmmAveCovParse.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
#!/usr/bin/env python
# script to get the best annotation from HMM search with 5 datbases (KEGG, metaCyc, Swiss-Prot, Pfam and TIGR Pfam) for each gene
# and calculate average coverage on each of the functions;
# the average coverage is calculated as the weighted mean of all genes with a function;
# takes 7 inputs: - the hit files for each database (output of consolidate_hmmscan_results.pl) in this order: KEGG, metaCyc, Swiss-Prot, Pfam, TIGR Pfam;
# - the coverage file, a tab separated table with the length, average coverage and covered length per gene (output of "calculateCoverageAndGaps2.pl"; only length and average coverage are used);
# -g, the number of genes in the dataset (non-optional in this script);
# the is only 1 output, a table with reads per function
# output is written into a file with the same name as the input coverage file (- the file extension) with "besthitsAllDB" prepended and "sumCov.tsv" appended
# written by Anna Heintz-Buschart (April 2015)
import os
import sys
import argparse
import math
parser = argparse.ArgumentParser(description='Select significant KOs 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('covFile', help='file with genes, reference length, average coverage, covered length; tsv')
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
covFile = args.covFile
if args.numberOfGenes:
sigVal = math.log(args.numberOfGenes,2)
annN = "ID"
outFile = "besthitsAllDB."+covFile[:-7] + "sumCov.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()
allIDs = []
gene_dict_tidy = {}
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])
koIDs = ";".join(priIDs)
hN = len(priIDs)
gene_dict_tidy[gene] = priIDs
print(len(allIDs))
cov_file = open(covFile, "r")
allIDVals = [allIDs,[0.0]*len(allIDs)] #0:name, 1:reads
otherIDs = [] #reads
header = 1
while 1:
linec = cov_file.readline()
if linec == "" and header==0:
break
if header == 1:
header = 0
else:
linec = linec.rstrip()
tabc = linec.split("\t") #0: name, 1:reference length 2:average coverage 3:covered length
gene = tabc[0]
cover = float(tabc[2])
if gene in gene_dict_tidy:
funLen = len(gene_dict_tidy[gene])
for ID in gene_dict_tidy[gene]:
indx = allIDVals[0].index(ID)
allIDVals[1][indx] += cover/funLen
else:
otherIDs.append(cover)
out_file = open(outFile,"w")
out_file.write("DB:ID\tsumCov\n")
for i in range(len(allIDVals[0])):
if allIDVals[1][i] > 0:
out_file.write(allIDVals[0][i]+"\t"+str(allIDVals[1][i])+"\n")
out_file.write("other\t"+str(sum(otherIDs))+"\n")