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.structures.models.AAStructureBindingModel;
30 import jalview.util.MessageManager;
32 import java.awt.Component;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.ItemEvent;
36 import java.awt.event.ItemListener;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Vector;
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JMenuItem;
45 * Base class with common functionality for JMol, Chimera or other structure
51 public abstract class StructureViewerBase extends GStructureViewer
52 implements Runnable, ViewSetProvider
56 * list of sequenceSet ids associated with the view
58 protected List<String> _aps = new ArrayList<String>();
61 * list of alignment panels to use for superposition
63 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
66 * list of alignment panels that are used for colouring structures by aligned
69 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
71 private String viewId = null;
73 private AlignmentPanel ap;
75 protected boolean alignAddedStructures = false;
77 protected boolean _started = false;
79 protected boolean addingStructures = false;
81 protected Thread worker = null;
83 protected boolean allChainsSelected = false;
88 * @return true if this Jmol instance is linked with the given alignPanel
90 public boolean isLinkedWith(AlignmentPanel ap2)
92 return _aps.contains(ap2.av.getSequenceSetId());
95 public boolean isUsedforaligment(AlignmentPanel ap2)
98 return (_alignwith != null) && _alignwith.contains(ap2);
101 public boolean isUsedforcolourby(AlignmentPanel ap2)
103 return (_colourwith != null) && _colourwith.contains(ap2);
108 * @return TRUE if the view is NOT being coloured by the alignment colours.
110 public boolean isColouredByViewer()
112 return !getBinding().isColourBySequence();
115 public String getViewId()
119 viewId = System.currentTimeMillis() + "." + this.hashCode();
124 protected void setViewId(String viewId)
126 this.viewId = viewId;
129 public abstract String getStateInfo();
131 protected void buildActionMenu()
133 if (_alignwith == null)
135 _alignwith = new Vector<AlignmentPanel>();
137 if (_alignwith.size() == 0 && ap != null)
142 for (Component c : viewerActionMenu.getMenuComponents())
144 if (c != alignStructs)
146 viewerActionMenu.remove((JMenuItem) c);
151 public AlignmentPanel getAlignmentPanel()
156 protected void setAlignmentPanel(AlignmentPanel alp)
162 public AlignmentPanel[] getAllAlignmentPanels()
164 AlignmentPanel[] t, list = new AlignmentPanel[0];
165 for (String setid : _aps)
167 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
170 t = new AlignmentPanel[list.length + panels.length];
171 System.arraycopy(list, 0, t, 0, list.length);
172 System.arraycopy(panels, 0, t, list.length, panels.length);
181 * set the primary alignmentPanel reference and add another alignPanel to the
182 * list of ones to use for colouring and aligning
186 public void addAlignmentPanel(AlignmentPanel nap)
188 if (getAlignmentPanel() == null)
190 setAlignmentPanel(nap);
192 if (!_aps.contains(nap.av.getSequenceSetId()))
194 _aps.add(nap.av.getSequenceSetId());
199 * remove any references held to the given alignment panel
203 public void removeAlignmentPanel(AlignmentPanel nap)
207 _alignwith.remove(nap);
208 _colourwith.remove(nap);
209 if (getAlignmentPanel() == nap)
211 setAlignmentPanel(null);
212 for (AlignmentPanel aps : getAllAlignmentPanels())
216 setAlignmentPanel(aps);
221 } catch (Exception ex)
224 if (getAlignmentPanel() != null)
230 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
232 addAlignmentPanel(nap);
233 if (!_alignwith.contains(nap))
239 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
241 if (_alignwith.contains(nap))
243 _alignwith.remove(nap);
247 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
248 boolean enableColourBySeq)
250 useAlignmentPanelForColourbyseq(nap);
251 getBinding().setColourBySequence(enableColourBySeq);
252 seqColour.setSelected(enableColourBySeq);
253 viewerColour.setSelected(!enableColourBySeq);
256 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
258 addAlignmentPanel(nap);
259 if (!_colourwith.contains(nap))
261 _colourwith.add(nap);
265 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
267 if (_colourwith.contains(nap))
269 _colourwith.remove(nap);
273 public abstract ViewerType getViewerType();
275 protected abstract AAStructureBindingModel getBindingModel();
277 protected abstract IProgressIndicator getIProgressIndicator();
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);
356 protected abstract List<StructureViewerBase> getViewersFor(
360 * Check for any existing views involving this alignment and give user the
361 * option to add and align this molecule to one of them
368 * @return true if user adds to a view, or cancels entirely, else false
370 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
371 String[] chains, final AlignmentPanel apanel, String pdbId)
373 for (StructureViewerBase view : getViewersFor(apanel))
375 // TODO: highlight the view somehow
377 * JAL-1742 exclude view with this structure already mapped (don't offer
378 * to align chain B to chain A of the same structure)
380 if (view.hasPdbId(pdbId))
384 int option = chooseAlignStructureToViewer(pdbId, view);
385 if (option == JvOptionPane.CANCEL_OPTION)
389 else if (option == JvOptionPane.YES_OPTION)
391 view.useAlignmentPanelForSuperposition(apanel);
392 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
397 // NO_OPTION - offer the next viewer if any
402 * nothing offered and selected
408 * Adds mappings for the given sequences to an already opened PDB structure,
409 * and updates any viewers that have the PDB file
416 protected void addSequenceMappingsToStructure(SequenceI[] seq,
417 String[] chains, final AlignmentPanel apanel, String pdbFilename)
419 // TODO : Fix multiple seq to one chain issue here.
421 * create the mappings
423 apanel.getStructureSelectionManager().setMapping(seq, chains,
424 pdbFilename, DataSourceType.FILE, getIProgressIndicator());
427 * alert the FeatureRenderer to show new (PDB RESNUM) features
429 if (apanel.getSeqPanel().seqCanvas.fr != null)
431 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
432 apanel.paintAlignment(true);
436 * add the sequences to any other viewers (of the same type) for this pdb
439 // JBPNOTE: this looks like a binding routine, rather than a gui routine
440 for (StructureViewerBase viewer : getViewersFor(null))
442 AAStructureBindingModel bindingModel = viewer.getBindingModel();
443 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
445 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
447 bindingModel.addSequence(pe, seq);
448 viewer.addAlignmentPanel(apanel);
450 * add it to the set of alignments used for colouring structure by
453 viewer.useAlignmentPanelForColourbyseq(apanel);
454 viewer.buildActionMenu();
455 apanel.getStructureSelectionManager().sequenceColoursChanged(
464 * Check if the PDB file is already loaded, if so offer to add it to the
471 * @return true if the user chooses to add to a viewer, or to cancel entirely
473 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
474 final AlignmentPanel apanel, String pdbId)
476 boolean finished = false;
477 String alreadyMapped = apanel.getStructureSelectionManager()
478 .alreadyMappedToFile(pdbId);
480 if (alreadyMapped != null)
483 * the PDB file is already loaded
485 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
486 MessageManager.formatMessage(
487 "label.pdb_entry_is_already_displayed",
488 new Object[] { pdbId }), MessageManager
490 "label.map_sequences_to_visible_window",
491 new Object[] { pdbId }),
492 JvOptionPane.YES_NO_CANCEL_OPTION);
493 if (option == JvOptionPane.CANCEL_OPTION)
497 else if (option == JvOptionPane.YES_OPTION)
499 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
506 void setChainMenuItems(List<String> chainNames)
508 chainMenu.removeAll();
509 if (chainNames == null || chainNames.isEmpty())
513 JMenuItem menuItem = new JMenuItem(
514 MessageManager.getString("label.all"));
515 menuItem.addActionListener(new ActionListener()
518 public void actionPerformed(ActionEvent evt)
520 allChainsSelected = true;
521 for (int i = 0; i < chainMenu.getItemCount(); i++)
523 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
525 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
528 showSelectedChains();
529 allChainsSelected = false;
533 chainMenu.add(menuItem);
535 for (String chain : chainNames)
537 menuItem = new JCheckBoxMenuItem(chain, true);
538 menuItem.addItemListener(new ItemListener()
541 public void itemStateChanged(ItemEvent evt)
543 if (!allChainsSelected)
545 showSelectedChains();
550 chainMenu.add(menuItem);
554 abstract void showSelectedChains();