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);
560 * Action on selecting one of Jalview's registered colour schemes
563 public void changeColour_actionPerformed(String colourSchemeName)
565 AlignmentI al = getAlignmentPanel().av.getAlignment();
566 ColourSchemeI cs = ColourSchemes.getInstance()
567 .getColourScheme(colourSchemeName, getAlignmentPanel().av, al,
569 getBinding().setJalviewColourScheme(cs);
573 * Builds the colour menu
575 protected void buildColourMenu()
577 colourMenu.removeAll();
578 AlignmentI al = getAlignmentPanel().av.getAlignment();
581 * add colour by sequence, by chain, by charge and cysteine
583 colourMenu.add(seqColour);
584 colourMenu.add(chainColour);
585 colourMenu.add(chargeColour);
586 chargeColour.setEnabled(!al.isNucleotide());
589 * add all 'simple' (per-residue) colour schemes registered to Jalview
591 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
595 * add 'colour by viewer' (menu item text is set in subclasses)
597 viewerColour.setSelected(false);
598 viewerColour.addActionListener(new ActionListener()
601 public void actionPerformed(ActionEvent actionEvent)
603 viewerColour_actionPerformed(actionEvent);
606 colourMenu.add(viewerColour);
609 * add 'set background colour'
611 JMenuItem backGround = new JMenuItem();
613 .setText(MessageManager.getString("action.background_colour"));
614 backGround.addActionListener(new ActionListener()
617 public void actionPerformed(ActionEvent actionEvent)
619 background_actionPerformed(actionEvent);
622 colourMenu.add(backGround);
625 * add colour buttons to a group so their selection is
626 * mutually exclusive (background colour is a separate option)
628 itemGroup.add(seqColour);
629 itemGroup.add(chainColour);
630 itemGroup.add(chargeColour);
631 itemGroup.add(viewerColour);
635 * Construct menu items
637 protected void initMenus()
639 AAStructureBindingModel binding = getBinding();
641 seqColour = new JRadioButtonMenuItem();
642 seqColour.setText(MessageManager.getString("action.by_sequence"));
643 seqColour.setName(ViewerColour.BySequence.name());
644 seqColour.setSelected(binding.isColourBySequence());
645 seqColour.addActionListener(new ActionListener()
648 public void actionPerformed(ActionEvent actionEvent)
650 seqColour_actionPerformed(actionEvent);
654 chainColour = new JRadioButtonMenuItem();
655 chainColour.setText(MessageManager.getString("action.by_chain"));
656 chainColour.setName(ViewerColour.ByChain.name());
657 chainColour.addActionListener(new ActionListener()
660 public void actionPerformed(ActionEvent actionEvent)
662 chainColour_actionPerformed(actionEvent);
666 chargeColour = new JRadioButtonMenuItem();
667 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
668 chargeColour.setName(ViewerColour.ChargeCysteine.name());
669 chargeColour.addActionListener(new ActionListener()
672 public void actionPerformed(ActionEvent actionEvent)
674 chargeColour_actionPerformed(actionEvent);
678 viewerColour = new JRadioButtonMenuItem();
679 // text is set in overrides of this method
680 viewerColour.setName(ViewerColour.ByViewer.name());
681 viewerColour.setSelected(!binding.isColourBySequence());
683 if (_colourwith == null)
685 _colourwith = new Vector<>();
687 if (_alignwith == null)
689 _alignwith = new Vector<>();
692 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
693 MessageManager.getString("label.colour_by"), this, _colourwith,
697 public void itemStateChanged(ItemEvent e)
699 if (!seqColour.isSelected())
705 // update the Chimera display now.
706 seqColour_actionPerformed(null);
710 viewMenu.add(seqColourBy);
712 showAlignmentOnly = new JCheckBoxMenuItem(
713 MessageManager.getString("label.show_alignment_only"));
714 showAlignmentOnly.addActionListener(new ActionListener()
717 public void actionPerformed(ActionEvent e)
719 hideHiddenRegions.setEnabled(showAlignmentOnly.isSelected());
720 getBinding().setShowAlignmentOnly(showAlignmentOnly.isSelected());
721 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
725 viewMenu.add(showAlignmentOnly);
727 hideHiddenRegions = new JCheckBoxMenuItem(
728 MessageManager.getString("label.hide_hidden_regions"));
729 hideHiddenRegions.setEnabled(false);
730 hideHiddenRegions.addActionListener(new ActionListener()
733 public void actionPerformed(ActionEvent e)
735 getBinding().setHideHiddenRegions(hideHiddenRegions.isSelected());
736 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),
740 viewMenu.add(hideHiddenRegions);
742 final ItemListener handler = new ItemListener()
745 public void itemStateChanged(ItemEvent e)
747 alignStructs.setEnabled(!_alignwith.isEmpty());
748 alignStructs.setToolTipText(MessageManager.formatMessage(
749 "label.align_structures_using_linked_alignment_views",
753 viewSelectionMenu = new ViewSelectionMenu(
754 MessageManager.getString("label.superpose_with"), this,
755 _alignwith, handler);
756 handler.itemStateChanged(null);
757 viewerActionMenu.add(viewSelectionMenu, 0);
758 viewerActionMenu.addMenuListener(new MenuListener()
761 public void menuSelected(MenuEvent e)
763 handler.itemStateChanged(null);
767 public void menuDeselected(MenuEvent e)
772 public void menuCanceled(MenuEvent e)
781 * Sends commands to the structure viewer to superimpose structures based on
782 * currently associated alignments. May optionally return an error message for
786 protected String alignStructs_actionPerformed(ActionEvent actionEvent)
788 return alignStructs_withAllAlignPanels();
791 protected String alignStructs_withAllAlignPanels()
793 if (getAlignmentPanel() == null)
798 if (_alignwith.size() == 0)
800 _alignwith.add(getAlignmentPanel());
806 AlignmentI[] als = new Alignment[_alignwith.size()];
807 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
808 int[] alm = new int[_alignwith.size()];
811 for (AlignmentViewPanel alignPanel : _alignwith)
813 AlignViewportI av = alignPanel.getAlignViewport();
814 als[a] = av.getAlignment();
816 alc[a++] = av.getAlignment().getHiddenColumns();
818 reply = getBinding().superposeStructures(als, alm, alc);
821 String text = MessageManager
822 .formatMessage("error.superposition_failed", reply);
823 statusBar.setText(text);
825 } catch (Exception e)
827 StringBuffer sp = new StringBuffer();
828 for (AlignmentViewPanel avp : _alignwith)
831 "'" + ((AlignmentPanel) avp).alignFrame.getTitle() + "' ");
833 Cache.log.info("Couldn't align structures with the " + sp.toString()
834 + "associated alignment panels.", e);
840 public void background_actionPerformed(ActionEvent actionEvent)
842 Color col = JColorChooser.showDialog(this,
843 MessageManager.getString("label.select_background_colour"),
847 getBinding().setBackgroundColour(col);
852 public void viewerColour_actionPerformed(ActionEvent actionEvent)
854 if (viewerColour.isSelected())
856 // disable automatic sequence colouring.
857 getBinding().setColourBySequence(false);
862 public void chainColour_actionPerformed(ActionEvent actionEvent)
864 chainColour.setSelected(true);
865 getBinding().colourByChain();
869 public void chargeColour_actionPerformed(ActionEvent actionEvent)
871 chargeColour.setSelected(true);
872 getBinding().colourByCharge();
876 public void seqColour_actionPerformed(ActionEvent actionEvent)
878 AAStructureBindingModel binding = getBinding();
879 binding.setColourBySequence(seqColour.isSelected());
880 if (_colourwith == null)
882 _colourwith = new Vector<>();
884 if (binding.isColourBySequence())
886 if (!binding.isLoadingFromArchive())
888 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
890 // Make the currently displayed alignment panel the associated view
891 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
894 // Set the colour using the current view for the associated alignframe
895 for (AlignmentViewPanel avp : _colourwith)
897 binding.updateStructureColours(avp);
899 seqColoursApplied = true;
904 public void pdbFile_actionPerformed(ActionEvent actionEvent)
906 JalviewFileChooser chooser = new JalviewFileChooser(
907 Cache.getProperty("LAST_DIRECTORY"));
909 chooser.setFileView(new JalviewFileView());
910 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
911 chooser.setToolTipText(MessageManager.getString("action.save"));
913 int value = chooser.showSaveDialog(this);
915 if (value == JalviewFileChooser.APPROVE_OPTION)
917 BufferedReader in = null;
920 // TODO: cope with multiple PDB files in view
921 in = new BufferedReader(
922 new FileReader(getBinding().getStructureFiles()[0]));
923 File outFile = chooser.getSelectedFile();
925 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
927 while ((data = in.readLine()) != null)
929 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
935 } catch (Exception ex)
937 ex.printStackTrace();
945 } catch (IOException e)
955 public void viewMapping_actionPerformed(ActionEvent actionEvent)
957 CutAndPasteTransfer cap = new CutAndPasteTransfer();
960 cap.appendText(getBinding().printMappings());
961 } catch (OutOfMemoryError e)
964 "composing sequence-structure alignments for display in text box.",
969 Desktop.addInternalFrame(cap,
970 MessageManager.getString("label.pdb_sequence_mapping"), 550,
974 protected abstract String getViewerName();
977 * Configures the title and menu items of the viewer panel.
980 public void updateTitleAndMenus()
982 AAStructureBindingModel binding = getBinding();
983 if (binding.hasFileLoadingError())
988 setChainMenuItems(binding.getChainNames());
990 this.setTitle(binding.getViewerTitle(getViewerName(), true));
993 * enable 'Superpose with' if more than one mapped structure
995 viewSelectionMenu.setEnabled(false);
996 if (getBinding().getStructureFiles().length > 1
997 && getBinding().getSequence().length > 1)
999 viewSelectionMenu.setEnabled(true);
1003 * Show action menu if it has any enabled items
1005 viewerActionMenu.setVisible(false);
1006 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1008 if (viewerActionMenu.getItem(i).isEnabled())
1010 viewerActionMenu.setVisible(true);
1015 if (!binding.isLoadingFromArchive())
1017 seqColour_actionPerformed(null);
1022 public String toString()
1028 public boolean hasMapping()
1030 if (worker != null && (addingStructures || _started))
1034 if (getBinding() == null)
1036 if (_aps == null || _aps.size() == 0)
1038 // viewer has been closed, but we did at some point run.
1043 String[] pdbids = getBinding().getStructureFiles();
1049 for (String pdbid:pdbids) {
1050 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1051 if (sm!=null && sm.length>0 && sm[0]!=null) {
1055 // only return true if there is a mapping for every structure file we have loaded
1056 if (p == 0 || p != pdbids.length)
1060 // and that coloring has been applied
1061 return seqColoursApplied;
1065 public void raiseViewer()
1071 public abstract AAStructureBindingModel getBinding();
1074 * Show only the selected chain(s) in the viewer
1076 protected void showSelectedChains()
1078 setSelectedChains();
1081 * refresh display without resizing - easier to see what changed
1083 getBinding().showStructures(getAlignmentPanel().getAlignViewport(),