2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
26 import java.util.BitSet;
28 import jalview.datamodel.*;
31 import jalview.schemes.*;
33 import org.jmol.api.*;
34 import org.jmol.adapter.smarter.SmarterJmolAdapter;
35 import org.jmol.util.Logger;
36 import org.jmol.popup.*;
38 public class PDBViewer
39 extends JInternalFrame implements Runnable
43 * The associated sequence in an alignment
49 public PDBViewer(PDBEntry entry,
68 pdbcanvas = new PDBCanvas(seqcanvas, sequence);
70 if (pdb.getFile() != null)
74 tmpPDBFile = pdb.getFile();
75 PDBfile pdbfile = new PDBfile(tmpPDBFile,
76 jalview.io.AppletFormatAdapter.FILE);
77 pdbcanvas.setPDBFile(pdbfile);
80 catch (java.io.IOException ex)
87 Thread worker = new Thread(this);
95 EBIFetchClient ebi = new EBIFetchClient();
96 String query = "pdb:" + pdb.getId();
97 tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();
98 if (tmpPDBFile != null)
100 PDBfile pdbfile = new PDBfile(tmpPDBFile,
101 jalview.io.AppletFormatAdapter.FILE);
102 pdbcanvas.setPDBFile(pdbfile);
106 throw new Exception("Empty result for WSDbFetch Query: " + query);
111 ex.printStackTrace();
112 showErrorMessage("Failed to retrieve PDB structure.");
118 setContentPane(pdbcanvas);
119 StringBuffer title = new StringBuffer(sequence.getName() + ":" + pdb.getId());
120 if (pdb.getProperty() != null)
122 if (pdb.getProperty().get("method") != null)
124 title.append(" Method: ");
125 title.append(pdb.getProperty().get("method"));
127 if (pdb.getProperty().get("chains") != null)
129 title.append(" Chain:");
130 title.append(pdb.getProperty().get("chains"));
135 Container contentPane = getContentPane();
136 JmolPanel jmolPanel = new JmolPanel();
137 contentPane.add(jmolPanel);
139 JmolViewer viewer = jmolPanel.viewer;
142 System.out.println(entry.getFile()+" "+entry.getId());
143 viewer.openFile(entry.getFile());
146 StringBuffer string = new StringBuffer();
148 for(int i=0; i<seq.getLength(); i++)
150 Color col = seqcanvas.getSequenceRenderer().getResidueBoxColour(seq, i);
152 string.append("select " + i + "B; color [" +
155 +col.getBlue()+"]; ");
157 System.out.println(string);
159 viewer.evalString(string.toString());
162 String strError = viewer.getOpenFileError();
163 if (strError != null)
164 Logger.error(strError);
167 // Desktop.addInternalFrame(this,
168 // seq.getName() + ":1GAQ"
176 static class JmolPanel
177 extends JPanel implements JmolSelectionListener
183 adapter = new SmarterJmolAdapter(null);
184 viewer = org.jmol.viewer.Viewer.allocateViewer(this, adapter);
185 viewer.addSelectionListener(this);
187 MyStatusListener myStatusListener;
188 myStatusListener = new MyStatusListener();
189 viewer.setJmolStatusListener(myStatusListener);
191 JFrame frame = new JFrame("script window");
192 // scriptWindow = new ScriptWindow(viewer, frame);
194 myStatusListener.jmolpopup = JmolPopup.newJmolPopup(viewer);
196 public ScriptWindow scriptWindow;
197 class MyStatusListener implements JmolStatusListener
200 JmolPopup jmolpopup ;
203 public String eval(String strEval) {
204 return "# 'eval' is implemented only for the applet.";
207 public void createImage(String file, String type, int quality) {
208 System.out.println("CREATE IMAGE");
209 // ImageCreator c = new ImageCreator(viewer, status);
210 // c.createImage(file, type, quality);
213 public void setCallbackFunction(String callbackType, String callbackFunction) {
217 public void notifyFileLoaded(String fullPathName, String fileName,
218 String modelName, Object clientFile,
223 public void notifyFrameChanged(int frameNo) {
224 // Note: twos-complement. To get actual frame number, use
225 // Math.max(frameNo, -2 - frameNo)
226 // -1 means all frames are now displayed
228 boolean isAnimationRunning = (frameNo <= -2);
232 public void notifyScriptStart(String statusMessage, String additionalInfo) {
233 //System.out.println("notifyScriptStart:" + statusMessage + (additionalInfo == "" ? "" : additionalInfo));
236 public void sendConsoleEcho(String strEcho) {
237 if (scriptWindow != null)
238 scriptWindow.sendConsoleEcho(strEcho);
241 public void sendConsoleMessage(String strStatus) {
242 if (scriptWindow != null)
243 scriptWindow.sendConsoleMessage(strStatus);
246 public void notifyScriptTermination(String strStatus, int msWalltime) {
247 if (scriptWindow != null)
248 scriptWindow.notifyScriptTermination(strStatus, msWalltime);
251 public void handlePopupMenu(int x, int y) {
252 jmolpopup.show(x, y);
255 public void notifyNewPickingModeMeasurement(int iatom, String strMeasure) {
256 notifyAtomPicked(iatom, strMeasure);
259 public void notifyNewDefaultModeMeasurement(int count, String strInfo) {
260 // measurementTable.updateTables();
263 public void notifyAtomPicked(int atomIndex, String strInfo) {
265 System.out.println("ATOM PICKED "+atomIndex+" "+strInfo);
266 if (scriptWindow != null) {
267 scriptWindow.sendConsoleMessage(strInfo);
268 scriptWindow.sendConsoleMessage("\n");
272 public void notifyAtomHovered(int atomIndex, String strInfo) {
273 System.out.println("HOVER "+atomIndex+" "+strInfo);
276 public void sendSyncScript(String script, String appletName) {
279 public void showUrl(String url) {
282 public void showConsole(boolean showConsole) {
283 System.out.println("SHOW CONSOLE" +(scriptWindow==null));
284 if (scriptWindow == null)
292 public float functionXY(String functionName, int x, int y) {
293 return 0; // for user-defined isosurface functions (testing only -- bob hanson)
299 class ScriptWindowAction extends AbstractAction {
301 public ScriptWindowAction() {
305 public void actionPerformed(ActionEvent e) {
306 if (scriptWindow != null)
312 * Called when the selected atoms change
313 * @param selection bit set giving selection of atom indexes
315 public void selectionChanged(BitSet selection)
317 System.out.println("SELECTION: "+selection);
321 public JmolSimpleViewer getViewer()
326 final Dimension currentSize = new Dimension();
327 final Rectangle rectClip = new Rectangle();
329 public void paint(Graphics g)
332 getSize(currentSize);
333 g.getClipBounds(rectClip);
334 viewer.renderScreenImage(g, currentSize, rectClip);
343 EBIFetchClient ebi = new EBIFetchClient();
344 String query = "pdb:" + pdb.getId();
345 tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();
346 if (tmpPDBFile != null)
348 PDBfile pdbfile = new PDBfile(tmpPDBFile,
349 jalview.io.AppletFormatAdapter.FILE);
350 pdbcanvas.setPDBFile(pdbfile);
354 throw new Exception("Empty result for WSDbFetch Query: " + query);
359 ex.printStackTrace();
360 showErrorMessage("Failed to retrieve PDB structure.");
365 private void jbInit()
368 this.addKeyListener(new KeyAdapter()
370 public void keyPressed(KeyEvent evt)
372 pdbcanvas.keyPressed(evt);
376 this.setJMenuBar(jMenuBar1);
377 fileMenu.setText("File");
378 coloursMenu.setText("Colours");
379 saveMenu.setActionCommand("Save Image");
380 saveMenu.setText("Save As");
382 png.addActionListener(new ActionListener()
384 public void actionPerformed(ActionEvent e)
386 png_actionPerformed(e);
390 eps.addActionListener(new ActionListener()
392 public void actionPerformed(ActionEvent e)
394 eps_actionPerformed(e);
397 mapping.setText("View Mapping");
398 mapping.addActionListener(new ActionListener()
400 public void actionPerformed(ActionEvent e)
402 mapping_actionPerformed(e);
405 wire.setText("Wireframe");
406 wire.addActionListener(new ActionListener()
408 public void actionPerformed(ActionEvent e)
410 wire_actionPerformed(e);
413 depth.setSelected(true);
414 depth.setText("Depthcue");
415 depth.addActionListener(new ActionListener()
417 public void actionPerformed(ActionEvent e)
419 depth_actionPerformed(e);
422 zbuffer.setSelected(true);
423 zbuffer.setText("Z Buffering");
424 zbuffer.addActionListener(new ActionListener()
426 public void actionPerformed(ActionEvent e)
428 zbuffer_actionPerformed(e);
431 charge.setText("Charge & Cysteine");
432 charge.addActionListener(new ActionListener()
434 public void actionPerformed(ActionEvent e)
436 charge_actionPerformed(e);
439 chain.setText("By Chain");
440 chain.addActionListener(new ActionListener()
442 public void actionPerformed(ActionEvent e)
444 chain_actionPerformed(e);
447 seqButton.setSelected(true);
448 seqButton.setText("By Sequence");
449 seqButton.addActionListener(new ActionListener()
451 public void actionPerformed(ActionEvent e)
453 seqButton_actionPerformed(e);
456 allchains.setSelected(true);
457 allchains.setText("Show All Chains");
458 allchains.addItemListener(new ItemListener()
460 public void itemStateChanged(ItemEvent e)
462 allchains_itemStateChanged(e);
465 zappo.setText("Zappo");
466 zappo.addActionListener(new ActionListener()
468 public void actionPerformed(ActionEvent e)
470 zappo_actionPerformed(e);
473 taylor.setText("Taylor");
474 taylor.addActionListener(new ActionListener()
476 public void actionPerformed(ActionEvent e)
478 taylor_actionPerformed(e);
481 hydro.setText("Hydro");
482 hydro.addActionListener(new ActionListener()
484 public void actionPerformed(ActionEvent e)
486 hydro_actionPerformed(e);
489 helix.setText("Helix");
490 helix.addActionListener(new ActionListener()
492 public void actionPerformed(ActionEvent e)
494 helix_actionPerformed(e);
497 strand.setText("Strand");
498 strand.addActionListener(new ActionListener()
500 public void actionPerformed(ActionEvent e)
502 strand_actionPerformed(e);
505 turn.setText("Turn");
506 turn.addActionListener(new ActionListener()
508 public void actionPerformed(ActionEvent e)
510 turn_actionPerformed(e);
513 buried.setText("Buried");
514 buried.addActionListener(new ActionListener()
516 public void actionPerformed(ActionEvent e)
518 buried_actionPerformed(e);
521 user.setText("User Defined...");
522 user.addActionListener(new ActionListener()
524 public void actionPerformed(ActionEvent e)
526 user_actionPerformed(e);
529 viewMenu.setText("View");
530 background.setText("Background Colour...");
531 background.addActionListener(new ActionListener()
533 public void actionPerformed(ActionEvent e)
535 background_actionPerformed(e);
538 savePDB.setText("PDB File");
539 savePDB.addActionListener(new ActionListener()
541 public void actionPerformed(ActionEvent e)
543 savePDB_actionPerformed(e);
546 jMenuBar1.add(fileMenu);
547 jMenuBar1.add(coloursMenu);
548 jMenuBar1.add(viewMenu);
549 fileMenu.add(saveMenu);
550 fileMenu.add(mapping);
551 saveMenu.add(savePDB);
554 coloursMenu.add(seqButton);
555 coloursMenu.add(chain);
556 coloursMenu.add(charge);
557 coloursMenu.add(zappo);
558 coloursMenu.add(taylor);
559 coloursMenu.add(hydro);
560 coloursMenu.add(helix);
561 coloursMenu.add(strand);
562 coloursMenu.add(turn);
563 coloursMenu.add(buried);
564 coloursMenu.add(user);
565 coloursMenu.add(background);
566 ButtonGroup bg = new ButtonGroup();
579 if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
581 java.util.Enumeration userColours = jalview.gui.UserDefinedColours.
582 getUserColourSchemes().keys();
584 while (userColours.hasMoreElements())
586 final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
588 nextElement().toString());
589 radioItem.setName("USER_DEFINED");
590 radioItem.addMouseListener(new MouseAdapter()
592 public void mousePressed(MouseEvent evt)
594 if (evt.isControlDown() || SwingUtilities.isRightMouseButton(evt))
596 radioItem.removeActionListener(radioItem.getActionListeners()[0]);
598 int option = JOptionPane.showInternalConfirmDialog(jalview.gui.
600 "Remove from default list?",
601 "Remove user defined colour",
602 JOptionPane.YES_NO_OPTION);
603 if (option == JOptionPane.YES_OPTION)
605 jalview.gui.UserDefinedColours.removeColourFromDefaults(
606 radioItem.getText());
607 coloursMenu.remove(radioItem);
611 radioItem.addActionListener(new ActionListener()
613 public void actionPerformed(ActionEvent evt)
615 user_actionPerformed(evt);
622 radioItem.addActionListener(new ActionListener()
624 public void actionPerformed(ActionEvent evt)
626 user_actionPerformed(evt);
629 coloursMenu.add(radioItem);
636 viewMenu.add(zbuffer);
637 viewMenu.add(allchains);
640 JMenuBar jMenuBar1 = new JMenuBar();
641 JMenu fileMenu = new JMenu();
642 JMenu coloursMenu = new JMenu();
643 JMenu saveMenu = new JMenu();
644 JMenuItem png = new JMenuItem();
645 JMenuItem eps = new JMenuItem();
646 JMenuItem mapping = new JMenuItem();
647 JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
648 JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
649 JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
650 JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
652 JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
653 JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
654 JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
655 JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
656 JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
657 JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
658 JRadioButtonMenuItem user = new JRadioButtonMenuItem();
659 JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
660 JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
661 JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
662 JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
663 JMenu viewMenu = new JMenu();
664 JMenuItem background = new JMenuItem();
665 JMenuItem savePDB = new JMenuItem();
670 * @param e DOCUMENT ME!
672 public void eps_actionPerformed(ActionEvent e)
674 makePDBImage(jalview.util.ImageMaker.EPS);
680 * @param e DOCUMENT ME!
682 public void png_actionPerformed(ActionEvent e)
684 makePDBImage(jalview.util.ImageMaker.PNG);
687 void makePDBImage(int type)
689 int width = pdbcanvas.getWidth();
690 int height = pdbcanvas.getHeight();
692 jalview.util.ImageMaker im;
694 if (type == jalview.util.ImageMaker.PNG)
696 im = new jalview.util.ImageMaker(this,
697 jalview.util.ImageMaker.PNG,
698 "Make PNG image from view",
704 im = new jalview.util.ImageMaker(this,
705 jalview.util.ImageMaker.EPS,
706 "Make EPS file from view",
708 null, this.getTitle());
711 if (im.getGraphics() != null)
713 pdbcanvas.drawAll(im.getGraphics(), width, height);
718 public void charge_actionPerformed(ActionEvent e)
720 pdbcanvas.bysequence = false;
721 pdbcanvas.pdb.setChargeColours();
722 pdbcanvas.redrawneeded = true;
726 public void hydro_actionPerformed(ActionEvent e)
728 pdbcanvas.bysequence = false;
729 pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
730 pdbcanvas.redrawneeded = true;
734 public void chain_actionPerformed(ActionEvent e)
736 pdbcanvas.bysequence = false;
737 pdbcanvas.pdb.setChainColours();
738 pdbcanvas.redrawneeded = true;
742 public void zbuffer_actionPerformed(ActionEvent e)
744 pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
745 pdbcanvas.redrawneeded = true;
749 public void molecule_actionPerformed(ActionEvent e)
751 pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
752 pdbcanvas.redrawneeded = true;
756 public void depth_actionPerformed(ActionEvent e)
758 pdbcanvas.depthcue = !pdbcanvas.depthcue;
759 pdbcanvas.redrawneeded = true;
763 public void wire_actionPerformed(ActionEvent e)
765 pdbcanvas.wire = !pdbcanvas.wire;
766 pdbcanvas.redrawneeded = true;
770 public void seqButton_actionPerformed(ActionEvent e)
772 pdbcanvas.bysequence = true;
773 pdbcanvas.updateSeqColours();
776 public void mapping_actionPerformed(ActionEvent e)
778 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
779 // Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600);
780 cap.setText(pdbcanvas.mappingDetails.toString());
783 public void allchains_itemStateChanged(ItemEvent e)
785 pdbcanvas.setAllchainsVisible(allchains.getState());
788 void showErrorMessage(String error)
790 // JOptionPane.showInternalMessageDialog(Desktop.desktop,
791 // error, "PDB Viewer Error",
792 // JOptionPane.WARNING_MESSAGE);
795 public void zappo_actionPerformed(ActionEvent e)
797 pdbcanvas.bysequence = false;
798 pdbcanvas.pdb.setColours(new ZappoColourScheme());
799 pdbcanvas.redrawneeded = true;
803 public void taylor_actionPerformed(ActionEvent e)
805 pdbcanvas.bysequence = false;
806 pdbcanvas.pdb.setColours(new TaylorColourScheme());
807 pdbcanvas.redrawneeded = true;
811 public void helix_actionPerformed(ActionEvent e)
813 pdbcanvas.bysequence = false;
814 pdbcanvas.pdb.setColours(new HelixColourScheme());
815 pdbcanvas.redrawneeded = true;
819 public void strand_actionPerformed(ActionEvent e)
821 pdbcanvas.bysequence = false;
822 pdbcanvas.pdb.setColours(new StrandColourScheme());
823 pdbcanvas.redrawneeded = true;
827 public void turn_actionPerformed(ActionEvent e)
829 pdbcanvas.bysequence = false;
830 pdbcanvas.pdb.setColours(new TurnColourScheme());
831 pdbcanvas.redrawneeded = true;
835 public void buried_actionPerformed(ActionEvent e)
837 pdbcanvas.bysequence = false;
838 pdbcanvas.pdb.setColours(new BuriedColourScheme());
839 pdbcanvas.redrawneeded = true;
843 public void user_actionPerformed(ActionEvent e)
845 if (e.getActionCommand().equals("User Defined..."))
847 // new UserDefinedColours(pdbcanvas, null);
851 UserColourScheme udc = (UserColourScheme) UserDefinedColours.
852 getUserColourSchemes().get(e.getActionCommand());
854 pdbcanvas.pdb.setColours(udc);
855 pdbcanvas.redrawneeded = true;
860 public void background_actionPerformed(ActionEvent e)
862 java.awt.Color col = JColorChooser.showDialog(this,
863 "Select Background Colour",
864 pdbcanvas.backgroundColour);
868 pdbcanvas.backgroundColour = col;
869 pdbcanvas.redrawneeded = true;
874 public void savePDB_actionPerformed(ActionEvent e)
876 JalviewFileChooser chooser = new JalviewFileChooser(
877 jalview.bin.Cache.getProperty(
880 chooser.setFileView(new JalviewFileView());
881 chooser.setDialogTitle("Save PDB File");
882 chooser.setToolTipText("Save");
884 int value = chooser.showSaveDialog(this);
886 if (value == JalviewFileChooser.APPROVE_OPTION)
890 BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
891 File outFile = chooser.getSelectedFile();
893 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
895 while ( (data = in.readLine()) != null)
898 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
908 ex.printStackTrace();