Spaces:
Runtime error
Runtime error
Commit ·
edd3b86
1
Parent(s): 5372347
add tf ko
Browse files
app.py
CHANGED
|
@@ -277,78 +277,85 @@ def get_heatmaps(gene, locus1, locus2, heatmap_range):
|
|
| 277 |
return alpha, beta, delta, pp, acinar, duct, psc
|
| 278 |
|
| 279 |
|
| 280 |
-
def perturb(gene, locus1, locus2, use_max_gene):
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
res = {'feat': [], 'log2FC': [], 'var': [], 'a1_idx': [], 'a2_idx': []}
|
| 284 |
-
for c in celltypes:
|
| 285 |
-
res[c] = []
|
| 286 |
-
res[f'{c}_var'] = []
|
| 287 |
-
|
| 288 |
-
links_dict = {}
|
| 289 |
-
for c in celltypes:
|
| 290 |
-
links_dict[c] = {'chrom1': [], 'chrom1Start': [], 'chrom1End': [], 'chrom2': [], 'chrom2Start': [], 'chrom2End': [], 'score': [], 'strand1': [], 'strand2': []}
|
| 291 |
-
|
| 292 |
-
if locus1.startswith('chr'):
|
| 293 |
-
chrom = locus1.split(':')[0]
|
| 294 |
-
pos1 = locus1.split(':')[1]
|
| 295 |
-
a1 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos1))).idxmin()
|
| 296 |
-
else:
|
| 297 |
-
a1 = get_closest_peak_to_gene(locus1, rna, peaks)
|
| 298 |
-
if locus2.startswith('chr'):
|
| 299 |
-
chrom = locus2.split(':')[0]
|
| 300 |
-
pos2 = locus2.split(':')[1]
|
| 301 |
-
a2 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos2))).idxmin()
|
| 302 |
-
else:
|
| 303 |
-
a2 = get_closest_peak_to_gene(locus2, rna, peaks)
|
| 304 |
-
|
| 305 |
-
print(a1)
|
| 306 |
-
print(a2)
|
| 307 |
-
|
| 308 |
-
a1_closest_gene = get_closest_gene_to_peak(a1, rna, peaks)
|
| 309 |
-
a2_closest_gene = get_closest_gene_to_peak(a2, rna, peaks)
|
| 310 |
-
print(a1_closest_gene, a2_closest_gene)
|
| 311 |
-
|
| 312 |
-
guidance_hvf = prior.copy()
|
| 313 |
-
try:
|
| 314 |
-
path_to_gene_a1 = nx.shortest_path(prior, source=a1, target=gene)
|
| 315 |
try:
|
| 316 |
-
guidance_hvf.
|
| 317 |
except:
|
| 318 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
try:
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
except:
|
| 322 |
pass
|
| 323 |
-
except:
|
| 324 |
-
pass
|
| 325 |
-
try:
|
| 326 |
-
path_to_gene_a2 = nx.shortest_path(prior, source=a2, target=gene)
|
| 327 |
try:
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
except:
|
| 330 |
pass
|
| 331 |
try:
|
| 332 |
-
guidance_hvf.remove_edge(
|
|
|
|
| 333 |
except:
|
| 334 |
pass
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
| 336 |
pass
|
| 337 |
-
try:
|
| 338 |
-
guidance_hvf.remove_edge(a1, a2)
|
| 339 |
-
guidance_hvf.remove_edge(a2, a1)
|
| 340 |
-
except:
|
| 341 |
-
pass
|
| 342 |
-
try:
|
| 343 |
-
guidance_hvf.remove_edge(a1, a1_closest_gene)
|
| 344 |
-
guidance_hvf.remove_edge(a1_closest_gene, a1)
|
| 345 |
-
except:
|
| 346 |
-
pass
|
| 347 |
-
try:
|
| 348 |
-
guidance_hvf.remove_edge(a2, a2_closest_gene)
|
| 349 |
-
guidance_hvf.remove_edge(a2_closest_gene, a2)
|
| 350 |
-
except:
|
| 351 |
-
pass
|
| 352 |
|
| 353 |
perterbed_rna_recon = glue.decode_data("rna", "rna", rna, guidance_hvf)
|
| 354 |
ism = np.log2((perterbed_rna_recon + eps) / (rna_recon + eps))
|
|
@@ -364,7 +371,7 @@ def perturb(gene, locus1, locus2, use_max_gene):
|
|
| 364 |
rna.obs['old_mean'] = rna_recon[:, gene_idx]
|
| 365 |
rna.obs['new_mean'] = perterbed_rna_recon[:, gene_idx]
|
| 366 |
# compute new count based on log2FC
|
| 367 |
-
rna.obs['new_count'] = rna.layers['counts'][:, gene_idx] * 2 ** rna.obs['log2FC']
|
| 368 |
|
| 369 |
fig = sc.pl.umap(rna,
|
| 370 |
color=['celltype', 'log2FC'],
|
|
@@ -422,12 +429,14 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
| 422 |
with gr.Row():
|
| 423 |
with gr.Column():
|
| 424 |
in_locus = gr.Textbox(label="Target Gene", elem_id='in-locus', scale=1)
|
| 425 |
-
|
|
|
|
|
|
|
| 426 |
anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
|
| 427 |
anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
|
| 428 |
heatmap_x = gr.Textbox(label="Heatmap X", elem_id='heatmap-x', scale=1, visible=False)
|
| 429 |
heatmap_y = gr.Textbox(label="Heatmap Y", elem_id='heatmap-y', scale=1, visible=False)
|
| 430 |
-
heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=5000, value=
|
| 431 |
heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
|
| 432 |
with gr.Row():
|
| 433 |
out_heatmaps = []
|
|
@@ -443,7 +452,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
| 443 |
out_violin_fc = gr.Image(elem_id='out-violin-fc', scale=1)
|
| 444 |
#out_plot = gr.Plot(elem_id='out-plot', scale=1)
|
| 445 |
|
| 446 |
-
inputs = [in_locus, anchor1, anchor2, max_gene_checkbox]
|
| 447 |
outputs = [out_img, out_violin, out_violin_new, out_violin_fc]
|
| 448 |
|
| 449 |
gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840', False],
|
|
@@ -456,6 +465,7 @@ with gr.Blocks(theme='WeixuanYuan/Soft_dark') as demo:
|
|
| 456 |
['LOXL4', 'chr10:100186215', 'chr10:99913493', False],
|
| 457 |
['KRT19', 'chr17:22,220,637', 'chr17:39,591,813', False],
|
| 458 |
['LPP', 'chr3:188,097,749', 'chr3:197,916,262', False],
|
|
|
|
| 459 |
['CEL', 'chr9:135,937,365', 'chr9:135,973,107', False]],
|
| 460 |
inputs=inputs,
|
| 461 |
outputs=outputs,
|
|
|
|
| 277 |
return alpha, beta, delta, pp, acinar, duct, psc
|
| 278 |
|
| 279 |
|
| 280 |
+
def perturb(gene, locus1, locus2, use_max_gene, tf_knockout):
|
| 281 |
+
if tf_knockout:
|
| 282 |
+
guidance_hvf = prior.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
try:
|
| 284 |
+
guidance_hvf.remove_node(gene)
|
| 285 |
except:
|
| 286 |
pass
|
| 287 |
+
else:
|
| 288 |
+
locus1 = locus1.replace(',', '')
|
| 289 |
+
locus2 = locus2.replace(',', '')
|
| 290 |
+
res = {'feat': [], 'log2FC': [], 'var': [], 'a1_idx': [], 'a2_idx': []}
|
| 291 |
+
for c in celltypes:
|
| 292 |
+
res[c] = []
|
| 293 |
+
res[f'{c}_var'] = []
|
| 294 |
+
|
| 295 |
+
links_dict = {}
|
| 296 |
+
for c in celltypes:
|
| 297 |
+
links_dict[c] = {'chrom1': [], 'chrom1Start': [], 'chrom1End': [], 'chrom2': [], 'chrom2Start': [], 'chrom2End': [], 'score': [], 'strand1': [], 'strand2': []}
|
| 298 |
+
|
| 299 |
+
if locus1.startswith('chr'):
|
| 300 |
+
chrom = locus1.split(':')[0]
|
| 301 |
+
pos1 = locus1.split(':')[1]
|
| 302 |
+
a1 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos1))).idxmin()
|
| 303 |
+
else:
|
| 304 |
+
a1 = get_closest_peak_to_gene(locus1, rna, peaks)
|
| 305 |
+
if locus2.startswith('chr'):
|
| 306 |
+
chrom = locus2.split(':')[0]
|
| 307 |
+
pos2 = locus2.split(':')[1]
|
| 308 |
+
a2 = peaks.query(f"chrom == '{chrom}'")['chromStart'].apply(lambda s: abs(s - int(pos2))).idxmin()
|
| 309 |
+
else:
|
| 310 |
+
a2 = get_closest_peak_to_gene(locus2, rna, peaks)
|
| 311 |
+
|
| 312 |
+
print(a1)
|
| 313 |
+
print(a2)
|
| 314 |
+
|
| 315 |
+
a1_closest_gene = get_closest_gene_to_peak(a1, rna, peaks)
|
| 316 |
+
a2_closest_gene = get_closest_gene_to_peak(a2, rna, peaks)
|
| 317 |
+
print(a1_closest_gene, a2_closest_gene)
|
| 318 |
+
|
| 319 |
+
guidance_hvf = prior.copy()
|
| 320 |
try:
|
| 321 |
+
path_to_gene_a1 = nx.shortest_path(prior, source=a1, target=gene)
|
| 322 |
+
try:
|
| 323 |
+
guidance_hvf.remove_edge(path_to_gene_a1[-2], path_to_gene_a1[-1])
|
| 324 |
+
except:
|
| 325 |
+
pass
|
| 326 |
+
try:
|
| 327 |
+
guidance_hvf.remove_edge(path_to_gene_a1[-1], path_to_gene_a1[-2])
|
| 328 |
+
except:
|
| 329 |
+
pass
|
| 330 |
except:
|
| 331 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
try:
|
| 333 |
+
path_to_gene_a2 = nx.shortest_path(prior, source=a2, target=gene)
|
| 334 |
+
try:
|
| 335 |
+
guidance_hvf.remove_edge(path_to_gene_a2[-2], path_to_gene_a2[-1])
|
| 336 |
+
except:
|
| 337 |
+
pass
|
| 338 |
+
try:
|
| 339 |
+
guidance_hvf.remove_edge(path_to_gene_a2[-1], path_to_gene_a2[-2])
|
| 340 |
+
except:
|
| 341 |
+
pass
|
| 342 |
+
except:
|
| 343 |
+
pass
|
| 344 |
+
try:
|
| 345 |
+
guidance_hvf.remove_edge(a1, a2)
|
| 346 |
+
guidance_hvf.remove_edge(a2, a1)
|
| 347 |
except:
|
| 348 |
pass
|
| 349 |
try:
|
| 350 |
+
guidance_hvf.remove_edge(a1, a1_closest_gene)
|
| 351 |
+
guidance_hvf.remove_edge(a1_closest_gene, a1)
|
| 352 |
except:
|
| 353 |
pass
|
| 354 |
+
try:
|
| 355 |
+
guidance_hvf.remove_edge(a2, a2_closest_gene)
|
| 356 |
+
guidance_hvf.remove_edge(a2_closest_gene, a2)
|
| 357 |
+
except:
|
| 358 |
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
perterbed_rna_recon = glue.decode_data("rna", "rna", rna, guidance_hvf)
|
| 361 |
ism = np.log2((perterbed_rna_recon + eps) / (rna_recon + eps))
|
|
|
|
| 371 |
rna.obs['old_mean'] = rna_recon[:, gene_idx]
|
| 372 |
rna.obs['new_mean'] = perterbed_rna_recon[:, gene_idx]
|
| 373 |
# compute new count based on log2FC
|
| 374 |
+
rna.obs['new_count'] = (rna.layers['counts'][:, gene_idx] + eps) * 2 ** rna.obs['log2FC'] - eps
|
| 375 |
|
| 376 |
fig = sc.pl.umap(rna,
|
| 377 |
color=['celltype', 'log2FC'],
|
|
|
|
| 429 |
with gr.Row():
|
| 430 |
with gr.Column():
|
| 431 |
in_locus = gr.Textbox(label="Target Gene", elem_id='in-locus', scale=1)
|
| 432 |
+
with gr.Row():
|
| 433 |
+
max_gene_checkbox = gr.Checkbox(label="Max Gene", elem_id='max-gene-checkbox', scale=1, checked=False)
|
| 434 |
+
tf_ko = gr.Checkbox(label="TF KO", elem_id='tf-ko', scale=1, checked=False)
|
| 435 |
anchor1 = gr.Textbox(label="Locus 1 (gene or genomic coords)", elem_id='anchor1', scale=1)
|
| 436 |
anchor2 = gr.Textbox(label="Locus 2 (gene or genomic coords)", elem_id='anchor2', scale=1)
|
| 437 |
heatmap_x = gr.Textbox(label="Heatmap X", elem_id='heatmap-x', scale=1, visible=False)
|
| 438 |
heatmap_y = gr.Textbox(label="Heatmap Y", elem_id='heatmap-y', scale=1, visible=False)
|
| 439 |
+
heatmap_size = gr.Slider(label="Heatmap Range", info="kb range aroung input gene locus to expand", minimum=50, maximum=5000, value=250)
|
| 440 |
heatmap_button = gr.Button(value="Generate Heatmaps", elem_id='heatmap-button', scale=1)
|
| 441 |
with gr.Row():
|
| 442 |
out_heatmaps = []
|
|
|
|
| 452 |
out_violin_fc = gr.Image(elem_id='out-violin-fc', scale=1)
|
| 453 |
#out_plot = gr.Plot(elem_id='out-plot', scale=1)
|
| 454 |
|
| 455 |
+
inputs = [in_locus, anchor1, anchor2, max_gene_checkbox, tf_ko]
|
| 456 |
outputs = [out_img, out_violin, out_violin_new, out_violin_fc]
|
| 457 |
|
| 458 |
gr.Examples(examples=[['INS', 'chr11:2,289,895', 'chr11:2,298,840', False],
|
|
|
|
| 465 |
['LOXL4', 'chr10:100186215', 'chr10:99913493', False],
|
| 466 |
['KRT19', 'chr17:22,220,637', 'chr17:39,591,813', False],
|
| 467 |
['LPP', 'chr3:188,097,749', 'chr3:197,916,262', False],
|
| 468 |
+
['MAFB', 'chr20:39431654', 'chr20:39368271', True],
|
| 469 |
['CEL', 'chr9:135,937,365', 'chr9:135,973,107', False]],
|
| 470 |
inputs=inputs,
|
| 471 |
outputs=outputs,
|