dylanplummer commited on
Commit
2945e69
·
1 Parent(s): e115ec1

add loop scatter

Browse files
Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -194,13 +194,15 @@ 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):
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.5), interpolation=None)
 
 
204
  # remove white padding
205
  plt.subplots_adjust(left=0, right=1, top=1, bottom=0)
206
  plt.axis('off')
@@ -216,10 +218,10 @@ def get_heatmap(celltype, gene, locus1, locus2, heatmap_range):
216
  plt.close()
217
  return img
218
 
219
- def get_heatmaps(gene, locus1, locus2, heatmap_range):
220
  res = []
221
  for celltype in cooler_celltypes:
222
- res.append(get_heatmap(celltype, gene, locus1, locus2, heatmap_range))
223
  alpha, beta, delta, pp, acinar, duct, psc, endothelial = res
224
  return alpha, beta, delta, pp, acinar, duct, psc, endothelial
225
 
@@ -350,10 +352,12 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
350
  in_locus = gr.Textbox(label="Target Gene", elem_id='in-locus', scale=1)
351
  anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
352
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
 
 
353
  heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=30000, value=2000)
354
  with gr.Row():
355
  out_heatmaps = []
356
- for celltype, c in zip(cooler_celltypes, coolers):
357
  out_heatmaps.append(gr.Image(label=celltype, elem_id=f'out-heatmap-{celltype}', scale=1))
358
  with gr.Row():
359
  heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
@@ -380,7 +384,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
380
  outputs=outputs,
381
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
382
  run_button.click(perturb, inputs, outputs=outputs)
383
- heatmap_button.click(get_heatmaps, [in_locus, anchor1, anchor2, heatmap_size], outputs=out_heatmaps)
384
  def set_loop(img, gene, locus1, locus2, heatmap_range, evt: gr.SelectData):
385
  h, w = img.shape[:2]
386
  idx = evt.index
@@ -393,7 +397,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
393
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
394
  return new_a1, new_a2
395
  for out_heatmap in out_heatmaps:
396
- out_heatmap.select(set_loop, [out_heatmap, in_locus, anchor1, anchor2, heatmap_size], outputs=[anchor1, anchor2])
397
 
398
 
399
  if __name__ == "__main__":
 
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 is not None and heatmap_y is not None:
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')
 
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
 
 
352
  in_locus = gr.Textbox(label="Target Gene", elem_id='in-locus', scale=1)
353
  anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
354
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
355
+ heatmap_x = gr.Textbox(label="Heatmap X", elem_id='heatmap-x', scale=1, visible=False)
356
+ heatmap_y = gr.Textbox(label="Heatmap Y", elem_id='heatmap-y', scale=1, visible=False)
357
  heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=30000, value=2000)
358
  with gr.Row():
359
  out_heatmaps = []
360
+ for celltype in cooler_celltypes:
361
  out_heatmaps.append(gr.Image(label=celltype, elem_id=f'out-heatmap-{celltype}', scale=1))
362
  with gr.Row():
363
  heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
 
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
 
397
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
398
  return new_a1, new_a2
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__":