Chapter 1
Writing CDK scripts
1.1 Groovy
Groovy (
) is a programming language that advertizes itself as
is an agile and dynamic language for the Java Virtual Machine
. Indeed, like BeanShellis provides an environment to quickly try Java code. However, unlike BeanShell, it providemore linguistic changes and adds quite interesting sugar to that Java language.A simple script may look like:
for (IAtom atom : molecule.atoms()) {System.out.println(atom.getSymbol());}
But in Groovy it can also look like:
for (atom in molecule.atoms()) {println atom.getSymbol()}
Groovy needs to be made aware of the CDK, which uses the common CLASSPATHapproach for doing this. To start the GUI console shown in Figure1.1:
$ CLASSPATH=cdk-1.2.0.jar groovyConsole
1.1.1 Closures
However, one of the more interesting features of Groovy is the
closure
s. I have know theprogramming pattern from R and happily used for a long time, but only recently learned thisto be called closures. Closures allow you to pass method as parameter, which can have manyapplications, and I will show one situation here.Consider the calculation of molecular properties which happen to be a mere summationover atomic properties, such as the total charge, or the molecular weight. Both these calcu-lations require an iteration over all atoms. If we need those properties at the same time, wecan combine the calcultion into one iteration. However, let’s generalize the situation a bitand assume they are not. Therefore, we have to slices of code which share a large amount of data:1
Leave a Comment