Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.19.0"
".": "0.20.0"
}
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.20.0 (2026-12-01)


### Features

* **models:** sample structure ensembles with gibbs

## 0.19.0 (2026-12-01)


Expand Down
4 changes: 4 additions & 0 deletions openprotein/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
BioEmuInput,
ESMFlow,
ESMFlowInput,
Gibbs,
GibbsInput,
StructureEnsemblePredictionModel,
)
from .estimators import GP, SVD, UMAP
Expand Down Expand Up @@ -96,6 +98,8 @@
"ESMFold",
"ESMFold2",
"GPModel",
"Gibbs",
"GibbsInput",
"LinearKernel",
"Matern12Kernel",
"Matern32Kernel",
Expand Down
3 changes: 3 additions & 0 deletions openprotein/models/ensemble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .base import StructureEnsemblePredictionModel
from .bioemu import BioEmu, BioEmuInput
from .esmflow import ESMFlow, ESMFlowInput
from .gibbs import Gibbs, GibbsInput

__all__ = [
"AlphaFlow",
Expand All @@ -12,5 +13,7 @@
"BioEmuInput",
"ESMFlow",
"ESMFlowInput",
"Gibbs",
"GibbsInput",
"StructureEnsemblePredictionModel",
]
32 changes: 32 additions & 0 deletions openprotein/models/ensemble/_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from typing import Any, TypeVar

from openprotein.fold.common import _msa_id_of
from openprotein.fold.future import FoldResultFuture
from openprotein.jobs.futures import StructurePredictionFuture
from openprotein.molecules import Complex, Protein, Structure
from openprotein.utils.uuid import _as_id

from .. import _inputs

_T = TypeVar("_T")

Expand Down Expand Up @@ -161,3 +166,30 @@ def flow_references(
}
for index, (reference, sequence) in enumerate(zip(references, sequences))
]


def gibbs_structure(value: Any, field: str) -> str:
if _as_id(value) is not None:
raise ValueError(f"{field} takes structure text, not a job id")
if isinstance(value, (str, bytes)):
return _text(value, field)
complex = _reference_complex(value, field, single_chain=False)
return complex.to_string(format="cif")


def gibbs_reference(value: Any, field: str) -> dict[str, str]:
"""``value`` as a ``job_id`` when it names a prediction, else ``structure_file``."""
if isinstance(value, (StructurePredictionFuture, FoldResultFuture)):
return {"job_id": value.id}
if (job_id := _as_id(value)) is not None:
return {"job_id": job_id}
return {"structure_file": gibbs_structure(value, field)}


def prompt_id(value: Any, field: str) -> str:
if value is None:
raise ValueError(f"{field} is required")
_inputs._check_prompt(value, field)
resolved = _inputs.prompt_id(value)
assert resolved is not None
return resolved
133 changes: 133 additions & 0 deletions openprotein/models/ensemble/gibbs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
"""Gibbs."""

from collections.abc import Sequence
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any

from openprotein.jobs import futures

from . import _inputs
from .base import StructureEnsemblePredictionModel

if TYPE_CHECKING:
from openprotein.data.prompts.prompts import Prompt as StoredPrompt
from openprotein.fold.future import FoldResultFuture
from openprotein.molecules import Complex, Protein, Structure
from openprotein.prompt import Prompt

__all__ = ["Gibbs", "GibbsInput"]


@dataclass(frozen=True)
class GibbsInput:
"""One Gibbs ensemble: a reference structure and the prompt it samples with.

Attributes
----------
reference_structure : str, bytes, Protein, Complex, Structure, StructurePredictionFuture or FoldResultFuture
PDB or mmCIF text, a protein-only molecule, or a structure prediction
of one structure, given as its future or job id. A reference may hold
several protein chains.
prompt : str, Prompt or StoredPrompt
The PoET-2 prompt the ensemble is conditioned on, or its id.
constraint : str, bytes, Protein, Complex, Structure or None
Part of the reference held fixed while the rest is sampled.
temperature : int or None
Temperature in Kelvin for this ensemble. ``None`` uses the call's.
"""

reference_structure: "str | bytes | Protein | Complex | Structure | futures.StructurePredictionFuture | FoldResultFuture"
prompt: "str | Prompt | StoredPrompt"
constraint: "str | bytes | Protein | Complex | Structure | None" = None
temperature: int | None = None


class Gibbs(StructureEnsemblePredictionModel):
"""Gibbs samples protein ensembles around reference structures.

Each ensemble starts from a reference and a PoET-2 prompt. A constraint
holds part of the reference fixed, and a temperature overrides the call's
for one ensemble. A reference may hold several protein chains. Its
checkpoint is ``gibbs``.
"""

Input = GibbsInput
_default_id = "gibbs"

def sample(
self,
inputs: Sequence[GibbsInput],
num_samples: int = 1,
temperature: int = 300,
random_seed: int | None = None,
) -> futures.StructurePredictionFuture:
"""Sample one ensemble per input.

Parameters
----------
inputs : sequence of GibbsInput
One entry per ensemble.
num_samples : int
Conformations to sample per input.
temperature : int
Sampling temperature in Kelvin, for inputs that set none.
random_seed : int or None
Random seed. Gibbs samples are not reproducible from it.

Returns
-------
StructurePredictionFuture
One ensemble per input, in input order.

Examples
--------
A reference straight off a structure prediction, and a second one
sampled hotter:

>>> prompt = msa.sample_prompt(num_sequences=16)
>>> predicted = session.models.esmfold.fold(sequence)
>>> gibbs = session.models.gibbs
>>> future = gibbs.sample(
... [
... gibbs.Input(reference_structure=predicted, prompt=prompt),
... gibbs.Input(
... reference_structure=reference, prompt=prompt, temperature=350
... ),
... ],
... num_samples=10,
... temperature=300,
... )
>>> ensembles = future.get()

Part of the reference held fixed while the rest is sampled:

>>> future = gibbs.sample(
... [
... gibbs.Input(
... reference_structure=reference, prompt=prompt, constraint=pocket
... )
... ]
... )
"""
references = []
for i, entry in enumerate(_inputs.input_list(inputs, GibbsInput)):
field = f"inputs[{i}]"
record: dict[str, Any] = {
**_inputs.gibbs_reference(
entry.reference_structure, f"{field}.reference_structure"
),
"prompt_id": _inputs.prompt_id(entry.prompt, f"{field}.prompt"),
}
if entry.constraint is not None:
record["constraint"] = _inputs.gibbs_structure(
entry.constraint, f"{field}.constraint"
)
if entry.temperature is not None:
record["temperature"] = entry.temperature
references.append(record)
return self._sample(
reference_structures=references,
num_samples=num_samples,
temperature=temperature,
random_seed=random_seed,
)
7 changes: 6 additions & 1 deletion openprotein/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from openprotein.errors import HTTPError

from . import _api, estimators, gp, svd, umap
from .ensemble import AlphaFlow, BioEmu, ESMFlow
from .ensemble import AlphaFlow, BioEmu, ESMFlow, Gibbs
from .fold import (
AlphaFold2,
Boltz1,
Expand Down Expand Up @@ -395,6 +395,11 @@ def esmflow_md_distilled_v1(self) -> ESMFlow:
"""``esmflow-md-distilled-v1``."""
return self._handle("esmflow-md-distilled-v1", ESMFlow)

@property
def gibbs(self) -> Gibbs:
"""``gibbs``."""
return self._handle("gibbs", Gibbs)

def list_models(
self,
verbose: bool = False,
Expand Down
14 changes: 14 additions & 0 deletions tests/data/ensemble_cards.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,19 @@
"variant": "distilled",
"base": false,
"max_sequence_length": 600
},
"gibbs": {
"input_types": [
"protein"
],
"multichain": true,
"tags": [
"structure",
"ensemble",
"openprotein"
],
"variant": null,
"base": true,
"max_sequence_length": 1500
}
}
115 changes: 115 additions & 0 deletions tests/data/models_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -5896,6 +5896,121 @@
"type": "object"
}
},
"gibbs/batch/structures": {
"outputs": [
"structures"
],
"params": {
"$defs": {
"GibbsReference": {
"additionalProperties": false,
"properties": {
"constraint": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "PDB or mmCIF text of the part of the reference held fixed while the rest is sampled."
},
"job_id": {
"anyOf": [
{
"format": "uuid",
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "A structure prediction job of yours to take the reference from, which must predict one structure; give this or structure_file."
},
"prompt_id": {
"description": "PoET-2 prompt the ensemble embedding is built from.",
"format": "uuid",
"type": "string"
},
"structure_file": {
"anyOf": [
{
"minLength": 1,
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"description": "Reference structure as PDB or mmCIF text; give this or job_id."
},
"temperature": {
"anyOf": [
{
"minimum": 1,
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"description": "Temperature in Kelvin for this reference; null uses the request's temperature."
}
},
"required": [
"prompt_id"
],
"type": "object"
}
},
"additionalProperties": false,
"properties": {
"num_samples": {
"default": 1,
"description": "Conformations to sample per reference.",
"minimum": 1,
"type": "integer"
},
"random_seed": {
"anyOf": [
{
"maximum": 2147483647,
"minimum": 0,
"type": "integer"
},
{
"type": "null"
}
],
"default": null,
"description": "Random seed. Not used yet by this model, so runs are not reproducible."
},
"reference_structures": {
"description": "References to sample an ensemble of, one ensemble each.",
"items": {
"$ref": "#/$defs/GibbsReference"
},
"minItems": 1,
"type": "array"
},
"temperature": {
"default": 300,
"description": "Sampling temperature in Kelvin.",
"minimum": 1,
"type": "integer"
}
},
"required": [
"reference_structures"
],
"type": "object"
}
},
"gp/batch/predictions": {
"outputs": [
"predictions"
Expand Down
Loading
Loading