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)
287 scriptWindow.setVisible(showConsole);
290 public float functionXY(String functionName, int x, int y) {
291 return 0; // for user-defined isosurface functions (testing only -- bob hanson)
297 class ScriptWindowAction extends AbstractAction {
299 public ScriptWindowAction() {
303 public void actionPerformed(ActionEvent e) {
304 if (scriptWindow != null)
310 * Called when the selected atoms change
311 * @param selection bit set giving selection of atom indexes
313 public void selectionChanged(BitSet selection)
315 System.out.println("SELECTION: "+selection);
319 public JmolSimpleViewer getViewer()
324 final Dimension currentSize = new Dimension();
325 final Rectangle rectClip = new Rectangle();
327 public void paint(Graphics g)
330 getSize(currentSize);
331 g.getClipBounds(rectClip);
332 viewer.renderScreenImage(g, currentSize, rectClip);
341 EBIFetchClient ebi = new EBIFetchClient();
342 String query = "pdb:" + pdb.getId();
343 tmpPDBFile = ebi.fetchDataAsFile(query, "default", "raw").getAbsolutePath();
344 if (tmpPDBFile != null)
346 PDBfile pdbfile = new PDBfile(tmpPDBFile,
347 jalview.io.AppletFormatAdapter.FILE);
348 pdbcanvas.setPDBFile(pdbfile);
352 throw new Exception("Empty result for WSDbFetch Query: " + query);
357 ex.printStackTrace();
358 showErrorMessage("Failed to retrieve PDB structure.");
363 private void jbInit()
366 this.addKeyListener(new KeyAdapter()
368 public void keyPressed(KeyEvent evt)
370 pdbcanvas.keyPressed(evt);
374 this.setJMenuBar(jMenuBar1);
375 fileMenu.setText("File");
376 coloursMenu.setText("Colours");
377 saveMenu.setActionCommand("Save Image");
378 saveMenu.setText("Save As");
380 png.addActionListener(new ActionListener()
382 public void actionPerformed(ActionEvent e)
384 png_actionPerformed(e);
388 eps.addActionListener(new ActionListener()
390 public void actionPerformed(ActionEvent e)
392 eps_actionPerformed(e);
395 mapping.setText("View Mapping");
396 mapping.addActionListener(new ActionListener()
398 public void actionPerformed(ActionEvent e)
400 mapping_actionPerformed(e);
403 wire.setText("Wireframe");
404 wire.addActionListener(new ActionListener()
406 public void actionPerformed(ActionEvent e)
408 wire_actionPerformed(e);
411 depth.setSelected(true);
412 depth.setText("Depthcue");
413 depth.addActionListener(new ActionListener()
415 public void actionPerformed(ActionEvent e)
417 depth_actionPerformed(e);
420 zbuffer.setSelected(true);
421 zbuffer.setText("Z Buffering");
422 zbuffer.addActionListener(new ActionListener()
424 public void actionPerformed(ActionEvent e)
426 zbuffer_actionPerformed(e);
429 charge.setText("Charge & Cysteine");
430 charge.addActionListener(new ActionListener()
432 public void actionPerformed(ActionEvent e)
434 charge_actionPerformed(e);
437 chain.setText("By Chain");
438 chain.addActionListener(new ActionListener()
440 public void actionPerformed(ActionEvent e)
442 chain_actionPerformed(e);
445 seqButton.setSelected(true);
446 seqButton.setText("By Sequence");
447 seqButton.addActionListener(new ActionListener()
449 public void actionPerformed(ActionEvent e)
451 seqButton_actionPerformed(e);
454 allchains.setSelected(true);
455 allchains.setText("Show All Chains");
456 allchains.addItemListener(new ItemListener()
458 public void itemStateChanged(ItemEvent e)
460 allchains_itemStateChanged(e);
463 zappo.setText("Zappo");
464 zappo.addActionListener(new ActionListener()
466 public void actionPerformed(ActionEvent e)
468 zappo_actionPerformed(e);
471 taylor.setText("Taylor");
472 taylor.addActionListener(new ActionListener()
474 public void actionPerformed(ActionEvent e)
476 taylor_actionPerformed(e);
479 hydro.setText("Hydro");
480 hydro.addActionListener(new ActionListener()
482 public void actionPerformed(ActionEvent e)
484 hydro_actionPerformed(e);
487 helix.setText("Helix");
488 helix.addActionListener(new ActionListener()
490 public void actionPerformed(ActionEvent e)
492 helix_actionPerformed(e);
495 strand.setText("Strand");
496 strand.addActionListener(new ActionListener()
498 public void actionPerformed(ActionEvent e)
500 strand_actionPerformed(e);
503 turn.setText("Turn");
504 turn.addActionListener(new ActionListener()
506 public void actionPerformed(ActionEvent e)
508 turn_actionPerformed(e);
511 buried.setText("Buried");
512 buried.addActionListener(new ActionListener()
514 public void actionPerformed(ActionEvent e)
516 buried_actionPerformed(e);
519 user.setText("User Defined...");
520 user.addActionListener(new ActionListener()
522 public void actionPerformed(ActionEvent e)
524 user_actionPerformed(e);
527 viewMenu.setText("View");
528 background.setText("Background Colour...");
529 background.addActionListener(new ActionListener()
531 public void actionPerformed(ActionEvent e)
533 background_actionPerformed(e);
536 savePDB.setText("PDB File");
537 savePDB.addActionListener(new ActionListener()
539 public void actionPerformed(ActionEvent e)
541 savePDB_actionPerformed(e);
544 jMenuBar1.add(fileMenu);
545 jMenuBar1.add(coloursMenu);
546 jMenuBar1.add(viewMenu);
547 fileMenu.add(saveMenu);
548 fileMenu.add(mapping);
549 saveMenu.add(savePDB);
552 coloursMenu.add(seqButton);
553 coloursMenu.add(chain);
554 coloursMenu.add(charge);
555 coloursMenu.add(zappo);
556 coloursMenu.add(taylor);
557 coloursMenu.add(hydro);
558 coloursMenu.add(helix);
559 coloursMenu.add(strand);
560 coloursMenu.add(turn);
561 coloursMenu.add(buried);
562 coloursMenu.add(user);
563 coloursMenu.add(background);
564 ButtonGroup bg = new ButtonGroup();
577 if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
579 java.util.Enumeration userColours = jalview.gui.UserDefinedColours.
580 getUserColourSchemes().keys();
582 while (userColours.hasMoreElements())
584 final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
586 nextElement().toString());
587 radioItem.setName("USER_DEFINED");
588 radioItem.addMouseListener(new MouseAdapter()
590 public void mousePressed(MouseEvent evt)
592 if (evt.isControlDown() || SwingUtilities.isRightMouseButton(evt))
594 radioItem.removeActionListener(radioItem.getActionListeners()[0]);
596 int option = JOptionPane.showInternalConfirmDialog(jalview.gui.
598 "Remove from default list?",
599 "Remove user defined colour",
600 JOptionPane.YES_NO_OPTION);
601 if (option == JOptionPane.YES_OPTION)
603 jalview.gui.UserDefinedColours.removeColourFromDefaults(
604 radioItem.getText());
605 coloursMenu.remove(radioItem);
609 radioItem.addActionListener(new ActionListener()
611 public void actionPerformed(ActionEvent evt)
613 user_actionPerformed(evt);
620 radioItem.addActionListener(new ActionListener()
622 public void actionPerformed(ActionEvent evt)
624 user_actionPerformed(evt);
627 coloursMenu.add(radioItem);
634 viewMenu.add(zbuffer);
635 viewMenu.add(allchains);
638 JMenuBar jMenuBar1 = new JMenuBar();
639 JMenu fileMenu = new JMenu();
640 JMenu coloursMenu = new JMenu();
641 JMenu saveMenu = new JMenu();
642 JMenuItem png = new JMenuItem();
643 JMenuItem eps = new JMenuItem();
644 JMenuItem mapping = new JMenuItem();
645 JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
646 JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
647 JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
648 JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
650 JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
651 JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
652 JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
653 JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
654 JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
655 JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
656 JRadioButtonMenuItem user = new JRadioButtonMenuItem();
657 JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
658 JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
659 JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
660 JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
661 JMenu viewMenu = new JMenu();
662 JMenuItem background = new JMenuItem();
663 JMenuItem savePDB = new JMenuItem();
668 * @param e DOCUMENT ME!
670 public void eps_actionPerformed(ActionEvent e)
672 makePDBImage(jalview.util.ImageMaker.EPS);
678 * @param e DOCUMENT ME!
680 public void png_actionPerformed(ActionEvent e)
682 makePDBImage(jalview.util.ImageMaker.PNG);
685 void makePDBImage(int type)
687 int width = pdbcanvas.getWidth();
688 int height = pdbcanvas.getHeight();
690 jalview.util.ImageMaker im;
692 if (type == jalview.util.ImageMaker.PNG)
694 im = new jalview.util.ImageMaker(this,
695 jalview.util.ImageMaker.PNG,
696 "Make PNG image from view",
702 im = new jalview.util.ImageMaker(this,
703 jalview.util.ImageMaker.EPS,
704 "Make EPS file from view",
706 null, this.getTitle());
709 if (im.getGraphics() != null)
711 pdbcanvas.drawAll(im.getGraphics(), width, height);
716 public void charge_actionPerformed(ActionEvent e)
718 pdbcanvas.bysequence = false;
719 pdbcanvas.pdb.setChargeColours();
720 pdbcanvas.redrawneeded = true;
724 public void hydro_actionPerformed(ActionEvent e)
726 pdbcanvas.bysequence = false;
727 pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
728 pdbcanvas.redrawneeded = true;
732 public void chain_actionPerformed(ActionEvent e)
734 pdbcanvas.bysequence = false;
735 pdbcanvas.pdb.setChainColours();
736 pdbcanvas.redrawneeded = true;
740 public void zbuffer_actionPerformed(ActionEvent e)
742 pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
743 pdbcanvas.redrawneeded = true;
747 public void molecule_actionPerformed(ActionEvent e)
749 pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
750 pdbcanvas.redrawneeded = true;
754 public void depth_actionPerformed(ActionEvent e)
756 pdbcanvas.depthcue = !pdbcanvas.depthcue;
757 pdbcanvas.redrawneeded = true;
761 public void wire_actionPerformed(ActionEvent e)
763 pdbcanvas.wire = !pdbcanvas.wire;
764 pdbcanvas.redrawneeded = true;
768 public void seqButton_actionPerformed(ActionEvent e)
770 pdbcanvas.bysequence = true;
771 pdbcanvas.updateSeqColours();
774 public void mapping_actionPerformed(ActionEvent e)
776 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
777 // Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600);
778 cap.setText(pdbcanvas.mappingDetails.toString());
781 public void allchains_itemStateChanged(ItemEvent e)
783 pdbcanvas.setAllchainsVisible(allchains.getState());
786 void showErrorMessage(String error)
788 // JOptionPane.showInternalMessageDialog(Desktop.desktop,
789 // error, "PDB Viewer Error",
790 // JOptionPane.WARNING_MESSAGE);
793 public void zappo_actionPerformed(ActionEvent e)
795 pdbcanvas.bysequence = false;
796 pdbcanvas.pdb.setColours(new ZappoColourScheme());
797 pdbcanvas.redrawneeded = true;
801 public void taylor_actionPerformed(ActionEvent e)
803 pdbcanvas.bysequence = false;
804 pdbcanvas.pdb.setColours(new TaylorColourScheme());
805 pdbcanvas.redrawneeded = true;
809 public void helix_actionPerformed(ActionEvent e)
811 pdbcanvas.bysequence = false;
812 pdbcanvas.pdb.setColours(new HelixColourScheme());
813 pdbcanvas.redrawneeded = true;
817 public void strand_actionPerformed(ActionEvent e)
819 pdbcanvas.bysequence = false;
820 pdbcanvas.pdb.setColours(new StrandColourScheme());
821 pdbcanvas.redrawneeded = true;
825 public void turn_actionPerformed(ActionEvent e)
827 pdbcanvas.bysequence = false;
828 pdbcanvas.pdb.setColours(new TurnColourScheme());
829 pdbcanvas.redrawneeded = true;
833 public void buried_actionPerformed(ActionEvent e)
835 pdbcanvas.bysequence = false;
836 pdbcanvas.pdb.setColours(new BuriedColourScheme());
837 pdbcanvas.redrawneeded = true;
841 public void user_actionPerformed(ActionEvent e)
843 if (e.getActionCommand().equals("User Defined..."))
845 // new UserDefinedColours(pdbcanvas, null);
849 UserColourScheme udc = (UserColourScheme) UserDefinedColours.
850 getUserColourSchemes().get(e.getActionCommand());
852 pdbcanvas.pdb.setColours(udc);
853 pdbcanvas.redrawneeded = true;
858 public void background_actionPerformed(ActionEvent e)
860 java.awt.Color col = JColorChooser.showDialog(this,
861 "Select Background Colour",
862 pdbcanvas.backgroundColour);
866 pdbcanvas.backgroundColour = col;
867 pdbcanvas.redrawneeded = true;
872 public void savePDB_actionPerformed(ActionEvent e)
874 JalviewFileChooser chooser = new JalviewFileChooser(
875 jalview.bin.Cache.getProperty(
878 chooser.setFileView(new JalviewFileView());
879 chooser.setDialogTitle("Save PDB File");
880 chooser.setToolTipText("Save");
882 int value = chooser.showSaveDialog(this);
884 if (value == JalviewFileChooser.APPROVE_OPTION)
888 BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
889 File outFile = chooser.getSelectedFile();
891 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
893 while ( (data = in.readLine()) != null)
896 ! (data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1)
906 ex.printStackTrace();