<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Depth-First: Drawing 2-D Structures with Structure-CDK</title>
    <link>http://depth-first.com/articles/2006/08/28/drawing-2-d-structures-with-structure-cdk</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>Drawing 2-D Structures with Structure-CDK</title>
      <description>&lt;p&gt;Rendering 2-D molecular structures is a fundamental part of chemical informatics. It's used in building end user systems, and more immediately, it can be critical for creating and debugging developer tools.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://cdk.sf.net"&gt;Chemistry Development Kit&lt;/a&gt; (CDK) is a highly-functional chemical informatics library written in Java. Although it provides built-in 2-D rendering capabilities through the &lt;tt&gt;org.openscience.cdk.renderer&lt;/tt&gt; package, I wanted something a little easier for me to customize. The result is &lt;a href="http://structure.sf.net"&gt;Structure-CDK&lt;/a&gt;, a 37K add-on library for the CDK. This article discusses the main features of Structure-CDK with some screenshots and code.&lt;/p&gt;

&lt;p&gt;To begin using Structure-CDK, &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=103744&amp;amp;package_id=202103&amp;amp;release_id=443008"&gt;download&lt;/a&gt; the current release. This package contains a complete copy of the most recent CDK release, so there is nothing else to install or download. Structure-CDK was developed with JDK-1.5.0. Because it contains no 1.5-specific features, it may work on earlier Java versions. &lt;a href="http://ant.apache.org/"&gt;Ant&lt;/a&gt; is useful, but not essential.&lt;/p&gt;

&lt;p&gt;The packages contains an interactive viewing application, which can be invoked with the "vis" Ant task:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ ant vis
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Two types of molecules can be viewed. The first consists of those defined in &lt;tt&gt;org.openscience.cdk.templates.MoleculeFactory&lt;/tt&gt;, which can be found under the &lt;strong&gt;Structure&lt;/strong&gt; menu. 2-D coordinates are provided by CDK's &lt;tt&gt;StructureDiagramGenerator&lt;/tt&gt;. Additionally, molecules can be opened as molfiles (&lt;strong&gt;File-&gt;Open&lt;/strong&gt;), several samples of which are contained in the distribution's &lt;strong&gt;molfiles&lt;/strong&gt; directory. Let's take a look at oseltamivir (Tamiflu).&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/files/oseltamivir_window.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;This view can be changed in a couple of ways. Resizing the window automatically resizes and centers the image, while maintaining proportionality of all measurements. This feature, when used with antialiasing, results in the image staying readable regardless of its size. Additionally, &lt;strong&gt;Edit-&gt;Preferences&lt;/strong&gt; produces a dialog for changing the rendering settings.&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/files/oseltamivir_window_prefs.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Now let's see some code that will read a molecule from a molfile and write a 2-D PNG image to disk. This can be done via the static convenience methods found in &lt;tt&gt;ImageKit&lt;/tt&gt;:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;import java.io.FileReader;

import org.openscience.cdk.io.MDLReader;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.Molecule;

import net.sf.structure.cdk.util.ImageKit;
...

public void writePNG(String pathToMolfile, String pathToPNG) throws Exception
{
  MDLReader mdlReader = new MDLReader(new FileReader(pathToMolfile));
  IMolecule mol = (IMolecule) mdlReader.read(new Molecule());

  ImageKit.writePNG(mol, 300, 300, pathToPNG);
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The above code fragment creates a 300x300 PNG image from the contents of the molfile specified by &lt;tt&gt;pathToMolfile&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;Although several rendering features, both aesthetic and functional, are supported, some are missing. Most importantly, atom labels are only rendered without hydrogen atoms and there is no stereochemistry support. Performance has not been optimized at all. Future versions of Structure-CDK will be aimed at addressing these issues.&lt;/p&gt;

&lt;p&gt;Given the central nature of 2-D structure rendering, it's nice to have options. Structure-CDK provides a convenient, interactive solution. Future articles will discuss the integration of Structure-CDK into more complex chemical informatics systems.&lt;/p&gt;</description>
      <pubDate>Mon, 28 Aug 2006 14:03:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f1d06b2d-cf86-407c-bf5e-0e3b74e150fa</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2006/08/28/drawing-2-d-structures-with-structure-cdk</link>
      <category>Graphics</category>
      <category>cdk</category>
      <category>2d</category>
      <category>render</category>
      <category>molfile</category>
      <category>java</category>
    </item>
  </channel>
</rss>
