kowith-2 commited on
Commit
f950b7d
·
verified ·
1 Parent(s): 6317315

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +98 -65
  2. core.py +20 -16
app.py CHANGED
@@ -962,24 +962,30 @@ def _poseview_ui(
962
  "View mode", ["🖱 Interactive (drag)", "🖼 Static SVG"],
963
  horizontal=True, key=btn_key + "_viewmode",
964
  )
965
- _cl2, _cr2 = st.columns(2)
966
- with _cl2:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
967
  st.markdown("##### Docked Pose")
968
  if _view_mode.startswith("🖱") and _new_ihtml:
969
  components.html(_new_ihtml, height=860, scrolling=False)
970
  else:
971
  _show_svg_new(_new_svg, f"pose{pose_idx+1}_interaction")
972
- with _cr2:
973
- st.markdown("##### Co-Crystal Reference")
974
- if _new_ref_svg:
975
- if _view_mode.startswith("🖱") and _new_ref_ihtml:
976
- components.html(_new_ref_ihtml, height=860, scrolling=False)
977
- else:
978
- _show_svg_new(_new_ref_svg, "cocrystal_interaction")
979
- elif _has_ref_local:
980
- st.info("Click Generate to produce the co-crystal diagram.")
981
- else:
982
- st.caption("No co-crystal ligand — use Auto-detect in receptor prep.")
983
 
984
 
985
  st.markdown("---")
@@ -1253,26 +1259,33 @@ def _poseview_ui(
1253
  )
1254
 
1255
  if _rdk_svg and not _rdk_stale:
1256
- _col_l2, _col_r2 = st.columns(2)
1257
- with _col_l2:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1258
  st.markdown("##### 🧪 Docked Pose (RDKit)")
1259
  _show_rdkit_svg_tab(
1260
  _rdk_svg,
1261
  dl_key_prefix=btn_key + "_rdk_dl",
1262
  dl_filename=f"pose{pose_idx+1}_rdkit.svg",
1263
  )
1264
- with _col_r2:
1265
- st.markdown("##### 🔮 Co-Crystal Reference (RDKit)")
1266
- if _ref_rdk_svg:
1267
- _show_rdkit_svg_tab(
1268
- _ref_rdk_svg,
1269
- dl_key_prefix=btn_key + "_rdk_ref_dl",
1270
- dl_filename="cocrystal_rdkit.svg",
1271
- )
1272
- elif _has_ref_rdkit2:
1273
- st.info("Click **Generate RDKit Diagrams** to generate co-crystal diagram.")
1274
- else:
1275
- st.caption("⚠️ No co-crystal ligand — use Auto-detect in receptor preparation.")
1276
 
1277
  st.markdown("---")
1278
  # AI Prompt (RDKit)
@@ -1498,8 +1511,62 @@ def _poseview_ui(
1498
 
1499
  if _pose_svg and not _stale:
1500
  _lbl = st.session_state.get(smiles_key, "ligand")[:20]
1501
- col_l, col_r = st.columns(2)
1502
- with col_l:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1503
  st.markdown("##### 🧪 Docked Pose (PoseView v1)")
1504
  _png_data = st.session_state.get(img_png_key)
1505
  _show_poseview_image(
@@ -1522,41 +1589,7 @@ def _poseview_ui(
1522
  file_name=f"pose{pose_idx+1}_poseview.svg",
1523
  mime="image/svg+xml", key=dl_svg_key + "_pv", width='stretch',
1524
  )
1525
-
1526
- with col_r:
1527
- st.markdown("##### 🔬 Co-Crystal Reference (PoseView2)")
1528
- if _ref_svg2:
1529
- _ref_png2 = st.session_state.get(ref_png_key) if ref_png_key else None
1530
- _show_poseview_image(
1531
- _ref_png2, _ref_svg2,
1532
- f"Co-crystal: {pdb_id.upper()} · {cocrystal_ligand_id}",
1533
- full_legend=True,
1534
- stamp=f"{pdb_id.upper()} · {cocrystal_ligand_id}",
1535
- )
1536
- _r1, _r2 = st.columns(2)
1537
- with _r1:
1538
- if _ref_png2:
1539
- st.download_button(
1540
- "⬇ PNG", data=_ref_png2,
1541
- file_name=f"cocrystal_{pdb_id}_{cocrystal_ligand_id}.png",
1542
- mime="image/png",
1543
- key=dl_png_key + "_pv_ref",
1544
- width='stretch',
1545
- )
1546
- with _r2:
1547
- st.download_button(
1548
- "⬇ SVG", data=_ref_svg2,
1549
- file_name=f"cocrystal_{pdb_id}_{cocrystal_ligand_id}.svg",
1550
- mime="image/svg+xml",
1551
- key=dl_svg_key + "_pv_ref",
1552
- width='stretch',
1553
- )
1554
- elif _has_ref:
1555
- st.info("Click **Generate 2D Diagrams** to load the co-crystal reference.")
1556
- else:
1557
- st.caption(
1558
- "⚠️ No co-crystal ligand ID — use Auto-detect in receptor preparation."
1559
- )
1560
 
1561
 
1562
  def _receptor_section(pfx: str, wdir: Path, step_label: str):
@@ -3338,4 +3371,4 @@ st.markdown(
3338
  'DFDD &#8212; Hengphasatporn et al. J. Chem. Inf. Model. 2026</a>'
3339
  '</div>',
3340
  unsafe_allow_html=True,
3341
- )
 
962
  "View mode", ["🖱 Interactive (drag)", "🖼 Static SVG"],
963
  horizontal=True, key=btn_key + "_viewmode",
964
  )
965
+ if _has_ref_local:
966
+ _cl2, _cr2 = st.columns(2)
967
+ with _cl2:
968
+ st.markdown("##### Docked Pose")
969
+ if _view_mode.startswith("🖱") and _new_ihtml:
970
+ components.html(_new_ihtml, height=860, scrolling=False)
971
+ else:
972
+ _show_svg_new(_new_svg, f"pose{pose_idx+1}_interaction")
973
+ with _cr2:
974
+ st.markdown("##### Co-Crystal Reference")
975
+ if _new_ref_svg:
976
+ if _view_mode.startswith("🖱") and _new_ref_ihtml:
977
+ components.html(_new_ref_ihtml, height=860, scrolling=False)
978
+ else:
979
+ _show_svg_new(_new_ref_svg, "cocrystal_interaction")
980
+ else:
981
+ st.info("Click Generate to produce the co-crystal diagram.")
982
+ else:
983
  st.markdown("##### Docked Pose")
984
  if _view_mode.startswith("🖱") and _new_ihtml:
985
  components.html(_new_ihtml, height=860, scrolling=False)
986
  else:
987
  _show_svg_new(_new_svg, f"pose{pose_idx+1}_interaction")
988
+ st.caption("ℹ️ No co-crystal ligand detected — co-crystal reference diagram is not available.")
 
 
 
 
 
 
 
 
 
 
989
 
990
 
991
  st.markdown("---")
 
1259
  )
1260
 
1261
  if _rdk_svg and not _rdk_stale:
1262
+ if _has_ref_rdkit2:
1263
+ _col_l2, _col_r2 = st.columns(2)
1264
+ with _col_l2:
1265
+ st.markdown("##### 🧪 Docked Pose (RDKit)")
1266
+ _show_rdkit_svg_tab(
1267
+ _rdk_svg,
1268
+ dl_key_prefix=btn_key + "_rdk_dl",
1269
+ dl_filename=f"pose{pose_idx+1}_rdkit.svg",
1270
+ )
1271
+ with _col_r2:
1272
+ st.markdown("##### 🔮 Co-Crystal Reference (RDKit)")
1273
+ if _ref_rdk_svg:
1274
+ _show_rdkit_svg_tab(
1275
+ _ref_rdk_svg,
1276
+ dl_key_prefix=btn_key + "_rdk_ref_dl",
1277
+ dl_filename="cocrystal_rdkit.svg",
1278
+ )
1279
+ else:
1280
+ st.info("Click **Generate RDKit Diagrams** to generate co-crystal diagram.")
1281
+ else:
1282
  st.markdown("##### 🧪 Docked Pose (RDKit)")
1283
  _show_rdkit_svg_tab(
1284
  _rdk_svg,
1285
  dl_key_prefix=btn_key + "_rdk_dl",
1286
  dl_filename=f"pose{pose_idx+1}_rdkit.svg",
1287
  )
1288
+ st.caption("ℹ️ No co-crystal ligand detected — co-crystal reference diagram is not available.")
 
 
 
 
 
 
 
 
 
 
 
1289
 
1290
  st.markdown("---")
1291
  # AI Prompt (RDKit)
 
1511
 
1512
  if _pose_svg and not _stale:
1513
  _lbl = st.session_state.get(smiles_key, "ligand")[:20]
1514
+ if _has_ref:
1515
+ col_l, col_r = st.columns(2)
1516
+ with col_l:
1517
+ st.markdown("##### 🧪 Docked Pose (PoseView v1)")
1518
+ _png_data = st.session_state.get(img_png_key)
1519
+ _show_poseview_image(
1520
+ _png_data, _pose_svg,
1521
+ f"Docked pose {pose_idx+1} — {_lbl}",
1522
+ full_legend=False,
1523
+ stamp=f"Pose {pose_idx+1} · {_lbl}",
1524
+ )
1525
+ _d1, _d2 = st.columns(2)
1526
+ with _d1:
1527
+ if _png_data:
1528
+ st.download_button(
1529
+ "⬇ PNG", data=_png_data,
1530
+ file_name=f"pose{pose_idx+1}_poseview.png",
1531
+ mime="image/png", key=dl_png_key + "_pv", width='stretch',
1532
+ )
1533
+ with _d2:
1534
+ st.download_button(
1535
+ "⬇ SVG", data=_pose_svg,
1536
+ file_name=f"pose{pose_idx+1}_poseview.svg",
1537
+ mime="image/svg+xml", key=dl_svg_key + "_pv", width='stretch',
1538
+ )
1539
+ with col_r:
1540
+ st.markdown("##### 🔬 Co-Crystal Reference (PoseView2)")
1541
+ if _ref_svg2:
1542
+ _ref_png2 = st.session_state.get(ref_png_key) if ref_png_key else None
1543
+ _show_poseview_image(
1544
+ _ref_png2, _ref_svg2,
1545
+ f"Co-crystal: {pdb_id.upper()} · {cocrystal_ligand_id}",
1546
+ full_legend=True,
1547
+ stamp=f"{pdb_id.upper()} · {cocrystal_ligand_id}",
1548
+ )
1549
+ _r1, _r2 = st.columns(2)
1550
+ with _r1:
1551
+ if _ref_png2:
1552
+ st.download_button(
1553
+ "⬇ PNG", data=_ref_png2,
1554
+ file_name=f"cocrystal_{pdb_id}_{cocrystal_ligand_id}.png",
1555
+ mime="image/png",
1556
+ key=dl_png_key + "_pv_ref",
1557
+ width='stretch',
1558
+ )
1559
+ with _r2:
1560
+ st.download_button(
1561
+ "⬇ SVG", data=_ref_svg2,
1562
+ file_name=f"cocrystal_{pdb_id}_{cocrystal_ligand_id}.svg",
1563
+ mime="image/svg+xml",
1564
+ key=dl_svg_key + "_pv_ref",
1565
+ width='stretch',
1566
+ )
1567
+ else:
1568
+ st.info("Click **Generate 2D Diagrams** to load the co-crystal reference.")
1569
+ else:
1570
  st.markdown("##### 🧪 Docked Pose (PoseView v1)")
1571
  _png_data = st.session_state.get(img_png_key)
1572
  _show_poseview_image(
 
1589
  file_name=f"pose{pose_idx+1}_poseview.svg",
1590
  mime="image/svg+xml", key=dl_svg_key + "_pv", width='stretch',
1591
  )
1592
+ st.caption("ℹ️ No co-crystal ligand detected — co-crystal reference diagram is not available.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1593
 
1594
 
1595
  def _receptor_section(pfx: str, wdir: Path, step_label: str):
 
3371
  'DFDD &#8212; Hengphasatporn et al. J. Chem. Inf. Model. 2026</a>'
3372
  '</div>',
3373
  unsafe_allow_html=True,
3374
+ )
core.py CHANGED
@@ -545,24 +545,28 @@ def prepare_receptor(
545
  )
546
  log.append(f"📍 Center: ({cx:.3f}, {cy:.3f}, {cz:.3f})")
547
 
548
- _resnames = list(dict.fromkeys(ref_atoms.getResnames()))
549
- _resids = list(dict.fromkeys(ref_atoms.getResnums()))
550
- _chains = list(dict.fromkeys(ref_atoms.getChids()))
551
-
552
- if len(_resnames) == 1 and len(_resids) == 1:
553
- rn = _resnames[0]
554
- ri = int(_resids[0])
555
- ch = _chains[0] if _chains else "A"
556
- ligand_sel_str = f"resname {rn} and resid {ri} and chain {ch}"
557
- cocrystal_ligand_id = f"{rn}_{ch}_{ri}"
558
  ligand_pdb_path = str(wdir / "LIG.pdb")
559
- writePDB(ligand_pdb_path, ref_atoms)
560
- log.append(f"✓ Ligand: {rn} chain {ch} resnum {ri}")
 
 
 
561
  log.append(f"🔑 PoseView2 ligand ID: {cocrystal_ligand_id}")
562
  else:
563
- ligand_pdb_path = str(wdir / "LIG_ref.pdb")
564
- writePDB(ligand_pdb_path, ref_atoms)
565
- log.append("⚠ Multi-residue selection — PoseView2 ligand ID not set")
 
566
 
567
  # Write receptor PDB without co-crystal ligand
568
  sel_str = (
@@ -3894,4 +3898,4 @@ def _svg_stamp(svg_text:str,title:str,w:int,h:int)->str:
3894
  f'<text x="{w//2}" y="{ty}" text-anchor="middle" dominant-baseline="middle"'
3895
  f' font-family="Arial,sans-serif" font-size="13" font-weight="500" fill="#1A1A1A">'
3896
  f'{esc}</text></g>')
3897
- return svg_text.replace("</svg>",f"{st}</svg>")
 
545
  )
546
  log.append(f"📍 Center: ({cx:.3f}, {cy:.3f}, {cz:.3f})")
547
 
548
+ # The ProDy selection positions the docking box only.
549
+ # Co-crystal ligand detection, receptor stripping, ligand PDB
550
+ # saving, and PoseView2 ID are handled identically to 'auto' mode
551
+ # so that RMSD calculation, 3D viewer, and 2D interaction diagrams
552
+ # all work correctly regardless of what the user selected.
553
+ info = detect_cocrystal_ligand(raw_pdb)
554
+ if info["found"]:
555
+ rn, ch, ri = info["resname"], info["chain"], info["resid"]
556
+ ligand_sel_str = info["sel_str"]
557
+ cocrystal_ligand_id = info["ligand_id"]
558
  ligand_pdb_path = str(wdir / "LIG.pdb")
559
+ writePDB(ligand_pdb_path, info["atoms"])
560
+ log.append(
561
+ f"✓ Co-crystal ligand: {rn} chain {ch} resnum {ri} "
562
+ f"({info['n_atoms']} atoms)"
563
+ )
564
  log.append(f"🔑 PoseView2 ligand ID: {cocrystal_ligand_id}")
565
  else:
566
+ log.append(
567
+ "⚠ No co-crystal ligand found — receptor keeps all "
568
+ "HETATM; RMSD and co-crystal diagrams will be skipped"
569
+ )
570
 
571
  # Write receptor PDB without co-crystal ligand
572
  sel_str = (
 
3898
  f'<text x="{w//2}" y="{ty}" text-anchor="middle" dominant-baseline="middle"'
3899
  f' font-family="Arial,sans-serif" font-size="13" font-weight="500" fill="#1A1A1A">'
3900
  f'{esc}</text></g>')
3901
+ return svg_text.replace("</svg>",f"{st}</svg>")