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