Visualizations
099 / 500

099 · Algorithms

Protein Folding

An HP lattice protein folds a hydrophobic core under replica-exchange Monte Carlo.

In Dill's HP model a protein is a chain of hydrophobic (orange) and polar (white) beads on a square or cubic lattice, and every pair of touching hydrophobic beads that are not bonded lowers the energy by one, drawn as red dashes. Eight copies of the chain run Metropolis Monte Carlo with pull moves, where a bead steps to a free diagonal site and the chain follows behind it, on a ladder of temperatures from 0.24 to 1.25; neighbors on the ladder regularly swap conformations, so a fold stuck in the cold can melt in the heat and come back down somewhere better. The panel shows every replica and flashes each accepted swap, while the main view follows the best current replica, aligned by lattice symmetry so a swap morphs smoothly. It starts on the classic 2D benchmark sequences and usually reaches their best known energies, such as -23 for the 48-residue S1-5, within seconds.

Try it. Drag a bead to pull the chain apart, and every replica refolds from your version. Type H and P letters and press Enter to fold your own sequence. Buttons or keys step through the benchmarks (N or the arrows), switch between 2D and 3D (D), unfold (U), change speed (up and down arrows) and pause (Space). In 3D, drag to turn the lattice.

  • HP lattice model
  • Replica-exchange Monte Carlo
  • Pull moves
  • Metropolis criterion
  • Lattice symmetry alignment

View the source · one module, plus a small shared runtime for sizing, the animation loop and input

Build your own

Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.

Build an HP lattice protein folding simulator with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.

Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and draws a light graph paper grid.
- Represent a protein as a string of H (hydrophobic) and P (polar) letters, for example PPHPPHHPPHHPPPPPHHHHHHHHHHPPPPPPHHPPHHPPHPPHHHHH, placed as a chain of beads on the square lattice, starting as a straight line. Keep a map from lattice cell to bead so you can check that the chain never overlaps itself.
- The energy is -1 for every pair of H beads that touch on the lattice but are not neighbors along the chain.
- Each frame, run a few hundred Monte Carlo moves at a fixed temperature: pick a bead, try an end move (an end bead jumps to a free cell next to its neighbor) or a corner flip (a bead at a corner flips to the opposite corner of the square), and accept with the Metropolis rule, keeping it if the energy drops or with probability exp(-increase / T).
- Draw bonds as thick lines, H beads in orange, P beads in white, and H to H contacts as red dashed lines.

Once that works, make it beautiful:
- Add pull moves, where a bead steps to a free diagonal cell and the chain follows behind it, which let it fold much faster.
- Run several copies at a ladder of temperatures and let neighbors swap temperatures now and then (replica exchange), accepting a swap with probability exp((1/T1 - 1/T2)(E1 - E2)).
- Smoothly animate beads between lattice positions, zoom to fit the chain, and chart the energy over time.

Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as a 3D cubic lattice, typing in your own sequence, or dragging a bead to pull the chain apart.
PreviousFPUT RecurrenceThe 1955 computer experiment where a chain of masses refused to thermalize. NextPlucked StringsSeven finite-difference strings to pluck, with live harmonics and slow-motion kinks.

Related visualizations

  • Cell SortingEmergence A Cellular Potts tissue unmixes itself by differential adhesion, on a stained slide.
  • Melting CrystalSimulation Nearly 2,000 atoms freeze into crystal grains, melt and boil from one pairwise force.
  • Self-AssemblyEmergence Lipid membrane patches assemble themselves from a random soup into closed vesicles.
  • Rock Paper LizardsEmergence Three lizard morphs chase each other in spirals until mobility tips them into extinction.
  • DendritesSimulation Supercooled melt freezes into branching dendrites, with latent heat glowing between arms.
  • Ising MagnetSimulation A magnet at its critical point, where domains of spins turn fractal.
  • Snowflake GrowthNature Steer the weather on a Nakaya diagram and grow one-of-a-kind snowflakes.
  • Critical PercolationSimulation Open hexagons merge into a giant cluster the instant p crosses one half.
  • MCTS BloomAlgorithms Four-in-a-row against Monte Carlo Tree Search, its search tree blooming as a flower.

Use ← and → to move between demos. While the canvas has focus, keys go to the demo instead.

← More from Emergent Mind Labs