3 import jalview.datamodel.PDBEntry;
4 import jalview.datamodel.SequenceI;
5 import jalview.gui.StructureViewer.ViewerType;
6 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
7 import jalview.io.AppletFormatAdapter;
8 import jalview.jbgui.GStructureViewer;
9 import jalview.structures.models.AAStructureBindingModel;
10 import jalview.util.MessageManager;
12 import java.awt.Component;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Vector;
17 import javax.swing.JMenuItem;
18 import javax.swing.JOptionPane;
21 * Base class with common functionality for JMol, Chimera or other structure
27 public abstract class StructureViewerBase extends GStructureViewer
28 implements Runnable, ViewSetProvider
32 * list of sequenceSet ids associated with the view
34 protected List<String> _aps = new ArrayList<String>();
36 * list of alignment panels to use for superposition
38 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
40 * list of alignment panels that are used for colouring structures by aligned
43 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
44 private String viewId = null;
45 private AlignmentPanel ap;
46 protected boolean alignAddedStructures = false;
47 protected boolean _started = false;
48 protected boolean addingStructures = false;
49 protected Thread worker = null;
54 * @return true if this Jmol instance is linked with the given alignPanel
56 public boolean isLinkedWith(AlignmentPanel ap2)
58 return _aps.contains(ap2.av.getSequenceSetId());
61 public boolean isUsedforaligment(AlignmentPanel ap2)
64 return (_alignwith != null) && _alignwith.contains(ap2);
67 public boolean isUsedforcolourby(AlignmentPanel ap2)
69 return (_colourwith != null) && _colourwith.contains(ap2);
74 * @return TRUE if the view is NOT being coloured by the alignment colours.
76 public boolean isColouredByViewer()
78 return !getBinding().isColourBySequence();
81 public String getViewId()
85 viewId = System.currentTimeMillis() + "." + this.hashCode();
90 protected void setViewId(String viewId)
95 public abstract String getStateInfo();
97 protected void buildActionMenu()
99 if (_alignwith == null)
101 _alignwith = new Vector<AlignmentPanel>();
103 if (_alignwith.size() == 0 && ap != null)
108 for (Component c : viewerActionMenu.getMenuComponents())
110 if (c != alignStructs)
112 viewerActionMenu.remove((JMenuItem) c);
117 public AlignmentPanel getAlignmentPanel()
122 protected void setAlignmentPanel(AlignmentPanel alp)
127 public AlignmentPanel[] getAllAlignmentPanels()
129 AlignmentPanel[] t, list = new AlignmentPanel[0];
130 for (String setid : _aps)
132 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
135 t = new AlignmentPanel[list.length + panels.length];
136 System.arraycopy(list, 0, t, 0, list.length);
137 System.arraycopy(panels, 0, t, list.length, panels.length);
146 * set the primary alignmentPanel reference and add another alignPanel to the
147 * list of ones to use for colouring and aligning
151 public void addAlignmentPanel(AlignmentPanel nap)
153 if (getAlignmentPanel() == null)
155 setAlignmentPanel(nap);
157 if (!_aps.contains(nap.av.getSequenceSetId()))
159 _aps.add(nap.av.getSequenceSetId());
164 * remove any references held to the given alignment panel
168 public void removeAlignmentPanel(AlignmentPanel nap)
172 _alignwith.remove(nap);
173 _colourwith.remove(nap);
174 if (getAlignmentPanel() == nap)
176 setAlignmentPanel(null);
177 for (AlignmentPanel aps : getAllAlignmentPanels())
181 setAlignmentPanel(aps);
186 } catch (Exception ex)
189 if (getAlignmentPanel() != null)
195 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
197 addAlignmentPanel(nap);
198 if (!_alignwith.contains(nap))
204 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
206 if (_alignwith.contains(nap))
208 _alignwith.remove(nap);
212 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap, boolean enableColourBySeq)
214 useAlignmentPanelForColourbyseq(nap);
215 getBinding().setColourBySequence(enableColourBySeq);
216 seqColour.setSelected(enableColourBySeq);
217 viewerColour.setSelected(!enableColourBySeq);
220 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
222 addAlignmentPanel(nap);
223 if (!_colourwith.contains(nap))
225 _colourwith.add(nap);
229 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
231 if (_colourwith.contains(nap))
233 _colourwith.remove(nap);
237 public abstract ViewerType getViewerType();
239 protected abstract AAStructureBindingModel getBindingModel();
242 * add a new structure (with associated sequences and chains) to this viewer,
243 * retrieving it if necessary first.
249 * if true, new structure(s) will be aligned using associated
253 protected void addStructure(final PDBEntry pdbentry, final SequenceI[] seqs, final String[] chains,
254 final boolean align, final IProgressIndicator alignFrame)
256 if (pdbentry.getFile() == null)
258 if (worker != null && worker.isAlive())
260 // a retrieval is in progress, wait around and add ourselves to the
262 new Thread(new Runnable()
266 while (worker != null && worker.isAlive() && _started)
270 Thread.sleep(100 + ((int) Math.random() * 100));
272 } catch (Exception e)
276 // and call ourselves again.
277 addStructure(pdbentry, seqs, chains, align, alignFrame);
283 // otherwise, start adding the structure.
284 getBindingModel().addSequenceAndChain(new PDBEntry[] { pdbentry },
285 new SequenceI[][] { seqs }, new String[][] { chains });
286 addingStructures = true;
288 alignAddedStructures = align;
289 worker = new Thread(this);
295 * Presents a dialog with the option to add an align a structure to an
296 * existing structure view
300 * @return YES, NO or CANCEL JOptionPane code
302 protected int chooseAlignStructureToViewer(String pdbId, StructureViewerBase view)
304 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
305 MessageManager.formatMessage("label.add_pdbentry_to_view",
307 { pdbId, view.getTitle() }), MessageManager
308 .getString("label.align_to_existing_structure_view"),
309 JOptionPane.YES_NO_CANCEL_OPTION);
313 protected abstract boolean hasPdbId(String pdbId);
315 protected abstract List<StructureViewerBase> getViewersFor(
319 * Check for any existing views involving this alignment and give user the
320 * option to add and align this molecule to one of them
327 * @return true if user adds to a view, or cancels entirely, else false
329 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
330 String[] chains, final AlignmentPanel apanel, String pdbId)
332 for (StructureViewerBase view : getViewersFor(apanel))
334 // TODO: highlight the view somehow
336 * JAL-1742 exclude view with this structure already mapped (don't offer
337 * to align chain B to chain A of the same structure)
339 if (view.hasPdbId(pdbId))
343 int option = chooseAlignStructureToViewer(pdbId, view);
344 if (option == JOptionPane.CANCEL_OPTION)
348 else if (option == JOptionPane.YES_OPTION)
350 view.useAlignmentPanelForSuperposition(apanel);
351 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
356 // NO_OPTION - offer the next viewer if any
361 * nothing offered and selected
367 * Adds mappings for the given sequences to an already opened PDB structure,
368 * and updates any viewers that have the PDB file
375 protected void addSequenceMappingsToStructure(SequenceI[] seq,
376 String[] chains, final AlignmentPanel apanel, String pdbFilename)
378 // TODO : Fix multiple seq to one chain issue here.
380 * create the mappings
382 apanel.getStructureSelectionManager().setMapping(seq, chains, pdbFilename,
383 AppletFormatAdapter.FILE);
386 * alert the FeatureRenderer to show new (PDB RESNUM) features
388 if (apanel.getSeqPanel().seqCanvas.fr != null)
390 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
391 apanel.paintAlignment(true);
395 * add the sequences to any other viewers (of the same type) for this pdb
398 // JBPNOTE: this looks like a binding routine, rather than a gui routine
399 for (StructureViewerBase viewer : getViewersFor(null))
401 AAStructureBindingModel bindingModel = viewer.getBindingModel();
402 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
404 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
406 bindingModel.addSequence(pe, seq);
407 viewer.addAlignmentPanel(apanel);
409 * add it to the set of alignments used for colouring structure by
412 viewer.useAlignmentPanelForColourbyseq(apanel);
413 viewer.buildActionMenu();
414 apanel.getStructureSelectionManager().sequenceColoursChanged(apanel);
422 * Check if the PDB file is already loaded, if so offer to add it to the
429 * @return true if the user chooses to add to a viewer, or to cancel entirely
431 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
432 final AlignmentPanel apanel, String pdbId)
434 boolean finished = false;
435 String alreadyMapped = apanel.getStructureSelectionManager()
436 .alreadyMappedToFile(pdbId);
438 if (alreadyMapped != null)
441 * the PDB file is already loaded
443 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
444 MessageManager.formatMessage(
445 "label.pdb_entry_is_already_displayed", new Object[]
446 { pdbId }), MessageManager.formatMessage(
447 "label.map_sequences_to_visible_window", new Object[]
448 { pdbId }), JOptionPane.YES_NO_CANCEL_OPTION);
449 if (option == JOptionPane.CANCEL_OPTION)
453 else if (option == JOptionPane.YES_OPTION)
455 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);