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.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.MouseAdapter;
26 import java.awt.event.MouseEvent;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
33 import javax.swing.JInternalFrame;
34 import javax.swing.JMenu;
35 import javax.swing.JMenuItem;
36 import javax.swing.event.InternalFrameAdapter;
37 import javax.swing.event.InternalFrameEvent;
39 import jalview.api.AlignmentViewPanel;
40 import jalview.api.FeatureRenderer;
41 import jalview.bin.Cache;
42 import jalview.datamodel.PDBEntry;
43 import jalview.datamodel.SequenceI;
44 import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
45 import jalview.gui.StructureViewer.ViewerType;
46 import jalview.io.DataSourceType;
47 import jalview.io.StructureFile;
48 import jalview.structures.models.AAStructureBindingModel;
49 import jalview.util.BrowserLauncher;
50 import jalview.util.MessageManager;
51 import jalview.util.Platform;
54 * GUI elements for handling an external chimera display
59 public class ChimeraViewFrame extends StructureViewerBase
61 private JalviewChimeraBinding jmb;
64 * Path to Chimera session file. This is set when an open Jalview/Chimera
65 * session is saved, or on restore from a Jalview project (if it holds the
66 * filename of any saved Chimera sessions).
68 private String chimeraSessionFile = null;
70 private int myWidth = 500;
72 private int myHeight = 150;
75 * Initialise menu options.
78 protected void initMenus()
82 viewerActionMenu.setText(MessageManager.getString("label.chimera"));
84 helpItem.setText(MessageManager.getString("label.chimera_help"));
85 savemenu.setVisible(false); // not yet implemented
86 viewMenu.add(fitToWindow);
88 JMenuItem writeFeatures = new JMenuItem(
89 MessageManager.getString("label.create_chimera_attributes"));
90 writeFeatures.setToolTipText(MessageManager
91 .getString("label.create_chimera_attributes_tip"));
92 writeFeatures.addActionListener(new ActionListener()
95 public void actionPerformed(ActionEvent e)
97 sendFeaturesToChimera();
100 viewerActionMenu.add(writeFeatures);
102 final JMenu fetchAttributes = new JMenu(
103 MessageManager.getString("label.fetch_chimera_attributes"));
104 fetchAttributes.setToolTipText(
105 MessageManager.getString("label.fetch_chimera_attributes_tip"));
106 fetchAttributes.addMouseListener(new MouseAdapter()
110 public void mouseEntered(MouseEvent e)
112 buildAttributesMenu(fetchAttributes);
115 viewerActionMenu.add(fetchAttributes);
119 * Query Chimera for its residue attribute names and add them as items off the
122 * @param attributesMenu
124 protected void buildAttributesMenu(JMenu attributesMenu)
126 List<String> atts = jmb.getChimeraAttributes();
127 attributesMenu.removeAll();
128 Collections.sort(atts);
129 for (String attName : atts)
131 JMenuItem menuItem = new JMenuItem(attName);
132 menuItem.addActionListener(new ActionListener()
135 public void actionPerformed(ActionEvent e)
137 getChimeraAttributes(attName);
140 attributesMenu.add(menuItem);
145 * Read residues in Chimera with the given attribute name, and set as features
146 * on the corresponding sequence positions (if any)
150 protected void getChimeraAttributes(String attName)
152 jmb.copyStructureAttributesToFeatures(attName, getAlignmentPanel());
156 * Send a command to Chimera to create residue attributes for Jalview features
158 * The syntax is: setattr r <attName> <attValue> <atomSpec>
160 * For example: setattr r jv_chain "Ferredoxin-1, Chloroplastic" #0:94.A
162 protected void sendFeaturesToChimera()
164 int count = jmb.sendFeaturesToViewer(getAlignmentPanel());
166 MessageManager.formatMessage("label.attributes_set", count));
170 * open a single PDB structure in a new Chimera view
177 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
178 String[] chains, final AlignmentPanel ap)
182 openNewChimera(ap, new PDBEntry[] { pdbentry },
188 * Create a helper to manage progress bar display
190 protected void createProgressBar()
192 if (getProgressIndicator() == null)
194 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
198 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
202 jmb = newBindingModel(ap, pdbentrys, seqs);
203 addAlignmentPanel(ap);
204 useAlignmentPanelForColourbyseq(ap);
206 if (pdbentrys.length > 1)
208 useAlignmentPanelForSuperposition(ap);
210 jmb.setColourBySequence(true);
211 setSize(myWidth, myHeight);
214 addingStructures = false;
215 worker = new Thread(this);
218 this.addInternalFrameListener(new InternalFrameAdapter()
221 public void internalFrameClosing(
222 InternalFrameEvent internalFrameEvent)
230 protected JalviewChimeraBindingModel newBindingModel(AlignmentPanel ap,
231 PDBEntry[] pdbentrys, SequenceI[][] seqs)
233 return new JalviewChimeraBindingModel(this,
234 ap.getStructureSelectionManager(), pdbentrys, seqs, null);
238 * Create a new viewer from saved session state data including Chimera session
241 * @param chimeraSessionFile
245 * @param colourByChimera
246 * @param colourBySequence
249 public ChimeraViewFrame(String chimeraSessionFile,
250 AlignmentPanel alignPanel, PDBEntry[] pdbArray,
251 SequenceI[][] seqsArray, boolean colourByChimera,
252 boolean colourBySequence, String newViewId)
255 setViewId(newViewId);
256 this.chimeraSessionFile = chimeraSessionFile;
257 openNewChimera(alignPanel, pdbArray, seqsArray);
260 jmb.setColourBySequence(false);
261 seqColour.setSelected(false);
262 viewerColour.setSelected(true);
264 else if (colourBySequence)
266 jmb.setColourBySequence(true);
267 seqColour.setSelected(true);
268 viewerColour.setSelected(false);
273 * create a new viewer containing several structures, optionally superimposed
274 * using the given alignPanel.
280 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
285 setAlignAddedStructures(alignAdded);
286 openNewChimera(ap, pe, seqs);
290 * Default constructor
292 public ChimeraViewFrame()
297 * closeViewer will decide whether or not to close this frame
298 * depending on whether user chooses to Cancel or not
300 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
304 * Launch Chimera. If we have a chimera session file name, send Chimera the
305 * command to open its saved session file.
309 jmb.setFinishedInit(false);
310 Desktop.addInternalFrame(this,
311 jmb.getViewerTitle(getViewerName(), true), getBounds().width,
314 if (!jmb.launchChimera())
316 JvOptionPane.showMessageDialog(Desktop.desktop,
317 MessageManager.getString("label.chimera_failed"),
318 MessageManager.getString("label.error_loading_file"),
319 JvOptionPane.ERROR_MESSAGE);
324 if (this.chimeraSessionFile != null)
326 boolean opened = jmb.openSession(chimeraSessionFile);
329 System.err.println("An error occurred opening Chimera session file "
330 + chimeraSessionFile);
334 jmb.startChimeraListener();
338 * Close down this instance of Jalview's Chimera viewer, giving the user the
339 * option to close the associated Chimera window (process). They may wish to
340 * keep it open until they have had an opportunity to save any work.
342 * @param closeChimera
343 * if true, close any linked Chimera process; if false, prompt first
346 public void closeViewer(boolean closeChimera)
348 if (jmb != null && jmb.isChimeraRunning())
352 String prompt = MessageManager
353 .formatMessage("label.confirm_close_chimera", new Object[]
354 { jmb.getViewerTitle(getViewerName(), false) });
355 prompt = JvSwingUtils.wrapTooltip(true, prompt);
356 int confirm = JvOptionPane.showConfirmDialog(this, prompt,
357 MessageManager.getString("label.close_viewer"),
358 JvOptionPane.YES_NO_CANCEL_OPTION);
360 * abort closure if user hits escape or Cancel
362 if (confirm == JvOptionPane.CANCEL_OPTION
363 || confirm == JvOptionPane.CLOSED_OPTION)
367 closeChimera = confirm == JvOptionPane.YES_OPTION;
369 jmb.closeViewer(closeChimera);
371 setAlignmentPanel(null);
375 // TODO: check for memory leaks where instance isn't finalised because jmb
376 // holds a reference to the window
382 * Open any newly added PDB structures in Chimera, having first fetched data
383 * from PDB (if not already saved).
389 // todo - record which pdbids were successfully imported.
390 StringBuilder errormsgs = new StringBuilder(128);
391 StringBuilder files = new StringBuilder(128);
392 List<PDBEntry> filePDB = new ArrayList<>();
393 List<Integer> filePDBpos = new ArrayList<>();
394 PDBEntry thePdbEntry = null;
395 StructureFile pdb = null;
398 String[] curfiles = jmb.getStructureFiles(); // files currently in viewer
399 // TODO: replace with reference fetching/transfer code (validate PDBentry
401 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
404 thePdbEntry = jmb.getPdbEntry(pi);
405 if (thePdbEntry.getFile() == null)
408 * Retrieve PDB data, save to file, attach to PDBEntry
410 file = fetchPdbFile(thePdbEntry);
413 errormsgs.append("'" + thePdbEntry.getId() + "' ");
419 * Got file already - ignore if already loaded in Chimera.
421 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
423 if (curfiles != null && curfiles.length > 0)
425 addingStructures = true; // already files loaded.
426 for (int c = 0; c < curfiles.length; c++)
428 if (curfiles[c].equals(file))
438 filePDB.add(thePdbEntry);
439 filePDBpos.add(Integer.valueOf(pi));
440 files.append(" \"" + Platform.escapeBackslashes(file) + "\"");
443 } catch (OutOfMemoryError oomerror)
445 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
447 } catch (Exception ex)
449 ex.printStackTrace();
451 "When retrieving pdbfiles for '" + thePdbEntry.getId() + "'");
453 if (errormsgs.length() > 0)
456 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
457 MessageManager.formatMessage(
458 "label.pdb_entries_couldnt_be_retrieved", new Object[]
459 { errormsgs.toString() }),
460 MessageManager.getString("label.couldnt_load_file"),
461 JvOptionPane.ERROR_MESSAGE);
464 if (files.length() > 0)
466 jmb.setFinishedInit(false);
467 if (!addingStructures)
472 } catch (Exception ex)
474 Cache.log.error("Couldn't open Chimera viewer!", ex);
478 for (PDBEntry pe : filePDB)
481 if (pe.getFile() != null)
485 int pos = filePDBpos.get(num).intValue();
486 long startTime = startProgressBar(getViewerName() + " "
487 + MessageManager.getString("status.opening_file_for")
490 jmb.addSequence(pos, jmb.getSequence()[pos]);
491 File fl = new File(pe.getFile());
492 DataSourceType protocol = DataSourceType.URL;
497 protocol = DataSourceType.FILE;
499 } catch (Throwable e)
503 stopProgressBar("", startTime);
505 // Explicitly map to the filename used by Chimera ;
507 pdb = jmb.getSsm().setMapping(jmb.getSequence()[pos],
508 jmb.getChains()[pos], pe.getFile(), protocol,
509 getProgressIndicator());
510 jmb.stashFoundChains(pdb, pe.getFile());
512 } catch (OutOfMemoryError oomerror)
515 "When trying to open and map structures from Chimera!",
517 } catch (Exception ex)
520 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
524 Cache.log.debug("File locations are " + files);
530 jmb.setFinishedInit(true);
531 jmb.setLoadingFromArchive(false);
534 * ensure that any newly discovered features (e.g. RESNUM)
535 * are added to any open feature settings dialog
537 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
543 // refresh the sequence colours for the new structure(s)
544 for (AlignmentViewPanel ap : _colourwith)
546 jmb.updateColours(ap);
548 // do superposition if asked to
549 if (alignAddedStructures)
551 new Thread(new Runnable()
556 alignStructsWithAllAlignPanels();
560 addingStructures = false;
567 public void eps_actionPerformed()
569 throw new Error(MessageManager
570 .getString("error.eps_generation_not_implemented"));
574 public void png_actionPerformed()
576 throw new Error(MessageManager
577 .getString("error.png_generation_not_implemented"));
581 public void showHelp_actionPerformed()
585 String url = jmb.getHelpURL();
586 BrowserLauncher.openURL(url);
587 } catch (IOException ex)
590 .println("Show Chimera help failed with: " + ex.getMessage());
595 public AAStructureBindingModel getBinding()
601 protected void fitToWindow_actionPerformed()
607 public ViewerType getViewerType()
609 return ViewerType.CHIMERA;
613 protected String getViewerName()