Hacking PubChem: Visually Inspect Results for CAS Number and Keyword Searches 1

Posted by Rich Apodaca Tue, 25 Sep 2007 14:55:00 GMT

A recent article described how PubChem could be used to quickly search for CAS numbers. Although useful, the approach is limited in that only an array of PubChem CIDs was returned. What would be really useful would be a simple way to create a report with entries hyperlinked into the PubChem site itself to aid in visual inspection. In this tutorial, we'll see how an HTML template and a few extra lines of code can do just that.

The Template

Ruby supports a number of HTML templating mechanisms. In this example, we'll use an ERB template resurrected from the Molbank graphical table of contents tutorial:

<html>
  <head>
    <title>
      <%= "PubChem Search for #{term}" %>
    </title>
  </head>
  <body>
    <h1>
      <%= "Search: #{term}" %>
    </h1>
    <table>
      <tr>
      <% col = 0 %>
      <% cids.each do |cid| %>
        <td>
          <% image = "http://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=#{cid}" %>
          <% summary = "http://pubchem.ncbi.nlm.nih.gov/summary/summary.cgi?cid=#{cid}" %>
          <a href="<%= summary %>">
            <img src="<%= image %>" border="2"></img>
          </a>
          <center>
            <span style="font-size: 8px">
              <a href="<%= summary %>"><%= "CID-#{cid}" %></a>
            </span>
          </center>
        </td>
        <% col += 1 %>
        <% if col > 5 %>
          <% col = 0 %>
          </tr>
          <tr>
        <% end %>
      <%end %>
      </tr>
    </table>
  </body>
</html>

The above template uses a search term and an array of CIDs to build a table of results. Each cell in the table contains a color 2D image and the CID, both hyperlinked into PubChem itself.

Saving this library to a file called template.rhtml is all we need to do.

The Library

The library is a modification of the one shown in the previous article in this series:

require 'rubygems'
require 'mechanize'
require 'erb'

module PubChemTerms
  def report term
    cids = get_cids term
    erb = ERB.new(IO.read("template.rhtml"))

    File.open "output.html", 'w+' do |file|
      file << erb.result(binding)
    end
  end

  def get_cids term
    agent = WWW::Mechanize.new
    page = agent.get "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=#{term}"

    (page.parser/"id").collect {|id| id.innerHTML}
  end
end

The method report accepts a search term and uses our template to render a report.

Testing

By saving the above library in a file called pubchem.rb, we can search by keyword via interactive ruby (irb):

$ irb
irb(main):001:0> require 'pubchem'
=> true
irb(main):002:0> include PubChemTerms
=> Object
irb(main):003:0> report 'esomeprazole'
=> #

This produces a file called output.html that can be viewed with any browser:

As in the original version of the library, we can also query by CAS number:

$ irb
irb(main):001:0> require 'pubchem'
=> true
irb(main):002:0> include PubChemTerms
=> Object
irb(main):003:0> report '119141-88-7'
=> #

Conclusions

The simple approach outlined here could be extended in many ways. For example, we could easily retrieve molfiles based on keyword or CAS number search. We could pipe queries together or work with query lists. We could blend in ChemSpider data. We could even build a simple Web application (with Rails) that returned customized reports. Mixing in Ruby CDK or Ruby Open Babel offers still more possibilities.

Increasingly, the most important question in cheminformatics is not "What can we build?", but rather "What should we build?" Success in this new world requires a much deeper understanding of how cheminformatics software is being used by real chemists and where it's not.

Hacking PubChem: Convert CAS Numbers into PubChem CIDs with Ruby 2

Posted by Rich Apodaca Thu, 13 Sep 2007 13:36:00 GMT

Although the PubChem system has been discussed in numerous recent D-F articles and elsewhere, there's much more to the story that hasn't been told. One of the more intriguing things PubChem can do is look up CAS Numbers for free. 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).

The Library

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 Entrez eUtils system. 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.

The library is simplicity itself:

require 'rubygems'
require 'mechanize'

module PubChemTerms
  def get_cids term
    agent = WWW::Mechanize.new
    page = agent.get "http://www.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pccompound&retmax=100&term=#{term}"

    (page.parser/"id").collect {|id| id.innerHTML}
  end
end

The excellent Ruby library Mechanize is used for submitting queries and processing the results. (This is the same library that was used to extract full bibliographical information from nothing more than a DOI). The only remarkable thing about the library above is how unremarkable it is.

A Test

We can test the library by saving it in a file called entrez.rb 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:
$ irb
irb(main):001:0> require 'entrez'
=> true
irb(main):002:0> include PubChemTerms
=> Object
irb(main):003:0> get_cids '64318-79-2'
=> ["5282237", "6434870"]

Our library has returned a Ruby array containing two compound identifiers. We can use PubChem to view their records here and here. Visual inspection reveals these two compounds to be isomers of each other, with the first member of the array containing the direct hit.

Let's try another CAS number selected from another random Merck index entry:

irb(main):004:0> get_cids '66981-73-5'
=> ["68870", "169125"]

Again we've obtained two CIDs, with the first one being the neutral form and the second one being the sodium salt of the antidepressant tianeptine.

Applications

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 observe the Entrez use policy and not overload the server with too many requests. We could build in a delay with Ruby's sleep method.

Notice that the library can be used to search for any keyword - not just CAS numbers. For example:

$ irb
irb(main):001:0> require 'entrez'
=> true
irb(main):002:0> include PubChemTerms
=> Object
irb(main):003:0> get_cids 'anandamide'
=> ["5281969", "5283455", "5283388", "4671", "5353407", "5283452", "5283456", "5283451", "5283450", "5283449", "5283448", "5283447", "5283445", "5283444"]

Like our previous queries, we've obtained multiple CIDs associated with the term 'anandamide', with the first one being the direct hit.

Conclusions

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 Ruby Open Babel and Ruby CDK), 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.