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.AlignmentViewPanel;
24 import jalview.bin.Cache;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.HiddenColumns;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.gui.StructureViewer.ViewerType;
31 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
32 import jalview.io.DataSourceType;
33 import jalview.io.JalviewFileChooser;
34 import jalview.io.JalviewFileView;
35 import jalview.jbgui.GStructureViewer;
36 import jalview.schemes.ColourSchemeI;
37 import jalview.schemes.ColourSchemes;
38 import jalview.structure.StructureMapping;
39 import jalview.structures.models.AAStructureBindingModel;
40 import jalview.structures.models.AAStructureBindingModel.ColourBy;
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<AlignmentPanel> _alignwith = new Vector<>();
97 * list of alignment panels that are used for colouring structures by aligned
100 protected Vector<AlignmentPanel> _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);
169 public boolean isUsedForColourBy(Object o)
171 return (_colourwith != null) && _colourwith.contains(o);
176 * @return TRUE if the view is NOT being coloured by the alignment colours.
178 public boolean isColouredByViewer()
180 return !getBinding().isColourBySequence();
183 public String getViewId()
187 viewId = System.currentTimeMillis() + "." + this.hashCode();
192 protected void setViewId(String viewId)
194 this.viewId = viewId;
197 public abstract String getStateInfo();
199 protected void buildActionMenu()
201 if (_alignwith == null)
203 _alignwith = new Vector<>();
205 if (_alignwith.size() == 0 && ap != null)
210 for (Component c : viewerActionMenu.getMenuComponents())
212 if (c != alignStructs)
214 viewerActionMenu.remove((JMenuItem) c);
219 public AlignmentPanel getAlignmentPanel()
224 protected void setAlignmentPanel(AlignmentPanel alp)
230 public AlignmentPanel[] getAllAlignmentPanels()
232 AlignmentPanel[] t, list = new AlignmentPanel[0];
233 for (String setid : _aps)
235 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
238 t = new AlignmentPanel[list.length + panels.length];
239 System.arraycopy(list, 0, t, 0, list.length);
240 System.arraycopy(panels, 0, t, list.length, panels.length);
249 * set the primary alignmentPanel reference and add another alignPanel to the
250 * list of ones to use for colouring and aligning
254 public void addAlignmentPanel(AlignmentPanel nap)
256 if (getAlignmentPanel() == null)
258 setAlignmentPanel(nap);
260 if (!_aps.contains(nap.av.getSequenceSetId()))
262 _aps.add(nap.av.getSequenceSetId());
267 * remove any references held to the given alignment panel
271 public void removeAlignmentPanel(AlignmentPanel nap)
275 _alignwith.remove(nap);
276 _colourwith.remove(nap);
277 if (getAlignmentPanel() == nap)
279 setAlignmentPanel(null);
280 for (AlignmentPanel aps : getAllAlignmentPanels())
284 setAlignmentPanel(aps);
289 } catch (Exception ex)
292 if (getAlignmentPanel() != null)
298 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
300 addAlignmentPanel(nap);
301 if (!_alignwith.contains(nap))
307 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
309 if (_alignwith.contains(nap))
311 _alignwith.remove(nap);
315 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
316 boolean enableColourBySeq)
318 useAlignmentPanelForColourbyseq(nap);
319 seqColour.setSelected(enableColourBySeq);
320 viewerColour.setSelected(!enableColourBySeq);
322 getBinding().setColourBy(enableColourBySeq ? ColourBy.Sequence : ColourBy.Viewer);
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, al, null);
570 getBinding().setJalviewColourScheme(cs, getAlignmentPanel());
574 * Builds the colour menu
576 protected void buildColourMenu()
578 colourMenu.removeAll();
579 AlignmentI al = getAlignmentPanel().av.getAlignment();
581 colourMenu.addSeparator();
584 * add colour by sequence, by chain, by charge and cysteine
586 colourMenu.add(seqColour);
587 colourMenu.add(chainColour);
588 colourMenu.add(chargeColour);
589 chargeColour.setEnabled(!al.isNucleotide());
592 * add all colour schemes registered to Jalview
594 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
598 * add 'colour by viewer' (menu item text is set in subclasses)
600 viewerColour.setSelected(false);
601 viewerColour.addActionListener(new ActionListener()
604 public void actionPerformed(ActionEvent actionEvent)
606 viewerColour_actionPerformed(actionEvent);
609 colourMenu.add(viewerColour);
612 * add 'set background colour'
614 JMenuItem backGround = new JMenuItem();
616 .setText(MessageManager.getString("action.background_colour"));
617 backGround.addActionListener(new ActionListener()
620 public void actionPerformed(ActionEvent actionEvent)
622 background_actionPerformed(actionEvent);
625 colourMenu.add(backGround);
628 * add colour buttons to a group so their selection is
629 * mutually exclusive (background colour is a separate option)
631 itemGroup.add(seqColour);
632 itemGroup.add(chainColour);
633 itemGroup.add(chargeColour);
634 itemGroup.add(viewerColour);
638 * Construct menu items
640 protected void initMenus()
642 AAStructureBindingModel binding = getBinding();
644 seqColour = new JRadioButtonMenuItem();
645 seqColour.setText(MessageManager.getString("action.by_sequence"));
646 seqColour.setName(ViewerColour.BySequence.name());
647 seqColour.setSelected(binding.isColourBySequence());
648 seqColour.addActionListener(new ActionListener()
651 public void actionPerformed(ActionEvent actionEvent)
653 seqColour_actionPerformed(actionEvent);
657 chainColour = new JRadioButtonMenuItem();
658 chainColour.setText(MessageManager.getString("action.by_chain"));
659 chainColour.setName(ViewerColour.ByChain.name());
660 chainColour.addActionListener(new ActionListener()
663 public void actionPerformed(ActionEvent actionEvent)
665 chainColour_actionPerformed(actionEvent);
669 chargeColour = new JRadioButtonMenuItem();
670 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
671 chargeColour.setName(ViewerColour.ChargeCysteine.name());
672 chargeColour.addActionListener(new ActionListener()
675 public void actionPerformed(ActionEvent actionEvent)
677 chargeColour_actionPerformed(actionEvent);
681 viewerColour = new JRadioButtonMenuItem();
682 // text is set in overrides of this method
683 viewerColour.setName(ViewerColour.ByViewer.name());
684 viewerColour.setSelected(!binding.isColourBySequence());
686 if (_colourwith == null)
688 _colourwith = new Vector<>();
690 if (_alignwith == null)
692 _alignwith = new Vector<>();
695 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
696 MessageManager.getString("label.colour_by"), this, _colourwith,
700 public void itemStateChanged(ItemEvent e)
702 if (!seqColour.isSelected())
708 // update the Chimera display now.
709 seqColour_actionPerformed(null);
713 viewMenu.add(seqColourBy);
715 final ItemListener handler = new ItemListener()
718 public void itemStateChanged(ItemEvent e)
720 alignStructs.setEnabled(!_alignwith.isEmpty());
721 alignStructs.setToolTipText(MessageManager.formatMessage(
722 "label.align_structures_using_linked_alignment_views",
726 viewSelectionMenu = new ViewSelectionMenu(
727 MessageManager.getString("label.superpose_with"), this,
728 _alignwith, handler);
729 handler.itemStateChanged(null);
730 viewerActionMenu.add(viewSelectionMenu, 0);
731 viewerActionMenu.addMenuListener(new MenuListener()
734 public void menuSelected(MenuEvent e)
736 handler.itemStateChanged(null);
740 public void menuDeselected(MenuEvent e)
745 public void menuCanceled(MenuEvent e)
754 * Sends commands to the structure viewer to superimpose structures based on
755 * currently associated alignments. May optionally return an error message for
759 protected String alignStructs_actionPerformed(ActionEvent actionEvent)
761 return alignStructs_withAllAlignPanels();
764 protected String alignStructs_withAllAlignPanels()
766 if (getAlignmentPanel() == null)
771 if (_alignwith.size() == 0)
773 _alignwith.add(getAlignmentPanel());
779 AlignmentI[] als = new Alignment[_alignwith.size()];
780 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
781 int[] alm = new int[_alignwith.size()];
784 for (AlignmentPanel alignPanel : _alignwith)
786 als[a] = alignPanel.av.getAlignment();
788 alc[a++] = alignPanel.av.getAlignment().getHiddenColumns();
790 reply = getBinding().superposeStructures(als, alm, alc);
793 String text = MessageManager
794 .formatMessage("error.superposition_failed", reply);
795 statusBar.setText(text);
797 } catch (Exception e)
799 StringBuffer sp = new StringBuffer();
800 for (AlignmentPanel alignPanel : _alignwith)
802 sp.append("'" + alignPanel.alignFrame.getTitle() + "' ");
804 Cache.log.info("Couldn't align structures with the " + sp.toString()
805 + "associated alignment panels.", e);
811 public void background_actionPerformed(ActionEvent actionEvent)
813 Color col = JColorChooser.showDialog(this,
814 MessageManager.getString("label.select_background_colour"),
818 getBinding().setBackgroundColour(col);
823 public void viewerColour_actionPerformed(ActionEvent actionEvent)
825 if (viewerColour.isSelected())
827 // disable automatic sequence colouring.
828 getBinding().setColourBy(ColourBy.Viewer);
833 public void chainColour_actionPerformed(ActionEvent actionEvent)
835 chainColour.setSelected(true);
836 getBinding().colourByChain();
840 public void chargeColour_actionPerformed(ActionEvent actionEvent)
842 chargeColour.setSelected(true);
843 getBinding().colourByCharge();
847 public void seqColour_actionPerformed(ActionEvent actionEvent)
849 AAStructureBindingModel binding = getBinding();
850 boolean colourBySequence = seqColour.isSelected();
851 if (_colourwith == null)
853 _colourwith = new Vector<>();
855 if (colourBySequence)
857 if (!binding.isLoadingFromArchive())
859 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
861 // Make the currently displayed alignment panel the associated view
862 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
865 // Set the colour using the current view for the associated alignframe
866 for (AlignmentPanel alignPanel : _colourwith)
868 binding.colourBySequence(alignPanel);
870 seqColoursApplied = true;
875 public void pdbFile_actionPerformed(ActionEvent actionEvent)
877 JalviewFileChooser chooser = new JalviewFileChooser(
878 Cache.getProperty("LAST_DIRECTORY"));
880 chooser.setFileView(new JalviewFileView());
881 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
882 chooser.setToolTipText(MessageManager.getString("action.save"));
884 int value = chooser.showSaveDialog(this);
886 if (value == JalviewFileChooser.APPROVE_OPTION)
888 BufferedReader in = null;
891 // TODO: cope with multiple PDB files in view
892 in = new BufferedReader(
893 new FileReader(getBinding().getStructureFiles()[0]));
894 File outFile = chooser.getSelectedFile();
896 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
898 while ((data = in.readLine()) != null)
900 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
906 } catch (Exception ex)
908 ex.printStackTrace();
916 } catch (IOException e)
926 public void viewMapping_actionPerformed(ActionEvent actionEvent)
928 CutAndPasteTransfer cap = new CutAndPasteTransfer();
931 cap.appendText(getBinding().printMappings());
932 } catch (OutOfMemoryError e)
935 "composing sequence-structure alignments for display in text box.",
940 Desktop.addInternalFrame(cap,
941 MessageManager.getString("label.pdb_sequence_mapping"), 550,
945 protected abstract String getViewerName();
948 * Configures the title and menu items of the viewer panel.
951 public void updateTitleAndMenus()
953 AAStructureBindingModel binding = getBinding();
954 if (binding.hasFileLoadingError())
959 setChainMenuItems(binding.getChainNames());
961 this.setTitle(binding.getViewerTitle(getViewerName(), true));
964 * enable 'Superpose with' if more than one mapped structure
966 viewSelectionMenu.setEnabled(false);
967 if (getBinding().getStructureFiles().length > 1
968 && getBinding().getSequence().length > 1)
970 viewSelectionMenu.setEnabled(true);
974 * Show action menu if it has any enabled items
976 viewerActionMenu.setVisible(false);
977 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
979 if (viewerActionMenu.getItem(i).isEnabled())
981 viewerActionMenu.setVisible(true);
986 if (!binding.isLoadingFromArchive())
988 seqColour_actionPerformed(null);
993 public String toString()
999 public boolean hasMapping()
1001 if (worker != null && (addingStructures || _started))
1005 if (getBinding() == null)
1007 if (_aps == null || _aps.size() == 0)
1009 // viewer has been closed, but we did at some point run.
1014 String[] pdbids = getBinding().getStructureFiles();
1020 for (String pdbid:pdbids) {
1021 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1022 if (sm!=null && sm.length>0 && sm[0]!=null) {
1026 // only return true if there is a mapping for every structure file we have loaded
1027 if (p == 0 || p != pdbids.length)
1031 // and that coloring has been applied
1032 return seqColoursApplied;
1036 public void raiseViewer()