<?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: Hacking PubChem: Convert CAS Numbers into PubChem CIDs with Ruby</title>
    <link>http://depth-first.com/articles/2007/09/13/hacking-pubchem-convert-cas-numbers-into-pubchem-cids-with-ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>Hacking PubChem: Convert CAS Numbers into PubChem CIDs with Ruby</title>
      <description>&lt;p&gt;&lt;a href="http://pubchem.ncbi.nlm.nih.gov/"&gt;&lt;img src="http://depth-first.com/files/pubchemlogo.gif" align="right" border="0"&gt;&lt;/img&gt;&lt;/a&gt;Although the PubChem system has been discussed in &lt;a href="http://depth-first.com/articles/tag/pubchem"&gt;numerous recent D-F articles&lt;/a&gt; and elsewhere, there's much more to the story that hasn't been told. One of the more intriguing things PubChem can do is &lt;a href="http://depth-first.com/articles/2007/05/21/simple-cas-number-lookup-with-pubchem"&gt;look up CAS Numbers for free&lt;/a&gt;. In this tutorial, we'll see how a simple Ruby script can be used to automate the conversion of CAS numbers into PubChem Compound IDs (CIDs).&lt;/p&gt;

&lt;h4&gt;The Library&lt;/h4&gt;

&lt;p&gt;Our library needs to accept a CAS number and return an array of PubChem CIDs in response. To do this, we'll make use of the &lt;a href="http://eutils.ncbi.nlm.nih.gov/entrez/query/static/eutils_help.html"&gt;Entrez eUtils system&lt;/a&gt;. Although Entrez is incredibly complex, the only two things that matter now are that the NIH requires automated scripts to access most of its databases through Entrez, and that Entrez can be used to perform PubChem keyword queries.&lt;/p&gt;

&lt;p&gt;The library is simplicity itself:&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;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;mechanize&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;PubChemTerms&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;get_cids&lt;/span&gt; &lt;span class="ident"&gt;term&lt;/span&gt;
    &lt;span class="ident"&gt;agent&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;WWW&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Mechanize&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
    &lt;span class="ident"&gt;page&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;agent&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;get&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&amp;amp;retmax=100&amp;amp;term=&lt;span class="expr"&gt;#{term}&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;

    &lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;page&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;parser&lt;/span&gt;&lt;span class="punct"&gt;/&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;).&lt;/span&gt;&lt;span class="ident"&gt;collect&lt;/span&gt; &lt;span class="punct"&gt;{|&lt;/span&gt;&lt;span class="ident"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;innerHTML&lt;/span&gt;&lt;span class="punct"&gt;}&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;

&lt;p&gt;The excellent Ruby library &lt;a href="http://mechanize.rubyforge.org/"&gt;Mechanize&lt;/a&gt; is used for submitting queries and processing the results. (This is the same library that was used to &lt;a href="http://depth-first.com/articles/2007/06/27/easily-convert-publisher-urls-and-dois-to-bibliographical-citations-synthesis-synlett-ruby-and-mechanize"&gt;extract full bibliographical information&lt;/a&gt; from nothing more than a DOI). The only remarkable thing about the library above is how unremarkable it is.&lt;/p&gt;

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

We can test the library by saving it in a file called &lt;strong&gt;entrez.rb&lt;/strong&gt; and starting an interactive Ruby (irb) session. Opening up my copy of the Merck index to a random page and selecting an entry gives a CAS number to try (64318-79-2 - gemeprost). Plugging this CAS number into our irb session gives:

&lt;div class="console"&gt;
&lt;pre&gt;
$ irb
irb(main):001:0&gt; require 'entrez'
=&gt; true
irb(main):002:0&gt; include PubChemTerms
=&gt; Object
irb(main):003:0&gt; get_cids '64318-79-2'
=&gt; ["5282237", "6434870"]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Our library has returned a Ruby array containing two compound identifiers. We can use PubChem to view their records &lt;a href="http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=5282237"&gt;here&lt;/a&gt; and &lt;a href="http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=6434870"&gt;here&lt;/a&gt;. Visual inspection reveals these two compounds to be isomers of each other, with the first member of the array containing the direct hit.&lt;/p&gt;

&lt;p&gt;Let's try another CAS number selected from another random Merck index entry:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
irb(main):004:0&gt; get_cids '66981-73-5'
=&gt; ["68870", "169125"]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Again we've obtained two CIDs, with the &lt;a href="http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=68870"&gt;first one&lt;/a&gt; being the neutral form and the second one being the &lt;a href="http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=169125"&gt;sodium salt&lt;/a&gt; of the antidepressant tianeptine.&lt;/p&gt;

&lt;h4&gt;Applications&lt;/h4&gt;

&lt;p&gt;Now, instead of converting one or two CAS numbers, imagine we've got a few thousand. Our library could be easily adapted to this purpose. The only caveat is that we'd need to &lt;a href="http://eutils.ncbi.nlm.nih.gov/entrez/query/static/eutils_help.html#UserSystemRequirements"&gt;observe the Entrez use policy&lt;/a&gt; and not overload the server with too many requests. We could build in a delay with Ruby's &lt;tt&gt;sleep&lt;/tt&gt; method.&lt;/p&gt;

&lt;p&gt;Notice that the library can be used to search for any keyword - not just CAS numbers. For example:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ irb
irb(main):001:0&gt; require 'entrez'
=&gt; true
irb(main):002:0&gt; include PubChemTerms
=&gt; Object
irb(main):003:0&gt; get_cids 'anandamide'
=&gt; ["5281969", "5283455", "5283388", "4671", "5353407", "5283452", "5283456", "5283451", "5283450", "5283449", "5283448", "5283447", "5283445", "5283444"]
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Like our previous queries, we've obtained multiple CIDs associated with the term 'anandamide', with the first one being the direct hit.&lt;/p&gt;

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

&lt;p&gt;Our little library isn't perfect, but it performs a very difficult task cheaply and conveniently in the majority of cases. By mashing up this functionality with other Ruby cheminformatics libraries (for example &lt;a href="http://depth-first.com/articles/2007/04/09/painless-installation-of-ruby-open-babel"&gt;Ruby Open Babel&lt;/a&gt; and &lt;a href="http://depth-first.com/articles/tag/rubycdk"&gt;Ruby CDK&lt;/a&gt;), a variety of tough and highly practical cheminformatics problems can be solved elegantly. Look to further installments of the Hacking PubChem series to find out how.&lt;/p&gt;</description>
      <pubDate>Thu, 13 Sep 2007 09:36:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:12e717a5-01e2-4b4c-871b-0a86c69f55e4</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2007/09/13/hacking-pubchem-convert-cas-numbers-into-pubchem-cids-with-ruby</link>
      <category>Tools</category>
      <category>pubchem</category>
      <category>hackingpubchem</category>
      <category>ruby</category>
      <category>entrez</category>
      <category>casnumber</category>
      <category>keyword</category>
    </item>
    <item>
      <title>"Hacking PubChem: Convert CAS Numbers into PubChem CIDs with Ruby" by Rich Apodaca</title>
      <description>&lt;p&gt;Antony,&lt;/p&gt;

&lt;p&gt;Your article raises some good points. The interesting question is not 'how do we get it right the first time', but 'how do we fix it when it's broken.' Wikipedia, Open Source, and many others have shown that if you find the answer to the second question, the first question becomes much less important than it may originally seem.&lt;/p&gt;</description>
      <pubDate>Sat, 22 Sep 2007 10:12:12 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d0b15325-5995-4917-9335-3d96372418dd</guid>
      <link>http://depth-first.com/articles/2007/09/13/hacking-pubchem-convert-cas-numbers-into-pubchem-cids-with-ruby#comment-185</link>
    </item>
    <item>
      <title>"Hacking PubChem: Convert CAS Numbers into PubChem CIDs with Ruby" by ChemSpiderMan</title>
      <description>&lt;p&gt;Rich...I know you've seen the commentary I made about the quality of the data available on online databases but I post it here for your readers awareness: &lt;a href="http://www.chemspider.com/blog/?p=137" rel="nofollow"&gt;http://www.chemspider.com/blog/?p=137&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 21 Sep 2007 21:12:54 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f85cb56e-a478-4d26-8ddd-a75b7d939cbc</guid>
      <link>http://depth-first.com/articles/2007/09/13/hacking-pubchem-convert-cas-numbers-into-pubchem-cids-with-ruby#comment-184</link>
    </item>
  </channel>
</rss>
