{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "1f8ea359-674c-4263-9c2a-7a8e7e464249", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7862\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "import requests\n", "from Bio.PDB import PDBParser\n", "from gradio_molecule3d import Molecule3D\n", "import numpy as np\n", "\n", "# Function to fetch a PDB file from RCSB PDB\n", "def fetch_pdb(pdb_id):\n", " pdb_url = f'https://files.rcsb.org/download/{pdb_id}.pdb'\n", " pdb_path = f'{pdb_id}.pdb'\n", " response = requests.get(pdb_url)\n", " if response.status_code == 200:\n", " with open(pdb_path, 'wb') as f:\n", " f.write(response.content)\n", " return pdb_path\n", " else:\n", " return None\n", "\n", "# Function to process the PDB file and return random predictions\n", "def process_pdb(pdb_id, segment):\n", " pdb_path = fetch_pdb(pdb_id)\n", " if not pdb_path:\n", " return \"Failed to fetch PDB file\", None, None\n", "\n", " parser = PDBParser(QUIET=True)\n", " structure = parser.get_structure('protein', pdb_path)\n", " \n", " try:\n", " chain = structure[0][segment]\n", " except KeyError:\n", " return \"Invalid Chain ID\", None, None\n", "\n", " sequence = [residue.get_resname() for residue in chain if residue.id[0] == ' ']\n", " random_scores = np.random.rand(len(sequence))\n", "\n", " result_str = \"\\n\".join(\n", " f\"{seq} {res.id[1]} {score:.2f}\" \n", " for seq, res, score in zip(sequence, chain, random_scores)\n", " )\n", "\n", " # Save the predictions to a file\n", " prediction_file = f\"{pdb_id}_predictions.txt\"\n", " with open(prediction_file, \"w\") as f:\n", " f.write(result_str)\n", " \n", " return result_str, pdb_path, prediction_file\n", "\n", "#reps = [{\"model\": 0, \"style\": \"cartoon\", \"color\": \"spectrum\"}]\n", "\n", "reps = [\n", " {\n", " \"model\": 0,\n", " \"style\": \"cartoon\",\n", " \"color\": \"whiteCarbon\",\n", " \"residue_range\": \"\",\n", " \"around\": 0,\n", " \"byres\": False,\n", " },\n", " {\n", " \"model\": 0,\n", " \"chain\": \"A\",\n", " \"resname\": \"HIS\",\n", " \"style\": \"stick\",\n", " \"color\": \"red\"\n", " }\n", " ]\n", "\n", "\n", "# Gradio UI\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Protein Binding Site Prediction (Random Scores)\")\n", "\n", " with gr.Row():\n", " pdb_input = gr.Textbox(value=\"2IWI\", label=\"PDB ID\", placeholder=\"Enter PDB ID here...\")\n", " segment_input = gr.Textbox(value=\"A\", label=\"Chain ID\", placeholder=\"Enter Chain ID here...\")\n", " visualize_btn = gr.Button(\"Visualize Structure\")\n", " prediction_btn = gr.Button(\"Predict Random Binding Site Scores\")\n", "\n", " molecule_output = Molecule3D(label=\"Protein Structure\", reps=reps)\n", " predictions_output = gr.Textbox(label=\"Binding Site Predictions\")\n", " download_output = gr.File(label=\"Download Predictions\")\n", "\n", " visualize_btn.click(fetch_pdb, inputs=[pdb_input], outputs=molecule_output)\n", " prediction_btn.click(process_pdb, inputs=[pdb_input, segment_input], outputs=[predictions_output, molecule_output, download_output])\n", "\n", " gr.Markdown(\"## Examples\")\n", " gr.Examples(\n", " examples=[\n", " [\"2IWI\", \"A\"],\n", " [\"7RPZ\", \"B\"],\n", " [\"3TJN\", \"C\"]\n", " ],\n", " inputs=[pdb_input, segment_input],\n", " outputs=[predictions_output, molecule_output, download_output]\n", " )\n", "\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "bd50ff2e-ed03-498e-8af2-73c0fb8ea07e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "raw", "id": "88affe12-7c48-4bd6-9e46-32cdffa729fe", "metadata": {}, "source": [ "import gradio as gr\n", "from gradio_molecule3d import Molecule3D\n", "\n", "\n", "example = Molecule3D().example_value()\n", "\n", "\n", "reps = [\n", " {\n", " \"model\": 0,\n", " \"style\": \"cartoon\",\n", " \"color\": \"whiteCarbon\",\n", " \"residue_range\": \"\",\n", " \"around\": 0,\n", " \"byres\": False,\n", " },\n", " {\n", " \"model\": 0,\n", " \"chain\": \"A\",\n", " \"resname\": \"HIS\",\n", " \"style\": \"stick\",\n", " \"color\": \"red\"\n", " }\n", " ]\n", "\n", "\n", "\n", "def predict(x):\n", " print(\"predict function\", x)\n", " print(x.name)\n", " return x\n", "\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Molecule3D\")\n", " inp = Molecule3D(label=\"Molecule3D\", reps=reps)\n", " out = Molecule3D(label=\"Output\", reps=reps)\n", "\n", " btn = gr.Button(\"Predict\")\n", " gr.Markdown(\"\"\" \n", " You can configure the default rendering of the molecule by adding a list of representations\n", "
\n",
    "        reps =    [\n",
    "        {\n",
    "          \"model\": 0,\n",
    "          \"style\": \"cartoon\",\n",
    "          \"color\": \"whiteCarbon\",\n",
    "          \"residue_range\": \"\",\n",
    "          \"around\": 0,\n",
    "          \"byres\": False,\n",
    "        },\n",
    "        {\n",
    "          \"model\": 0,\n",
    "          \"chain\": \"A\",\n",
    "          \"resname\": \"HIS\",\n",
    "          \"style\": \"stick\",\n",
    "          \"color\": \"red\"\n",
    "        }\n",
    "      ]\n",
    "    
\n", " \"\"\")\n", " btn.click(predict, inputs=inp, outputs=out)\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "d27cc368-26a0-42c2-a68a-8833de7bb4a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "raw", "id": "2b970adb-3152-427f-bb58-b92974ff406e", "metadata": {}, "source": [ "import gradio as gr\n", "import os\n", "import requests\n", "from Bio.PDB import PDBParser, PDBIO\n", "import biotite.structure.io as bsio\n", "\n", "def read_mol(pdb_path):\n", " \"\"\"Read PDB file and return its content as a string\"\"\"\n", " with open(pdb_path, 'r') as f:\n", " return f.read()\n", "\n", "# Function to fetch or upload the PDB file\n", "def get_pdb(pdb_code=\"\", filepath=\"\"):\n", " if pdb_code and len(pdb_code) == 4:\n", " pdb_file = f\"{pdb_code}.pdb\"\n", " if not os.path.exists(pdb_file):\n", " os.system(f\"wget -qnc https://files.rcsb.org/view/{pdb_code}.pdb\")\n", " return pdb_file\n", " elif filepath is not None:\n", " return filepath\n", " else:\n", " return None\n", "\n", "def molecule(input_pdb):\n", " mol = read_mol(input_pdb) # Read PDB file content\n", " \n", " html_content = f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \"\"\"\n", " \n", " # Return the HTML content within an iframe safely encoded for special characters\n", " return f''\n", "\n", "# Gradio function to update the visualization\n", "def update(inp, file):\n", " pdb_path = get_pdb(inp, file)\n", " if pdb_path:\n", " return molecule(pdb_path)\n", " else:\n", " return \"Invalid input. Please provide a valid PDB code or upload a PDB file.\"\n", "\n", "# Gradio UI\n", "demo = gr.Blocks()\n", "with demo:\n", " gr.Markdown(\"# PDB Viewer using 3Dmol.js\")\n", " with gr.Row():\n", " with gr.Column():\n", " inp = gr.Textbox(\n", " placeholder=\"PDB Code or upload file below\", label=\"Input structure\"\n", " )\n", " file = gr.File(file_count=\"single\")\n", " btn = gr.Button(\"View structure\")\n", " mol = gr.HTML()\n", " btn.click(fn=update, inputs=[inp, file], outputs=mol)\n", "\n", "# Launch the Gradio interface \n", "demo.launch(debug=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "ee215c16-a1fb-450f-bb93-37aaee6fb3f1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "raw", "id": "050aa2e8-2dbe-4a28-8692-58ca7c50fccd", "metadata": {}, "source": [ "import gradio as gr\n", "import os\n", "import requests\n", "import numpy as np\n", "from Bio.PDB import PDBParser\n", "\n", "def read_mol(pdb_path):\n", " \"\"\"Read PDB file and return its content as a string\"\"\"\n", " with open(pdb_path, 'r') as f:\n", " return f.read()\n", "\n", "# Function to fetch a PDB file from RCSB PDB\n", "def fetch_pdb(pdb_id):\n", " pdb_url = f'https://files.rcsb.org/download/{pdb_id}.pdb'\n", " pdb_path = f'{pdb_id}.pdb'\n", " response = requests.get(pdb_url)\n", " if response.status_code == 200:\n", " with open(pdb_path, 'wb') as f:\n", " f.write(response.content)\n", " return molecule(pdb_path)\n", " else:\n", " return None\n", "\n", "# Function to process the PDB file and return random predictions\n", "def process_pdb(pdb_id, segment):\n", " pdb_path = fetch_pdb(pdb_id)\n", " if not pdb_path:\n", " return \"Failed to fetch PDB file\", None, None\n", " \n", " parser = PDBParser(QUIET=True)\n", " structure = parser.get_structure('protein', pdb_path)\n", " \n", " try:\n", " chain = structure[0][segment]\n", " except KeyError:\n", " return \"Invalid Chain ID\", None, None\n", " \n", " sequence = [residue.get_resname() for residue in chain if residue.id[0] == ' ']\n", " random_scores = np.random.rand(len(sequence))\n", " result_str = \"\\n\".join(\n", " f\"{seq} {res.id[1]} {score:.2f}\" \n", " for seq, res, score in zip(sequence, chain, random_scores)\n", " )\n", " \n", " # Save the predictions to a file\n", " prediction_file = f\"{pdb_id}_predictions.txt\"\n", " with open(prediction_file, \"w\") as f:\n", " f.write(result_str)\n", " \n", " return result_str, molecule(pdb_path), prediction_file\n", "\n", "def molecule(input_pdb):\n", " mol = read_mol(input_pdb) # Read PDB file content\n", " \n", " html_content = f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \"\"\"\n", " \n", " # Return the HTML content within an iframe safely encoded for special characters\n", " return f''\n", "\n", "# Gradio UI\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Protein Binding Site Prediction (Random Scores)\")\n", " with gr.Row():\n", " pdb_input = gr.Textbox(value=\"2IWI\", label=\"PDB ID\", placeholder=\"Enter PDB ID here...\")\n", " segment_input = gr.Textbox(value=\"A\", label=\"Chain ID\", placeholder=\"Enter Chain ID here...\")\n", " visualize_btn = gr.Button(\"Visualize Structure\")\n", " prediction_btn = gr.Button(\"Predict Random Binding Site Scores\")\n", " \n", " # Use HTML output instead of Molecule3D\n", " molecule_output = gr.HTML(label=\"Protein Structure\")\n", " predictions_output = gr.Textbox(label=\"Binding Site Predictions\")\n", " download_output = gr.File(label=\"Download Predictions\")\n", " \n", " visualize_btn.click(fetch_pdb, inputs=[pdb_input], outputs=molecule_output)\n", " prediction_btn.click(process_pdb, inputs=[pdb_input, segment_input], outputs=[predictions_output, molecule_output, download_output])\n", " \n", " gr.Markdown(\"## Examples\")\n", " gr.Examples(\n", " examples=[\n", " [\"2IWI\", \"A\"],\n", " [\"7RPZ\", \"B\"],\n", " [\"3TJN\", \"C\"]\n", " ],\n", " inputs=[pdb_input, segment_input],\n", " outputs=[predictions_output, molecule_output, download_output]\n", " )\n", "\n", "demo.launch(debug=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "9a5facd9-855c-4b35-8dd3-2c0c8c7dd356", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "raw", "id": "a762170f-92a9-473d-b18d-53607a780e3b", "metadata": {}, "source": [ "import gradio as gr\n", "import requests\n", "from Bio.PDB import PDBParser\n", "import numpy as np\n", "import os\n", "\n", "def read_mol(pdb_path):\n", " \"\"\"Read PDB file and return its content as a string\"\"\"\n", " with open(pdb_path, 'r') as f:\n", " return f.read()\n", "\n", "# Function to fetch a PDB file from RCSB PDB\n", "def fetch_pdb(pdb_id):\n", " pdb_url = f'https://files.rcsb.org/download/{pdb_id}.pdb'\n", " pdb_path = f'{pdb_id}.pdb'\n", " response = requests.get(pdb_url)\n", " if response.status_code == 200:\n", " with open(pdb_path, 'wb') as f:\n", " f.write(response.content)\n", " return pdb_path\n", " else:\n", " return None\n", "\n", "# Function to process the PDB file and return random predictions\n", "def process_pdb(pdb_id, segment):\n", " pdb_path = fetch_pdb(pdb_id)\n", " if not pdb_path:\n", " return \"Failed to fetch PDB file\", None, None\n", " parser = PDBParser(QUIET=True)\n", " structure = parser.get_structure('protein', pdb_path)\n", " \n", " try:\n", " chain = structure[0][segment]\n", " except KeyError:\n", " return \"Invalid Chain ID\", None, None\n", " sequence = [residue.get_resname() for residue in chain if residue.id[0] == ' ']\n", " random_scores = np.random.rand(len(sequence))\n", " result_str = \"\\n\".join(\n", " f\"{seq} {res.id[1]} {score:.2f}\" \n", " for seq, res, score in zip(sequence, chain, random_scores)\n", " )\n", " # Save the predictions to a file\n", " prediction_file = f\"{pdb_id}_predictions.txt\"\n", " with open(prediction_file, \"w\") as f:\n", " f.write(result_str)\n", " \n", " return result_str, molecule(pdb_path), prediction_file\n", "\n", "def molecule(input_pdb):\n", " mol = read_mol(input_pdb) # Read PDB file content\n", " \n", " html_content = f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \"\"\"\n", " \n", " # Return the HTML content within an iframe safely encoded for special characters\n", " return f''\n", "\n", "# Gradio UI\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Protein Binding Site Prediction (Random Scores)\")\n", " with gr.Row():\n", " pdb_input = gr.Textbox(value=\"2IWI\", label=\"PDB ID\", placeholder=\"Enter PDB ID here...\")\n", " segment_input = gr.Textbox(value=\"A\", label=\"Chain ID\", placeholder=\"Enter Chain ID here...\")\n", " visualize_btn = gr.Button(\"Visualize Structure\")\n", " prediction_btn = gr.Button(\"Predict Random Binding Site Scores\")\n", " \n", " molecule_output = gr.HTML(label=\"Protein Structure\")\n", " predictions_output = gr.Textbox(label=\"Binding Site Predictions\")\n", " download_output = gr.File(label=\"Download Predictions\")\n", " \n", " # Update to explicitly use molecule() function for visualization\n", " visualize_btn.click(\n", " fn=lambda pdb_id: molecule(fetch_pdb(pdb_id)), \n", " inputs=[pdb_input], \n", " outputs=molecule_output\n", " )\n", " \n", " prediction_btn.click(process_pdb, inputs=[pdb_input, segment_input], outputs=[predictions_output, molecule_output, download_output])\n", " \n", " gr.Markdown(\"## Examples\")\n", " gr.Examples(\n", " examples=[\n", " [\"2IWI\", \"A\"],\n", " [\"7RPZ\", \"B\"],\n", " [\"3TJN\", \"C\"]\n", " ],\n", " inputs=[pdb_input, segment_input],\n", " outputs=[predictions_output, molecule_output, download_output]\n", " )\n", "\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "15527a58-c449-4da0-8fab-3baaede15e41", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 2, "id": "9ef3e330-cb88-4c29-b84a-2f8652883cfc", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7860\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "import requests\n", "from Bio.PDB import PDBParser\n", "import numpy as np\n", "import os\n", "from gradio_molecule3d import Molecule3D\n", "\n", "def read_mol(pdb_path):\n", " \"\"\"Read PDB file and return its content as a string\"\"\"\n", " with open(pdb_path, 'r') as f:\n", " return f.read()\n", "\n", "def fetch_pdb(pdb_id):\n", " pdb_url = f'https://files.rcsb.org/download/{pdb_id}.pdb'\n", " pdb_path = f'{pdb_id}.pdb'\n", " response = requests.get(pdb_url)\n", " if response.status_code == 200:\n", " with open(pdb_path, 'wb') as f:\n", " f.write(response.content)\n", " return pdb_path\n", " else:\n", " return None\n", "\n", "def process_pdb(pdb_id, segment):\n", " pdb_path = fetch_pdb(pdb_id)\n", " if not pdb_path:\n", " return \"Failed to fetch PDB file\", None, None\n", " parser = PDBParser(QUIET=True)\n", " structure = parser.get_structure('protein', pdb_path)\n", " \n", " try:\n", " chain = structure[0][segment]\n", " except KeyError:\n", " return \"Invalid Chain ID\", None, None\n", " sequence = [residue.get_resname() for residue in chain if residue.id[0] == ' ']\n", " random_scores = np.random.rand(len(sequence))\n", " result_str = \"\\n\".join(\n", " f\"{seq} {res.id[1]} {score:.2f}\" \n", " for seq, res, score in zip(sequence, chain, random_scores)\n", " )\n", " # Save the predictions to a file\n", " prediction_file = f\"{pdb_id}_predictions.txt\"\n", " with open(prediction_file, \"w\") as f:\n", " f.write(result_str)\n", " \n", " return result_str, molecule(pdb_path, random_scores), prediction_file\n", "\n", "def molecule(input_pdb, scores=None):\n", " mol = read_mol(input_pdb) # Read PDB file content\n", " \n", " # Prepare high-scoring residues script if scores are provided\n", " high_score_script = \"\"\n", " if scores is not None:\n", " high_score_script = \"\"\"\n", " // Highlight residues with high scores\n", " let highScoreResidues = [{}];\n", " viewer.getModel(0).setStyle(\n", " {{\"resi\": highScoreResidues}}, \n", " {{\"stick\": {{\"color\": \"red\"}}}}\n", " );\n", " \"\"\".format(\n", " \", \".join(str(i+1) for i, score in enumerate(scores) if score > 0.8)\n", " )\n", " \n", " html_content = f\"\"\"\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \"\"\"\n", " \n", " # Return the HTML content within an iframe safely encoded for special characters\n", " return f''\n", "\n", "reps = [\n", " {\n", " \"model\": 0,\n", " \"style\": \"cartoon\",\n", " \"color\": \"whiteCarbon\",\n", " \"residue_range\": \"\",\n", " \"around\": 0,\n", " \"byres\": False,\n", " }\n", " ]\n", "# Gradio UI\n", "with gr.Blocks() as demo:\n", " gr.Markdown(\"# Protein Binding Site Prediction (Random Scores)\")\n", " with gr.Row():\n", " pdb_input = gr.Textbox(value=\"2IWI\", label=\"PDB ID\", placeholder=\"Enter PDB ID here...\")\n", " segment_input = gr.Textbox(value=\"A\", label=\"Chain ID\", placeholder=\"Enter Chain ID here...\")\n", " visualize_btn = gr.Button(\"Visualize Structure\")\n", " #prediction_btn = gr.Button(\"Predict Random Binding Site Scores\")\n", "\n", " molecule_output2 = Molecule3D(label=\"Protein Structure\", reps=reps)\n", "\n", " with gr.Row():\n", " pdb_input = gr.Textbox(value=\"2IWI\", label=\"PDB ID\", placeholder=\"Enter PDB ID here...\")\n", " segment_input = gr.Textbox(value=\"A\", label=\"Chain ID\", placeholder=\"Enter Chain ID here...\")\n", " prediction_btn = gr.Button(\"Predict Random Binding Site Scores\")\n", "\n", " molecule_output = gr.HTML(label=\"Protein Structure\")\n", " predictions_output = gr.Textbox(label=\"Binding Site Predictions\")\n", " download_output = gr.File(label=\"Download Predictions\")\n", " \n", " #visualize_btn.click(\n", " # fn=lambda pdb_id: molecule(fetch_pdb(pdb_id)), \n", " # inputs=[pdb_input], \n", " # outputs=molecule_output\n", " #)\n", " visualize_btn.click(fetch_pdb, inputs=[pdb_input], outputs=molecule_output2)\n", " \n", " \n", " prediction_btn.click(process_pdb, inputs=[pdb_input, segment_input], outputs=[predictions_output, molecule_output, download_output])\n", " \n", " gr.Markdown(\"## Examples\")\n", " gr.Examples(\n", " examples=[\n", " [\"2IWI\", \"A\"],\n", " [\"7RPZ\", \"B\"],\n", " [\"3TJN\", \"C\"]\n", " ],\n", " inputs=[pdb_input, segment_input],\n", " outputs=[predictions_output, molecule_output, download_output]\n", " )\n", "\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "14605615-8610-4d9e-841b-db7618cde844", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python (LLM)", "language": "python", "name": "llm" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }