<?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: A Simple Vector Graphics API for Chemical Structure Output Part 1: In Search of a Simplifying Approach for ChemPhoto</title>
    <link>http://depth-first.com/articles/2008/10/31/a-simple-vector-graphics-api-for-chemical-structure-output-part-1-in-search-of-a-simplifying-approach-for-chemphoto</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>A Simple Vector Graphics API for Chemical Structure Output Part 1: In Search of a Simplifying Approach for ChemPhoto</title>
      <description>&lt;p&gt;&lt;a href="http://flickr.com/photos/estherase/2584941208/"&gt;&lt;img src="http://depth-first.com/demo/20081031/layers.jpg" align="right"&gt;&lt;/img&gt;&lt;/a&gt;One of the main design goals of &lt;a href="http://metamolecular.com/chemphoto"&gt;ChemPhoto&lt;/a&gt;, the &lt;a href="http://depth-first.com/articles/2008/09/08/smarter-cheminformatics-from-sd-file-to-image-collection-with-chemphoto"&gt;chemical structure imaging application&lt;/a&gt;, was to support all Web-relevant image output formats, both vector-based and pixel-based. Like most things in software development, there are far more approaches that add complexity to this problem than there are approaches that remove it. And for some reason, the complexity-reducing methods tend to be the last to be considered. This article, the first in a series, will discuss how ChemPhoto simplifies the problem of supporting multiple chemical structure image output formats from a common representation.&lt;/p&gt;

&lt;h4&gt;The Problem in a Nutshell&lt;/h4&gt;

&lt;p&gt;ChemPhoto uses an internal representation of molecular structure based closely on the industry-standard &lt;a href="http://www.mdl.com/downloads/public/ctfile/ctfile.pdf"&gt;MDL molfile format&lt;/a&gt;. Given this representation, ChemPhoto needs to be able to write a variety of vector- and raster-based image formats. Raster formats are fortunately limited to PNG and JPG, which are supported directly by the standard Java library.&lt;/p&gt;

&lt;p&gt;Vector formats, on the other hand are more diverse and less accessible. Currently, ChemPhoto supports Scalable Vector Graphics (SVG) and Encapsulated PostScript (EPS). Complete support for Adobe Flash (SWF) output is expected soon. Proof of concept for Microsoft's Vector Markup Language (VML) &lt;a href="http://depth-first.com/articles/2008/07/22/vector-markup-language-for-cheminformatics"&gt;has already been demonstrated&lt;/a&gt;. Support for Adobe Acrobat format, through the &lt;a href="http://www.lowagie.com/iText/"&gt;iText library&lt;/a&gt; is anticipated. Last but not least is Java2D itself for use in Swing components such as &lt;a href="http://metamolecular.com/chemwriter"&gt;renderers and editors&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Clearly, supporting all of these formats requires rendering code that is minimally coupled to the underlying display system. But how to do this in practice?&lt;/p&gt;

&lt;h4&gt;The Batik Approach: Extend Graphics2D&lt;/h4&gt;

&lt;p&gt;&lt;a href="http://xmlgraphics.apache.org/batik/"&gt;Batik&lt;/a&gt; is a widely-used library for creating and processing SVG in Java. At its core is the &lt;a href="http://xmlgraphics.apache.org/batik/using/svg-generator.html"&gt;SVGGraphics2D class&lt;/a&gt; which extends &lt;a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics2D.html"&gt;Graphics2D&lt;/a&gt;, overriding many of its methods in the process. The idea seems simple enough - create your drawing code using the Java2D API like you normally would. When you want to generate SVG, just pass an instance of &lt;tt&gt;SVGGraphics2D&lt;/tt&gt; and then read out the SVG document using &lt;tt&gt;stream&lt;/tt&gt; method.&lt;/p&gt;

&lt;p&gt;The problem with this approach is that every new image output format to be supported needs to extend Graphics2D and essentially re-implement most of its methods. Graphics2D is a large and complex class with many associated helper classes. Just knowing when you've completely covered the API is a major challenge, aside from the even bigger challenge of implementing the overridden methods.&lt;/p&gt;

&lt;p&gt;Fine, you might say, given that so many SVG interconverters exist, why not just use SVG (created by Batik) as the universal interconversion format and get a third-party-library to convert SVG into other vector formats?&lt;/p&gt;

&lt;p&gt;This approach is appealing in principle, but fails in practice. Many SVG implementations are partial at best - and many lack the documentation that would warn that a problem might exist with the exact form of SVG you're using. For example, in an early iteration of ChemPhoto, Batik was used to create SVG from some representative chemical structures. Unfortunately, the way Batik represented path data was not fully interpreted by any of the SVG-&gt;SWF converters that were examined. The result was bumpy instead of smooth curves for atom labels, and other unacceptable abnormalities.&lt;/p&gt;

&lt;p&gt;Finally, after spending some time reading J. David Eisenberg's &lt;a href="http://oreilly.com/catalog/9780596002237/toc.html"&gt;excellent book about SVG&lt;/a&gt;, it became clear that for drawing 2D chemical structures and even reactions and reaction schemes, only a fraction of the SVG specification was relevant.&lt;/p&gt;

&lt;p&gt;In this case, Batik, and its approach of extending Graphics2D was simply overkill that made the problem more complex than it needed to be.&lt;/p&gt;

&lt;h4&gt;A Better Approach: Create a Custom Vector Graphics Interface&lt;/h4&gt;

&lt;p&gt;Batik has the right idea: isolate drawing code from the specific format being generated. The problem is that the Graphics2D class wasn't really designed for this purpose. For one thing, it's a concrete class that inherits from another concrete class. And as mentioned before, Graphics2D a very complex class with many dependencies.&lt;/p&gt;

&lt;p&gt;How can we create a simple vector graphics API tailored to chemical structure image creation, which is easily re-implemented, and which works with the existing Java2D API?&lt;/p&gt;

&lt;p&gt;Part 2 of this series will describe one approach.&lt;/p&gt;

&lt;h4&gt;Conclusions&lt;/h4&gt;

&lt;p&gt;Creating the ChemPhoto rendering engine has been an evolutionary process. It started with the idea of directly using the Graphics2D class in rendering code, but has since moved on to the definition of a vector graphics abstraction layer to simplify the addition of new image formats.&lt;/p&gt;

&lt;p&gt;I'd like to thank those beta testers who have already offered valuable feedback on ChemPhoto. If you'd like an unlimited 30-day trial for yourself, please &lt;a href="http://mailhide.recaptcha.net/d?k=01R9bxyP6XNdc0duoUCzBBHA==&amp;amp;c=vZ7R0VDctRzIRzbSs1-LZwDzjTjAnfCS4KONqGHxY9I=" onclick="window.open('http://mailhide.recaptcha.net/d?k=01R9bxyP6XNdc0duoUCzBBHA==&amp;amp;c=vZ7R0VDctRzIRzbSs1-LZwDzjTjAnfCS4KONqGHxY9I=', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;" title="Reveal this e-mail address"&gt;drop me a line.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Credit: &lt;a href="http://flickr.com/photos/estherase/"&gt;estherase&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 31 Oct 2008 18:25:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ca61daeb-1523-4f0e-a4f6-f95a9d69f14e</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/10/31/a-simple-vector-graphics-api-for-chemical-structure-output-part-1-in-search-of-a-simplifying-approach-for-chemphoto</link>
      <category>Tools</category>
      <category>chemphoto</category>
      <category>chemicalstructureimaging</category>
      <category>vectorgraphics</category>
      <category>pdf</category>
      <category>svg</category>
      <category>vml</category>
      <category>eps</category>
      <category>png</category>
      <category>jpg</category>
    </item>
    <item>
      <title>"A Simple Vector Graphics API for Chemical Structure Output Part 1: In Search of a Simplifying Approach for ChemPhoto" by Gilleain Torrance</title>
      <description>&lt;p&gt;Hi Rich,&lt;/p&gt;

&lt;p&gt;The discussion has mostly been on the JChempaint mailing list, and through the medium of checking code into experimental branches!&lt;/p&gt;

&lt;p&gt;It is turning out to be quite hard to find the right balance between flexibility and efficiency in the abstraction layer.&lt;/p&gt;

&lt;p&gt;Also, there is a danger of any such layer turning into a general drawing framework, which is something to be resisted..&lt;/p&gt;

&lt;p&gt;gilleain&lt;/p&gt;</description>
      <pubDate>Sun, 02 Nov 2008 16:37:55 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:84d4e7d3-4fc9-474f-b5fc-b51441ce7112</guid>
      <link>http://depth-first.com/articles/2008/10/31/a-simple-vector-graphics-api-for-chemical-structure-output-part-1-in-search-of-a-simplifying-approach-for-chemphoto#comment-875</link>
    </item>
    <item>
      <title>"A Simple Vector Graphics API for Chemical Structure Output Part 1: In Search of a Simplifying Approach for ChemPhoto" by Rich Apodaca</title>
      <description>&lt;p&gt;Hello Egon,&lt;/p&gt;

&lt;p&gt;I had seen &lt;a href="http://gilleain.blogspot.com/" rel="nofollow"&gt;Gilleain's Blog&lt;/a&gt;, which looks very interesting, but not a discussion of a vector graphics abstraction layer - I probably missed it. Could you give some direct links to that discussion?&lt;/p&gt;</description>
      <pubDate>Sat, 01 Nov 2008 15:52:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a403f7c0-d2d8-45bb-a85d-26b3dc978231</guid>
      <link>http://depth-first.com/articles/2008/10/31/a-simple-vector-graphics-api-for-chemical-structure-output-part-1-in-search-of-a-simplifying-approach-for-chemphoto#comment-874</link>
    </item>
    <item>
      <title>"A Simple Vector Graphics API for Chemical Structure Output Part 1: In Search of a Simplifying Approach for ChemPhoto" by Egon Willighagen</title>
      <description>&lt;p&gt;Hi Rich, the JChemPaint team is working on something quite similar, as we aim to support not just Java2D, but SVG (...) and SWT (Eclipse) too. Check Gilleain's blog, for example:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://gilleain.blogspot.com/" rel="nofollow"&gt;http://gilleain.blogspot.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're making good progress and hope to deliver first applications later this month. I'd just love to see you on board. The rendering code is LGPL, and possibly integrate with your Metamolecular efforts...&lt;/p&gt;</description>
      <pubDate>Sat, 01 Nov 2008 08:31:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:90d9d439-e0ab-420d-8bef-f3c13c5296ba</guid>
      <link>http://depth-first.com/articles/2008/10/31/a-simple-vector-graphics-api-for-chemical-structure-output-part-1-in-search-of-a-simplifying-approach-for-chemphoto#comment-873</link>
    </item>
  </channel>
</rss>
