<?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: A Simple and Portable Ruby Interface to InChI - Part 2: Silencing Console Output</title>
    <link>http://depth-first.com/articles/2008/05/30/a-simple-and-portable-ruby-interface-to-inchi-part-2-silencing-console-output</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>A Simple and Portable Ruby Interface to InChI - Part 2: Silencing Console Output</title>
      <description>&lt;p&gt;&lt;a href="http://ruby-lang.org/"&gt;&lt;img src="http://depth-first.com/files/ruby_logo_new.gif" align="right"&gt;&lt;/img&gt;&lt;/a&gt;The previous article in this series described a &lt;a href="http://depth-first.com/articles/2008/05/29/a-simple-and-portable-ruby-interface-to-inchi"&gt;simple and portable method&lt;/a&gt; for interfacing Ruby to the cInChI-1 binary. One disadvantage was noisy console output. This article offers a minor modification to disable it.&lt;/p&gt;

&lt;h4&gt;The Code&lt;/h4&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;InChI&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;inchi_for&lt;/span&gt; &lt;span class="ident"&gt;molfile&lt;/span&gt;
    &lt;span class="ident"&gt;output&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;%x[&lt;/span&gt;&lt;span class="string"&gt;echo &amp;quot;&lt;span class="expr"&gt;#{molfile}&lt;/span&gt;&amp;quot; | cInChI-1 -STDIO 2&amp;gt;/dev/null&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;

    &lt;span class="ident"&gt;output&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;eql?&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&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="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&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;output&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;(/&lt;/span&gt;&lt;span class="regex"&gt;&lt;span class="escape"&gt;\n&lt;/span&gt;&lt;/span&gt;&lt;span class="punct"&gt;/)[&lt;/span&gt;&lt;span class="number"&gt;1&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;Here, we're taking advantage of the ability to redirect certain output streams to &lt;tt&gt;/dev/null&lt;/tt&gt;.&lt;/p&gt;

&lt;h4&gt;Testing the Code&lt;/h4&gt;

&lt;p&gt;Saving the above in a file called &lt;strong&gt;inchi.rb&lt;/strong&gt;, we can test it from IRB. To make things interesting, let's pull a molfile from &lt;a href="http://chempedia.com"&gt;Chempedia&lt;/a&gt;:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ irb
irb(main):001:0&amp;gt; require 'open-uri'
=&amp;gt; true
irb(main):002:0&amp;gt; require 'inchi'
=&amp;gt; true
irb(main):003:0&amp;gt; include InChI
=&amp;gt; Object
irb(main):004:0&amp;gt; open 'http://chempedia.com/compounds/83490.mol' do |f|
irb(main):005:1*   puts inchi_for(f.read)
irb(main):006:1&amp;gt; end
InChI=1/C15H15NO3S/c17-14(16-18)11-20(19)15(12-7-3-1-4-8-12)13-9-5-2-6-10-13/h1-10,15,18H,11H2,(H,16,17)
=&amp;gt; nil
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We should be able to run this code unmodified on any UNIX-like system in which the &lt;strong&gt;cInChI-1&lt;/strong&gt; binary is on the path. And of course we could take this one step further by allowing &lt;a href="http://depth-first.com/articles/2007/03/19/customize-inchi-output-with-rino"&gt;command line options&lt;/a&gt; to be passed in as parameters to the &lt;tt&gt;inchi_for&lt;/tt&gt; method.&lt;/p&gt;

&lt;p&gt;Simplicity has its advantages.&lt;/p&gt;</description>
      <pubDate>Fri, 30 May 2008 10:04:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:86f5938d-6519-4d2a-87e1-14b281f1323b</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2008/05/30/a-simple-and-portable-ruby-interface-to-inchi-part-2-silencing-console-output</link>
      <category>Tools</category>
      <category>inchi</category>
      <category>ruby</category>
      <category>designingtheobvious</category>
      <category>chempedia</category>
      <category>console</category>
      <category>unix</category>
    </item>
  </channel>
</rss>
