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 jalview.datamodel.PDBEntry;
24 import jalview.datamodel.SequenceI;
25 import jalview.gui.StructureViewer.ViewerType;
26 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
27 import jalview.io.DataSourceType;
28 import jalview.jbgui.GStructureViewer;
29 import jalview.structure.StructureSelectionManager;
30 import jalview.structures.models.AAStructureBindingModel;
31 import jalview.util.MessageManager;
33 import java.awt.Component;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.awt.event.ItemEvent;
37 import java.awt.event.ItemListener;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Vector;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JInternalFrame;
44 import javax.swing.JMenuItem;
47 * Base class with common functionality for JMol, Chimera or other structure
53 public abstract class StructureViewerBase extends GStructureViewer
54 implements Runnable, ViewSetProvider
58 * list of sequenceSet ids associated with the view
60 protected List<String> _aps = new ArrayList<String>();
63 * list of alignment panels to use for superposition
65 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
68 * list of alignment panels that are used for colouring structures by aligned
71 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
73 private String viewId = null;
75 private AlignmentPanel ap;
77 protected boolean alignAddedStructures = false;
79 protected boolean _started = false;
81 protected boolean addingStructures = false;
83 protected Thread worker = null;
85 protected boolean allChainsSelected = false;
90 * @return true if this Jmol instance is linked with the given alignPanel
92 public boolean isLinkedWith(AlignmentPanel ap2)
94 return _aps.contains(ap2.av.getSequenceSetId());
97 public boolean isUsedforaligment(AlignmentPanel ap2)
100 return (_alignwith != null) && _alignwith.contains(ap2);
103 public boolean isUsedforcolourby(AlignmentPanel ap2)
105 return (_colourwith != null) && _colourwith.contains(ap2);
110 * @return TRUE if the view is NOT being coloured by the alignment colours.
112 public boolean isColouredByViewer()
114 return !getBinding().isColourBySequence();
117 public String getViewId()
121 viewId = System.currentTimeMillis() + "." + this.hashCode();
126 protected void setViewId(String viewId)
128 this.viewId = viewId;
131 public abstract String getStateInfo();
133 protected void buildActionMenu()
135 if (_alignwith == null)
137 _alignwith = new Vector<AlignmentPanel>();
139 if (_alignwith.size() == 0 && ap != null)
144 for (Component c : viewerActionMenu.getMenuComponents())
146 if (c != alignStructs)
148 viewerActionMenu.remove((JMenuItem) c);
153 public AlignmentPanel getAlignmentPanel()
158 protected void setAlignmentPanel(AlignmentPanel alp)
164 public AlignmentPanel[] getAllAlignmentPanels()
166 AlignmentPanel[] t, list = new AlignmentPanel[0];
167 for (String setid : _aps)
169 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
172 t = new AlignmentPanel[list.length + panels.length];
173 System.arraycopy(list, 0, t, 0, list.length);
174 System.arraycopy(panels, 0, t, list.length, panels.length);
183 * set the primary alignmentPanel reference and add another alignPanel to the
184 * list of ones to use for colouring and aligning
188 public void addAlignmentPanel(AlignmentPanel nap)
190 if (getAlignmentPanel() == null)
192 setAlignmentPanel(nap);
194 if (!_aps.contains(nap.av.getSequenceSetId()))
196 _aps.add(nap.av.getSequenceSetId());
201 * remove any references held to the given alignment panel
205 public void removeAlignmentPanel(AlignmentPanel nap)
209 _alignwith.remove(nap);
210 _colourwith.remove(nap);
211 if (getAlignmentPanel() == nap)
213 setAlignmentPanel(null);
214 for (AlignmentPanel aps : getAllAlignmentPanels())
218 setAlignmentPanel(aps);
223 } catch (Exception ex)
226 if (getAlignmentPanel() != null)
232 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
234 addAlignmentPanel(nap);
235 if (!_alignwith.contains(nap))
241 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
243 if (_alignwith.contains(nap))
245 _alignwith.remove(nap);
249 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
250 boolean enableColourBySeq)
252 useAlignmentPanelForColourbyseq(nap);
253 getBinding().setColourBySequence(enableColourBySeq);
254 seqColour.setSelected(enableColourBySeq);
255 viewerColour.setSelected(!enableColourBySeq);
258 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
260 addAlignmentPanel(nap);
261 if (!_colourwith.contains(nap))
263 _colourwith.add(nap);
267 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
269 if (_colourwith.contains(nap))
271 _colourwith.remove(nap);
275 public abstract ViewerType getViewerType();
277 protected abstract AAStructureBindingModel getBindingModel();
280 * add a new structure (with associated sequences and chains) to this viewer,
281 * retrieving it if necessary first.
287 * if true, new structure(s) will be aligned using associated
291 protected void addStructure(final PDBEntry pdbentry,
292 final SequenceI[] seqs, final String[] chains,
293 final boolean align, final IProgressIndicator alignFrame)
295 if (pdbentry.getFile() == null)
297 if (worker != null && worker.isAlive())
299 // a retrieval is in progress, wait around and add ourselves to the
301 new Thread(new Runnable()
306 while (worker != null && worker.isAlive() && _started)
310 Thread.sleep(100 + ((int) Math.random() * 100));
312 } catch (Exception e)
316 // and call ourselves again.
317 addStructure(pdbentry, seqs, chains, align, alignFrame);
323 // otherwise, start adding the structure.
324 getBindingModel().addSequenceAndChain(new PDBEntry[] { pdbentry },
325 new SequenceI[][] { seqs }, new String[][] { chains });
326 addingStructures = true;
328 alignAddedStructures = align;
329 worker = new Thread(this);
335 * Presents a dialog with the option to add an align a structure to an
336 * existing structure view
340 * @return YES, NO or CANCEL JvOptionPane code
342 protected int chooseAlignStructureToViewer(String pdbId,
343 StructureViewerBase view)
345 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
346 MessageManager.formatMessage("label.add_pdbentry_to_view",
347 new Object[] { pdbId, view.getTitle() }),
349 .getString("label.align_to_existing_structure_view"),
350 JvOptionPane.YES_NO_CANCEL_OPTION);
354 protected abstract boolean hasPdbId(String pdbId);
357 * Returns a list of any structure viewers of the same type. The list is
358 * restricted to those linked to the given alignment panel if it is not null.
360 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp){
362 List<StructureViewerBase> result = new ArrayList<StructureViewerBase>();
363 JInternalFrame[] frames = Desktop.instance.getAllFrames();
365 for (JInternalFrame frame : frames)
367 if (this.getClass().isAssignableFrom(frame.getClass()))
370 || ((StructureViewerBase) frame).isLinkedWith(alp))
372 result.add((StructureViewerBase) frame);
381 * Check for any existing views involving this alignment and give user the
382 * option to add and align this molecule to one of them
389 * @return true if user adds to a view, or cancels entirely, else false
391 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
392 String[] chains, final AlignmentPanel apanel, String pdbId)
394 for (StructureViewerBase view : getViewersFor(apanel))
396 // TODO: highlight the view somehow
398 * JAL-1742 exclude view with this structure already mapped (don't offer
399 * to align chain B to chain A of the same structure)
401 if (view.hasPdbId(pdbId))
405 int option = chooseAlignStructureToViewer(pdbId, view);
406 if (option == JvOptionPane.CANCEL_OPTION)
410 else if (option == JvOptionPane.YES_OPTION)
412 view.useAlignmentPanelForSuperposition(apanel);
413 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
418 // NO_OPTION - offer the next viewer if any
423 * nothing offered and selected
429 * Adds mappings for the given sequences to an already opened PDB structure,
430 * and updates any viewers that have the PDB file
437 protected void addSequenceMappingsToStructure(SequenceI[] seq,
438 String[] chains, final AlignmentPanel apanel, String pdbFilename)
440 // TODO : Fix multiple seq to one chain issue here.
442 * create the mappings
444 apanel.getStructureSelectionManager().setMapping(seq, chains,
445 pdbFilename, DataSourceType.FILE);
448 * alert the FeatureRenderer to show new (PDB RESNUM) features
450 if (apanel.getSeqPanel().seqCanvas.fr != null)
452 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
453 apanel.paintAlignment(true);
457 * add the sequences to any other viewers (of the same type) for this pdb
460 // JBPNOTE: this looks like a binding routine, rather than a gui routine
461 for (StructureViewerBase viewer : getViewersFor(null))
463 AAStructureBindingModel bindingModel = viewer.getBindingModel();
464 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
466 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
468 bindingModel.addSequence(pe, seq);
469 viewer.addAlignmentPanel(apanel);
471 * add it to the set of alignments used for colouring structure by
474 viewer.useAlignmentPanelForColourbyseq(apanel);
475 viewer.buildActionMenu();
476 apanel.getStructureSelectionManager().sequenceColoursChanged(
485 * Check if the PDB file is already loaded, if so offer to add it to the
492 * @return true if the user chooses to add to a viewer, or to cancel entirely
494 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
495 final AlignmentPanel apanel, String pdbId)
497 boolean finished = false;
498 StructureSelectionManager ssm = apanel.getStructureSelectionManager();
499 String alreadyMapped = ssm.alreadyMappedToFile(pdbId);
501 if (alreadyMapped != null)
504 * the PDB file is already loaded
506 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
507 MessageManager.formatMessage(
508 "label.pdb_entry_is_already_displayed",
509 new Object[] { pdbId }), MessageManager
511 "label.map_sequences_to_visible_window",
512 new Object[] { pdbId }),
513 JvOptionPane.YES_NO_CANCEL_OPTION);
514 if (option == JvOptionPane.CANCEL_OPTION)
518 else if (option == JvOptionPane.YES_OPTION)
520 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
527 void setChainMenuItems(List<String> chainNames)
529 chainMenu.removeAll();
530 if (chainNames == null || chainNames.isEmpty())
534 JMenuItem menuItem = new JMenuItem(
535 MessageManager.getString("label.all"));
536 menuItem.addActionListener(new ActionListener()
539 public void actionPerformed(ActionEvent evt)
541 allChainsSelected = true;
542 for (int i = 0; i < chainMenu.getItemCount(); i++)
544 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
546 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
549 showSelectedChains();
550 allChainsSelected = false;
554 chainMenu.add(menuItem);
556 for (String chain : chainNames)
558 menuItem = new JCheckBoxMenuItem(chain, true);
559 menuItem.addItemListener(new ItemListener()
562 public void itemStateChanged(ItemEvent evt)
564 if (!allChainsSelected)
566 showSelectedChains();
571 chainMenu.add(menuItem);
575 abstract void showSelectedChains();