Molecule Alignment

The following two code examples demonstrate how to align molecules either based on their maximum common substructure (Listing 1) or based on substructure matches (Listing 2).

Molecule Alignment Based on MCS

The Figure: Example of depiction without alignment is generated by highlighting the maximum common substructure of two molecules (MCS), but keeping the original orientation of the compounds.

../_images/MCSNoAlignment.png

Example of depiction without alignment

The Listing 1 code example shows how to align these two molecules by their maximum common substructures (i.e. by the highlighted atoms and bonds in Figure: Example of depiction without alignment).

  • First, two molecules are initialized from SMILES string and prepared for depiction.
  • A OEMCSSearch object is initialized with the reference 2D molecule onto which the fit molecule will be aligned.
  • An OEImageGrid object is constructed that allows to render the two molecules in a grid next to each other.
  • The fit molecule is then aligned to the reference by calling the OEPrepareAlignedDepiction function that performs MCS search and then aligns the fit molecule to the reference based on the detected common substructure(s).
  • An OE2DMolDisplayOptions object is initialized that stores the properties determine how the molecules are rendered. In order to render the molecules in equal size the smallest depiction scaling factor is calculated.
  • If the molecule alignment was successful (i.e OEAlignmentResult.IsValid is true), then the correspondence between the two molecules stored in the OEAlignmentResult returned by the OEPrepareAlignedDepiction function. This OEAlignmentResult object can be used to highlight the matched common substructure by invoking the OEAddHighlighting function.
  • After both molecules are rendered into the separate cells of the grid layout, the image is written into a png file.

After initializing the two molecules and the OEMCSSearch object, the OEMCSSearch.Match method is called to perform the search and return an iterator over the identified maximum common substructures. The first match is then utilized to call the OEPrepareAlignedDepiction function that aligns one molecule to the other based on the given match. After the alignment, the common atoms and bonds are highlighted by invoking the OEAddHighlighting function. The image created by Listing 1 is shown in Figure: Example of depiction with alignment based on MCS.

Listing 1: Example of molecule alignment based on MCS

package openeye.docexamples.oedepict;

import openeye.oechem.*;
import openeye.oedepict.*;

public class MCSAlign {
    public static void main(String argv[]) {
        OEGraphMol refmol = new OEGraphMol();
        oechem.OESmilesToMol(refmol, "c1cc(c2cc(cnc2c1)CCCO)C(=O)CCO");
        oedepict.OEPrepareDepiction(refmol);

        OEGraphMol fitmol = new OEGraphMol();
        oechem.OESmilesToMol(fitmol, "c1cc2ccc(cc2c(c1)C(=O)O)CCO");
        oedepict.OEPrepareDepiction(fitmol);

        OEMCSSearch mcss = new OEMCSSearch(OEMCSType.Approximate);
        int atomexpr = OEExprOpts.DefaultAtoms;
        int bondexpr = OEExprOpts.DefaultBonds;
        mcss.Init(refmol, atomexpr, bondexpr);
        mcss.SetMCSFunc(new OEMCSMaxBondsCompleteCycles());

        OEAlignmentResult alignres = new OEAlignmentResult(oedepict.OEPrepareAlignedDepiction(fitmol, mcss));

        OEImage image = new OEImage(400, 200);

        int rows = 1;
        int cols = 2;
        OEImageGrid grid = new OEImageGrid(image, rows, cols);

        OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(grid.GetCellWidth(), 
                grid.GetCellHeight(), 
                OEScale.AutoScale);
        opts.SetTitleLocation(OETitleLocation.Hidden);

        double refscale = oedepict.OEGetMoleculeScale(refmol, opts);
        double fitscale = oedepict.OEGetMoleculeScale(fitmol, opts);

        opts.SetScale(Math.min(refscale, fitscale));

        int hstyle = OEHighlightStyle.BallAndStick;

        OE2DMolDisplay refdisp = new OE2DMolDisplay(mcss.GetPattern(), opts);
        OE2DMolDisplay fitdisp = new OE2DMolDisplay(fitmol, opts);

        if (alignres.IsValid()) {
            OEAtomBondSet refabset = new OEAtomBondSet(alignres.GetPatternAtoms(), alignres.GetPatternBonds());
            oedepict.OEAddHighlighting(refdisp, oechem.getOEBlueTint(), hstyle, refabset);

            OEAtomBondSet fitabset = new OEAtomBondSet(alignres.GetTargetAtoms(), alignres.GetTargetBonds());
            oedepict.OEAddHighlighting(fitdisp, oechem.getOEBlueTint(), hstyle, fitabset);
        }

        OEImageBase refcell = grid.GetCell(1, 1);
        oedepict.OERenderMolecule(refcell, refdisp);

        OEImageBase fitcell = grid.GetCell(1, 2);
        oedepict.OERenderMolecule(fitcell, fitdisp);

        oedepict.OEWriteImage("MCSAlign.png", image);
    }
}
../_images/MCSAlign.png

Example of depiction with alignment based on maximum common substructure

See also