<?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: Tag javascript</title>
    <link>http://depth-first.com/articles/tag/javascript</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>Building ChemWriter: What to Do When Requesting Applet Keyboard Focus Leads to Disappearing Popup Windows</title>
      <description>&lt;p&gt;&lt;a href="http://metamolecular.com/chemwriter"&gt;&lt;img src="http://metamolecular.com/images/global/chemwriter_small.png" align="right"&gt;&lt;/img&gt;&lt;/a&gt;Recently a customer reported a problem in which mousing over an instance of the &lt;a href="http://metamolecular.com/chemwriter"&gt;ChemWriter&lt;/a&gt; editor applet caused browser popup windows to disappear behind the parent window. Although many view browser popup windows as bad UI design, there are situations in which no alternative exists. This article describes the window focus problem in detail and outlines one solution.&lt;/p&gt;

&lt;h4&gt;Background&lt;/h4&gt;

&lt;p&gt;One of the ways ChemWriter makes chemists more efficient at drawing chemical structures is through the use of &lt;a href="http://metamolecular.com/articles/chemwriter-keyboard"&gt;keyboard shortcuts&lt;/a&gt;. Rather than having to mouse back and forth between a tool palette and drawing canvas to put in atom labels, simply hover the mouse over the atom to label, and press a key on the keyboard. In addition to atom labels, there are keyboard shortcuts for chains (keys 1-9 while hovered over atom), for benzene rings ("a" key), and to edit bond order (1-3 while hovered over bond). &lt;a href="http://depth-first.com/articles/2008/06/18/screencast-drawing-structures-quickly-with-chemwriter"&gt;A short movie&lt;/a&gt; explains the feature in more detail.&lt;/p&gt;

&lt;p&gt;Although quite helpful to users, this feature requires some behind-the-scenes magic. Keyboard focus is one of those topics at the boundary between applet and browser for which very little documentation exists and, not surprisingly, one sees the most variation in platform and browser behavior.&lt;/p&gt;

&lt;p&gt;The approach ChemWriter takes is to wait for a signal that the mouse cursor has entered the canvas area. When this happens, keyboard focus is requested through &lt;tt&gt;Component.requestFocus()&lt;/tt&gt;.&lt;/p&gt;

&lt;h4&gt;Scope of the Problem&lt;/h4&gt;

&lt;p&gt;It turns out that on Windows, &lt;tt&gt;Component.requestFocus()&lt;/tt&gt; also causes the hosting window to be pulled to the top of the windows stack, explaining the behavior described above. On Linux and OS X, this doesn't happen, which is the behavior you'd expect.&lt;/p&gt;

&lt;p&gt;All Windows browsers, except the much maligned Internet Explorer 6, show this behavior. This includes Internet Explorer 7, Firefox 3, and Google Chrome. Internet Explorer 8 beta 2 also shows the behavior, but only once per popup window.&lt;/p&gt;

&lt;p&gt;Minimizing, then maximizing the popup window eliminated the problem some of the time. But a new popup window would still show the behavior.&lt;/p&gt;

&lt;h4&gt;The Solution&lt;/h4&gt;

&lt;p&gt;The root of the problem is that on Windows, keyboard focus is granted to an object regardless of whether the object's hosting browser window is focused. What's needed, therefore, is a way for the applet to implement a window focus check.&lt;/p&gt;

&lt;p&gt;Apparently, nothing in the Applet API itself can solve this problem. The &lt;tt&gt;Applet&lt;/tt&gt;, &lt;tt&gt;JApplet&lt;/tt&gt;, and &lt;tt&gt;AppletContext&lt;/tt&gt; classes only deal with much higher-level considerations. &lt;/p&gt;

&lt;p&gt;However, it's possible to take advantage of support for &lt;a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide:LiveConnect_Overview"&gt;LiveConnect&lt;/a&gt; technology on Windows, which is actually quite good. Using LiveConnect in combination with JavaScript's &lt;tt&gt;Document.hasFocus()&lt;/tt&gt; method offers the makings of a solution. For example, the following code can be used as a starting point within a Java applet to determine if the containing browser window is focused:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_java "&gt;netscape.javascript.JSObject js = netscape.javascript.JSObject.getWindow(this);
Object result = js.eval(&amp;quot;document.hasFocus();&amp;quot;);

if (&amp;quot;true&amp;quot;.equals(result.toString()))
{
  requestFocusMethod();
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Of course, &lt;tt&gt;requestFocusMethod()&lt;/tt&gt; needs to be defined, we need to check for &lt;tt&gt;null&lt;/tt&gt;, and we need to handle exceptions that could arise from a missing &lt;tt&gt;netscape.javascript&lt;/tt&gt; package. You'll also need to ensure that non-windows browsers such as Safari/OS X never even try to execute the LiveConnect code due to a very buggy implementation.&lt;/p&gt;

&lt;h4&gt;Issues&lt;/h4&gt;

&lt;p&gt;I've seen mixed signals about the status of LiveConnect in the next major release of the Java plugin. Regardless of the specific way it's implemented, it seems safe to say that Java-Javascript communication is far too valuable to abandon. The only question is what form support for this feature will take going forward.&lt;/p&gt;

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

&lt;p&gt;ChemWriter's ability to accept keyboard input is a helpful user interface feature, but one that resulted in window focus issues on Windows. Using LiveConnect in combination with some simple JavaScript in the focus-management code offered an effective solution.&lt;/p&gt;</description>
      <pubDate>Thu, 06 Nov 2008 19:35:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:2c99c2e5-7eeb-4a8f-b578-f892877770d3</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/11/06/building-chemwriter-what-to-do-when-requesting-applet-keyboard-focus-leads-to-disappearing-popup-windows</link>
      <category>Tools</category>
      <category>chemwriter</category>
      <category>buildingchemwriter</category>
      <category>windowfocus</category>
      <category>windows</category>
      <category>liveconnect</category>
      <category>applet</category>
      <category>javascript</category>
      <category>java</category>
    </item>
    <item>
      <title>Crazy Idea #6,349: JavaScript in PDF</title>
      <description>&lt;p&gt;&lt;a href="http://www.manning.com/lowagie/"&gt;&lt;img src="http://depth-first.com/demo/20080917/book_cover.jpg" align="right"&gt;&lt;/img&gt;&lt;/a&gt;In reading through Bruno Lowagie's excellent book &lt;a href="http://www.manning.com/lowagie/"&gt;iText in Action&lt;/a&gt;, I noticed several references to JavaScript. I knew it was possible to do a lot of interesting things with PDF to promote interactivity. But JavaScript?&lt;/p&gt;

&lt;p&gt;It turns out Adobe has even published about 1,000 pages worth of documentation on the subject in the form of the &lt;a href="http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJSGuide.pdf"&gt;Acrobat JavaScript Scripting Guide&lt;/a&gt; and the &lt;a href="http://www.adobe.com/devnet/acrobat/pdfs/AcroJS.pdf"&gt;Acrobat JavaScript Scripting Reference&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.lowagie.com/iText/"&gt;iText&lt;/a&gt;, the premier Java library for creating and manipulating PDF documents, supports JavaScript out of the box. The extent of this support is not clear to me yet, but the API does exist.&lt;/p&gt;

&lt;p&gt;JavaScript has a lot of &lt;a href="http://depth-first.com/articles/tag/javascript"&gt;untapped potential&lt;/a&gt; for enhancing interactivity in cheminformatics. Similarly, PDF has a lot of untapped potential, which will be the topic of future articles.&lt;/p&gt;

&lt;p&gt;What kinds of things could you do in cheminformatics by combining PDF and JavaScript?&lt;/p&gt;</description>
      <pubDate>Thu, 18 Sep 2008 18:04:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e4abb37e-674c-4e6d-8882-f7dd789cb0de</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/09/18/crazy-idea-6-349-javascript-in-pdf</link>
      <category>Tools</category>
      <category>itext</category>
      <category>javascript</category>
      <category>pdf</category>
    </item>
    <item>
      <title>Chrome and a V8: JavaScript Takes a Giant Leap Forward?</title>
      <description>&lt;p&gt;&lt;a href="http://www.google.com/googlebooks/chrome/index.html"&gt;&lt;img src="http://depth-first.com/demo/20080904/jsvm.png" align="right"&gt;&lt;/img&gt;&lt;/a&gt;Back when I started writing Java software in 1997, the Java Virtual Machine was &lt;em&gt;slow&lt;/em&gt;. It was so slow that for years, many developers abandoned all hope of using the language for "serious" work once it became clear how much slower it was than C and C++. Eleven years of Moore's Law compounding, and countless JVM optimizations later, Java is so fast today that relative speed is rarely even considered when developing client and server applications.&lt;/p&gt;

&lt;p&gt;Today, JavaScript occupies a similar position to that held by Java in 1997: a ubiquitous language with a basically good design that has significant performance issues.&lt;/p&gt;

&lt;h4&gt;The Next Big Thing? JavaScript Virtual Machines&lt;/h4&gt;

&lt;p&gt;This situation may be about to change - radically. Several groups are going to great effort to improve the performance of JavaScript by creating JavaScript Virtual Machines. The most recent entry into this increasingly crowded field is &lt;a href="http://depth-first.com/articles/2008/09/02/google-chrome-rethinking-the-browser-as-an-operating-system"&gt;Google Chrome&lt;/a&gt;. Among Chrome's many innovations is the introduction of one of the first &lt;a href="http://code.google.com/apis/v8/intro.html"&gt;JavaScript Virtual Machines&lt;/a&gt; (V8) into a production browser. A virtual machine works fundamentally differently from traditional JavaScript interpreters, with the potential for greatly reduced memory requirements and speed increases.&lt;/p&gt;

&lt;h4&gt;Put a V8 in Your Browser&lt;/h4&gt;

&lt;p&gt;How fast is Chrome's V8 engine? We can get an idea by running some benchmarks.&lt;/p&gt;

&lt;p&gt;The chart below shows the results of running &lt;a href="http://code.google.com/apis/v8/run.html"&gt;Google's V8 Benchmark Suite&lt;/a&gt; (bigger bars mean faster execution):&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20080904/chart.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;As you can see, Chrome leaves both Firefox 3 and IE 7 in the dust, at least according to this benchmark. Another popular benchmark is &lt;a href="http://www2.webkit.org/perf/sunspider-0.9/sunspider.html"&gt;SunSpider&lt;/a&gt;, where the results are qualitiatively similar; Chrome's execution time surpasses that of IE 7 by over two orders of magnitude.&lt;/p&gt;

&lt;p&gt;My system consisted of an Ubuntu Linux machine running a clean install of Windows XP on Sun's excellent virtualization product, &lt;a href="http://www.virtualbox.org/"&gt;VirtualBox&lt;/a&gt;. Your mileage may vary. Note: it's important to disable Internet Explorer's warning prompt that reads "This page contains a script which is taking an unusually long time to finish. To end this script now, click Cancel." (the presence of which is telling in itself).This can be done by following the &lt;a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;175500"&gt;instructions here&lt;/a&gt;.&lt;/p&gt;

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

&lt;p&gt;JavaScript is in the middle of a textbook &lt;a href="http://en.wikipedia.org/wiki/Disruptive_technology"&gt;Marketplace Disruption&lt;/a&gt;. Just four years ago, few even thought about the language. Today it's the centerpiece of Web interactivity. Perhaps the biggest issue remaining, performance, is now the focus of intense research that is beginning to bear fruit. Many of the key technologies now starting to appear, such as V8, are modular and open source; other browser vendors can adapt them for use in their own products. It's an offer few can afford to refuse.&lt;/p&gt;

&lt;p&gt;Sooner than many might have thought possible, JavaScript may stop being viewed as the &lt;em&gt;slow&lt;/em&gt; language. Then what?&lt;/p&gt;</description>
      <pubDate>Fri, 05 Sep 2008 01:42:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5c35762b-cc88-4097-a2b1-49a3646df8ca</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/09/05/chrome-and-a-v8-javascript-takes-a-giant-leap-forward</link>
      <category>Tools</category>
      <category>javascript</category>
      <category>chrome</category>
      <category>virtualmachine</category>
      <category>google</category>
      <category>disruptiveinnovation</category>
      <category>performance</category>
    </item>
    <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>
    <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>Building WebSpex: Putting Custom Data Types In Their Place</title>
      <description>&lt;p&gt;&lt;a href="http://flickr.com/photos/mdezemery/457694256/"&gt;&lt;img src="http://depth-first.com/demo/20080724/spectrum.jpg" align="right"&gt;&lt;/img&gt;&lt;/a&gt;The previous article in this series introduced &lt;a href="http://depth-first.com/articles/2008/07/17/javascript-for-cheminformatics-an-introduction-to-webspex-a-spectroscopy-tool-for-the-internet"&gt;WebSpex&lt;/a&gt;, a spectroscopic data visualization tool being designed especially for use in a Web browser. Previously, the platform on which the user interface would be built was discussed. This article will discuss the question of where to put the spectroscopy data that WebSpex will display.&lt;/p&gt;

&lt;h4&gt;Tag Soup&lt;/h4&gt;

&lt;p&gt;We've decided to target WebSpex for use on the Web, which means that spectroscopy data would need to be referenced or embedded in a Web page. How should we do this? The answer, it turns out, is far from obvious.&lt;/p&gt;

&lt;p&gt;If we knew that WebSpex were going to be created as a Java or Flash applet, which is not the current plan, we might be tempted to pass a reference to the data (or the data itself) as a parameter in the &lt;a href="http://depth-first.com/articles/2008/03/12/demystifying-java-applets-part-3-failing-gracefully-when-your-users-dont-have-java"&gt;&lt;tt&gt;&amp;lt;object&amp;gt;&lt;/tt&gt; tag&lt;/a&gt;. For an applet, this might look something like:&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;object&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;application/x-java-applet;version=1.4.2&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;520&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;350&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;com/metamolecular/webspex/applet/FullApplet.class&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;archive&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://metamolecular.com/applets/webspex.jar&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;jcamp&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://base-url/spectrum.jdx&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;object&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;In the example above, the parameter &lt;tt&gt;jcamp&lt;/tt&gt; would encode the path to a JCAMP-DX file for WebSpex to load.&lt;/p&gt;

&lt;p&gt;Alternatively, if we were going to develop WebSpex as a Flash applet, we might use an object tag like this:&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;object&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;application/x-shockwave-flash&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;520&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;350&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;movie&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;webspex.swf&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;FlashVars&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;filename=http://spectrum.jdx&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;object&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;In this example, we associate the parameter &lt;tt&gt;filename&lt;/tt&gt; with the value "spectrum.dx" using &lt;a href="http://www.permadi.com/tutorial/flashVars/index.html"&gt;FlashVars&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This works well enough, but what if we need to load a custom data type in a Web page without a plugin?&lt;/p&gt;

&lt;h4&gt;Some Options&lt;/h4&gt;

&lt;p&gt;There are a few options for including custom data in an HTML document:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Invent our Own Tag&lt;/strong&gt; Browsers are designed to ignore content they don't understand. We could just hack our own tag, let's call it &lt;tt&gt;&amp;lt;spectrum&amp;gt;&lt;/tt&gt;. But for a variety of reasons, this is a bad idea. Most importantly, we'd be breaking with conventions used worldwide, which is never a good idea without a very good reason. For another, any developer tools we'd use would probably complain about a mis-formed HTML document. Still another reason might be that browsers could parse our invented tag in unpredictable ways. We may also run into problems with search engines not indexing our content properly.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use XHML&lt;/strong&gt; We could try inventing a tag the right way: with &lt;a href="http://en.wikipedia.org/wiki/XHTML"&gt;XHTML&lt;/a&gt;. This might be a worthwhile option if our data type (&lt;a href="http://dx.doi.org/10.1351/pac199163121781"&gt;JCAMP-DX&lt;/a&gt;) were XML-based, but it's not. At best we'd expend a lot of effort learning about namespaces, schema, and HTTP response headers only to end up with an amorphous flat &lt;tt&gt;&amp;lt;spectrum&amp;gt;&lt;/tt&gt; tag containing freeform text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use JSON&lt;/strong&gt; We could encode our JCAMP-DX files as &lt;a href="http://www.json.org/js.html"&gt;JSON&lt;/a&gt;. JSON is a markup language like XML, but with the difference that it can be &lt;tt&gt;eval&lt;/tt&gt;ed directly by the JavaScript interpreter. This has the advantage that either a filename, or the actual data could be encoded. We could, in fact, create the entire object model for our spectrum, ready to be displayed, if we had software that could make the conversion from JCAMP-DX to JSON. This approach has the disadvantage that it could require significant amounts JavaScript code to be mixed in with our HTML, &lt;a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript"&gt;a less than ideal solution&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use the Object Tag&lt;/strong&gt; Given that none of the three options above are especially appealing, we might ask ourselves whether we've really tried everything possible to use plain old HTML to encode our data. More specifically, what if we were to use the object tag itself, without actually having a plugin?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;Encoding Custom Data Types With The Object Tag&lt;/h4&gt;

&lt;p&gt;The &lt;a href="http://www.w3.org/TR/REC-html40/struct/objects.html#h-13.3"&gt;HTML 4 specification&lt;/a&gt; has this to say about the object tag:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;Most user agents have built-in mechanisms for rendering common data types such as text, GIF images, colors, fonts, and a handful of graphic elements. To render data types they don't support natively, user agents generally run external applications. The OBJECT element allows authors to control whether data should be rendered externally or by some program, specified by the author, that renders the data within the user agent.&lt;/p&gt;
    
    &lt;p&gt;In the most general case, an author may need to specify three types of information:&lt;/p&gt;
    
    &lt;ul&gt;
    &lt;li&gt;The implementation of the included object. For instance, if the included object is a clock applet, the author must indicate the location of the applet's executable code.&lt;/li&gt;
    &lt;li&gt;The data to be rendered. For instance, if the included object is a program that renders font data, the author must indicate the location of that data.&lt;/li&gt;
    &lt;li&gt;Additional values required by the object at run-time. For example, some applets may require initial values for parameters.&lt;/li&gt;
    &lt;/ul&gt;
    
    &lt;p&gt;The OBJECT element allows authors to specify all three types of data, but authors may not have to specify all three at once. For example, some objects may not require data (e.g., a self-contained applet that performs a small animation). Others may not require run-time initialization. Still others may not require additional implementation information, i.e., the user agent itself may already know how to render that type of data (e.g., GIF images).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, we could place a reference to a spectrum object in an HTML page with code like this:&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;object&lt;/span&gt; &lt;span class="attribute"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;520&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;350&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;data&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://base-url/spectrum.jdx&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;object&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;After loading the document, we could have WebSpex walk the DOM looking for object tags that could be replaced with an instance of WebSpex. That instance could actually be placed inside the original object tag like this:&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;object&lt;/span&gt; &lt;span class="attribute"&gt;width&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;520&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;height&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;350&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;param&lt;/span&gt; &lt;span class="attribute"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;data&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="attribute"&gt;value&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://base-url/spectrum.jdx&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="tag"&gt;div&lt;/span&gt; &lt;span class="attribute"&gt;class&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;webspex&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
    &lt;span class="comment"&gt;&amp;lt;!-- WebSpex visual presentation --&amp;gt;&lt;/span&gt;
  &lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="tag"&gt;div&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;object&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;The HTML 4 documentation states that any content contained within object tags not recognized by the user agent will be rendered (fallback content). So dynamically inserting the div into the object tag as shown above would have the effect of giving the browser something to display in place of the object tag.&lt;/p&gt;

&lt;h4&gt;Advantages of Using This Approach&lt;/h4&gt;

&lt;p&gt;This approach has several advantages worth mentioning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It's fully compliant with the HTML 4 specification.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It provides a natural anchor point to attach both the custom data and the visual presentation of that data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's pure HTML, requiring minimal mixing in of JavaScript content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web spiders can be taught a single method to associate a spectrum with a URL, regardless of how the viewer is implemented.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It's technology-agnostic. This approach lets us implement WebSpex as a Java or Flash applet (or some other plugin technology) just as easily as a pure JavaScript UI. To change our viewer implementation, we just change a JavaScript file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It allows spectra to be inlined, or place directly into the HTML. Using a &lt;a href="http://en.wikipedia.org/wiki/Data:_URI_scheme"&gt;Data URI&lt;/a&gt;, we could replace "http://base-url/spectrum.jdx" with something like "data:chemical/x-jcamp-dx;base64,iVBORw0KGgoAA...". This would be important in those situations in which a public URL to a JCAMP file was not feasible and/or desirable. It could also accelerate the rendering of multiple spectra in the same page by eliminating the need to create a separate HTTP request for each file.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The method carries an imporant limitation: if a user has disabled JavaScript, they may see nothing to indicate a problem. We could address this issue by always placing fallback content in the object tag that would then be overwritten by the JavaScript code.&lt;/p&gt;

&lt;h4&gt;Implementation Detail&lt;/h4&gt;

&lt;p&gt;This approach relies on &lt;a href="http://en.wikipedia.org/wiki/Unobtrusive_JavaScript"&gt;Onobtrusive JavaScript&lt;/a&gt; techniques to keep JavaScript as separate from HTML as possible. One way to implement such a scheme would be to include a single reference to the relevant JavaScript somewhere in the document, probably withing the &amp;lt;head&amp;gt; tag or right after the opening &amp;lt;body&amp;gt; tag:&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;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;webspex.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;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The file webspex.js would then execute code to place a function into the document's &lt;tt&gt;onLoad&lt;/tt&gt; queue that would scan for object tags containing JCAMP-DX content and insert the needed viewer.&lt;/p&gt;

&lt;h4&gt;Previous Uses&lt;/h4&gt;

&lt;p&gt;I'm unaware of any previous applications of this technique, although is seems like something that may have been used before.&lt;/p&gt;

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

&lt;p&gt;Encoding and displaying custom data types in HTML is possible by using the HTML 4 object tag coupled with client-side JavaScript to rewrite the DOM. It offers the potential to create HTML documents that are both human- and machine-readable. Although the approach described here was developed for the special case of spectroscopy data, it could in principle be used for any data type requiring a visual presentation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Credit: &lt;a href="http://flickr.com/photos/mdezemery/"&gt;mdezemery&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 24 Jul 2008 16:40:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a859a6ea-6443-48ad-a35d-3f242546b252</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/07/24/building-webspex-putting-custom-data-types-in-their-place</link>
      <category>Tools</category>
      <category>webspex</category>
      <category>jcamp</category>
      <category>dx</category>
      <category>objecttag</category>
      <category>html4</category>
      <category>javascript</category>
      <category>flash</category>
      <category>java</category>
      <category>plugin</category>
    </item>
    <item>
      <title>JavaScript for Cheminformatics: An Introduction to WebSpex, a Spectroscopy Tool for the Internet</title>
      <description>&lt;p&gt;&lt;a href="http://flickr.com/photos/ecstaticist/403053824/"&gt;&lt;img src="http://depth-first.com/demo/20080717/spectrum.jpg" align="right"&gt;&lt;/img&gt;&lt;/a&gt;The &lt;a href="http://depth-first.com/articles/2008/07/15/javascript-for-cheminformatics"&gt;previous article&lt;/a&gt; in this series discussed the untapped potential of JavaScript for building rich, chemistry-oriented Web applications. This article will describe the design of WebSpex, a spectrum viewer designed for the Web and written entirely in JavaScript.&lt;/p&gt;

&lt;h4&gt;Warning: Potential Vaporware Ahead&lt;/h4&gt;

&lt;p&gt;WebSpex can't yet be download or deployed, and it may never be finished. Articles like this one will document the tool's transition from concept to hopefully something more substantial. There may be false-starts and dead-ends along the way. But I'm hoping that there will also be feedback from readers like you. Feel free to chime in, regardless of your background.&lt;/p&gt;

&lt;p&gt;This process will be necessarily non-linear. But I believe that being able to incorporate feedback in real-time increases the chances of creating a better product, and seeing mistakes, false-starts, and dead-ends in context can be useful to everyone.&lt;/p&gt;

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

&lt;p&gt;Of all the forms of chemical data generated in labs around the world today, spectra are one of the most common. Although several tools can display and manipulate these data, few are capable of being used on the Web. If you agree with the hypothesis that the Web is fast becoming the only information exchange platform that matters, this presents a significant problem (or &lt;a href="http://depth-first.com/articles/tag/broken"&gt;opportunity&lt;/a&gt;, depending on your perspective).&lt;/p&gt;

&lt;p&gt;Recently, a series of articles on the Web-based spectroscopy tool &lt;a href="http://depth-first.com/articles/2008/02/01/hacking-jspecview-enhancing-the-user-interface"&gt;JSpecView&lt;/a&gt; appeared here. JSpecView is one of the only free tools currently available that enables spectra to be displayed and manipulated on the Web.&lt;/p&gt;

&lt;p&gt;JSpecView is written in Java and deployed as an applet. In very many situations, applets are the best technology for delivering interactive Web content of the kind required by spectroscopy.&lt;/p&gt;

&lt;p&gt;But no technology is perfect. One of the biggest limitations of applets is that they require the correct version of the Java plug-in to be installed on a users' machine (a limitation shared by all plug-in technologies, including &lt;a href="http://depth-first.com/articles/2008/06/10/adobe-flash-for-cheminformatics-fast-scalable-and-attractive-2d-depiction-of-chemical-structures-with-vector-graphics"&gt;Flash&lt;/a&gt;). In situations where users either can't or won't install the plugin, or in which adequate resources are not available to ensure &lt;a href="http://depth-first.com/articles/2008/03/12/demystifying-java-applets-part-3-failing-gracefully-when-your-users-dont-have-java"&gt;smooth applet deployment&lt;/a&gt;, Java applets may not be the best technology platform.&lt;/p&gt;

&lt;p&gt;What if there were a &lt;a href="http://depth-first.com/articles/2007/12/20/if-you-want-to-change-the-world-build-the-tool-first-part-2"&gt;spectroscopy tool&lt;/a&gt; that worked on any browser without any plugin dependencies?&lt;/p&gt;

&lt;h4&gt;Scenarios&lt;/h4&gt;

&lt;p&gt;For a better idea of what WebSpex will be about, consider some scenarios:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Viewing&lt;/strong&gt; Sally logs into her research group's spectroscopy archive website. She clicks on the link to her most recently-collected IR spectrum and is taken to a page displaying an image of her spectrum along with textual metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Zooming&lt;/strong&gt; Fred is viewing an IR spectrum published on a chemical supplier's Website. Wanting a better view of the fingerprint region, he uses the mouse to zoom the spectrum.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Peak Picking&lt;/strong&gt; Victor is using a publisher website to view the IR spectrum of a compound described in a recent paper. Wanting to match the carbonyl stretching frequency of the material he prepared, he uses the mouse to pick the peak.&lt;/p&gt;

&lt;h4&gt;Data&lt;/h4&gt;

&lt;p&gt;WebSpex will use as its input format &lt;a href="http://dx.doi.org/10.1351/pac199163121781"&gt;JCAMP-DX&lt;/a&gt;, the de facto standard for spectral data encoding. Although JCAMP-DX has been extended in many ways over the last several years, for now the goal of WebSpex will be to simply read and display &lt;a href="http://wwwchem.uwimona.edu.jm:1104/spectra/testdata/index.html"&gt;error-free examples&lt;/a&gt; of the original format specification.&lt;/p&gt;

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

&lt;p&gt;Good Web-based spectroscopy tools are a prerequisite for the open sharing of this important form of experimental data. WebSpex could fill this need by providing an installation-free tool that can be used in any browser without plugins. Currently consisting of nothing more than just some ideas, WebSpex successes and failures will be documented here in several installments.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Credit: &lt;a href="http://flickr.com/photos/ecstaticist/"&gt;estaticist&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 17 Jul 2008 12:18:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b64ddb59-d060-400c-a7a2-d20f0d61483b</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/07/17/javascript-for-cheminformatics-an-introduction-to-webspex-a-spectroscopy-tool-for-the-internet</link>
      <category>Tools</category>
      <category>javascript</category>
      <category>webspex</category>
      <category>jspecview</category>
      <category>spectroscopy</category>
    </item>
    <item>
      <title>JavaScript for Cheminformatics</title>
      <description>&lt;p&gt;&lt;a href="http://flickr.com/photos/jm999uk/244054409/"&gt;&lt;img src="http://depth-first.com/demo/20080715/rhino.jpg" align="right"&gt;&lt;/img&gt;&lt;/a&gt;Regular readers of this blog know that one of its recurring themes is the convergence of Web technologies and chemical information. Although several articles describe how &lt;a href="http://depth-first.com/articles/tag/ruby"&gt;Ruby&lt;/a&gt; and &lt;a href="http://depth-first.com/articles/tag/java"&gt;Java&lt;/a&gt; can be applied to cheminformatics, one language has never been featured: JavaScript. JavaScript is both the default language of the Web client, and a language of growing importance &lt;a href="http://depth-first.com/articles/2007/05/25/flex-rich-internet-applications-and-cheminformatics"&gt;elsewhere&lt;/a&gt;. This article, the first in a series, introduces JavaScript as a tool for creating rich, chemically-oriented Web applications.&lt;/p&gt;

&lt;h4&gt;Surely You Jest&lt;/h4&gt;

&lt;p&gt;If your last exposure to JavaScript was in the 1990s, a lot has changed for the better. Most importantly, performance has increased considerably, making possible JavaScript software of considerable complexity. A growing collection of well-crafted libraries such as &lt;a href="http://www.prototypejs.org/"&gt;Prototype&lt;/a&gt; and &lt;a href="http://script.aculo.us/"&gt;Scriptaculous&lt;/a&gt; now make it possible to focus precious developer effort at a higher level of abstraction. Although cross-browser compatibility continues to be an issue with browser-specific features, the JavaScript language itself is now remarkably stable and consistent across browsers and platforms.&lt;/p&gt;

&lt;p&gt;JavaScript will obviously never enjoy the performance of Java or C++, and it would be a mistake to assume otherwise. The key is to focus on what JavaScript can do that no other language or platform can. With this thought in mind, it's interesting to speculate on the role JavaScript could play in developing Web-based software for chemistry.&lt;/p&gt;

&lt;p&gt;For example, with essentially complete read-write access to the document object model (DOM) of sites on which they're active, tools based on JavaScript have great potential to enhance static content with content created either on a server or locally.&lt;/p&gt;

&lt;p&gt;As another example, consider the combination of JavaScript and an invisible Java applet. Java-JavaScript communication is possible in all browsers through &lt;a href="http://developer.mozilla.org/en/docs/LiveConnect"&gt;LiveConnect&lt;/a&gt;, which can be used to offload computation-intensive operations from JavaScript to an applet. In many ways, this approach to development on the client resembles the approach, discussed here many times, of using Java from Ruby through &lt;a href="http://depth-first.com/articles/tag/rjb"&gt;Ruby Java Bridge&lt;/a&gt; (RJB) or &lt;a href="http://depth-first.com/articles/tag/jruby"&gt;JRuby&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Ultimately, JavaScript is the only programming language that allows cross-browser, client-side software to be written independently of a plugin. This may not sound like a big deal, but for many developers and organizations, Flash, Java, and other plugin technologies are &lt;a href="http://www.javaworld.com/javaworld/jw-05-2008/jw-05-applets.html?page=2"&gt;less than ideal&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;What's Known?&lt;/h4&gt;

&lt;p&gt;There are currently only scattered examples of the use of JavaScript in chemistry, an indication that either the field is ready for this idea, or that it will never work.&lt;/p&gt;

&lt;p&gt;Perhaps the two best-known examples of JavaScript applied to chemical informatics are the &lt;a href="http://depth-first.com/articles/2006/11/09/look-ma-no-applets"&gt;WebME&lt;/a&gt; and &lt;a href="http://pubchem.ncbi.nlm.nih.gov/edit/"&gt;PubChem&lt;/a&gt; chemical structure editors. Although remarkable accomplishments, both packages rely heavily on server back-ends for processing and analysis of chemical structures. The JavaScript code serves mainly to asynchronously pass low-level mouse events to the server, which then asynchronously passes a raster image back to the client. To an extent, the back and forth degrades the responsiveness of these tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blueobelisk.sourceforge.net/wiki/Main_Page"&gt;The Blue Obelisk&lt;/a&gt; maintains a Wiki page discussing some of the uses of &lt;a href="http://blueobelisk.sourceforge.net/wiki/Using_Javascript_and_Greasemonkey_for_Chemistry"&gt;Greasemonkey scripts in chemistry&lt;/a&gt;. Greasemonkey is a wonderful tool for augmenting existing Websites, and these scripts do some remarkable things, but they don't fall into the category of cross-browser, installation-free, general-purpose tools.&lt;/p&gt;

&lt;p&gt;Recently, &lt;a href="http://www.stolaf.edu/people/hansonr/"&gt;Robert Hanson&lt;/a&gt; discussed &lt;a href="http://chemapps.stolaf.edu/jmol/presentations/bcce19/hansonr-bcce19-ajax.ppt"&gt;on-demand Javascript&lt;/a&gt; as a potential tool for building chemistry-oriented Web applications. Although the proposal contains many important points, it has to my knowledge not been followed up with the release of code.&lt;/p&gt;

&lt;p&gt;An early compilation of resources can be found &lt;a href="http://www.ch.ic.ac.uk/java/"&gt;here&lt;/a&gt;, although all of the listed sites have since either disappeared or converted to other content.&lt;/p&gt;

&lt;p&gt;Poking around the Web, it's possible to find many examples of one-off chemistry tools created in JavaScript. For example, &lt;a href="http://chemistry.about.com/od/periodictables/l/bljavaperiodictable.htm"&gt;this page&lt;/a&gt; contains a JavaScript-enabled periodic table. Useful as tools like this may be, they're not general-purpose solutions designed to create a platform for further work.&lt;/p&gt;

&lt;h4&gt;What Would Be Most Valuable?&lt;/h4&gt;

&lt;p&gt;There are many cheminformatics tools that could be built in JavaScript. But to be of the greatest value, a tool should be usable in a variety of contexts. And it should serve as a platform on which more complex software can be built. Even more importantly, the tool should fix something that really is &lt;a href="http://depth-first.com/articles/tag/broken"&gt;broken&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We might begin by asking: what makes chemical information difficult to work with in the greatest number of cases?&lt;/p&gt;

&lt;p&gt;Since the earliest days of chemistry and computers, &lt;a href="http://depth-first.com/articles/2006/09/03/peculiarities-of-chemical-information"&gt;it has been clear&lt;/a&gt; that one of the distinguishing characteristics of chemical information is the central role played by chemical structures and the difficulty in accurately representing and processing them on machines.&lt;/p&gt;

&lt;p&gt;So, one answer to the question of what would be the most useful JavaScript tool for cheminformatics could be: a low-level cheminformatics toolkit that understands chemical structures and their associated graph operations.&lt;/p&gt;

&lt;p&gt;Another possibility: the handling of NMR, IR, UV, and mass spectra. &lt;a href="http://depth-first.com/articles/tag/jspecview"&gt;JSpecView&lt;/a&gt; is a good first choice, but it may be possible to build a pure JavaScript tool for interactively viewing and manipulating spectra, if a low-level toolkit for processing &lt;a href="http://old.iupac.org/standing/cpep/wp_jcamp_dx.html"&gt;JCAMP-DX&lt;/a&gt; were available.&lt;/p&gt;

&lt;p&gt;Would the performance and usability of such toolkits be high enough to make them a serious choice for use in chemistry-driven Web applications? What would the availability of such a toolkit enable that is currently difficult or impossible?&lt;/p&gt;

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

&lt;p&gt;JavaScript is the default programming language of the Web client, offering a great deal to creators of chemically-oriented applications. One of the biggest barriers to using JavaScript for this purpose is the lack of key developer tools. Future articles will explore this idea.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Credit: &lt;a href="http://flickr.com/photos/jm999uk/"&gt;johnmuk&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 15 Jul 2008 08:32:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:212652a1-9a6e-4297-800e-bc62bc301bad</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/07/15/javascript-for-cheminformatics</link>
      <category>Tools</category>
      <category>java</category>
      <category>javascript</category>
      <category>toolkit</category>
      <category>platform</category>
      <category>applet</category>
      <category>jruby</category>
      <category>rjb</category>
      <category>client</category>
    </item>
    <item>
      <title>Anatomy of a Cheminformatics Web Application: Structure Cleanup in Java Molecular Editor</title>
      <description>&lt;p&gt;&lt;a href="http://rubyonrails.org"&gt;&lt;img src="http://depth-first.com/files/rails_logo.png" align="right" border="0"&gt;&lt;/img&gt;&lt;/a&gt;A very useful feature of many 2-D structure editors is a "clean" function that tidies up bond lengths and angles. &lt;a href="http://www.molinspiration.com/jme/"&gt;Java Molecular Editor&lt;/a&gt; (JME) is a lightweight 2-D editor that lacks this functionality. In this article, I'll describe a small Web application called "Cleanup" that adds a "clean" function to JME through Ajax and server-side programming, rather than directly extending JME itself. The technique described here differs somewhat from that described in a previous article on &lt;a href="http://depth-first.com/articles/2006/12/15/anatomy-of-a-cheminformatics-web-application-inchimatic"&gt;adding InChI support to JME with Ajax&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;Cleanup in Action&lt;/h4&gt;

&lt;p&gt;Let's say Bob needs to draw the structure of the H&lt;sub&gt;1&lt;/sub&gt; antagonist &lt;a href="http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=2725"&gt;chlorpheniramine&lt;/a&gt; with JME. He mistakenly creates irregular bond angles at several points, but continues drawing anyway. His finished molecule looks like that shown below:&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20061218/screenshot_1.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Rather than starting over to beautify his molecule, Bob, simply presses the &lt;strong&gt;Clean Molecule&lt;/strong&gt; button. This produces a structure with much more aesthetically-pleasing atom coordinates:&lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20061218/screenshot_2.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;If Bob needs to continue drawing at this point he can. In fact, he can press &lt;strong&gt;Clean Molecule&lt;/strong&gt; as many times as he wants to clean his structure at any time. Each time he presses the button, his structure is retained within the JME window.&lt;/p&gt;

&lt;h4&gt;Download and Prerequisites&lt;/h4&gt;

&lt;p&gt;Cleanup requires &lt;a href="http://rubyonrails.org"&gt;Ruby on Rails&lt;/a&gt; and &lt;a href="http://depth-first.com/articles/2006/10/30/agile-chemical-informatics-development-with-cdk-and-ruby-rcdk-0-3-0"&gt;Ruby CDK&lt;/a&gt;. Both of these libraries can be installed using the &lt;a href="http://rubygems.org/"&gt;RubyGems&lt;/a&gt; packaging system.&lt;/p&gt;

&lt;p&gt;A recent article described the small amount of system configuration required for &lt;a href="http://depth-first.com/articles/2006/09/25/cdk-the-ruby-way-rcdk-0-2-0"&gt;Ruby CDK on Linux&lt;/a&gt;. Another article showed how to install &lt;a href="http://depth-first.com/articles/2006/10/12/running-ruby-java-bridge-on-windows"&gt;Ruby CDK on Windows&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://rubyforge.org/frs/download.php/15687/jme-cleanup-0.0.1.tar.gz"&gt;complete Cleanup source package&lt;/a&gt; can be downloaded from RubyForge. For convenience, a copy of JME is included with the distribution. The author, Peter Ertl, has kindly given permission for the bundled JME applet to be used with Cleanup. For other uses, consult the &lt;a href="http://www.molinspiration.com/jme/"&gt;JME homepage&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;Running Cleanup&lt;/h4&gt;

&lt;p&gt;After inflating the Cleanup archive, the following commands will start the server:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ cd jme-cleanup-0.0.1
$ ruby script/server
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;AMD64 Linux users will need to prepend a &lt;tt&gt;LD_PRELOAD&lt;/tt&gt; assignment to the &lt;tt&gt;script/server&lt;/tt&gt; invocation. On my system, which uses Sun's JDK, this looks like:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ cd jme-cleanup-0.0.1
$ LD_PRELOAD=/usr/java/jdk1.5.0_09/jre/lib/amd64/libzip.so ruby script/server
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;After starting the Cleanup server, pointing your browser to &lt;a href="http://localhost:3000/editor/cleanup"&gt;http://localhost:3000/editor/cleanup&lt;/a&gt; will run the application.&lt;/p&gt;

&lt;h4&gt;How It Works: A Web Application in Two Parts&lt;/h4&gt;

&lt;p&gt;Cleanup is a Web application consisting of two main parts - one written for a Web server, and one written for a Browser client. These two components work together to achieve an effect that, to a user, is indistinguishable from extending the JME applet with Java.&lt;/p&gt;

&lt;p&gt;The first component consists of small Rails application that accepts a Molfile as input and produces a Molfile with re-assigned coordinates as output. A Rails Action, &lt;tt&gt;clean_structure&lt;/tt&gt; accepts a Molfile encoded as form data and produces a response Molfile with re-assigned coordinates.&lt;/p&gt;

&lt;p&gt;The second component of the Cleanup application is written in JavaScript and executed from within the Browser. Let's take a look:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&amp;lt;script language=&amp;quot;JavaScript&amp;quot;&amp;gt;

  /*
   * Returns the client-specific version of XMLHttpRequest
   */
  function createXHR()
  {
    var xhr;

    try
    {
      xhr = new ActiveXObject(&amp;quot;Msxml2.XMLHTTP&amp;quot;); // IE 5.0+
    }

    catch (e)
    {
      try
      {
        xhr = new ActiveXObject(&amp;quot;Microsoft.XMLHTTP&amp;quot;); // IE 5.0-
      }

      catch (E)
      {
        xhr = false;
      }
    }

    if (!xhr &amp;amp;&amp;amp; typeof XMLHttpRequest != 'undefined')
    {
      xhr = new XMLHttpRequest(); // every other browser
    }

    return xhr;  
  }

  function cleanStructure()
  {       
    var molfile = document.jme.molFile();
    var xhr = createXHR();

    xhr.open(&amp;quot;GET&amp;quot;, &amp;quot;clean_structure?molfile=&amp;quot; + encodeURIComponent(molfile));

    xhr.onreadystatechange=function()
    {
      if (xhr.readyState != 4) return;

      cleanMolfile = xhr.responseText;

      document.jme.readMolFile(cleanMolfile);
    }

    xhr.send(null);
  }
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="http://www.amazon.com/gp/product/0976694085?ie=UTF8&amp;amp;tag=depthfirst-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;creativeASIN=0976694085"&gt;&lt;img border="0" src="http://depth-first.com/files/pragmatic_ajax.jpg" align="right" &gt;&lt;/a&gt;&lt;img src="http://www.assoc-amazon.com/e/ir?t=depthfirst-20&amp;amp;l=as2&amp;amp;o=1&amp;amp;a=0976694085" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /&gt;As you can see, the client side of Cleanup consists of two JavaScript functions, &lt;tt&gt;createXHR&lt;/tt&gt; and &lt;tt&gt;cleanStructure&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;The purpose of &lt;tt&gt;createXHR&lt;/tt&gt; is to return a valid instance of the central Ajax JavaScript object, &lt;tt&gt;XMLHttpRequest&lt;/tt&gt;. This function is a standard idiom in Ajax programming, and many JavaScript toolkits eliminate the need to write it explicitly. The function is included here mainly for the purpose of illustration. Microsoft browsers define two different flavors of &lt;tt&gt;XMLHttpRequest&lt;/tt&gt;, and both differ from the flavor supported by every other browser. To take this browser-specific behavior into account, a series of try/catch blocks are used.&lt;/p&gt;

&lt;p&gt;The second function, &lt;tt&gt;cleanStructure&lt;/tt&gt; does all of the JME-specific work. After obtaining an instance of &lt;tt&gt;XMLHttpRequest&lt;/tt&gt;, a HTTP GET request is built from JME's molfile. Of course, the magic of this request is that it is &lt;em&gt;asynchronous&lt;/em&gt;; it will not block the browser while it is being processed. When the request is complete, the cleaned Molfile is read by JME.&lt;/p&gt;

&lt;p&gt;Through the coordinated action of both of Cleanup's components, the application gives the appearance that JME has cleaned its own structure.&lt;/p&gt;

&lt;h4&gt;So What?&lt;/h4&gt;

&lt;p&gt;Well-designed, rich functionality makes software interesting and useful. At the same time, users demand software that loads and responds quickly. Using the technique presented here, it's possible to satisfy both of these contradictory requirements. Delegating key tasks to a server obviates the need to transmit large Java libraries to clients. Instead, small Java libraries can be transmitted, and several small asynchronous requests will be processed along the way.&lt;/p&gt;

&lt;p&gt;Viewed from this perspective, the capabilities of a good Java applet take on a very different character from what many have become accustomed to. In particular, extensibility and a robust, text-based communication protocol become much more important than built-in features.&lt;/p&gt;

&lt;p&gt;For example, we could provide a much more consistent user experience if the &lt;strong&gt;Clean Molfile&lt;/strong&gt; button were contained inside the JME editor itself, instead of on the Web page. In a more general sense, we'd like JME to offer the option of defining custom buttons that can be assigned arbitrary actions. Because Java/JavaScript integration is very well-supported, these custom actions could actually be written in JavaScript.&lt;/p&gt;

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

&lt;p&gt;Java applets have been much maligned of late, partly due to the realization that in many situations they can be replaced with Ajax. However, well-designed, small, and extensible Java applets can play a key role in certain kinds of Ajax applications such as the one described here. Future articles in this series will explore some more of the many possibilities.&lt;/p&gt;</description>
      <pubDate>Mon, 18 Dec 2006 15:15:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9e354945-8529-4ef0-98ad-8cba4e1dfe0d</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2006/12/18/anatomy-of-a-cheminformatics-web-application-structure-cleanup-in-java-molecular-editor</link>
      <category>Web</category>
      <category>ajax</category>
      <category>java</category>
      <category>applet</category>
      <category>rails</category>
      <category>ruby</category>
      <category>2d</category>
      <category>cleanup</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Look Ma, No Applets!</title>
      <description>&lt;p&gt;&lt;center&gt;&lt;img src="http://depth-first.com/demo/20061109/webme.png"&gt;&lt;/img&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;The state of the art in structure editors for chemical Web services is Java applets. Although closed editors have long dominated this field, &lt;a href="http://depth-first.com/articles/2006/08/21/four-free-2-d-structure-editors-for-web-applications"&gt;Open Source editors&lt;/a&gt; are a possibly viable option. Java applets are great from a developer's perspective. But applets are avoided by some end users and IT support for their underlying need to install a Java plug-in of some kind and long startup times.&lt;/p&gt;

&lt;p&gt;Through David Bradly's &lt;a href="http://www.sciencebase.com/science-blog/chemists-escape-browser-lock-down.html"&gt;sciencebase&lt;/a&gt;, I came across a non-Java solution to the structure editor problem. The software is called &lt;a href="http://www.molinspiration.com/products/webme/"&gt;WebME&lt;/a&gt;. WebME looks and feels similar to &lt;a href="http://www.molinspiration.com/jme/"&gt;Java Molecular Editor&lt;/a&gt;. It loads quickly and provides a clean, inviting user interface. It should work in any modern browser. Most interesting of all, WebME works without a browser plugin of any kind.&lt;/p&gt;

&lt;p&gt;The magic behind WebME is AJAX, which has been summarized by Paul Graham as &lt;a href="http://paulgraham.com/web20.html"&gt;"Javascript now works"&lt;/a&gt;. By asynchronously interacting with a Web server as user interface events occur, WebME is able to cram a lot of functionality into a relatively small deployment package. The user interface is written in a mixture of HTML and JavaScript, thus eliminating the need for Java.&lt;/p&gt;

&lt;p&gt;Although WebME's use of AJAX is innovative, other non-Java solutions to the structure editor problem have also been implemented. For example, &lt;a href="http://pubchem.ncbi.nlm.nih.gov/"&gt;PubChem&lt;/a&gt; have developed an editor in JavaScript/HTML for use with their popular service.&lt;/p&gt;

&lt;p&gt;Despite its advantages, the AJAX approach does involve some trade-offs. For example, WebME is not nearly as responsive as, say, JME. I would imagine that unusually high network latency could further erode WebME's responsiveness. Furthermore, the subtle visual cues that make JME a productive tool, such as highlighting the node or edge the cursor is about to edit, are non-existent. It's unclear if this is a limitation of this particular version of WebME I used or the underlying technology.&lt;/p&gt;

&lt;p&gt;AJAX is a promising new technology that may well have a place in producing ergonomic user interfaces for chemistry Web services. On the other hand, it wasn't too long ago that JavaScript "didn't work", was loathed, or simply ignored altogether. It may well be that Java applets undergo a revival similar to that of JavaScript, perhaps triggered by built-in support for applets made possible through an &lt;a href="http://java.sun.com/developer/technicalArticles/Interviews/gosling_os1_qa.html"&gt;open source Java implementation&lt;/a&gt;. Regardless of how Java applet technology evolves, WebME's approach is worth serious consideration.&lt;/p&gt;</description>
      <pubDate>Thu, 09 Nov 2006 14:28:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:35a9ed1c-b1db-4538-ad05-6736318ec237</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2006/11/09/look-ma-no-applets</link>
      <category>Tools</category>
      <category>webme</category>
      <category>ajax</category>
      <category>javascript</category>
      <category>applets</category>
      <category>editor</category>
    </item>
  </channel>
</rss>
