Skip to main content
Energy terms define what BAGEL optimizes for. This guide shows you how to create your own.

The EnergyTerm base class

All energy terms inherit from EnergyTerm (defined in bagel/energies.py). The base class handles weight management, oracle association, residue group tracking, and integration with the grand-canonical ensemble. Your __init__ must call the parent constructor with these arguments:
Key attributes:
  • name — a string identifier, used in logging and output files
  • oracle — the oracle instance that provides predictions for this term
  • inheritable — if True, newly inserted residues (in GrandCanonical mode) inherit this energy term from their neighbors. Set to False for terms like TemplateMatchEnergy where new residues would be ill-defined
  • weight — multiplier applied to the unweighted energy
  • residue_groups — list of ResidueGroup tuples, each containing arrays of chain IDs and residue indices

Implementing compute()

The only method you must implement is compute(). It receives an OraclesResultDict mapping oracles to their results and must return a tuple of (unweighted_energy, weighted_energy):
Guidelines:
  • Unweighted energy should be normalized to the 0–1 range where possible (0 = best, 1 = worst)
  • Weighted energy is unweighted * self.weight — this is what gets summed into the state energy
  • The method is called once per optimization step, after all oracles have produced their results

Working with oracle results

The OraclesResultDict provides typed access to oracle outputs:

Residue groups

Residue groups define which residues an energy term operates on. The helper methods get_residue_mask() and get_atom_mask() let you filter structure data to your groups:
For grand-canonical support (when residues can be inserted/deleted), you may need to implement remove_residue() and add_residue() to update internal state when residue indices shift. The base class handles index bookkeeping for residue_groups automatically.

Example: a custom energy term

Here is a complete example of a custom energy term that penalizes low per-residue pLDDT confidence on a specific residue group, but only for residues that are also surface-exposed (SASA above a threshold):
Usage: