<?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: JavaScript for Cheminformatics: Using the Prototype Framework</title>
    <link>http://depth-first.com/articles/2008/08/26/javascript-for-cheminformatics-using-the-prototype-framework</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>JavaScript for Cheminformatics: Using the Prototype Framework</title>
      <description>&lt;p&gt;&lt;a href="http://www.prototypejs.org/"&gt;&lt;img src="http://depth-first.com/demo/20080826/prototype.png" align="right"&gt;&lt;/img&gt;&lt;/a&gt;If you want to do the kind of cheminformatics that involves manipulating atoms, bonds, and molecules, object-oriented programming works well as a development paradigm. Although perhaps not readily apparent, JavaScript &lt;a href="http://depth-first.com/articles/2008/07/15/javascript-for-cheminformatics"&gt;offers everything&lt;/a&gt; needed to create object-oriented models just as intricate at those written in languages like C++ and Java. This article discusses one approach that makes use of the &lt;a href="http://www.prototypejs.org/"&gt;Prototype Framework&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;About Prototype&lt;/h4&gt;

&lt;p&gt;Prototype is a set of extensions to the JavaScript language that make developing in it less painful. Some of the extensions relate to DOM manipulation. Other have to do with the way Strings and Arrays behave. For the purposes of this article, we'll be using Prototype's syntax support for classes and inheritance.&lt;/p&gt;

&lt;h4&gt;Atoms, Bonds, and Molecules&lt;/h4&gt;

&lt;p&gt;To start, we'll need classes that define the basic behavior of atoms, bonds, and molecules. Although we may ultimately need to consider issues such as &lt;a href="http://depth-first.com/articles/2006/12/19/ferrocene-and-beyond-a-solution-to-the-molecular-representation-problem"&gt;multicentered bonding&lt;/a&gt;, for now, we'll stick to a simplified view of chemistry that has bonds connecting two and only two atoms.&lt;/p&gt;

&lt;h4&gt;Creating the Classes&lt;/h4&gt;

&lt;p&gt;We could use JavaScript's built-in method for creating objects from class-like structures, but Prototype's approach is cleaner.&lt;/p&gt;

&lt;p&gt;In the following library, we define a Molecule as a collection of Atoms and Bonds with useful relationships:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_javascript "&gt;var Molecule = Class.create({
  initialize: function(){
    this.atoms = [];
    this.bonds = [];
  },

  addAtom: function(label){
    var atom = new Atom(label);

    this.atoms.push(atom);

    return atom;
  },

  connect: function(sourceAtom, targetAtom, bondType){
    var bond = new Bond(sourceAtom, targetAtom, bondType);

    sourceAtom.neighbors.push(targetAtom);
    sourceAtom.bonds.push(bond);
    targetAtom.neighbors.push(sourceAtom);
    targetAtom.bonds.push(bond);

    this.bonds.push(bond);

    return bond;
  }
});

var Atom = Class.create({
  initialize: function(label){
    this.label = label;
    this.neighbors = [];
    this.bonds = [];
  }
});

var Bond= Class.create({
  initialize: function(source, target, type){
    this.source = source;
    this.target = target;
    this.type = type;
  },

  getMate: function(atom){
    if (atom == this.source) return this.target;
    if (atom == this.target) return this.source;

    return null;
  },

  contains: function(atom){
    return (atom == this.source || atom == this.target);
  }
});&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;h4&gt;Testing the Library&lt;/h4&gt;

&lt;p&gt;We can test the library interactively by saving it in a file called &lt;strong&gt;chem.js&lt;/strong&gt; and creating some simple HTML:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_xml "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;html&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;head&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;script&lt;/span&gt; &lt;span class="attribute"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;text/javascript&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;prototype.js&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;script&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;script&lt;/span&gt; &lt;span class="attribute"&gt;type&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;text/javascript&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;src&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;chem.js&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;script&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;head&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;body&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;body&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;html&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We can then use the &lt;a href="http://addons.mozilla.org/en-US/firefox/addon/1843"&gt;Firebug console&lt;/a&gt; to test the library interactively:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; m = new Molecule();
Object atoms=[0] bonds=[0]
&amp;gt;&amp;gt;&amp;gt; c = m.addAtom("C");
Object label=C neighbors=[0] bonds=[0]
&amp;gt;&amp;gt;&amp;gt;n = m.addAtom("N");
Object label=N neighbors=[0] bonds=[0]
&amp;gt;&amp;gt;&amp;gt; m.connect(c, n, 3);
&amp;gt;&amp;gt;&amp;gt; c.neighbors.size()
1
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;p&gt;Although the cheminformatics library discussed here is far from being useful, it's not difficult to see how to extend it. Prototype offers a several possibilities for doing so.&lt;/p&gt;</description>
      <pubDate>Tue, 26 Aug 2008 14:01:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9e437bb7-d41a-4e97-93a9-4e6a0ced3c1c</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/08/26/javascript-for-cheminformatics-using-the-prototype-framework</link>
      <category>Tools</category>
      <category>javascript</category>
      <category>prototype</category>
      <category>atom</category>
      <category>molecule</category>
      <category>bond</category>
      <category>flexmol</category>
    </item>
    <item>
      <title>"JavaScript for Cheminformatics: Using the Prototype Framework" by Kris Brower</title>
      <description>&lt;p&gt;Very cool, I never considered using prototype for this sort of thing. I wonder how hard it would be to use prototype to visualize these structures in css...I think we can ignore browser cross compatibility for that task.&lt;/p&gt;</description>
      <pubDate>Wed, 27 Aug 2008 13:02:27 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7b1055f3-b669-4db6-9578-b620e1059ac2</guid>
      <link>http://depth-first.com/articles/2008/08/26/javascript-for-cheminformatics-using-the-prototype-framework#comment-700</link>
    </item>
  </channel>
</rss>
