<?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 http</title>
    <link>http://depth-first.com/articles/tag/http</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>The Best API May Be No API At All: PubChem and PDB</title>
      <description>&lt;p&gt;&lt;a href="http://flickr.com/photos/druclimb/325661568/"&gt;&lt;img src="http://depth-first.com/demo/20070813/invisible.jpg" align="right" border="0"&gt;&lt;/img&gt;&lt;/a&gt;Both &lt;a href="http://pubchem.ncbi.nlm.nih.gov/"&gt;PubChem&lt;/a&gt; and the &lt;a href="http://www.rcsb.org/pdb/home/home.do"&gt;Protein Data Bank&lt;/a&gt; (PDB) maintain vast collections of molecular data. Individual users are free to view and search these collections via standard Web browsers. But what are the options if you're developing software to interact with these databases?&lt;/p&gt;

&lt;p&gt;Various application programming interfaces (APIs) are available for accessing PubChem and PDB records. For example, PubChem recently introduced its &lt;a href="http://depth-first.com/articles/tag/pug"&gt;Power User Gateway&lt;/a&gt; (PUG), an XML-based query language. But writing APIs is extremely difficult; reconciling the need for simplicity with the need for rich functionality is a tough balancing act. Where do you draw the line?&lt;/p&gt;

&lt;p&gt;Recently, &lt;a href="http://boscoh.com/"&gt;Bosco&lt;/a&gt; described a &lt;a href="http://boscoh.com/protein/fetching-pdb-files-remotely-in-pure-python-code"&gt;remarkably short method&lt;/a&gt; to retrieve PDB records using nothing more than standard Python. Given the similarities between Python and Ruby, it seemed reasonable that his method could be adapted to Ruby.&lt;/p&gt;

&lt;p&gt;The following Ruby library accepts a PDB identifier and returns the corresponding PDB record:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;net/http&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;PDB&lt;/span&gt;
  &lt;span class="comment"&gt;# Returns a PDB record for the given id&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.get_record&lt;/span&gt; &lt;span class="ident"&gt;id&lt;/span&gt;
    &lt;span class="constant"&gt;Net&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HTTP&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;get_response&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;www.rcsb.org&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;/pdb/files/&lt;span class="expr"&gt;#{id}&lt;/span&gt;.pdb&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;body&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

Notice how the business end of this library is nothing more than a single line of Ruby code.

The library can be tested by saving it in a file called &lt;strong&gt;pdb.rb&lt;/strong&gt; and invoking interactive Ruby (irb):

&lt;div class="console"&gt;
&lt;pre&gt;
$ irb
irb(main):001:0&gt; require 'pdb'
=&gt; true
irb(main):002:0&gt; puts PDB::get_record('1hpn')
HEADER    GLYCOSAMINOGLYCAN                       17-JAN-95   1HPN
TITLE     N.M.R. AND MOLECULAR-MODELLING STUDIES OF THE SOLUTION
TITLE    2 CONFORMATION OF HEPARIN

[truncated]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Several months ago, a D-F article described a related, but somewhat lengthier approach to &lt;a href="http://depth-first.com/articles/2006/08/30/hacking-pubchem-with-ruby"&gt;retrieving PubChem molfiles&lt;/a&gt;. Using the same approach we used for PDB, we can create the world's shortest PubChem library:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;net/http&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;PubChem&lt;/span&gt;
  &lt;span class="comment"&gt;# Returns a molfile for the given PubChem CID&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.get_molfile&lt;/span&gt; &lt;span class="ident"&gt;cid&lt;/span&gt;
    &lt;span class="constant"&gt;Net&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;HTTP&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;get_response&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;pubchem.ncbi.nlm.nih.gov&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;/summary/summary.cgi?cid=&lt;span class="expr"&gt;#{cid}&lt;/span&gt;&amp;amp;disopt=DisplaySDF&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;body&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

This library can be tested by saving it in a file called &lt;strong&gt;pubchem.rb&lt;/strong&gt; followed by running irb:

&lt;div class="console"&gt;
&lt;pre&gt;
$ irb
irb(main):001:0&gt; require 'pubchem'
=&gt; true
irb(main):002:0&gt; puts PubChem::get_molfile('969472') #eszopiclone (Lunesta)
969472
  -OEChem-08130700422D

 44 47  0     1  0  0  0  0  0999 V2000
    9.2619   -2.2732    0.0000 Cl  0  0  0  0  0  0  0  0  0  0  0  0

[truncated]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Both of these Ruby libraries leverage one the most versatile and robust protocols ever developed: plain old http. The last few years have witnessed a renaissance in using bare http as platform for building simplified yet powerful Web APIs with less software. Referred to as &lt;a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm"&gt;REST&lt;/a&gt;, the approach has gained traction partly in response to the wasteful complexities introduced by various XML-based approaches. Although &lt;a href="http://depth-first.com/articles/2007/05/30/restful-cheminformatics"&gt;slow to catch on in cheminformatics&lt;/a&gt;, REST has enormous potential in unifying &lt;a href="http://depth-first.com/articles/2007/01/24/thirty-two-free-chemistry-databases"&gt;a diverse array&lt;/a&gt; of isolated database systems.&lt;/p&gt;

&lt;p&gt;One limitation of the approach described here is that the PubChem (or PDB) folks may get upset if you use it a lot. For example, if you examine the &lt;a href="http://pubchem.ncbi.nlm.nih.gov/robots.txt"&gt;PubChem robots.txt file&lt;/a&gt;, you'll notice that access to the &lt;tt&gt;summary.cgi&lt;/tt&gt; resource, which our library makes use of, is prohibited to robots:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_robots "&gt;...

User-agent: *

...
Disallow: /summary/summary.cgi
...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What makes a "robot" and does your software qualify for exclusion? The answer is not enirely clear-cut, especially in the era of browser-side scripting.&lt;/p&gt;

&lt;p&gt;Regardless, it looks like PubChem's policy was put in place in 2004, long before PubChem had experience with usage patterns for its service. It may be that this restriction could be relaxed without adversely affecting PubChem's ability to operate efficiently. It may even be possible to offer a low-level http retrieval method alongside PubChem's PUG interface on a machine dedicated to automated queries (i.e., &lt;a href="http://eutils.ncbi.nlm.nih.gov/entrez/query/static/eutils_help.html"&gt;Entrez eUtils&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;As developers, our mission is to deliver functionality, not to write software. We should extract every possible ounce of value from established protocols and APIs before writing a single line of additional code. REST, and the creative use of good old http, are powerful tools to do so.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Image Credit &lt;a href="http://flickr.com/photos/druclimb/"&gt;Dru!&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;</description>
      <pubDate>Mon, 13 Aug 2007 07:55:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0632ab5e-4c6a-4bb5-b898-5606e7743230</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2007/08/13/the-best-api-may-be-no-api-at-all-pubchem-and-pdb</link>
      <category>Tools</category>
      <category>pubchem</category>
      <category>pdb</category>
      <category>pug</category>
      <category>xml</category>
      <category>rest</category>
      <category>http</category>
      <category>ruby</category>
    </item>
  </channel>
</rss>
