Chemical Canvas

A jQuery plugin for drawing and interacting with chemical diagrams.

Support for popular chemical formats and the PDB Chemical Component Dictionary files.

Follows an MVC design for integration with different visualization components (like 3-D viewers or tabular formats).

How to use: see the documentations and view the source below. All necessary scripts and files can be downloaded from this site

For questions or comments contact ddimitrop@gmail.com

Layered organization of plugins:


Standalone chemical editor (chemDiagram)

Create a chemical diagram editor from an element.

Use:
<div id="chemEdit"><div>
$('#chemEdit').chemDiagram();

Options:
width, height: the width and height of the drawing area (excluding the controls) - default 600x300
readOnly: disable editing operations (adding or deleting atoms and bonds) - default false
fragments: enable the display of the fragment library - default true
fragmentsArray: provide custom fragments

Example:


This is the chemEdit div



Run this code: $('#chemEdit').chemDiagram({width:1000});


or view the clean source of an example page

Integration of 2D, 3D and tabular views (chemViews)

It integrates jmol (http://www.jmol.org/) as a 3D viewer and jqGrid jQuery plugin (http://www.trirand.com/blog/) as a tabular viewer for atoms and bonds properties.

Use:

<div id="chemView"><div>
$('#chemView').chemViews();             

Options:
diagram:options for the diagram editor (see above)
jmol: witdth and height for jmol (default {width:600,height:500})
atomModel,bondModel: jqGrid model settings for atoms and bonds tables

Example:
$('#chemView').chemViews({diagram:{width:1000},jmol{width:500}});             
Try it out

Integration of windowing system (chemWin)

It integrates the jQuery Windows Engine Plugin (http://hernan.amiune.com/labs) and demonstrates the use of Ajax requests in order to integrate with chemoinformatics programs like CACTVS (http://www2.chemie.uni-erlangen.de/software/cactvs/)

<div id="chemWin"></div>
$('#chemWin').chemWin();                        

Options:
jmol: options for the jmol window - defaults {title: "3-D view",width:600,height:500,posx:10,posy:585}
file: options for the chemical file output window - defaults {title: "Chemical format",width:400,height:500,posx:615,posy:0}
atoms,bonds: options for the atom,bond grid windows
edit:o options for the chemical canvas editor window -defaults {title: "Chemical diagram",width:600,height:500,posx:10,posy:40}
control:options for the controls area {hide:false,load:true,upload:false,align2d:true,addhydro:true,align3d:true,reset:true}

Example
$('#chemWin').chemWin({mol:{title:"JMOL",width:1000}});                        
Try it out

Accessing components:

You may access the molecule (and diagram) objects from your javascript code like this:

For a plain chemical canvas editor:
Run the code below (First make sure that you have clicked on the link above that creates a chemCanvas. Also you may want to draw something inside the editor)
molecule=$('#chemEdit').chemMol();
diagram=$('#chemEdit').chemCanvasDiagram();
alert(molecule.atoms.length);
Similarily for a chemical views component:
molecule=$('#chemView').chemViewMol();
diagram= $('#chemView').chemViewDiagram();
Or for a chemical windows component:
molecule=$('#chemWin').chemWinMol();
diagram= $('#chemWin').chemWinDiagram();

Core functionality of a molecule object

Click on the links below to try out the code, next to each. First make sure that you have created a chemical canvas and grabbed the molecule object by using the links above. Also make sure that you have drawn something in the editor.

Run this: alert(molecule.atoms.length) // array of atoms
Run this: alert(molecule.bonds.length) // array of bonds
Run this: molecule.id='test' // identifier
Run this: molCopy=molecule.clone(); alert(molCopy.atoms.length)
Run this: alert(molecule.formula());

Core functionality of the diagram object:

Run this: diagram.setModel(new Molecule()); //use another molecule as the model of the diagram
Run this: diagram.redraw(); // programmatically redraw the diagram
Run this: diagram.center();

Select, Change and Highlight events

You can register listeners and capture events on the model like when the users selects or changes atoms and bonds.

Example:
Run this:
molecule.select(function(a) {
           if (a instanceof Atom) 
               alert('You selected atom '+a.name);
         });
and now go a click on atoms of the diagram editor.

Other functionality:

Chemical Formats

There is support for export import from common chemical formats like SDF, PDB and mmCif

Click on the links below to try out the code, next to each. First make sure that you have created a chemical canvas and grabbed the molecule object by using the links above. Also make sure that you have drawn something in the editor.

Run this: alert(molecule.toSdf())
Run this: alert(molecule.toPdb())

This is the textarea with id="bnz"

Parse the contents of this textarea as SDF and load it in the editor

Run this:
    molecule.fromSdf($('#bnz').val());
    diagram.center();
    diagram.redraw();

Scroll up and check the chemical editor above

This is the textarea with id="plq"

Parse the contents of this textarea as mmCif and load it in the editor

Run this:
    molecule.fromCif($('#plq').val());
    diagram.center();
    diagram.redraw();

Scroll up and check the chemical editor above

Run this: alert(molecule.toCif())

How to add or change the fragments

The chemical editor comes with a collection of fragments and you may add on this or replace it. The chemical library provides support for easily builting your own custom fragments.



This is the chemEdit1 div


Run this code:
   $('#chemEdit1').chemDiagram({
      fragmentsArray:{
                      cyclopropane:Fragment.prototype.fragments.cyclopropane, // keep one from the standard library
                      propane: new CustomFragment([{},{on:0},{on:1}]),
                      carboxyl: new CustomFragment([{element:'C',name:'CX'},{element:'O',name:'OX',on:0,order:2},{element:'O',name:'OY',on:0}])
                     }
    });

Note: In order that your custom fragments UI looks nice, you should create small images with the same names as the fragments you define (i.e. propane.png and carboxyl.png)

Model view example

The model - a molecule and its listeners - is seperated from the view and control code (like the chemical editor). To demo that the 2 editors that will be created with the code below will share the same model, so any modifications on one will be immediatelly propagated in the other

This is the chemEdit2 div
This is the chemEdit3 div


Run this code:
   $('#chemEdit2').chemDiagram();
   molecule2=$('#chemEdit2').chemMol();
   diagram2=$('#chemEdit2').chemCanvasDiagram();
   $('#chemEdit3').chemDiagram();
   molecule3=$('#chemEdit3').chemMol();
   diagram3=$('#chemEdit3').chemCanvasDiagram();
   diagram3.setModel(molecule2);                 // The editor 3 uses the same model as the editor 2                  
   // Make sure that both editors get redrawn when the model changes
   molecule2.change (function() {diagram2.redraw();diagram3.redraw() });
   molecule2.select (function() {diagram2.redraw();diagram3.redraw() });

Ajax calls to chemoinformatics packages

The chemical canvas plugin contains very few and simple chemical algorithms. Its basic idea to run existing chemoinformatics packages on the server side and retrieve the results with Ajax calls.

For example it has been used with CACTVS (http://www2.chemie.uni-erlangen.de/software/cactvs/) support in the backend in order to retrieve an optimal 2D layout of the molecule (when the input is an SDF file with 3D or no coordinates for example) or in order to convert input from other formats (like SMILES) to an explicit chemical format (like SDF) that it supports.

For more information you may look at the code provided by the jQuery.chemAjax.js script.


chemComp mmCif editor

A more advanced example of customising and using the plugin to edit Chemical Component Dictionary (mmCif) files is available below.
(some functions will not work since they depend on the server-side installation of chemoinformatic packages)

Try it out

Project details

Copyright (c) 2010 Dimitris Dimitropoulos (ddimitrop@gmail.com)
Dual licensed under the MIT and GPL licenses:
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
This project is using the following: