Mercurial > repos > greg > draw_amr_matrix
comparison draw_amr_matrix.py @ 11:da1c9c1be421 draft
Uploaded
author | greg |
---|---|
date | Mon, 27 Mar 2023 19:41:30 +0000 |
parents | 03240ffe969a |
children | aa2b737102dc |
comparison
equal
deleted
inserted
replaced
10:03240ffe969a | 11:da1c9c1be421 |
---|---|
63 ofh = open('process_log', 'w') | 63 ofh = open('process_log', 'w') |
64 | 64 |
65 # Read amr_feature_hits_files. | 65 # Read amr_feature_hits_files. |
66 amr_feature_hits = pandas.Series(dtype=object) | 66 amr_feature_hits = pandas.Series(dtype=object) |
67 for amr_feature_hits_file in amr_feature_hits_files: | 67 for amr_feature_hits_file in amr_feature_hits_files: |
68 ofh.write("\namr_feature_hits_file: %s\n" % amr_feature_hits_file) | |
68 feature_name = os.path.basename(amr_feature_hits_file) | 69 feature_name = os.path.basename(amr_feature_hits_file) |
70 ofh.write("\nfeature_name: %s\n" % feature_name) | |
69 # Make sure the file is not empty. | 71 # Make sure the file is not empty. |
70 if os.path.isfile(amr_feature_hits_file) and os.path.getsize(amr_feature_hits_file) > 0: | 72 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) | 73 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)) | 74 ofh.write("\nFeature file %s will be processed\n" % os.path.basename(amr_feature_hits_file)) |
73 else: | 75 else: |
146 ofh.write("\ncmd:\n%s\n" % cmd) | 148 ofh.write("\ncmd:\n%s\n" % cmd) |
147 run_command(cmd) | 149 run_command(cmd) |
148 try: | 150 try: |
149 ofh.write("After running command, os.path.getsize((region_mutations_tsv): %s\n" % str(os.path.getsize(region_mutations_tsv))) | 151 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) | 152 region_mutations = pandas.read_csv(region_mutations_tsv, sep='\t', header=0, index_col=False) |
153 ofh.write("\nregion_mutations: %s\n" % region_mutations) | |
151 except Exception: | 154 except Exception: |
152 continue | 155 continue |
153 # Figure out what kind of mutations are in this region. | 156 # 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) | 157 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' | 158 region_mutation_types[region_mutations['REF'].str.len() != region_mutations['ALT'].str.len()] = 'small-indel' |
158 region_mutations = pandas.concat([region_mutations, region_mutation_types, region_mutation_drugs, region_notes], axis=1) | 161 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']] | 162 region_mutations = region_mutations[['#CHROM', 'POS', 'TYPE', 'REF', 'ALT', 'DRUG', 'NOTE']] |
160 amr_mutations[region['name']] = region_mutations | 163 amr_mutations[region['name']] = region_mutations |
161 else: | 164 else: |
162 ofh.write("\nMutation region BED file not received.\n") | 165 ofh.write("\nMutation region BED file not received.\n") |
166 ofh.write("\nAfter processing mutations, amr_mutations: %s\n" % str(amr_mutations)) | |
163 # Roll up potentially resistance conferring mutations. | 167 # Roll up potentially resistance conferring mutations. |
164 ofh.write("\n##### Rolling up potentially resistance conferring mutations..\n") | 168 ofh.write("\n##### Rolling up potentially resistance conferring mutations..\n") |
165 for mutation_region, mutation_hits in amr_mutations.iteritems(): | 169 for mutation_region, mutation_hits in amr_mutations.iteritems(): |
166 ofh.write("mutation_region: %s\n" % str(mutation_region)) | 170 ofh.write("mutation_region: %s\n" % str(mutation_region)) |
167 ofh.write("mutation_hits: %s\n" % str(mutation_hits)) | 171 ofh.write("mutation_hits: %s\n" % str(mutation_hits)) |
169 ofh.write("mutation_idx: %s\n" % str(mutation_idx)) | 173 ofh.write("mutation_idx: %s\n" % str(mutation_idx)) |
170 ofh.write("mutation_hit: %s\n" % str(mutation_hit)) | 174 ofh.write("mutation_hit: %s\n" % str(mutation_hit)) |
171 mutation_name = mutation_region + ' ' + mutation_hit['REF'] + '->' + mutation_hit['ALT'] | 175 mutation_name = mutation_region + ' ' + mutation_hit['REF'] + '->' + mutation_hit['ALT'] |
172 ofh.write("mutation_name: %s\n" % str(mutation_name)) | 176 ofh.write("mutation_name: %s\n" % str(mutation_name)) |
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)) | 177 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)) |
178 ofh.write("\nAfter processing mutations, amr_to_draw: %s\n" % str(amr_to_draw)) | |
179 ofh.write("\nAfter processing mutations, amr_to_draw.shape[0]: %s\n" % str(amr_to_draw.shape[0])) | |
174 | 180 |
175 if amr_deletions_file not in [None, 'None'] and os.path.getsize(amr_deletions_file) > 0: | 181 if amr_deletions_file not in [None, 'None'] and os.path.getsize(amr_deletions_file) > 0: |
176 # Roll up deletions that might confer resistance. | 182 # Roll up deletions that might confer resistance. |
177 try: | 183 try: |
178 amr_deletions = pandas.read_csv(filepath_or_buffer=amr_deletions_file, sep='\t', header=None) | 184 amr_deletions = pandas.read_csv(filepath_or_buffer=amr_deletions_file, sep='\t', header=None) |
181 if amr_deletions.shape[0] > 0: | 187 if amr_deletions.shape[0] > 0: |
182 amr_deletions.columns = ['contig', 'start', 'stop', 'name', 'type', 'drug', 'note'] | 188 amr_deletions.columns = ['contig', 'start', 'stop', 'name', 'type', 'drug', 'note'] |
183 amr_deletions = amr_deletions.loc[amr_deletions['type'].isin(['large-deletion', 'any']), :] | 189 amr_deletions = amr_deletions.loc[amr_deletions['type'].isin(['large-deletion', 'any']), :] |
184 for deletion_idx, deleted_gene in amr_deletions.iterrows(): | 190 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)) | 191 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)) |
186 | 192 ofh.write("\nAfter processing deletions, amr_to_draw: %s\n" % str(amr_to_draw)) |
187 if amr_to_draw.shape[0] > 1: | 193 |
194 ofh.write("\namr_to_draw.shape[0]: %s\n" % str(amr_to_draw.shape[0])) | |
195 if amr_to_draw.shape[0] > 0: | |
188 ofh.write("\nDrawing AMR matrix...\n") | 196 ofh.write("\nDrawing AMR matrix...\n") |
189 present_genes = amr_to_draw['gene'].unique() | 197 present_genes = amr_to_draw['gene'].unique() |
190 present_drugs = amr_to_draw['drug'].unique() | 198 present_drugs = amr_to_draw['drug'].unique() |
191 amr_matrix = pandas.DataFrame(0, index=present_genes, columns=present_drugs) | 199 amr_matrix = pandas.DataFrame(0, index=present_genes, columns=present_drugs) |
192 for hit_idx, hit in amr_to_draw.iterrows(): | 200 for hit_idx, hit in amr_to_draw.iterrows(): |