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.AppletFormatAdapter;
28 import jalview.jbgui.GStructureViewer;
29 import jalview.structures.models.AAStructureBindingModel;
30 import jalview.util.MessageManager;
32 import java.awt.Component;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Vector;
37 import javax.swing.JMenuItem;
38 import javax.swing.JOptionPane;
41 * Base class with common functionality for JMol, Chimera or other structure
47 public abstract class StructureViewerBase extends GStructureViewer
48 implements Runnable, ViewSetProvider
52 * list of sequenceSet ids associated with the view
54 protected List<String> _aps = new ArrayList<String>();
57 * list of alignment panels to use for superposition
59 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
62 * list of alignment panels that are used for colouring structures by aligned
65 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
67 private String viewId = null;
69 private AlignmentPanel ap;
71 protected boolean alignAddedStructures = false;
73 protected boolean _started = false;
75 protected boolean addingStructures = false;
77 protected Thread worker = null;
82 * @return true if this Jmol instance is linked with the given alignPanel
84 public boolean isLinkedWith(AlignmentPanel ap2)
86 return _aps.contains(ap2.av.getSequenceSetId());
89 public boolean isUsedforaligment(AlignmentPanel ap2)
92 return (_alignwith != null) && _alignwith.contains(ap2);
95 public boolean isUsedforcolourby(AlignmentPanel ap2)
97 return (_colourwith != null) && _colourwith.contains(ap2);
102 * @return TRUE if the view is NOT being coloured by the alignment colours.
104 public boolean isColouredByViewer()
106 return !getBinding().isColourBySequence();
109 public String getViewId()
113 viewId = System.currentTimeMillis() + "." + this.hashCode();
118 protected void setViewId(String viewId)
120 this.viewId = viewId;
123 public abstract String getStateInfo();
125 protected void buildActionMenu()
127 if (_alignwith == null)
129 _alignwith = new Vector<AlignmentPanel>();
131 if (_alignwith.size() == 0 && ap != null)
136 for (Component c : viewerActionMenu.getMenuComponents())
138 if (c != alignStructs)
140 viewerActionMenu.remove((JMenuItem) c);
145 public AlignmentPanel getAlignmentPanel()
150 protected void setAlignmentPanel(AlignmentPanel alp)
156 public AlignmentPanel[] getAllAlignmentPanels()
158 AlignmentPanel[] t, list = new AlignmentPanel[0];
159 for (String setid : _aps)
161 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
164 t = new AlignmentPanel[list.length + panels.length];
165 System.arraycopy(list, 0, t, 0, list.length);
166 System.arraycopy(panels, 0, t, list.length, panels.length);
175 * set the primary alignmentPanel reference and add another alignPanel to the
176 * list of ones to use for colouring and aligning
180 public void addAlignmentPanel(AlignmentPanel nap)
182 if (getAlignmentPanel() == null)
184 setAlignmentPanel(nap);
186 if (!_aps.contains(nap.av.getSequenceSetId()))
188 _aps.add(nap.av.getSequenceSetId());
193 * remove any references held to the given alignment panel
197 public void removeAlignmentPanel(AlignmentPanel nap)
201 _alignwith.remove(nap);
202 _colourwith.remove(nap);
203 if (getAlignmentPanel() == nap)
205 setAlignmentPanel(null);
206 for (AlignmentPanel aps : getAllAlignmentPanels())
210 setAlignmentPanel(aps);
215 } catch (Exception ex)
218 if (getAlignmentPanel() != null)
224 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
226 addAlignmentPanel(nap);
227 if (!_alignwith.contains(nap))
233 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
235 if (_alignwith.contains(nap))
237 _alignwith.remove(nap);
241 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
242 boolean enableColourBySeq)
244 useAlignmentPanelForColourbyseq(nap);
245 getBinding().setColourBySequence(enableColourBySeq);
246 seqColour.setSelected(enableColourBySeq);
247 viewerColour.setSelected(!enableColourBySeq);
250 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
252 addAlignmentPanel(nap);
253 if (!_colourwith.contains(nap))
255 _colourwith.add(nap);
259 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
261 if (_colourwith.contains(nap))
263 _colourwith.remove(nap);
267 public abstract ViewerType getViewerType();
269 protected abstract AAStructureBindingModel getBindingModel();
271 protected abstract IProgressIndicator getIProgressIndicator();
274 * add a new structure (with associated sequences and chains) to this viewer,
275 * retrieving it if necessary first.
281 * if true, new structure(s) will be aligned using associated
285 protected void addStructure(final PDBEntry pdbentry,
286 final SequenceI[] seqs, final String[] chains,
287 final boolean align, final IProgressIndicator alignFrame)
289 if (pdbentry.getFile() == null)
291 if (worker != null && worker.isAlive())
293 // a retrieval is in progress, wait around and add ourselves to the
295 new Thread(new Runnable()
300 while (worker != null && worker.isAlive() && _started)
304 Thread.sleep(100 + ((int) Math.random() * 100));
306 } catch (Exception e)
310 // and call ourselves again.
311 addStructure(pdbentry, seqs, chains, align, alignFrame);
317 // otherwise, start adding the structure.
318 getBindingModel().addSequenceAndChain(new PDBEntry[] { pdbentry },
319 new SequenceI[][] { seqs }, new String[][] { chains });
320 addingStructures = true;
322 alignAddedStructures = align;
323 worker = new Thread(this);
329 * Presents a dialog with the option to add an align a structure to an
330 * existing structure view
334 * @return YES, NO or CANCEL JOptionPane code
336 protected int chooseAlignStructureToViewer(String pdbId,
337 StructureViewerBase view)
339 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
340 MessageManager.formatMessage("label.add_pdbentry_to_view",
341 new Object[] { pdbId, view.getTitle() }),
343 .getString("label.align_to_existing_structure_view"),
344 JOptionPane.YES_NO_CANCEL_OPTION);
348 protected abstract boolean hasPdbId(String pdbId);
350 protected abstract List<StructureViewerBase> getViewersFor(
354 * Check for any existing views involving this alignment and give user the
355 * option to add and align this molecule to one of them
362 * @return true if user adds to a view, or cancels entirely, else false
364 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
365 String[] chains, final AlignmentPanel apanel, String pdbId)
367 for (StructureViewerBase view : getViewersFor(apanel))
369 // TODO: highlight the view somehow
371 * JAL-1742 exclude view with this structure already mapped (don't offer
372 * to align chain B to chain A of the same structure)
374 if (view.hasPdbId(pdbId))
378 int option = chooseAlignStructureToViewer(pdbId, view);
379 if (option == JOptionPane.CANCEL_OPTION)
383 else if (option == JOptionPane.YES_OPTION)
385 view.useAlignmentPanelForSuperposition(apanel);
386 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
391 // NO_OPTION - offer the next viewer if any
396 * nothing offered and selected
402 * Adds mappings for the given sequences to an already opened PDB structure,
403 * and updates any viewers that have the PDB file
410 protected void addSequenceMappingsToStructure(SequenceI[] seq,
411 String[] chains, final AlignmentPanel apanel, String pdbFilename)
413 // TODO : Fix multiple seq to one chain issue here.
415 * create the mappings
417 apanel.getStructureSelectionManager().setMapping(seq, chains,
418 pdbFilename, AppletFormatAdapter.FILE, getIProgressIndicator());
421 * alert the FeatureRenderer to show new (PDB RESNUM) features
423 if (apanel.getSeqPanel().seqCanvas.fr != null)
425 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
426 apanel.paintAlignment(true);
430 * add the sequences to any other viewers (of the same type) for this pdb
433 // JBPNOTE: this looks like a binding routine, rather than a gui routine
434 for (StructureViewerBase viewer : getViewersFor(null))
436 AAStructureBindingModel bindingModel = viewer.getBindingModel();
437 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
439 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
441 bindingModel.addSequence(pe, seq);
442 viewer.addAlignmentPanel(apanel);
444 * add it to the set of alignments used for colouring structure by
447 viewer.useAlignmentPanelForColourbyseq(apanel);
448 viewer.buildActionMenu();
449 apanel.getStructureSelectionManager().sequenceColoursChanged(
458 * Check if the PDB file is already loaded, if so offer to add it to the
465 * @return true if the user chooses to add to a viewer, or to cancel entirely
467 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
468 final AlignmentPanel apanel, String pdbId)
470 boolean finished = false;
471 String alreadyMapped = apanel.getStructureSelectionManager()
472 .alreadyMappedToFile(pdbId);
474 if (alreadyMapped != null)
477 * the PDB file is already loaded
479 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
480 MessageManager.formatMessage(
481 "label.pdb_entry_is_already_displayed",
482 new Object[] { pdbId }), MessageManager
484 "label.map_sequences_to_visible_window",
485 new Object[] { pdbId }),
486 JOptionPane.YES_NO_CANCEL_OPTION);
487 if (option == JOptionPane.CANCEL_OPTION)
491 else if (option == JOptionPane.YES_OPTION)
493 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);