Difference between revisions of "Programmatic Access to Jmol"

From Jmol
Jump to navigation Jump to search
(First entry)
 
m
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
Jmol can be [[Applications Embedding Jmol|embedded]] in your existing Java applications. After embedding the Jmol Viewer in your application, it can be accessed programmatically. This allows developers to accomplish every thing in Jmol by running scripts.
 
Jmol can be [[Applications Embedding Jmol|embedded]] in your existing Java applications. After embedding the Jmol Viewer in your application, it can be accessed programmatically. This allows developers to accomplish every thing in Jmol by running scripts.
  
To embed Jmol and to show the structure of a molecule having its PDB file, can be done using the following example.
+
To embed Jmol and to show the structure of a molecule using its PDB file, follow the example below.
(Biojava should be in your classpath. Get it from [http://www.biojava.org/wiki/BioJava:Download Biojava Downloads Page])
 
  
package org.jmoltest;
+
package org.jmoltest;
 
+
import java.awt.Container;
+
import java.awt.Dimension;
+
import java.awt.Container;
import java.awt.Graphics;
+
import java.awt.Dimension;
import java.awt.Rectangle;
+
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
+
import java.awt.Rectangle;
import java.awt.event.WindowEvent;
+
import java.awt.event.WindowAdapter;
import javax.swing.JFrame;
+
import java.awt.event.WindowEvent;
import javax.swing.JPanel;
+
import javax.swing.JFrame;
import org.biojava.bio.structure.Structure;
+
import javax.swing.JPanel;
import org.biojava.bio.structure.io.PDBFileReader;
+
import org.jmol.adapter.smarter.SmarterJmolAdapter;
import org.jmol.adapter.smarter.SmarterJmolAdapter;
+
import org.jmol.api.JmolAdapter;
import org.jmol.api.JmolAdapter;
+
import org.jmol.api.JmolSimpleViewer;
import org.jmol.api.JmolSimpleViewer;
 
 
   
 
   
 +
public class MyJmolViewer {
 
   
 
   
public class SimpleJmolExample {
+
    private String title = "Programmatic Access to Jmol";
 
     JmolSimpleViewer viewer;
 
     JmolSimpleViewer viewer;
    Structure structure;
 
 
   
 
   
 
     JmolPanel jmolPanel;
 
     JmolPanel jmolPanel;
     JFrame frame ;
+
     JFrame jframe;
 
   
 
   
     public static void main(String[] args){
+
     public MyJmolViewer() {
 
         try {
 
         try {
 
   
 
   
Line 38: Line 36:
 
             Structure struc = pdbr.getStructureById(pdbCode);
 
             Structure struc = pdbr.getStructureById(pdbCode);
 
   
 
   
             SimpleJmolExample ex = new SimpleJmolExample();
+
             initializeJmolPanel ex = new initializeJmolPanel();
 
             ex.setStructure(struc);
 
             ex.setStructure(struc);
 
   
 
   
Line 48: Line 46:
 
   
 
   
 
   
 
   
     public SimpleJmolExample() {
+
     public initializeJmolPanel() {
 
         frame = new JFrame();
 
         frame = new JFrame();
 
         frame.addWindowListener(new ApplicationCloser());
 
         frame.addWindowListener(new ApplicationCloser());
Line 59: Line 57:
 
         frame.pack();
 
         frame.pack();
 
         frame.setVisible(true);  
 
         frame.setVisible(true);  
 
    }
 
    public void setStructure(Structure s) {
 
 
        frame.setName(s.getPDBCode());
 
 
        // actually this is very simple
 
        // just convert the structure to a PDB file
 
 
        String pdb = s.toPDB();
 
 
        structure = s;
 
        JmolSimpleViewer viewer = jmolPanel.getViewer();
 
 
        // Jmol could also read the file directly from your file system
 
        //viewer.openFile("/Path/To/PDB/1tim.pdb");
 
 
        // send the PDB file to Jmol.
 
        // there are also other ways to interact with Jmol, but they require more
 
        // code. See the link to SPICE above...
 
        viewer.openStringInline(pdb);
 
        viewer.evalString("select *; spacefill off; wireframe off; backbone 0.4;  ");
 
        viewer.evalString("color chain;  ");
 
        this.viewer = viewer;
 
 
 
     }
 
     }
 
 
     public void setTitle(String label){
 
     public void setTitle(String label){
 
         frame.setTitle(label);
 
         frame.setTitle(label);
Line 91: Line 63:
 
   
 
   
 
     public JmolSimpleViewer getViewer(){
 
     public JmolSimpleViewer getViewer(){
 
 
         return jmolPanel.getViewer();
 
         return jmolPanel.getViewer();
 
     }
 
     }
Line 103: Line 74:
 
   
 
   
 
     static class JmolPanel extends JPanel {
 
     static class JmolPanel extends JPanel {
        /**
 
        *
 
        */
 
 
         private static final long serialVersionUID = -3661941083797644242L;
 
         private static final long serialVersionUID = -3661941083797644242L;
 
         JmolSimpleViewer viewer;
 
         JmolSimpleViewer viewer;
Line 133: Line 101:
 
         }
 
         }
 
     }
 
     }
   
+
  }
}
+
 
 +
For a shorter example, use Biojava. For more details, visit [http://www.biojava.org/wiki/BioJava:CookBook:PDB:Jmol Biojava Cookbook].

Latest revision as of 09:08, 15 September 2009

Jmol can be embedded in your existing Java applications. After embedding the Jmol Viewer in your application, it can be accessed programmatically. This allows developers to accomplish every thing in Jmol by running scripts.

To embed Jmol and to show the structure of a molecule using its PDB file, follow the example below.

package org.jmoltest;


import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jmol.adapter.smarter.SmarterJmolAdapter;
import org.jmol.api.JmolAdapter;
import org.jmol.api.JmolSimpleViewer;

public class MyJmolViewer {

   private String title = "Programmatic Access to Jmol";
   JmolSimpleViewer viewer;

   JmolPanel jmolPanel;
   JFrame jframe;

   public MyJmolViewer() {
       try {

           PDBFileReader pdbr = new PDBFileReader();          
           pdbr.setPath("/Path/To/PDBFiles/");

           String pdbCode = "5pti";

           Structure struc = pdbr.getStructureById(pdbCode);

           initializeJmolPanel ex = new initializeJmolPanel();
           ex.setStructure(struc);


       } catch (Exception e){
           e.printStackTrace();
       }
   }


   public initializeJmolPanel() {
       frame = new JFrame();
       frame.addWindowListener(new ApplicationCloser());
       Container contentPane = frame.getContentPane();
       jmolPanel = new JmolPanel();

       jmolPanel.setPreferredSize(new Dimension(200,200));
       contentPane.add(jmolPanel);

       frame.pack();
       frame.setVisible(true); 
   }
   public void setTitle(String label){
       frame.setTitle(label);
   }

   public JmolSimpleViewer getViewer(){
       return jmolPanel.getViewer();
   }


   static class ApplicationCloser extends WindowAdapter {
       public void windowClosing(WindowEvent e) {
           System.exit(0);
       }
   }

   static class JmolPanel extends JPanel {
       private static final long serialVersionUID = -3661941083797644242L;
       JmolSimpleViewer viewer;
       JmolAdapter adapter;
       JmolPanel() {
           adapter = new SmarterJmolAdapter();
           viewer = JmolSimpleViewer.allocateSimpleViewer(this, adapter);

       }

       public JmolSimpleViewer getViewer() {
           return viewer;
       }

       public void executeCmd(String rasmolScript){
           viewer.evalString(rasmolScript);
       }


       final Dimension currentSize = new Dimension();
       final Rectangle rectClip = new Rectangle();

       public void paint(Graphics g) {
           getSize(currentSize);
           g.getClipBounds(rectClip);
           viewer.renderScreenImage(g, currentSize, rectClip);
       }
   }
}

For a shorter example, use Biojava. For more details, visit Biojava Cookbook.