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.Cache;
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;
82 * Initialise menu options.
85 protected void initMenus()
89 savemenu.setVisible(false); // not yet implemented
90 viewMenu.add(fitToWindow);
92 writeFeatures = new JMenuItem(
93 MessageManager.getString("label.create_viewer_attributes"));
94 writeFeatures.setToolTipText(MessageManager
95 .getString("label.create_viewer_attributes_tip"));
96 writeFeatures.addActionListener(new ActionListener()
99 public void actionPerformed(ActionEvent e)
101 sendFeaturesToChimera();
104 viewerActionMenu.add(writeFeatures);
106 fetchAttributes = new JMenu(
107 MessageManager.getString("label.fetch_chimera_attributes"));
108 fetchAttributes.setToolTipText(
109 MessageManager.getString("label.fetch_chimera_attributes_tip"));
110 fetchAttributes.addMouseListener(new MouseAdapter()
114 public void mouseEntered(MouseEvent e)
116 buildAttributesMenu(fetchAttributes);
119 viewerActionMenu.add(fetchAttributes);
123 protected void buildActionMenu()
125 super.buildActionMenu();
126 // add these back in after menu is refreshed
127 viewerActionMenu.add(writeFeatures);
128 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());
170 MessageManager.formatMessage("label.attributes_set", count));
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);
241 * Create a new viewer from saved session state data including Chimera session
244 * @param chimeraSessionFile
248 * @param colourByChimera
249 * @param colourBySequence
252 public ChimeraViewFrame(StructureViewerModel viewerData,
253 AlignmentPanel alignPanel, String sessionFile, String vid)
257 this.chimeraSessionFile = sessionFile;
258 Map<File, StructureData> pdbData = viewerData.getFileData();
259 PDBEntry[] pdbArray = new PDBEntry[pdbData.size()];
260 SequenceI[][] seqsArray = new SequenceI[pdbData.size()][];
262 for (StructureData data : pdbData.values())
264 PDBEntry pdbentry = new PDBEntry(data.getPdbId(), null,
265 PDBEntry.Type.PDB, data.getFilePath());
266 pdbArray[i] = pdbentry;
267 List<SequenceI> sequencesForPdb = data.getSeqList();
268 seqsArray[i] = sequencesForPdb
269 .toArray(new SequenceI[sequencesForPdb.size()]);
272 openNewChimera(alignPanel, pdbArray, seqsArray);
273 if (viewerData.isColourByViewer())
275 jmb.setColourBySequence(false);
276 seqColour.setSelected(false);
277 viewerColour.setSelected(true);
279 else if (viewerData.isColourWithAlignPanel())
281 jmb.setColourBySequence(true);
282 seqColour.setSelected(true);
283 viewerColour.setSelected(false);
288 * create a new viewer containing several structures, optionally superimposed
289 * using the given alignPanel.
295 public ChimeraViewFrame(PDBEntry[] pe, boolean alignAdded,
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.getDesktopPane(),
332 MessageManager.formatMessage("label.open_viewer_failed",
334 MessageManager.getString("label.error_loading_file"),
335 JvOptionPane.ERROR_MESSAGE);
340 if (this.chimeraSessionFile != null)
342 boolean opened = jmb.openSession(chimeraSessionFile);
345 System.err.println("An error occurred opening Chimera session file "
346 + chimeraSessionFile);
350 jmb.startChimeraListener();
354 * Open any newly added PDB structures in Chimera, having first fetched data
355 * from PDB (if not already saved).
361 // todo - record which pdbids were successfully imported.
362 StringBuilder errormsgs = new StringBuilder(128);
363 StringBuilder files = new StringBuilder(128);
364 List<PDBEntry> filePDB = new ArrayList<>();
365 List<Integer> filePDBpos = new ArrayList<>();
366 PDBEntry thePdbEntry = null;
367 StructureFile pdb = null;
370 String[] curfiles = jmb.getStructureFiles(); // files currently in viewer
371 // TODO: replace with reference fetching/transfer code (validate PDBentry
373 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
376 thePdbEntry = jmb.getPdbEntry(pi);
377 if (thePdbEntry.getFile() == null)
380 * Retrieve PDB data, save to file, attach to PDBEntry
382 file = fetchPdbFile(thePdbEntry);
385 errormsgs.append("'" + thePdbEntry.getId() + "' ");
391 * Got file already - ignore if already loaded in Chimera.
393 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
395 if (curfiles != null && curfiles.length > 0)
397 addingStructures = true; // already files loaded.
398 for (int c = 0; c < curfiles.length; c++)
400 if (curfiles[c].equals(file))
410 filePDB.add(thePdbEntry);
411 filePDBpos.add(Integer.valueOf(pi));
412 files.append(" \"" + Platform.escapeBackslashes(file) + "\"");
415 } catch (OutOfMemoryError oomerror)
417 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
419 } catch (Exception ex)
421 ex.printStackTrace();
423 "When retrieving pdbfiles for '" + thePdbEntry.getId() + "'");
425 if (errormsgs.length() > 0)
428 JvOptionPane.showInternalMessageDialog(Desktop.getDesktopPane(),
429 MessageManager.formatMessage(
430 "label.pdb_entries_couldnt_be_retrieved", new Object[]
431 { errormsgs.toString() }),
432 MessageManager.getString("label.couldnt_load_file"),
433 JvOptionPane.ERROR_MESSAGE);
436 if (files.length() > 0)
438 jmb.setFinishedInit(false);
439 if (!addingStructures)
444 } catch (Exception ex)
446 Cache.log.error("Couldn't open Chimera viewer!", ex);
450 for (PDBEntry pe : filePDB)
453 if (pe.getFile() != null)
457 int pos = filePDBpos.get(num).intValue();
458 long startTime = startProgressBar(getViewerName() + " "
459 + MessageManager.getString("status.opening_file_for")
462 jmb.addSequence(pos, jmb.getSequence()[pos]);
463 File fl = new File(pe.getFile());
464 DataSourceType protocol = DataSourceType.URL;
469 protocol = DataSourceType.FILE;
471 } catch (Throwable e)
475 stopProgressBar("", startTime);
477 // Explicitly map to the filename used by Chimera ;
479 pdb = jmb.getSsm().setMapping(jmb.getSequence()[pos],
480 jmb.getChains()[pos], pe.getFile(), protocol,
481 getProgressIndicator());
482 jmb.stashFoundChains(pdb, pe.getFile());
484 } catch (OutOfMemoryError oomerror)
487 "When trying to open and map structures from Chimera!",
489 } catch (Exception ex)
492 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
496 Cache.log.debug("File locations are " + files);
502 jmb.setFinishedInit(true);
503 jmb.setLoadingFromArchive(false);
506 * ensure that any newly discovered features (e.g. RESNUM)
507 * are notified to the FeatureRenderer (and added to any
508 * open feature settings dialog)
510 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
516 // refresh the sequence colours for the new structure(s)
517 for (AlignmentViewPanel ap : _colourwith)
519 jmb.updateColours(ap);
521 // do superposition if asked to
522 if (alignAddedStructures)
524 new Thread(new Runnable()
529 alignStructsWithAllAlignPanels();
533 addingStructures = false;
541 public void makePDBImage(TYPE imageType)
543 throw new UnsupportedOperationException(
544 "Image export for Chimera is not implemented");
548 public AAStructureBindingModel getBinding()
555 public ViewerType getViewerType()
557 return ViewerType.CHIMERA;
561 protected String getViewerName()