<?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: Atom Typing with Prototype and Iterators</title>
    <link>http://depth-first.com/articles/2008/08/28/javascript-for-cheminformatics-atom-typing-with-prototype-and-iterators</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>JavaScript for Cheminformatics: Atom Typing with Prototype and Iterators</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;The previous article in this series discussed the use of &lt;a href="http://depth-first.com/articles/2008/08/26/javascript-for-cheminformatics-using-the-prototype-framework"&gt;Prototype to build a simple cheminformatics model&lt;/a&gt;. But there's much more to &lt;a href="http://www.prototypejs.org/"&gt;Prototype&lt;/a&gt; than an improved class-like syntax. This article discusses one specific way that Prototype enhances JavaScript collections through iterators.&lt;/p&gt;

&lt;h4&gt;The Problem&lt;/h4&gt;

&lt;p&gt;Let's say we have an instance of &lt;tt&gt;Molecule&lt;/tt&gt;, as defined in &lt;a href="http://depth-first.com/articles/2008/08/26/javascript-for-cheminformatics-using-the-prototype-framework"&gt;the previous article&lt;/a&gt;, and we'd like to group the carbon atoms separately from the heteroatoms. In many languages, including Java, we'd have to write a for-loop complete with logic for comparing atoms and then placing them into bins. Prototype makes possible a more modular approach with iterators.&lt;/p&gt;

&lt;h4&gt;Functional Programming and Iterators&lt;/h4&gt;

&lt;p&gt;JavaScript is a multi-paradigm programming language that offers tools for both object-oriented and &lt;a href="http://www.joelonsoftware.com/items/2006/08/01.html"&gt;functional programming&lt;/a&gt; approaches. In practical terms, this simply means that even functions in JavaScript behave as objects: they can be created dynamically and passed as parameters. Prototype takes advantage of this to extend collections instances such as &lt;tt&gt;Arrays&lt;/tt&gt; with built-in iterators that are analogous to iterators found in languages such as Ruby.&lt;/p&gt;

&lt;h4&gt;A Simple Test&lt;/h4&gt;

&lt;p&gt;The JavaScript below builds a pyridine molecule:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_javascript "&gt;function createPyridine(){
  var pyridine = new Molecule();
  var c1 = pyridine.addAtom(&amp;quot;C&amp;quot;);
  var c2 = pyridine.addAtom(&amp;quot;C&amp;quot;);
  var c3 = pyridine.addAtom(&amp;quot;C&amp;quot;);
  var c4 = pyridine.addAtom(&amp;quot;C&amp;quot;);
  var c5 = pyridine.addAtom(&amp;quot;C&amp;quot;);
  var n6 = pyridine.addAtom(&amp;quot;N&amp;quot;);

  pyridine.connect(c1, c2, 1);
  pyridine.connect(c2, c3, 2);
  pyridine.connect(c3, c4, 1);
  pyridine.connect(c4, c5, 2);
  pyridine.connect(c5, n6, 1);
  pyridine.connect(n6, c1, 2);

  return pyridine;
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Saving this code in a file called &lt;strong&gt;molecules.js&lt;/strong&gt;, we can use Firefox with Firebug to test it with the following 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;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;molecules.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;

With the Firebug console, we create a &lt;tt&gt;Molecule&lt;/tt&gt; of pyridine:

&lt;div class="console"&gt;
&lt;pre&gt;
&amp;gt;&amp;gt;&amp;gt; m = createPyridine();
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To separate carbons from heteroatoms, we use the Prototype &lt;tt&gt;partition&lt;/tt&gt; iterator:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
&gt;&gt;&gt; m=createPyridine();
Object atoms=[6] bonds=[6]
&gt;&gt;&gt; m.atoms.partition(function(atom){return atom.label == "C"});
[[Object label=C neighbors=[2] bonds=[2], Object label=C neighbors=[2] bonds=[2], Object label=C neighbors=[2] bonds=[2], 2 more...], [Object label=N neighbors=[2] bonds=[2]]]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;tt&gt;partition&lt;/tt&gt; iterator accepts a function as a parameter and returns an array containing two sub-arrays: the first contains the elements for which the function evaluated to &lt;tt&gt;true&lt;/tt&gt; (carbons) and the second contains the elements for which the function evaluated to &lt;tt&gt;false&lt;/tt&gt; (heteroatoms).&lt;/p&gt;

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

&lt;p&gt;Although the example shown here is rather simple, it's possible to extend the general principle to more complex atom typing systems. By creating a single function that evaluated atom type, we could pass it as a parameter to any number of collection iterator functions.&lt;/p&gt;</description>
      <pubDate>Thu, 28 Aug 2008 19:08:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a0d87c70-ab16-43d9-a09b-21aca32ae696</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/08/28/javascript-for-cheminformatics-atom-typing-with-prototype-and-iterators</link>
      <category>Tools</category>
      <category>javascript</category>
      <category>prototype</category>
      <category>iterator</category>
      <category>functionalprogramming</category>
    </item>
  </channel>
</rss>
