dylanplummer commited on
Commit
e36e2a7
·
1 Parent(s): 471fb7f

heatmap test

Browse files
Files changed (2) hide show
  1. app.py +39 -0
  2. requirements.txt +3 -1
app.py CHANGED
@@ -6,7 +6,9 @@ import scglue
6
  import os
7
  import gzip
8
  import shutil
 
9
  import numpy as np
 
10
  import matplotlib.pyplot as plt
11
 
12
  from huggingface_hub import hf_hub_download
@@ -22,6 +24,7 @@ dist_range = 10e6
22
  window_size = 64
23
  eps = 1e-4
24
  n_neighbors = 5
 
25
 
26
 
27
  def unzip(file):
@@ -38,6 +41,9 @@ graph_file = hf_hub_download(repo_id="dylanplummer/islet-epigenome", filename=f"
38
  rna_file = unzip(rna_file)
39
  hic_file = unzip(hic_file)
40
 
 
 
 
41
  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'])
42
 
43
  rna = ad.read_h5ad(rna_file)
@@ -105,6 +111,35 @@ def get_chromosome_from_filename(filename):
105
  return filename[chr_index:]
106
  else:
107
  return filename[chr_index: file_ending_index]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  def perturb(gene, locus1, locus2):
110
  locus1 = locus1.replace(',', '')
@@ -233,6 +268,9 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
233
  anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
234
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
235
  with gr.Row():
 
 
 
236
  run_button = gr.Button(label="Run", elem_id='run-button', scale=1)
237
  with gr.Row():
238
  out_img = gr.Image(elem_id='out-img', scale=1)
@@ -256,6 +294,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
256
  outputs=outputs,
257
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
258
  run_button.click(perturb, inputs, outputs=outputs)
 
259
 
260
 
261
  if __name__ == "__main__":
 
6
  import os
7
  import gzip
8
  import shutil
9
+ import cooler
10
  import numpy as np
11
+ import plotply.express as px
12
  import matplotlib.pyplot as plt
13
 
14
  from huggingface_hub import hf_hub_download
 
24
  window_size = 64
25
  eps = 1e-4
26
  n_neighbors = 5
27
+ heatmap_range = 3 # 3X the distance of the interaction
28
 
29
 
30
  def unzip(file):
 
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
 
49
  rna = ad.read_h5ad(rna_file)
 
111
  return filename[chr_index:]
112
  else:
113
  return filename[chr_index: file_ending_index]
114
+
115
+ def get_heatmap(gene, locus1, locus2):
116
+ try:
117
+ loc = rna.var.loc[gene]
118
+ except KeyError:
119
+ print('Could not find loci', gene)
120
+ return None
121
+ chrom = loc["chrom"]
122
+ chromStart = loc["chromStart"]
123
+ chromEnd = loc["chromEnd"]
124
+ if locus1.startswith('chr'):
125
+ chrom = locus1.split(':')[0]
126
+ pos1 = locus1.split(':')[1]
127
+ a1 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos1))).idxmin()
128
+ else:
129
+ a1 = get_closest_peak_to_gene(locus1, rna, peaks)
130
+ if locus2.startswith('chr'):
131
+ chrom = locus2.split(':')[0]
132
+ pos2 = locus2.split(':')[1]
133
+ a2 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos2))).idxmin()
134
+ else:
135
+ a2 = get_closest_peak_to_gene(locus2, rna, peaks)
136
+ a1_start = peaks.loc[a1, 'chromStart']
137
+ a2_start = peaks.loc[a2, 'chromStart']
138
+ interaction_dist = abs(a1_start - a2_start)
139
+ heatmap_locus = f'{chrom}:{chromStart - interaction_dist * heatmap_range}:{chromEnd + interaction_dist * heatmap_range}'
140
+ mat = beta_cooler.matrix().fetch(heatmap_locus)
141
+ fig = px.imshow(mat)
142
+ return fig
143
 
144
  def perturb(gene, locus1, locus2):
145
  locus1 = locus1.replace(',', '')
 
268
  anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
269
  anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
270
  with gr.Row():
271
+ out_heatmap = gr.Plot(elem_id='out-heatmap', scale=1)
272
+ with gr.Row():
273
+ heatmap_button = gr.Button(label="Generate Heatmaps", elem_id='heatmap-button', scale=1)
274
  run_button = gr.Button(label="Run", elem_id='run-button', scale=1)
275
  with gr.Row():
276
  out_img = gr.Image(elem_id='out-img', scale=1)
 
294
  outputs=outputs,
295
  fn=perturb, cache_examples=os.getenv('SYSTEM') == 'spaces')
296
  run_button.click(perturb, inputs, outputs=outputs)
297
+ heatmap_button.click(get_heatmap, inputs, outputs=[out_heatmap])
298
 
299
 
300
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -4,4 +4,6 @@ scanpy
4
  scglue
5
  numpy
6
  pandas<2.0.0
7
- matplotlib
 
 
 
4
  scglue
5
  numpy
6
  pandas<2.0.0
7
+ matplotlib
8
+ cooler
9
+ plotly