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.util.MessageManager;
42 import java.awt.Color;
43 import java.awt.Component;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46 import java.awt.event.ItemEvent;
47 import java.awt.event.ItemListener;
48 import java.io.BufferedReader;
50 import java.io.FileOutputStream;
51 import java.io.FileReader;
52 import java.io.IOException;
53 import java.io.PrintWriter;
54 import java.util.ArrayList;
55 import java.util.List;
56 import java.util.Vector;
58 import javax.swing.ButtonGroup;
59 import javax.swing.JCheckBoxMenuItem;
60 import javax.swing.JColorChooser;
61 import javax.swing.JMenu;
62 import javax.swing.JMenuItem;
63 import javax.swing.JRadioButtonMenuItem;
64 import javax.swing.event.MenuEvent;
65 import javax.swing.event.MenuListener;
68 * Base class with common functionality for JMol, Chimera or other structure
74 public abstract class StructureViewerBase extends GStructureViewer
75 implements Runnable, ViewSetProvider
78 * names for colour options (additional to Jalview colour schemes)
82 BySequence, ByChain, ChargeCysteine, ByViewer
86 * list of sequenceSet ids associated with the view
88 protected List<String> _aps = new ArrayList<>();
91 * list of alignment panels to use for superposition
93 protected Vector<AlignmentPanel> _alignwith = new Vector<>();
96 * list of alignment panels that are used for colouring structures by aligned
99 protected Vector<AlignmentPanel> _colourwith = new Vector<>();
101 private String viewId = null;
103 private AlignmentPanel ap;
105 protected boolean alignAddedStructures = false;
107 protected volatile boolean _started = false;
109 protected volatile boolean addingStructures = false;
111 protected Thread worker = null;
113 protected boolean allChainsSelected = false;
115 protected JMenu viewSelectionMenu;
118 * set after sequence colouring has been applied for this structure viewer.
119 * used to determine if the final sequence/structure mapping has been
122 protected volatile boolean seqColoursApplied = false;
125 * Default constructor
127 public StructureViewerBase()
133 * @return true if added structures should be aligned to existing one(s)
136 public boolean isAlignAddedStructures()
138 return alignAddedStructures;
144 * if added structures should be aligned to existing one(s)
147 public void setAlignAddedStructures(boolean alignAdded)
149 alignAddedStructures = alignAdded;
155 * @return true if this Jmol instance is linked with the given alignPanel
157 public boolean isLinkedWith(AlignmentPanel ap2)
159 return _aps.contains(ap2.av.getSequenceSetId());
162 public boolean isUsedforaligment(AlignmentPanel ap2)
165 return (_alignwith != null) && _alignwith.contains(ap2);
168 public boolean isUsedforcolourby(AlignmentPanel ap2)
170 return (_colourwith != null) && _colourwith.contains(ap2);
175 * @return TRUE if the view is NOT being coloured by the alignment colours.
177 public boolean isColouredByViewer()
179 return !getBinding().isColourBySequence();
182 public String getViewId()
186 viewId = System.currentTimeMillis() + "." + this.hashCode();
191 protected void setViewId(String viewId)
193 this.viewId = viewId;
196 public abstract String getStateInfo();
198 protected void buildActionMenu()
200 if (_alignwith == null)
202 _alignwith = new Vector<>();
204 if (_alignwith.size() == 0 && ap != null)
209 for (Component c : viewerActionMenu.getMenuComponents())
211 if (c != alignStructs)
213 viewerActionMenu.remove((JMenuItem) c);
218 public AlignmentPanel getAlignmentPanel()
223 protected void setAlignmentPanel(AlignmentPanel alp)
229 public AlignmentPanel[] getAllAlignmentPanels()
231 AlignmentPanel[] t, list = new AlignmentPanel[0];
232 for (String setid : _aps)
234 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
237 t = new AlignmentPanel[list.length + panels.length];
238 System.arraycopy(list, 0, t, 0, list.length);
239 System.arraycopy(panels, 0, t, list.length, panels.length);
248 * set the primary alignmentPanel reference and add another alignPanel to the
249 * list of ones to use for colouring and aligning
253 public void addAlignmentPanel(AlignmentPanel nap)
255 if (getAlignmentPanel() == null)
257 setAlignmentPanel(nap);
259 if (!_aps.contains(nap.av.getSequenceSetId()))
261 _aps.add(nap.av.getSequenceSetId());
266 * remove any references held to the given alignment panel
270 public void removeAlignmentPanel(AlignmentPanel nap)
274 _alignwith.remove(nap);
275 _colourwith.remove(nap);
276 if (getAlignmentPanel() == nap)
278 setAlignmentPanel(null);
279 for (AlignmentPanel aps : getAllAlignmentPanels())
283 setAlignmentPanel(aps);
288 } catch (Exception ex)
291 if (getAlignmentPanel() != null)
297 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
299 addAlignmentPanel(nap);
300 if (!_alignwith.contains(nap))
306 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
308 if (_alignwith.contains(nap))
310 _alignwith.remove(nap);
314 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
315 boolean enableColourBySeq)
317 useAlignmentPanelForColourbyseq(nap);
318 getBinding().setColourBySequence(enableColourBySeq);
319 seqColour.setSelected(enableColourBySeq);
320 viewerColour.setSelected(!enableColourBySeq);
323 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
325 addAlignmentPanel(nap);
326 if (!_colourwith.contains(nap))
328 _colourwith.add(nap);
332 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
334 if (_colourwith.contains(nap))
336 _colourwith.remove(nap);
340 public abstract ViewerType getViewerType();
342 protected abstract IProgressIndicator getIProgressIndicator();
345 * add a new structure (with associated sequences and chains) to this viewer,
346 * retrieving it if necessary first.
352 * if true, new structure(s) will be aligned using associated
356 protected void addStructure(final PDBEntry pdbentry,
357 final SequenceI[] seqs, final String[] chains,
358 final IProgressIndicator alignFrame)
360 if (pdbentry.getFile() == null)
362 if (worker != null && worker.isAlive())
364 // a retrieval is in progress, wait around and add ourselves to the
366 new Thread(new Runnable()
371 while (worker != null && worker.isAlive() && _started)
375 Thread.sleep(100 + ((int) Math.random() * 100));
377 } catch (Exception e)
381 // and call ourselves again.
382 addStructure(pdbentry, seqs, chains, alignFrame);
388 // otherwise, start adding the structure.
389 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
391 { seqs }, new String[][] { chains });
392 addingStructures = true;
394 worker = new Thread(this);
399 protected boolean hasPdbId(String pdbId)
401 return getBinding().hasPdbId(pdbId);
405 * Returns a list of any viewer of the instantiated type. The list is
406 * restricted to those linked to the given alignment panel if it is not null.
408 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
410 return Desktop.instance.getStructureViewers(alp, this.getClass());
414 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
415 String[] chains, final AlignmentViewPanel apanel, String pdbId)
418 * JAL-1742 exclude view with this structure already mapped (don't offer
419 * to align chain B to chain A of the same structure); code may defend
420 * against this possibility before we reach here
426 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error if this
428 useAlignmentPanelForSuperposition(alignPanel);
429 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
433 * Adds mappings for the given sequences to an already opened PDB structure,
434 * and updates any viewers that have the PDB file
441 public void addSequenceMappingsToStructure(SequenceI[] seq,
442 String[] chains, final AlignmentViewPanel alpanel,
445 AlignmentPanel apanel = (AlignmentPanel) alpanel;
447 // TODO : Fix multiple seq to one chain issue here.
449 * create the mappings
451 apanel.getStructureSelectionManager().setMapping(seq, chains,
452 pdbFilename, DataSourceType.FILE, getIProgressIndicator());
455 * alert the FeatureRenderer to show new (PDB RESNUM) features
457 if (apanel.getSeqPanel().seqCanvas.fr != null)
459 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
460 // note - we don't do a refresh for structure here because we do it
461 // explicitly for all panels later on
462 apanel.paintAlignment(true, false);
466 * add the sequences to any other viewers (of the same type) for this pdb
469 // JBPNOTE: this looks like a binding routine, rather than a gui routine
470 for (StructureViewerBase viewer : getViewersFor(null))
472 AAStructureBindingModel bindingModel = viewer.getBinding();
473 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
475 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
477 bindingModel.addSequence(pe, seq);
478 viewer.addAlignmentPanel(apanel);
480 * add it to the set of alignments used for colouring structure by
483 viewer.useAlignmentPanelForColourbyseq(apanel);
484 viewer.buildActionMenu();
485 apanel.getStructureSelectionManager()
486 .sequenceColoursChanged(apanel);
494 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
495 final AlignmentViewPanel apanel, String pdbId)
497 String alreadyMapped = apanel.getStructureSelectionManager()
498 .alreadyMappedToFile(pdbId);
500 if (alreadyMapped == null)
505 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
509 void setChainMenuItems(List<String> chainNames)
511 chainMenu.removeAll();
512 if (chainNames == null || chainNames.isEmpty())
516 JMenuItem menuItem = new JMenuItem(
517 MessageManager.getString("label.all"));
518 menuItem.addActionListener(new ActionListener()
521 public void actionPerformed(ActionEvent evt)
523 allChainsSelected = true;
524 for (int i = 0; i < chainMenu.getItemCount(); i++)
526 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
528 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
531 showSelectedChains();
532 allChainsSelected = false;
536 chainMenu.add(menuItem);
538 for (String chain : chainNames)
540 menuItem = new JCheckBoxMenuItem(chain, true);
541 menuItem.addItemListener(new ItemListener()
544 public void itemStateChanged(ItemEvent evt)
546 if (!allChainsSelected)
548 showSelectedChains();
553 chainMenu.add(menuItem);
557 abstract void showSelectedChains();
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 final ItemListener handler = new ItemListener()
715 public void itemStateChanged(ItemEvent e)
717 alignStructs.setEnabled(!_alignwith.isEmpty());
718 alignStructs.setToolTipText(MessageManager.formatMessage(
719 "label.align_structures_using_linked_alignment_views",
723 viewSelectionMenu = new ViewSelectionMenu(
724 MessageManager.getString("label.superpose_with"), this,
725 _alignwith, handler);
726 handler.itemStateChanged(null);
727 viewerActionMenu.add(viewSelectionMenu, 0);
728 viewerActionMenu.addMenuListener(new MenuListener()
731 public void menuSelected(MenuEvent e)
733 handler.itemStateChanged(null);
737 public void menuDeselected(MenuEvent e)
742 public void menuCanceled(MenuEvent e)
751 public void setJalviewColourScheme(ColourSchemeI cs)
753 getBinding().setJalviewColourScheme(cs);
757 * Sends commands to the structure viewer to superimpose structures based on
758 * currently associated alignments. May optionally return an error message for
762 protected String alignStructs_actionPerformed(ActionEvent actionEvent)
764 return alignStructs_withAllAlignPanels();
767 protected String alignStructs_withAllAlignPanels()
769 if (getAlignmentPanel() == null)
774 if (_alignwith.size() == 0)
776 _alignwith.add(getAlignmentPanel());
782 AlignmentI[] als = new Alignment[_alignwith.size()];
783 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
784 int[] alm = new int[_alignwith.size()];
787 for (AlignmentPanel alignPanel : _alignwith)
789 als[a] = alignPanel.av.getAlignment();
791 alc[a++] = alignPanel.av.getAlignment().getHiddenColumns();
793 reply = getBinding().superposeStructures(als, alm, alc);
796 String text = MessageManager
797 .formatMessage("error.superposition_failed", reply);
798 statusBar.setText(text);
800 } catch (Exception e)
802 StringBuffer sp = new StringBuffer();
803 for (AlignmentPanel alignPanel : _alignwith)
805 sp.append("'" + alignPanel.alignFrame.getTitle() + "' ");
807 Cache.log.info("Couldn't align structures with the " + sp.toString()
808 + "associated alignment panels.", e);
814 public void background_actionPerformed(ActionEvent actionEvent)
816 Color col = JColorChooser.showDialog(this,
817 MessageManager.getString("label.select_background_colour"),
821 getBinding().setBackgroundColour(col);
826 public void viewerColour_actionPerformed(ActionEvent actionEvent)
828 if (viewerColour.isSelected())
830 // disable automatic sequence colouring.
831 getBinding().setColourBySequence(false);
836 public void chainColour_actionPerformed(ActionEvent actionEvent)
838 chainColour.setSelected(true);
839 getBinding().colourByChain();
843 public void chargeColour_actionPerformed(ActionEvent actionEvent)
845 chargeColour.setSelected(true);
846 getBinding().colourByCharge();
850 public void seqColour_actionPerformed(ActionEvent actionEvent)
852 AAStructureBindingModel binding = getBinding();
853 binding.setColourBySequence(seqColour.isSelected());
854 if (_colourwith == null)
856 _colourwith = new Vector<>();
858 if (binding.isColourBySequence())
860 if (!binding.isLoadingFromArchive())
862 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
864 // Make the currently displayed alignment panel the associated view
865 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
868 // Set the colour using the current view for the associated alignframe
869 for (AlignmentPanel alignPanel : _colourwith)
871 binding.colourBySequence(alignPanel);
873 seqColoursApplied = true;
878 public void pdbFile_actionPerformed(ActionEvent actionEvent)
880 JalviewFileChooser chooser = new JalviewFileChooser(
881 Cache.getProperty("LAST_DIRECTORY"));
883 chooser.setFileView(new JalviewFileView());
884 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
885 chooser.setToolTipText(MessageManager.getString("action.save"));
887 int value = chooser.showSaveDialog(this);
889 if (value == JalviewFileChooser.APPROVE_OPTION)
891 BufferedReader in = null;
894 // TODO: cope with multiple PDB files in view
895 in = new BufferedReader(
896 new FileReader(getBinding().getStructureFiles()[0]));
897 File outFile = chooser.getSelectedFile();
899 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
901 while ((data = in.readLine()) != null)
903 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
909 } catch (Exception ex)
911 ex.printStackTrace();
919 } catch (IOException e)
929 public void viewMapping_actionPerformed(ActionEvent actionEvent)
931 CutAndPasteTransfer cap = new CutAndPasteTransfer();
934 cap.appendText(getBinding().printMappings());
935 } catch (OutOfMemoryError e)
938 "composing sequence-structure alignments for display in text box.",
943 Desktop.addInternalFrame(cap,
944 MessageManager.getString("label.pdb_sequence_mapping"), 550,
948 protected abstract String getViewerName();
951 * Configures the title and menu items of the viewer panel.
954 public void updateTitleAndMenus()
956 AAStructureBindingModel binding = getBinding();
957 if (binding.hasFileLoadingError())
962 setChainMenuItems(binding.getChainNames());
964 this.setTitle(binding.getViewerTitle(getViewerName(), true));
967 * enable 'Superpose with' if more than one mapped structure
969 viewSelectionMenu.setEnabled(false);
970 if (getBinding().getStructureFiles().length > 1
971 && getBinding().getSequence().length > 1)
973 viewSelectionMenu.setEnabled(true);
977 * Show action menu if it has any enabled items
979 viewerActionMenu.setVisible(false);
980 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
982 if (viewerActionMenu.getItem(i).isEnabled())
984 viewerActionMenu.setVisible(true);
989 if (!binding.isLoadingFromArchive())
991 seqColour_actionPerformed(null);
996 public String toString()
1002 public boolean hasMapping()
1004 if (worker != null && (addingStructures || _started))
1008 if (getBinding() == null)
1010 if (_aps == null || _aps.size() == 0)
1012 // viewer has been closed, but we did at some point run.
1017 String[] pdbids = getBinding().getStructureFiles();
1023 for (String pdbid:pdbids) {
1024 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1025 if (sm!=null && sm.length>0 && sm[0]!=null) {
1029 // only return true if there is a mapping for every structure file we have loaded
1030 if (p == 0 || p != pdbids.length)
1034 // and that coloring has been applied
1035 return seqColoursApplied;
1039 public void raiseViewer()