Cheminfo-to-web Project

Indigo Demo

Overview

In current compilation, hundreds of Indigo API functions has been exported to JavaScript. Together with indigo.js and indigo.wasm, an additional JS file (indigoAdapter.js) should be used, which encapsulates all those functions:

<script src="indigo.js"></script>
<script src="indigoAdapter.js"></script>

After the scripts being loaded, an Indigo namespace with API functions can be created. Since the loading of wasm file is a asynchronous process, you have to use a callback function to be notified with the ready state of Indigo:

var Indigo = CreateIndigo();
Indigo.Module.onRuntimeInitialized = function()
{
  // Now the functions inside Indigo namespace can be called safely
}
			

The original Indigo C functions are all starts with "indigo" prefix, such as indigoLoadMoleculeFromString. Since in JavaScript, all functions are encapsulated in one namespace, those leading prefixes are all omitted, you should call the function as Indigo.loadMoleculeFromString (note the first letter is lowercased).

I/O

For example, the following code can be used to load a MDL MOL format molecule and save it in CML/SMILES format:

var molData = '...';  // Set input MDL MOL format data here
var mol = Indigo.loadMoleculeFromString(molData);  // load the data and get molecule handle
var cmlData = Indigo.cml(mol);  // save to CML
console.log(cmlData);
var smiles = Indigo.canonicalSmiles(mol);  // get canonicalized SMILES
console.log(smoles);
Indigo.free(mol);  // free memory of mol object

Live demo:




Auto layout

Indigo is able to automatically do a 2D layout to molecule without explicit 2D coordinates (e.g., loaded from SMILES). The following code can do the same job in JavaScript:

var smiles = 'c1ccccc1';  // SMILES of benzene
var mol = Indigo.loadMoleculeFromString(smiles);  // load smiles and get molecule without coordinates
Indigo.setOption("smart-layout", "true");  // set option of layout
if (Indigo.layout(mol) >= 0)  // layout successful
{
	var molData = Indigo.molfile(mol);  // output MDL MOL data
	console.log(molData);
}
Indigo.free(mol);

Live demo:




Tautomers

Tautomers of a molecule can be listed with Indigo iterator API, as in following code:

var mol = Indigo.loadMoleculeFromString('...');  // load source molecule
var iterator = Indigo.iterateTautomers(mol);
var tautomerItem;		
while (Indigo.hasNext(iterator))
{
	tautomerItem = Indigo.next(iterator);
	if (tautomerItem >= 0)
	{
		var mol2 = Indigo.clone(tautomerItem);  // copy tautomer molecule
		var tautomerSmiles = Indigo.canonicalSmiles(mol2);  // output smiles
		console.log(tautomerSmiles);
		Indigo.free(mol2);
	}
}
Indigo.free(iterator);

Live demo: