Sunday 11 March 2012

Accessing the CDK from Jython in Knime

I've been trying to find out how to use the CDK and/or Open Babel from Jython in Knime. Unfortunately, there's no documentation beyond a single example script. It's a bit strange, as having this working would enable a whole host of developers to write nodes, particularly in the area of chemistry where Python has made considerable inroads.

After asking and not receiving any answer, I've tried to hack something together. I haven't yet figured out how to do it properly, but at least I've learnt a bit, and the end result was that I was able to calculate the molecular weights with the CDK as in the following diagram:

Here's the code for the node. It was debugged by keeping the console log file open in gvim, and reloading (:edit) after every run. Also I had to configure the node by selecting the "Script Output" tab, choosing "Append columns to input table spec", and adding an Output column of type Double:

The way I did this was by hacking the source code for the Jython node itself. First I figured out how to compile it (YMMV). The source is available, but for some reason various files are missing that prevent it compiling out of the box (e.g. the plugin.xml). So I ran the Create Node Wizard (manipulator), copied over various files from the source, copied over various files from the binary (the files in lib and schema), added the lib/jython.jar (as described here), and installed the org.python.plugin node (not forgetting to remove the duplicate org.knime.ext.jython).

To add the CDK jar to the Classpath, I placed it in the lib directory, repeated the procedure described above for lib/jython.jar, and then added the following hack to PythonScriptNodeModel.execute:
classpath.append("/C:/workspace/Jython/lib/cdk-1.4.5.jar");
If there's someone from Knime reading this, please let me know the correct way of doing this. I know that there's an extension point for extending the Classpath of the Jython node, but how is this used?

4 comments:

  1. I do not think it is possible to make it for OpenBabel - unfortunatelly jython does not support C-Extensions atm.

    ReplyDelete
  2. @Santi: Actually, that part is not a problem. Open Babel has Java bindings (using the Java Native Interface), and I have used them from Jython. See my Cinfony project.

    ReplyDelete
  3. This is really nice. I've always been a bit disappointed by the cdk implementation in knime but this might help. On the other hand, one of the nice things of knime is you need less code.

    ReplyDelete