CDK Renderer - cannot colour bonds, only atoms?


In the "Substructure Search" Node one can highlight atoms & bonds in a molecule which match a supplied substructure.  I like this functionality (which I'm using to highlight the maximum common substructure between two molecules), so I've largely copied the methodology from the "Substructure Search" node, which uses the CDKConstants.ANNOTATIONS field to colour atoms and bonds.

I've noticed that as of CDK 1.5.3 atoms are coloured, but curiously not bonds (or perhaps more specifically, assigning colours to bonds via CDKConstants.ANNOTATIONS does nothing, yet when assigned to atoms - the bonds, between said coloured atoms, are coloured).

For instance, try performing a substructure search using these 2:

target - C1CCc2ccccc2C1

query - C1=C[CH2](=CC=C1)CCCC

The target in my case has all bonds coloured, despite the fact that the query is actually the target molecule with one of the bonds missing.  This occurs with the "Substructure Search" node.



I'm not sure why it's the case that bonds aren't coloured on their own, but I noticed that a "static" block in the code has this:


static {
        try {
            List<IGenerator<IAtomContainer>> generators = new ArrayList<IGenerator<IAtomContainer>>();
            generators.add(new BasicSceneGenerator());
            generators.add(new SmartRingGenerator());
            generators.add(new SmartExtendedAtomGenerator());
            generators.add(new SmartAtomNumberGenerator());
            renderer = new AtomContainerRenderer(generators, new AWTFontManager());

            setDefaultRendererProps(renderer.getRenderer2DModel(), CDKNodePlugin.showAomaticity());

        } catch (Exception e) {
            LOGGER.error("Error during renderer initialization!", e);

        RENDERER = renderer;

I see no mention of a, which itself contains code that uses the CDKConstants.ANNOTATIONS field.  Unsure if this actually does the colouring though.


Update:  I Note that 1.5.10 is actually the latest version, I got lost on what the latest CDK release actually was.  I'll check this next week when I attempt an update but does anyone else get this?


Thanks in advance,


Hi Ed,

that's indeed odd. The color of the bonds is set on line 160 in the SmartAWTDrawVisitor. The bond generator is only used as parent of the SmartRingGenerator.

After debugging, I believe the bug is a side effect of the ring generator. I haven't tracked it down yet but will take another look over the next few of days.

Please let me know if you manage to make any progress on that front.



Hi Stephan,


In short, this:

bond.setProperty(CDKConstants.ANNOTATIONS, red.getRGB());

does nothing, given that "bond" is an IBond object and "red" is a java.awt.Color object.  However, for atoms they're coloured fine (but as colouring atoms "fills in the gaps" the bonds between are coloured).


TBH for colouring atoms the code works fine.  It's just that basically, I'm seeking to have atom and bond colouring separate.  I thought this was the case before?



Hi Ed,

that's what I thought. The separation between bond and atom coloring must have gone lost quite some time ago. If you look into the CDKCell2 class, the bond colorings are actually not serialized (line 424, colorDfs()). They are purely deduced from the connected atoms. That's why the unmatched bond in your example is colored. It gets assigned the wrong color on deserialization.

Otherwise the bond coloring works just fine. :) The individual functions read the bonds' ANNOTATIONS property correctly.


Hi Stephan,


I see thank you for clarifying that...  Though is it possible that the bond-induced (as opposed to atom-induced) colouring could be re-introduced?  Maybe as a separate function in the subgraph colouring node for example?  I need the latter (that is, bond colouring as opposed to atom colouring) to show the edge-induced MCS between two molecules.

I could of course attempt to do this myself, but your fix would be less messy than mine ;-)



Hi Ed,

it could be. The CDK cell type would have to change to accommodate that feature (without breaking backwards compatibility). The information about bond colors needs to be stored in the auxillary byte array.

I am not sure when I will get around to do that, so in the meantime feel free to give it a go yourself.



Hi Stephan,


I've finally modified to colour bonds manually rather than between atoms.  It's somewhat hacky and ugly but it works and doesn't slow down the computer like speed humps slow down traffic progression.  I can send you my code if you wish.


Hi Ed,

that would be good, thanks.