ImmuneBuilder

repository·main·Indexed 41 days ago

https://github.com/oxpig/immunebuilder

A suite of deep learning models for high-speed, accurate structural prediction of immune proteins. It includes specialized predictors such as ABodyBuilder2 for antibodies, NanoBodyBuilder2 for nanobodies, and TCRBuilder2 for T-cell receptors (TCRs). The toolkit provides both a Python API and a Command Line Interface (CLI) for predictions, utilizing dependencies like PyTorch, OpenMM, and ANARCI for sequence numbering and structural refinement.

Tokens
2.7K
Snippets
8
Records
10
Agent score
2%

What's inside ImmuneBuilder

  1. Install ImmuneBuilder

    main

    ImmuneBuilder requires several dependencies to be installed in a single environment before installing the package itself.

    Prerequisites

    1. PyTorch: Install via the official instructions.
    2. OpenMM and pdbfixer: Required for the refinement step. Install via conda:
      conda install -c conda-forge openmm pdbfixer
    3. ANARCI: Used for trimming and numbering sequences. Install via conda:
      conda install -c bioconda anarci

    Installation

    Once dependencies are ready, install ImmuneBuilder via PyPI:

    pip install ImmuneBuilder
    conda install -c conda-forge openmm pdbfixer
    conda install -c bioconda anarci
    pip install ImmuneBuilder
  2. Install ImmuneBuilder and dependencies

    main

    To use ImmuneBuilder, you must install the core package along with py3Dmol for visualization. The environment also requires conda for managing specialized bioinformatics and molecular dynamics tools like ANARCI (for sequence numbering) and OpenMM (for Amber refinement).

    In a notebook environment, the following commands are used to set up the environment:

    # Install core packages
    pip install ImmuneBuilder
    pip install py3Dmol
    
    # Install OpenMM for Amber refinement via conda
    conda install -y -q -c conda-forge openmm=7.7.0 pdbfixer
    
    # Install ANARCI and related tools via bioconda
    conda install -y -q anarci hmmer biopython -c bioconda --no-deps --solver=classic
    # Note: The notebook uses a bash script to automate this.
    # pip install ImmuneBuilder
    # pip install py3Dmol
    # conda install -y -q -c conda-forge openmm=7.7.0 pdbfixer
    # conda install -y -q anarci hmmer biopython -c bioconda --no-deps --solver=classic
  3. Troubleshoot ImmuneBuilder installation issues

    main

    GLIBCXX Error

    If you encounter Import Error: 'GLIBCXX_3.4.30' not found after installation, this is an OpenMM-related issue. Resolve it by installing libstdcxx-ng via conda:

    conda install -c conda-forge libstdcxx-ng

    CUDA Toolkit Compatibility

    Installing OpenMM via conda may automatically download a cudatoolkit version that is incompatible with your hardware. Verify your device compatibility if you encounter GPU-related errors.

  4. Predict Antibody structures with ABodyBuilder2

    main

    You can predict antibody structures using either the Python API or the Command Line Interface (CLI). Using the Python API is recommended for batch processing as it avoids reloading models for every prediction.

    Python API

    Use the ABodyBuilder2 class. The predict method accepts a dictionary of sequences where keys are chain identifiers (e.g., 'H' for heavy, 'L' for light).

    CLI

    Run the ABodyBuilder2 command directly on a FASTA file.

    ABodyBuilder2 --fasta_file my_antibody.fasta -v
    from ImmuneBuilder import ABodyBuilder2
    predictor = ABodyBuilder2()
    
    output_file = "my_antibody.pdb"
    sequences = {
      'H': 'EVQLVESGGGVVQPGGSLRLSCAASGFTFNSYGMHWVRQAPGKGLEWVAFIRYDGGNKYYADSVKGRFTISRDNSKNTLYLQMKSLRAEDTAVYYCANLKDSRYSGSYYDYWGQGTLVTVS',
      'L': 'VIWMTQSPSSLSASVGDRVTITCQASQDIRFYLNWYQQKPGKAPKLLISDASNMETGVPSRFSGSGTDFTFTISSLQPEDIATYYCQQYDNLPFTFGPGTKVDFK'
    }
    
    antibody = predictor.predict(sequences)
    antibody.save(output_file)
  5. Predict TCR structures with TCRBuilder2

    main

    Predict T-Cell receptor structures using the TCRBuilder2 class.

    Note on Weights: By default, TCRBuilder2 uses the updated TCRBuilder2+ weights. To use the original weights, set use_TCRBuilder2_PLUS_weights=False in the Python constructor or use the --original_weights flag in the CLI.

    Python API

    The predict method accepts a dictionary of sequences where keys are chain identifiers (typically 'A' for alpha and 'B' for beta).

    CLI

    TCRBuilder2 --fasta_file my_tcr.fasta -v
    from ImmuneBuilder import TCRBuilder2
    predictor = TCRBuilder2()
    
    output_file = "my_tcr.pdb"
    sequences = {
    "A": "AQSVTQLGSHVSVSEGALVLLRCNYSSSVPPYLFWYVQYPNQGLQLLLKYTSAATLVKGINGFEAEFKKSETSFHLTKPSAHMSDAAEYFCAVSEQDDKIIFGKGTRLHILP",
    "B": "ADVTQTPRNRITKTGKRIMLECSQTKGHDRMYWYRQDPGLGLRLIYYSFDVKDINKGEISDGYSVSRQAQAKFSLSLESAIPNQTALYFCATSDESYGYTFGSGTRLTVV"
    }
    
    tcr = predictor.predict(sequences)
    tcr.save(output_file)
  6. Predict Nanobody structures with NanoBodyBuilder2

    main

    Predict nanobody structures using the NanoBodyBuilder2 class via Python or the CLI.

    Python API

    The predict method accepts a dictionary containing the heavy chain sequence (key 'H').

    CLI

    NanoBodyBuilder2 --fasta_file my_nanobody.fasta -v
    from ImmuneBuilder import NanoBodyBuilder2
    predictor = NanoBodyBuilder2()
    
    output_file = "my_nanobody.pdb"
    sequence = {'H': 'QVQLVESGGGLVQPGESLRLSCAASGSIFGIYAVHWFRMAPGKEREFTAGFGSHGSTNYAASVKGRFTMSRDNAKNTTYLQMNSLKPADTAVYYCHALIKNELGFLDYWGPGTQVTVSS'}
    
    nanobody = predictor.predict(sequence)
    nanobody.save(output_file)
  7. Format FASTA files for ImmuneBuilder

    main

    When running models via the CLI, your FASTA file must follow specific labeling conventions based on the protein type:

    Antibodies

    Use >H for the heavy chain and >L for the light chain.

    >H
    SEQUENCE
    >L
    SEQUENCE

    Nanobodies

    Use only the heavy chain labeled >H.

    >H
    SEQUENCE

    TCRs

    Use >A for the alpha chain and >B for the beta chain.

    >A
    SEQUENCE
    >B
    SEQUENCE
    >H
    YOURHEAVYCHAINSEQUENCE
    >L
    YOURLIGHCHAINSEQUENCE
  8. Troubleshoot ImmuneBuilder predictions

    main

    If you encounter issues with the prediction pipeline, check the following:

    • Sequence Validity: Ensure your input sequences are specifically antibodies, nanobodies, or TCRs. ImmuneBuilder is not designed for general protein structure prediction.
    • Hardware Acceleration: Ensure your runtime is set to use a GPU (e.g., in Google Colab: Runtime -> Change runtime type -> T4 GPU).
    • Environment Errors: If the environment is corrupted, perform a Runtime -> Factory reset runtime to clear all installed dependencies and start fresh.
    • Missing Chains: If you receive a KeyError, verify that the anarci.number step correctly identified the chain IDs for the sequences provided.
  9. Visualize predicted structures with py3Dmol

    main

    Once a structure is predicted and saved as a .pdb file, you can visualize it using py3Dmol. The notebook implementation supports several coloring modes:

    • chain: Colors chains specifically (e.g., Heavy chain 'H' as purple, Light chain 'L' as green).
    • rainbow: Uses a standard spectrum coloring.
    • predicted_error: Colors the structure by the B-factor, which represents the residue-level error estimate (calculated by comparing model agreement). A gradient from red to blue (roygb) is used, where the scale is mapped from min: 5 to max: 0.

    You can also toggle the visibility of sidechains and mainchains.

    import py3Dmol
    
    view = py3Dmol.view()
    view.addModel(open('ImmuneBuilder_model.pdb', 'r').read(), 'pdb')
    view.zoomTo()
    view.setBackgroundColor('white')
    
    # Example: Color by predicted error (B-factor)
    view.setStyle({'cartoon': {'colorscheme': {'prop':'b','gradient': 'roygb','min':5,'max':0}}})
    
    # Example: Show sidechains
    view.addStyle({'and':[{'resn':["GLY","PRO"],'invert':True},{'atom':['C','O','N'],'invert':True}]},
                  {'stick':{'colorscheme':'WhiteCarbon','radius':0.3}})
    
    view.show()
  10. Predict structures for Antibodies, Nanobodies, or TCRs

    main

    ImmuneBuilder provides three distinct predictor classes depending on the type of immune protein you are modeling. You must first instantiate the appropriate predictor and then pass a dictionary where keys are the chain identifiers (determined via anarci.number) and values are the amino acid sequences.

    Predictor Classes

    • ABodyBuilder2(): For Antibodies.
    • NanoBodyBuilder2(): For Nanobodies.
    • TCRBuilder2(): For T-Cell Receptors.

    Usage Pattern

    1. Identify chain IDs using anarci.number(sequence).
    2. Construct an input dictionary: {chain_id: sequence}.
    3. Call .predict(input) on the predictor instance.
    4. Use .save(filename) to export the result as a PDB file.
    from ImmuneBuilder import ABodyBuilder2, NanoBodyBuilder2, TCRBuilder2
    from anarci import number
    
    # 1. Initialize predictor
    predictor = ABodyBuilder2()
    
    # 2. Prepare input (example for antibody with two chains)
    _, chain1 = number(sequence_1)
    _, chain2 = number(sequence_2)
    
    input_dict = {}
    if chain1: input_dict[chain1] = sequence_1
    if chain2: input_dict[chain2] = sequence_2
    
    # 3. Predict and save
    try:
        predictor.predict(input_dict).save('output.pdb')
    except KeyError as e:
        print(f"ERROR: Missing sequence for chain {str(e)}")