-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVirBot.py
executable file
·381 lines (327 loc) · 14.1 KB
/
VirBot.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /usr/bin/env python3
import argparse
import subprocess
from collections import Counter
import pandas as pd
import os
VirBot_path = str(os.path.dirname(os.path.abspath(__file__)))
global positive_cluster
positive_cluster = []
# argument parser
def virbot_cmd():
parser = argparse.ArgumentParser(description="ARGUMENTS")
parser.add_argument('--input', type=str, help="The input contig file.")
parser.add_argument('--output', default="VB_result", type=str, help="The output directory.")
parser.add_argument('--sen', action='store_true', help="Run the sensitive mode of VirBot.")
parser.add_argument('--taxa', default="TOP", help="The mode of VirBot's taxanomic module (TOP(default)/LCA)")
parser.add_argument('--threads', default="8", help="The threads number run for HMMER and DIAMOND")
args = parser.parse_args()
return args
def read_thresholding():
filename = VirBot_path + "/ref/VirBot_hmm_threshold.txt"
threshold = {}
with open(filename, 'r') as f:
for line in f:
t = line.strip().split()
threshold[int(t[0])] = float(t[1])
return threshold
def read_hmmtaxa():
filename = VirBot_path + "/ref/VirBot_hmm_taxa_full.txt"
hmm_taxa = {}
with open(filename, 'r') as f:
for line in f:
t = line.strip().split('\t')
hmm_taxa[int(t[0])] = t[1]
return hmm_taxa
def read_rv_acc():
filename = VirBot_path + "/ref/VirBot_RNAvirus_acc.txt"
db_rc_acc = set()
with open(filename, 'r') as f:
for line in f:
db_rc_acc.add(line.strip())
return db_rc_acc
def longest_substring(sa,sb):
for i in range(min(len(sa),len(sb))):
if sa[i]!=sb[i]:
return sa[0:i]
return sa[0:i]
class contig:
def __init__(self,fullname):
self.fullname = fullname
self.name = fullname.strip('>').split()[0]
self.seq = None
self.proteins = {}
self.rnaviralness = 0
self.taxa = None
def add_protein(self,prot_name,prot):
self.proteins[prot_name] = prot
def calculate_rnaviralness(self):
# t, tmp_top_score = 0, 0
t = 0
for key, value in self.proteins.items():
if value.rnavaralness:
t += 1
# if value.potential_taxa and tmp_top_score < value.score:
# self.taxa = value.potential_taxa
# tmp_top_score = value.score
if len(self.proteins):
self.rnaviralness = t / len(self.proteins)
def taxa_assignment(self, taxa_mode = "TOP"):
if taxa_mode == "TOP":
tmp_top_score = 0
for key, value in self.proteins.items():
if value.rnavaralness:
if value.potential_taxa and tmp_top_score < value.score:
self.taxa = value.potential_taxa
tmp_top_score = value.score
elif taxa_mode == "LCA":
for key, value in self.proteins.items():
if value.rnavaralness and value.potential_taxa:
if self.taxa == None:
self.taxa = value.potential_taxa
else:
self.taxa = longest_substring(self.taxa,value.potential_taxa)
#####################################################################################
class protein:
db_threshold = read_thresholding()
db_hmmtaxa = read_hmmtaxa()
db_rv_acc = read_rv_acc()
def __init__(self,fullname):
self.fullname = fullname
self.contig_name = self._set_contig_name(fullname)
self.seq=''
self.potential_taxa = None
self.best_hit = 0
self.e_value = 1
self.score = 0
self.rnavaralness = 0
self.diamond = None
def _set_contig_name(self, fullname):
t = fullname.strip('>').split()[0]
t = t.rsplit('_',1)[0]
return t
def _set_protein_name(self,fullname):
t=fullname.split('#')[0][1:-1]
return t
def filter2neg(self):
# only keep the best hit; If it is not pos, then block other hit
# self.best_hit = 0
self.e_value = 1
self.rnavaralness = 0
self.score = 0
def parse_match_search(self, result):
t = result.split()
hit_clustet_index, e_value, score \
= int(t[2].strip("cluster_")), float(t[4]), float(t[5])
if score > self.score:
self.score, self.e_value, self.best_hit \
= score, e_value, hit_clustet_index
def rnavaralness_for_search(self):
if self.best_hit and self.score >= protein.db_threshold[self.best_hit] and self.e_value< 1e-3:
self.rnavaralness = 1
positive_cluster.append(self.best_hit)
self.potential_taxa = protein.db_hmmtaxa[self.best_hit]
print(self.fullname.split('#')[0][1:-1],
'\tcluster_%d_(%.1f)' % (self.best_hit, protein.db_threshold[self.best_hit]),
'\t', self.score, '\t', self.e_value,
'\t', self.potential_taxa)
def parse_match_diamond_blastx(self, result):
t = result.split()
hit_acc = t[1]
if hit_acc in protein.db_rv_acc:
self.rnavaralness, self.diamond_acc = \
1, hit_acc
print(self.fullname.split('#')[0][1:-1], '\t', hit_acc,
'\t', t[-1], '\t', t[-2])
#####################################################################################
def predict(output_dir, temp_dir,
file_input, file_output,
sen=False, taxa_mode = "TOP"):
"""
:param file_input: input contigs
:param file_output: positive contigs predicted by our strategy
:return: None
"""
def parse_protein(filename):
'''
:param filename: predicted protein file
:return: a dict recording all the proteins
'''
proteins={}
with open(filename,'r') as f:
for line in f:
if line.startswith('>'):
prot_name = line.strip('>').split()[0]
proteins[prot_name]=protein(line)
return proteins
def parse_hmmsearch(filename,proteins):
'''
:param filename: hmmsearch result file
:param proteins: the tmp protein dict
:return: the tmp protein dict with hmmsearch result
'''
with open(filename,'r') as f:
for line in f:
if line.startswith('#'):
continue
else:
t = line.strip().split()
prot_name = t[0]
if prot_name in proteins:
proteins[prot_name].parse_match_search(line)
print("Protein_acc\tHMM_name_(corresponding_threshold)\tBit_score\tE-value\tTaxa")
for prot_name, protein in proteins.items():
protein.rnavaralness_for_search()
return proteins
def parse_diamond_blastx(filename, proteins):
'''
:param filename: the DIAMOND result diamond
:param proteins: the tmp protein dict with prediction result
:return: the tmp protein dict with sensitive prediction result
'''
print("Protein_acc\tReference_acc\tBit_score\tE-value")
with open(filename,'r') as f:
for line in f:
if line.startswith('#'):
continue
else:
t = line.strip().split()
prot_name = t[0]
if prot_name in proteins:
proteins[prot_name].parse_match_diamond_blastx(line)
return proteins
def parse_contig(filename,proteins):
'''
:param filename: the input file
:param proteins: the tmp protein dict
:return: the tmp contigs dict that containing description and encoded proteins
'''
contigs={}
with open(filename,'r') as f:
for line in f:
if line.startswith('>'):
contig_name = line.strip('>').split()[0]
contigs[contig_name] = contig(line)
for prot_name, prot in proteins.items():
if prot.contig_name in contigs:
contigs[prot.contig_name].add_protein(prot_name,prot)
for c_name, c in contigs.items():
c.calculate_rnaviralness()
return contigs
def output_rnaviralness(filename, contigs, taxa_mode):
"""
:param filename: the input file
:param contigs: the tmp contigs dict that containing description and encoded proteins
:return: the tmp contigs dict that containing the accession and protein prediction result
"""
i = 0
print('Contig_acc\tRNA-viral_gene_content\tEncoded_proteins_num\tLikely_taxa')
positive_contigs = {}
# Sum the proteins into the contig, and recode the acc.
for c_name,c in contigs.items():
c.taxa_assignment(taxa_mode)
# viral gene content cutoff: 1/16
if c.rnaviralness >= 0.0625:
if c.taxa == None:
c.taxa = "Riboviria"
print(c_name,'\t',round(c.rnaviralness,2),'\t',len(c.proteins),'\t',c.taxa)
positive_contigs[c_name] = c
if c.proteins.__len__()>0:
i+=1
print("total num of contigs:",contigs.__len__())
print("num of contigs containing gene(s):",i)
print("num of contigs lacking gene(s)",contigs.__len__()-i)
print("num of positive_contigs:",positive_contigs.__len__())
# print("Counter of cluster hit",Counter(positive_cluster))
# Retrieve the DNA sequences of positive contigs.
with open(filename,'r') as f:
significant =False
seq = ''
contig_name = None
for line in f:
if line.startswith('>'):
if significant:
positive_contigs[contig_name].seq= seq
significant = False
contig_name = line.strip('>').split()[0]
if contig_name in positive_contigs:
significant = True
seq=''
elif significant:
seq += line
#do not ignore the last contig
if significant:
positive_contigs[contig_name].seq = seq
return positive_contigs
def write_positive_file(output_dir, file_output, positive_contigs):
"""
:param filename: the output filename
:param positive_contigs: all the predicted positive contigs
:return: None
"""
positive_contig_output = []
with open(output_dir + file_output,'w') as f:
for c_name,c in positive_contigs.items():
f.write(c.fullname)
f.write(c.seq)
positive_contig_output.append([c_name, round(c.rnaviralness,2), len(c.proteins), c.taxa])
df = pd.DataFrame(positive_contig_output, columns=['Contig_acc','RNA-viral_gene_content','Encoded_proteins_num','Likely_taxa'])
df.to_csv(output_dir + "pos_contig_score.csv",index=False)
#####################################################################################
# store all the predicted proteins into a ditc {prot_name: proteins_information} without the seq.
proteins = parse_protein(temp_dir + "protein.faa")
print("Num of proteins",proteins.__len__())
# parse the hmmsearch result for the proteins, ditc {prot_name: proteins_information}.
proteins = parse_hmmsearch(temp_dir + "VB_hmmer.out", proteins)
print("Parsing of protein HMM-match result finished")
# parse the diamond blasp result for the proteins, ditc {prot_name: proteins_information}.
if sen:
proteins = parse_diamond_blastx(temp_dir + "VB_diamond.out", proteins)
print("Parsing of protein DIAMOND-match result finished")
# record all the contigs from input return, ditc {contig_name: contigs_information}.
contigs = parse_contig(file_input, proteins)
# summarize the protein result into contigs, and retrieve the identified sequences.
# ditc {positive_contig_name: contigs_information}.
positive_contigs = output_rnaviralness(file_input, contigs, taxa_mode)
# write the output file
write_positive_file(output_dir, file_output, positive_contigs)
#####################################################################################
if __name__ == "__main__":
args = virbot_cmd()
if args.taxa != "TOP" and args.taxa != "LCA":
raise Exception("The taxonmic mode should be \"TOP\" or \"LCA\".")
output_dir = args.output
if os.path.exists(output_dir):
raise Exception("The output directory already exists.")
else:
os.makedirs(output_dir)
temp_dir = f"{output_dir}/tmp"
os.makedirs(temp_dir)
FNULL = open(os.devnull, 'w')
print(f"Input contig: {args.input}")
print(f"Output directory: {output_dir}")
if args.sen:
print(f"Mode: sensitive mode")
else:
print(f"Mode: default mode")
# run Prodigal
print("Predicting the encoded proteins...")
subprocess.run(f"prodigal -i {args.input} -a {temp_dir}/protein.faa -p meta", shell=True, stdout=FNULL, stderr=subprocess.STDOUT)
print("Proteins prediction finished.")
# run HMMER
print("Scanning the protein by hmmsearch...")
subprocess.run(f"hmmsearch --tblout {temp_dir}/VB_hmmer.out --noali -E 0.001 --cpu {args.threads} {VirBot_path}/ref/VirBot.hmm {temp_dir}/protein.faa", shell=True, stdout=FNULL)
print("HMMER finshed.")
# run DIAMOND (in sensitive mode)
if args.sen:
print("Scanning the protein by DIAMOND...")
subprocess.run(f"diamond blastp --db {VirBot_path}/ref/VirBot.dmnd --query {temp_dir}/protein.faa "
f"--outfmt 6 --max-target-seqs 1 --threads {args.threads} "
f"--evalue 1e-5 --out {temp_dir}/VB_diamond.out", shell=True, stdout=FNULL, stderr=subprocess.STDOUT)
print("DIAMOND finshed.")
# predict using VirBot
predict(output_dir=f"{output_dir}/",
temp_dir=f"{temp_dir}/",
file_input = args.input,
file_output = "output.vb.fasta",
sen=args.sen, taxa_mode = args.taxa)