dylanplummer commited on
Commit
0bc69db
·
1 Parent(s): 13640f6

add all heatmaps

Browse files
Files changed (1) hide show
  1. app.py +22 -8
app.py CHANGED
@@ -41,8 +41,12 @@ graph_file = hf_hub_download(repo_id="dylanplummer/islet-epigenome", filename=f"
41
  rna_file = unzip(rna_file)
42
  hic_file = unzip(hic_file)
43
 
44
- beta_cooler_file = hf_hub_download(repo_id="dylanplummer/islet-epigenome", filename=f"pseudobulk_coolers/Beta_50kb.cool", repo_type="dataset", token=os.environ['DATASET_SECRET'])
45
- beta_cooler = cooler.Cooler(beta_cooler_file)
 
 
 
 
46
 
47
  model_file = hf_hub_download(repo_id="dylanplummer/HiGLUE-islet", filename=f"glue_hic_{prior_name}_prior_{resolution}_{suffix}.dill", repo_type="model", token=os.environ['DATASET_SECRET'])
48
 
@@ -173,14 +177,15 @@ def get_heatmap_locus(gene, locus1, locus2, heatmap_range):
173
  a2_start = peaks.loc[a2, 'chromStart']
174
  interaction_dist = abs(a1_start - a2_start)
175
  chrom_size = beta_cooler.chromsizes[chrom]
176
- heatmap_locus = f'{chrom}:{max(0, chromStart - interaction_dist - heatmap_range)}-{min(chrom_size, chromEnd + interaction_dist + heatmap_range)}'
177
  print(heatmap_locus)
178
  return heatmap_locus
179
 
180
 
181
- def get_heatmap(gene, locus1, locus2, heatmap_range):
182
  heatmap_locus = get_heatmap_locus(gene, locus1, locus2, heatmap_range)
183
- mat = beta_cooler.matrix().fetch(heatmap_locus)
 
184
  #img = draw_heatmap(mat, 0, return_image=True)
185
  fig, ax = plt.subplots(figsize=(4, 4))
186
  ax.imshow(mat, cmap='Reds', norm=PowerNorm(gamma=0.5), interpolation=None)
@@ -199,6 +204,12 @@ def get_heatmap(gene, locus1, locus2, heatmap_range):
199
  plt.close()
200
  return img
201
 
 
 
 
 
 
 
202
 
203
  def perturb(gene, locus1, locus2):
204
  locus1 = locus1.replace(',', '')
@@ -328,7 +339,9 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
328
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
329
  heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=30000, value=2000)
330
  with gr.Row():
331
- out_heatmap = gr.Image(elem_id='out-heatmap', scale=1)
 
 
332
  with gr.Row():
333
  heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
334
  run_button = gr.Button(value="Run", elem_id='run-button', scale=1)
@@ -354,7 +367,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
354
  outputs=outputs,
355
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
356
  run_button.click(perturb, inputs, outputs=outputs)
357
- heatmap_button.click(get_heatmap, [in_locus, anchor1, anchor2, heatmap_size], outputs=[out_heatmap])
358
  def set_loop(img, gene, locus1, locus2, heatmap_range, evt: gr.SelectData):
359
  h, w = img.shape[:2]
360
  idx = evt.index
@@ -366,7 +379,8 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
366
  new_a1 = f"{bins.iloc[bins_idx_x]['chrom']}:{bins.iloc[bins_idx_x]['start']}"
367
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
368
  return new_a1, new_a2
369
- out_heatmap.select(set_loop, [out_heatmap, in_locus, anchor1, anchor2, heatmap_size], outputs=[anchor1, anchor2])
 
370
 
371
 
372
  if __name__ == "__main__":
 
41
  rna_file = unzip(rna_file)
42
  hic_file = unzip(hic_file)
43
 
44
+ celltypes = ['Alpha', 'Beta', 'Delta', 'PP', 'Acinar', 'Duct', 'PSC', 'Endothelial']
45
+ coolers = []
46
+ for celltype in celltypes:
47
+ cooler_file = hf_hub_download(repo_id="dylanplummer/islet-epigenome", filename=f"pseudobulk_coolers/{celltype}_50kb.cool", repo_type="dataset", token=os.environ['DATASET_SECRET'])
48
+ coolers.append(cooler.Cooler(cooler_file))
49
+ beta_cooler = coolers[1]
50
 
51
  model_file = hf_hub_download(repo_id="dylanplummer/HiGLUE-islet", filename=f"glue_hic_{prior_name}_prior_{resolution}_{suffix}.dill", repo_type="model", token=os.environ['DATASET_SECRET'])
52
 
 
177
  a2_start = peaks.loc[a2, 'chromStart']
178
  interaction_dist = abs(a1_start - a2_start)
179
  chrom_size = beta_cooler.chromsizes[chrom]
180
+ heatmap_locus = f'{chrom}:{max(0, chromStart - interaction_dist - heatmap_range * 1000)}-{min(chrom_size, chromEnd + interaction_dist + heatmap_range * 1000)}'
181
  print(heatmap_locus)
182
  return heatmap_locus
183
 
184
 
185
+ def get_heatmap(celltype, gene, locus1, locus2, heatmap_range):
186
  heatmap_locus = get_heatmap_locus(gene, locus1, locus2, heatmap_range)
187
+ c = coolers[celltypes.index(celltype)]
188
+ mat = c.matrix().fetch(heatmap_locus)
189
  #img = draw_heatmap(mat, 0, return_image=True)
190
  fig, ax = plt.subplots(figsize=(4, 4))
191
  ax.imshow(mat, cmap='Reds', norm=PowerNorm(gamma=0.5), interpolation=None)
 
204
  plt.close()
205
  return img
206
 
207
+ def get_heatmaps(gene, locus1, locus2, heatmap_range):
208
+ res = []
209
+ for celltype in celltypes:
210
+ res.append(get_heatmap(celltype, gene, locus1, locus2, heatmap_range))
211
+ return res
212
+
213
 
214
  def perturb(gene, locus1, locus2):
215
  locus1 = locus1.replace(',', '')
 
339
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
340
  heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=30000, value=2000)
341
  with gr.Row():
342
+ out_heatmaps = []
343
+ for celltype, c in zip(celltypes, coolers):
344
+ out_heatmaps.append(gr.Image(label=c, elem_id=f'out-heatmap-{celltype}', scale=1))
345
  with gr.Row():
346
  heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
347
  run_button = gr.Button(value="Run", elem_id='run-button', scale=1)
 
367
  outputs=outputs,
368
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
369
  run_button.click(perturb, inputs, outputs=outputs)
370
+ heatmap_button.click(get_heatmaps, [in_locus, anchor1, anchor2, heatmap_size], outputs=out_heatmaps)
371
  def set_loop(img, gene, locus1, locus2, heatmap_range, evt: gr.SelectData):
372
  h, w = img.shape[:2]
373
  idx = evt.index
 
379
  new_a1 = f"{bins.iloc[bins_idx_x]['chrom']}:{bins.iloc[bins_idx_x]['start']}"
380
  new_a2 = f"{bins.iloc[bins_idx_y]['chrom']}:{bins.iloc[bins_idx_y]['start']}"
381
  return new_a1, new_a2
382
+ for out_heatmap in out_heatmaps:
383
+ out_heatmap.select(set_loop, [out_heatmap, in_locus, anchor1, anchor2, heatmap_size], outputs=[anchor1, anchor2])
384
 
385
 
386
  if __name__ == "__main__":