001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on 24.05.2004
021 * @author Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.gui;
026
027
028import org.biojava.nbio.structure.Structure;
029import org.biojava.nbio.structure.align.gui.jmol.JmolPanel;
030import org.biojava.nbio.structure.gui.util.MenuCreator;
031import org.biojava.nbio.structure.io.PDBFileReader;
032
033import javax.swing.*;
034import java.awt.*;
035import java.awt.event.*;
036
037
038/** A class that provides a simple GUI for Jmol
039 *
040 * @author Andreas Prlic
041 * @since 1.6
042 *
043 *
044 *
045 */
046public class BiojavaJmol  {
047
048        public static final String viewer       = "org.jmol.api.JmolSimpleViewer";
049        public static final String adapter      = "org.jmol.api.JmolAdapter";
050        public static final String smartAdapter = "org.jmol.adapter.smarter.SmarterJmolAdapter";
051
052        Structure structure;
053
054        JmolPanel jmolPanel;
055        JFrame frame ;
056
057
058        public static void main(String[] args){
059                try {
060
061                        PDBFileReader pdbr = new PDBFileReader();
062
063                        pdbr.setPath("/tmp/");
064
065                        String pdbCode = "5pti";
066
067                        Structure struc = pdbr.getStructureById(pdbCode);
068
069                        BiojavaJmol jmolPanel = new BiojavaJmol();
070
071                        jmolPanel.setStructure(struc);
072
073                        // send some RASMOL style commands to Jmol
074                        jmolPanel.evalString("select * ; color chain;");
075                        jmolPanel.evalString("select *; spacefill off; wireframe off; backbone 0.4;  ");
076                        jmolPanel.evalString("save STATE state_1");
077                } catch (Exception e){
078                        e.printStackTrace();
079                }
080        }
081
082
083
084
085        public BiojavaJmol() {
086
087                frame = new JFrame();
088
089                JMenuBar menu = MenuCreator.initMenu();
090
091                frame.setJMenuBar(menu);
092
093                frame.addWindowListener( new WindowAdapter() {
094                        @Override
095                        public void windowClosing(WindowEvent e) {
096                                frame.dispose();
097                                //System.exit(0);
098                        }
099                });
100
101                Container contentPane = frame.getContentPane();
102
103                Box vBox = Box.createVerticalBox();
104
105                jmolPanel = new JmolPanel();
106
107                jmolPanel.setPreferredSize(new Dimension(500,500));
108                vBox.add(jmolPanel);
109
110
111                JTextField field = new JTextField();
112
113                field.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
114                field.setText("enter RASMOL like command...");
115                org.biojava.nbio.structure.align.gui.jmol.RasmolCommandListener listener = new org.biojava.nbio.structure.align.gui.jmol.RasmolCommandListener(jmolPanel,field) ;
116
117                field.addActionListener(listener);
118                field.addMouseListener(listener);
119                field.addKeyListener(listener);
120                vBox.add(field);
121
122
123                /// COMBO BOXES
124                Box hBox1 = Box.createHorizontalBox();
125                hBox1.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
126
127
128                String[] styles = new String[] { "Cartoon", "Backbone", "CPK", "Ball and Stick", "Ligands","Ligands and Pocket"};
129                JComboBox style = new JComboBox(styles);
130
131                hBox1.add(new JLabel("Style"));
132                hBox1.add(style);
133                vBox.add(hBox1);
134
135
136                style.addActionListener(jmolPanel);
137
138                String[] colorModes = new String[] { "Secondary Structure", "By Chain", "Rainbow", "By Element", "By Amino Acid", "Hydrophobicity" };
139                JComboBox colors = new JComboBox(colorModes);
140                colors.addActionListener(jmolPanel);
141                hBox1.add(Box.createGlue());
142                hBox1.add(new JLabel("Color"));
143                hBox1.add(colors);
144
145                // Check boxes
146                Box hBox2 = Box.createHorizontalBox();
147                hBox2.setMaximumSize(new Dimension(Short.MAX_VALUE,30));
148
149
150                JButton resetDisplay = new JButton("Reset Display");
151
152                resetDisplay.addActionListener(new ActionListener() {
153
154                        @Override
155                        public void actionPerformed(ActionEvent e) {
156                                System.out.println("reset!!");
157                                jmolPanel.executeCmd("restore STATE state_1");
158
159                        }
160                });
161
162                hBox2.add(resetDisplay); hBox2.add(Box.createGlue());
163
164                JCheckBox toggleSelection = new JCheckBox("Show Selection");
165                toggleSelection.addItemListener(
166                            new ItemListener() {
167
168                                        @Override
169                                        public void itemStateChanged(ItemEvent e) {
170                                                  boolean showSelection = (e.getStateChange() == ItemEvent.SELECTED);
171
172                                                  if (showSelection){
173                                                          jmolPanel.executeCmd("set display selected");
174                                                  } else {
175                                                          jmolPanel.executeCmd("set display off");
176                                                  }
177
178                                        }
179                                }
180                        );
181
182
183
184                hBox2.add(toggleSelection);
185
186                hBox2.add(Box.createGlue());
187                vBox.add(hBox2);
188
189
190                // finish up
191                contentPane.add(vBox);
192                frame.pack();
193                frame.setVisible(true);
194
195
196        }
197
198
199
200        /** returns true if Jmol can be found in the classpath, otherwise false.
201         *
202         * @return true/false depending if Jmol can be found
203         */
204        public static boolean jmolInClassPath(){
205                try {
206                        Class.forName(viewer);
207                } catch (ClassNotFoundException e){
208                        e.printStackTrace();
209                        return false;
210                }
211                return true;
212        }
213
214        public void evalString(String rasmolScript){
215                if ( jmolPanel == null ){
216                        System.err.println("please install Jmol first");
217                        return;
218                }
219                jmolPanel.evalString(rasmolScript);
220        }
221
222        public void setStructure(Structure s) {
223
224                if ( jmolPanel == null ){
225                        System.err.println("please install Jmol first");
226                        return;
227                }
228
229                setTitle(s.getPDBCode());
230
231                // actually this is very simple
232                // just convert the structure to a PDB file
233
234                String pdb = s.toPDB();
235                //System.out.println(s.isNmr());
236
237                //System.out.println(pdb);
238                // Jmol could also read the file directly from your file system
239                //viewer.openFile("/Path/To/PDB/1tim.pdb");
240
241                //System.out.println(pdb);
242                jmolPanel.openStringInline(pdb);
243
244                // send the PDB file to Jmol.
245                // there are also other ways to interact with Jmol, e.g make it directly
246                // access the biojava structure object, but they require more
247                // code. See the SPICE code repository for how to do this.
248
249
250
251
252        }
253
254        public void setTitle(String label){
255                frame.setTitle(label);
256                frame.repaint();
257        }
258
259        public JFrame getFrame(){
260                return frame;
261        }
262
263
264}