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.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.bin.Cache;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.StructureViewer.ViewerType;
32 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
33 import jalview.io.DataSourceType;
34 import jalview.io.JalviewFileChooser;
35 import jalview.io.JalviewFileView;
36 import jalview.jbgui.GStructureViewer;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.schemes.ColourSchemes;
39 import jalview.structure.StructureMapping;
40 import jalview.structures.models.AAStructureBindingModel;
41 import jalview.util.MessageManager;
43 import java.awt.Color;
44 import java.awt.Component;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
49 import java.io.BufferedReader;
51 import java.io.FileOutputStream;
52 import java.io.FileReader;
53 import java.io.IOException;
54 import java.io.PrintWriter;
55 import java.util.ArrayList;
56 import java.util.List;
57 import java.util.Vector;
59 import javax.swing.ButtonGroup;
60 import javax.swing.JCheckBoxMenuItem;
61 import javax.swing.JColorChooser;
62 import javax.swing.JMenu;
63 import javax.swing.JMenuItem;
64 import javax.swing.JRadioButtonMenuItem;
65 import javax.swing.event.MenuEvent;
66 import javax.swing.event.MenuListener;
69 * Base class with common functionality for JMol, Chimera or other structure
75 public abstract class StructureViewerBase extends GStructureViewer
76 implements Runnable, ViewSetProvider
79 * names for colour options (additional to Jalview colour schemes)
83 BySequence, ByChain, ChargeCysteine, ByViewer
87 * list of sequenceSet ids associated with the view
89 protected List<String> _aps = new ArrayList<>();
92 * list of alignment panels to use for superposition
94 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
97 * list of alignment panels that are used for colouring structures by aligned
100 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
102 private String viewId = null;
104 private AlignmentPanel ap;
106 protected boolean alignAddedStructures = false;
108 protected volatile boolean _started = false;
110 protected volatile boolean addingStructures = false;
112 protected Thread worker = null;
114 protected boolean allChainsSelected = false;
116 protected JMenu viewSelectionMenu;
119 * set after sequence colouring has been applied for this structure viewer.
120 * used to determine if the final sequence/structure mapping has been
123 protected volatile boolean seqColoursApplied = false;
126 * Default constructor
128 public StructureViewerBase()
134 * @return true if added structures should be aligned to existing one(s)
137 public boolean isAlignAddedStructures()
139 return alignAddedStructures;
145 * if added structures should be aligned to existing one(s)
148 public void setAlignAddedStructures(boolean alignAdded)
150 alignAddedStructures = alignAdded;
156 * @return true if this Jmol instance is linked with the given alignPanel
158 public boolean isLinkedWith(AlignmentPanel ap2)
160 return _aps.contains(ap2.av.getSequenceSetId());
163 public boolean isUsedforaligment(AlignmentPanel ap2)
166 return (_alignwith != null) && _alignwith.contains(ap2);
170 public boolean isUsedforcolourby(AlignmentViewPanel avp)
172 return (_colourwith != null) && _colourwith.contains(avp);
177 * @return TRUE if the view is NOT being coloured by the alignment colours.
179 public boolean isColouredByViewer()
181 return !getBinding().isColourBySequence();
184 public String getViewId()
188 viewId = System.currentTimeMillis() + "." + this.hashCode();
193 protected void setViewId(String viewId)
195 this.viewId = viewId;
198 public abstract String getStateInfo();
200 protected void buildActionMenu()
202 if (_alignwith == null)
204 _alignwith = new Vector<>();
206 if (_alignwith.size() == 0 && ap != null)
211 for (Component c : viewerActionMenu.getMenuComponents())
213 if (c != alignStructs)
215 viewerActionMenu.remove((JMenuItem) c);
220 public AlignmentPanel getAlignmentPanel()
225 protected void setAlignmentPanel(AlignmentPanel alp)
231 public AlignmentPanel[] getAllAlignmentPanels()
233 AlignmentPanel[] t, list = new AlignmentPanel[0];
234 for (String setid : _aps)
236 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
239 t = new AlignmentPanel[list.length + panels.length];
240 System.arraycopy(list, 0, t, 0, list.length);
241 System.arraycopy(panels, 0, t, list.length, panels.length);
250 * set the primary alignmentPanel reference and add another alignPanel to the
251 * list of ones to use for colouring and aligning
255 public void addAlignmentPanel(AlignmentPanel nap)
257 if (getAlignmentPanel() == null)
259 setAlignmentPanel(nap);
261 if (!_aps.contains(nap.av.getSequenceSetId()))
263 _aps.add(nap.av.getSequenceSetId());
268 * remove any references held to the given alignment panel
272 public void removeAlignmentPanel(AlignmentPanel nap)
276 _alignwith.remove(nap);
277 _colourwith.remove(nap);
278 if (getAlignmentPanel() == nap)
280 setAlignmentPanel(null);
281 for (AlignmentPanel aps : getAllAlignmentPanels())
285 setAlignmentPanel(aps);
290 } catch (Exception ex)
293 if (getAlignmentPanel() != null)
299 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
301 addAlignmentPanel(nap);
302 if (!_alignwith.contains(nap))
308 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
310 if (_alignwith.contains(nap))
312 _alignwith.remove(nap);
316 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
317 boolean enableColourBySeq)
319 useAlignmentPanelForColourbyseq(nap);
320 getBinding().setColourBySequence(enableColourBySeq);
321 seqColour.setSelected(enableColourBySeq);
322 viewerColour.setSelected(!enableColourBySeq);
325 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
327 addAlignmentPanel(nap);
328 if (!_colourwith.contains(nap))
330 _colourwith.add(nap);
334 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
336 if (_colourwith.contains(nap))
338 _colourwith.remove(nap);
342 public abstract ViewerType getViewerType();
344 protected abstract IProgressIndicator getIProgressIndicator();
347 * add a new structure (with associated sequences and chains) to this viewer,
348 * retrieving it if necessary first.
354 * if true, new structure(s) will be aligned using associated
358 protected void addStructure(final PDBEntry pdbentry,
359 final SequenceI[] seqs, final String[] chains,
360 final IProgressIndicator alignFrame)
362 if (pdbentry.getFile() == null)
364 if (worker != null && worker.isAlive())
366 // a retrieval is in progress, wait around and add ourselves to the
368 new Thread(new Runnable()
373 while (worker != null && worker.isAlive() && _started)
377 Thread.sleep(100 + ((int) Math.random() * 100));
379 } catch (Exception e)
383 // and call ourselves again.
384 addStructure(pdbentry, seqs, chains, alignFrame);
390 // otherwise, start adding the structure.
391 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
393 { seqs }, new String[][] { chains });
394 addingStructures = true;
396 worker = new Thread(this);
401 protected boolean hasPdbId(String pdbId)
403 return getBinding().hasPdbId(pdbId);
407 * Returns a list of any viewer of the instantiated type. The list is
408 * restricted to those linked to the given alignment panel if it is not null.
410 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
412 return Desktop.instance.getStructureViewers(alp, this.getClass());
416 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
417 String[] chains, final AlignmentViewPanel apanel, String pdbId)
420 * JAL-1742 exclude view with this structure already mapped (don't offer
421 * to align chain B to chain A of the same structure); code may defend
422 * against this possibility before we reach here
428 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error if this
430 useAlignmentPanelForSuperposition(alignPanel);
431 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
435 * Adds mappings for the given sequences to an already opened PDB structure,
436 * and updates any viewers that have the PDB file
443 public void addSequenceMappingsToStructure(SequenceI[] seq,
444 String[] chains, final AlignmentViewPanel alpanel,
447 AlignmentPanel apanel = (AlignmentPanel) alpanel;
449 // TODO : Fix multiple seq to one chain issue here.
451 * create the mappings
453 apanel.getStructureSelectionManager().setMapping(seq, chains,
454 pdbFilename, DataSourceType.FILE, getIProgressIndicator());
457 * alert the FeatureRenderer to show new (PDB RESNUM) features
459 if (apanel.getSeqPanel().seqCanvas.fr != null)
461 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
462 // note - we don't do a refresh for structure here because we do it
463 // explicitly for all panels later on
464 apanel.paintAlignment(true, false);
468 * add the sequences to any other viewers (of the same type) for this pdb
471 // JBPNOTE: this looks like a binding routine, rather than a gui routine
472 for (StructureViewerBase viewer : getViewersFor(null))
474 AAStructureBindingModel bindingModel = viewer.getBinding();
475 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
477 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
479 bindingModel.addSequence(pe, seq);
480 viewer.addAlignmentPanel(apanel);
482 * add it to the set of alignments used for colouring structure by
485 viewer.useAlignmentPanelForColourbyseq(apanel);
486 viewer.buildActionMenu();
487 apanel.getStructureSelectionManager()
488 .sequenceColoursChanged(apanel);
496 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
497 final AlignmentViewPanel apanel, String pdbId)
499 String alreadyMapped = apanel.getStructureSelectionManager()
500 .alreadyMappedToFile(pdbId);
502 if (alreadyMapped == null)
507 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
511 void setChainMenuItems(List<String> chainNames)
513 chainMenu.removeAll();
514 if (chainNames == null || chainNames.isEmpty())
518 JMenuItem menuItem = new JMenuItem(
519 MessageManager.getString("label.all"));
520 menuItem.addActionListener(new ActionListener()
523 public void actionPerformed(ActionEvent evt)
525 allChainsSelected = true;
526 for (int i = 0; i < chainMenu.getItemCount(); i++)
528 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
530 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
533 showSelectedChains();
534 allChainsSelected = false;
538 chainMenu.add(menuItem);
540 for (String chain : chainNames)
542 menuItem = new JCheckBoxMenuItem(chain, true);
543 menuItem.addItemListener(new ItemListener()
546 public void itemStateChanged(ItemEvent evt)
548 if (!allChainsSelected)
550 showSelectedChains();
555 chainMenu.add(menuItem);
559 abstract void showSelectedChains();
562 * Action on selecting one of Jalview's registered colour schemes
565 public void changeColour_actionPerformed(String colourSchemeName)
567 AlignmentI al = getAlignmentPanel().av.getAlignment();
568 ColourSchemeI cs = ColourSchemes.getInstance()
569 .getColourScheme(colourSchemeName, getAlignmentPanel().av, al,
571 getBinding().setJalviewColourScheme(cs);
575 * Builds the colour menu
577 protected void buildColourMenu()
579 colourMenu.removeAll();
580 AlignmentI al = getAlignmentPanel().av.getAlignment();
583 * add colour by sequence, by chain, by charge and cysteine
585 colourMenu.add(seqColour);
586 colourMenu.add(chainColour);
587 colourMenu.add(chargeColour);
588 chargeColour.setEnabled(!al.isNucleotide());
591 * add all 'simple' (per-residue) colour schemes registered to Jalview
593 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
597 * add 'colour by viewer' (menu item text is set in subclasses)
599 viewerColour.setSelected(false);
600 viewerColour.addActionListener(new ActionListener()
603 public void actionPerformed(ActionEvent actionEvent)
605 viewerColour_actionPerformed(actionEvent);
608 colourMenu.add(viewerColour);
611 * add 'set background colour'
613 JMenuItem backGround = new JMenuItem();
615 .setText(MessageManager.getString("action.background_colour"));
616 backGround.addActionListener(new ActionListener()
619 public void actionPerformed(ActionEvent actionEvent)
621 background_actionPerformed(actionEvent);
624 colourMenu.add(backGround);
627 * add colour buttons to a group so their selection is
628 * mutually exclusive (background colour is a separate option)
630 itemGroup.add(seqColour);
631 itemGroup.add(chainColour);
632 itemGroup.add(chargeColour);
633 itemGroup.add(viewerColour);
637 * Construct menu items
639 protected void initMenus()
641 AAStructureBindingModel binding = getBinding();
643 seqColour = new JRadioButtonMenuItem();
644 seqColour.setText(MessageManager.getString("action.by_sequence"));
645 seqColour.setName(ViewerColour.BySequence.name());
646 seqColour.setSelected(binding.isColourBySequence());
647 seqColour.addActionListener(new ActionListener()
650 public void actionPerformed(ActionEvent actionEvent)
652 seqColour_actionPerformed(actionEvent);
656 chainColour = new JRadioButtonMenuItem();
657 chainColour.setText(MessageManager.getString("action.by_chain"));
658 chainColour.setName(ViewerColour.ByChain.name());
659 chainColour.addActionListener(new ActionListener()
662 public void actionPerformed(ActionEvent actionEvent)
664 chainColour_actionPerformed(actionEvent);
668 chargeColour = new JRadioButtonMenuItem();
669 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
670 chargeColour.setName(ViewerColour.ChargeCysteine.name());
671 chargeColour.addActionListener(new ActionListener()
674 public void actionPerformed(ActionEvent actionEvent)
676 chargeColour_actionPerformed(actionEvent);
680 viewerColour = new JRadioButtonMenuItem();
681 // text is set in overrides of this method
682 viewerColour.setName(ViewerColour.ByViewer.name());
683 viewerColour.setSelected(!binding.isColourBySequence());
685 if (_colourwith == null)
687 _colourwith = new Vector<>();
689 if (_alignwith == null)
691 _alignwith = new Vector<>();
694 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
695 MessageManager.getString("label.colour_by"), this, _colourwith,
699 public void itemStateChanged(ItemEvent e)
701 if (!seqColour.isSelected())
707 // update the Chimera display now.
708 seqColour_actionPerformed(null);
712 viewMenu.add(seqColourBy);
714 showAlignmentOnly = new JCheckBoxMenuItem(
715 MessageManager.getString("label.show_alignment_only"));
716 showAlignmentOnly.addActionListener(new ActionListener()
719 public void actionPerformed(ActionEvent e)
721 hideHiddenRegions.setEnabled(showAlignmentOnly.isSelected());
722 getBinding().setShowAlignmentOnly(showAlignmentOnly.isSelected());
723 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
727 viewMenu.add(showAlignmentOnly);
729 hideHiddenRegions = new JCheckBoxMenuItem(
730 MessageManager.getString("label.hide_hidden_regions"));
731 hideHiddenRegions.setEnabled(false);
732 hideHiddenRegions.addActionListener(new ActionListener()
735 public void actionPerformed(ActionEvent e)
737 getBinding().setHideHiddenRegions(hideHiddenRegions.isSelected());
738 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
742 viewMenu.add(hideHiddenRegions);
744 final ItemListener handler = new ItemListener()
747 public void itemStateChanged(ItemEvent e)
749 alignStructs.setEnabled(!_alignwith.isEmpty());
750 alignStructs.setToolTipText(MessageManager.formatMessage(
751 "label.align_structures_using_linked_alignment_views",
755 viewSelectionMenu = new ViewSelectionMenu(
756 MessageManager.getString("label.superpose_with"), this,
757 _alignwith, handler);
758 handler.itemStateChanged(null);
759 viewerActionMenu.add(viewSelectionMenu, 0);
760 viewerActionMenu.addMenuListener(new MenuListener()
763 public void menuSelected(MenuEvent e)
765 handler.itemStateChanged(null);
769 public void menuDeselected(MenuEvent e)
774 public void menuCanceled(MenuEvent e)
783 public void setJalviewColourScheme(ColourSchemeI cs)
785 getBinding().setJalviewColourScheme(cs);
789 * Sends commands to the structure viewer to superimpose structures based on
790 * currently associated alignments. May optionally return an error message for
794 protected String alignStructs_actionPerformed(ActionEvent actionEvent)
796 return alignStructs_withAllAlignPanels();
799 protected String alignStructs_withAllAlignPanels()
801 if (getAlignmentPanel() == null)
806 if (_alignwith.size() == 0)
808 _alignwith.add(getAlignmentPanel());
814 AlignmentI[] als = new Alignment[_alignwith.size()];
815 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
816 int[] alm = new int[_alignwith.size()];
819 for (AlignmentViewPanel alignPanel : _alignwith)
821 AlignViewportI av = alignPanel.getAlignViewport();
822 als[a] = av.getAlignment();
824 alc[a++] = av.getAlignment().getHiddenColumns();
826 reply = getBinding().superposeStructures(als, alm, alc);
829 String text = MessageManager
830 .formatMessage("error.superposition_failed", reply);
831 statusBar.setText(text);
833 } catch (Exception e)
835 StringBuffer sp = new StringBuffer();
836 for (AlignmentViewPanel avp : _alignwith)
839 "'" + ((AlignmentPanel) avp).alignFrame.getTitle() + "' ");
841 Cache.log.info("Couldn't align structures with the " + sp.toString()
842 + "associated alignment panels.", e);
848 public void background_actionPerformed(ActionEvent actionEvent)
850 Color col = JColorChooser.showDialog(this,
851 MessageManager.getString("label.select_background_colour"),
855 getBinding().setBackgroundColour(col);
860 public void viewerColour_actionPerformed(ActionEvent actionEvent)
862 if (viewerColour.isSelected())
864 // disable automatic sequence colouring.
865 getBinding().setColourBySequence(false);
870 public void chainColour_actionPerformed(ActionEvent actionEvent)
872 chainColour.setSelected(true);
873 getBinding().colourByChain();
877 public void chargeColour_actionPerformed(ActionEvent actionEvent)
879 chargeColour.setSelected(true);
880 getBinding().colourByCharge();
884 public void seqColour_actionPerformed(ActionEvent actionEvent)
886 AAStructureBindingModel binding = getBinding();
887 binding.setColourBySequence(seqColour.isSelected());
888 if (_colourwith == null)
890 _colourwith = new Vector<>();
892 if (binding.isColourBySequence())
894 if (!binding.isLoadingFromArchive())
896 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
898 // Make the currently displayed alignment panel the associated view
899 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
902 // Set the colour using the current view for the associated alignframe
903 for (AlignmentViewPanel avp : _colourwith)
905 binding.colourBySequence(avp);
907 seqColoursApplied = true;
912 public void pdbFile_actionPerformed(ActionEvent actionEvent)
914 JalviewFileChooser chooser = new JalviewFileChooser(
915 Cache.getProperty("LAST_DIRECTORY"));
917 chooser.setFileView(new JalviewFileView());
918 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
919 chooser.setToolTipText(MessageManager.getString("action.save"));
921 int value = chooser.showSaveDialog(this);
923 if (value == JalviewFileChooser.APPROVE_OPTION)
925 BufferedReader in = null;
928 // TODO: cope with multiple PDB files in view
929 in = new BufferedReader(
930 new FileReader(getBinding().getStructureFiles()[0]));
931 File outFile = chooser.getSelectedFile();
933 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
935 while ((data = in.readLine()) != null)
937 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
943 } catch (Exception ex)
945 ex.printStackTrace();
953 } catch (IOException e)
963 public void viewMapping_actionPerformed(ActionEvent actionEvent)
965 CutAndPasteTransfer cap = new CutAndPasteTransfer();
968 cap.appendText(getBinding().printMappings());
969 } catch (OutOfMemoryError e)
972 "composing sequence-structure alignments for display in text box.",
977 Desktop.addInternalFrame(cap,
978 MessageManager.getString("label.pdb_sequence_mapping"), 550,
982 protected abstract String getViewerName();
985 * Configures the title and menu items of the viewer panel.
988 public void updateTitleAndMenus()
990 AAStructureBindingModel binding = getBinding();
991 if (binding.hasFileLoadingError())
996 setChainMenuItems(binding.getChainNames());
998 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1001 * enable 'Superpose with' if more than one mapped structure
1003 viewSelectionMenu.setEnabled(false);
1004 if (getBinding().getStructureFiles().length > 1
1005 && getBinding().getSequence().length > 1)
1007 viewSelectionMenu.setEnabled(true);
1011 * Show action menu if it has any enabled items
1013 viewerActionMenu.setVisible(false);
1014 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1016 if (viewerActionMenu.getItem(i).isEnabled())
1018 viewerActionMenu.setVisible(true);
1023 if (!binding.isLoadingFromArchive())
1025 seqColour_actionPerformed(null);
1030 public String toString()
1036 public boolean hasMapping()
1038 if (worker != null && (addingStructures || _started))
1042 if (getBinding() == null)
1044 if (_aps == null || _aps.size() == 0)
1046 // viewer has been closed, but we did at some point run.
1051 String[] pdbids = getBinding().getStructureFiles();
1057 for (String pdbid:pdbids) {
1058 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1059 if (sm!=null && sm.length>0 && sm[0]!=null) {
1063 // only return true if there is a mapping for every structure file we have loaded
1064 if (p == 0 || p != pdbids.length)
1068 // and that coloring has been applied
1069 return seqColoursApplied;
1073 public void raiseViewer()