Pages

Thursday, June 10, 2010

CDK-JChemPaint #6: rendering atom numbers

I have made a few new CDK-JChemPaint patches in the past two days, the latest being patch 15. With the help from Gilleain, all rendering parameters are now using the new API, as explained earlier in this series.

Additionally, the API to work with rendering parameters is now much simpler. The previous posts did not really explain how to tune parameters, so here goes. One important thing to realize, is that a rendering parameter can only be changed if the generator that defines it has been registered. To see what parameters belongs to what generator, for which you can use the script discussed in post #3.


Atom Numbers
In some situations you like to draw atom numbers. This can be done by replacing the BasicAtomGenerator by an AtomNumberGenerator in the script given in post #1:
List generators = new ArrayList();
generators.add(new BasicSceneGenerator());
generators.add(new BasicBondGenerator());
generators.add(new AtomNumberGenerator());

This would result in an image like this:

Now, we also might want to give those numbers a color, to make them stand out a bit. Orange, perhaps :) This is where rendering parameters come in. To the code from post #1, after the instantiation of the renderer, we add:
// tune parameters
model = renderer.getRenderer2DModel();
model.set(
  AtomNumberGenerator.AtomNumberTextColor.class,
  Color.orange
);
The output then looks like:


Atom Numbers and Symbols
But you can also render both element symbols and numbers. Then, clearly, you just add both generators:
List generators = new ArrayList();
generators.add(new BasicSceneGenerator());
generators.add(new BasicAtomGenerator());
generators.add(new BasicBondGenerator());
generators.add(new AtomNumberGenerator());
But, in order to have the label and the symbol not overlap, we define an offset (Thanx to Sebastian, of MetFrag fame, for the feature request!):
model.set(
  AtomNumberGenerator.Offset.class,
  new javax.vecmath.Vector2d(10,10)
);
Then it gets to look like:
This last full example will be available from GitHub shortly.

Update Steffen asked in the comments if it is possible to just color the atoms by element type. CDK-JChemPaint patch 15 does not allow that, but adding that feature is easy enough, and the patch will be part of the next release. Use this configuration:
generators.add(new BasicSceneGenerator());
generators.add(new BasicBondGenerator());
generators.add(new AtomNumberGenerator());
And these parameter settings:
model = renderer.getRenderer2DModel();
model.set(
  AtomNumberGenerator.ColorByType.class,
  true
);
This give you for triazole:

8 comments:

  1. But having C in black, N in blue and O in red would be asked too much ?
    I'd probably have to write an own implementation then.

    Yours,
    Steffen

    ReplyDelete
  2. Sorry, I meant the *numbers* for C,N,O in colors...

    ReplyDelete
  3. Sure. One of the powers of the new JChemPaint is that the code base is much easier to work with. See the updates in the blog post for the patch I just hacked up, which I will commit as part of the next release.

    ReplyDelete
  4. Sure, you would have to make a FancyNumberGenerator (or some such name) that used BasicAtomGenerator.ColorByType to get the color for the atom.

    ReplyDelete
  5. I am looking at that enumeration + atom type image and can't help thinking that the numbers should be outside the molecule on all sides instead of just a simple offset. I mean if you put the numbers outside the atoms just like you would with a binding wouldn't that just look so much more awesome?

    ReplyDelete
  6. Jonathan, how would that work when atoms are buried inside the molecule, as in complexer ring systems?

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. I've just began using jchempaint and find it good. but i was looking for something where i can mark bond angles and torsion angles. i am not very good at coding. but if i am not asking too much, can you guys fix it?

    ReplyDelete