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.util.ArrayList;
29 import java.util.Collections;
30 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.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.ext.rbvi.chimera.JalviewChimeraBinding;
47 import jalview.gui.StructureViewer.ViewerType;
48 import jalview.io.DataSourceType;
49 import jalview.io.StructureFile;
50 import jalview.structures.models.AAStructureBindingModel;
51 import jalview.util.ImageMaker.TYPE;
52 import jalview.util.MessageManager;
53 import jalview.util.Platform;
56 * GUI elements for handling an external chimera display
61 public class ChimeraViewFrame extends StructureViewerBase
63 private JalviewChimeraBinding jmb;
66 * Path to Chimera session file. This is set when an open Jalview/Chimera
67 * session is saved, or on restore from a Jalview project (if it holds the
68 * filename of any saved Chimera sessions).
70 private String chimeraSessionFile = null;
72 private int myWidth = 500;
74 private int myHeight = 150;
76 private JMenuItem writeFeatures = null;
78 private JMenu fetchAttributes = null;
81 * Initialise menu options.
84 protected void initMenus()
88 savemenu.setVisible(false); // not yet implemented
89 viewMenu.add(fitToWindow);
91 writeFeatures = new JMenuItem(
92 MessageManager.getString("label.create_viewer_attributes"));
93 writeFeatures.setToolTipText(
94 MessageManager.getString("label.create_viewer_attributes_tip"));
95 writeFeatures.addActionListener(new ActionListener()
98 public void actionPerformed(ActionEvent e)
100 sendFeaturesToChimera();
103 viewerActionMenu.add(writeFeatures);
105 fetchAttributes = new JMenu(MessageManager.formatMessage(
106 "label.fetch_viewer_attributes", getViewerName()));
107 fetchAttributes.setToolTipText(MessageManager.formatMessage(
108 "label.fetch_viewer_attributes_tip", getViewerName()));
109 fetchAttributes.addMouseListener(new MouseAdapter()
113 public void mouseEntered(MouseEvent e)
115 buildAttributesMenu(fetchAttributes);
118 viewerActionMenu.add(fetchAttributes);
122 protected void buildActionMenu()
124 super.buildActionMenu();
125 // add these back in after menu is refreshed
126 viewerActionMenu.add(writeFeatures);
127 viewerActionMenu.add(fetchAttributes);
132 * Query the structure viewer for its residue attribute names and add them as
133 * items off the attributes menu
135 * @param attributesMenu
137 protected void buildAttributesMenu(JMenu attributesMenu)
139 List<String> atts = jmb.getChimeraAttributes();
140 attributesMenu.removeAll();
141 Collections.sort(atts);
142 for (String attName : atts)
144 JMenuItem menuItem = new JMenuItem(attName);
145 menuItem.addActionListener(new ActionListener()
148 public void actionPerformed(ActionEvent e)
150 if (getBinding().copyStructureAttributesToFeatures(attName,
151 getAlignmentPanel()) > 0)
153 getAlignmentPanel().getFeatureRenderer().featuresAdded();
157 attributesMenu.add(menuItem);
162 * Sends command(s) to the structure viewer to create residue attributes for
163 * visible Jalview features
165 protected void sendFeaturesToChimera()
168 int count = jmb.sendFeaturesToViewer(getAlignmentPanel());
169 statusBar.setText(MessageManager.formatMessage("label.attributes_set",
170 count, getViewerName()));
174 * open a single PDB structure in a new Chimera view
181 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
182 String[] chains, final AlignmentPanel ap)
186 openNewChimera(ap, new PDBEntry[] { pdbentry },
192 * Create a helper to manage progress bar display
194 protected void createProgressBar()
196 if (getProgressIndicator() == null)
198 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
202 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
206 jmb = newBindingModel(ap, pdbentrys, seqs);
207 addAlignmentPanel(ap);
208 useAlignmentPanelForColourbyseq(ap);
210 if (pdbentrys.length > 1)
212 useAlignmentPanelForSuperposition(ap);
214 jmb.setColourBySequence(true);
215 setSize(myWidth, myHeight);
218 addingStructures = false;
219 worker = new Thread(this);
222 this.addInternalFrameListener(new InternalFrameAdapter()
225 public void internalFrameClosing(
226 InternalFrameEvent internalFrameEvent)
234 protected JalviewChimeraBindingModel newBindingModel(AlignmentPanel ap,
235 PDBEntry[] pdbentrys, SequenceI[][] seqs)
237 return new JalviewChimeraBindingModel(this,
238 ap.getStructureSelectionManager(), pdbentrys, seqs, null);
242 * Create a new viewer from saved session state data including Chimera session
245 * @param chimeraSessionFile
249 * @param colourByChimera
250 * @param colourBySequence
253 public ChimeraViewFrame(StructureViewerModel viewerData,
254 AlignmentPanel alignPanel, String sessionFile, String vid)
258 this.chimeraSessionFile = sessionFile;
259 Map<File, StructureData> pdbData = viewerData.getFileData();
260 PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
261 SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
263 for (StructureData data : pdbData.values())
265 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
266 PDBEntry.Type.PDB, data.getFilePath());
267 pdbArray[i] = pdbentry;
268 List<SequenceI> sequencesForPdb = data.getSeqList();
269 seqsArray[i] = sequencesForPdb
270 .toArray(new SequenceI[sequencesForPdb.size()]);
273 openNewChimera(alignPanel, pdbArray, seqsArray);
274 if (viewerData.isColourByViewer())
276 jmb.setColourBySequence(false);
277 seqColour.setSelected(false);
278 viewerColour.setSelected(true);
280 else if (viewerData.isColourWithAlignPanel())
282 jmb.setColourBySequence(true);
283 seqColour.setSelected(true);
284 viewerColour.setSelected(false);
289 * create a new viewer containing several structures, optionally superimposed
290 * using the given alignPanel.
296 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
297 SequenceI[][] seqs, AlignmentPanel ap)
300 setAlignAddedStructures(alignAdded);
301 openNewChimera(ap, pe, seqs);
305 * Default constructor
307 public ChimeraViewFrame()
312 * closeViewer will decide whether or not to close this frame
313 * depending on whether user chooses to Cancel or not
315 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
319 * Launch Chimera. If we have a chimera session file name, send Chimera the
320 * command to open its saved session file.
324 jmb.setFinishedInit(false);
325 Desktop.addInternalFrame(this,
326 jmb.getViewerTitle(getViewerName(), true), getBounds().width,
329 if (!jmb.launchChimera())
331 JvOptionPane.showMessageDialog(Desktop.desktop,
332 MessageManager.formatMessage("label.open_viewer_failed",
334 MessageManager.getString("label.error_loading_file"),
335 JvOptionPane.ERROR_MESSAGE);
336 jmb.closeViewer(true);
341 if (this.chimeraSessionFile != null)
343 boolean opened = jmb.openSession(chimeraSessionFile);
346 jalview.bin.Console.errPrintln(
347 "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.desktop,
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;
548 public void makePDBImage(TYPE imageType)
550 throw new UnsupportedOperationException(
551 "Image export for Chimera is not implemented");
555 public AAStructureBindingModel getBinding()
561 public ViewerType getViewerType()
563 return ViewerType.CHIMERA;
567 protected String getViewerName()