0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import argparse
|
|
4 import csv
|
|
5 import os
|
4
|
6 import subprocess
|
|
7 import sys
|
|
8 import tempfile
|
0
|
9
|
4
|
10 import Bio.SeqIO
|
0
|
11 import numpy
|
|
12 import pandas
|
|
13 import matplotlib.pyplot as pyplot
|
|
14
|
|
15
|
1
|
16 def get_amr_in_feature_hits(amr_feature_hits):
|
|
17 for k in amr_feature_hits.keys():
|
0
|
18 if k.lower().find('amr') >= 0:
|
1
|
19 return amr_feature_hits[k]
|
0
|
20 return None
|
|
21
|
|
22
|
4
|
23 def load_fasta(fasta_file):
|
|
24 sequence = pandas.Series(dtype=object)
|
|
25 for contig in Bio.SeqIO.parse(fasta_file, 'fasta'):
|
|
26 sequence[contig.id] = contig
|
|
27 return sequence
|
|
28
|
|
29
|
|
30 def run_command(cmd):
|
|
31 try:
|
|
32 tmp_name = tempfile.NamedTemporaryFile(dir=".").name
|
|
33 tmp_stderr = open(tmp_name, 'wb')
|
|
34 proc = subprocess.Popen(args=cmd, shell=True, stderr=tmp_stderr.fileno())
|
|
35 returncode = proc.wait()
|
|
36 tmp_stderr.close()
|
|
37 if returncode != 0:
|
|
38 # Get stderr, allowing for case where it's very large.
|
|
39 tmp_stderr = open(tmp_name, 'rb')
|
|
40 stderr = ''
|
|
41 buffsize = 1048576
|
|
42 try:
|
|
43 while True:
|
|
44 stderr += tmp_stderr.read(buffsize)
|
|
45 if not stderr or len(stderr) % buffsize != 0:
|
|
46 break
|
|
47 except OverflowError:
|
|
48 pass
|
|
49 tmp_stderr.close()
|
|
50 os.remove(tmp_name)
|
|
51 stop_err(stderr)
|
|
52 except Exception as e:
|
|
53 stop_err('Command:\n%s\n\nended with error:\n%s\n\n' % (cmd, str(e)))
|
|
54
|
|
55
|
|
56 def stop_err(msg):
|
|
57 sys.stderr.write(msg)
|
|
58 sys.exit(1)
|
|
59
|
|
60
|
9
|
61 def draw_amr_matrix(amr_feature_hits_files, amr_deletions_file, varscan_vcf_file, amr_mutation_regions_bed_file, amr_gene_drug_file, reference, reference_size, mutation_regions_dir, amr_matrix_png_dir, errors):
|
|
62 efh = open(errors, 'w')
|
0
|
63 ofh = open('process_log', 'w')
|
|
64
|
1
|
65 # Read amr_feature_hits_files.
|
|
66 amr_feature_hits = pandas.Series(dtype=object)
|
|
67 for amr_feature_hits_file in amr_feature_hits_files:
|
|
68 feature_name = os.path.basename(amr_feature_hits_file)
|
0
|
69 # Make sure the file is not empty.
|
1
|
70 if os.path.isfile(amr_feature_hits_file) and os.path.getsize(amr_feature_hits_file) > 0:
|
|
71 best_hits = pandas.read_csv(filepath_or_buffer=amr_feature_hits_file, sep='\t', header=None)
|
|
72 ofh.write("\nFeature file %s will be processed\n" % os.path.basename(amr_feature_hits_file))
|
0
|
73 else:
|
1
|
74 ofh.write("\nEmpty feature file %s will NOT be processed\n" % os.path.basename(amr_feature_hits_file))
|
0
|
75 best_hits = None
|
1
|
76 amr_feature_hits[feature_name] = best_hits
|
0
|
77
|
1
|
78 amr_hits = get_amr_in_feature_hits(amr_feature_hits)
|
0
|
79 ofh.write("\namr_hits:\n%s\n" % str(amr_hits))
|
|
80 if amr_hits is not None:
|
|
81 amr_to_draw = pandas.DataFrame(columns=['gene', 'drug'])
|
|
82 ofh.write("\namr_to_draw:\n%s\n" % str(amr_to_draw))
|
|
83 # Read amr_drug_gene_file.
|
|
84 amr_gene_drug = pandas.read_csv(amr_gene_drug_file, index_col=None, sep='\t', quoting=csv.QUOTE_NONE, header=None)
|
|
85 ofh.write("\namr_gene_drug:\n%s\n" % str(amr_gene_drug))
|
|
86
|
|
87 # Roll up AMR gene hits.
|
4
|
88 ofh.write("\namr_hits.shape[0]: %s\n" % str(amr_hits.shape[0]))
|
0
|
89 if amr_hits.shape[0] > 0:
|
|
90 for gene_idx, gene in amr_hits.iterrows():
|
4
|
91 ofh.write("gene_idx: %s\n" % str(gene_idx))
|
|
92 ofh.write("gene: %s\n" % str(gene))
|
0
|
93 gene_name = gene[3]
|
|
94 ofh.write("gene_name: %s\n" % str(gene_name))
|
|
95 ofh.write("amr_gene_drug[0]: %s\n" % str(amr_gene_drug[0]))
|
|
96 drugs = amr_gene_drug.loc[amr_gene_drug[0] == gene_name, :][1]
|
4
|
97 ofh.write("drugs: %s\n" % str(drugs))
|
0
|
98 for drug in drugs:
|
|
99 amr_to_draw = amr_to_draw.append(pandas.Series([gene_name, drug], name=amr_to_draw.shape[0], index=amr_to_draw.columns))
|
4
|
100 ofh.write("\amr_to_draw: %s\n" % str(amr_to_draw))
|
0
|
101
|
9
|
102 ofh.write("\nvarscan_vcf_file is None: %s\n" % str(varscan_vcf_file == 'None'))
|
7
|
103 if varscan_vcf_file not in [None, 'None'] and os.path.getsize(varscan_vcf_file) > 0:
|
4
|
104 amr_mutations = pandas.Series(dtype=object)
|
7
|
105 if amr_mutation_regions_bed_file is not None:
|
|
106 mutation_regions = pandas.read_csv(amr_mutation_regions_bed_file, header=0, sep='\t', index_col=False)
|
9
|
107 # Validate mutation regions.
|
4
|
108 if mutation_regions.shape[1] != 7:
|
10
|
109 efh.write("The selected mutations regions BED file is invalid, it should be a six column file.\n")
|
4
|
110 elif mutation_regions.shape[0] == 0:
|
10
|
111 efh.write("There are no rows in the selected mutation regions file.\n")
|
4
|
112 else:
|
|
113 for region_i in range(mutation_regions.shape[0]):
|
|
114 region = mutation_regions.iloc[region_i, :]
|
9
|
115 if region[0] not in reference:
|
10
|
116 efh.write("Mutation region '%s' not found in reference genome.\n" % str(region))
|
9
|
117 break
|
|
118 if not isinstance(region[1], numpy.int64):
|
10
|
119 efh.write("Non-integer found in mutation region start (column 2): %s.\n" % str(region[1]))
|
9
|
120 break
|
|
121 if not isinstance(region[2], numpy.int64):
|
10
|
122 efh.write("Non-integer found in mutation region start (column 3): %s.\n" % str(region[2]))
|
9
|
123 break
|
|
124 if region[1] <= 0 or region[2] <= 0:
|
10
|
125 efh.write("Mutation region '%s' starts before the reference sequence.\n" % str(region))
|
9
|
126 if region[1] > len(reference[region[0]].seq) or region[2] > len(reference[region[0]].seq):
|
10
|
127 efh.write("Mutation region '%s' ends after the reference sequence.\n" % str(region))
|
4
|
128 if not region.get('type', default='No Type') in ['snp', 'small-indel', 'any']:
|
9
|
129 ofh.write("\n\nSkipping mutation region '%s' with invalid type '%s', valid types are 'snp', 'small-indel', 'any'.\n\n" % (str(region), str(region.get('type', default='No Type'))))
|
4
|
130 continue
|
|
131 ofh.write("\nFinding AMR mutations for %s.\n" % str(region['name']))
|
7
|
132 region_bed = 'region_%s.bed' % region_i
|
10
|
133 ofh.write("region_bed: %s\n" % str(region_bed))
|
4
|
134 mutation_regions.loc[[region_i], ].to_csv(path_or_buf=region_bed, sep='\t', header=False, index=False)
|
10
|
135 ofh.write("mutation_regions.loc[[region_i], ]:\n%s\n" % str(mutation_regions.loc[[region_i], ]))
|
7
|
136 region_mutations_tsv = os.path.join(mutation_regions_dir, 'region_%s_mutations.tsv' % region_i)
|
10
|
137 ofh.write("region_mutations_tsv: %s\n" % str(region_mutations_tsv))
|
|
138 cmd = ' '.join(['bedtools intersect',
|
|
139 '-nonamecheck',
|
|
140 '-wb',
|
|
141 '-a', region_bed,
|
|
142 '-b', varscan_vcf_file,
|
7
|
143 ' | awk \'BEGIN{getline < "' + amr_mutation_regions_bed_file + '";printf $0"\\t";',
|
|
144 'getline < "' + varscan_vcf_file + '"; getline < "' + varscan_vcf_file + '";print $0}{print}\'',
|
|
145 '1>' + region_mutations_tsv])
|
6
|
146 ofh.write("\ncmd:\n%s\n" % cmd)
|
4
|
147 run_command(cmd)
|
10
|
148 try:
|
|
149 ofh.write("After running command, os.path.getsize((region_mutations_tsv): %s\n" % str(os.path.getsize(region_mutations_tsv)))
|
|
150 region_mutations = pandas.read_csv(region_mutations_tsv, sep='\t', header=0, index_col=False)
|
|
151 except Exception:
|
|
152 continue
|
|
153 # Figure out what kind of mutations are in this region.
|
|
154 region_mutation_types = pandas.Series(['snp'] * region_mutations.shape[0], name='TYPE', index=region_mutations.index)
|
|
155 region_mutation_types[region_mutations['REF'].str.len() != region_mutations['ALT'].str.len()] = 'small-indel'
|
|
156 region_mutation_drugs = pandas.Series(region['drug'] * region_mutations.shape[0], name='DRUG', index=region_mutations.index)
|
|
157 region_notes = pandas.Series(region['note'] * region_mutations.shape[0], name='NOTE', index=region_mutations.index)
|
|
158 region_mutations = pandas.concat([region_mutations, region_mutation_types, region_mutation_drugs, region_notes], axis=1)
|
|
159 region_mutations = region_mutations[['#CHROM', 'POS', 'TYPE', 'REF', 'ALT', 'DRUG', 'NOTE']]
|
|
160 amr_mutations[region['name']] = region_mutations
|
4
|
161 else:
|
10
|
162 ofh.write("\nMutation region BED file not received.\n")
|
0
|
163 # Roll up potentially resistance conferring mutations.
|
10
|
164 ofh.write("\n##### Rolling up potentially resistance conferring mutations..\n")
|
0
|
165 for mutation_region, mutation_hits in amr_mutations.iteritems():
|
10
|
166 ofh.write("mutation_region: %s\n" % str(mutation_region))
|
|
167 ofh.write("mutation_hits: %s\n" % str(mutation_hits))
|
0
|
168 for mutation_idx, mutation_hit in mutation_hits.iterrows():
|
10
|
169 ofh.write("mutation_idx: %s\n" % str(mutation_idx))
|
|
170 ofh.write("mutation_hit: %s\n" % str(mutation_hit))
|
0
|
171 mutation_name = mutation_region + ' ' + mutation_hit['REF'] + '->' + mutation_hit['ALT']
|
10
|
172 ofh.write("mutation_name: %s\n" % str(mutation_name))
|
0
|
173 amr_to_draw = amr_to_draw.append(pandas.Series([mutation_name, mutation_hit['DRUG']], name=amr_to_draw.shape[0], index=amr_to_draw.columns))
|
|
174
|
4
|
175 if amr_deletions_file not in [None, 'None'] and os.path.getsize(amr_deletions_file) > 0:
|
0
|
176 # Roll up deletions that might confer resistance.
|
3
|
177 try:
|
|
178 amr_deletions = pandas.read_csv(filepath_or_buffer=amr_deletions_file, sep='\t', header=None)
|
|
179 except Exception:
|
|
180 amr_deletions = pandas.DataFrame()
|
1
|
181 if amr_deletions.shape[0] > 0:
|
|
182 amr_deletions.columns = ['contig', 'start', 'stop', 'name', 'type', 'drug', 'note']
|
|
183 amr_deletions = amr_deletions.loc[amr_deletions['type'].isin(['large-deletion', 'any']), :]
|
|
184 for deletion_idx, deleted_gene in amr_deletions.iterrows():
|
|
185 amr_to_draw = amr_to_draw.append(pandas.Series(['\u0394' + deleted_gene[3], deleted_gene[5]], name=amr_to_draw.shape[0], index=amr_to_draw.columns))
|
0
|
186
|
|
187 if amr_to_draw.shape[0] > 1:
|
|
188 ofh.write("\nDrawing AMR matrix...\n")
|
|
189 present_genes = amr_to_draw['gene'].unique()
|
|
190 present_drugs = amr_to_draw['drug'].unique()
|
|
191 amr_matrix = pandas.DataFrame(0, index=present_genes, columns=present_drugs)
|
|
192 for hit_idx, hit in amr_to_draw.iterrows():
|
|
193 amr_matrix.loc[hit[0], hit[1]] = 1
|
7
|
194 amr_matrix_png = os.path.join(amr_matrix_png_dir, 'amr_matrix.png')
|
0
|
195 int_matrix = amr_matrix[amr_matrix.columns].astype(int)
|
|
196 figure, axis = pyplot.subplots()
|
6
|
197 heatmap = axis.pcolor(int_matrix, cmap=pyplot.cm.Blues, linewidth=0)
|
0
|
198 axis.invert_yaxis()
|
|
199 axis.set_yticks(numpy.arange(0.5, len(amr_matrix.index)), minor=False)
|
|
200 axis.set_yticklabels(int_matrix.index.values)
|
|
201 axis.set_xticks(numpy.arange(0.5, len(amr_matrix.columns)), minor=False)
|
|
202 axis.set_xticklabels(amr_matrix.columns.values, rotation=90)
|
|
203 axis.xaxis.tick_top()
|
|
204 axis.xaxis.set_label_position('top')
|
|
205 pyplot.tight_layout()
|
|
206 pyplot.savefig(amr_matrix_png, dpi=300)
|
|
207 else:
|
|
208 ofh.write("\nEmpty AMR matrix, nothing to draw...\n")
|
9
|
209 efh.close()
|
0
|
210 ofh.close()
|
|
211
|
|
212
|
|
213 if __name__ == '__main__':
|
|
214 parser = argparse.ArgumentParser()
|
|
215
|
1
|
216 parser.add_argument('--amr_feature_hits_dir', action='store', dest='amr_feature_hits_dir', help='Directory of tabular files containing feature hits')
|
|
217 parser.add_argument('--amr_deletions_file', action='store', dest='amr_deletions_file', default=None, help='AMR deletions BED file')
|
7
|
218 parser.add_argument('--varscan_vcf_file', action='store', dest='varscan_vcf_file', default=None, help='Varscan VCF file produced by the call_amr_mutations tool')
|
|
219 parser.add_argument('--amr_mutation_regions_bed_file', action='store', dest='amr_mutation_regions_bed_file', default=None, help='AMR mutation regions BED file')
|
0
|
220 parser.add_argument('--amr_gene_drug_file', action='store', dest='amr_gene_drug_file', help='AMR_gene_drugs tsv file')
|
4
|
221 parser.add_argument('--reference_genome', action='store', dest='reference_genome', help='Reference genome fasta file')
|
7
|
222 parser.add_argument('--mutation_regions_dir', action='store', dest='mutation_regions_dir', help='Directory for mutation regions TSV files produced by this tool')
|
|
223 parser.add_argument('--amr_matrix_png_dir', action='store', dest='amr_matrix_png_dir', help='Directory for PNG files produced by this tool')
|
9
|
224 parser.add_argument('--errors', action='store', dest='errors', help='Output file containing errors')
|
0
|
225
|
|
226 args = parser.parse_args()
|
|
227
|
4
|
228 # Get the collection of feature hits files. The collection
|
0
|
229 # will be sorted alphabetically and will contain 2 files
|
|
230 # named something like AMR_CDS_311_2022_12_20.fasta and
|
|
231 # Incompatibility_Groups_2023_01_01.fasta.
|
1
|
232 amr_feature_hits_files = []
|
|
233 for file_name in sorted(os.listdir(args.amr_feature_hits_dir)):
|
|
234 file_path = os.path.abspath(os.path.join(args.amr_feature_hits_dir, file_name))
|
|
235 amr_feature_hits_files.append(file_path)
|
0
|
236
|
4
|
237 # Load the reference genome into memory.
|
|
238 reference = load_fasta(args.reference_genome)
|
|
239 reference_size = 0
|
|
240 for i in reference:
|
|
241 reference_size += len(i.seq)
|
|
242
|
9
|
243 draw_amr_matrix(amr_feature_hits_files, args.amr_deletions_file, args.varscan_vcf_file, args.amr_mutation_regions_bed_file, args.amr_gene_drug_file, reference, reference_size, args.mutation_regions_dir, args.amr_matrix_png_dir, args.errors)
|