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>();
37 * list of alignment panels to use for superposition
39 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
42 * list of alignment panels that are used for colouring structures by aligned
45 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
47 private String viewId = null;
49 private AlignmentPanel ap;
51 protected boolean alignAddedStructures = false;
53 protected boolean _started = false;
55 protected boolean addingStructures = false;
57 protected Thread worker = null;
62 * @return true if this Jmol instance is linked with the given alignPanel
64 public boolean isLinkedWith(AlignmentPanel ap2)
66 return _aps.contains(ap2.av.getSequenceSetId());
69 public boolean isUsedforaligment(AlignmentPanel ap2)
72 return (_alignwith != null) && _alignwith.contains(ap2);
75 public boolean isUsedforcolourby(AlignmentPanel ap2)
77 return (_colourwith != null) && _colourwith.contains(ap2);
82 * @return TRUE if the view is NOT being coloured by the alignment colours.
84 public boolean isColouredByViewer()
86 return !getBinding().isColourBySequence();
89 public String getViewId()
93 viewId = System.currentTimeMillis() + "." + this.hashCode();
98 protected void setViewId(String viewId)
100 this.viewId = viewId;
103 public abstract String getStateInfo();
105 protected void buildActionMenu()
107 if (_alignwith == null)
109 _alignwith = new Vector<AlignmentPanel>();
111 if (_alignwith.size() == 0 && ap != null)
116 for (Component c : viewerActionMenu.getMenuComponents())
118 if (c != alignStructs)
120 viewerActionMenu.remove((JMenuItem) c);
125 public AlignmentPanel getAlignmentPanel()
130 protected void setAlignmentPanel(AlignmentPanel alp)
135 public AlignmentPanel[] getAllAlignmentPanels()
137 AlignmentPanel[] t, list = new AlignmentPanel[0];
138 for (String setid : _aps)
140 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
143 t = new AlignmentPanel[list.length + panels.length];
144 System.arraycopy(list, 0, t, 0, list.length);
145 System.arraycopy(panels, 0, t, list.length, panels.length);
154 * set the primary alignmentPanel reference and add another alignPanel to the
155 * list of ones to use for colouring and aligning
159 public void addAlignmentPanel(AlignmentPanel nap)
161 if (getAlignmentPanel() == null)
163 setAlignmentPanel(nap);
165 if (!_aps.contains(nap.av.getSequenceSetId()))
167 _aps.add(nap.av.getSequenceSetId());
172 * remove any references held to the given alignment panel
176 public void removeAlignmentPanel(AlignmentPanel nap)
180 _alignwith.remove(nap);
181 _colourwith.remove(nap);
182 if (getAlignmentPanel() == nap)
184 setAlignmentPanel(null);
185 for (AlignmentPanel aps : getAllAlignmentPanels())
189 setAlignmentPanel(aps);
194 } catch (Exception ex)
197 if (getAlignmentPanel() != null)
203 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
205 addAlignmentPanel(nap);
206 if (!_alignwith.contains(nap))
212 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
214 if (_alignwith.contains(nap))
216 _alignwith.remove(nap);
220 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
221 boolean enableColourBySeq)
223 useAlignmentPanelForColourbyseq(nap);
224 getBinding().setColourBySequence(enableColourBySeq);
225 seqColour.setSelected(enableColourBySeq);
226 viewerColour.setSelected(!enableColourBySeq);
229 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
231 addAlignmentPanel(nap);
232 if (!_colourwith.contains(nap))
234 _colourwith.add(nap);
238 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
240 if (_colourwith.contains(nap))
242 _colourwith.remove(nap);
246 public abstract ViewerType getViewerType();
248 protected abstract AAStructureBindingModel getBindingModel();
251 * add a new structure (with associated sequences and chains) to this viewer,
252 * retrieving it if necessary first.
258 * if true, new structure(s) will be aligned using associated
262 protected void addStructure(final PDBEntry pdbentry,
263 final SequenceI[] seqs, final String[] chains,
264 final boolean align, final IProgressIndicator alignFrame)
266 if (pdbentry.getFile() == null)
268 if (worker != null && worker.isAlive())
270 // a retrieval is in progress, wait around and add ourselves to the
272 new Thread(new Runnable()
276 while (worker != null && worker.isAlive() && _started)
280 Thread.sleep(100 + ((int) Math.random() * 100));
282 } catch (Exception e)
286 // and call ourselves again.
287 addStructure(pdbentry, seqs, chains, align, alignFrame);
293 // otherwise, start adding the structure.
294 getBindingModel().addSequenceAndChain(new PDBEntry[] { pdbentry },
295 new SequenceI[][] { seqs }, new String[][] { chains });
296 addingStructures = true;
298 alignAddedStructures = align;
299 worker = new Thread(this);
305 * Presents a dialog with the option to add an align a structure to an
306 * existing structure view
310 * @return YES, NO or CANCEL JOptionPane code
312 protected int chooseAlignStructureToViewer(String pdbId,
313 StructureViewerBase view)
315 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
316 MessageManager.formatMessage("label.add_pdbentry_to_view",
317 new Object[] { pdbId, view.getTitle() }),
319 .getString("label.align_to_existing_structure_view"),
320 JOptionPane.YES_NO_CANCEL_OPTION);
324 protected abstract boolean hasPdbId(String pdbId);
326 protected abstract List<StructureViewerBase> getViewersFor(
330 * Check for any existing views involving this alignment and give user the
331 * option to add and align this molecule to one of them
338 * @return true if user adds to a view, or cancels entirely, else false
340 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
341 String[] chains, final AlignmentPanel apanel, String pdbId)
343 for (StructureViewerBase view : getViewersFor(apanel))
345 // TODO: highlight the view somehow
347 * JAL-1742 exclude view with this structure already mapped (don't offer
348 * to align chain B to chain A of the same structure)
350 if (view.hasPdbId(pdbId))
354 int option = chooseAlignStructureToViewer(pdbId, view);
355 if (option == JOptionPane.CANCEL_OPTION)
359 else if (option == JOptionPane.YES_OPTION)
361 view.useAlignmentPanelForSuperposition(apanel);
362 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
367 // NO_OPTION - offer the next viewer if any
372 * nothing offered and selected
378 * Adds mappings for the given sequences to an already opened PDB structure,
379 * and updates any viewers that have the PDB file
386 protected void addSequenceMappingsToStructure(SequenceI[] seq,
387 String[] chains, final AlignmentPanel apanel, String pdbFilename)
389 // TODO : Fix multiple seq to one chain issue here.
391 * create the mappings
393 apanel.getStructureSelectionManager().setMapping(seq, chains,
394 pdbFilename, AppletFormatAdapter.FILE);
397 * alert the FeatureRenderer to show new (PDB RESNUM) features
399 if (apanel.getSeqPanel().seqCanvas.fr != null)
401 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
402 apanel.paintAlignment(true);
406 * add the sequences to any other viewers (of the same type) for this pdb
409 // JBPNOTE: this looks like a binding routine, rather than a gui routine
410 for (StructureViewerBase viewer : getViewersFor(null))
412 AAStructureBindingModel bindingModel = viewer.getBindingModel();
413 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
415 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
417 bindingModel.addSequence(pe, seq);
418 viewer.addAlignmentPanel(apanel);
420 * add it to the set of alignments used for colouring structure by
423 viewer.useAlignmentPanelForColourbyseq(apanel);
424 viewer.buildActionMenu();
425 apanel.getStructureSelectionManager().sequenceColoursChanged(
434 * Check if the PDB file is already loaded, if so offer to add it to the
441 * @return true if the user chooses to add to a viewer, or to cancel entirely
443 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
444 final AlignmentPanel apanel, String pdbId)
446 boolean finished = false;
447 String alreadyMapped = apanel.getStructureSelectionManager()
448 .alreadyMappedToFile(pdbId);
450 if (alreadyMapped != null)
453 * the PDB file is already loaded
455 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
456 MessageManager.formatMessage(
457 "label.pdb_entry_is_already_displayed",
458 new Object[] { pdbId }), MessageManager
460 "label.map_sequences_to_visible_window",
461 new Object[] { pdbId }),
462 JOptionPane.YES_NO_CANCEL_OPTION);
463 if (option == JOptionPane.CANCEL_OPTION)
467 else if (option == JOptionPane.YES_OPTION)
469 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);