This project implements the HP lattice model for protein folding in Python.
The HP lattice model is a simplified approach to explore basic protein folding behaviors via Monte Carlo simulations that explore the free energy landscape of protein configurations. It reduces protein sequences to two amino acid categories: H (hydrophobic) and P (polar). For more information, see this paper or the theory section below.
A command-line interface allows running simulations with configurable temperature and optional annealing, producing energy evolution, structures (foldings), minimum-energy configurations, and compactness.
Clone the repository, move to the project directory, create a Python virtual environment, install dependencies, and run the application:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python src/main.pyYou can modify protein sequences, structures, and other parameters by editing the config.yaml file or creating a new
one.
# Sequences can use just H/P monomers
# or all 20 amino acids (automatically converted to H/P).
sequence: MGLSDGEWQLVLNV...
# This is an optional initial structure (fold)
structure:
use_structure: false
coordinates: [ [ 0,0 ], [ 0,1 ], ... ] # only needed if use_structure: true
# Simulation configuration
simulation:
folding_steps: 5000 # Number of folding steps
annealing: true # Whether to apply annealing or not
temperature: 5.0 # Initial annealing temperature
# Plot configuration
plot:
create_gif: true # Whether to create a gif for representing evolution or not
# Seed for reproducibility (set none for random)
seed: 42Protein folding is the biological process through which proteins acquire their three-dimensional structure, known as the native state (or native conformation). This structure is essential for the protein’s biological function and emerges through a self-assembly process driven by non-covalent interactions. When folding occurs incorrectly (misfolding), proteins can become dysfunctional and may lead to disease.
Protein folding can be seen as a descent toward a stable state ruled by thermodynamic principles. The native conformation is generally considered the state of minimum free energy. This minimum corresponds to the most stable thermodynamic equilibrium, where internal forces and biological interactions are balanced, and no spontaneous changes can further reduce the energy. The folding process is often visualized as an energy landscape, where the native state lies in the deepest region (global minimum).
The HP lattice model simplifies protein folding by categorizing amino acids as either hydrophobic (H) or polar (P). Hydrophobic amino acids tend to cluster inside the protein to avoid water, while polar residues remain on the surface. This model is mainly educational and helps introduce the basic principles of protein folding, although real protein folding involves many additional factors. More advanced models are used for accurate predictions.
-
Initialization:
-
$\alpha :=$ initial protein fold -
$T :=$ initial temperature for annealing
-
-
Main Loop (Repeat for
$n$ steps):- Optionally, decrease temperature
$T$ (anneal) - Compute current energy
$E(\alpha)$ - Generate new random fold
$\alpha'$ - Calculate
$\Delta E := E(\alpha') - E(\alpha)$ - If
$\Delta E < 0$ :-
$\alpha \leftarrow \alpha'$ // Accept new fold$\alpha'$
-
- Else:
- Accept new fold with probability
$p = e^{-\frac{\Delta E}{k_B T}}$
- Accept new fold with probability
- Optionally, decrease temperature
The energy
-
Initialization:
$n_{contacts} := 0$
-
Monomer Loop (For each monomer
$i$ in the protein sequence):- If monomer
$i$ is Hydrophobic:-
$neighbors(i) \leftarrow$ get_neighbors(i)// excluding$i \pm 1$ neighbors $n_{contacts} \leftarrow n_{contacts} + neighbors(i)$
-
- If monomer
-
Final Calculation:
-
$E(\alpha) \leftarrow - \varepsilon \times \frac{n_{contacts}}{2}$ // unique contacts
-
-
Result:
-
Return
$E(\alpha)$
-
Return
A random monomer (between 1 and length-2) of the protein is selected and the tail starting from that position is modified using a random geometric transformation, such as a rotation, reflection, or diagonal move when allowed. The transformed tail is then reattached to the unchanged part of the protein. The new configuration is accepted only if it forms a valid self-avoiding walk, ensuring that no overlaps occur. Once validated, the resulting configuration becomes the new protein fold.
After generating a random fold, its energy is computed and accepted according to the Metropolis algorithm. In this specific implementation, only topological contacts between H-H monomers decrease the system's energy. More formally:
- If the new structure's energy is lower, accept it.
- If higher, accept with probability:
Where:
-
$e$ — Euler's number (≈ 2.71828) -
$\Delta E$ — energy difference between folds -
$k_B$ — Boltzmann constant (set to 1) -
$T$ — temperature (for optional annealing)
Notes
-
$k_B = 1$ simplifies the simulation and comparisons between runs - All quantities are treated as dimensionless
- Double-counting of contacts is automatically handled for correct energy and compactness
This example runs on a simulation of the Myoglobin (Camelus dromedarius) protein sequence.
- Simulate 5000 folding steps
- Starting annealing temperature: 5.0
Results (found in the output/ folder):
- Initial protein sequence:

- Evolution process:

- Final protein folding:

- Energy evolution:

- Compactness evolution:

- Minimum energy folding

- Maximum compactness folding

The plots show how energy and compactness stabilize as the temperature decreases. The lowest energy configuration does not necessarily correspond to the highest compactness.