Skip to content

metamolecular/inchi-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

InChI.js - InChI for the Web Browser

InChI is an open chemical identifier system created by IUPAC. InChI is also a program and library written in C, and the only implementation to have been released publicly.

InChI's C codebase poses challenges to Web developers. The only viable way to generate InChIs is to deploy native binaries on a Web server. InChIs are then generated through asynchronous HTTP requests and responses. Although this approach works, it comes at the price of requiring the installation and maintenance of server-side software, as well as requiring an application layer capable of sanitizing and processing HTTP requests.

InChI.js solves this problem by offering an InChI implementation that runs unmodified on modern Web browsers without plugins. Eliminating the server-side InChI component can reduce Web development costs and make new kinds of browser-based applications viable. For example, InChI.js can be used to build static sites.

Optimization efforts have shown early promise. For example, the size of the main InChI.js file is under 900 KB, which compresses to under 300 KB with gzip. Performance should be adequate for most applications. For example, benchmarks performed on average hardware using Google Chrome and Mozilla Firefox gave InChI generation times of 10 ms or less for simple structures.

InChI.js is made possible by Emscripten, a C-to-JavaScript transpilation toolchain.

Demo

http://metamolecular.com/inchi-js

Compilation

InChI.js can be compiled from source. To do so, first install the Emscripten toolchain.

If using the Emscripten portable installation, activate it with:

$ path-to-portable-installation/emsdk activate latest

On OSX an additional step is needed:

$ source path-to-portable-installation/emsdk_env.sh

After Emscripten has been installed and initialized, clone this repository, initialize its InChI submodule, and run make:

$ cd inchi.js
$ git submodule init
$ git submodule update
$ make

Deployment

The build directory contains three files:

  • inchi.js: The complete JavaScript code required to generate InChIs.
  • inchi.js.mem: The Emscripten memory file that will be loaded by inchi.js
  • inchi-node.js: All-in-one file for Node.js deployment.

Browser deployment uses the first two file. Create an HTML document containing the following tag:

<script async src="../build/inchi.js"></script>

Using the async attribute allows the included inchi.js.mem to be loaded correctly.

Initialization takes place by defining an InChI object, and registering an onRuntimeInitialized callback function. Code within that function will be guaranteed to have a fully initialized InChI object.

The following script initializes InChI.js. Place it within an inline script tag.

var InChI = {
  onRuntimeInitialized: function () {
    InChI.fromMolfile = InChI.cwrap('get_inchi', 'string', ['string']);
    
    var molfile; // get a molfile
    var inchi = InChI.fromMolfile(molfile);
  },
  memoryInitializerPrefixURL: 'path/to/js.mem/folder'
};

InChI.getMolfile returns an InChI string on success. On warning or failure, a multi-line string is returned in which warnings and errors appear after the first line.

Node.js deployment uses the third file, inchi-node.js. InChIs can be generated from this file using a script such as the following:

let lib = require('inchi-node');

const molfileToInchi = lib.wrap('get_inchi', 'string', [ 'string' ]);

let molfile = getMolfile(); // load a molfile string
let inchi = molfileToInchi(molfile);

Alternatively, the convenience library contained in lib can be used:

let InChI = require('./lib/inchi');

let molfile = getMolfile(); // load a molfile string
let inchi = InChI.molfileToInchi(molfile);
let key = InChI.inchiToKey(inchi);

Running Compiled inchi.js

After compiling the code, a sample web page can be run from the web/index.html file. Depending on your browser (e.g., Chrome), it may be necessary to run this page behind a localhost server. OS X bundles a lightweight server in the form of SimpleHTTPServer. Another option is the Node.js http-server module.

Other Work

InChI.js was made possible through the previous work of Noel O'Boyle and Michał Nowotka. Read about their progress here:

License

Copyright (C) 2015 Metamolecular, LLC. Compiled code uses content copyright (C) 2011 IUPAC and InChI Trust Limited.

This library and build system are licensed under the terms of the IUPAC/InChI-Trust InChI License No. 1.0 (see: LICENSE.txt).