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.bin.Instance;
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.JalviewColourChooser.ColourChooserListener;
32 import jalview.gui.StructureViewer.ViewerType;
33 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
34 import jalview.io.DataSourceType;
35 import jalview.io.JalviewFileChooser;
36 import jalview.io.JalviewFileView;
37 import jalview.jbgui.GStructureViewer;
38 import jalview.schemes.ColourSchemeI;
39 import jalview.schemes.ColourSchemes;
40 import jalview.structure.StructureMapping;
41 import jalview.structures.models.AAStructureBindingModel;
42 import jalview.util.MessageManager;
44 import java.awt.Color;
45 import java.awt.Component;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.ItemEvent;
49 import java.awt.event.ItemListener;
50 import java.io.BufferedReader;
52 import java.io.FileOutputStream;
53 import java.io.FileReader;
54 import java.io.IOException;
55 import java.io.PrintWriter;
56 import java.util.ArrayList;
57 import java.util.List;
58 import java.util.Vector;
60 import javax.swing.ButtonGroup;
61 import javax.swing.JCheckBoxMenuItem;
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(AlignmentPanel ap2)
171 return (_colourwith != null) && _colourwith.contains(ap2);
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 getBinding().setColourBySequence(enableColourBySeq);
320 seqColour.setSelected(enableColourBySeq);
321 viewerColour.setSelected(!enableColourBySeq);
324 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
326 addAlignmentPanel(nap);
327 if (!_colourwith.contains(nap))
329 _colourwith.add(nap);
333 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
335 if (_colourwith.contains(nap))
337 _colourwith.remove(nap);
341 public abstract ViewerType getViewerType();
343 protected abstract IProgressIndicator getIProgressIndicator();
346 * add a new structure (with associated sequences and chains) to this viewer,
347 * retrieving it if necessary first.
353 * if true, new structure(s) will be aligned using associated
357 protected void addStructure(final PDBEntry pdbentry,
358 final SequenceI[] seqs, final String[] chains,
359 final IProgressIndicator alignFrame)
361 if (pdbentry.getFile() == null)
363 if (worker != null && worker.isAlive())
365 // a retrieval is in progress, wait around and add ourselves to the
367 new Thread(new Runnable()
372 while (worker != null && worker.isAlive() && _started)
376 Thread.sleep(100 + ((int) Math.random() * 100));
378 } catch (Exception e)
382 // and call ourselves again.
383 addStructure(pdbentry, seqs, chains, alignFrame);
389 // otherwise, start adding the structure.
390 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
392 { seqs }, new String[][] { chains });
393 addingStructures = true;
395 worker = new Thread(this);
400 protected boolean hasPdbId(String pdbId)
402 return getBinding().hasPdbId(pdbId);
406 * Returns a list of any viewer of the instantiated type. The list is
407 * restricted to those linked to the given alignment panel if it is not null.
409 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
411 return Instance.getDesktop().getStructureViewers(alp, this.getClass());
415 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
416 String[] chains, final AlignmentViewPanel apanel, String pdbId)
419 * JAL-1742 exclude view with this structure already mapped (don't offer
420 * to align chain B to chain A of the same structure); code may defend
421 * against this possibility before we reach here
427 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error if this
429 useAlignmentPanelForSuperposition(alignPanel);
430 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
434 * Adds mappings for the given sequences to an already opened PDB structure,
435 * and updates any viewers that have the PDB file
442 public void addSequenceMappingsToStructure(SequenceI[] seq,
443 String[] chains, final AlignmentViewPanel alpanel,
446 AlignmentPanel apanel = (AlignmentPanel) alpanel;
448 // TODO : Fix multiple seq to one chain issue here.
450 * create the mappings
452 apanel.getStructureSelectionManager().setMapping(seq, chains,
453 pdbFilename, DataSourceType.FILE, getIProgressIndicator());
456 * alert the FeatureRenderer to show new (PDB RESNUM) features
458 if (apanel.getSeqPanel().seqCanvas.fr != null)
460 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
461 // note - we don't do a refresh for structure here because we do it
462 // explicitly for all panels later on
463 apanel.paintAlignment(true, false);
467 * add the sequences to any other viewers (of the same type) for this pdb
470 // JBPNOTE: this looks like a binding routine, rather than a gui routine
471 for (StructureViewerBase viewer : getViewersFor(null))
473 AAStructureBindingModel bindingModel = viewer.getBinding();
474 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
476 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
478 bindingModel.addSequence(pe, seq);
479 viewer.addAlignmentPanel(apanel);
481 * add it to the set of alignments used for colouring structure by
484 viewer.useAlignmentPanelForColourbyseq(apanel);
485 viewer.buildActionMenu();
486 apanel.getStructureSelectionManager()
487 .sequenceColoursChanged(apanel);
495 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
496 final AlignmentViewPanel apanel, String pdbId)
498 String alreadyMapped = apanel.getStructureSelectionManager()
499 .alreadyMappedToFile(pdbId);
501 if (alreadyMapped == null)
506 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
510 void setChainMenuItems(List<String> chainNames)
512 chainMenu.removeAll();
513 if (chainNames == null || chainNames.isEmpty())
517 JMenuItem menuItem = new JMenuItem(
518 MessageManager.getString("label.all"));
519 menuItem.addActionListener(new ActionListener()
522 public void actionPerformed(ActionEvent evt)
524 allChainsSelected = true;
525 for (int i = 0; i < chainMenu.getItemCount(); i++)
527 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
529 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
532 showSelectedChains();
533 allChainsSelected = false;
537 chainMenu.add(menuItem);
539 for (String chain : chainNames)
541 menuItem = new JCheckBoxMenuItem(chain, true);
542 menuItem.addItemListener(new ItemListener()
545 public void itemStateChanged(ItemEvent evt)
547 if (!allChainsSelected)
549 showSelectedChains();
554 chainMenu.add(menuItem);
558 abstract void showSelectedChains();
561 * Action on selecting one of Jalview's registered colour schemes
564 public void changeColour_actionPerformed(String colourSchemeName)
566 AlignmentI al = getAlignmentPanel().av.getAlignment();
567 ColourSchemeI cs = ColourSchemes.getInstance()
568 .getColourScheme(colourSchemeName, getAlignmentPanel().av, al,
570 getBinding().setJalviewColourScheme(cs);
574 * Builds the colour menu
576 protected void buildColourMenu()
578 colourMenu.removeAll();
579 AlignmentI al = getAlignmentPanel().av.getAlignment();
582 * add colour by sequence, by chain, by charge and cysteine
584 colourMenu.add(seqColour);
585 colourMenu.add(chainColour);
586 colourMenu.add(chargeColour);
587 chargeColour.setEnabled(!al.isNucleotide());
590 * add all 'simple' (per-residue) colour schemes registered to Jalview
592 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
596 * add 'colour by viewer' (menu item text is set in subclasses)
598 viewerColour.setSelected(false);
599 viewerColour.addActionListener(new ActionListener()
602 public void actionPerformed(ActionEvent actionEvent)
604 viewerColour_actionPerformed(actionEvent);
607 colourMenu.add(viewerColour);
610 * add 'set background colour'
612 JMenuItem backGround = new JMenuItem();
614 .setText(MessageManager.getString("action.background_colour"));
615 backGround.addActionListener(new ActionListener()
618 public void actionPerformed(ActionEvent actionEvent)
620 background_actionPerformed(actionEvent);
623 colourMenu.add(backGround);
626 * add colour buttons to a group so their selection is
627 * mutually exclusive (background colour is a separate option)
629 itemGroup.add(seqColour);
630 itemGroup.add(chainColour);
631 itemGroup.add(chargeColour);
632 itemGroup.add(viewerColour);
636 * Construct menu items
638 protected void initMenus()
640 AAStructureBindingModel binding = getBinding();
642 seqColour = new JRadioButtonMenuItem();
643 seqColour.setText(MessageManager.getString("action.by_sequence"));
644 seqColour.setName(ViewerColour.BySequence.name());
645 seqColour.setSelected(binding.isColourBySequence());
646 seqColour.addActionListener(new ActionListener()
649 public void actionPerformed(ActionEvent actionEvent)
651 seqColour_actionPerformed(actionEvent);
655 chainColour = new JRadioButtonMenuItem();
656 chainColour.setText(MessageManager.getString("action.by_chain"));
657 chainColour.setName(ViewerColour.ByChain.name());
658 chainColour.addActionListener(new ActionListener()
661 public void actionPerformed(ActionEvent actionEvent)
663 chainColour_actionPerformed(actionEvent);
667 chargeColour = new JRadioButtonMenuItem();
668 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
669 chargeColour.setName(ViewerColour.ChargeCysteine.name());
670 chargeColour.addActionListener(new ActionListener()
673 public void actionPerformed(ActionEvent actionEvent)
675 chargeColour_actionPerformed(actionEvent);
679 viewerColour = new JRadioButtonMenuItem();
680 // text is set in overrides of this method
681 viewerColour.setName(ViewerColour.ByViewer.name());
682 viewerColour.setSelected(!binding.isColourBySequence());
684 if (_colourwith == null)
686 _colourwith = new Vector<>();
688 if (_alignwith == null)
690 _alignwith = new Vector<>();
693 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
694 MessageManager.getString("label.colour_by"), this, _colourwith,
698 public void itemStateChanged(ItemEvent e)
700 if (!seqColour.isSelected())
706 // update the Chimera display now.
707 seqColour_actionPerformed(null);
711 viewMenu.add(seqColourBy);
713 final ItemListener handler = new ItemListener()
716 public void itemStateChanged(ItemEvent e)
718 alignStructs.setEnabled(!_alignwith.isEmpty());
719 alignStructs.setToolTipText(MessageManager.formatMessage(
720 "label.align_structures_using_linked_alignment_views",
724 viewSelectionMenu = new ViewSelectionMenu(
725 MessageManager.getString("label.superpose_with"), this,
726 _alignwith, handler);
727 handler.itemStateChanged(null);
728 viewerActionMenu.add(viewSelectionMenu, 0);
729 viewerActionMenu.addMenuListener(new MenuListener()
732 public void menuSelected(MenuEvent e)
734 handler.itemStateChanged(null);
738 public void menuDeselected(MenuEvent e)
743 public void menuCanceled(MenuEvent e)
752 public void setJalviewColourScheme(ColourSchemeI cs)
754 getBinding().setJalviewColourScheme(cs);
758 * Sends commands to the structure viewer to superimpose structures based on
759 * currently associated alignments. May optionally return an error message for
763 protected String alignStructs_actionPerformed(ActionEvent actionEvent)
765 return alignStructs_withAllAlignPanels();
768 protected String alignStructs_withAllAlignPanels()
770 if (getAlignmentPanel() == null)
775 if (_alignwith.size() == 0)
777 _alignwith.add(getAlignmentPanel());
783 AlignmentI[] als = new Alignment[_alignwith.size()];
784 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
785 int[] alm = new int[_alignwith.size()];
788 for (AlignmentPanel alignPanel : _alignwith)
790 als[a] = alignPanel.av.getAlignment();
792 alc[a++] = alignPanel.av.getAlignment().getHiddenColumns();
794 reply = getBinding().superposeStructures(als, alm, alc);
797 String text = MessageManager
798 .formatMessage("error.superposition_failed", reply);
799 statusBar.setText(text);
801 } catch (Exception e)
803 StringBuffer sp = new StringBuffer();
804 for (AlignmentPanel alignPanel : _alignwith)
806 sp.append("'" + alignPanel.alignFrame.getTitle() + "' ");
808 Cache.log.info("Couldn't align structures with the " + sp.toString()
809 + "associated alignment panels.", e);
815 * Opens a colour chooser dialog, and applies the chosen colour to the
816 * background of the structure viewer
819 public void background_actionPerformed(ActionEvent actionEvent)
821 String ttl = MessageManager.getString("label.select_background_colour");
822 ColourChooserListener listener = new ColourChooserListener()
825 public void colourSelected(Color c)
827 getBinding().setBackgroundColour(c);
830 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
834 public void viewerColour_actionPerformed(ActionEvent actionEvent)
836 if (viewerColour.isSelected())
838 // disable automatic sequence colouring.
839 getBinding().setColourBySequence(false);
844 public void chainColour_actionPerformed(ActionEvent actionEvent)
846 chainColour.setSelected(true);
847 getBinding().colourByChain();
851 public void chargeColour_actionPerformed(ActionEvent actionEvent)
853 chargeColour.setSelected(true);
854 getBinding().colourByCharge();
858 public void seqColour_actionPerformed(ActionEvent actionEvent)
860 AAStructureBindingModel binding = getBinding();
861 binding.setColourBySequence(seqColour.isSelected());
862 if (_colourwith == null)
864 _colourwith = new Vector<>();
866 if (binding.isColourBySequence())
868 if (!binding.isLoadingFromArchive())
870 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
872 // Make the currently displayed alignment panel the associated view
873 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
876 // Set the colour using the current view for the associated alignframe
877 for (AlignmentPanel alignPanel : _colourwith)
879 binding.colourBySequence(alignPanel);
881 seqColoursApplied = true;
886 public void pdbFile_actionPerformed(ActionEvent actionEvent)
888 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
889 JalviewFileChooser chooser = new JalviewFileChooser(
890 Cache.getProperty("LAST_DIRECTORY"));
892 chooser.setFileView(new JalviewFileView());
893 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
894 chooser.setToolTipText(MessageManager.getString("action.save"));
896 int value = chooser.showSaveDialog(this);
898 if (value == JalviewFileChooser.APPROVE_OPTION)
900 BufferedReader in = null;
903 // TODO: cope with multiple PDB files in view
904 in = new BufferedReader(
905 new FileReader(getBinding().getStructureFiles()[0]));
906 File outFile = chooser.getSelectedFile();
908 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
910 while ((data = in.readLine()) != null)
912 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
918 } catch (Exception ex)
920 ex.printStackTrace();
928 } catch (IOException e)
938 public void viewMapping_actionPerformed(ActionEvent actionEvent)
940 CutAndPasteTransfer cap = new CutAndPasteTransfer();
943 cap.appendText(getBinding().printMappings());
944 } catch (OutOfMemoryError e)
947 "composing sequence-structure alignments for display in text box.",
952 Desktop.addInternalFrame(cap,
953 MessageManager.getString("label.pdb_sequence_mapping"), 550,
957 protected abstract String getViewerName();
960 * Configures the title and menu items of the viewer panel.
963 public void updateTitleAndMenus()
965 AAStructureBindingModel binding = getBinding();
966 if (binding.hasFileLoadingError())
971 setChainMenuItems(binding.getChainNames());
973 this.setTitle(binding.getViewerTitle(getViewerName(), true));
976 * enable 'Superpose with' if more than one mapped structure
978 viewSelectionMenu.setEnabled(false);
979 if (getBinding().getStructureFiles().length > 1
980 && getBinding().getSequence().length > 1)
982 viewSelectionMenu.setEnabled(true);
986 * Show action menu if it has any enabled items
988 viewerActionMenu.setVisible(false);
989 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
991 if (viewerActionMenu.getItem(i).isEnabled())
993 viewerActionMenu.setVisible(true);
998 if (!binding.isLoadingFromArchive())
1000 seqColour_actionPerformed(null);
1005 public String toString()
1011 public boolean hasMapping()
1013 if (worker != null && (addingStructures || _started))
1017 if (getBinding() == null)
1019 if (_aps == null || _aps.size() == 0)
1021 // viewer has been closed, but we did at some point run.
1026 String[] pdbids = getBinding().getStructureFiles();
1032 for (String pdbid:pdbids) {
1033 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1034 if (sm!=null && sm.length>0 && sm[0]!=null) {
1038 // only return true if there is a mapping for every structure file we have loaded
1039 if (p == 0 || p != pdbids.length)
1043 // and that coloring has been applied
1044 return seqColoursApplied;
1048 public void raiseViewer()