{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains material from [PyRosetta](https://RosettaCommons.github.io/PyRosetta.notebooks);\n", "content is available [on Github](https://github.com/RosettaCommons/PyRosetta.notebooks.git).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Introduction to Folding](http://nbviewer.jupyter.org/github/RosettaCommons/PyRosetta.notebooks/blob/master/notebooks/04.00-Introduction-to-Folding.ipynb) | [Contents](toc.ipynb) | [Index](index.ipynb) | [Low-Res Scoring and Fragments](http://nbviewer.jupyter.org/github/RosettaCommons/PyRosetta.notebooks/blob/master/notebooks/04.02-Low-Res-Scoring-and-Fragments.ipynb) >
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic Folding Algorithm\n",
"Keywords: pose_from_sequence(), random move, scoring move, Metropolis, assign(), Pose()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install pyrosettacolabsetup\n",
"import pyrosettacolabsetup; pyrosettacolabsetup.install_pyrosetta()\n",
"import pyrosetta; pyrosetta.init()\n",
"from pyrosetta import *\n",
"from pyrosetta.teaching import *\n",
"init()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Building the Pose"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this workshop, you will be folding a 10 residue protein by building a simple de novo folding algorithm. Start by initializing PyRosetta as usual."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a simple poly-alanine `pose` with 10 residues for testing your folding algorithm. Store the pose in a variable called \"polyA.\""
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-e4c1f7f4858d8ee4",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [],
"source": [
"### BEGIN SOLUTION\n",
"polyA = pyrosetta.pose_from_sequence('A' * 10)\n",
"### END SOLUTION\n",
"\n",
"polyA.pdb_info().name(\"polyA\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Question:__\n",
"Check the backbone dihedrals of a few residues (except the first and last) using the `.phi()` and `.psi()` methods in `Pose`. What are the values of $\\phi$ and $\\psi$ dihedrals? You should see ideal bond lengths and angles, but the dihedrals may not be as realistic."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-8ff53305ceab1573",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"phi: 180\n",
"psi: 180\n"
]
}
],
"source": [
"### BEGIN SOLUTION\n",
"print(\"phi: %i\" %polyA.phi(9))\n",
"print(\"psi: %i\" %polyA.psi(9))\n",
"### END SOLUTION"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"OPTIONAL:\n",
"We may want to visualize folding as it happens. Before starting with the folding protocol, instantiate a PyMOL mover and use a UNIQUE port number between 10,000 and 65,536. We will retain history in order to view the entire folding process by utilizing the `.keep_history()` method. Make sure it says `PyMOL <---> PyRosetta link started!` on its command line."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"pmm = PyMOLMover()\n",
"pmm.keep_history(True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the PyMOL mover to view the `polyA` `Pose`. You should see a long thread-like structure in PyMOL."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"pmm.apply(polyA)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Building A Basic *de Novo* Folding Algorithm\n",
"\n",
"Now, write a program that implements a Monte Carlo algorithm to optimize the protein conformation. You can do this here in the notebook, or you may use a code editor to write a `.py` file and execute in a Python or iPython shell. \n",
"\n",
"Our main program will include 100 iterations of making a random trial move, scoring the protein, and accepting/rejecting the move. Therefore, we can break this algorithm down into three smaller subroutines: **random, score, and decision.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 1: Random Move\n",
"\n",
"For the **random** trial move, write a subroutine to choose one residue at random using `random.randint()` and then randomly perturb either the φ or ψ angles by a random number chosen from a Gaussian distribution. Use the Python built-in function `random.gauss()` from the `random` library with a mean of the current angle and a standard deviation of 25°. After changing the torsion angle, use `pmm.apply(polyA)` to update the structure in PyMOL."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-745a45e62c624566",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [],
"source": [
"import math\n",
"import random\n",
"\n",
"def randTrial(your_pose):\n",
"### BEGIN SOLUTION\n",
" randNum = random.randint(2, your_pose.total_residue())\n",
" currPhi = your_pose.phi(randNum)\n",
" currPsi = your_pose.psi(randNum)\n",
" newPhi = random.gauss(currPhi, 25)\n",
" newPsi = random.gauss(currPsi, 25)\n",
" your_pose.set_phi(randNum,newPhi) \n",
" your_pose.set_psi(randNum,newPsi)\n",
" pmm.apply(your_pose)\n",
"### END SOLUTION\n",
" return your_pose"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: Scoring Move\n",
"\n",
"For the **scoring** step, we need to create a scoring function and make a subroutine that simply returns the numerical energy score of the pose."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-bd1a937d32ede4e2",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0mcore.scoring.ScoreFunctionFactory: \u001b[0mSCOREFUNCTION: \u001b[32mref2015\u001b[0m\n"
]
}
],
"source": [
"sfxn = get_fa_scorefxn()\n",
"\n",
"def score(your_pose):\n",
" ### BEGIN SOLUTION\n",
" return sfxn(your_pose)\n",
" ### END SOLUTION"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: Accepting/Rejecting Move\n",
"For the **decision** step, we need to make a subroutine that either accepts or rejects the new conformatuon based on the Metropolis criterion. The Metropolis criterion has a probability of accepting a move as $P = \\exp( -\\Delta G / kT )$. When $ΔE ≥ 0$, the Metropolis criterion probability of accepting the move is $P = \\exp( -\\Delta G / kT )$. When $ΔE < 0$, the Metropolis criterion probability of accepting the move is $P = 1$. Use $kT = 1$ Rosetta Energy Unit (REU)."
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-9f9766e421aae110",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [],
"source": [
"def decision(before_pose, after_pose):\n",
" ### BEGIN SOLUTION\n",
" E = score(after_pose) - score(before_pose)\n",
" if E < 0:\n",
" return after_pose\n",
" elif random.uniform(0, 1) >= math.exp(-E/1):\n",
" return before_pose\n",
" else:\n",
" return after_pose\n",
" ### END SOLUTION"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: Execution\n",
"Now we can put these three subroutines together in our main program! Write a loop in the main program so that it performs 100 iterations of: making a random trial move, scoring the protein, and accepting/rejecting the move. \n",
"\n",
"After each iteration of the search, output the current pose energy and the lowest energy ever observed. **The final output of this program should be the lowest energy conformation that is achieved at *any* point during the simulation.** Be sure to use `low_pose.assign(pose)` rather than `low_pose = pose`, since the latter will only copy a pointer to the original pose."
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"nbgrader": {
"grade": true,
"grade_id": "cell-2556d0daef881f24",
"locked": false,
"points": 0,
"schema_version": 3,
"solution": true
}
},
"outputs": [],
"source": [
"def basic_folding(your_pose):\n",
" \"\"\"Your basic folding algorithm that completes 100 Monte-Carlo iterations on a given pose\"\"\"\n",
" \n",
" lowest_pose = Pose() # Create an empty pose for tracking the lowest energy pose.\n",
" \n",
" ### BEGIN SOLUTION\n",
" for i in range(100):\n",
" if i == 0:\n",
" lowest_pose.assign(your_pose)\n",
" \n",
" before_pose = Pose()\n",
" before_pose.assign(your_pose) # keep track of pose before random move\n",
"\n",
" after_pose = Pose()\n",
" after_pose.assign(randTrial(your_pose)) # do random move and store the pose\n",
" \n",
" your_pose.assign(decision(before_pose, after_pose)) # keep the new pose or old pose\n",
" \n",
" if score(your_pose) < score(lowest_pose): # updating lowest pose\n",
" lowest_pose.assign(your_pose)\n",
" \n",
" print(\"Iteration # %i\" %i) # output \n",
" print(\"Current pose score: %1.3f\" %score(your_pose)) # output\n",
" print(\"Lowest pose score: %1.3f\" %score(lowest_pose)) # output\n",
" \n",
" ### END SOLUTION\n",
" return lowest_pose"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, output the last pose and the lowest-scoring pose observed and view them in PyMOL. Plot the energy and lowest-energy observed vs. cycle number. What are the energies of the initial, last, and lowest-scoring pose? Is your program working? Has it converged to a good solution?\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Iteration # 0\n",
"Current pose score: 29.926\n",
"Lowest pose score: 29.926\n",
"Iteration # 1\n",
"Current pose score: 29.926\n",
"Lowest pose score: 29.926\n",
"Iteration # 2\n",
"Current pose score: 27.155\n",
"Lowest pose score: 27.155\n",
"Iteration # 3\n",
"Current pose score: 26.881\n",
"Lowest pose score: 26.881\n",
"Iteration # 4\n",
"Current pose score: 26.881\n",
"Lowest pose score: 26.881\n",
"Iteration # 5\n",
"Current pose score: 26.881\n",
"Lowest pose score: 26.881\n",
"Iteration # 6\n",
"Current pose score: 25.102\n",
"Lowest pose score: 25.102\n",
"Iteration # 7\n",
"Current pose score: 26.531\n",
"Lowest pose score: 25.102\n",
"Iteration # 8\n",
"Current pose score: 26.531\n",
"Lowest pose score: 25.102\n",
"Iteration # 9\n",
"Current pose score: 26.116\n",
"Lowest pose score: 25.102\n",
"Iteration # 10\n",
"Current pose score: 26.116\n",
"Lowest pose score: 25.102\n",
"Iteration # 11\n",
"Current pose score: 25.488\n",
"Lowest pose score: 25.102\n",
"Iteration # 12\n",
"Current pose score: 24.199\n",
"Lowest pose score: 24.199\n",
"Iteration # 13\n",
"Current pose score: 24.442\n",
"Lowest pose score: 24.199\n",
"Iteration # 14\n",
"Current pose score: 24.442\n",
"Lowest pose score: 24.199\n",
"Iteration # 15\n",
"Current pose score: 24.442\n",
"Lowest pose score: 24.199\n",
"Iteration # 16\n",
"Current pose score: 22.996\n",
"Lowest pose score: 22.996\n",
"Iteration # 17\n",
"Current pose score: 22.849\n",
"Lowest pose score: 22.849\n",
"Iteration # 18\n",
"Current pose score: 22.849\n",
"Lowest pose score: 22.849\n",
"Iteration # 19\n",
"Current pose score: 22.849\n",
"Lowest pose score: 22.849\n",
"Iteration # 20\n",
"Current pose score: 26.195\n",
"Lowest pose score: 22.849\n",
"Iteration # 21\n",
"Current pose score: 26.195\n",
"Lowest pose score: 22.849\n",
"Iteration # 22\n",
"Current pose score: 25.703\n",
"Lowest pose score: 22.849\n",
"Iteration # 23\n",
"Current pose score: 25.703\n",
"Lowest pose score: 22.849\n",
"Iteration # 24\n",
"Current pose score: 23.290\n",
"Lowest pose score: 22.849\n",
"Iteration # 25\n",
"Current pose score: 23.100\n",
"Lowest pose score: 22.849\n",
"Iteration # 26\n",
"Current pose score: 23.472\n",
"Lowest pose score: 22.849\n",
"Iteration # 27\n",
"Current pose score: 23.472\n",
"Lowest pose score: 22.849\n",
"Iteration # 28\n",
"Current pose score: 23.472\n",
"Lowest pose score: 22.849\n",
"Iteration # 29\n",
"Current pose score: 21.248\n",
"Lowest pose score: 21.248\n",
"Iteration # 30\n",
"Current pose score: 21.664\n",
"Lowest pose score: 21.248\n",
"Iteration # 31\n",
"Current pose score: 20.803\n",
"Lowest pose score: 20.803\n",
"Iteration # 32\n",
"Current pose score: 20.803\n",
"Lowest pose score: 20.803\n",
"Iteration # 33\n",
"Current pose score: 20.803\n",
"Lowest pose score: 20.803\n",
"Iteration # 34\n",
"Current pose score: 20.803\n",
"Lowest pose score: 20.803\n",
"Iteration # 35\n",
"Current pose score: 21.181\n",
"Lowest pose score: 20.803\n",
"Iteration # 36\n",
"Current pose score: 21.181\n",
"Lowest pose score: 20.803\n",
"Iteration # 37\n",
"Current pose score: 21.181\n",
"Lowest pose score: 20.803\n",
"Iteration # 38\n",
"Current pose score: 21.181\n",
"Lowest pose score: 20.803\n",
"Iteration # 39\n",
"Current pose score: 20.994\n",
"Lowest pose score: 20.803\n",
"Iteration # 40\n",
"Current pose score: 20.994\n",
"Lowest pose score: 20.803\n",
"Iteration # 41\n",
"Current pose score: 20.994\n",
"Lowest pose score: 20.803\n",
"Iteration # 42\n",
"Current pose score: 21.001\n",
"Lowest pose score: 20.803\n",
"Iteration # 43\n",
"Current pose score: 21.001\n",
"Lowest pose score: 20.803\n",
"Iteration # 44\n",
"Current pose score: 21.001\n",
"Lowest pose score: 20.803\n",
"Iteration # 45\n",
"Current pose score: 18.952\n",
"Lowest pose score: 18.952\n",
"Iteration # 46\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 47\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 48\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 49\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 50\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 51\n",
"Current pose score: 18.959\n",
"Lowest pose score: 18.952\n",
"Iteration # 52\n",
"Current pose score: 18.844\n",
"Lowest pose score: 18.844\n",
"Iteration # 53\n",
"Current pose score: 19.462\n",
"Lowest pose score: 18.844\n",
"Iteration # 54\n",
"Current pose score: 16.719\n",
"Lowest pose score: 16.719\n",
"Iteration # 55\n",
"Current pose score: 16.719\n",
"Lowest pose score: 16.719\n",
"Iteration # 56\n",
"Current pose score: 16.719\n",
"Lowest pose score: 16.719\n",
"Iteration # 57\n",
"Current pose score: 16.591\n",
"Lowest pose score: 16.591\n",
"Iteration # 58\n",
"Current pose score: 16.591\n",
"Lowest pose score: 16.591\n",
"Iteration # 59\n",
"Current pose score: 16.284\n",
"Lowest pose score: 16.284\n",
"Iteration # 60\n",
"Current pose score: 15.937\n",
"Lowest pose score: 15.937\n",
"Iteration # 61\n",
"Current pose score: 15.937\n",
"Lowest pose score: 15.937\n",
"Iteration # 62\n",
"Current pose score: 16.357\n",
"Lowest pose score: 15.937\n",
"Iteration # 63\n",
"Current pose score: 16.357\n",
"Lowest pose score: 15.937\n",
"Iteration # 64\n",
"Current pose score: 16.357\n",
"Lowest pose score: 15.937\n",
"Iteration # 65\n",
"Current pose score: 17.403\n",
"Lowest pose score: 15.937\n",
"Iteration # 66\n",
"Current pose score: 17.386\n",
"Lowest pose score: 15.937\n",
"Iteration # 67\n",
"Current pose score: 17.373\n",
"Lowest pose score: 15.937\n",
"Iteration # 68\n",
"Current pose score: 16.537\n",
"Lowest pose score: 15.937\n",
"Iteration # 69\n",
"Current pose score: 16.537\n",
"Lowest pose score: 15.937\n",
"Iteration # 70\n",
"Current pose score: 16.760\n",
"Lowest pose score: 15.937\n",
"Iteration # 71\n",
"Current pose score: 16.760\n",
"Lowest pose score: 15.937\n",
"Iteration # 72\n",
"Current pose score: 15.756\n",
"Lowest pose score: 15.756\n",
"Iteration # 73\n",
"Current pose score: 15.756\n",
"Lowest pose score: 15.756\n",
"Iteration # 74\n",
"Current pose score: 15.756\n",
"Lowest pose score: 15.756\n",
"Iteration # 75\n",
"Current pose score: 15.881\n",
"Lowest pose score: 15.756\n",
"Iteration # 76\n",
"Current pose score: 15.881\n",
"Lowest pose score: 15.756\n",
"Iteration # 77\n",
"Current pose score: 15.881\n",
"Lowest pose score: 15.756\n",
"Iteration # 78\n",
"Current pose score: 16.610\n",
"Lowest pose score: 15.756\n",
"Iteration # 79\n",
"Current pose score: 15.449\n",
"Lowest pose score: 15.449\n",
"Iteration # 80\n",
"Current pose score: 14.957\n",
"Lowest pose score: 14.957\n",
"Iteration # 81\n",
"Current pose score: 14.957\n",
"Lowest pose score: 14.957\n",
"Iteration # 82\n",
"Current pose score: 14.957\n",
"Lowest pose score: 14.957\n",
"Iteration # 83\n",
"Current pose score: 14.957\n",
"Lowest pose score: 14.957\n",
"Iteration # 84\n",
"Current pose score: 14.957\n",
"Lowest pose score: 14.957\n",
"Iteration # 85\n",
"Current pose score: 14.986\n",
"Lowest pose score: 14.957\n",
"Iteration # 86\n",
"Current pose score: 15.092\n",
"Lowest pose score: 14.957\n",
"Iteration # 87\n",
"Current pose score: 15.092\n",
"Lowest pose score: 14.957\n",
"Iteration # 88\n",
"Current pose score: 17.306\n",
"Lowest pose score: 14.957\n",
"Iteration # 89\n",
"Current pose score: 17.306\n",
"Lowest pose score: 14.957\n",
"Iteration # 90\n",
"Current pose score: 17.306\n",
"Lowest pose score: 14.957\n",
"Iteration # 91\n",
"Current pose score: 17.077\n",
"Lowest pose score: 14.957\n",
"Iteration # 92\n",
"Current pose score: 17.077\n",
"Lowest pose score: 14.957\n",
"Iteration # 93\n",
"Current pose score: 17.077\n",
"Lowest pose score: 14.957\n",
"Iteration # 94\n",
"Current pose score: 17.077\n",
"Lowest pose score: 14.957\n",
"Iteration # 95\n",
"Current pose score: 17.077\n",
"Lowest pose score: 14.957\n",
"Iteration # 96\n",
"Current pose score: 17.097\n",
"Lowest pose score: 14.957\n",
"Iteration # 97\n",
"Current pose score: 18.420\n",
"Lowest pose score: 14.957\n",
"Iteration # 98\n",
"Current pose score: 18.398\n",
"Lowest pose score: 14.957\n",
"Iteration # 99\n",
"Current pose score: 18.247\n",
"Lowest pose score: 14.957\n"
]
},
{
"data": {
"text/plain": [
" "
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Create Assignment",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {
"height": "calc(100% - 180px)",
"left": "10px",
"top": "150px",
"width": "254.188px"
},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 1
}