# ESM

> Protein language models and structure prediction

- Category: Science & Biomedical AI / Protein & Molecular Structure
- Language: Python
- Repository: https://github.com/facebookresearch/esm
- Canonical page: https://ai-tldr.dev/tools/esm/

## In short

ESM (Evolutionary Scale Modeling) is a collection of transformer-based protein language models with pretrained weights, developed by Meta's Fundamental AI Research Protein Team. The repository packages several models under a single Python library, from the general-purpose ESM-2 language model to the ESMFold structure predictor. It sits in the Protein & Molecular Structure category of the Science & Biomedical AI tools and is open-source, written in Python.

## Overview

ESM (Evolutionary Scale Modeling) is a collection of transformer-based protein language models with pretrained weights, developed by Meta's Fundamental AI Research Protein Team. The repository packages several models under a single Python library, from the general-purpose ESM-2 language model to the ESMFold structure predictor.

ESM-2 is a protein language model available in a range of sizes, from 8M up to 15B parameters. It learns representations of protein sequences that can be used for tasks such as structure prediction, function prediction, and contact prediction. ESMFold builds on ESM-2 to predict a protein's 3D structure end to end directly from a single sequence.

Alongside these, the project includes ESM-1v for zero-shot prediction of the effects of mutations on protein function, ESM-IF1 for inverse folding (designing sequences from backbone structures), the MSA Transformer, and the earlier ESM-1b model. The library ships both a Python API and command-line tools for bulk embedding extraction and structure prediction.

## What it does

- ESM-2 general-purpose protein language model in sizes from 8M to 15B parameters
- ESMFold, an end-to-end single-sequence 3D structure predictor built on ESM-2
- ESM-1v for zero-shot prediction of the effects of mutations on protein function
- ESM-IF1 inverse folding model for sequence design from backbone coordinates
- esm-extract command-line tool for bulk embedding extraction from FASTA files
- esm-fold command-line tool and a Python API, with models also loadable via PyTorch Hub

## Getting started

Install the library with pip, then load a pretrained ESM-2 model to extract sequence representations, or use ESMFold to predict a 3D structure.

1. Install ESM

   Install the base package with pip. To use ESMFold, install the esmfold extra, which pulls in the OpenFold dependencies.

```bash
pip install fair-esm

# For ESMFold structure prediction:
pip install "fair-esm[esmfold]"
```

2. Load ESM-2 and extract representations

   Load a pretrained ESM-2 model, tokenize your sequences with the batch converter, and run the model to get per-token representations.

```python
import torch
import esm

model, alphabet = esm.pretrained.esm2_t33_650M_UR50D()
batch_converter = alphabet.get_batch_converter()
model.eval()

data = [
    ("protein1", "MKTVRQERLKSIVRILERSKEPVSGAQLAEELSVSRQVIVQDIAYLRSLGYNIVATPRGYVLAGG"),
]
batch_labels, batch_strs, batch_tokens = batch_converter(data)

with torch.no_grad():
    results = model(batch_tokens, repr_layers=[33], return_contacts=True)
token_representations = results["representations"][33]
```

3. Predict a structure with ESMFold

   Load ESMFold and call infer_pdb on a single sequence to get a predicted 3D structure as a PDB string.

```python
import torch
import esm

model = esm.pretrained.esmfold_v1()
model = model.eval().cuda()

sequence = "MKTVRQERLKSIVRILERSKEPVSGAQLAEELSVSRQVIVQDIAYLRSLGYNIVATPRGYVLAGG"

with torch.no_grad():
    output = model.infer_pdb(sequence)

with open("result.pdb", "w") as f:
    f.write(output)
```

4. Extract embeddings in bulk from the command line

   Use the esm-extract tool to compute embeddings for every sequence in a FASTA file.

```bash
esm-extract esm2_t33_650M_UR50D examples/data/some_proteins.fasta \
  examples/data/some_proteins_emb_esm2 --repr_layers 0 32 33 --include mean per_tok
```

## When to use it

- Generate learned embeddings of protein sequences with ESM-2 to feed into downstream machine learning models
- Predict a protein's 3D structure directly from its sequence using ESMFold, without a multiple sequence alignment
- Score the likely functional impact of mutations with ESM-1v using zero-shot variant effect prediction
- Design candidate sequences for a given backbone structure with the ESM-IF1 inverse folding model
