Mercurial > repos > greg > draw_amr_matrix
comparison draw_amr_matrix.py @ 6:caf554e039b2 draft
Uploaded
| author | greg |
|---|---|
| date | Wed, 22 Feb 2023 21:23:25 +0000 |
| parents | 33a0ea992043 |
| children | 389c98d344ce |
comparison
equal
deleted
inserted
replaced
| 5:46d31ec5d24c | 6:caf554e039b2 |
|---|---|
| 131 ofh.write("\nFinding AMR mutations for %s.\n" % str(region['name'])) | 131 ofh.write("\nFinding AMR mutations for %s.\n" % str(region['name'])) |
| 132 region_dir = os.path.join(mutations_dir, 'region_' + str(region_i)) | 132 region_dir = os.path.join(mutations_dir, 'region_' + str(region_i)) |
| 133 os.mkdir(region_dir) | 133 os.mkdir(region_dir) |
| 134 region_bed = os.path.join(region_dir, 'region.bed') | 134 region_bed = os.path.join(region_dir, 'region.bed') |
| 135 mutation_regions.loc[[region_i], ].to_csv(path_or_buf=region_bed, sep='\t', header=False, index=False) | 135 mutation_regions.loc[[region_i], ].to_csv(path_or_buf=region_bed, sep='\t', header=False, index=False) |
| 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) | 136 cmd = ' '.join(['bedtools intersect -nonamecheck -wb -a', |
| 137 region_bed, | |
| 138 '-b', | |
| 139 amr_mutations_file, | |
| 140 ' | awk \'BEGIN{getline < "' + amr_mutation_regions_file + '";printf $0"\\t";', | |
| 141 'getline < "' + amr_mutations_file + '"; getline < "' + amr_mutations_file + '";print $0}{print}\'', | |
| 142 '1>' + region_mutations_output_file]) | |
| 143 ofh.write("\ncmd:\n%s\n" % cmd) | |
| 137 run_command(cmd) | 144 run_command(cmd) |
| 138 try: | 145 try: |
| 139 region_mutations = pandas.read_csv(region_mutations_output_file, sep='\t', header=0, index_col=False) | 146 region_mutations = pandas.read_csv(region_mutations_output_file, sep='\t', header=0, index_col=False) |
| 140 except Exception: | 147 except Exception: |
| 141 region_mutations = pandas.DataFrame() | 148 region_mutations = pandas.DataFrame() |
| 156 for mutation_idx, mutation_hit in mutation_hits.iterrows(): | 163 for mutation_idx, mutation_hit in mutation_hits.iterrows(): |
| 157 mutation_name = mutation_region + ' ' + mutation_hit['REF'] + '->' + mutation_hit['ALT'] | 164 mutation_name = mutation_region + ' ' + mutation_hit['REF'] + '->' + mutation_hit['ALT'] |
| 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)) | 165 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)) |
| 159 | 166 |
| 160 if amr_deletions_file not in [None, 'None'] and os.path.getsize(amr_deletions_file) > 0: | 167 if amr_deletions_file not in [None, 'None'] and os.path.getsize(amr_deletions_file) > 0: |
| 161 # TODO: So far, no samples have produced deletions, but we do have all the pices in place | |
| 162 # within the workflow to receive the amr_deletions_file here, although it is currently | |
| 163 # always empty... | |
| 164 # Roll up deletions that might confer resistance. | 168 # Roll up deletions that might confer resistance. |
| 165 try: | 169 try: |
| 166 amr_deletions = pandas.read_csv(filepath_or_buffer=amr_deletions_file, sep='\t', header=None) | 170 amr_deletions = pandas.read_csv(filepath_or_buffer=amr_deletions_file, sep='\t', header=None) |
| 167 except Exception: | 171 except Exception: |
| 168 amr_deletions = pandas.DataFrame() | 172 amr_deletions = pandas.DataFrame() |
| 180 for hit_idx, hit in amr_to_draw.iterrows(): | 184 for hit_idx, hit in amr_to_draw.iterrows(): |
| 181 amr_matrix.loc[hit[0], hit[1]] = 1 | 185 amr_matrix.loc[hit[0], hit[1]] = 1 |
| 182 amr_matrix_png = os.path.join(output_dir, 'amr_matrix.png') | 186 amr_matrix_png = os.path.join(output_dir, 'amr_matrix.png') |
| 183 int_matrix = amr_matrix[amr_matrix.columns].astype(int) | 187 int_matrix = amr_matrix[amr_matrix.columns].astype(int) |
| 184 figure, axis = pyplot.subplots() | 188 figure, axis = pyplot.subplots() |
| 189 heatmap = axis.pcolor(int_matrix, cmap=pyplot.cm.Blues, linewidth=0) | |
| 185 axis.invert_yaxis() | 190 axis.invert_yaxis() |
| 186 axis.set_yticks(numpy.arange(0.5, len(amr_matrix.index)), minor=False) | 191 axis.set_yticks(numpy.arange(0.5, len(amr_matrix.index)), minor=False) |
| 187 axis.set_yticklabels(int_matrix.index.values) | 192 axis.set_yticklabels(int_matrix.index.values) |
| 188 axis.set_xticks(numpy.arange(0.5, len(amr_matrix.columns)), minor=False) | 193 axis.set_xticks(numpy.arange(0.5, len(amr_matrix.columns)), minor=False) |
| 189 axis.set_xticklabels(amr_matrix.columns.values, rotation=90) | 194 axis.set_xticklabels(amr_matrix.columns.values, rotation=90) |
