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.util.Locale;
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Dimension;
29 import java.awt.Graphics;
31 import java.util.List;
34 import javax.swing.JPanel;
35 import javax.swing.JSplitPane;
36 import javax.swing.SwingUtilities;
37 import javax.swing.event.InternalFrameAdapter;
38 import javax.swing.event.InternalFrameEvent;
40 import jalview.api.AlignmentViewPanel;
41 import jalview.bin.Console;
42 import jalview.datamodel.PDBEntry;
43 import jalview.datamodel.SequenceI;
44 import jalview.datamodel.StructureViewerModel;
45 import jalview.datamodel.StructureViewerModel.StructureData;
46 import jalview.fts.service.alphafold.AlphafoldRestClient;
47 import jalview.gui.ImageExporter.ImageWriterI;
48 import jalview.gui.StructureViewer.ViewerType;
49 import jalview.structure.StructureCommand;
50 import jalview.structures.models.AAStructureBindingModel;
51 import jalview.util.BrowserLauncher;
52 import jalview.util.ImageMaker;
53 import jalview.util.MessageManager;
54 import jalview.util.Platform;
56 public class AppJmol extends StructureViewerBase
58 // ms to wait for Jmol to load files
59 private static final int JMOL_LOAD_TIMEOUT = 20000;
61 private static final String SPACE = " ";
63 private static final String QUOTE = "\"";
71 RenderPanel renderPanel;
80 * - add the alignment panel to the list used for colouring these
83 * - add the alignment panel to the list used for aligning these
85 * @param leaveColouringToJmol
86 * - do not update the colours from any other source. Jmol is
92 public AppJmol(StructureViewerModel viewerModel, AlignmentPanel ap,
93 String sessionFile, String viewid)
95 Map<File, StructureData> pdbData = viewerModel.getFileData();
96 PDBEntry[] pdbentrys = new PDBEntry[pdbData.size()];
97 SequenceI[][] seqs = new SequenceI[pdbData.size()][];
99 for (StructureData data : pdbData.values())
101 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
102 PDBEntry.Type.PDB, data.getFilePath());
103 pdbentrys[i] = pdbentry;
104 List<SequenceI> sequencesForPdb = data.getSeqList();
105 seqs[i] = sequencesForPdb
106 .toArray(new SequenceI[sequencesForPdb.size()]);
110 // TODO: check if protocol is needed to be set, and if chains are
112 jmb = new AppJmolBinding(this, ap.getStructureSelectionManager(),
113 pdbentrys, seqs, null);
115 jmb.setLoadingFromArchive(true);
116 addAlignmentPanel(ap);
117 if (viewerModel.isAlignWithPanel())
119 useAlignmentPanelForSuperposition(ap);
122 boolean useToColour = viewerModel.isColourWithAlignPanel();
123 boolean leaveColouringToJmol = viewerModel.isColourByViewer();
124 if (leaveColouringToJmol || !useToColour)
126 jmb.setColourBySequence(false);
127 seqColour.setSelected(false);
128 viewerColour.setSelected(true);
130 else if (useToColour)
132 useAlignmentPanelForColourbyseq(ap);
133 jmb.setColourBySequence(true);
134 seqColour.setSelected(true);
135 viewerColour.setSelected(false);
138 this.setBounds(viewerModel.getX(), viewerModel.getY(),
139 viewerModel.getWidth(), viewerModel.getHeight());
142 this.addInternalFrameListener(new InternalFrameAdapter()
145 public void internalFrameClosing(
146 InternalFrameEvent internalFrameEvent)
151 StringBuilder cmd = new StringBuilder();
152 cmd.append("load FILES ").append(QUOTE)
153 .append(Platform.escapeBackslashes(sessionFile)).append(QUOTE);
154 initJmol(cmd.toString());
158 protected void initMenus()
163 .setText(MessageManager.getString("label.colour_with_jmol"));
164 viewerColour.setToolTipText(MessageManager
165 .getString("label.let_jmol_manage_structure_colours"));
169 * display a single PDB structure in a new Jmol view
176 public AppJmol(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
177 final AlignmentPanel ap)
179 setProgressIndicator(ap.alignFrame);
181 openNewJmol(ap, alignAddedStructures, new PDBEntry[] { pdbentry },
186 private void openNewJmol(AlignmentPanel ap, boolean alignAdded,
187 PDBEntry[] pdbentrys,
190 setProgressIndicator(ap.alignFrame);
191 jmb = new AppJmolBinding(this, ap.getStructureSelectionManager(),
192 pdbentrys, seqs, null);
193 addAlignmentPanel(ap);
194 useAlignmentPanelForColourbyseq(ap);
196 alignAddedStructures = alignAdded;
197 if (pdbentrys.length > 1)
199 useAlignmentPanelForSuperposition(ap);
202 jmb.setColourBySequence(true);
203 setSize(400, 400); // probably should be a configurable/dynamic default here
205 addingStructures = false;
206 worker = new Thread(this);
209 this.addInternalFrameListener(new InternalFrameAdapter()
212 public void internalFrameClosing(
213 InternalFrameEvent internalFrameEvent)
222 * create a new Jmol containing several structures optionally superimposed
223 * using the given alignPanel.
227 * - true to superimpose
231 public AppJmol(AlignmentPanel ap, boolean alignAdded, PDBEntry[] pe,
234 openNewJmol(ap, alignAdded, pe, seqs);
238 void initJmol(String command)
240 jmb.setFinishedInit(false);
241 renderPanel = new RenderPanel();
242 // TODO: consider waiting until the structure/view is fully loaded before
244 this.getContentPane().add(renderPanel, java.awt.BorderLayout.CENTER);
245 jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle(),
246 getBounds().width, getBounds().height);
247 if (scriptWindow == null)
249 BorderLayout bl = new BorderLayout();
252 scriptWindow = new JPanel(bl);
253 scriptWindow.setVisible(false);
256 jmb.allocateViewer(renderPanel, true, "", null, null, "", scriptWindow,
258 // jmb.newJmolPopup("Jmol");
263 jmb.executeCommand(new StructureCommand(command), false);
264 jmb.executeCommand(new StructureCommand("set hoverDelay=0.1"), false);
265 jmb.setFinishedInit(true);
274 List<String> files = jmb.fetchPdbFiles(this);
275 if (files.size() > 0)
277 showFilesInViewer(files);
287 * Either adds the given files to a structure viewer or opens a new viewer to
291 * list of absolute paths to structure files
293 void showFilesInViewer(List<String> files)
295 long lastnotify = jmb.getLoadNotifiesHandled();
296 StringBuilder fileList = new StringBuilder();
297 for (String s : files)
299 fileList.append(SPACE).append(QUOTE)
300 .append(Platform.escapeBackslashes(s)).append(QUOTE);
302 String filesString = fileList.toString();
304 if (!addingStructures)
308 initJmol("load FILES " + filesString);
309 } catch (OutOfMemoryError oomerror)
311 new OOMWarning("When trying to open the Jmol viewer!", oomerror);
312 Console.debug("File locations are " + filesString);
313 } catch (Exception ex)
315 Console.error("Couldn't open Jmol viewer!", ex);
316 ex.printStackTrace();
322 StringBuilder cmd = new StringBuilder();
323 cmd.append("loadingJalviewdata=true\nload APPEND ");
324 cmd.append(filesString);
325 cmd.append("\nloadingJalviewdata=null");
326 final StructureCommand command = new StructureCommand(cmd.toString());
327 lastnotify = jmb.getLoadNotifiesHandled();
331 jmb.executeCommand(command, false);
332 } catch (OutOfMemoryError oomerror)
334 new OOMWarning("When trying to add structures to the Jmol viewer!",
336 Console.debug("File locations are " + filesString);
338 } catch (Exception ex)
340 Console.error("Couldn't add files to Jmol viewer!", ex);
341 ex.printStackTrace();
346 // need to wait around until script has finished
347 int waitMax = JMOL_LOAD_TIMEOUT;
350 while (addingStructures ? lastnotify >= jmb.getLoadNotifiesHandled()
351 : !(jmb.isFinishedInit() && jmb.getStructureFiles() != null
352 && jmb.getStructureFiles().length == files.size()))
356 Console.debug("Waiting around for jmb notify.");
357 waitTotal += waitFor;
359 // Thread.sleep() throws an exception in JS
360 Thread.sleep(waitFor);
361 } catch (Exception e)
364 if (waitTotal > waitMax)
366 System.err.println("Timed out waiting for Jmol to load files after "
368 // System.err.println("finished: " + jmb.isFinishedInit()
369 // + "; loaded: " + Arrays.toString(jmb.getPdbFile())
370 // + "; files: " + files.toString());
371 jmb.getStructureFiles();
376 // refresh the sequence colours for the new structure(s)
377 for (AlignmentViewPanel ap : _colourwith)
379 jmb.updateColours(ap);
381 // do superposition if asked to
382 if (alignAddedStructures)
384 alignAddedStructures();
386 addingStructures = false;
390 * Queues a thread to align structures with Jalview alignments
392 void alignAddedStructures()
394 javax.swing.SwingUtilities.invokeLater(new Runnable()
399 if (jmb.jmolViewer.isScriptExecuting())
401 SwingUtilities.invokeLater(this);
405 } catch (InterruptedException q)
412 alignStructsWithAllAlignPanels();
420 * Outputs the Jmol viewer image as an image file, after prompting the user to
421 * choose a file and (for EPS) choice of Text or Lineart character rendering
422 * (unless a preference for this is set)
427 public void makePDBImage(ImageMaker.TYPE type)
429 int width = getWidth();
430 int height = getHeight();
431 ImageWriterI writer = new ImageWriterI()
434 public void exportImage(Graphics g) throws Exception
436 jmb.jmolViewer.renderScreenImage(g, width, height);
439 String view = MessageManager.getString("action.view").toLowerCase(Locale.ROOT);
440 ImageExporter exporter = new ImageExporter(writer,
441 getProgressIndicator(), type, getTitle());
442 exporter.doExport(null, this, width, height, view);
446 public void showHelp_actionPerformed()
450 BrowserLauncher // BH 2018
451 .openURL("http://wiki.jmol.org");//http://jmol.sourceforge.net/docs/JmolUserGuide/");
452 } catch (Exception ex)
454 System.err.println("Show Jmol help failed with: " + ex.getMessage());
459 public void showConsole(boolean showConsole)
463 if (splitPane == null)
465 splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
466 splitPane.setTopComponent(renderPanel);
467 splitPane.setBottomComponent(scriptWindow);
468 this.getContentPane().add(splitPane, BorderLayout.CENTER);
469 splitPane.setDividerLocation(getHeight() - 200);
470 scriptWindow.setVisible(true);
471 scriptWindow.validate();
472 splitPane.validate();
478 if (splitPane != null)
480 splitPane.setVisible(false);
485 this.getContentPane().add(renderPanel, BorderLayout.CENTER);
491 class RenderPanel extends JPanel
493 final Dimension currentSize = new Dimension();
496 public void paintComponent(Graphics g)
498 getSize(currentSize);
500 if (jmb != null && jmb.hasFileLoadingError())
502 g.setColor(Color.black);
503 g.fillRect(0, 0, currentSize.width, currentSize.height);
504 g.setColor(Color.white);
505 g.setFont(new Font("Verdana", Font.BOLD, 14));
506 g.drawString(MessageManager.getString("label.error_loading_file")
507 + "...", 20, currentSize.height / 2);
508 StringBuffer sb = new StringBuffer();
510 for (int e = 0; e < jmb.getPdbCount(); e++)
512 sb.append(jmb.getPdbEntry(e).getId());
513 if (e < jmb.getPdbCount() - 1)
518 if (e == jmb.getPdbCount() - 1 || sb.length() > 20)
521 g.drawString(sb.toString(), 20, currentSize.height / 2
522 - lines * g.getFontMetrics().getHeight());
526 else if (jmb == null || jmb.jmolViewer == null || !jmb.isFinishedInit())
528 g.setColor(Color.black);
529 g.fillRect(0, 0, currentSize.width, currentSize.height);
530 g.setColor(Color.white);
531 g.setFont(new Font("Verdana", Font.BOLD, 14));
532 g.drawString(MessageManager.getString("label.retrieving_pdb_data"),
533 20, currentSize.height / 2);
537 jmb.jmolViewer.renderScreenImage(g, currentSize.width,
544 public AAStructureBindingModel getBinding()
550 public ViewerType getViewerType()
552 return ViewerType.JMOL;
556 protected String getViewerName()