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.Cache;
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;
77 * Initialise menu options.
80 protected void initMenus()
84 savemenu.setVisible(false); // not yet implemented
85 viewMenu.add(fitToWindow);
87 JMenuItem writeFeatures = new JMenuItem(
88 MessageManager.getString("label.create_viewer_attributes"));
89 writeFeatures.setToolTipText(MessageManager
90 .getString("label.create_viewer_attributes_tip"));
91 writeFeatures.addActionListener(new ActionListener()
94 public void actionPerformed(ActionEvent e)
96 sendFeaturesToChimera();
99 viewerActionMenu.add(writeFeatures);
101 final JMenu fetchAttributes = new JMenu(
102 MessageManager.getString("label.fetch_chimera_attributes"));
103 fetchAttributes.setToolTipText(
104 MessageManager.getString("label.fetch_chimera_attributes_tip"));
105 fetchAttributes.addMouseListener(new MouseAdapter()
109 public void mouseEntered(MouseEvent e)
111 buildAttributesMenu(fetchAttributes);
114 viewerActionMenu.add(fetchAttributes);
118 * Query Chimera for its residue attribute names and add them as items off the
121 * @param attributesMenu
123 protected void buildAttributesMenu(JMenu attributesMenu)
125 List<String> atts = jmb.getChimeraAttributes();
126 attributesMenu.removeAll();
127 Collections.sort(atts);
128 for (String attName : atts)
130 JMenuItem menuItem = new JMenuItem(attName);
131 menuItem.addActionListener(new ActionListener()
134 public void actionPerformed(ActionEvent e)
136 getChimeraAttributes(attName);
139 attributesMenu.add(menuItem);
144 * Read residues in Chimera with the given attribute name, and set as features
145 * on the corresponding sequence positions (if any)
149 protected void getChimeraAttributes(String attName)
151 jmb.copyStructureAttributesToFeatures(attName, getAlignmentPanel());
155 * Sends command(s) to the structure viewer to create residue attributes for
156 * visible Jalview features
158 protected void sendFeaturesToChimera()
161 int count = jmb.sendFeaturesToViewer(getAlignmentPanel());
163 MessageManager.formatMessage("label.attributes_set", count));
167 * open a single PDB structure in a new Chimera view
174 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
175 String[] chains, final AlignmentPanel ap)
179 openNewChimera(ap, new PDBEntry[] { pdbentry },
185 * Create a helper to manage progress bar display
187 protected void createProgressBar()
189 if (getProgressIndicator() == null)
191 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
195 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
199 jmb = newBindingModel(ap, pdbentrys, seqs);
200 addAlignmentPanel(ap);
201 useAlignmentPanelForColourbyseq(ap);
203 if (pdbentrys.length > 1)
205 useAlignmentPanelForSuperposition(ap);
207 jmb.setColourBySequence(true);
208 setSize(myWidth, myHeight);
211 addingStructures = false;
212 worker = new Thread(this);
215 this.addInternalFrameListener(new InternalFrameAdapter()
218 public void internalFrameClosing(
219 InternalFrameEvent internalFrameEvent)
227 protected JalviewChimeraBindingModel newBindingModel(AlignmentPanel ap,
228 PDBEntry[] pdbentrys, SequenceI[][] seqs)
230 return new JalviewChimeraBindingModel(this,
231 ap.getStructureSelectionManager(), pdbentrys, seqs, null);
235 * Create a new viewer from saved session state data including Chimera session
238 * @param chimeraSessionFile
242 * @param colourByChimera
243 * @param colourBySequence
246 public ChimeraViewFrame(StructureViewerModel viewerData,
247 AlignmentPanel alignPanel, String sessionFile, String vid)
251 this.chimeraSessionFile = sessionFile;
252 Map<File, StructureData> pdbData = viewerData.getFileData();
253 PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
254 SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
256 for (StructureData data : pdbData.values())
258 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
259 PDBEntry.Type.PDB, data.getFilePath());
260 pdbArray[i] = pdbentry;
261 List<SequenceI> sequencesForPdb = data.getSeqList();
262 seqsArray[i] = sequencesForPdb
263 .toArray(new SequenceI[sequencesForPdb.size()]);
266 openNewChimera(alignPanel, pdbArray, seqsArray);
267 if (viewerData.isColourByViewer())
269 jmb.setColourBySequence(false);
270 seqColour.setSelected(false);
271 viewerColour.setSelected(true);
273 else if (viewerData.isColourWithAlignPanel())
275 jmb.setColourBySequence(true);
276 seqColour.setSelected(true);
277 viewerColour.setSelected(false);
282 * create a new viewer containing several structures, optionally superimposed
283 * using the given alignPanel.
289 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
294 setAlignAddedStructures(alignAdded);
295 openNewChimera(ap, pe, seqs);
299 * Default constructor
301 public ChimeraViewFrame()
306 * closeViewer will decide whether or not to close this frame
307 * depending on whether user chooses to Cancel or not
309 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
313 * Launch Chimera. If we have a chimera session file name, send Chimera the
314 * command to open its saved session file.
318 jmb.setFinishedInit(false);
319 Desktop.addInternalFrame(this,
320 jmb.getViewerTitle(getViewerName(), true), getBounds().width,
323 if (!jmb.launchChimera())
325 JvOptionPane.showMessageDialog(Desktop.desktop,
326 MessageManager.formatMessage("label.open_viewer_failed",
328 MessageManager.getString("label.error_loading_file"),
329 JvOptionPane.ERROR_MESSAGE);
334 if (this.chimeraSessionFile != null)
336 boolean opened = jmb.openSession(chimeraSessionFile);
339 System.err.println("An error occurred opening Chimera session file "
340 + chimeraSessionFile);
344 jmb.startChimeraListener();
348 * Open any newly added PDB structures in Chimera, having first fetched data
349 * from PDB (if not already saved).
355 // todo - record which pdbids were successfully imported.
356 StringBuilder errormsgs = new StringBuilder(128);
357 StringBuilder files = new StringBuilder(128);
358 List<PDBEntry> filePDB = new ArrayList<>();
359 List<Integer> filePDBpos = new ArrayList<>();
360 PDBEntry thePdbEntry = null;
361 StructureFile pdb = null;
364 String[] curfiles = jmb.getStructureFiles(); // files currently in viewer
365 // TODO: replace with reference fetching/transfer code (validate PDBentry
367 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
370 thePdbEntry = jmb.getPdbEntry(pi);
371 if (thePdbEntry.getFile() == null)
374 * Retrieve PDB data, save to file, attach to PDBEntry
376 file = fetchPdbFile(thePdbEntry);
379 errormsgs.append("'" + thePdbEntry.getId() + "' ");
385 * Got file already - ignore if already loaded in Chimera.
387 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
389 if (curfiles != null && curfiles.length > 0)
391 addingStructures = true; // already files loaded.
392 for (int c = 0; c < curfiles.length; c++)
394 if (curfiles[c].equals(file))
404 filePDB.add(thePdbEntry);
405 filePDBpos.add(Integer.valueOf(pi));
406 files.append(" \"" + Platform.escapeBackslashes(file) + "\"");
409 } catch (OutOfMemoryError oomerror)
411 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
413 } catch (Exception ex)
415 ex.printStackTrace();
417 "When retrieving pdbfiles for '" + thePdbEntry.getId() + "'");
419 if (errormsgs.length() > 0)
422 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
423 MessageManager.formatMessage(
424 "label.pdb_entries_couldnt_be_retrieved", new Object[]
425 { errormsgs.toString() }),
426 MessageManager.getString("label.couldnt_load_file"),
427 JvOptionPane.ERROR_MESSAGE);
430 if (files.length() > 0)
432 jmb.setFinishedInit(false);
433 if (!addingStructures)
438 } catch (Exception ex)
440 Cache.log.error("Couldn't open Chimera viewer!", ex);
444 for (PDBEntry pe : filePDB)
447 if (pe.getFile() != null)
451 int pos = filePDBpos.get(num).intValue();
452 long startTime = startProgressBar(getViewerName() + " "
453 + MessageManager.getString("status.opening_file_for")
456 jmb.addSequence(pos, jmb.getSequence()[pos]);
457 File fl = new File(pe.getFile());
458 DataSourceType protocol = DataSourceType.URL;
463 protocol = DataSourceType.FILE;
465 } catch (Throwable e)
469 stopProgressBar("", startTime);
471 // Explicitly map to the filename used by Chimera ;
473 pdb = jmb.getSsm().setMapping(jmb.getSequence()[pos],
474 jmb.getChains()[pos], pe.getFile(), protocol,
475 getProgressIndicator());
476 jmb.stashFoundChains(pdb, pe.getFile());
478 } catch (OutOfMemoryError oomerror)
481 "When trying to open and map structures from Chimera!",
483 } catch (Exception ex)
486 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
490 Cache.log.debug("File locations are " + files);
496 jmb.setFinishedInit(true);
497 jmb.setLoadingFromArchive(false);
500 * ensure that any newly discovered features (e.g. RESNUM)
501 * are added to any open feature settings dialog
503 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
509 // refresh the sequence colours for the new structure(s)
510 for (AlignmentViewPanel ap : _colourwith)
512 jmb.updateColours(ap);
514 // do superposition if asked to
515 if (alignAddedStructures)
517 new Thread(new Runnable()
522 alignStructsWithAllAlignPanels();
526 addingStructures = false;
533 public void makePDBImage(TYPE imageType)
535 throw new UnsupportedOperationException(
536 "Image export for Chimera is not implemented");
540 public AAStructureBindingModel getBinding()
546 protected void fitToWindow_actionPerformed()
552 public ViewerType getViewerType()
554 return ViewerType.CHIMERA;
558 protected String getViewerName()