2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
27 import java.awt.Graphics;
29 import java.util.List;
32 import javax.swing.JPanel;
33 import javax.swing.JSplitPane;
34 import javax.swing.SwingUtilities;
35 import javax.swing.event.InternalFrameAdapter;
36 import javax.swing.event.InternalFrameEvent;
38 import jalview.api.AlignmentViewPanel;
39 import jalview.bin.Cache;
40 import jalview.datamodel.PDBEntry;
41 import jalview.datamodel.SequenceI;
42 import jalview.datamodel.StructureViewerModel;
43 import jalview.datamodel.StructureViewerModel.StructureData;
44 import jalview.fts.service.alphafold.AlphafoldRestClient;
45 import jalview.gui.ImageExporter.ImageWriterI;
46 import jalview.gui.StructureViewer.ViewerType;
47 import jalview.structure.StructureCommand;
48 import jalview.structures.models.AAStructureBindingModel;
49 import jalview.util.BrowserLauncher;
50 import jalview.util.ImageMaker;
51 import jalview.util.MessageManager;
52 import jalview.util.Platform;
54 public class AppJmol extends StructureViewerBase
56 // ms to wait for Jmol to load files
57 private static final int JMOL_LOAD_TIMEOUT = 20000;
59 private static final String SPACE = " ";
61 private static final String QUOTE = "\"";
69 RenderPanel renderPanel;
78 * - add the alignment panel to the list used for colouring these
81 * - add the alignment panel to the list used for aligning these
83 * @param leaveColouringToJmol
84 * - do not update the colours from any other source. Jmol is
90 public AppJmol(StructureViewerModel viewerModel, AlignmentPanel ap,
91 String sessionFile, String viewid)
93 Map<File, StructureData> pdbData = viewerModel.getFileData();
94 PDBEntry[] pdbentrys = new PDBEntry[pdbData.size()];
95 SequenceI[][] seqs = new SequenceI[pdbData.size()][];
97 for (StructureData data : pdbData.values())
99 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
100 PDBEntry.Type.PDB, data.getFilePath());
101 pdbentrys[i] = pdbentry;
102 List<SequenceI> sequencesForPdb = data.getSeqList();
103 seqs[i] = sequencesForPdb
104 .toArray(new SequenceI[sequencesForPdb.size()]);
108 // TODO: check if protocol is needed to be set, and if chains are
110 jmb = new AppJmolBinding(this, ap.getStructureSelectionManager(),
111 pdbentrys, seqs, null);
113 jmb.setLoadingFromArchive(true);
114 addAlignmentPanel(ap);
115 if (viewerModel.isAlignWithPanel())
117 useAlignmentPanelForSuperposition(ap);
120 boolean useToColour = viewerModel.isColourWithAlignPanel();
121 boolean leaveColouringToJmol = viewerModel.isColourByViewer();
122 if (leaveColouringToJmol || !useToColour)
124 jmb.setColourBySequence(false);
125 seqColour.setSelected(false);
126 viewerColour.setSelected(true);
128 else if (useToColour)
130 useAlignmentPanelForColourbyseq(ap);
131 jmb.setColourBySequence(true);
132 seqColour.setSelected(true);
133 viewerColour.setSelected(false);
136 this.setBounds(viewerModel.getX(), viewerModel.getY(),
137 viewerModel.getWidth(), viewerModel.getHeight());
140 this.addInternalFrameListener(new InternalFrameAdapter()
143 public void internalFrameClosing(
144 InternalFrameEvent internalFrameEvent)
149 StringBuilder cmd = new StringBuilder();
150 cmd.append("load FILES ").append(QUOTE)
151 .append(Platform.escapeBackslashes(sessionFile)).append(QUOTE);
152 initJmol(cmd.toString());
156 protected void initMenus()
161 .setText(MessageManager.getString("label.colour_with_jmol"));
162 viewerColour.setToolTipText(MessageManager
163 .getString("label.let_jmol_manage_structure_colours"));
167 * display a single PDB structure in a new Jmol view
174 public AppJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
175 final AlignmentPanel ap)
177 setProgressIndicator(ap.alignFrame);
179 openNewJmol(ap, alignAddedStructures, new PDBEntry[] { pdbentry },
184 private void openNewJmol(AlignmentPanel ap, boolean alignAdded,
185 PDBEntry[] pdbentrys,
188 setProgressIndicator(ap.alignFrame);
189 jmb = new AppJmolBinding(this, ap.getStructureSelectionManager(),
190 pdbentrys, seqs, null);
191 addAlignmentPanel(ap);
192 useAlignmentPanelForColourbyseq(ap);
194 alignAddedStructures = alignAdded;
195 if (pdbentrys.length > 1)
197 useAlignmentPanelForSuperposition(ap);
200 jmb.setColourBySequence(true);
201 setSize(400, 400); // probably should be a configurable/dynamic default here
203 addingStructures = false;
204 worker = new Thread(this);
207 this.addInternalFrameListener(new InternalFrameAdapter()
210 public void internalFrameClosing(
211 InternalFrameEvent internalFrameEvent)
220 * create a new Jmol containing several structures optionally superimposed
221 * using the given alignPanel.
225 * - true to superimpose
229 public AppJmol(AlignmentPanel ap, boolean alignAdded, PDBEntry[] pe,
232 openNewJmol(ap, alignAdded, pe, seqs);
236 void initJmol(String command)
238 jmb.setFinishedInit(false);
239 renderPanel = new RenderPanel();
240 // TODO: consider waiting until the structure/view is fully loaded before
242 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
243 jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle(),
244 getBounds().width, getBounds().height);
245 if (scriptWindow == null)
247 BorderLayout bl = new BorderLayout();
250 scriptWindow = new JPanel(bl);
251 scriptWindow.setVisible(false);
254 jmb.allocateViewer(renderPanel, true, "", null, null, "", scriptWindow,
256 // jmb.newJmolPopup("Jmol");
261 jmb.executeCommand(new StructureCommand(command), false);
262 jmb.executeCommand(new StructureCommand("set hoverDelay=0.1"), false);
263 jmb.setFinishedInit(true);
272 List<String> files = jmb.fetchPdbFiles(this);
273 if (files.size() > 0)
275 showFilesInViewer(files);
285 * Either adds the given files to a structure viewer or opens a new viewer to
289 * list of absolute paths to structure files
291 void showFilesInViewer(List<String> files)
293 long lastnotify = jmb.getLoadNotifiesHandled();
294 StringBuilder fileList = new StringBuilder();
295 for (String s : files)
297 fileList.append(SPACE).append(QUOTE)
298 .append(Platform.escapeBackslashes(s)).append(QUOTE);
300 String filesString = fileList.toString();
302 if (!addingStructures)
306 initJmol("load FILES " + filesString);
307 } catch (OutOfMemoryError oomerror)
309 new OOMWarning("When trying to open the Jmol viewer!", oomerror);
310 Cache.log.debug("File locations are " + filesString);
311 } catch (Exception ex)
313 Cache.log.error("Couldn't open Jmol viewer!", ex);
314 ex.printStackTrace();
320 StringBuilder cmd = new StringBuilder();
321 cmd.append("loadingJalviewdata=true\nload APPEND ");
322 cmd.append(filesString);
323 cmd.append("\nloadingJalviewdata=null");
324 final StructureCommand command = new StructureCommand(cmd.toString());
325 lastnotify = jmb.getLoadNotifiesHandled();
329 jmb.executeCommand(command, false);
330 } catch (OutOfMemoryError oomerror)
332 new OOMWarning("When trying to add structures to the Jmol viewer!",
334 Cache.log.debug("File locations are " + filesString);
336 } catch (Exception ex)
338 Cache.log.error("Couldn't add files to Jmol viewer!", ex);
339 ex.printStackTrace();
344 // need to wait around until script has finished
345 int waitMax = JMOL_LOAD_TIMEOUT;
348 while (addingStructures ? lastnotify >= jmb.getLoadNotifiesHandled()
349 : !(jmb.isFinishedInit() && jmb.getStructureFiles() != null
350 && jmb.getStructureFiles().length == files.size()))
354 Cache.log.debug("Waiting around for jmb notify.");
355 waitTotal += waitFor;
357 // Thread.sleep() throws an exception in JS
358 Thread.sleep(waitFor);
359 } catch (Exception e)
362 if (waitTotal > waitMax)
364 System.err.println("Timed out waiting for Jmol to load files after "
366 // System.err.println("finished: " + jmb.isFinishedInit()
367 // + "; loaded: " + Arrays.toString(jmb.getPdbFile())
368 // + "; files: " + files.toString());
369 jmb.getStructureFiles();
374 // refresh the sequence colours for the new structure(s)
375 for (AlignmentViewPanel ap : _colourwith)
377 jmb.updateColours(ap);
379 // do superposition if asked to
380 if (alignAddedStructures)
382 alignAddedStructures();
384 addingStructures = false;
388 * Queues a thread to align structures with Jalview alignments
390 void alignAddedStructures()
392 javax.swing.SwingUtilities.invokeLater(new Runnable()
397 if (jmb.jmolViewer.isScriptExecuting())
399 SwingUtilities.invokeLater(this);
403 } catch (InterruptedException q)
410 alignStructsWithAllAlignPanels();
418 * Outputs the Jmol viewer image as an image file, after prompting the user to
419 * choose a file and (for EPS) choice of Text or Lineart character rendering
420 * (unless a preference for this is set)
425 public void makePDBImage(ImageMaker.TYPE type)
427 int width = getWidth();
428 int height = getHeight();
429 ImageWriterI writer = new ImageWriterI()
432 public void exportImage(Graphics g) throws Exception
434 jmb.jmolViewer.renderScreenImage(g, width, height);
437 String view = MessageManager.getString("action.view").toLowerCase();
438 ImageExporter exporter = new ImageExporter(writer,
439 getProgressIndicator(), type, getTitle());
440 exporter.doExport(null, this, width, height, view);
444 public void showHelp_actionPerformed()
448 BrowserLauncher // BH 2018
449 .openURL("http://wiki.jmol.org");//http://jmol.sourceforge.net/docs/JmolUserGuide/");
450 } catch (Exception ex)
452 System.err.println("Show Jmol help failed with: " + ex.getMessage());
457 public void showConsole(boolean showConsole)
461 if (splitPane == null)
463 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
464 splitPane.setTopComponent(renderPanel);
465 splitPane.setBottomComponent(scriptWindow);
466 this.getContentPane().add(splitPane, BorderLayout.CENTER);
467 splitPane.setDividerLocation(getHeight() - 200);
468 scriptWindow.setVisible(true);
469 scriptWindow.validate();
470 splitPane.validate();
476 if (splitPane != null)
478 splitPane.setVisible(false);
483 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
489 class RenderPanel extends JPanel
491 final Dimension currentSize = new Dimension();
494 public void paintComponent(Graphics g)
496 getSize(currentSize);
498 if (jmb != null && jmb.hasFileLoadingError())
500 g.setColor(Color.black);
501 g.fillRect(0, 0, currentSize.width, currentSize.height);
502 g.setColor(Color.white);
503 g.setFont(new Font("Verdana", Font.BOLD, 14));
504 g.drawString(MessageManager.getString("label.error_loading_file")
505 + "...", 20, currentSize.height / 2);
506 StringBuffer sb = new StringBuffer();
508 for (int e = 0; e < jmb.getPdbCount(); e++)
510 sb.append(jmb.getPdbEntry(e).getId());
511 if (e < jmb.getPdbCount() - 1)
516 if (e == jmb.getPdbCount() - 1 || sb.length() > 20)
519 g.drawString(sb.toString(), 20, currentSize.height / 2
520 - lines * g.getFontMetrics().getHeight());
524 else if (jmb == null || jmb.jmolViewer == null || !jmb.isFinishedInit())
526 g.setColor(Color.black);
527 g.fillRect(0, 0, currentSize.width, currentSize.height);
528 g.setColor(Color.white);
529 g.setFont(new Font("Verdana", Font.BOLD, 14));
530 g.drawString(MessageManager.getString("label.retrieving_pdb_data"),
531 20, currentSize.height / 2);
535 jmb.jmolViewer.renderScreenImage(g, currentSize.width,
542 public AAStructureBindingModel getBinding()
548 public ViewerType getViewerType()
550 return ViewerType.JMOL;
554 protected String getViewerName()