Instructions to use Synthyra/ESMFold2-Fast with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Synthyra/ESMFold2-Fast with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="Synthyra/ESMFold2-Fast", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Synthyra/ESMFold2-Fast", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 6,859 Bytes
6cc35b0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | """Typed, JSON-safe inputs for ESMFold2 feature preparation."""
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any, TypeAlias
import numpy as np
from .esmfold2_msa import MSA
MSAInput: TypeAlias = MSA | None
@dataclass
class Modification:
"""A zero-indexed residue substitution using a CCD component."""
position: int
ccd: str
smiles: str | None = None
@dataclass
class ProteinInput:
id: str | list[str]
sequence: str
modifications: list[Modification] | None = None
msa: MSAInput = None
@dataclass
class RNAInput:
id: str | list[str]
sequence: str
modifications: list[Modification] | None = None
@dataclass
class DNAInput:
id: str | list[str]
sequence: str
modifications: list[Modification] | None = None
@dataclass
class LigandInput:
id: str | list[str]
smiles: str | None = None
ccd: list[str] | None = None
@dataclass
class DistogramConditioning:
chain_id: str
distogram: np.ndarray
@dataclass
class PocketConditioning:
binder_chain_id: str
contacts: list[tuple[str, int]]
@dataclass
class CovalentBond:
chain_id1: str
res_idx1: int
atom_idx1: int
chain_id2: str
res_idx2: int
atom_idx2: int
SequenceInput: TypeAlias = ProteinInput | RNAInput | DNAInput | LigandInput
@dataclass
class StructurePredictionInput:
sequences: Sequence[SequenceInput]
pocket: PocketConditioning | None = None
distogram_conditioning: list[DistogramConditioning] | None = None
covalent_bonds: list[CovalentBond] | None = None
_CHAIN_TYPE = {
ProteinInput: "protein",
RNAInput: "rna",
DNAInput: "dna",
}
def _serialize_modifications(
modifications: list[Modification] | None,
) -> list[dict[str, Any]] | None:
if not modifications:
return None
return [{"position": item.position, "ccd": item.ccd} for item in modifications]
def _serialize_chain(chain: SequenceInput) -> dict[str, Any]:
if isinstance(chain, LigandInput):
return {
"smiles": chain.smiles,
"id": chain.id,
"ccd": chain.ccd,
"type": "ligand",
}
chain_type = _CHAIN_TYPE.get(type(chain))
if chain_type is None:
raise ValueError(f"Unsupported sequence input type: {type(chain)}")
serialized: dict[str, Any] = {
"sequence": chain.sequence,
"id": chain.id,
"type": chain_type,
}
if modifications := _serialize_modifications(chain.modifications):
serialized["modifications"] = modifications
if isinstance(chain, ProteinInput):
if chain.msa is not None and not isinstance(chain.msa, MSA):
raise AttributeError(f"MSA must be None or MSA. Got {chain.msa} instead.")
serialized["msa"] = None if chain.msa is None else {"sequences": chain.msa.sequences}
return serialized
def serialize_structure_prediction_input(
structure_input: StructurePredictionInput,
) -> dict[str, Any]:
"""Convert an input object to a JSON-safe mapping."""
serialized: dict[str, Any] = {
"sequences": [_serialize_chain(chain) for chain in structure_input.sequences]
}
if structure_input.covalent_bonds is not None:
serialized["covalent_bonds"] = [
vars(bond).copy() for bond in structure_input.covalent_bonds
]
if structure_input.pocket is not None:
serialized["pocket"] = {
"binder_chain_id": structure_input.pocket.binder_chain_id,
"contacts": structure_input.pocket.contacts,
}
if structure_input.distogram_conditioning is not None:
serialized["distogram_conditioning"] = [
{"chain_id": item.chain_id, "distogram": item.distogram.tolist()}
for item in structure_input.distogram_conditioning
]
return serialized
def _deserialize_modifications(chain: dict[str, Any]) -> list[Modification] | None:
raw = chain.get("modifications")
if not raw:
return None
return [Modification(position=item["position"], ccd=item["ccd"]) for item in raw]
def _deserialize_msa(chain: dict[str, Any]) -> MSAInput:
raw = chain.get("msa")
if raw is None:
return None
if not isinstance(raw, dict) or not isinstance(raw.get("sequences"), list):
raise ValueError(f"Unexpected MSA value: {raw!r}")
return MSA.from_sequences(raw["sequences"])
def _deserialize_chain(chain: dict[str, Any]) -> SequenceInput:
chain_type = chain.get("type")
common = {"id": chain["id"]}
if chain_type == "protein":
return ProteinInput(
**common,
sequence=chain["sequence"],
modifications=_deserialize_modifications(chain),
msa=_deserialize_msa(chain),
)
if chain_type == "rna":
return RNAInput(
**common,
sequence=chain["sequence"],
modifications=_deserialize_modifications(chain),
)
if chain_type == "dna":
return DNAInput(
**common,
sequence=chain["sequence"],
modifications=_deserialize_modifications(chain),
)
if chain_type == "ligand":
return LigandInput(**common, smiles=chain.get("smiles"), ccd=chain.get("ccd"))
raise ValueError(f"Unsupported sequence type: {chain_type!r}")
def deserialize_structure_prediction_input(data: dict[str, Any]) -> StructurePredictionInput:
"""Reconstruct the typed input represented by a serialized mapping."""
pocket_data = data.get("pocket")
pocket = None
if pocket_data is not None:
pocket = PocketConditioning(
binder_chain_id=pocket_data["binder_chain_id"],
contacts=[tuple(contact) for contact in pocket_data["contacts"]],
)
distogram_data = data.get("distogram_conditioning")
distograms = None
if distogram_data is not None:
distograms = [
DistogramConditioning(
chain_id=item["chain_id"], distogram=np.asarray(item["distogram"])
)
for item in distogram_data
]
bond_data = data.get("covalent_bonds")
bonds = None
if bond_data is not None:
bonds = [CovalentBond(**item) for item in bond_data]
return StructurePredictionInput(
sequences=[_deserialize_chain(chain) for chain in data["sequences"]],
pocket=pocket,
distogram_conditioning=distograms,
covalent_bonds=bonds,
)
__all__ = [
"CovalentBond",
"DNAInput",
"DistogramConditioning",
"LigandInput",
"MSAInput",
"Modification",
"PocketConditioning",
"ProteinInput",
"RNAInput",
"SequenceInput",
"StructurePredictionInput",
"deserialize_structure_prediction_input",
"serialize_structure_prediction_input",
]
|