Spaces:
Running
Running
Update qsar_core_streamlit.py
Browse files251 unmodified lines252253254255256257258259260261262263264265356 unmodified lines6226236246256266276286292 unmodified lines6326336346356366376386396406415 unmodified lines6476486496506516526535 unmodified lines6596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507511 unmodified line75375475575675775875976038 unmodified lines799800801802803804251 unmodified linesdef clean_receptor_pdb_for_meeko(infile: Path, outfile: Path) -> None:"""Conservative receptor cleaning:- keep ATOM records only- remove waters- keep only blank or A altloc- infer missing element symbols- skip unresolved atoms"""kept = 0with open(infile, "r", errors="ignore") as fin, open(outfile, "w") as fout:356 unmodified linesdef init_state() -> None:st.session_state.setdefault("protein_name", None)st.session_state.setdefault("ligand_name", None)st.session_state.setdefault("run_outputs", None)2 unmodified lines"protein_uploader","ligand_uploader","smiles_text","protein_name","ligand_name","run_outputs","residue_selection_text",]:if key in st.session_state:del st.session_state[key]5 unmodified linesinit_state()st.title("Protein-Ligand Docking Tool")st.caption("Upload a protein, upload a ligand or paste a SMILES string, then run blind, residue-based, or manual docking.")with st.sidebar:st.header("Run settings")5 unmodified linesmissing_cmds = [cmd for cmd in ["vina", "obabel", "mk_prepare_receptor.py"] if not ensure_command(cmd)]if missing_cmds:st.error("Missing container tools: " + ", ".join(missing_cmds) +". Fix the Dockerfile first, rebuild the Space, then test upload again.")col1, col2 = st.columns(2)with col1:st.subheader("Upload input files")protein_upload = st.file_uploader("Upload protein structure (PDB or PDBQT)",type=["pdb", "pdbqt"],accept_multiple_files=False,key="protein_uploader",help="Upload a receptor file in PDB or PDBQT format.",)if protein_upload is not None:validate_extension(protein_upload.name, SUPPORTED_PROTEIN_EXTS, "Protein file")st.session_state["protein_name"] = protein_upload.namest.success(f"Protein uploaded: {protein_upload.name} ({protein_upload.size} bytes)")elif st.session_state.get("protein_name"):st.info(f"Protein loaded in session: {st.session_state['protein_name']}")ligand_upload = st.file_uploader("Upload ligand (SDF, MOL, MOL2, PDB, or PDBQT)",type=["sdf", "mol", "mol2", "pdb", "pdbqt"],accept_multiple_files=False,key="ligand_uploader",help="Upload a ligand file in SDF, MOL, MOL2, PDB, or PDBQT format.",)if ligand_upload is not None:validate_extension(ligand_upload.name, SUPPORTED_LIGAND_EXTS, "Ligand file")st.session_state["ligand_name"] = ligand_upload.namest.success(f"Ligand uploaded: {ligand_upload.name} ({ligand_upload.size} bytes)")elif st.session_state.get("ligand_name"):st.info(f"Ligand loaded in session: {st.session_state['ligand_name']}")smiles_text = st.text_area("Or paste ligand SMILES",placeholder="CC(=O)Oc1ccccc1C(=O)O",key="smiles_text",)with col2:st.subheader("Docking mode and box definition")docking_mode = st.radio("Choose docking mode",["Residue-based docking", "Blind docking", "Manual box coordinates"],index=0,)residue_selection_text = ""padding = 8.0center_x = center_y = center_z = 0.0size_x = size_y = size_z = 20.0if docking_mode == "Residue-based docking":st.markdown("Specify residues using chain and residue identifiers.")st.code("Examples:\nA:LYS45,ASP46,GLU47\nA:45,46,47\nA:45-50\nA:LYS45;B:TYR120",language="text",)residue_selection_text = st.text_area("Residues and chains",placeholder="A:LYS45,ASP46,GLU47",key="residue_selection_text",)padding = st.number_input("Padding around selected residues (Å)", min_value=2.0, value=8.0, step=1.0)elif docking_mode == "Blind docking":st.info("The docking box will be generated automatically from the whole protein.")padding = st.number_input("Blind docking padding (Å)", min_value=2.0, value=8.0, step=1.0)else:c1, c2, c3 = st.columns(3)center_x = c1.number_input("Center X", value=0.0, format="%.3f")center_y = c2.number_input("Center Y", value=0.0, format="%.3f")center_z = c3.number_input("Center Z", value=0.0, format="%.3f")s1, s2, s3 = st.columns(3)size_x = s1.number_input("Size X", value=20.0, min_value=5.0, format="%.3f")size_y = s2.number_input("Size Y", value=20.0, min_value=5.0, format="%.3f")size_z = s3.number_input("Size Z", value=20.0, min_value=5.0, format="%.3f")st.info("Manual docking uses explicit center coordinates and box sizes.")run_clicked = st.button("Run docking", type="primary", disabled=bool(missing_cmds))1 unmodified linetry:if protein_upload is None:raise ValueError("Upload a protein file before running docking.")if ligand_upload is None and not smiles_text.strip():raise ValueError("Upload a ligand file or paste a SMILES string before running docking.")with st.spinner("Running docking..."):with tempfile.TemporaryDirectory(prefix="dock_ui_") as ui_tmp:38 unmodified lines"mode": docking_mode,"matched_residues": residue_match_df.to_dict(orient="records") if residue_match_df is not None else None,}st.success("Docking completed successfully.")except Exception as e:251 unmodified lines252253254255256257356 unmodified lines6146156166176186192 unmodified lines6226236246256266276286295 unmodified lines6356366376386396406415 unmodified lines6476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387391 unmodified line74174274374474574674774874975075175238 unmodified lines791792793794795796797251 unmodified linesdef clean_receptor_pdb_for_meeko(infile: Path, outfile: Path) -> None:kept = 0with open(infile, "r", errors="ignore") as fin, open(outfile, "w") as fout:356 unmodified linesdef init_state() -> None:st.session_state.setdefault("run_outputs", None)2 unmodified lines"protein_uploader","ligand_uploader","smiles_text","residue_selection_text","run_outputs",]:if key in st.session_state:del st.session_state[key]5 unmodified linesinit_state()st.title("Protein-Ligand Docking Tool")st.caption("Upload a protein and ligand, then run blind, residue-based, or manual docking.")with st.sidebar:st.header("Run settings")5 unmodified linesmissing_cmds = [cmd for cmd in ["vina", "obabel", "mk_prepare_receptor.py"] if not ensure_command(cmd)]if missing_cmds:st.warning("Missing container tools: " + ", ".join(missing_cmds) +". Upload may appear unstable until the Docker image is fixed.")st.subheader("Upload input files")protein_upload = st.file_uploader("Upload protein structure (PDB or PDBQT)",type=["pdb", "pdbqt"],accept_multiple_files=False,key="protein_uploader",help="Upload a receptor file in PDB or PDBQT format.",)ligand_upload = st.file_uploader("Upload ligand (SDF, MOL, MOL2, PDB, or PDBQT)",type=["sdf", "mol", "mol2", "pdb", "pdbqt"],accept_multiple_files=False,key="ligand_uploader",help="Upload a ligand file in SDF, MOL, MOL2, PDB, or PDBQT format.",)smiles_text = st.text_area("Or paste ligand SMILES",placeholder="CC(=O)Oc1ccccc1C(=O)O",key="smiles_text",)if protein_upload is not None:try:validate_extension(protein_upload.name, SUPPORTED_PROTEIN_EXTS, "Protein file")st.success(f"Protein uploaded successfully: {protein_upload.name}")except Exception as e:st.error(str(e))else:st.info("No protein uploaded yet.")if ligand_upload is not None:try:validate_extension(ligand_upload.name, SUPPORTED_LIGAND_EXTS, "Ligand file")st.success(f"Ligand uploaded successfully: {ligand_upload.name}")except Exception as e:st.error(str(e))elif smiles_text.strip():st.success("Ligand SMILES provided.")else:st.info("No ligand uploaded yet.")st.subheader("Docking mode and box definition")docking_mode = st.radio("Choose docking mode",["Residue-based docking", "Blind docking", "Manual box coordinates"],index=0,)residue_selection_text = ""padding = 8.0center_x = center_y = center_z = 0.0size_x = size_y = size_z = 20.0if docking_mode == "Residue-based docking":st.code("Examples:\nA:LYS45,ASP46,GLU47\nA:45,46,47\nA:45-50\nA:LYS45;B:TYR120",language="text",)residue_selection_text = st.text_area("Residues and chains",placeholder="A:LYS45,ASP46,GLU47",key="residue_selection_text",)padding = st.number_input("Padding around selected residues (Å)", min_value=2.0, value=8.0, step=1.0)elif docking_mode == "Blind docking":padding = st.number_input("Blind docking padding (Å)", min_value=2.0, value=8.0, step=1.0)else:c1, c2, c3 = st.columns(3)center_x = c1.number_input("Center X", value=0.0, format="%.3f")center_y = c2.number_input("Center Y", value=0.0, format="%.3f")center_z = c3.number_input("Center Z", value=0.0, format="%.3f")s1, s2, s3 = st.columns(3)size_x = s1.number_input("Size X", value=20.0, min_value=5.0, format="%.3f")size_y = s2.number_input("Size Y", value=20.0, min_value=5.0, format="%.3f")size_z = s3.number_input("Size Z", value=20.0, min_value=5.0, format="%.3f")run_clicked = st.button("Run docking", type="primary", disabled=bool(missing_cmds))1 unmodified linetry:if protein_upload is None:raise ValueError("Upload a protein file before running docking.")validate_extension(protein_upload.name, SUPPORTED_PROTEIN_EXTS, "Protein file")if ligand_upload is None and not smiles_text.strip():raise ValueError("Upload a ligand file or paste a SMILES string before running docking.")if ligand_upload is not None:validate_extension(ligand_upload.name, SUPPORTED_LIGAND_EXTS, "Ligand file")with st.spinner("Running docking..."):with tempfile.TemporaryDirectory(prefix="dock_ui_") as ui_tmp:38 unmodified lines"mode": docking_mode,"matched_residues": residue_match_df.to_dict(orient="records") if residue_match_df is not None else None,}st.success("Docking completed successfully.")except Exception as e:
qsar_core_streamlit.py+80-87