Drawing a Molecule Surface

The 2D representation of the molecule surface is calculated by first drawing circles around each atom of the molecule and then identifying the external arc segments that define a continuous surface around the molecule. This process is illustrated in the table Table: Calculation of the 2D representation of a molecule surface

Calculation of the 2D representation of a molecule surface
../_images/Draw2DSurface-Step-01.png ../_images/Draw2DSurface-Step-02.png
../_images/Draw2DSurface-Step-03.png ../_images/Draw2DSurface-Step-04.png
../_images/Draw2DSurface-Step-05.png ../_images/Draw2DSurface-Step-06.png

The following Listing 1 example shows how to the display a molecule surface. After creating an image object and preparing the molecule for 2D depiction, an arc drawing functor is added to each atom of the molecule by using the OESetSurfaceArcFxn function. The surface then can be drawn by calling the OEDraw2DSurface function that calculates the arc segments that form a continuous curve around the molecule. The image created by Listing 1 is shown in Figure: Example of surface drawing.

Listing 1: Example of surface drawing

double scale  = OEScale.AutoScale;
OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(width, height, scale);
opts.SetTitleLocation(OETitleLocation.Hidden);
opts.SetScale(OEGrapheme.OEGetMoleculeSurfaceScale(mol, opts));

OEEyelashArcFxn arcfxn= new OEEyelashArcFxn(new OEPen(OEChem.OEGrey, OEChem.OEGrey));

foreach (OEAtomBase atom in mol.GetAtoms())
{
    OEGrapheme.OESetSurfaceArcFxn(mol, atom, arcfxn);
}

OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
OEGrapheme.OEDraw2DSurface(disp);
../_images/Draw2DSurface.png

Example of surface drawing

The Listing 1 example uses the OESurfaceArcStyle.Eyelash style to draw the molecule surface. The following table lists all surface drawing styles that are available in Grapheme TK.

Supported surface drawing styles
../_images/OESurfaceArc_Small_OEAlphaRainbowArcFxn.png ../_images/OESurfaceArc_Small_OEBrickRoadArcFxn.png ../_images/OESurfaceArc_Small_OECastleArcFxn.png ../_images/OESurfaceArc_Small_OECogArcFxn.png

OEAlphaRainbowArcFxn

OEBrickRoadArcFxn

OECastleArcFxn

OECogArcFxn

../_images/OESurfaceArc_Small_OEDefaultArcFxn.png ../_images/OESurfaceArc_Small_OEEyelashArcFxn.png ../_images/OESurfaceArc_Small_OEFlowerArcFxn.png ../_images/OESurfaceArc_Small_OENecklaceArcFxn.png

OEDefaultArcFxn

OEEyelashArcFxn

OEFlowerArcFxn

OENecklaceArcFxn

../_images/OESurfaceArc_Small_OENullArcFxn.png ../_images/OESurfaceArc_Small_OEOliveBranchArcFxn.png ../_images/OESurfaceArc_Small_OEPearlsArcFxn.png ../_images/OESurfaceArc_Small_OERaceTrackArcFxn.png

OENullArcFxn

OEOliveBranchArcFxn

OEPearlsArcFxn

OERaceTrackArcFxn

../_images/OESurfaceArc_Small_OERailroadTrackArcFxn.png ../_images/OESurfaceArc_Small_OESawArcFxn.png ../_images/OESurfaceArc_Small_OESimpsonArcFxn.png ../_images/OESurfaceArc_Small_OEStitchArcFxn.png

OEStitchArcFxn

OESunArcFxn

OEWreathArcFxn

OERailroadTrackArcFxn

../_images/OESurfaceArc_Small_OESunArcFxn.png ../_images/OESurfaceArc_Small_OEWreathArcFxn.png

OESawArcFxn

OESimpsonArcFxn

User-defined surface drawing functors can be implemented by deriving from the OESurfaceArcFxnBase abstract base class. In the Listing 2 example, a user defined arc drawing functor is implemented that colors the eyelash arcs according to the atom they belong to. The image created by Listing 2 is shown in Figure: Example of user-defined surface drawing.

Listing 2: Example of user-defined surface drawing

public class AtomColorArcFxn : OESurfaceArcFxnBase
{
    public override bool Call(OEImageBase image, OESurfaceArc arc)
    {
        OE2DAtomDisplay adisp = arc.GetAtomDisplay();
        if (adisp == null || !adisp.IsVisible())
            return false;

        OEPen pen = new OEPen();
        OEColor color = adisp.GetLabelFont().GetColor();
        pen.SetForeColor(color);

        OE2DPoint center = arc.GetCenter();
        double radius    = arc.GetRadius();
        double bgnAngle  = arc.GetBgnAngle();
        double endAngle  = arc.GetEndAngle();

        OEGrapheme.OEDrawEyelashSurfaceArc(image, center, bgnAngle, endAngle, radius, pen);
        return true;
    }
    public override OESurfaceArcFxnBase CreateCopy()
    {
        AtomColorArcFxn copy = new AtomColorArcFxn();
        copy.ReleaseOwnership();
        return copy;
    }
}
static void draw2DSurface(OEImageBase image, OEMolBase mol) {

    OEDepict.OEPrepareDepiction(mol);

    double scale = OEScale.AutoScale;
    OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), scale);
    opts.SetTitleLocation(OETitleLocation.Hidden);
    opts.SetScale(OEGrapheme.OEGetMoleculeSurfaceScale(mol, opts));

    AtomColorArcFxn arcfxn = new AtomColorArcFxn();
    foreach (OEAtomBase atom in mol.GetAtoms())
    {
        OEGrapheme.OESetSurfaceArcFxn(mol, atom, arcfxn);
    }

    OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
    OEGrapheme.OEDraw2DSurface(disp);
    OEDepict.OERenderMolecule(image, disp);
}
../_images/Draw2DSurfaceUserDefined.png

Example of user-defined surface drawing

The Listing 3 example shows how to project atom properties, such as partial charges into the molecule surface. The image created by Listing 3 is shown in Figure: Example of depicting atom properties.

Listing 3: Example of depicting atom properties on the molecule surface

private class AtomPartialChargeArcFxn : OESurfaceArcFxnBase
{
    private OELinearColorGradient colorg;
    private AtomPartialChargeArcFxn() {}
    public AtomPartialChargeArcFxn(OELinearColorGradient cg)
    {
        colorg = new OELinearColorGradient(cg);
    }

    public override bool Call(OEImageBase image, OESurfaceArc arc)
    {
        OE2DAtomDisplay adisp = arc.GetAtomDisplay();
        if (adisp == null || !adisp.IsVisible())
            return false;

        OEAtomBase atom = adisp.GetAtom();
        if (atom == null)
            return false;

        double charge = atom.GetPartialCharge();
        if (charge == 0.0)
            return true;
        OEColor color = colorg.GetColorAt(charge);

        OEPen pen = new OEPen();
        pen.SetForeColor(color);
        pen.SetLineWidth(2.0);

        OE2DPoint center = arc.GetCenter();
        double radius = arc.GetRadius();
        double bAngle = arc.GetBgnAngle();
        double eAngle = arc.GetEndAngle();

        double edgeAngle = 5.0;
        uint dir = OEPatternDirection.Outside;
        double patternAngle = 10.0;
        OEGrapheme.OEDrawBrickRoadSurfaceArc(image, center, bAngle, eAngle, radius,
                                             pen, edgeAngle, dir, patternAngle);
        return true;
    }

    public override OESurfaceArcFxnBase CreateCopy()
    {
        AtomPartialChargeArcFxn copy = new AtomPartialChargeArcFxn(colorg);
        copy.ReleaseOwnership();
        return copy;
    }
}

static void draw2DSurfacePartialCharge(OEImageBase image, OEMolBase mol) {

    OEDepict.OEPrepareDepiction(mol);

    OEChem.OEMMFFAtomTypes(mol);
    OEChem.OEMMFF94PartialCharges(mol);

    double scale  = OEScale.AutoScale;
    OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), scale);
    opts.SetTitleLocation(OETitleLocation.Hidden);
    opts.SetScale(OEGrapheme.OEGetMoleculeSurfaceScale(mol, opts));

    OEColorStop coloranion = new OEColorStop(-1.0, OEChem.OEDarkRed);
    OEColorStop colorcation = new OEColorStop(+1.0, OEChem.OEDarkBlue);
    OELinearColorGradient colorg = new OELinearColorGradient(coloranion, colorcation);
    colorg.AddStop(new OEColorStop(0.0, OEChem.OEWhite));

    AtomPartialChargeArcFxn arcfxn = new AtomPartialChargeArcFxn(colorg);

    foreach (OEAtomBase atom in mol.GetAtoms())
    {
        OEGrapheme.OESetSurfaceArcFxn(mol, atom, arcfxn);
    }

    OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);
    OEGrapheme.OEDraw2DSurface(disp);

    OEDepict.OERenderMolecule(image, disp);
}
../_images/Draw2DSurfacePartialCharge.png

Example of depicting atom properties

See also

It is also possible to draw multiple 2D surfaces arc-by-arc. In the Listing 4 example below the arcs returned by the OEGet2DSurfaceArcs function for the given atom display and radius are directly rendered below the molecule diagram. In this case it is important to call the OEGetMoleculeSurfaceScale function with the largest radius scale of the 2D surfaces drawn. This reduces the scaling of the molecule in order to able to fit the molecule diagram and all of the arcs of the 2D surfaces into the image. The image created by Listing 4 is shown in Figure: Example of drawing multiple 2D surfaces.

Listing 4: Example of drawing multiple 2D surfaces

static void draw2DSurface(OE2DMolDisplay disp,  OEUnaryAtomPred atompred,
                          double radius,  OEColor color)
{

    OEPen penA = new OEPen(color, color, OEFill.Off, 2.0);
    OEDefaultArcFxn arcfxnA = new OEDefaultArcFxn(penA);

    OEColor grey = new OEColor(OEChem.OELightGrey);
    OEPen penB = new OEPen(grey, grey, OEFill.Off, 2.0, OEStipple.ShortDash);
    OEDefaultArcFxn arcfxnB = new OEDefaultArcFxn(penB);

    OEImage layer = disp.GetLayer(OELayerPosition.Below);

    foreach (OE2DAtomDisplay adisp in disp.GetAtomDisplays())
    {
        foreach (OESurfaceArc arc in OEGrapheme.OEGet2DSurfaceArcs(disp, adisp, radius))
        {
            if (atompred.Call(adisp.GetAtom()))
                arcfxnA.Call(layer, arc);
            else
                arcfxnB.Call(layer, arc);
        }
    }
}

static void draw2DSurfaces(OEImageBase image, OEMolBase mol)
{

    OEDepict.OEPrepareDepiction(mol);

    double scale =  OEScale.AutoScale;
    OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(image.GetWidth(), image.GetHeight(), scale);
    opts.SetTitleLocation(OETitleLocation.Hidden);
    opts.SetScale(OEGrapheme.OEGetMoleculeSurfaceScale(mol, opts, 1.50));

    OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);

    draw2DSurface(disp, new OEHasAtomicNum(OEElemNo.C), 1.00, OEChem.OEBlack);
    draw2DSurface(disp, new OEHasAtomicNum(OEElemNo.N), 1.25, OEChem.OEDarkBlue);
    draw2DSurface(disp, new OEHasAtomicNum(OEElemNo.O), 1.50, OEChem.OEDarkRed);

    OEDepict.OERenderMolecule(image, disp);

}
../_images/DrawMultiple2DSurfaces.png

Example of drawing multiple 2D surfaces

See also

OESurfaceArcScale namespace

The last Listing 5 example shows how to draw a 2D surface with various radii. It draws two surfaces one with a minimum radius scale and one with various radius depending on the covalent radius of the atoms. The OEGetMoleculeSurfaceScale function has to be called in this example too with the largest radius scale in order to able to fit the molecule diagram and all of the arcs of the 2D surfaces into the image. The image created by Listing 5 is shown in Figure: Example of drawing 2D surface with various radii.

Listing 5: Example of drawing 2D surface with various radii

    public static void DrawSurfaces (OEImageBase image, OEMolBase mol)
    {
        OEChem.OEAssignCovalentRadii(mol);
        double minradius = OEChem.OEGetCovalentRadius(OEElemNo.H);

        OEDoubleVector radiusScales = new OEDoubleVector((int)mol.GetMaxAtomIdx());

        double maxrscale = Double.MinValue;

        foreach (OEAtomBase atom in mol.GetAtoms())
        {
            double rscale = (atom.GetRadius() - minradius) + OESurfaceArcScale.Minimum;
            radiusScales.Insert((int)atom.GetIdx(), rscale);
            maxrscale = Math.Max(maxrscale, rscale);
        }

        OE2DMolDisplayOptions opts = new OE2DMolDisplayOptions(image.GetWidth(), 
                                                               image.GetHeight(), OEScale.AutoScale);
        opts.SetTitleLocation(OETitleLocation.Hidden);
        opts.SetScale(OEGrapheme.OEGetMoleculeSurfaceScale(mol, opts, maxrscale));

        OE2DMolDisplay disp = new OE2DMolDisplay(mol, opts);

        OEImage layer = disp.GetLayer(OELayerPosition.Below);

        OEPen penA = new OEPen(OEChem.OELightGrey, OEChem.OELightGrey,
                               OEFill.Off, 2.0, OEStipple.ShortDash);
        OEDefaultArcFxn arcfxnA = new OEDefaultArcFxn(penA);

        foreach (OESurfaceArc arc in OEGrapheme.OEGet2DSurfaceArcs(disp, OESurfaceArcScale.Minimum))
        {
            arcfxnA.Call(layer, arc);
        }

        OEPen penB = new OEPen(OEChem.OEGrey, OEChem.OEGrey, OEFill.Off, 2.0);
        OEDefaultArcFxn arcfxnB = new OEDefaultArcFxn(penB);

        foreach (OESurfaceArc arc in OEGrapheme.OEGet2DSurfaceArcs(disp, radiusScales))
        { 
            arcfxnB.Call(layer, arc);
        }

        OEDepict.OERenderMolecule(image, disp);
    }    
../_images/Draw2DSurfaceVariousRadii.png

Example of drawing 2D surface with various radii

See also

OEGetCovalentRadius function in the OEChem TK manual