<?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 jni</title>
    <link>http://depth-first.com/articles/tag/jni</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Walking the Web of Chemical Informatics</description>
    <item>
      <title>JRuby for Cheminformatics: Reading and Writing InChIs Via the Java Native Interface</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 increased use of the &lt;a href="http://depth-first.com/articles/2007/09/27/inchi-for-newbies"&gt;InChI identifier&lt;/a&gt; is making the reading and writing of InChIs a standard cheminformatics capability. Recent articles have discussed the &lt;a href="http://depth-first.com/articles/tag/jruby"&gt;advantages of JRuby for cheminformatics&lt;/a&gt;. One disadvantage of JRuby is that code written in C can't be directly used. The presents a potential problem for libraries, such as the InChI toolkit, that are written in C. Fortunately, the solution is simple. Today's tutorial will demonstrate how InChIs can be both read and written using the C-InChI toolkit via JRuby and the excellent &lt;a href="http://jni-inchi.sourceforge.net/"&gt;JNI-InChI library&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;About JNI-InChI&lt;/h4&gt;

&lt;p&gt;The &lt;a href="http://jni-inchi.sourceforge.net/"&gt;JNI-InChI&lt;/a&gt; library, written by Jim Downing and Sam Adams, wraps the &lt;a href="http://www.iupac.org/inchi/"&gt;C InChI toolkit&lt;/a&gt; in a Java Native Interface. This low-level toolkit is suitable for building more complex software, but lacks many features present in the C InChI toolkit. For example, JNI-InChI doesn't directly interconvert SMILES or molfile with InChI. For that you'd need to build a support library. If you're building a toolkit from scratch, this lightweight approach can be a significant advantage.&lt;/p&gt;

&lt;p&gt;The JNI-InChI binary distribution jarfile includes the compiled native InChI library. In this sense it's virtually indistinguishable from any other Java library. This simplified packaging makes it exceptionally easy to use JNI-InChI from JRuby, as we'll see below.&lt;/p&gt;

&lt;h4&gt;Installation&lt;/h4&gt;

&lt;p&gt;JRuby &lt;a href="http://depth-first.com/articles/2007/10/09/jruby-for-cheminformatics-parsing-smiles-simply"&gt;can be installed&lt;/a&gt; as described previously. To install the JNI-InChI library for JRuby, simply copy the &lt;a href="http://sourceforge.net/project/showfiles.php?group_id=173262"&gt;current release jarfile&lt;/a&gt; into the &lt;tt&gt;lib&lt;/tt&gt; directory of your JRuby installation. That's all there is to it.&lt;/p&gt;

&lt;h4&gt;A Simple Library&lt;/h4&gt;

&lt;p&gt;We can now write a simple library to read InChIs via JRuby:&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;java&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="ident"&gt;include_class&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;net.sf.jniinchi.JniInchiInput&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;include_class&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;net.sf.jniinchi.JniInchiInputInchi&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;include_class&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;net.sf.jniinchi.JniInchiWrapper&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;IUPAC&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;read_inchi&lt;/span&gt; &lt;span class="ident"&gt;inchi&lt;/span&gt;
    &lt;span class="ident"&gt;input&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;JniInchiInputInchi&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;inchi&lt;/span&gt;

    &lt;span class="constant"&gt;JniInchiWrapper&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;getStructureFromInchi&lt;/span&gt; &lt;span class="ident"&gt;input&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;h4&gt;Testing the Library&lt;/h4&gt;

&lt;p&gt;By saving the above library to a file called &lt;strong&gt;iupac.rb&lt;/strong&gt;, we can parse InChIs via JRuby:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ jirb
irb(main):001:0&gt; require 'iupac'
=&gt; true
irb(main):002:0&gt; include IUPAC
=&gt; Object
irb(main):003:0&gt; output = read_inchi 'InChI=1/C14H10/c1-3-7-13-11(5-1)9-10-12-6-2-4-8-14(12)13/h1-10H'
=&gt; #&lt;Java::NetSfJniinchi::JniInchiOutputStructure:0x1ed5459 @java_object=net.sf.jniinchi.JniInchiOutputStructure@313170&gt;
irb(main):004:0&gt; output.num_atoms
=&gt; 14
irb(main):005:0&gt; output.num_bonds
=&gt; 16
&lt;/pre&gt;
&lt;/div&gt;

&lt;h4&gt;Writing InChIs&lt;/h4&gt;

&lt;p&gt;Because JNI-InChI is a low-level toolkit, writing InChIs is feasible, but not trivial. We must first construct a representation, and then get the InChI for it. For example, we could get the InChI for methane as follows:&lt;/p&gt;

&lt;div class="console"&gt;
&lt;pre&gt;
$ jirb
irb(main):001:0&gt; require 'java'
=&gt; true
irb(main):002:0&gt; include_class 'net.sf.jniinchi.JniInchiInput'
=&gt; ["net.sf.jniinchi.JniInchiInput"]
irb(main):003:0&gt; include_class 'net.sf.jniinchi.JniInchiAtom'
=&gt; ["net.sf.jniinchi.JniInchiAtom"]
irb(main):004:0&gt; include_class 'net.sf.jniinchi.JniInchiWrapper'
=&gt; ["net.sf.jniinchi.JniInchiWrapper"]
irb(main):005:0&gt; input = JniInchiInput.new
=&gt; #&lt;Java::NetSfJniinchi::JniInchiInput:0x2f2295 @java_object=net.sf.jniinchi.JniInchiInput@15b0333&gt;
irb(main):006:0&gt; a1 = input.add_atom JniInchiAtom.new(0,0,0, "C")
=&gt; #&lt;Java::NetSfJniinchi::JniInchiAtom:0x1b22920 @java_object=net.sf.jniinchi.JniInchiAtom@2f356f&gt;
irb(main):007:0&gt; a1.set_implicit_h(4)
=&gt; nil
irb(main):008:0&gt; output = JniInchiWrapper.get_inchi input
=&gt; #&lt;Java::NetSfJniinchi::JniInchiOutput:0xf894ce @java_object=net.sf.jniinchi.JniInchiOutput@132ae7&gt;
irb(main):009:0&gt; output.get_inchi
=&gt; "InChI=1/CH4/h1H4"
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Fortunately, we don't have to work that hard. The &lt;a href="http://cdk.sf.net"&gt;Chemistry Development Kit&lt;/a&gt;, through JNI-InChI, supports reading and writing of InChIs via a variety of molecular languages, including SMILES and molfile. More on that later, though.&lt;/p&gt;

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

&lt;p&gt;Provided that a Java Native Interface exists for a C library, it can be used from JRuby. Future articles will discuss the use of other cheminformatics libraries written in either C or C++ from JRuby, and their integration with pure Java and Ruby libraries.&lt;/p&gt;</description>
      <pubDate>Wed, 10 Oct 2007 08:21:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0348fa93-7376-488d-9afc-789590ac9fcb</guid>
      <author>Rich Apodaca</author>
      <link>http://depth-first.com/articles/2007/10/10/jruby-for-cheminformatics-reading-and-writing-inchis-via-the-java-native-interface</link>
      <category>Tools</category>
      <category>rubidium</category>
      <category>jruby</category>
      <category>ruby</category>
      <category>java</category>
      <category>jni</category>
      <category>inchi</category>
      <category>cdk</category>
    </item>
  </channel>
</rss>
