13 Demo: Protein Structure Visualization with PyMol
In this demo, we’ll create publication-quality visualizations of a protein structure—without prior PyMol knowledge. This demonstrates using Claude Code to learn and apply unfamiliar tools.
13.1 The Goal
We want to visualize the binding site of a protein-ligand complex and create a figure suitable for a paper or presentation. We’ll learn PyMol along the way.
13.2 Prerequisites
- PyMol installed (we’ll cover installation)
- Internet connection (to download structures)
- Basic understanding of proteins (helpful but not required)
13.3 Project Setup
mkdir ~/Projects/pymol-visualization
cd ~/Projects/pymol-visualization
claude> Set up a project for protein structure visualization. I'll need to download PDB files and generate PyMol scripts.
13.4 Phase 1: Installing PyMol
First, let’s get PyMol installed:
> How do I install PyMol on my system? I'm on [Mac/Windows/Linux]
Mac (with Homebrew):
brew install pymolMac (alternative): Download from pymol.org (educational license available)
Linux (Ubuntu):
sudo apt-get install pymolWindows: Download installer from pymol.org
Verify installation:
> Check if pymol is installed and working
pymol -c -q # Should exit without errors13.5 Phase 2: Choosing a Structure
Let’s pick an interesting protein. For this demo, we’ll use Human Carbonic Anhydrase II bound to a drug—it’s well-studied and has a clear binding site.
> Find a PDB structure for Human Carbonic Anhydrase II bound to a small molecule inhibitor
Claude might suggest PDB ID 1BN1 (CA-II with acetazolamide) or similar.
> Download the PDB file for 1BN1 to our data folder
import urllib.request
import os
os.makedirs('data', exist_ok=True)
url = "https://files.rcsb.org/download/1BN1.pdb"
urllib.request.urlretrieve(url, "data/1BN1.pdb")13.6 Phase 3: First Visualization
Now let’s create our first image. We’ll ask Claude to help with PyMol commands.
> Create a PyMol script that:
> 1. Loads 1BN1.pdb
> 2. Colors the protein in a nice blue color
> 3. Shows the ligand (acetazolamide) as sticks in yellow
> 4. Saves an image
Claude generates scripts/basic_view.pml:
# Load structure
load data/1BN1.pdb
# Color protein
color marine, polymer
# Show ligand as sticks
select ligand, organic
show sticks, ligand
color yellow, ligand
# Clean up display
hide lines
show cartoon, polymer
set cartoon_fancy_helices, 1
# Set background
bg_color white
# Ray trace for high quality
ray 1200, 1200
# Save image
png results/basic_view.png
Run it:
> Run this PyMol script
pymol -c scripts/basic_view.pml # -c for command line mode13.7 Phase 4: Visualizing the Binding Site
The basic view shows the whole protein. Let’s focus on the binding site:
> Modify the script to:
> 1. Zoom in on the binding site (within 5 Angstroms of the ligand)
> 2. Show residues near the ligand as sticks
> 3. Color binding site residues by element (carbon gray, oxygen red, nitrogen blue)
Updated script:
# Load and basic setup
load data/1BN1.pdb
bg_color white
# Color protein
color marine, polymer
show cartoon, polymer
# Select and show ligand
select ligand, organic
show sticks, ligand
color yellow, ligand and elem C
util.cnc ligand # Color by element except carbon
# Select binding site residues (within 5A of ligand)
select binding_site, byres (polymer within 5 of ligand)
show sticks, binding_site
color gray, binding_site and elem C
util.cnc binding_site
# Zoom to binding site
zoom binding_site
# Add surface representation (transparent)
show surface, binding_site
set transparency, 0.7, binding_site
color white, binding_site and surface
# High quality render
set ray_shadow, 0
set antialias, 2
ray 1600, 1600
png results/binding_site.png
13.8 Phase 5: Showing Interactions
Let’s visualize the key interactions between the ligand and protein:
> Add visualization of hydrogen bonds between the ligand and nearby residues
# Find and display hydrogen bonds
distance hbonds, ligand, binding_site, mode=2
set dash_color, black, hbonds
set dash_width, 2
> Also show the zinc ion that's important for catalysis, as a sphere
# Show zinc
select zinc, elem Zn
show spheres, zinc
color gray50, zinc
set sphere_scale, 0.4, zinc
13.9 Phase 6: Publication Quality
Let’s refine for publication:
> Make these changes for publication quality:
> 1. Increase overall size and resolution
> 2. Add smooth lighting
> 3. Remove the PyMol axes
> 4. Use a cleaner font for any labels
# Publication settings
set ray_trace_mode, 1
set ray_trace_frames, 1
set ray_shadows, 0
set orthoscopic, on
# Remove axes
set axes, 0
# Smooth appearance
set antialias, 2
set spec_reflect, 0.5
set spec_power, 200
# High resolution output
ray 2400, 2400
png results/figure_1_binding_site.png, dpi=300
13.10 Phase 7: Multiple Views
For a paper, we often need multiple perspectives:
> Create a script that generates 3 views:
> 1. Overview of the full protein with binding site highlighted
> 2. Close-up of binding site
> 3. Rotated view showing the depth of the binding pocket
Claude creates a comprehensive script that: - Sets up a common base configuration - Saves the session - Generates each view with appropriate rotation and zoom - Outputs as both PNG and PDF
13.11 Phase 8: Making It Reproducible
> Create a main.py script that:
> 1. Downloads the PDB if not present
> 2. Runs all PyMol scripts
> 3. Generates all figures
> 4. Works from the command line
#!/usr/bin/env python
"""Generate protein structure figures."""
import os
import subprocess
import urllib.request
PDB_ID = "1BN1"
def download_pdb():
"""Download PDB file if not present."""
path = f"data/{PDB_ID}.pdb"
if not os.path.exists(path):
os.makedirs("data", exist_ok=True)
url = f"https://files.rcsb.org/download/{PDB_ID}.pdb"
print(f"Downloading {PDB_ID}...")
urllib.request.urlretrieve(url, path)
return path
def run_pymol_script(script):
"""Run a PyMol script in command-line mode."""
result = subprocess.run(
["pymol", "-c", "-q", script],
capture_output=True,
text=True
)
if result.returncode != 0:
print(f"Error running {script}:")
print(result.stderr)
return result.returncode == 0
def main():
os.makedirs("results", exist_ok=True)
# Download structure
download_pdb()
# Generate figures
scripts = [
"scripts/overview.pml",
"scripts/binding_site.pml",
"scripts/rotated_view.pml",
]
for script in scripts:
print(f"Running {script}...")
run_pymol_script(script)
print("Done! Figures saved to results/")
if __name__ == "__main__":
main()13.12 Learning PyMol Along the Way
Throughout this demo, Claude has been teaching us PyMol:
13.12.1 Key Concepts Learned
| Concept | What We Learned |
|---|---|
| Selections | select name, criteria to create named groups |
| Representations | cartoon, sticks, surface, spheres |
| Coloring | color, util.cnc for element colors |
| Distance | distance for measuring and showing bonds |
| Views | zoom, orient, turn, move |
| Ray tracing | ray for publication-quality rendering |
13.12.2 Asking for Help
When stuck, ask Claude:
> I want the protein backbone to be semi-transparent. How do I do that in PyMol?
> What's the difference between 'show' and 'set' in PyMol?
13.13 Exploring Further
> What other cool visualizations could we do with this structure?
Ideas: - Electrostatic surface potential - Hydrophobicity mapping - Animation of binding - Multiple structures superimposed
13.14 Final Cleanup
> Create a README that documents:
> 1. What structures we visualized
> 2. How to run the scripts
> 3. What each output figure shows
> Commit everything with a descriptive message
13.15 What You’ve Learned
By completing this demo, you’ve:
- Installed and used PyMol without prior experience
- Learned PyMol syntax through practical application
- Created publication-quality figures programmatically
- Built a reproducible pipeline for structure visualization
13.16 Applying This to Your Work
Try with your own protein of interest:
> Help me visualize [your protein]. I want to show [specific feature].
Claude will adapt the approach to your structure.
13.17 Next Steps
Try another demo or continue to Part 4: Paper Writing Track.