Jmol Cdk Integration

From Jmol
Revision as of 21:30, 26 April 2006 by 134.95.200.25 (talk) (The CdkModelAdapter)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Integrating Jmol into CDK Programs

This page will introduce ways in which Jmol can be integrated into CDK programs, so that the first can be used as a 3D viewer for the CDK data structures.

There are two principal options.

The Inline String method

This method defines the interface between CDK and Jmol as a plain text file format, e.g. CML or XYZ. It assumes that CDK has a writer for this common format, and that Jmol has a reader for this format. There are many formats that match these criteria, and the two formats mentioned earlier are two examples.

The interesting bits are:

JmolPanel jmolPanel = new JmolPanel();
contentPane.add(jmolPanel);
       
JmolViewer viewer = jmolPanel.getViewer();
viewer.openFile(filename);

and

class JmolPanel extends JPanel {
  JmolViewer viewer;
  JmolAdapter adapter;
  JmolPanel() {
    // use CDK IO
    adapter = new CdkJmolAdapter(null);
    viewer = new JmolViewer(this, adapter);
  }
}

The full source code can be retrieved from Jmol SVN: CDKIntegration.java.

The CdkModelAdapter

The JmolViewer can be instantiated such that it uses the CdkJmolAdapter, instead of the SmarterJmolAdapter. These JmolAdapters convert internal representations - CDK or Jmol, respectively - into an API (the JmolAdapter) used by the JmolViewer.

 * CdkJmolAdapter for Jmol 10.2

This CdkJmolAdapter can be used to interface between CDK and Jmol when integrating the two.

The source code for this method is quite similar to that above. The JmolPanel is identical only the first bit is different:

JmolPanel jmolPanel = new JmolPanel();
JmolViewer viewer = jmolPanel.getViewer();

// create a CDK data object: ChemFile        
Molecule methane = new Molecule();
Atom atom = new Atom("C");
atom.setPoint3d(new Point3d( 0.257,-0.363, 0.000));
methane.addAtom(atom);
atom = new Atom("H");
atom.setPoint3d(new Point3d( 0.257, 0.727, 0.000));
methane.addAtom(atom);
atom = new Atom("H");
atom.setPoint3d(new Point3d( 0.771,-0.727, 0.890));
methane.addAtom(atom);
atom = new Atom("H");
atom.setPoint3d(new Point3d( 0.771,-0.727,-0.890));
methane.addAtom(atom);
atom = new Atom("H");
atom.setPoint3d(new Point3d(-0.771,-0.727, 0.000));
methane.addAtom(atom);

// then send it to the Jmol Viewer
viewer.openClientFile("", "", methane);

(This requires the CdkJmolAdapter from SVN, or the first release after 10.00).

Instead of an AtomContainer, you can also pass a ChemFile for which the ChemSequence is interpreted a sequence of Jmol models. Or, a Crystal in which the unit cell is rendered too.

The full source code can be retrieved from: CDKIntegration2.java (for AtomContainer) and CDKIntegration3.java (for Crystal). Note: Those classes have been removed from Jmol.

Contributors

NicolasVervelle