annotate draw_amr_matrix.py @ 4:33a0ea992043 draft

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