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.
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.MouseAdapter;
27 import java.awt.event.MouseEvent;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
34 import javax.swing.JInternalFrame;
35 import javax.swing.JMenu;
36 import javax.swing.JMenuItem;
37 import javax.swing.event.InternalFrameAdapter;
38 import javax.swing.event.InternalFrameEvent;
40 import jalview.api.AlignmentViewPanel;
41 import jalview.api.FeatureRenderer;
42 import jalview.bin.Console;
43 import jalview.datamodel.PDBEntry;
44 import jalview.datamodel.SequenceI;
45 import jalview.datamodel.StructureViewerModel;
46 import jalview.datamodel.StructureViewerModel.StructureData;
47 import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
48 import jalview.gui.StructureViewer.ViewerType;
49 import jalview.io.DataSourceType;
50 import jalview.io.StructureFile;
51 import jalview.structures.models.AAStructureBindingModel;
52 import jalview.util.ImageMaker.TYPE;
53 import jalview.util.MessageManager;
54 import jalview.util.Platform;
56 * GUI elements for handling an external chimera display
61 public class ChimeraViewFrame extends StructureViewerBase
63 private JalviewChimeraBinding jmb;
67 * Path to Chimera session file. This is set when an open Jalview/Chimera
68 * session is saved, or on restore from a Jalview project (if it holds the
69 * filename of any saved Chimera sessions).
71 private String chimeraSessionFile = null;
74 private int myWidth = 500;
76 private int myHeight = 150;
78 private JMenuItem writeFeatures = null;
80 private JMenu fetchAttributes = null;
83 * Initialise menu options.
86 protected void initMenus()
90 savemenu.setVisible(false); // not yet implemented
91 viewMenu.add(fitToWindow);
93 writeFeatures = new JMenuItem(
94 MessageManager.getString("label.create_viewer_attributes"));
95 writeFeatures.setToolTipText(
96 MessageManager.getString("label.create_viewer_attributes_tip"));
97 writeFeatures.addActionListener(new ActionListener()
100 public void actionPerformed(ActionEvent e)
102 sendFeaturesToChimera();
105 viewerActionMenu.add(writeFeatures);
107 fetchAttributes = new JMenu(MessageManager.formatMessage(
108 "label.fetch_viewer_attributes", getViewerName()));
109 fetchAttributes.setToolTipText(MessageManager.formatMessage(
110 "label.fetch_viewer_attributes_tip", getViewerName()));
111 fetchAttributes.addMouseListener(new MouseAdapter()
115 public void mouseEntered(MouseEvent e)
117 buildAttributesMenu(fetchAttributes);
120 viewerActionMenu.add(fetchAttributes);
124 protected void buildActionMenu()
126 super.buildActionMenu();
127 // add these back in after menu is refreshed
128 viewerActionMenu.add(writeFeatures);
129 viewerActionMenu.add(fetchAttributes);
134 * Query the structure viewer for its residue attribute names and add them as
135 * items off the attributes menu
137 * @param attributesMenu
139 protected void buildAttributesMenu(JMenu attributesMenu)
141 List<String> atts = jmb.getChimeraAttributes();
142 attributesMenu.removeAll();
143 Collections.sort(atts);
144 for (String attName : atts)
146 JMenuItem menuItem = new JMenuItem(attName);
147 menuItem.addActionListener(new ActionListener()
150 public void actionPerformed(ActionEvent e)
152 if (getBinding().copyStructureAttributesToFeatures(attName,
153 getAlignmentPanel()) > 0)
155 getAlignmentPanel().getFeatureRenderer().featuresAdded();
159 attributesMenu.add(menuItem);
164 * Sends command(s) to the structure viewer to create residue attributes for
165 * visible Jalview features
167 protected void sendFeaturesToChimera()
170 int count = jmb.sendFeaturesToViewer(getAlignmentPanel());
171 statusBar.setText(MessageManager.formatMessage("label.attributes_set",
172 count, getViewerName()));
176 * open a single PDB structure in a new Chimera view
183 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
184 String[] chains, final AlignmentPanel ap)
188 openNewChimera(ap, new PDBEntry[] { pdbentry },
194 * Create a helper to manage progress bar display
196 protected void createProgressBar()
198 if (getProgressIndicator() == null)
200 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
204 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
208 jmb = newBindingModel(ap, pdbentrys, seqs);
209 addAlignmentPanel(ap);
210 useAlignmentPanelForColourbyseq(ap);
212 if (pdbentrys.length > 1)
214 useAlignmentPanelForSuperposition(ap);
216 jmb.setColourBySequence(true);
217 setSize(myWidth, myHeight);
220 addingStructures = false;
221 worker = new Thread(this);
224 this.addInternalFrameListener(new InternalFrameAdapter()
227 public void internalFrameClosing(
228 InternalFrameEvent internalFrameEvent)
236 protected JalviewChimeraBindingModel newBindingModel(AlignmentPanel ap,
237 PDBEntry[] pdbentrys, SequenceI[][] seqs)
239 return new JalviewChimeraBindingModel(this,
240 ap.getStructureSelectionManager(), pdbentrys, seqs, null);
243 * Create a new viewer from saved session state data including Chimera session
246 * @param chimeraSessionFile
250 * @param colourByChimera
251 * @param colourBySequence
254 public ChimeraViewFrame(StructureViewerModel viewerData,
255 AlignmentPanel alignPanel, String sessionFile, String vid)
259 this.chimeraSessionFile = sessionFile;
260 Map<File, StructureData> pdbData = viewerData.getFileData();
261 PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
262 SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
264 for (StructureData data : pdbData.values())
266 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
267 PDBEntry.Type.PDB, data.getFilePath());
268 pdbArray[i] = pdbentry;
269 List<SequenceI> sequencesForPdb = data.getSeqList();
270 seqsArray[i] = sequencesForPdb
271 .toArray(new SequenceI[sequencesForPdb.size()]);
274 openNewChimera(alignPanel, pdbArray, seqsArray);
275 if (viewerData.isColourByViewer())
277 jmb.setColourBySequence(false);
278 seqColour.setSelected(false);
279 viewerColour.setSelected(true);
281 else if (viewerData.isColourWithAlignPanel())
283 jmb.setColourBySequence(true);
284 seqColour.setSelected(true);
285 viewerColour.setSelected(false);
290 * create a new viewer containing several structures, optionally superimposed
291 * using the given alignPanel.
297 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
298 SequenceI[][] seqs, AlignmentPanel ap)
301 setAlignAddedStructures(alignAdded);
302 openNewChimera(ap, pe, seqs);
306 * Default constructor
308 public ChimeraViewFrame()
313 * closeViewer will decide whether or not to close this frame
314 * depending on whether user chooses to Cancel or not
316 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
320 * Launch Chimera. If we have a chimera session file name, send Chimera the
321 * command to open its saved session file.
325 jmb.setFinishedInit(false);
326 Desktop.addInternalFrame(this,
327 jmb.getViewerTitle(getViewerName(), true), getBounds().width,
330 if (!jmb.launchChimera())
332 JvOptionPane.showMessageDialog(Desktop.getDesktopPane(),
333 MessageManager.formatMessage("label.open_viewer_failed",
335 MessageManager.getString("label.error_loading_file"),
336 JvOptionPane.ERROR_MESSAGE);
337 jmb.closeViewer(true);
342 if (this.chimeraSessionFile != null)
344 boolean opened = jmb.openSession(chimeraSessionFile);
347 System.err.println("An error occurred opening Chimera session file "
348 + chimeraSessionFile);
352 jmb.startChimeraListener();
356 * Open any newly added PDB structures in Chimera, having first fetched data
357 * from PDB (if not already saved).
363 // todo - record which pdbids were successfully imported.
364 StringBuilder errormsgs = new StringBuilder(128);
365 StringBuilder files = new StringBuilder(128);
366 List<PDBEntry> filePDB = new ArrayList<>();
367 List<Integer> filePDBpos = new ArrayList<>();
368 PDBEntry thePdbEntry = null;
369 StructureFile pdb = null;
372 String[] curfiles = jmb.getStructureFiles(); // files currently in viewer
373 // TODO: replace with reference fetching/transfer code (validate PDBentry
375 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
378 thePdbEntry = jmb.getPdbEntry(pi);
379 if (thePdbEntry.getFile() == null)
382 * Retrieve PDB data, save to file, attach to PDBEntry
384 file = fetchPdbFile(thePdbEntry);
387 errormsgs.append("'" + thePdbEntry.getId() + "' ");
393 * Got file already - ignore if already loaded in Chimera.
395 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
397 if (curfiles != null && curfiles.length > 0)
399 addingStructures = true; // already files loaded.
400 for (int c = 0; c < curfiles.length; c++)
402 if (curfiles[c].equals(file))
412 filePDB.add(thePdbEntry);
413 filePDBpos.add(Integer.valueOf(pi));
414 files.append(" \"" + Platform.escapeBackslashes(file) + "\"");
417 } catch (OutOfMemoryError oomerror)
419 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
421 } catch (Exception ex)
423 ex.printStackTrace();
425 "When retrieving pdbfiles for '" + thePdbEntry.getId() + "'");
427 if (errormsgs.length() > 0)
430 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
431 MessageManager.formatMessage(
432 "label.pdb_entries_couldnt_be_retrieved", new Object[]
433 { errormsgs.toString() }),
434 MessageManager.getString("label.couldnt_load_file"),
435 JvOptionPane.ERROR_MESSAGE);
438 if (files.length() > 0)
440 jmb.setFinishedInit(false);
441 if (!addingStructures)
446 } catch (Exception ex)
448 Console.error("Couldn't open Chimera viewer!", ex);
451 if (!jmb.isViewerRunning())
454 // TODO: ensure we tidy up JAL-3619
458 for (PDBEntry pe : filePDB)
461 if (pe.getFile() != null)
465 int pos = filePDBpos.get(num).intValue();
466 long startTime = startProgressBar(getViewerName() + " "
467 + MessageManager.getString("status.opening_file_for")
470 jmb.addSequence(pos, jmb.getSequence()[pos]);
471 File fl = new File(pe.getFile());
472 DataSourceType protocol = DataSourceType.URL;
477 protocol = DataSourceType.FILE;
479 } catch (Throwable e)
483 stopProgressBar("", startTime);
485 // Explicitly map to the filename used by Chimera ;
487 pdb = jmb.getSsm().setMapping(jmb.getSequence()[pos],
488 jmb.getChains()[pos], pe.getFile(), protocol,
489 getProgressIndicator());
490 jmb.stashFoundChains(pdb, pe.getFile());
492 } catch (OutOfMemoryError oomerror)
495 "When trying to open and map structures from Chimera!",
497 } catch (Exception ex)
500 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
504 Console.debug("File locations are " + files);
510 jmb.setFinishedInit(true);
511 jmb.setLoadingFromArchive(false);
514 * ensure that any newly discovered features (e.g. RESNUM)
515 * are notified to the FeatureRenderer (and added to any
516 * open feature settings dialog)
518 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
524 // refresh the sequence colours for the new structure(s)
525 for (AlignmentViewPanel ap : _colourwith)
527 jmb.updateColours(ap);
529 // do superposition if asked to
530 if (alignAddedStructures)
532 new Thread(new Runnable()
537 alignStructsWithAllAlignPanels();
541 addingStructures = false;
549 public void makePDBImage(TYPE imageType)
551 throw new UnsupportedOperationException(
552 "Image export for Chimera is not implemented");
556 public AAStructureBindingModel getBinding()
563 public ViewerType getViewerType()
565 return ViewerType.CHIMERA;
569 protected String getViewerName()