Working with SDFs using MX and groovy

Rich Apodaca recently introduced MX, a lightweight cheminformatics toolkit. He also recently published a quick demo to demonstrate how easy it was to use MX to read and write SDFiles using jruby to script it.

I wanted to replicate the functionality in groovy, another scripting language that runs on the JVM — one I’ve recently been using quite extensively (in conjunction with its web development framework called, inevitably, grails).

Here’s what I did to get this working

  1. Download mx
    $ wget wget http://mx-java.googlecode.com/files/mx-0.107.0.jar
  2. Copy to ~/.groovy/lib
    $ cp mx-0.107.0.jar ~/.groovy/lib
  3. Test reading an SD file in groovy (this is transliterated from the example provided by Mr Apodaca)
    $ groovysh
    Groovy Shell (1.5.6, JVM: 1.6.0_0-b12)
    Type 'help' or '\h' for help.
    -------------------------------------------------------------------------------
    groovy:000> import com.metamolecular.mx.io.mdl.SDFileReader
    groovy:000> r = new SDFileReader("example.sdf")
    ===> com.metamolecular.mx.io.mdl.SDFileReader@4e2892b
    groovy:000> r.nextRecord()
    ===> null
    groovy:000> m = r.molecule
    ===> com.metamolecular.mx.model.DefaultMolecule@64c272bc
    groovy:000> m.countAtoms()
    ===> 20
    groovy:000> r.keys.each{ key -> println "$key : ${r.getData(key)}" }
    Mol Weight : 275.2203
    Formula : C11H9N5O4
    ===> [Mol Weight, Formula]

There you have it! I am impressed with the relatively straightforward API, though it is rather tempting to wrap it inside a groovy builder to make a mini-DSL. SDFileBuilder has a certain ring to it…

Advertisement

Post a Comment

Required fields are marked *

*
*

Follow

Get every new post delivered to your Inbox.