dylanplummer commited on
Commit
7ea80fb
·
1 Parent(s): 53541a8

more cleanup and prettyify

Browse files
Files changed (1) hide show
  1. app.py +10 -27
app.py CHANGED
@@ -9,8 +9,6 @@ import shutil
9
  import numpy as np
10
  import matplotlib.pyplot as plt
11
 
12
- from itertools import chain
13
-
14
  from huggingface_hub import hf_hub_download
15
 
16
 
@@ -58,10 +56,7 @@ color_map['Other'] = 'gray'
58
 
59
  hic = ad.read_h5ad(hic_file)
60
  hic.var["highly_variable"] = hic.var[f"{prior_name}_highly_variable"]
61
- print(hic)
62
- print('Loading prior knowledge graph...')
63
  prior = nx.read_graphml(graph_file)
64
- #scglue.models.configure_dataset(hic, "NB", use_highly_variable=True)
65
  glue = scglue.models.load_model(model_file)
66
 
67
 
@@ -76,18 +71,14 @@ for e, attr in dict(prior.edges).items():
76
  genes.append(gene_name)
77
  elif attr["type"] == 'hic':
78
  loops.append(e)
79
- print(len(genes))
80
  rna.var["highly_variable"] = rna.var["highly_variable"] & rna.var["in_hic"]
81
  genes = rna.var.query(f"highly_variable").index
82
- print(len(genes))
83
  gene_idx_map = {}
84
  for i, gene in enumerate(genes):
85
  gene_idx_map[gene] = i
86
- peaks = hic.var.query("highly_variable")
87
 
88
  rna_recon = glue.decode_data("rna", "rna", rna, prior)
89
- #hic_recon = glue.decode_data("hic", "hic", hic, prior)
90
- print(rna_recon.shape)
91
 
92
 
93
  def get_closest_peak_to_gene(gene_name, rna, peaks):
@@ -118,7 +109,7 @@ def get_chromosome_from_filename(filename):
118
  def perturb(gene, locus1, locus2):
119
  locus1 = locus1.replace(',', '')
120
  locus2 = locus2.replace(',', '')
121
- res = {'feat': [], 'ism': [], 'var': [], 'a1_idx': [], 'a2_idx': []}
122
  for c in celltypes:
123
  res[c] = []
124
  res[f'{c}_var'] = []
@@ -181,12 +172,12 @@ def perturb(gene, locus1, locus2):
181
  print('Max gene:', max_gene)
182
  # get integer index of gene
183
  gene_idx = gene_idx_map[gene]
184
- rna.obs['ism'] = ism[:, gene_idx]
185
 
186
  fig = sc.pl.umap(rna,
187
- color=["celltype", "ism"],
188
  color_map='Spectral',
189
- wspace=0.15,
190
  legend_loc='on data',
191
  legend_fontoutline=2,
192
  frameon=False,
@@ -204,24 +195,14 @@ def perturb(gene, locus1, locus2):
204
 
205
  return image_from_plot
206
 
207
- # if max_gene != gene:
208
- # rna.obs['ism_max'] = ism[:, max_gene_idx]
209
- # fig = sc.pl.umap(rna, color=["celltype", "ism_max"], color_map='Spectral', wspace=0.35, return_fig=True)
210
- # filename = os.path.join(plot_dir, f'{gene}/islet_glue_rna_umap_perturb_{a1}_{a2}_{max_gene}.png')
211
- # fig.suptitle(f'{max_gene} after removing ' + a1 + '-' + a2)
212
- # fig.tight_layout()
213
- # fig.savefig(filename)
214
- # #fig.savefig(filename.replace('.png', '.pdf'))
215
- # plt.close()
216
-
217
 
218
 
219
  with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
220
  with gr.Row():
221
  with gr.Column():
222
- in_locus = gr.Textbox(label="Gene", elem_id='in-locus', scale=1)
223
- anchor1 = gr.Textbox(label="Locus 1", elem_id='anchor1', scale=1)
224
- anchor2 = gr.Textbox(label="Locus 2", elem_id='anchor2', scale=1)
225
  with gr.Row():
226
  run_button = gr.Button(label="Run", elem_id='run-button', scale=1)
227
  with gr.Row():
@@ -230,7 +211,9 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
230
 
231
  gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840'],
232
  ['INS', 'INS', 'IGF1'],
 
233
  ['GCG', 'GCG', 'FAP'],
 
234
  ['LPP', 'chr3:188,097,749', 'chr3:197,916,262'],
235
  ['CEL', 'chr9:135,937,365', 'chr9:135,973,107']],
236
  inputs=[in_locus, anchor1, anchor2],
 
9
  import numpy as np
10
  import matplotlib.pyplot as plt
11
 
 
 
12
  from huggingface_hub import hf_hub_download
13
 
14
 
 
56
 
57
  hic = ad.read_h5ad(hic_file)
58
  hic.var["highly_variable"] = hic.var[f"{prior_name}_highly_variable"]
 
 
59
  prior = nx.read_graphml(graph_file)
 
60
  glue = scglue.models.load_model(model_file)
61
 
62
 
 
71
  genes.append(gene_name)
72
  elif attr["type"] == 'hic':
73
  loops.append(e)
 
74
  rna.var["highly_variable"] = rna.var["highly_variable"] & rna.var["in_hic"]
75
  genes = rna.var.query(f"highly_variable").index
 
76
  gene_idx_map = {}
77
  for i, gene in enumerate(genes):
78
  gene_idx_map[gene] = i
79
+ peaks = hic.var.query("highly_variable").copy()
80
 
81
  rna_recon = glue.decode_data("rna", "rna", rna, prior)
 
 
82
 
83
 
84
  def get_closest_peak_to_gene(gene_name, rna, peaks):
 
109
  def perturb(gene, locus1, locus2):
110
  locus1 = locus1.replace(',', '')
111
  locus2 = locus2.replace(',', '')
112
+ res = {'feat': [], 'log2FC': [], 'var': [], 'a1_idx': [], 'a2_idx': []}
113
  for c in celltypes:
114
  res[c] = []
115
  res[f'{c}_var'] = []
 
172
  print('Max gene:', max_gene)
173
  # get integer index of gene
174
  gene_idx = gene_idx_map[gene]
175
+ rna.obs['log2FC'] = ism[:, gene_idx]
176
 
177
  fig = sc.pl.umap(rna,
178
+ color=['celltype', 'log2FC'],
179
  color_map='Spectral',
180
+ wspace=0.05,
181
  legend_loc='on data',
182
  legend_fontoutline=2,
183
  frameon=False,
 
195
 
196
  return image_from_plot
197
 
 
 
 
 
 
 
 
 
 
 
198
 
199
 
200
  with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
201
  with gr.Row():
202
  with gr.Column():
203
+ in_locus = gr.Textbox(label="Target Gene", elem_id='in-locus', scale=1)
204
+ anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
205
+ anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
206
  with gr.Row():
207
  run_button = gr.Button(label="Run", elem_id='run-button', scale=1)
208
  with gr.Row():
 
211
 
212
  gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840'],
213
  ['INS', 'INS', 'IGF1'],
214
+ ['TSPAN1', 'TSPAN1', 'PIK3R3'],
215
  ['GCG', 'GCG', 'FAP'],
216
+ ['KRT19', 'chr17:22,220,637', 'chr17:39,591,813'],
217
  ['LPP', 'chr3:188,097,749', 'chr3:197,916,262'],
218
  ['CEL', 'chr9:135,937,365', 'chr9:135,973,107']],
219
  inputs=[in_locus, anchor1, anchor2],