Metadata-Version: 2.4
Name: eqc-bio
Version: 0.1.2
Summary: Biological modeling package targeting EQC devices
Author-email: "Quantum Computing Inc." <support@quantumcomputinginc.com>
Project-URL: Homepage, https://quantumcomputinginc.com
Project-URL: Documentation, https://quantumcomputinginc.com/learn/support/software-packages/
Project-URL: Issues, https://support.quantumcomputinginc.com/
Requires-Python: <3.14,>=3.9
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Requires-Dist: numpy<3,>=1.22.1
Requires-Dist: networkx<3,>=2.6.3
Requires-Dist: pandas<3,>=2.1.0
Requires-Dist: eqc-models>=0.22.2
Requires-Dist: pyyaml<7,>=6.0
Requires-Dist: pymetis>=2023.1
Provides-Extra: dev
Requires-Dist: pytest<8,>=7.1.0; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: sphinx_rtd_theme<2,>=1.3.0; extra == "dev"
Provides-Extra: viewer
Requires-Dist: py3Dmol<3,>=2.0; extra == "viewer"
Requires-Dist: nglview<5,>=3.0; extra == "viewer"
Dynamic: license-file

# eqc_bio.protein_design

The `eqc_bio.protein_design` module provides tools for fixed-backbone protein design using quantum-inspired optimization on Dirac-3 solvers.

## Overview

Fixed-backbone protein design (also known as rotamer optimization or side-chain packing) involves selecting optimal side-chain conformations (rotamers) for a given protein backbone structure to minimize the total energy of the system. The `eqc_bio.protein_design` package formulates this combinatorial problem as a quadratic binary optimization (QUBO) model that can be solved efficiently using Quantum Computing Inc.'s Dirac-3 solvers.

## Key Components

### ProteinDesignFixed

The main class `ProteinDesignFixed` (in `fixed_backbone.py`) handles:
- Loading problem configurations from YAML files
- Reading precomputed energy matrices (interaction energies between rotamers)
- Constructing the QUBO Hamiltonian with linear and quadratic terms
- Running optimization on Dirac-3 (cloud or direct) solvers
- Iterative batch solving for larger problems using METIS graph partitioning
- Post-processing solutions into rotamer selections and PDB structures

Configuration parameters include:
- `n_samples`, relaxation schedule, photon number, quantum fluctuation coefficient
- Energy thresholds and scaling factors (alpha, beta)
- Support for integer vs. continuous variables and iterative refinement

### Supporting Modules

- `base.py`: Core `ProteinDesign` class with Dirac-3 execution logic and iterative batching
- `rotamers.py`: Rotamer library handling (Dunbrack, OSPREY formats), chi angle extraction, library parsing and validation
- `sidechains.py`: Side-chain heavy-atom construction from χ angles using ideal geometry
- `viewer.py`: PDB building (`build_designed_pdb`), visualization (`view_pdb`), backbone dihedrals and rotamer angle utilities

## Usage

Typical workflow:
1. Prepare a config YAML pointing to an energy matrix CSV and expected solution
2. Instantiate `ProteinDesignFixed(config_file, solver)`
3. Call `run()` to solve and obtain design DataFrame
4. Use `get_rotamer_angles()` and `build_pdb()` to generate output structures

Example scripts are provided in `scripts/protein_design_fixed_backbone.py` and `scripts/protein_design_expected_pdb.py`.

## Building and Viewing Designed Structures

A solution assigns a rotamer such as `MET-3` (the third MET rotamer) to each designed position. The structure tools turn that into a PDB in three steps:

1. **Rotamer angles**: look up the χ angles of each selected rotamer in the rotamer library the energy matrix was generated with.
2. **PDB building**: keep the template's backbone atoms (N, CA, C, O, OXT) at each designed position, rename the residue, and build its side-chain heavy atoms from the χ angles. Other residues are copied unchanged.
3. **Viewing**: show the PDB in Jupyter with py3Dmol or nglview.

### Configuration

The structure tools read these optional keys from the config YAML:

```yaml
template_pdb: data/1MJC/1MJC.pdb   # backbone the energy matrix was built on
rotamer_library: null              # null = OSPREY Lovell default, or a path to a library file
rotamer_library_format: osprey     # osprey (OSPREY .dat, e.g. Lovell) or dunbrack (Dunbrack bbdep file)
position_numbering: index          # index (1-based residue order in the file) or resseq (PDB residue number)
```

- `template_pdb` is needed to build or view a PDB, and also for a Dunbrack library.
- `rotamer_library` must be the library the energy matrix was generated with, because rotamer numbers index into it. The rotamer names in the matrix are checked against the library's rotamer counts. A name that exceeds the library's count raises an error, and a residue type that uses fewer rotamers than the library has gives a warning.
- `position_numbering: index` matches how OSPREY energy matrices number positions. Use `resseq` when positions are PDB residue numbers.

### Rotamer Libraries

- **Lovell (default)**: the Lovell "Penultimate" library as distributed with OSPREY (`LovellRotamer.dat`). It is downloaded from a pinned OSPREY commit on first use and cached in `~/.eqc_bio/rotamers` (set `EQC_BIO_CACHE` to use another directory).
- **Dunbrack (backbone-dependent)**: set `rotamer_library_format: dunbrack` and give a local copy of the Dunbrack bbdep file by path, since the library is not redistributable. It is read the way OSPREY reads it (10° φ/ψ bins, 0.001 probability cutoff, no PRO rotamers), so rotamer numbers match OSPREY matrices. Each position's φ/ψ is measured from `template_pdb`. The energy matrix must use position-prefixed rotamer names such as `4-MET-3`.

### With ProteinDesignFixed

```python
from eqc_bio.protein_design import ProteinDesignFixed

obj = ProteinDesignFixed(config_file="config_files/config_file_1MJC.yml", solver=solver)
obj.run()

angles = obj.get_rotamer_angles()                    # position, residue, rotamer, chi_angles
pdb_str = obj.build_pdb(output_path="output/designed_1MJC.pdb")
obj.view()                                           # in Jupyter; designed side chains drawn as sticks
```

Each method uses the design from the last `run()`, or a design DataFrame (columns `position` and `selected_rotamer`) passed as `design_df`. `load_expected_design()` returns the expected solution in that form, so its structure can be built without running a solver:

```bash
cd scripts
python protein_design_expected_pdb.py config_files/config_file_1MJC.yml   # writes output/expected_1MJC.pdb
```

`view(viewer="nglview")` uses nglview instead of py3Dmol. Neither viewer is a required dependency. Install both with the `viewer` extra (`pip install "eqc-bio[viewer]"`), or install the one you need (`pip install py3Dmol` or `pip install nglview`).

### Standalone Functions

The same steps are available as functions that take plain data, so angles or structures from other sources can be used at any step:

```python
from eqc_bio.protein_design import get_rotamer_angles, build_designed_pdb, view_pdb

angles = get_rotamer_angles(design_df)               # default Lovell library
pdb_str = build_designed_pdb("data/1MJC/1MJC.pdb", angles, output_path="out.pdb")
view_pdb(pdb_str, highlight=[4, 6])                  # PDB residue numbers to draw as sticks
```

- `get_rotamer_angles(design, rotamer_library=None, template_pdb=None, position_numbering="index", library_format="osprey")`: χ angles for each selected rotamer. A Dunbrack library also needs `template_pdb`.
- `build_designed_pdb(template_pdb, rotamer_angles, output_path=None, position_numbering="index")`: returns the PDB as a string and writes it if `output_path` is given. A position missing from the template raises `ValueError`.
- `view_pdb(pdb_str, viewer="py3Dmol", highlight=None)`: returns a py3Dmol or nglview widget.
- `get_backbone_dihedrals(template_pdb, position_numbering="index")`: `{position: (phi, psi)}` in degrees, with NaN where an atom is missing, such as at chain ends.
- `resolve_rotamer_library`, `load_rotamer_library`, `download_rotamer_library`, `parse_dunbrack_library`, `check_rotamer_counts` and `get_chi_angles` give lower-level access to the rotamer libraries.

## Applications

- Computational protein engineering
- Designing stable mutants or novel sequences on fixed backbones
- Benchmarking quantum-inspired solvers on NP-hard bioinformatics problems

(C) Quantum Computing Inc., 2026.
