### Install HMMER from Source Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Downloads, compiles, and installs HMMER version 3.4. This process involves creating build directories, extracting the tarball, configuring the build with a specified prefix, and then compiling and installing. ```bash mkdir /path/to/hmmer_build /path/to/hmmer wget http://eddylab.org/software/hmmer/hmmer-3.4.tar.gz -P /path/to/hmmer_build cd /path/to/hmmer_build tar -zxf hmmer-3.4.tar.gz cd hmmer-3.4 ./configure --prefix=/path/to/hmmer make -j8 make install ``` -------------------------------- ### Install AlphaFold 3.0.1 and Dependencies Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Clones the AlphaFold 3 repository, checks out version 3.0.1, installs development requirements, and then installs AlphaFold itself. It also includes a step to build necessary data. ```bash git clone https://github.com/google-deepmind/alphafold3.git cd alphafold3 git checkout v3.0.1 pip install -r dev-requirements.txt pip install --no-deps . build_data ``` -------------------------------- ### Install GCC and G++ Compilers Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Installs necessary GNU compiler tools (g++, gcc) for Linux systems, which are often required for compiling C++ dependencies. ```bash conda install gxx_linux-64 gxx_impl_linux-64 gcc_linux-64 gcc_impl_linux-64=13.2.0 ``` -------------------------------- ### Install TorchFold Inference Package Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Clones the TorchFold repository, navigates to the inference package directory, and installs it using pip. This makes the TorchFold inference functionality available. ```bash git clone https://github.com/Mingchenchen/TorchFold.git cd TorchFold/TorchFold_inference/torchfold pip install . ``` -------------------------------- ### Download AlphaFold Databases Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Executes a script to download the required databases for AlphaFold. The script takes a directory path as an argument where the databases will be stored. ```bash bash fetch_databases.sh /path/to/database/dir cd .. ``` -------------------------------- ### Install TorchFold and Dependencies (Bash) Source: https://context7.com/mingchenchen/torchfold/llms.txt Sets up the Conda environment, installs necessary compiler dependencies, HMMER for sequence alignment, AlphaFold 3.0.1 for the data pipeline, and finally TorchFold itself. This process includes downloading databases required for feature preparation. ```bash # Create and activate conda environment conda create -n TorchFold_inference_env python=3.11 conda activate TorchFold_inference_env # Install compiler dependencies conda install gxx_linux-64 gxx_impl_linux-64 gcc_linux-64 gcc_impl_linux-64=13.2.0 # Install HMMER for sequence alignment mkdir ~/hmmer_build ~/hmmer wget http://eddylab.org/software/hmmer/hmmer-3.4.tar.gz -P ~/hmmer_build cd ~/hmmer_build && tar -zxf hmmer-3.4.tar.gz cd hmmer-3.4 && ./configure --prefix=$HOME/hmmer && make -j8 && make install # Install AlphaFold 3.0.1 (required for data pipeline) git clone https://github.com/google-deepmind/alphafold3.git cd alphafold3 && git checkout v3.0.1 pip install -r dev-requirements.txt && pip install --no-deps . build_data # Download databases bash fetch_databases.sh ~/public_databases # Install TorchFold git clone https://github.com/Mingchenchen/TorchFold.git cd TorchFold/TorchFold_inference/torchfold pip install . ``` -------------------------------- ### Create Conda Environment for TorchFold Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Creates a new Conda environment named 'TorchFold_inference_env' with Python 3.11 and activates it. This isolates project dependencies. ```bash conda create -n TorchFold_inference_env python=3.11 conda activate TorchFold_inference_env ``` -------------------------------- ### Run TorchFold Inference Source: https://github.com/mingchenchen/torchfold/blob/main/readme.md Executes the inference process using a bash script. Before running, it's necessary to configure paths in `scripts/env.sh` and `run.sh` to match your environment. ```bash bash run.sh ``` -------------------------------- ### Structure Input Features with TorchFold Batch Dataclass Source: https://context7.com/mingchenchen/torchfold/llms.txt The `Batch` dataclass in `torchfold.feat_batch` organizes input features for model inference. This example demonstrates creating a `Batch` object from a dictionary of PyTorch tensors and accessing its structured features. ```python from torchfold.feat_batch import Batch from torchfold import features import torch # Create batch from dictionary of tensors batch_dict = { 'msa': torch.randint(0, 21, (512, 100)), # MSA sequences 'msa_mask': torch.ones(512, 100), # MSA mask 'deletion_matrix': torch.zeros(512, 100), # Deletion info 'profile': torch.randn(100, 21), # Sequence profile 'deletion_mean': torch.zeros(100), # Mean deletions 'num_alignments': torch.tensor(512), # MSA depth 'aatype': torch.randint(0, 21, (100,)), # Residue types 'residue_index': torch.arange(100), # Residue indices 'token_index': torch.arange(100), # Token indices 'seq_mask': torch.ones(100), # Sequence mask 'asym_id': torch.ones(100, dtype=torch.int32), # Chain ID 'entity_id': torch.ones(100, dtype=torch.int32), # Entity ID 'sym_id': torch.ones(100, dtype=torch.int32), # Symmetry ID 'seq_length': torch.tensor(100), # Sequence length # ... additional required features } # Convert to Batch object batch = Batch.from_data_dict(batch_dict) # Access structured features num_residues = batch.num_res # 100 msa_data = batch.msa # MSA dataclass token_features = batch.token_features # Token features template_data = batch.templates # Template features # Convert back to dictionary output_dict = batch.as_data_dict() ``` -------------------------------- ### Process Embeddings with TorchFold Evoformer Module Source: https://context7.com/mingchenchen/torchfold/llms.txt The `Evoformer` module in `torchfold.alphafold3` processes MSA and pair representations using attention and triangular multiplicative updates. This example shows initializing the module and performing a forward pass with batch data and previous embeddings. ```python from torchfold.alphafold3 import Evoformer import torch # Initialize Evoformer with default parameters evoformer = Evoformer(msa_channel=64) # Evoformer configuration: # - msa_channel: 64 (MSA embedding dimension) # - seq_channel: 384 (single representation dimension) # - pair_channel: 128 (pair representation dimension) # - msa_stack_num_layer: 4 (Evoformer MSA blocks) # - pairformer_num_layer: 48 (Pairformer trunk blocks) # Initialize embeddings num_res = 100 prev_embeddings = { 'pair': torch.zeros(num_res, num_res, 128), 'single': torch.zeros(num_res, 384), } target_feat = torch.randn(num_res, 447) # Target features # Forward pass output = evoformer( batch=batch, # Batch object with all features prev=prev_embeddings, # Previous cycle embeddings target_feat=target_feat, # Target feature embedding idx=0 # Recycle iteration index ) # Output contains updated embeddings single_embedding = output['single'] # Shape: [num_res, 384] pair_embedding = output['pair'] # Shape: [num_res, num_res, 128] ``` -------------------------------- ### Initialize and Use AlphaFold3 Model (Python) Source: https://context7.com/mingchenchen/torchfold/llms.txt Demonstrates how to initialize the `AlphaFold3` model, load weights either from JAX parameters or a PyTorch checkpoint, move the model to a GPU, and perform inference. The forward pass returns diffusion samples, distogram, and confidence metrics. ```python import torch from torchfold.alphafold3 import AlphaFold3 from torchfold.params import import_jax_weights_ import pathlib # Initialize the model with configuration model = AlphaFold3( num_recycles=10, # Number of recycling iterations num_samples=5, # Number of diffusion samples to generate diffusion_steps=200 # Number of diffusion denoising steps ) # Load JAX weights from official AlphaFold 3 parameters model_dir = pathlib.Path("~/models/model_103275239_1") import_jax_weights_(model, model_dir) # Or load from a PyTorch checkpoint checkpoint = torch.load("checkpoint.pt", map_location="cuda") model.load_state_dict(checkpoint.get('model_state_dict', checkpoint)) # Move model to GPU and set to evaluation mode device = torch.device('cuda') model = model.to(device=device) model.eval() # Forward pass returns diffusion samples, distogram, and confidence metrics with torch.inference_mode(): result = model(batch_dict) # result contains: # - 'diffusion_samples': {'atom_positions': tensor, 'mask': tensor} # - 'distogram': {'bin_edges': tensor, 'contact_probs': tensor} # - 'predicted_lddt': per-atom confidence scores # - 'full_pae': predicted aligned error matrix # - 'tmscore_adjusted_pae_global': global TM-score adjusted PAE ``` -------------------------------- ### Initialize and Access Template Features in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt Shows how to initialize Template features, including aatype, atom_positions, and atom_mask, using PyTorch tensors. It also demonstrates accessing an individual template from the collection. ```python import torch from torchfold.data.templates import Templates templates = Templates( aatype=torch.randint(0, 21, (4, 100)), # 4 templates atom_positions=torch.randn(4, 100, 24, 3), # Template coordinates atom_mask=torch.ones(4, 100, 24, dtype=torch.bool) ) first_template = templates[0] ``` -------------------------------- ### Run Inference with ModelRunner (Python) Source: https://context7.com/mingchenchen/torchfold/llms.txt Shows how to use the `ModelRunner` class to manage model initialization, execute inference on featurised input data, and extract predicted structures along with confidence metrics. The `ModelRunner` simplifies the inference pipeline. ```python from run_alphafold import ModelRunner, predict_structure, write_outputs import pathlib import torch # Initialize ModelRunner with model directory model_runner = ModelRunner( model_dir=pathlib.Path("~/models/model_103275239_1"), device=torch.device('cuda') ) # Run inference on a featurised example featurised_example = { 'msa': msa_tensor, 'msa_mask': msa_mask_tensor, 'aatype': aatype_tensor, 'residue_index': residue_index_tensor, # ... other required features } with torch.inference_mode(): result = model_runner.run_inference(featurised_example) # result contains model outputs including atom positions and confidence scores # Extract structure from model outputs inference_results = model_runner.extract_structures( batch=featurised_example, result=result, target_name="my_protein" ) # Each inference_result contains: # - Predicted atom coordinates # - Confidence metrics (pLDDT, PAE) # - Ranking scores for sample selection ``` -------------------------------- ### Initialize Token Features for Sequence in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt Illustrates the initialization of TokenFeatures for a sequence, including various attributes like residue_index, token_index, aatype, and masks. This is fundamental for representing sequence information in TorchFold. ```python import torch from torchfold.data.templates import TokenFeatures token_features = TokenFeatures( residue_index=torch.arange(100), token_index=torch.arange(100), aatype=torch.randint(0, 21, (100,)), mask=torch.ones(100), seq_length=torch.tensor(100), asym_id=torch.ones(100, dtype=torch.int32), entity_id=torch.ones(100, dtype=torch.int32), sym_id=torch.ones(100, dtype=torch.int32), is_protein=torch.ones(100, dtype=torch.bool), is_rna=torch.zeros(100, dtype=torch.bool), is_dna=torch.zeros(100, dtype=torch.bool), is_ligand=torch.zeros(100, dtype=torch.bool), is_nonstandard_polymer_chain=torch.zeros(100, dtype=torch.bool), is_water=torch.zeros(100, dtype=torch.bool) ) ``` -------------------------------- ### Import JAX Weights into PyTorch Model with TorchFold Source: https://context7.com/mingchenchen/torchfold/llms.txt This snippet illustrates how to load official AlphaFold 3 JAX parameters into a PyTorch model using TorchFold. It covers fetching parameters from a binary file, inspecting their keys and shapes, and then importing them into a PyTorch model, handling necessary weight transformations. ```python from torchfold.params import ( import_jax_weights_, get_alphafold3_params, select_model_files ) import pathlib # Load JAX parameters from binary file model_dir = pathlib.Path("~/models/model_103275239_1") params = get_alphafold3_params(model_dir / "af3.bin.zst") # Inspect parameter keys print(f"Total parameters: {len(params)}") for key in list(params.keys())[:5]: print(f" {key}: {params[key].shape}") # Import weights into PyTorch model model = AlphaFold3(num_samples=5) import_jax_weights_(model, model_dir) # The function handles: # - Weight transposition for Linear layers # - Multi-head attention reshaping # - Stacked layer parameter organization # - Fourier embedding initialization # Verify model identifier identifier = model.__identifier__ print(f"Model identifier: {identifier.tobytes().decode('ascii')}") ``` -------------------------------- ### Perform Distributed Inference Across Multiple GPUs with PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt This snippet demonstrates how to set up and run distributed inference across multiple GPUs using PyTorch's distributed package. It includes configuring the environment variables, initializing the process group, and running inference on a model distributed across ranks, followed by synchronization and cleanup. ```python import os import torch import torch.distributed as dist # Configure distributed environment os.environ["RANK"] = str(os.environ.get("PMI_RANK", 0)) os.environ["LOCAL_RANK"] = str(os.environ.get("MPI_LOCALRANKID", 0)) os.environ["WORLD_SIZE"] = str(os.environ.get("PMI_SIZE", 1)) os.environ["USE_DIST"] = "1" # Initialize process group backend = os.environ.get("USE_BACKEND", "nccl") # or "ccl" for Intel dist.init_process_group(backend, init_method="env://") rank = dist.get_rank() world_size = dist.get_world_size() # Each rank processes a subset of diffusion samples # For 5 samples across 5 GPUs: each GPU handles 1 complete sample # For 5 samples across 2 GPUs: work is split across timesteps # Run distributed inference model = AlphaFold3(num_samples=5, diffusion_steps=200) model = model.to(f'cuda:{rank}') with torch.inference_mode(): result = model(batch_dict) # Distributed diffusion sampling # Synchronize and cleanup dist.barrier() dist.destroy_process_group() ``` -------------------------------- ### Initialize and Truncate MSA Features in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt Demonstrates the initialization of MSA features using PyTorch tensors and the subsequent truncation of MSA rows. This is useful for selecting a subset of alignments for processing. ```python import torch from torchfold.data.templates import MSA msa = MSA( rows=torch.randint(0, 21, (512, 100)), mask=torch.ones(512, 100), deletion_matrix=torch.zeros(512, 100), profile=torch.randn(100, 21), deletion_mean=torch.zeros(100), num_alignments=torch.tensor(512) ) truncated_msa = msa.index_msa_rows(torch.arange(128)) ``` -------------------------------- ### Generate 3D Atomic Coordinates with DiffusionHead in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt This snippet demonstrates how to initialize and use the DiffusionHead for generating 3D atomic coordinates through iterative denoising. It covers noise schedule generation, initialization of noisy positions, a single denoising step, and applying random augmentation for training or sampling. ```python from torchfold.nn.diffusion_head import DiffusionHead, noise_schedule, random_augmentation import torch # Initialize diffusion head diffusion_head = DiffusionHead() # Channels: c_act=768, pair_channel=128, seq_channel=384 # Generate noise schedule num_steps = 200 t = torch.linspace(0, 1, num_steps + 1, device='cuda') noise_levels = noise_schedule(t) # Decreasing noise levels # Initialize noisy positions num_atoms = 100 * 24 # tokens × atoms_per_token atom_mask = torch.ones(100, 24, device='cuda') positions = torch.randn(100, 24, 3, device='cuda') * noise_levels[0] # Single denoising step embeddings = { 'single': single_embedding, 'pair': pair_embedding, 'target_feat': target_feat } denoised_positions = diffusion_head( positions_noisy=positions, noise_level=noise_levels[100], # Current noise level batch=batch, embeddings=embeddings, use_conditioning=True ) # Apply random augmentation for training/sampling augmented = random_augmentation( positions=denoised_positions, mask=atom_mask.flatten() ) ``` -------------------------------- ### Run TorchFold Inference Pipeline with Shell Scripts Source: https://context7.com/mingchenchen/torchfold/llms.txt This bash script configures environment variables and executes the TorchFold inference pipeline. It supports using either a TorchFold checkpoint or official JAX weights for prediction. The output structure details the generated files. ```bash # Configure environment variables in scripts/env.sh export JSON_PATH=/path/to/json/dir export OUTPUT_DIR=/path/to/output/dir export DB_DIR=/path/to/database/dir export PATH="/path/to/hmmer/bin:$PATH" export RUN_DATA_PIPELINE=true export NUM_DIFFUSION_SAMPLES=5 # Option 1: Use TorchFold checkpoint export CHECKPOINT_PATH=/path/to/checkpoint/file # Option 2: Use JAX official weights (comment out CHECKPOINT_PATH) # export MODEL_DIR=/path/to/AlphaFold3/parameters/dir # Run inference cd TorchFold/TorchFold_inference/torchfold bash run.sh test.json # Output structure: # output_dir/ # ── protein_complex/ # │ ── protein_complex_data.json # Input with MSA/templates # │ ── seed-1_sample-0/ # Per-sample outputs # │ │ ── model.cif # Predicted structure # │ │ ── confidence.json # Confidence metrics # │ ── seed-1_sample-1/ # ── ranking_scores.csv # Sample rankings # ── protein_complex_model.cif # Best-ranked structure ``` -------------------------------- ### Assess Structure Quality with ConfidenceHead and DistogramHead in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt This snippet shows how to initialize and use ConfidenceHead to predict structure quality metrics like pLDDT, PAE, and contact probabilities, and DistogramHead for distance predictions. It details the inputs required for the confidence head and the outputs generated by both heads. ```python from torchfold.nn.head import ConfidenceHead, DistogramHead import torch # Initialize confidence head confidence_head = ConfidenceHead( c_single=384, c_pair=128, c_target_feat=447, n_pairformer_layers=4 ) # Predict confidence metrics confidence_output = confidence_head( dense_atom_positions=predicted_positions, # [num_tokens, 24, 3] embeddings={ 'single': single_embedding, 'pair': pair_embedding, 'target_feat': target_feat }, seq_mask=sequence_mask, token_atoms_to_pseudo_beta=pseudo_beta_info, asym_id=chain_ids ) # Output metrics plddt = confidence_output['predicted_lddt'] # Per-atom confidence [0-100] pae = confidence_output['full_pae'] # Predicted aligned error matrix pde = confidence_output['full_pde'] # Predicted distance error global_pae = confidence_output['tmscore_adjusted_pae_global'] # TM-score adjusted # Distogram prediction distogram_head = DistogramHead(c_pair=128, num_bins=64) distogram = distogram_head(batch, embeddings) contact_probs = distogram['contact_probs'] # Contact probability matrix bin_edges = distogram['bin_edges'] # Distance bin boundaries ``` -------------------------------- ### Utilize Feature Dataclasses for Type-Safe Data Handling in PyTorch Source: https://context7.com/mingchenchen/torchfold/llms.txt This snippet shows the import of various feature dataclasses from TorchFold, such as MSA, Templates, TokenFeatures, RefStructure, PredictedStructureInfo, and BatchDict. These dataclasses are used for structured and type-safe handling of input features in PyTorch. ```python from torchfold.features import ( MSA, Templates, TokenFeatures, RefStructure, PredictedStructureInfo, BatchDict ) import torch ``` -------------------------------- ### Define Input JSON for AlphaFold 3 Structure Prediction Source: https://context7.com/mingchenchen/torchfold/llms.txt This JSON structure defines the input for AlphaFold 3 structure prediction, including protein sequences, ligand information, model seeds, and dialect settings. It is crucial for preparing data before running inference. ```json { "name": "protein_complex", "sequences": [ { "protein": { "id": "A", "sequence": "GMRESYANENQFGFKTINSDIHKIVIVGGYGHDHNMTYIQALRHFSTFANGLHLSKQPINNLAQ" } }, { "protein": { "id": "B", "sequence": "MKFLILLFNILCLFPVLAADNHGVGPQGASGVDPITFDINSNQTGVQLTLPLGFGMSCQNPSGNS" } }, { "ligand": { "id": "C", "smiles": "CC(=O)OC1=CC=CC=C1C(=O)O" } } ], "modelSeeds": [1, 42, 123], "dialect": "alphafold3", "version": 1 } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.