#!/usr/bin/env python3 """ app.py — Streamlit UI layer for Anyone Can Dock. All computation is delegated to core.py — this file contains only layout, widgets, session state, and 3D/2D visualisation. """ import io import os import tempfile import zipfile import re as _re from pathlib import Path import matplotlib.pyplot as plt import pandas as pd import streamlit as st import streamlit.components.v1 as components from core import ( prepare_receptor, prepare_ligand, prepare_ligand_from_file, smiles_from_file, run_vina, get_vina_binary, check_obabel, fix_sdf_bond_orders, load_mols_from_sdf, write_single_pose, get_interacting_residues, calc_rmsd_heavy, call_poseview_v1, svg_to_png, stamp_png, is_cif_file, convert_cif_to_pdb, ) try: from core import call_poseview2_ref except ImportError: def call_poseview2_ref(pdb_code, ligand_id): return None, "call_poseview2_ref not available — please update core.py" try: from core import warm_poseview_cache, clear_poseview_cache except ImportError: def warm_poseview_cache(path): return False, "core.py not updated yet" def clear_poseview_cache(): pass try: from core import ( draw_interactions_rdkit, draw_interaction_diagram, draw_interactions_rdkit_classic, draw_interaction_diagram_data, ) except ImportError: draw_interactions_rdkit = None draw_interaction_diagram = None draw_interactions_rdkit_classic = None try: from core import COFACTOR_NAMES as _COFACTOR_NAMES except ImportError: _COFACTOR_NAMES = { "ATP", "ADP", "AMP", "GTP", "GDP", "GMP", "NAD", "NAP", "NDP", "FAD", "FMN", "GOL", "PEG", "EDO", "MPD", "PGE", "PG4", "SO4", "PO4", "SUL", "PHO", "IHP", "TTP", "CTP", "UTP", "COA", "SAM", "SAH", "EPE", "MES", "TRS", "ACT", "ACY", } try: from core import HEME_RESNAMES as _HEME_RESNAMES except ImportError: _HEME_RESNAMES = {"HEM", "HEC", "HEA", "HEB", "HDD", "HDM"} # ══════════════════════════════════════════════════════════════════════════════ # PAGE CONFIG # ══════════════════════════════════════════════════════════════════════════════ st.set_page_config( page_title="anyone can dock", page_icon="https://raw.githubusercontent.com/nyelidl/anyone-docking/main/any-L.svg", layout="wide", initial_sidebar_state="collapsed", ) # ══════════════════════════════════════════════════════════════════════════════ # THEME + COLOUR HELPERS # ══════════════════════════════════════════════════════════════════════════════ import json as _json def _render_interactive_diagram(data: dict, height: int = 800) -> str: W = data["W"] H = data["H"] title = data["title"] lig_svg = data["ligand_svg"] placements = data["placements"] title_esc = title.replace("\\", "\\\\").replace('"', '\\"').replace("'", "\\'") tw = min(len(title) * 14 + 48, W - 40) pill_x = (W - tw) / 2 TYPE_CFG = { "hbond": {"fill": "#80dd80", "stroke": "#1a7a1a", "lineclr": "#1a7a1a", "dash": "5 3", "lw": "1.6"}, "hbond_to_halogen": {"fill": "#c4a0ff", "stroke": "#6633aa", "lineclr": "#6633aa", "dash": "4 2 1 2", "lw": "1.6"}, "pi_pi": {"fill": "#f0a0ff", "stroke": "#e200e8", "lineclr": "#e200e8", "dash": "5 3", "lw": "1.6"}, "cation_pi": {"fill": "#f0a0ff", "stroke": "#e200e8", "lineclr": "#e200e8", "dash": "5 3", "lw": "1.6"}, "hydrophobic": {"fill": "#a0c8ff", "stroke": "#2287ff", "lineclr": None, "dash": "", "lw": "0"}, "ionic": {"fill": "#ffb0d0", "stroke": "#cc2277", "lineclr": "#cc2277", "dash": "6 2 2 2", "lw": "1.8"}, "metal": {"fill": "#ffe090", "stroke": "#cc8800", "lineclr": "#cc8800", "dash": "3 2", "lw": "1.8"}, "halogen": {"fill": "#ffb0d0", "stroke": "#cc2277", "lineclr": "#cc2277", "dash": "5 2", "lw": "1.6"}, } active_types = list(dict.fromkeys(p["itype"] for p in placements)) LEG_LABEL = { "hbond": "H-bond", "hbond_to_halogen": "H···Halogen", "pi_pi": "π-π", "cation_pi": "Cation-π", "hydrophobic": "Hydrophobic", "ionic": "Ionic", "metal": "Metal", "halogen": "Halogen", } legend_items = [] for it in active_types: cfg = TYPE_CFG.get(it, TYPE_CFG["hbond"]) legend_items.append({ "label": LEG_LABEL.get(it, it), "fill": cfg["fill"], "stroke": cfg["stroke"], "lineclr": cfg["lineclr"], "dash": cfg["dash"], }) LEG_Y = H - 45 leg_entry_w = 115 leg_total = len(legend_items) * leg_entry_w leg_x0 = (W - leg_total) / 2 leg_parts = [ f'' ] for k, li in enumerate(legend_items): ix = leg_x0 + k * leg_entry_w + 14 leg_parts.append( f'' ) if li["lineclr"]: leg_parts.append( f'' ) leg_parts.append( f'{li["label"]}' ) else: leg_parts.append( f'{li["label"]}' ) legend_svg = "\n".join(leg_parts) placements_json = _json.dumps(placements) type_cfg_json = _json.dumps(TYPE_CFG) html = f"""
{caption}