Skip to main content
Article
pyrosettaprotein-designstructural-biologycomputational-biologybiotechprotein-foldingenergy-minimization

Relax a Protein Structure with PyRosetta

Use PyRosetta, the Python interface for the Rosetta software suite, to perform a 'relax' protocol. This refines a protein's atomic coordinates to find a lower-energy, more stable conformation, a crucial first step for many computational biology tasks.

intermediate1 hour5 steps
The play
  1. Install and License PyRosetta
    Rosetta is academic software and requires a license (free for non-commercial use). Go to the RosettaCommons website, obtain a license, and follow the specific download and installation instructions for PyRosetta for your operating system. This step is mandatory before running any code.
  2. Initialize PyRosetta and Load a Structure
    Start your Python script by initializing PyRosetta. This loads the necessary databases and parameters. Then, load a protein structure from a PDB file into a 'Pose' object, which is Rosetta's internal representation of a macromolecule.
  3. Set up a Score Function
    A Score Function is used by Rosetta to calculate the 'energy' of a protein structure. Lower scores generally correspond to more stable, native-like conformations. We'll load the default 'ref2015' score function, which is standard for high-resolution refinement.
  4. Create and Apply the FastRelax Mover
    In Rosetta, a 'Mover' is an object that performs an action on a Pose. We will create a 'FastRelax' mover, which performs rounds of side-chain packing and energy minimization to find a low-energy state. Applying the mover modifies the pose in-place.
  5. Check the Final Score and Save
    After the relax protocol completes, calculate the new score to see the improvement. Finally, save the refined structure to a new PDB file for further analysis or visualization in a molecular viewer like PyMOL.
Starter code
import pyrosetta
import os
import urllib.request

def relax_protein(pdb_code):
    """Downloads, cleans, and relaxes a protein structure using PyRosetta."""
    pdb_filename = f"{pdb_code}.pdb"
    
    # --- 1. Download PDB file if it doesn't exist ---
    if not os.path.exists(pdb_filename):
        print(f"Downloading {pdb_filename} from the PDB...")
        url = f"https://files.rcsb.org/download/{pdb_code}.pdb"
        urllib.request.urlretrieve(url, pdb_filename)

    # --- 2. Initialize PyRosetta and Load Pose ---
    # The -ignore_unrecognized_res flag is helpful for non-canonical PDBs
    pyrosetta.init("-ignore_unrecognized_res")
    
    # Clean the PDB to remove non-standard atoms (HETATMs)
    from pyrosetta.toolbox import cleanATOM
    cleanATOM(pdb_filename)
    clean_pdb_filename = f"{pdb_code}.clean.pdb"

    # Load the cleaned structure into a Rosetta Pose
    pose = pyrosetta.pose_from_pdb(clean_pdb_filename)
    
    # --- 3. Set up Score Function and get initial score ---
    scorefxn = pyrosetta.get_fa_scorefxn()
    initial_score = scorefxn(pose)
    print(f"Initial score for {pdb_code}: {initial_score:.2f}")

    # --- 4. Create and Apply FastRelax Mover ---
    relax = pyrosetta.rosetta.protocols.relax.FastRelax()
    relax.set_scorefxn(scorefxn)
    
    print("Running FastRelax protocol...")
    relax.apply(pose)

    # --- 5. Get final score and save the result ---
    final_score = scorefxn(pose)
    output_filename = f"{pdb_code}_relaxed.pdb"
    pose.dump_pdb(output_filename)
    
    print(f"Final score for {pdb_code}: {final_score:.2f}")
    print(f"Relaxed structure saved to {output_filename}")

if __name__ == '__main__':
    # Example: Relax the Villin Headpiece (a small, fast-folding protein)
    # Make sure you have obtained a license and installed PyRosetta first!
    relax_protein("1QYS")
Relax a Protein Structure with PyRosetta — Action Pack