dylanplummer commited on
Commit
fbada56
·
1 Parent(s): 7694a74

fix updateing indices

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -194,20 +194,32 @@ def get_heatmap_locus(gene, locus1, locus2, heatmap_range):
194
  return heatmap_locus, res
195
 
196
 
197
- def get_heatmap(celltype, gene, locus1, locus2, heatmap_range, heatmap_x, heatmap_y):
198
  heatmap_locus, res = get_heatmap_locus(gene, locus1, locus2, heatmap_range)
199
  c = coolers[res][cooler_celltypes.index(celltype)]
200
  mat = c.matrix().fetch(heatmap_locus)
 
 
 
 
 
 
 
 
201
  #img = draw_heatmap(mat, 0, return_image=True)
202
  fig, ax = plt.subplots(figsize=(4, 4))
203
  ax.imshow(mat, cmap='Reds', norm=PowerNorm(gamma=0.3), interpolation=None)
204
- if heatmap_x != '' and heatmap_y != '':
205
- ax.scatter(int(heatmap_x), int(heatmap_y), color='green', marker='*', s=100)
206
  # remove white padding
207
  plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
208
  plt.axis('off')
209
  plt.axis('image')
210
 
 
 
 
 
 
211
  # redraw the canvas
212
  fig = plt.gcf()
213
  fig.canvas.draw()
@@ -218,10 +230,10 @@ def get_heatmap(celltype, gene, locus1, locus2, heatmap_range, heatmap_x, heatma
218
  plt.close()
219
  return img
220
 
221
- def get_heatmaps(gene, locus1, locus2, heatmap_range, heatmap_x, heatmap_y):
222
  res = []
223
  for celltype in cooler_celltypes:
224
- res.append(get_heatmap(celltype, gene, locus1, locus2, heatmap_range, heatmap_x, heatmap_y))
225
  alpha, beta, delta, pp, acinar, duct, psc, endothelial = res
226
  return alpha, beta, delta, pp, acinar, duct, psc, endothelial
227
 
@@ -384,7 +396,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
384
  outputs=outputs,
385
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
386
  run_button.click(perturb, inputs, outputs=outputs)
387
- heatmap_button.click(get_heatmaps, [in_locus, anchor1, anchor2, heatmap_size, heatmap_x, heatmap_y], outputs=out_heatmaps)
388
  def set_loop(img, gene, locus1, locus2, heatmap_range, evt: gr.SelectData):
389
  h, w = img.shape[:2]
390
  idx = evt.index
@@ -395,9 +407,9 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
395
  bins_idx_y = int(y / h * len(bins))
396
  new_a1 = f"{bins.iloc[bins_idx_x]['chrom']}:{bins.iloc[bins_idx_x]['start']}"
397
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
398
- return new_a1, new_a2, x, y
399
  for out_heatmap in out_heatmaps:
400
- out_heatmap.select(set_loop, [out_heatmap, in_locus, anchor1, anchor2, heatmap_size], outputs=[anchor1, anchor2, heatmap_x, heatmap_y])
401
 
402
 
403
  if __name__ == "__main__":
 
194
  return heatmap_locus, res
195
 
196
 
197
+ def get_heatmap(celltype, gene, locus1, locus2, heatmap_range):
198
  heatmap_locus, res = get_heatmap_locus(gene, locus1, locus2, heatmap_range)
199
  c = coolers[res][cooler_celltypes.index(celltype)]
200
  mat = c.matrix().fetch(heatmap_locus)
201
+ bins = c.bins().fetch(heatmap_locus)
202
+ a1_chrom = locus1.split(':')[0]
203
+ a1_start = int(locus1.split(':')[1])
204
+ a1_idx = bins.query(f"chrom == '{a1_chrom}'")['start'].apply(lambda s: abs(s - a1_start)).argmin()
205
+ a2_chrom = locus2.split(':')[0]
206
+ a2_start = int(locus2.split(':')[1])
207
+ a2_idx = bins.query(f"chrom == '{a2_chrom}'")['start'].apply(lambda s: abs(s - a2_start)).argmin()
208
+ print(a1_idx, a2_idx)
209
  #img = draw_heatmap(mat, 0, return_image=True)
210
  fig, ax = plt.subplots(figsize=(4, 4))
211
  ax.imshow(mat, cmap='Reds', norm=PowerNorm(gamma=0.3), interpolation=None)
212
+
 
213
  # remove white padding
214
  plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
215
  plt.axis('off')
216
  plt.axis('image')
217
 
218
+ w, h = fig.canvas.get_width_height()
219
+ heatmap_x = int((a1_idx / len(bins)) * w)
220
+ heatmap_y = int((a2_idx / len(bins)) * h)
221
+ ax.scatter(int(heatmap_x), int(heatmap_y), color='green', marker='*', s=100)
222
+
223
  # redraw the canvas
224
  fig = plt.gcf()
225
  fig.canvas.draw()
 
230
  plt.close()
231
  return img
232
 
233
+ def get_heatmaps(gene, locus1, locus2, heatmap_range):
234
  res = []
235
  for celltype in cooler_celltypes:
236
+ res.append(get_heatmap(celltype, gene, locus1, locus2, heatmap_range))
237
  alpha, beta, delta, pp, acinar, duct, psc, endothelial = res
238
  return alpha, beta, delta, pp, acinar, duct, psc, endothelial
239
 
 
396
  outputs=outputs,
397
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
398
  run_button.click(perturb, inputs, outputs=outputs)
399
+ heatmap_button.click(get_heatmaps, [in_locus, anchor1, anchor2, heatmap_size], outputs=out_heatmaps)
400
  def set_loop(img, gene, locus1, locus2, heatmap_range, evt: gr.SelectData):
401
  h, w = img.shape[:2]
402
  idx = evt.index
 
407
  bins_idx_y = int(y / h * len(bins))
408
  new_a1 = f"{bins.iloc[bins_idx_x]['chrom']}:{bins.iloc[bins_idx_x]['start']}"
409
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
410
+ return new_a1, new_a2
411
  for out_heatmap in out_heatmaps:
412
+ out_heatmap.select(set_loop, [out_heatmap, in_locus, anchor1, anchor2, heatmap_size], outputs=[anchor1, anchor2])
413
 
414
 
415
  if __name__ == "__main__":