0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import argparse
|
|
4
|
|
5
|
|
6 BLACKLIST_HEADER = ['Blacklisted Gene', 'Reason', 'Risk Category']
|
|
7 VFDB_HEADER = ['Gene', 'Contig', '% Identity', '% Coverage', 'E-Value', 'Annotation', 'Comparison to Publicly Available Genomes']
|
|
8
|
|
9
|
|
10 def get_species_from_gtdb(f):
|
|
11 # get GTDB species
|
|
12 # assumes there is one genome in the GTDB-Tk output file
|
|
13 with open(f, 'r') as fh:
|
1
|
14 for i, line in enumerate(fh):
|
|
15 if i == 0:
|
|
16 # Skip header.
|
|
17 continue
|
2
|
18 try:
|
|
19 items = line.split('\t')
|
|
20 tax = items[1]
|
|
21 tax = tax.split(';')[-1]
|
|
22 # split on GTDB species tag
|
|
23 tax = tax.split('s__')[1]
|
|
24 except Exception:
|
|
25 return '(Unknown Species)'
|
1
|
26 if len(tax) == 0:
|
2
|
27 return '(Unknown Species)'
|
0
|
28 return tax
|
|
29
|
|
30
|
|
31 def get_blast_genes(f):
|
|
32 # reads genes detected via BLAST
|
|
33 # BLAST header is as follows:
|
|
34 # qseqid sseqid pident length mismatch gapopen qstart qend sstart send evalue bitscore nident qlen
|
|
35 d = {}
|
|
36 with open(f, 'r') as fh:
|
|
37 for line in fh:
|
2
|
38 try:
|
|
39 items = line.split('\t')
|
|
40 gene = items[0]
|
|
41 # contig = items[1]
|
|
42 # pid = items[2]
|
|
43 alen = items[3]
|
|
44 # e = items[-4]
|
|
45 qlen = items[-1]
|
|
46 # calculate query coverage by dividing alignment length by query length
|
|
47 qcov = round(float(alen) / float(qlen) * 100.0, 2)
|
|
48 if gene not in d.keys():
|
|
49 d[gene] = []
|
|
50 d[gene].append('%s\t%s' % (line, str(qcov)))
|
|
51 except Exception:
|
|
52 return d
|
0
|
53 return d
|
|
54
|
|
55
|
|
56 def get_blacklist(v, b):
|
|
57 # identify high-risk isolates based on blacklisted genes
|
|
58 # blacklisted genes file contains two columns:
|
|
59 # column 0=the gene name as it appears in the gene database
|
|
60 # column 1=the reason why the gene was blacklisted, which will be reported
|
|
61 # e.g., 'ANTHRAX TOXIN'
|
|
62 bdict = {}
|
2
|
63 blacklist_present = {}
|
0
|
64 with open(b, 'r') as fh:
|
|
65 for line in fh:
|
2
|
66 try:
|
|
67 items = line.split('\t')
|
|
68 gene = items[0]
|
|
69 val = items[1]
|
|
70 bdict[gene] = val
|
|
71 except Exception:
|
|
72 return blacklist_present
|
0
|
73 for key in v.keys():
|
|
74 if key in bdict.keys():
|
|
75 val = bdict[key]
|
|
76 blacklist_present[key] = val
|
|
77 return blacklist_present
|
|
78
|
|
79
|
|
80 def gene_dist(f, blast, gtdb):
|
|
81 # get within-species prevalence of genes
|
|
82 # for virulence factors (VFs): uses VFDB VFs detected via ABRicate's VFDB db
|
|
83 # for AMR genes: uses AMR genes detected via ABRicate's ResFinder db
|
|
84 # for VFs and AMR genes: genes were detected via ABRicate XXX
|
|
85 # minimum nucleotide identity and coverage values >=80%
|
|
86 # total of 61,161 genomes queried
|
|
87 # takes VFDB or AMR gene distribution file as input (f)
|
|
88 # BLAST file of VFDB or AMR genes (blast)
|
|
89 # GTDB species (gtdb)
|
|
90 # create dictionaries based on gene distribution
|
|
91 d = {}
|
|
92 annd = {}
|
|
93 gtdbd = {}
|
2
|
94 finallines = []
|
0
|
95 with open(f, 'r') as fh:
|
|
96 for line in fh:
|
2
|
97 try:
|
|
98 items = line.split('\t')
|
|
99 tax = items[0]
|
|
100 tax = tax.split('s__')[1]
|
|
101 if len(tax) == 0:
|
|
102 tax = '(Unknown Species)'
|
|
103 gene = items[1]
|
|
104 ann = items[-1]
|
|
105 denom = items[3]
|
|
106 d['%s___%s' % (tax, gene)] = line
|
|
107 annd[gene] = ann
|
|
108 gtdbd[tax] = denom
|
|
109 except Exception:
|
|
110 return finallines
|
0
|
111 # parse BLAST results
|
|
112 for key in blast.keys():
|
|
113 blastval = blast[key]
|
|
114 for bv in blastval:
|
|
115 testkey = '%s___%s' % (gtdb, key)
|
|
116 if testkey in d.keys() and gtdb != '(Unknown Species)':
|
|
117 taxval = d[testkey]
|
|
118 items = taxval.split('\t')
|
2
|
119 tax = items[0]
|
|
120 tax = tax.split('s__')[1]
|
0
|
121 if len(tax) == 0:
|
|
122 tax = '(Unknown Species)'
|
2
|
123 gene = items[1]
|
|
124 pres = items[2]
|
|
125 denom = items[3]
|
|
126 perc = items[4]
|
0
|
127 perc = str(round(float(perc), 2))
|
2
|
128 ann = items[-1]
|
0
|
129 freetext = 'Gene {0} has been detected in {1}% of {2} genomes ({3} of {4} genomes queried)'.format(gene, perc, tax, pres, denom)
|
|
130 elif gtdb != '(Unknown Species)':
|
|
131 ann = annd[key]
|
|
132 denom = gtdbd[gtdb]
|
|
133 freetext = 'WARNING: Gene {0} ({1}) has never been detected in species {2} (n={3} genomes queried)! Interpret with caution!'.format(key, ann, gtdb, denom)
|
|
134 else:
|
|
135 ann = annd[key]
|
|
136 freetext = 'WARNING: Genome belongs to an undescribed species. Interpret with caution!'
|
|
137 finalline = '%s\t%s\t%s' % (bv, ann, freetext)
|
|
138 finallines.append(finalline)
|
|
139 return finallines
|
|
140
|
|
141
|
|
142 def output_blacklist(blacklist, blacklist_output_file):
|
|
143 # takes detected blacklisted genes as input (blacklist)
|
|
144 # blacklist results
|
|
145 with open(blacklist_output_file, 'w') as fh:
|
|
146 fh.write('%s\n' % '\t'.join(BLACKLIST_HEADER))
|
|
147 if len(blacklist.keys()) == 0:
|
|
148 # print this if no blacklisted genes are detected
|
|
149 fh.write('(No blacklisted genes detected)\tNA\tNot high risk\n')
|
|
150 else:
|
|
151 # print this if blacklisted genes are detected
|
|
152 # print a table with one row per detected blacklisted gene
|
|
153 for key in blacklist.keys():
|
|
154 val = blacklist[key]
|
|
155 fh.write('%s\t%s\tHIGH RISK\n' % (key, val))
|
|
156
|
|
157
|
|
158 def output_vfdb(vfdist, vfdb_output_file):
|
|
159 # takes distribution of virulence factors as input (vfdist)
|
|
160 # VFDB results
|
|
161 with open(vfdb_output_file, 'w') as fh:
|
|
162 fh.write('%s\n' % '\t'.join(VFDB_HEADER))
|
|
163 if len(vfdist) == 0:
|
|
164 # print this if no VFs detected
|
|
165 fh.write('%s\n' % '\t'.join(['(No VFs Detected)'] * 7))
|
|
166 else:
|
|
167 # print table of VFs if VFs detected
|
|
168 for vline in vfdist:
|
|
169 # blast_header=['Gene', 'Contig', 'Percent (%) Nucleotide Identity', 'Alignment Length', 'Mismatches', 'Gaps', 'Query Start', 'Query End', 'Subject Start', 'Subject End', 'E-Value', 'Bit Score', 'Identical Matches', 'Query Length']
|
|
170 # lc_header=['Query Coverage', 'Annotation', 'Comparison to Publicly Available Genomes']
|
|
171 items = vline.split('\t')
|
2
|
172 vgene = items[0]
|
|
173 vcontig = items[1]
|
|
174 vid = items[2]
|
|
175 vcov = items[-3]
|
|
176 veval = items[-7]
|
|
177 vann = items[-2]
|
|
178 vnotes = items[-1]
|
0
|
179 vfinal = [vgene, vcontig, vid, vcov, veval, vann, vnotes]
|
2
|
180 vfinal = '\t'.join(vfinal)
|
0
|
181 fh.write('%s\n' % vfinal)
|
|
182
|
|
183
|
|
184 def output_amr(amrdist, amr_output_file):
|
|
185 # takes distribution of AMR genes as input (amrdist)
|
|
186 # AMR results
|
|
187 with open(amr_output_file, 'w') as fh:
|
|
188 fh.write('%s\n' % '\t'.join(VFDB_HEADER))
|
|
189 if len(amrdist) == 0:
|
|
190 # print this if no AMR genes detected
|
|
191 fh.write('%s\n' % '\t'.join(['(No AMR Genes Detected)'] * 7))
|
|
192 else:
|
|
193 # print this if AMR genes detected
|
|
194 for aline in amrdist:
|
|
195 # blast_header=['Gene', 'Contig', 'Percent (%) Nucleotide Identity', 'Alignment Length', 'Mismatches', 'Gaps', 'Query Start', 'Query End', 'Subject Start', 'Subject End', 'E-Value', 'Bit Score', 'Identical Matches', 'Query Length']
|
|
196 # lc_header=['Query Coverage', 'Annotation', 'Comparison to Publicly Available Genomes']
|
|
197 items = aline.split('\t')
|
2
|
198 agene = items[0]
|
|
199 acontig = items[1]
|
|
200 aid = items[2]
|
|
201 acov = items[-3]
|
|
202 aeval = items[-7]
|
|
203 aann = items[-2]
|
|
204 anotes = items[-1]
|
0
|
205 afinal = [agene, acontig, aid, acov, aeval, aann, anotes]
|
2
|
206 afinal = '\t'.join(afinal)
|
0
|
207 fh.write('%s\n' % afinal)
|
|
208
|
|
209
|
|
210 # lrnrisk_prototype arguments
|
|
211 parser = argparse.ArgumentParser()
|
|
212
|
|
213 parser.add_argument('--gtdb_file', action='store', dest='gtdb_file', help='Path to gtdbtk tsv file')
|
|
214 parser.add_argument('--virulence_factors_file', action='store', dest='virulence_factors_file', help='Path to tsv virulence factors file')
|
|
215 parser.add_argument('--amr_determinants_file', action='store', dest='amr_determinants_file', help='Path to AMR determinants tsv file')
|
|
216 parser.add_argument('--blacklist_file', action='store', dest='blacklist_file', help='Path to blacklisted high-risk virulence factors tsv file')
|
|
217 parser.add_argument('--vf_distribution_file', action='store', dest='vf_distribution_file', help='Path to virulence factor distribution tsv file')
|
|
218 parser.add_argument('--amr_distribution_file', action='store', dest='amr_distribution_file', help='Path to AMR determinant distribution tsv file')
|
|
219 parser.add_argument('--blacklist_output_file', action='store', dest='blacklist_output_file', help='Path to blacklist output file')
|
|
220 parser.add_argument('--vfdb_output_file', action='store', dest='vfdb_output_file', help='Path to vfdb output file')
|
|
221 parser.add_argument('--amr_output_file', action='store', dest='amr_output_file', help='Path to amr output file')
|
|
222
|
|
223 # parse arguments and run pipeline
|
|
224 args = parser.parse_args()
|
|
225
|
|
226 # print_output(blacklist, vf_distribution, amr_distribution, args.output, species)
|
|
227 virulence_genes = get_blast_genes(args.virulence_factors_file)
|
|
228 species = get_species_from_gtdb(args.gtdb_file)
|
|
229
|
|
230 blacklist = get_blacklist(virulence_genes, args.blacklist_file)
|
|
231 output_blacklist(blacklist, args.blacklist_output_file)
|
|
232
|
|
233 vf_distribution = gene_dist(args.vf_distribution_file, virulence_genes, species)
|
|
234 output_vfdb(vf_distribution, args.vfdb_output_file)
|
|
235
|
|
236 amr_genes = get_blast_genes(args.amr_determinants_file)
|
|
237 amr_distribution = gene_dist(args.amr_distribution_file, amr_genes, species)
|
|
238 output_amr(amr_distribution, args.amr_output_file)
|