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 the structure viewer for its residue attribute names and add them as
119 * items off the attributes menu
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 if (getBinding().copyStructureAttributesToFeatures(attName,
137 getAlignmentPanel()) > 0)
139 getAlignmentPanel().getFeatureRenderer().featuresAdded();
143 attributesMenu.add(menuItem);
148 * Sends command(s) to the structure viewer to create residue attributes for
149 * visible Jalview features
151 protected void sendFeaturesToChimera()
154 int count = jmb.sendFeaturesToViewer(getAlignmentPanel());
156 MessageManager.formatMessage("label.attributes_set", count));
160 * open a single PDB structure in a new Chimera view
167 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
168 String[] chains, final AlignmentPanel ap)
172 openNewChimera(ap, new PDBEntry[] { pdbentry },
178 * Create a helper to manage progress bar display
180 protected void createProgressBar()
182 if (getProgressIndicator() == null)
184 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
188 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
192 jmb = newBindingModel(ap, pdbentrys, seqs);
193 addAlignmentPanel(ap);
194 useAlignmentPanelForColourbyseq(ap);
196 if (pdbentrys.length > 1)
198 useAlignmentPanelForSuperposition(ap);
200 jmb.setColourBySequence(true);
201 setSize(myWidth, myHeight);
204 addingStructures = false;
205 worker = new Thread(this);
208 this.addInternalFrameListener(new InternalFrameAdapter()
211 public void internalFrameClosing(
212 InternalFrameEvent internalFrameEvent)
220 protected JalviewChimeraBindingModel newBindingModel(AlignmentPanel ap,
221 PDBEntry[] pdbentrys, SequenceI[][] seqs)
223 return new JalviewChimeraBindingModel(this,
224 ap.getStructureSelectionManager(), pdbentrys, seqs, null);
228 * Create a new viewer from saved session state data including Chimera session
231 * @param chimeraSessionFile
235 * @param colourByChimera
236 * @param colourBySequence
239 public ChimeraViewFrame(StructureViewerModel viewerData,
240 AlignmentPanel alignPanel, String sessionFile, String vid)
244 this.chimeraSessionFile = sessionFile;
245 Map<File, StructureData> pdbData = viewerData.getFileData();
246 PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
247 SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
249 for (StructureData data : pdbData.values())
251 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
252 PDBEntry.Type.PDB, data.getFilePath());
253 pdbArray[i] = pdbentry;
254 List<SequenceI> sequencesForPdb = data.getSeqList();
255 seqsArray[i] = sequencesForPdb
256 .toArray(new SequenceI[sequencesForPdb.size()]);
259 openNewChimera(alignPanel, pdbArray, seqsArray);
260 if (viewerData.isColourByViewer())
262 jmb.setColourBySequence(false);
263 seqColour.setSelected(false);
264 viewerColour.setSelected(true);
266 else if (viewerData.isColourWithAlignPanel())
268 jmb.setColourBySequence(true);
269 seqColour.setSelected(true);
270 viewerColour.setSelected(false);
275 * create a new viewer containing several structures, optionally superimposed
276 * using the given alignPanel.
282 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
287 setAlignAddedStructures(alignAdded);
288 openNewChimera(ap, pe, seqs);
292 * Default constructor
294 public ChimeraViewFrame()
299 * closeViewer will decide whether or not to close this frame
300 * depending on whether user chooses to Cancel or not
302 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
306 * Launch Chimera. If we have a chimera session file name, send Chimera the
307 * command to open its saved session file.
311 jmb.setFinishedInit(false);
312 Desktop.addInternalFrame(this,
313 jmb.getViewerTitle(getViewerName(), true), getBounds().width,
316 if (!jmb.launchChimera())
318 JvOptionPane.showMessageDialog(Desktop.desktop,
319 MessageManager.formatMessage("label.open_viewer_failed",
321 MessageManager.getString("label.error_loading_file"),
322 JvOptionPane.ERROR_MESSAGE);
327 if (this.chimeraSessionFile != null)
329 boolean opened = jmb.openSession(chimeraSessionFile);
332 System.err.println("An error occurred opening Chimera session file "
333 + chimeraSessionFile);
337 jmb.startChimeraListener();
341 * Open any newly added PDB structures in Chimera, having first fetched data
342 * from PDB (if not already saved).
348 // todo - record which pdbids were successfully imported.
349 StringBuilder errormsgs = new StringBuilder(128);
350 StringBuilder files = new StringBuilder(128);
351 List<PDBEntry> filePDB = new ArrayList<>();
352 List<Integer> filePDBpos = new ArrayList<>();
353 PDBEntry thePdbEntry = null;
354 StructureFile pdb = null;
357 String[] curfiles = jmb.getStructureFiles(); // files currently in viewer
358 // TODO: replace with reference fetching/transfer code (validate PDBentry
360 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
363 thePdbEntry = jmb.getPdbEntry(pi);
364 if (thePdbEntry.getFile() == null)
367 * Retrieve PDB data, save to file, attach to PDBEntry
369 file = fetchPdbFile(thePdbEntry);
372 errormsgs.append("'" + thePdbEntry.getId() + "' ");
378 * Got file already - ignore if already loaded in Chimera.
380 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
382 if (curfiles != null && curfiles.length > 0)
384 addingStructures = true; // already files loaded.
385 for (int c = 0; c < curfiles.length; c++)
387 if (curfiles[c].equals(file))
397 filePDB.add(thePdbEntry);
398 filePDBpos.add(Integer.valueOf(pi));
399 files.append(" \"" + Platform.escapeBackslashes(file) + "\"");
402 } catch (OutOfMemoryError oomerror)
404 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
406 } catch (Exception ex)
408 ex.printStackTrace();
410 "When retrieving pdbfiles for '" + thePdbEntry.getId() + "'");
412 if (errormsgs.length() > 0)
415 JvOptionPane.showInternalMessageDialog(Desktop.desktop,
416 MessageManager.formatMessage(
417 "label.pdb_entries_couldnt_be_retrieved", new Object[]
418 { errormsgs.toString() }),
419 MessageManager.getString("label.couldnt_load_file"),
420 JvOptionPane.ERROR_MESSAGE);
423 if (files.length() > 0)
425 jmb.setFinishedInit(false);
426 if (!addingStructures)
431 } catch (Exception ex)
433 Cache.log.error("Couldn't open Chimera viewer!", ex);
437 for (PDBEntry pe : filePDB)
440 if (pe.getFile() != null)
444 int pos = filePDBpos.get(num).intValue();
445 long startTime = startProgressBar(getViewerName() + " "
446 + MessageManager.getString("status.opening_file_for")
449 jmb.addSequence(pos, jmb.getSequence()[pos]);
450 File fl = new File(pe.getFile());
451 DataSourceType protocol = DataSourceType.URL;
456 protocol = DataSourceType.FILE;
458 } catch (Throwable e)
462 stopProgressBar("", startTime);
464 // Explicitly map to the filename used by Chimera ;
466 pdb = jmb.getSsm().setMapping(jmb.getSequence()[pos],
467 jmb.getChains()[pos], pe.getFile(), protocol,
468 getProgressIndicator());
469 jmb.stashFoundChains(pdb, pe.getFile());
471 } catch (OutOfMemoryError oomerror)
474 "When trying to open and map structures from Chimera!",
476 } catch (Exception ex)
479 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
483 Cache.log.debug("File locations are " + files);
489 jmb.setFinishedInit(true);
490 jmb.setLoadingFromArchive(false);
493 * ensure that any newly discovered features (e.g. RESNUM)
494 * are added to any open feature settings dialog
496 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
502 // refresh the sequence colours for the new structure(s)
503 for (AlignmentViewPanel ap : _colourwith)
505 jmb.updateColours(ap);
507 // do superposition if asked to
508 if (alignAddedStructures)
510 new Thread(new Runnable()
515 alignStructsWithAllAlignPanels();
519 addingStructures = false;
526 public void makePDBImage(TYPE imageType)
528 throw new UnsupportedOperationException(
529 "Image export for Chimera is not implemented");
533 public AAStructureBindingModel getBinding()
539 public ViewerType getViewerType()
541 return ViewerType.CHIMERA;
545 protected String getViewerName()