Upload 15 files
Browse filesFiles changed (15)
- README.md+172-3
- configs/config.pyAdded+19
- data/test.csvAdded
- data/train.csvAdded
- data/val.csvAdded
- models/diffusion.pyAdded+88
- requirements.txtAdded+13
- scripts/generate.pyAdded+131
- scripts/test.pyAdded+17
- scripts/train.pyAdded+24
- test.csvAdded
- train.csvAdded
- utils/data_loader.pyAdded+30
- utils/esm_utils.pyAdded+13
- val.csvAdded
123---license: cc-by-nc-nd-4.0---123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172# Latent Diffusion Model for Protein Sequence Generation using MDLM and ESM-2-650MHere, we implement a masked discrete latent diffusion model for generating protein sequences. The model leverages the MDLM framework and ESM-2-650M for latent space representation and diffusion.## Directory Structure```project/│├── configs/│ ├── config.py│├── data/│ ├── train.csv│ ├── val.csv│ ├── test.csv│├── models/│ ├── diffusion.py│├── scripts/│ ├── train.py│ ├── test.py│ ├── generate.py│├── utils/│ ├── data_loader.py│ ├── esm_utils.py│├── checkpoints/│ ├── example.ckpt # Placeholder for checkpoints│├── requirements.txt│└── README.md```## Setup and Requirements### Prerequisites- Python 3.8+- CUDA (for GPU support)### Install Dependencies1. Create and activate a virtual environment:```bashpython -m venv venvsource venv/bin/activate # On Windows use `venv\Scripts\activate````2. Install the required packages:```bashpip install -r requirements.txt```### Prepare DataPlace your data files (`train.csv`, `val.csv`, `test.csv`) in the `data/` directory. Ensure that these CSV files contain a column named `sequence` with the protein sequences.## ConfigurationModify the `configs/config.py` file to set your hyperparameters, model configurations, and data paths. Here is an example configuration:```pythonclass Config:model_name = "facebook/esm2_t33_650M_UR50D"latent_dim = 1280 # Adjust based on ESM-2 latent dimensionoptim = {"lr": 1e-4}training = {"ema": 0.999,"epochs": 10,"batch_size": 32,"gpus": 8,"precision": 16, # Mixed precision training"accumulate_grad_batches": 2, # Gradient accumulation"save_dir": "./checkpoints/",}data_path = "./data/"T = 1000 # Number of diffusion stepssubs_masking = False```## Mathematical Formulations### Forward DiffusionThe forward diffusion process adds noise to the latent representations of the protein sequences:\[ ext{noisy\_latents} = ext{latents} + \sigma \cdot \epsilon \]where:- \(\sigma\) is the noise level.- \(\epsilon \sim \mathcal{N}(0, 1)\) is Gaussian noise.### Reverse DiffusionThe reverse diffusion process denoises the latent representations:\[ ext{denoised\_latents} = ext{backbone}( ext{noisy\_latents}, \sigma) \]where the backbone model predicts the denoised latent representations.### Loss FunctionThe loss function used to train the model is the Mean Squared Error (MSE) between the denoised latents and the original latents:\[ \mathcal{L} = ext{MSE}( ext{denoised\_latents}, ext{latents}) \]## TrainingTo train the model, run the `train.py` script:```bashpython scripts/train.py```This script will:- Load the ESM-2-650M model and tokenizer from Hugging Face.- Prepare the data loaders for training and validation datasets.- Initialize the latent diffusion model.- Train the model using the specified configurations.## TestingTo test the model, run the `test.py` script:```bashpython scripts/test.py```This script will:- Load the trained model from the checkpoint.- Prepare the data loader for the test dataset.- Evaluate the model on the test dataset.## Generating Protein SequencesTo generate protein sequences, use the `generate.py` script. This script supports three strategies:1. **Generating a Scaffold to Connect Multiple Peptides**:```bashpython scripts/generate.py scaffold <peptide1> <peptide2> ... <final_length>```Example:```bashpython scripts/generate.py scaffold MKTAYIAKQRQ GLIEVQ 30```2. **Filling in Specified Regions in a Given Protein Sequence**:```bashpython scripts/generate.py fill <sequence_with_X>```Example:```bashpython scripts/generate.py fill MKTAYIAKXXXXXXXLEERLGLIEVQ```3. **Purely De Novo Generation of a Protein Sequence**:```bashpython scripts/generate.py de_novo <sequence_length>```Example:```bashpython scripts/generate.py de_novo 50```## Notes- Ensure you have a compatible CUDA environment if you are training on GPUs.- Modify the paths and configurations in `configs/config.py` as needed to match your setup.## AcknowledgementsThis implementation is based on the MDLM framework and uses the ESM-2-650M model.
12345678910111213141516171819### configs/config.py
```pythonclass Config: model_name = "facebook/esm2_t33_650M_UR50D" latent_dim = 1280 # Adjust based on ESM-2 latent dimension optim = {"lr": 1e-4} training = { "ema": 0.999, "epochs": 10, "batch_size": 32, "gpus": 8, "precision": 16, # Mixed precision training "accumulate_grad_batches": 2, # Gradient accumulation "save_dir": "./checkpoints/", } data_path = "./data/" T = 1000 # Number of diffusion steps subs_masking = False12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import itertoolsimport mathimport torchimport torch.nn.functional as Fimport pytorch_lightning as Limport torchmetricsfrom dataclasses import dataclassfrom models import dit, emaimport noise_schedule # Assuming this is part of the MDLM repository
LOG2 = math.log(2)
@dataclassclass Loss: loss: torch.FloatTensor nlls: torch.FloatTensor token_mask: torch.FloatTensor
class NLL(torchmetrics.MeanMetric): pass
class BPD(NLL): def compute(self) -> torch.Tensor: """Computes the bits per dimension.
Returns: bpd """ return self.mean_value / self.weight / LOG2
class Perplexity(NLL): def compute(self) -> torch.Tensor: """Computes the Perplexity.
Returns: Perplexity """ return torch.exp(self.mean_value / self.weight)
class Diffusion(L.LightningModule): def __init__(self, config, latent_dim): super().__init__() self.config = config self.latent_dim = latent_dim
self.backbone = dit.DIT(config, vocab_size=self.latent_dim) self.T = self.config.T self.subs_masking = self.config.subs_masking
self.softplus = torch.nn.Softplus() metrics = torchmetrics.MetricCollection({ 'nll': NLL(), 'bpd': BPD(), 'ppl': Perplexity(), }) metrics.set_dtype(torch.float64) self.train_metrics = metrics.clone(prefix='train/') self.valid_metrics = metrics.clone(prefix='val/') self.test_metrics = metrics.clone(prefix='test/')
self.noise = noise_schedule.get_noise(self.config, dtype=self.dtype) self.lr = self.config.optim["lr"] self.sampling_eps = self.config.training.get("sampling_eps", 1e-5) self.time_conditioning = self.config.get("time_conditioning", True) self.neg_infinity = -1000000.0
def forward(self, latents, sigma): """Forward diffusion process, adds noise to the latents.""" noise = sigma * torch.randn_like(latents) noisy_latents = latents + noise return noisy_latents
def reverse_diffusion(self, noisy_latents, sigma): """Reverse diffusion process, denoises the latents.""" denoised_latents = self.backbone(noisy_latents, sigma) return denoised_latents
def training_step(self, batch, batch_idx): sigma = torch.rand(batch.size(0), device=self.device) noisy_latents = self.forward(batch, sigma) denoised_latents = self.reverse_diffusion(noisy_latents, sigma) loss = F.mse_loss(denoised_latents, batch) self.log("train_loss", loss) return loss
def configure_optimizers(self): optimizer = torch.optim.Adam(self.parameters(), lr=self.lr) return optimizer12345678910111213torch==1.10.0torchvision==0.11.1torchaudio==0.10.0pytorch-lightning==1.5.10transformers==4.12.3pandas==1.3.4numpy==1.21.4scipy==1.7.3scikit-learn==1.0.1tqdm==4.62.3omegaconf==2.1.1hydra-core==1.1.1torchmetrics==0.6.2123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131import torchimport numpy as npfrom transformers import AutoTokenizer, AutoModelfrom models.diffusion import Diffusionfrom configs.config import Configfrom utils.esm_utils import load_esm2_model, get_latents
def mask_sequence(sequence, mask_char='X'): """Masks parts of the sequence based on the mask_char.""" mask_indices = [i for i, char in enumerate(sequence) if char == mask_char] masked_sequence = sequence.replace(mask_char, '[MASK]') return masked_sequence, mask_indices
def generate_filled_sequence(model, tokenizer, esm_model, masked_sequence, mask_indices): """Generates the filled sequence for the masked regions.""" inputs = tokenizer(masked_sequence, return_tensors="pt") with torch.no_grad(): outputs = esm_model(**inputs) latents = outputs.last_hidden_state.squeeze(0) sigma = torch.rand(1, device=latents.device) noisy_latents = model.forward(latents, sigma) denoised_latents = model.reverse_diffusion(noisy_latents, sigma) filled_sequence = list(masked_sequence) for idx in mask_indices: token_id = torch.argmax(denoised_latents[idx]).item() filled_sequence[idx] = tokenizer.decode([token_id]) return ''.join(filled_sequence)
def generate_scaffold_sequence(model, tokenizer, esm_model, peptides, final_length): """Generates a scaffold sequence to connect multiple peptides.""" total_peptide_length = sum(len(peptide) for peptide in peptides) scaffold_length = final_length - total_peptide_length if scaffold_length <= 0: raise ValueError("Final length must be greater than the combined length of the peptides.") scaffold = "[MASK]" * scaffold_length masked_sequence = "".join(peptides[:1] + [scaffold] + peptides[1:]) inputs = tokenizer(masked_sequence, return_tensors="pt") with torch.no_grad(): outputs = esm_model(**inputs) latents = outputs.last_hidden_state.squeeze(0) sigma = torch.rand(1, device=latents.device) noisy_latents = model.forward(latents, sigma) denoised_latents = model.reverse_diffusion(noisy_latents, sigma) filled_sequence = list(masked_sequence) scaffold_start = len(peptides[0]) scaffold_end = scaffold_start + scaffold_length for idx in range(scaffold_start, scaffold_end): token_id = torch.argmax(denoised_latents[idx]).item() filled_sequence[idx] = tokenizer.decode([token_id]) return ''.join(filled_sequence)
def generate_de_novo_sequence(model, tokenizer, esm_model, sequence_length): """Generates a de novo protein sequence of the specified length.""" scaffold = "[MASK]" * sequence_length masked_sequence = scaffold inputs = tokenizer(masked_sequence, return_tensors="pt") with torch.no_grad(): outputs = esm_model(**inputs) latents = outputs.last_hidden_state.squeeze(0) sigma = torch.rand(1, device=latents.device) noisy_latents = model.forward(latents, sigma) denoised_latents = model.reverse_diffusion(noisy_latents, sigma) filled_sequence = list(masked_sequence) for idx in range(sequence_length): token_id = torch.argmax(denoised_latents[idx]).item() filled_sequence[idx] = tokenizer.decode([token_id]) return ''.join(filled_sequence)
if __name__ == "__main__": import argparse
# Argument parsing parser = argparse.ArgumentParser(description="Generate protein sequences using latent diffusion model.") subparsers = parser.add_subparsers(dest="mode")
# Subparser for the first strategy (multiple peptides to scaffold) parser_scaffold = subparsers.add_parser("scaffold", help="Generate scaffold to connect multiple peptides.") parser_scaffold.add_argument("peptides", nargs='+', help="Peptides to connect.") parser_scaffold.add_argument("final_length", type=int, help="Final length of the protein sequence.")
# Subparser for the second strategy (fill in regions) parser_fill = subparsers.add_parser("fill", help="Fill in specified regions in a given protein sequence.") parser_fill.add_argument("sequence", help="Protein sequence with regions to fill specified by 'X'.")
# Subparser for the third strategy (de novo generation) parser_de_novo = subparsers.add_parser("de_novo", help="Generate a de novo protein sequence.") parser_de_novo.add_argument("sequence_length", type=int, help="Length of the de novo generated protein sequence.")
args = parser.parse_args()
# Load configurations config = Config()
# Load models tokenizer, esm_model = load_esm2_model(config.model_name) diffusion_model = Diffusion.load_from_checkpoint(config.training["save_dir"] + "example.ckpt", config=config, latent_dim=config.latent_dim) diffusion_model.eval()
if args.mode == "scaffold": peptides = args.peptides final_length = args.final_length filled_sequence = generate_scaffold_sequence(diffusion_model, tokenizer, esm_model, peptides, final_length) print(f"Peptides: {' '.join(peptides)}") print(f"Final Length: {final_length}") print(f"Generated Protein: {filled_sequence}")
elif args.mode == "fill": sequence = args.sequence masked_sequence, mask_indices = mask_sequence(sequence) filled_sequence = generate_filled_sequence(diffusion_model, tokenizer, esm_model, masked_sequence, mask_indices) print(f"Original Sequence: {sequence}") print(f"Masked Sequence: {masked_sequence}") print(f"Filled Sequence: {filled_sequence}")
elif args.mode == "de_novo": sequence_length = args.sequence_length filled_sequence = generate_de_novo_sequence(diffusion_model, tokenizer, esm_model, sequence_length) print(f"De Novo Sequence Length: {sequence_length}") print(f"Generated Protein: {filled_sequence}")1234567891011121314151617import pytorch_lightning as Lfrom configs.config import Configfrom utils.data_loader import get_dataloadersfrom models.diffusion import Diffusion
# Get dataloaders_, _, test_loader = get_dataloaders(Config)
# Initialize modelcheckpoint_path = Config.training["save_dir"] + "example.ckpt"latent_diffusion_model = Diffusion.load_from_checkpoint(checkpoint_path, config=Config, latent_dim=Config.latent_dim)
# Initialize trainertrainer = L.Trainer(gpus=Config.training["gpus"], precision=Config.training["precision"])
# Test the modeltrainer.test(latent_diffusion_model, test_loader)123456789101112131415161718192021222324import pytorch_lightning as Lfrom pytorch_lightning.strategies import DDPStrategyfrom configs.config import Configfrom utils.data_loader import get_dataloadersfrom models.diffusion import Diffusion
# Get dataloaderstrain_loader, val_loader, _ = get_dataloaders(Config)
# Initialize modellatent_diffusion_model = Diffusion(Config, latent_dim=Config.latent_dim)
# Initialize trainertrainer = L.Trainer( max_epochs=Config.training["epochs"], gpus=Config.training["gpus"], precision=Config.training["precision"], strategy=DDPStrategy(find_unused_parameters=False), accumulate_grad_batches=Config.training["accumulate_grad_batches"], default_root_dir=Config.training["save_dir"])
# Train the modeltrainer.fit(latent_diffusion_model, train_loader, val_loader)123456789101112131415161718192021222324252627282930import pandas as pdfrom torch.utils.data import Dataset, DataLoaderfrom utils.esm_utils import get_latents, load_esm2_model
class ProteinDataset(Dataset): def __init__(self, csv_file, tokenizer, model): self.data = pd.read_csv(csv_file) self.tokenizer = tokenizer self.model = model
def __len__(self): return len(self.data)
def __getitem__(self, idx): sequence = self.data.iloc[idx]['sequence'] latents = get_latents(self.model, self.tokenizer, sequence) return latents
def get_dataloaders(config): tokenizer, model = load_esm2_model(config.model_name) train_dataset = ProteinDataset(config.data_path + "train.csv", tokenizer, model) val_dataset = ProteinDataset(config.data_path + "val.csv", tokenizer, model) test_dataset = ProteinDataset(config.data_path + "test.csv", tokenizer, model) train_loader = DataLoader(train_dataset, batch_size=config.training["batch_size"], shuffle=True) val_loader = DataLoader(val_dataset, batch_size=config.training["batch_size"], shuffle=False) test_loader = DataLoader(test_dataset, batch_size=config.training["batch_size"], shuffle=False) return train_loader, val_loader, test_loader12345678910111213import torchfrom transformers import AutoTokenizer, AutoModel
def load_esm2_model(model_name): tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModel.from_pretrained(model_name) return tokenizer, model
def get_latents(model, tokenizer, sequence): inputs = tokenizer(sequence, return_tensors="pt") with torch.no_grad(): outputs = model(**inputs) return outputs.last_hidden_state.squeeze(0)README.md+172-3
configs/config.pyAdded+19
data/test.csvAdded
data/train.csvAdded
data/val.csvAdded
models/diffusion.pyAdded+88
requirements.txtAdded+13
scripts/generate.pyAdded+131
scripts/test.pyAdded+17
scripts/train.pyAdded+24
test.csvAdded
train.csvAdded
utils/data_loader.pyAdded+30
utils/esm_utils.pyAdded+13
val.csvAdded