{
  "version": "https://jsonfeed.org/version/1.1",
  "title": "chem-bla-ics",
  "description": "Chemblaics (pronounced chem-bla-ics) is the science that uses open science and computers to solve problems in chemistry, biochemistry and related fields.",
  "home_page_url": "https://chem-bla-ics.linkedchemistry.info/",
  "feed_url": "https://chem-bla-ics.linkedchemistry.info/2005/12/16/cdk-debug-classes-and-fixing.json",
  "icon": "https://chem-bla-ics.linkedchemistry.info/assets/images/chem-bla-ics_logo.png",
  "language": "en",
  "authors": [
    {
      "name": "Egon Willighagen",
      "url": "https://orcid.org/0000-0001-7542-0286",
      "_orcid": "0000-0001-7542-0286"
    }
  ],
  "items": [

    {
      "id": "https://doi.org/10.59350/6p49t-sj396",
      "url": "https://chem-bla-ics.linkedchemistry.info/2005/12/16/cdk-debug-classes-and-fixing.html",
      "title": "CDK Debug classes and fixing the ModelBuilder3D bug",
      "content_html": "<p>For some weeks now I have been thinking about bug <a href=\"https://sourceforge.net/tracker/index.php?func=detail&amp;aid=1309731&amp;group_id=20024&amp;atid=120024\">1309731</a>:\n“ModelBuilder3D overwrites Atom IDs”. The <a href=\"http://cvs.sourceforge.net/viewcvs.py/cdk/cdk/src/org/openscience/cdk/modeling/builder3d/ModelBuilder3D.java?rev=1.23&amp;view=markup\">ModelBuilder3D</a>\nis a complex piece of source code, reusing many other parts of the CDK, including\n<a href=\"http://cdk.sourceforge.net/api/org/openscience/cdk/atomtype/package-summary.html\">atom type perception</a>.</p>\n\n<p>Somewhere in October, however, I found that Taverna could not create 3D models and convert these into reasonable CML because the Atom ID’s were messed up. So the question is, where did the\nModelBuilder3D do this? Did it do this itself, or is it done by one of the other pieces of CDK that it uses? But due to the complex nature of this algorithm, it quickly became clear\nthat looking at the code was not going to solve it; there was too much code to look at.</p>\n\n<p>The solution was clear to me: use the [new data interfaces <i class=\"fa-solid fa-recycle fa-xs\">](https://chem-bla-ics.linkedchemistry.info/2005/10/25/more-cdkinterfaces-updates.html).\nTo identify where the IDs where messed up, I only needed to write a DebugAtom class with a method that looked like:</i></p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">public</span> <span class=\"kt\">void</span> <span class=\"nf\">setID</span><span class=\"o\">(</span><span class=\"nc\">String</span> <span class=\"n\">identifier</span><span class=\"o\">)</span> <span class=\"o\">{</span>\n  <span class=\"n\">logger</span><span class=\"o\">.</span><span class=\"na\">debug</span><span class=\"o\">(</span><span class=\"s\">\"Setting ID: \"</span><span class=\"o\">,</span> <span class=\"n\">identifier</span><span class=\"o\">);</span>\n  <span class=\"kd\">super</span><span class=\"o\">.</span><span class=\"na\">setID</span><span class=\"o\">(</span><span class=\"n\">identifier</span><span class=\"o\">);</span>\n<span class=\"o\">}</span>\n</code></pre></div></div>\n\n<p>And I would immediately at what stage the ID was overwritten.</p>\n\n<p>So I started this week to implement the <a href=\"http://cvs.sourceforge.net/viewcvs.py/cdk/cdk/src/org/openscience/cdk/debug/DebugAtom.java?rev=1.1&amp;view=markup\">DebugAtom</a> and related classes.\nBy extending <code class=\"language-plaintext highlighter-rouge\">Atom</code>, I could just add debugging stuff and reuse the code in that class. However, the <code class=\"language-plaintext highlighter-rouge\">DebugAtom</code> can not extend <code class=\"language-plaintext highlighter-rouge\">DebugAtomType</code> too then. And this is a pity,\nbecause all methods inherited by the <code class=\"language-plaintext highlighter-rouge\">Atom</code> interface from <code class=\"language-plaintext highlighter-rouge\">AtomType</code>, <code class=\"language-plaintext highlighter-rouge\">Isotope</code>, <code class=\"language-plaintext highlighter-rouge\">Element</code> and <code class=\"language-plaintext highlighter-rouge\">ChemObject</code> interfaces could not be inherited from the <code class=\"language-plaintext highlighter-rouge\">DebugAtomType</code> class.\nInstead, they now have to duplicate those bits of code.</p>\n\n<p>This is not a clean solution, as duplicate code is a known cause of bugs. So, the next step was to write JUnit tests for the new debug classes. And for this\nI wanted to reuse, i.e. extend, the tests for the default data classes. This required, however, changes to those test classes.</p>\n\n<p>The first thing that needed to be changed was that instantiation of data classes in the tests would now have to depend on the data classes being tested. A simple</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nc\">Atom</span> <span class=\"n\">atom</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nc\">Atom</span><span class=\"o\">(</span><span class=\"s\">\"C\"</span><span class=\"o\">);</span>\n</code></pre></div></div>\n\n<p>only makes sense when a specific <code class=\"language-plaintext highlighter-rouge\">Atom</code> class was important. Fortunately, the new interfaces provide a solution for this: the <code class=\"language-plaintext highlighter-rouge\">ChemObjectBuilder</code> implementations.\nThese allow to use the following syntax to replace the hard coded instantiation:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nc\">Atom</span> <span class=\"n\">atom</span> <span class=\"o\">=</span> <span class=\"n\">builder</span><span class=\"o\">.</span><span class=\"na\">newAtom</span><span class=\"o\">(</span><span class=\"s\">\"C\"</span><span class=\"o\">);</span>\n</code></pre></div></div>\n\n<p>Therefore, I added a protected field to the <code class=\"language-plaintext highlighter-rouge\">AtomTest</code>, which was instantiated in the <code class=\"language-plaintext highlighter-rouge\">setUp()</code>:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">protected</span> <span class=\"nc\">ChemObjectBuilder</span> <span class=\"n\">builder</span><span class=\"o\">;</span>\n<span class=\"kd\">public</span> <span class=\"kt\">void</span> <span class=\"nf\">setUp</span><span class=\"o\">()</span> <span class=\"o\">{</span>\n  <span class=\"n\">builder</span> <span class=\"o\">=</span> <span class=\"nc\">DefaultChemObjectBuilder</span><span class=\"o\">.</span><span class=\"na\">getInstance</span><span class=\"o\">();</span>\n<span class=\"o\">}</span>\n</code></pre></div></div>\n\n<p>and use this builder to instantiate all test objects, as shows for the atom above.</p>\n\n<p>And then I can simply reuse this JUnit test by defining the <code class=\"language-plaintext highlighter-rouge\">DebugAtomTest</code> like:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"kd\">public</span> <span class=\"kd\">class</span> <span class=\"nc\">DebugAtomTest</span> <span class=\"kd\">extends</span> <span class=\"nc\">AtomTest</span> <span class=\"o\">{</span>\n  <span class=\"kd\">public</span> <span class=\"nf\">DebugAtomTest</span><span class=\"o\">(</span><span class=\"nc\">String</span> <span class=\"n\">name</span><span class=\"o\">)</span> <span class=\"o\">{</span>\n    <span class=\"kd\">super</span><span class=\"o\">(</span><span class=\"n\">name</span><span class=\"o\">);</span>\n  <span class=\"o\">}</span>\n\n  <span class=\"kd\">public</span> <span class=\"kt\">void</span> <span class=\"nf\">setUp</span><span class=\"o\">()</span> <span class=\"o\">{</span>\n    <span class=\"kd\">super</span><span class=\"o\">.</span><span class=\"na\">builder</span> <span class=\"o\">=</span> <span class=\"nc\">DebugChemObjectBuilder</span><span class=\"o\">.</span><span class=\"na\">getInstance</span><span class=\"o\">();</span>\n  <span class=\"o\">}</span>\n\n  <span class=\"kd\">public</span> <span class=\"kd\">static</span> <span class=\"nc\">Test</span> <span class=\"nf\">suite</span><span class=\"o\">()</span> <span class=\"o\">{</span>\n    <span class=\"k\">return</span> <span class=\"k\">new</span> <span class=\"nf\">TestSuite</span><span class=\"o\">(</span><span class=\"nc\">DebugAtomTest</span><span class=\"o\">.</span><span class=\"na\">class</span><span class=\"o\">);</span>\n  <span class=\"o\">}</span>\n<span class=\"o\">}</span>\n</code></pre></div></div>\n\n<p>The sources for these debug data classes tests are found in the new <code class=\"language-plaintext highlighter-rouge\">cdk.test.debug</code> package.</p>\n\n<p>The number of JUnit tests for the CDK jumped from around 1250 to over 1500 tests right now. And if you think these new\ntests only test old code, because of all the <code class=\"language-plaintext highlighter-rouge\">super.bla()</code> calls in the debug classes, you’re way off. I found bugs in the\nnew debug classes, but <strong>also</strong> many class cast bugs and several other problems in the real data classes!</p>\n\n<p>Anyway. Does this help fix the <code class=\"language-plaintext highlighter-rouge\">ModelBuilder3D</code> bug? Yes, it does:</p>\n\n<div class=\"language-shell highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nv\">$ </span><span class=\"nb\">grep</span> <span class=\"s2\">\"Setting ID\"</span> reports/result.modeling.builder3d.ModelBuilder3dTest.txt\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: carbon1\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: oxygen1\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: C\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: HC\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: HC\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: HC\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: O\norg.openscience.cdk.debug.DebugAtom DEBUG: Setting ID: HO\n</code></pre></div></div>\n\n<p>This shows me where the <code class=\"language-plaintext highlighter-rouge\">Atom</code> ID is overwritten to be something other than “carbon1”! I can now look at the rest of the\n<code class=\"language-plaintext highlighter-rouge\">result.modeling.builder3d.ModelBuilder3dTest.txt</code> file to see what the <code class=\"language-plaintext highlighter-rouge\">ModelBuilder3D</code> was doing at the time,\nand which CDK class made the <code class=\"language-plaintext highlighter-rouge\">setID()</code> call.</p>\n\n<p>I only needed to change this line in the JUnit test for the bug to generate the above debug lines:</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nc\">Molecule</span> <span class=\"n\">methanol</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nc\">Molecule</span><span class=\"o\">();</span>\n</code></pre></div></div>\n\n<p>into</p>\n\n<div class=\"language-java highlighter-rouge\"><div class=\"highlight\"><pre class=\"highlight\"><code><span class=\"nc\">Molecule</span> <span class=\"n\">methanol</span> <span class=\"o\">=</span> <span class=\"k\">new</span> <span class=\"nc\">DebugMolecule</span><span class=\"o\">();</span>\n</code></pre></div></div>",
      "summary": "For some weeks now I have been thinking about bug 1309731: “ModelBuilder3D overwrites Atom IDs”. The ModelBuilder3D is a complex piece of source code, reusing many other parts of the CDK, including atom type perception.",
      
      "date_published": "2005-12-16T00:00:00+00:00",
      "date_modified": "2024-03-23T00:00:00+00:00",
      "tags": ["cdk","cheminf"],
      
      
      
      
      
      
        "authors": [ { "name": "Egon Willighagen", "url": "https://orcid.org/0000-0001-7542-0286" } ]
      
    }

  ]
}
