Spaces:
Sleeping
Sleeping
Add chain selector (show heavy/light/both)
Browse files
app.py
CHANGED
|
@@ -357,27 +357,37 @@ def format_flags(flags, h3_len, ok):
|
|
| 357 |
|
| 358 |
|
| 359 |
# -------------------------------------------------------------------- viewer
|
| 360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
"""py3Dmol view -> self-contained HTML in an iframe (renders inside Gradio).
|
| 362 |
-
Framework grey, CDR loops colored, liability residues as magenta sticks.
|
|
|
|
| 363 |
view = py3Dmol.view(width=760, height=560)
|
| 364 |
view.addModel(pdb, "pdb")
|
| 365 |
-
view.setStyle({
|
| 366 |
-
for chain in
|
|
|
|
| 367 |
for cdr, (lo, hi) in CDR_RANGES.items():
|
| 368 |
view.addStyle(
|
| 369 |
{"chain": chain, "resi": list(range(lo, hi + 1))},
|
| 370 |
{"cartoon": {"color": CDR_COLORS[chain][cdr]}},
|
| 371 |
)
|
| 372 |
-
|
| 373 |
-
for chain in ("H", "L"):
|
| 374 |
pos = highlights.get(chain, [])
|
| 375 |
if pos:
|
| 376 |
view.addStyle(
|
| 377 |
{"chain": chain, "resi": pos},
|
| 378 |
{"stick": {"color": "magenta", "radius": 0.3}},
|
| 379 |
)
|
| 380 |
-
view.zoomTo()
|
| 381 |
if spin:
|
| 382 |
view.spin(True)
|
| 383 |
html = view._make_html()
|
|
@@ -386,7 +396,7 @@ def render_structure(pdb: str, spin: bool = False, highlights=None) -> str:
|
|
| 386 |
|
| 387 |
|
| 388 |
# ---------------------------------------------------------------------- fold
|
| 389 |
-
def fold(heavy: str, light: str):
|
| 390 |
"""Fold VH+VL, scan liabilities, render, and reset the spin toggle."""
|
| 391 |
heavy, light = clean(heavy), clean(light)
|
| 392 |
reset_btn = gr.update(value="▶ Spin")
|
|
@@ -404,7 +414,8 @@ def fold(heavy: str, light: str):
|
|
| 404 |
|
| 405 |
pdb = open("myabs_fold.pdb").read()
|
| 406 |
flags, highlights, h3_len, ok = developability(heavy, light, pdb)
|
| 407 |
-
|
|
|
|
| 408 |
status = (f"Folded in {dt:.1f} s · VH {len(heavy)} aa / VL {len(light)} aa · "
|
| 409 |
f"CDRs highlighted (H: yellow/orange/red, L: cyan/blue).")
|
| 410 |
flags_md = format_flags(flags, h3_len, ok)
|
|
@@ -424,13 +435,22 @@ def load_library(name: str):
|
|
| 424 |
return entry["H"], entry["L"], prov
|
| 425 |
|
| 426 |
|
| 427 |
-
def toggle_spin(spin: bool, pdb: str, highlights):
|
| 428 |
"""Flip spin on/off and re-render the stored structure (no re-fold)."""
|
| 429 |
spin = not spin
|
| 430 |
label = "⏸ Stop" if spin else "▶ Spin"
|
| 431 |
if not pdb:
|
| 432 |
return spin, gr.update(value=label), gr.update()
|
| 433 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 434 |
|
| 435 |
|
| 436 |
with gr.Blocks(title="MyAbs") as demo: # theme moved to launch() in Gradio 6
|
|
@@ -455,6 +475,10 @@ with gr.Blocks(title="MyAbs") as demo: # theme moved to launch() in Gradio 6
|
|
| 455 |
status = gr.Markdown()
|
| 456 |
pdb_file = gr.File(label="Download structure (.pdb)")
|
| 457 |
with gr.Column(scale=3):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
viewer = gr.HTML()
|
| 459 |
gr.HTML(LEGEND_HTML)
|
| 460 |
gr.Markdown(
|
|
@@ -472,18 +496,24 @@ with gr.Blocks(title="MyAbs") as demo: # theme moved to launch() in Gradio 6
|
|
| 472 |
|
| 473 |
fold_outputs = [viewer, status, flags_md, pdb_file, pdb_state, hi_state, spin_state, spin_btn]
|
| 474 |
|
| 475 |
-
go.click(fold, inputs=[h_in, l_in], outputs=fold_outputs)
|
| 476 |
|
| 477 |
# Pick a therapeutic -> load its sequences + provenance -> auto-fold.
|
| 478 |
lib_dd.change(load_library, inputs=lib_dd, outputs=[h_in, l_in, provenance]).then(
|
| 479 |
-
fold, inputs=[h_in, l_in], outputs=fold_outputs
|
| 480 |
)
|
| 481 |
|
| 482 |
spin_btn.click(
|
| 483 |
-
toggle_spin, inputs=[spin_state, pdb_state, hi_state],
|
| 484 |
outputs=[spin_state, spin_btn, viewer],
|
| 485 |
)
|
| 486 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
if __name__ == "__main__":
|
| 488 |
# Local dev defaults to 127.0.0.1:7900. On an HF Docker Space the Dockerfile
|
| 489 |
# sets GRADIO_SERVER_NAME=0.0.0.0 and GRADIO_SERVER_PORT=7860 (app_port).
|
|
|
|
| 357 |
|
| 358 |
|
| 359 |
# -------------------------------------------------------------------- viewer
|
| 360 |
+
# Which chains the viewer draws, for the "Show chains" selector.
|
| 361 |
+
CHAIN_VIEW = {
|
| 362 |
+
"Both chains": ("H", "L"),
|
| 363 |
+
"Heavy only (VH)": ("H",),
|
| 364 |
+
"Light only (VL)": ("L",),
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
def render_structure(pdb: str, spin: bool = False, highlights=None,
|
| 369 |
+
chains=("H", "L")) -> str:
|
| 370 |
"""py3Dmol view -> self-contained HTML in an iframe (renders inside Gradio).
|
| 371 |
+
Framework grey, CDR loops colored, liability residues as magenta sticks.
|
| 372 |
+
Only the chains in `chains` are drawn; the rest are hidden."""
|
| 373 |
view = py3Dmol.view(width=760, height=560)
|
| 374 |
view.addModel(pdb, "pdb")
|
| 375 |
+
view.setStyle({}, {}) # hide everything; only the selected chains get drawn below
|
| 376 |
+
for chain in chains:
|
| 377 |
+
view.addStyle({"chain": chain}, {"cartoon": {"color": "#cfd8dc"}})
|
| 378 |
for cdr, (lo, hi) in CDR_RANGES.items():
|
| 379 |
view.addStyle(
|
| 380 |
{"chain": chain, "resi": list(range(lo, hi + 1))},
|
| 381 |
{"cartoon": {"color": CDR_COLORS[chain][cdr]}},
|
| 382 |
)
|
| 383 |
+
if highlights:
|
|
|
|
| 384 |
pos = highlights.get(chain, [])
|
| 385 |
if pos:
|
| 386 |
view.addStyle(
|
| 387 |
{"chain": chain, "resi": pos},
|
| 388 |
{"stick": {"color": "magenta", "radius": 0.3}},
|
| 389 |
)
|
| 390 |
+
view.zoomTo({"chain": list(chains)}) # re-center on the visible chain(s)
|
| 391 |
if spin:
|
| 392 |
view.spin(True)
|
| 393 |
html = view._make_html()
|
|
|
|
| 396 |
|
| 397 |
|
| 398 |
# ---------------------------------------------------------------------- fold
|
| 399 |
+
def fold(heavy: str, light: str, chain_choice: str = "Both chains"):
|
| 400 |
"""Fold VH+VL, scan liabilities, render, and reset the spin toggle."""
|
| 401 |
heavy, light = clean(heavy), clean(light)
|
| 402 |
reset_btn = gr.update(value="▶ Spin")
|
|
|
|
| 414 |
|
| 415 |
pdb = open("myabs_fold.pdb").read()
|
| 416 |
flags, highlights, h3_len, ok = developability(heavy, light, pdb)
|
| 417 |
+
chains = CHAIN_VIEW.get(chain_choice, ("H", "L"))
|
| 418 |
+
viewer = render_structure(pdb, spin=False, highlights=highlights, chains=chains)
|
| 419 |
status = (f"Folded in {dt:.1f} s · VH {len(heavy)} aa / VL {len(light)} aa · "
|
| 420 |
f"CDRs highlighted (H: yellow/orange/red, L: cyan/blue).")
|
| 421 |
flags_md = format_flags(flags, h3_len, ok)
|
|
|
|
| 435 |
return entry["H"], entry["L"], prov
|
| 436 |
|
| 437 |
|
| 438 |
+
def toggle_spin(spin: bool, pdb: str, highlights, chain_choice: str = "Both chains"):
|
| 439 |
"""Flip spin on/off and re-render the stored structure (no re-fold)."""
|
| 440 |
spin = not spin
|
| 441 |
label = "⏸ Stop" if spin else "▶ Spin"
|
| 442 |
if not pdb:
|
| 443 |
return spin, gr.update(value=label), gr.update()
|
| 444 |
+
chains = CHAIN_VIEW.get(chain_choice, ("H", "L"))
|
| 445 |
+
return spin, gr.update(value=label), render_structure(pdb, spin, highlights, chains)
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
def select_chains(chain_choice: str, pdb: str, highlights, spin: bool):
|
| 449 |
+
"""Re-render the stored structure showing only the chosen chain(s), no re-fold."""
|
| 450 |
+
if not pdb:
|
| 451 |
+
return gr.update()
|
| 452 |
+
chains = CHAIN_VIEW.get(chain_choice, ("H", "L"))
|
| 453 |
+
return render_structure(pdb, spin, highlights, chains)
|
| 454 |
|
| 455 |
|
| 456 |
with gr.Blocks(title="MyAbs") as demo: # theme moved to launch() in Gradio 6
|
|
|
|
| 475 |
status = gr.Markdown()
|
| 476 |
pdb_file = gr.File(label="Download structure (.pdb)")
|
| 477 |
with gr.Column(scale=3):
|
| 478 |
+
chain_dd = gr.Radio(
|
| 479 |
+
choices=list(CHAIN_VIEW.keys()), value="Both chains",
|
| 480 |
+
label="Show chains", scale=1,
|
| 481 |
+
)
|
| 482 |
viewer = gr.HTML()
|
| 483 |
gr.HTML(LEGEND_HTML)
|
| 484 |
gr.Markdown(
|
|
|
|
| 496 |
|
| 497 |
fold_outputs = [viewer, status, flags_md, pdb_file, pdb_state, hi_state, spin_state, spin_btn]
|
| 498 |
|
| 499 |
+
go.click(fold, inputs=[h_in, l_in, chain_dd], outputs=fold_outputs)
|
| 500 |
|
| 501 |
# Pick a therapeutic -> load its sequences + provenance -> auto-fold.
|
| 502 |
lib_dd.change(load_library, inputs=lib_dd, outputs=[h_in, l_in, provenance]).then(
|
| 503 |
+
fold, inputs=[h_in, l_in, chain_dd], outputs=fold_outputs
|
| 504 |
)
|
| 505 |
|
| 506 |
spin_btn.click(
|
| 507 |
+
toggle_spin, inputs=[spin_state, pdb_state, hi_state, chain_dd],
|
| 508 |
outputs=[spin_state, spin_btn, viewer],
|
| 509 |
)
|
| 510 |
|
| 511 |
+
# Switch which chain(s) are shown, re-rendering the stored fold (no re-fold).
|
| 512 |
+
chain_dd.change(
|
| 513 |
+
select_chains, inputs=[chain_dd, pdb_state, hi_state, spin_state],
|
| 514 |
+
outputs=viewer,
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
if __name__ == "__main__":
|
| 518 |
# Local dev defaults to 127.0.0.1:7900. On an HF Docker Space the Dockerfile
|
| 519 |
# sets GRADIO_SERVER_NAME=0.0.0.0 and GRADIO_SERVER_PORT=7860 (app_port).
|