{ "cells": [ { "cell_type": "markdown", "id": "cba4e00c", "metadata": {}, "source": [ "# Tutorial 13 — Extending the packer\n", "\n", "[](https://colab.research.google.com/github/uw-ipd/tmol/blob/master/docs/tutorial/13_extending_the_packer.ipynb)\n", "\n", "Subclass `PackerPalette`, audit rotamer candidates, export their coordinates, and verify a packed structure. Complete [packing](04_packing_and_mutation_scan.ipynb) first.\n", "\n", "Rotamer enumeration is deterministic for a fixed task. Annealing searches candidate assignments stochastically; candidate counts do not predict the final sequence.\n" ] }, { "cell_type": "markdown", "id": "c5a2d1f0", "metadata": {}, "source": [ "## Setup\n", "\n", "In Colab, select **T4 GPU**, then **Run all**. For local execution, follow the [installation guide](../installation.md). Setup installs TMol and downloads the fixtures on first use.\n", "\n", "The notebook loads the first ten residues of the checked-in 1UBQ structure and optimizes polar hydrogens before interpreting all-atom scores. Only one internal position is designable, keeping CPU documentation execution practical.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "7ebcd4da", "metadata": {}, "outputs": [], "source": [ "try:\n", " import google.colab # noqa: F401\n", "except ImportError:\n", " IN_COLAB = False\n", "else:\n", " IN_COLAB = True\n", "\n", "if IN_COLAB:\n", " from urllib.request import urlopen\n", "\n", " exec(\n", " urlopen(\n", " \"https://raw.githubusercontent.com/uw-ipd/tmol/\"\n", " \"master/docs/tutorial/colab_setup.py\"\n", " ).read(),\n", " globals(),\n", " )\n", " setup_colab([\"tmol/tests/data/cif/1UBQ.cif\"])\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "cb00cf14", "metadata": { "tags": [ "collapse-code" ] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Environment variable CCD_MIRROR_PATH not set. Will not be able to use function requiring this variable. To set it you may:\n", " (1) add the line 'export VAR_NAME=path/to/variable' to your .bashrc or .zshrc file\n", " (2) set it in your current shell with 'export VAR_NAME=path/to/variable'\n", " (3) write it to a .env file in the root of the atomworks.io repository\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Environment variable PDB_MIRROR_PATH not set. Will not be able to use function requiring this variable. To set it you may:\n", " (1) add the line 'export VAR_NAME=path/to/variable' to your .bashrc or .zshrc file\n", " (2) set it in your current shell with 'export VAR_NAME=path/to/variable'\n", " (3) write it to a .env file in the root of the atomworks.io repository\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "TMol 0.1.62; PyTorch 2.14.1+cpu; device=cpu; blocks=10\n" ] } ], "source": [ "from contextlib import redirect_stderr, redirect_stdout\n", "from io import StringIO\n", "from pathlib import Path\n", "import tempfile\n", "import warnings\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "import torch\n", "from IPython.display import display\n", "from biotite.structure.io import load_structure\n", "\n", "import tmol\n", "from tmol.database import ParameterDatabase\n", "from tmol.io import pose_stack_from_biotite\n", "from tmol.numeric import coord_dihedrals\n", "from tmol.ops import res_mask_to_coord_mask\n", "from tmol.pack import PackerPalette, PackerTask, SetPackerTask, pack_rotamers\n", "from tmol.pack.rotamer import FixedAAChiSampler, IncludeCurrentSampler, build_rotamers\n", "from tmol.pack.rotamer.dunbrack import create_dunbrack_sampler_from_database\n", "from tmol.score import beta2016_score_function\n", "\n", "SEED = 20260910\n", "np.random.seed(SEED)\n", "torch.manual_seed(SEED)\n", "if torch.cuda.is_available():\n", " torch.cuda.manual_seed_all(SEED)\n", "warnings.filterwarnings(\n", " \"ignore\", message=r\"Sparse invariant checks are implicitly disabled.*\"\n", ")\n", "\n", "device = (\n", " torch.device(\"cuda\", torch.cuda.current_device())\n", " if torch.cuda.is_available()\n", " else torch.device(\"cpu\")\n", ")\n", "repo_root = Path.cwd()\n", "if not (repo_root / \"tmol/tests/data/cif/1UBQ.cif\").exists():\n", " repo_root = Path(tmol.__file__).resolve().parents[1]\n", "cif_path = repo_root / \"tmol/tests/data/cif/1UBQ.cif\"\n", "\n", "parameter_db = ParameterDatabase.get_default()\n", "atom_array = load_structure(str(cif_path), model=1, include_bonds=True)\n", "protein_slice = atom_array[(atom_array.chain_id == \"A\") & (atom_array.res_id <= 10)]\n", "diagnostics = StringIO()\n", "try:\n", " with redirect_stdout(diagnostics), redirect_stderr(diagnostics):\n", " pose_stack = pose_stack_from_biotite(\n", " protein_slice,\n", " device,\n", " param_db=parameter_db,\n", " no_optH=False,\n", " )\n", "except Exception:\n", " print(diagnostics.getvalue())\n", " raise\n", "score_function = beta2016_score_function(device, param_db=parameter_db)\n", "\n", "\n", "def show_table(frame):\n", " try:\n", " from itables import show\n", " except ImportError:\n", " return display(frame)\n", " return show(frame)\n", "\n", "\n", "def total_score(pose):\n", " scorer = score_function.render_whole_pose_scoring_module(pose)\n", " return float(scorer(pose.coords).detach().cpu()[0])\n", "\n", "\n", "print(\n", " f\"TMol {tmol.__version__}; PyTorch {torch.__version__}; \"\n", " f\"device={device}; blocks={pose_stack.max_n_blocks}\"\n", ")\n" ] }, { "cell_type": "markdown", "id": "b9daef61", "metadata": {}, "source": [ "## Subclass `PackerPalette`\n", "\n", "Start from the default L-amino-acid compatibility rules, retain a hydrophobic alphabet, and always keep the original type. A palette or task may remove choices but must not re-enable rejected types.\n", "\n", "Only block 5 (LYS in the 1UBQ slice) is active. Every position retains at least one valid choice.\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "abe86466", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
| \n", "\n", " Loading ITables v2.9.1 from the internet...\n", " (need help?)\n", " | \n", "
| ⓘchoice_slot | \n", "block_type | \n", "name3 | \n", "allowed_after_palette | \n", "is_original | \n", "
|---|---|---|---|---|
| 0 | ALA | ALA | True | False |
| 1 | ILE | ILE | True | False |
| 2 | LEU | LEU | True | False |
| 3 | LYS | LYS | True | True |
| 4 | PHE | PHE | True | False |
| 5 | VAL | VAL | True | False |
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
| \n", "\n", " Loading ITables v2.9.1 from the internet...\n", " (need help?)\n", " | \n", "
| ⓘ | \n", "name3 | \n", "candidate_rotamers | \n", "
|---|---|---|
| 3 | LYS | 41 |
| 4 | PHE | 11 |
| 2 | LEU | 6 |
| 1 | ILE | 6 |
| 5 | VAL | 3 |
| 0 | ALA | 1 |
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
| \n", "\n", " Loading ITables v2.9.1 from the internet...\n", " (need help?)\n", " | \n", "
| ⓘstage | \n", "target_identity | \n", "weighted_score_units | \n", "score_change | \n", "
|---|---|---|---|
| input | LYS | 28.778711 | 0.000000 |
| custom-palette packing | ILE | 27.658930 | -1.119781 |
3Dmol.js failed to load for some reason. Please check your browser console for error messages.
3Dmol.js failed to load for some reason. Please check your browser console for error messages.