# OEFF Examples¶

## Simple Functions and Optimization¶

### Solving a simple equation¶

The following example illustrates how to define a simple objective function. By deriving the objective function from OEFunc2, we can find the roots of the simple quadratic equation using OENewtonOpt optimizer. A class derived from OEFunc2 must contain analytical gradients and Hessians. This examples expects a number as input for the initial guess to solve the simple function.

### Using checkpoints for optimization monitoring¶

The following example illustrates how to define checkpoints and use then along with optimizers to monitor progress during optimization. For simplicity, a simple quadratic equation is defined as objective function and derived from OEFunc1. The quadratic equation is solved using the OEBFGSOpt optimizer. A class derived from OEFunc1 must contain analytical gradients. This examples expects a number as input for the initial guess to solve the simple function.

## Molecule Functions¶

### User defined molecule function¶

The following example illustrates how to define an objective function within the context of a molecule. Generally speaking, a molecule function (OEMolFunc) defines some sort of interaction involving a part or all of a molecule. For simplicity, a simple energy function is defined that consists sum of square of distance of all the atoms in the molecule. The molecule function is optimized using the OEBFGSOpt optimizer. A class derived from OEMolFunc1 must contain analytical gradients.

## Single ligand and MMFF¶

### Single ligand in vacuum¶

The following example illustrates how to calculate energy and optimize a single ligand in vacuum. The OEMMFF force field is used as is for this example. The molecule needs to be prepared (OEForceField.PrepMol), followed by a call to OEMolFunc.Setup to create the interactions. Optimization is carried out using the OEBFGSOpt optimizer.

### Energy components of single ligand in vacuum¶

The following example illustrates how to obtain various intermolecular energy components of a single ligand in vacuum, calculated using the OEMMFF force field.

### MMFF interactions of single ligand¶

The following example illustrates how to obtain various interactions of a single ligand in the context of the OEMMFF force field.

## Protein-Ligand Complexes¶

### Protein-ligand optimization with MMFF¶

The following example illustrates how to setup a ligand optimization within the context of a protein using the OEMMFF force field. Besides setting up the ligand MMFF interactions, protein preparation (OEForceField.PrepMol) followed by creation of the intermolecular interactions and addition of those to the force field are required to perform.

### Protein-ligand optimization with MMFFAmber¶

The following example illustrates how to setup a ligand optimization within the context of a protein using the OEMMFFAmber force field. Using an intermolecular forcefield like OEMMFFAmber for protein-ligand complexes is simpler compared to using OEMMFF, as the forcefield is already equipped to combine a ligand with a protein.

## Using adaptors¶

### Optimizing single ligand with fixed atoms¶

The following example illustrates how to fix a subset of atoms using the OESubsetAdaptor during a single ligand optimization in vacuum. The adaptor is initialized with the OEMMFF interactions of the ligand, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.

### Optimizing single ligand torsions¶

The following example illustrates how to optimize a set of torsion angles using the OETorAdaptor for a single ligand in vacuum. The adaptor is initialized with the OEMMFF interactions of the ligand, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.

### Optimizing rigid ligand in protein¶

The following example illustrates how to perform rigid optimization of a ligand in the context of a protein using the OEQuatAdaptor. The adaptor is initialized with the protein-ligand interactions, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.