Depicting Reaction Components


You want to depict your MDL reaction with highlighting of its reactant and product components.

OEDepict TK provides ability to depict the mapping index between reactant and product atoms. See example in Figure 1.


Figure 1. Example of MDL reaction depiction with atom mapping

However, there is a better way to visualize this information using highlighting. In the image below, each reactant of the reaction is highlighted using a different color and then the same color is used to highlight the atoms of the product(s) which are mapped to atoms of the given reactant. See example in Figure 2.


Figure 2. Example of MDL reaction depiction with component highlighting


Difficulty Level

../_images/chilly.png ../_images/chilly.png


The ColorReactionByComponents shows how a reaction is partitioned into components by calling the OEDetermineComponents function. The OEDetermineComponents function returns the number of components of a molecule i.e. in this case the number of reactants and products of the reaction and it also returns a list that stores which part each atom belongs to (line 4) . This parts list is then used to set up a OEPartPredAtom predicate (line 5). A loop over the reactant components of the reaction (line 9), associates each with different color. Inside the loop the atoms of each reactant are highlighted (lines 15-17) along with the product atoms that are mapped to the given reactant (lines 19-21).

 1def ColorReactionByComponents(rdisp):
 3    rmol = rdisp.GetMolecule()
 4    nrparts, parts = oechem.OEDetermineComponents(rmol)
 5    partpred = oechem.OEPartPredAtom(parts)
 7    colors = [c for c in oechem.OEGetLightColors()]
 8    highlightstyle = oedepict.OEHighlightStyle_BallAndStick
10    for partidx, color in zip(range(1, nrparts + 1), colors):
12        partpred.SelectPart(partidx)
14        if IsReactantComponent(rmol, partpred):
16            reactant = oechem.OEAtomBondSet(rmol.GetAtoms(partpred))
17            AddBondsBetweenAtoms(rmol, reactant)
18            oedepict.OEAddHighlighting(rdisp, color, highlightstyle, reactant)
20            product = GetProductAtoms(rmol, reactant)
21            AddBondsBetweenAtoms(rmol, product)
22            oedepict.OEAddHighlighting(rdisp, color, highlightstyle, product)

The IsReactantComponent function identifies whether a given component is a reactant in which case it returns True.

1def IsReactantComponent(rmol, partpred):
3    atom = rmol.GetAtom(oechem.OEAndAtom(partpred, oechem.OEAtomIsInReactant()))
4    return (atom is not None)

The GetProductAtoms function is used to identify the product atoms of the reaction that are mapped to the atoms of the given reactant. Two atoms are mapped to each other if they have the same index returned by the OEAtomBase.GetMapIdx method.

 1def GetProductAtoms(rmol, reactant):
 3    product = oechem.OEAtomBondSet()
 4    for ratom in reactant.GetAtoms():
 5        mapidx = ratom.GetMapIdx()
 6        if mapidx != 0:
 7            patom = rmol.GetAtom(oechem.OEAndAtom(oechem.OEHasMapIdx(mapidx),
 8                                                  oechem.OEAtomIsInProduct()))
 9            if patom is not None:
10                product.AddAtom(patom)
12    return product

In order to highlight not only the atoms but also the bonds of the reaction the AddBondsBetweenAtoms function is called. This adds a bond to the given OEAtomBondSet object if both of its end atoms belong to the same component.

1def AddBondsBetweenAtoms(rmol, abset):
3    pred = oechem.OEIsAtomMember(abset.GetAtoms())
4    for bond in rmol.GetBonds():
5        if pred(bond.GetBgn()) and pred(bond.GetEnd()):
6            abset.AddBond(bond)

Download code and supporting data set ugi.rxn


prompt > python3 ugi.rxn ugi.png

See also

See also in OEChem TK manual



See also in OEDepict TK manual