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.bin.Cache;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.HiddenColumns;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.gui.StructureViewer.ViewerType;
30 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
31 import jalview.io.DataSourceType;
32 import jalview.io.JalviewFileChooser;
33 import jalview.io.JalviewFileView;
34 import jalview.jbgui.GStructureViewer;
35 import jalview.schemes.ColourSchemeI;
36 import jalview.schemes.ColourSchemes;
37 import jalview.structures.models.AAStructureBindingModel;
38 import jalview.util.MessageManager;
40 import java.awt.Color;
41 import java.awt.Component;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
46 import java.io.BufferedReader;
48 import java.io.FileOutputStream;
49 import java.io.FileReader;
50 import java.io.IOException;
51 import java.io.PrintWriter;
52 import java.util.ArrayList;
53 import java.util.List;
54 import java.util.Vector;
56 import javax.swing.ButtonGroup;
57 import javax.swing.JCheckBoxMenuItem;
58 import javax.swing.JColorChooser;
59 import javax.swing.JMenu;
60 import javax.swing.JMenuItem;
61 import javax.swing.JRadioButtonMenuItem;
62 import javax.swing.event.MenuEvent;
63 import javax.swing.event.MenuListener;
66 * Base class with common functionality for JMol, Chimera or other structure
72 public abstract class StructureViewerBase extends GStructureViewer
73 implements Runnable, ViewSetProvider
76 * names for colour options (additional to Jalview colour schemes)
80 BySequence, ByChain, ChargeCysteine, ByViewer
84 * list of sequenceSet ids associated with the view
86 protected List<String> _aps = new ArrayList<String>();
89 * list of alignment panels to use for superposition
91 protected Vector<AlignmentPanel> _alignwith = new Vector<AlignmentPanel>();
94 * list of alignment panels that are used for colouring structures by aligned
97 protected Vector<AlignmentPanel> _colourwith = new Vector<AlignmentPanel>();
99 private String viewId = null;
101 private AlignmentPanel ap;
103 protected boolean alignAddedStructures = false;
105 protected boolean _started = false;
107 protected boolean addingStructures = false;
109 protected Thread worker = null;
111 protected boolean allChainsSelected = false;
113 protected JMenu viewSelectionMenu;
116 * Default constructor
118 public StructureViewerBase()
125 * @return true if this Jmol instance is linked with the given alignPanel
127 public boolean isLinkedWith(AlignmentPanel ap2)
129 return _aps.contains(ap2.av.getSequenceSetId());
132 public boolean isUsedforaligment(AlignmentPanel ap2)
135 return (_alignwith != null) && _alignwith.contains(ap2);
138 public boolean isUsedforcolourby(AlignmentPanel ap2)
140 return (_colourwith != null) && _colourwith.contains(ap2);
145 * @return TRUE if the view is NOT being coloured by the alignment colours.
147 public boolean isColouredByViewer()
149 return !getBinding().isColourBySequence();
152 public String getViewId()
156 viewId = System.currentTimeMillis() + "." + this.hashCode();
161 protected void setViewId(String viewId)
163 this.viewId = viewId;
166 public abstract String getStateInfo();
168 protected void buildActionMenu()
170 if (_alignwith == null)
172 _alignwith = new Vector<AlignmentPanel>();
174 if (_alignwith.size() == 0 && ap != null)
179 for (Component c : viewerActionMenu.getMenuComponents())
181 if (c != alignStructs)
183 viewerActionMenu.remove((JMenuItem) c);
188 public AlignmentPanel getAlignmentPanel()
193 protected void setAlignmentPanel(AlignmentPanel alp)
199 public AlignmentPanel[] getAllAlignmentPanels()
201 AlignmentPanel[] t, list = new AlignmentPanel[0];
202 for (String setid : _aps)
204 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
207 t = new AlignmentPanel[list.length + panels.length];
208 System.arraycopy(list, 0, t, 0, list.length);
209 System.arraycopy(panels, 0, t, list.length, panels.length);
218 * set the primary alignmentPanel reference and add another alignPanel to the
219 * list of ones to use for colouring and aligning
223 public void addAlignmentPanel(AlignmentPanel nap)
225 if (getAlignmentPanel() == null)
227 setAlignmentPanel(nap);
229 if (!_aps.contains(nap.av.getSequenceSetId()))
231 _aps.add(nap.av.getSequenceSetId());
236 * remove any references held to the given alignment panel
240 public void removeAlignmentPanel(AlignmentPanel nap)
244 _alignwith.remove(nap);
245 _colourwith.remove(nap);
246 if (getAlignmentPanel() == nap)
248 setAlignmentPanel(null);
249 for (AlignmentPanel aps : getAllAlignmentPanels())
253 setAlignmentPanel(aps);
258 } catch (Exception ex)
261 if (getAlignmentPanel() != null)
267 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
269 addAlignmentPanel(nap);
270 if (!_alignwith.contains(nap))
276 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
278 if (_alignwith.contains(nap))
280 _alignwith.remove(nap);
284 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
285 boolean enableColourBySeq)
287 useAlignmentPanelForColourbyseq(nap);
288 getBinding().setColourBySequence(enableColourBySeq);
289 seqColour.setSelected(enableColourBySeq);
290 viewerColour.setSelected(!enableColourBySeq);
293 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
295 addAlignmentPanel(nap);
296 if (!_colourwith.contains(nap))
298 _colourwith.add(nap);
302 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
304 if (_colourwith.contains(nap))
306 _colourwith.remove(nap);
310 public abstract ViewerType getViewerType();
312 protected abstract AAStructureBindingModel getBindingModel();
314 protected abstract IProgressIndicator getIProgressIndicator();
317 * add a new structure (with associated sequences and chains) to this viewer,
318 * retrieving it if necessary first.
324 * if true, new structure(s) will be aligned using associated
328 protected void addStructure(final PDBEntry pdbentry,
329 final SequenceI[] seqs, final String[] chains,
330 final boolean align, final IProgressIndicator alignFrame)
332 if (pdbentry.getFile() == null)
334 if (worker != null && worker.isAlive())
336 // a retrieval is in progress, wait around and add ourselves to the
338 new Thread(new Runnable()
343 while (worker != null && worker.isAlive() && _started)
347 Thread.sleep(100 + ((int) Math.random() * 100));
349 } catch (Exception e)
353 // and call ourselves again.
354 addStructure(pdbentry, seqs, chains, align, alignFrame);
360 // otherwise, start adding the structure.
361 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
362 new SequenceI[][] { seqs }, new String[][] { chains });
363 addingStructures = true;
365 alignAddedStructures = align;
366 worker = new Thread(this);
372 * Presents a dialog with the option to add an align a structure to an
373 * existing structure view
377 * @return YES, NO or CANCEL JvOptionPane code
379 protected int chooseAlignStructureToViewer(String pdbId,
380 StructureViewerBase view)
382 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
383 MessageManager.formatMessage("label.add_pdbentry_to_view",
384 new Object[] { pdbId, view.getTitle() }),
386 .getString("label.align_to_existing_structure_view"),
387 JvOptionPane.YES_NO_CANCEL_OPTION);
391 protected boolean hasPdbId(String pdbId)
393 return getBinding().hasPdbId(pdbId);
396 protected abstract List<StructureViewerBase> getViewersFor(
400 * Check for any existing views involving this alignment and give user the
401 * option to add and align this molecule to one of them
408 * @return true if user adds to a view, or cancels entirely, else false
410 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
411 String[] chains, final AlignmentPanel apanel, String pdbId)
413 for (StructureViewerBase view : getViewersFor(apanel))
415 // TODO: highlight the view somehow
417 * JAL-1742 exclude view with this structure already mapped (don't offer
418 * to align chain B to chain A of the same structure)
420 if (view.hasPdbId(pdbId))
424 int option = chooseAlignStructureToViewer(pdbId, view);
425 if (option == JvOptionPane.CANCEL_OPTION)
429 else if (option == JvOptionPane.YES_OPTION)
431 view.useAlignmentPanelForSuperposition(apanel);
432 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
437 // NO_OPTION - offer the next viewer if any
442 * nothing offered and selected
448 * Adds mappings for the given sequences to an already opened PDB structure,
449 * and updates any viewers that have the PDB file
456 protected void addSequenceMappingsToStructure(SequenceI[] seq,
457 String[] chains, final AlignmentPanel apanel, String pdbFilename)
459 // TODO : Fix multiple seq to one chain issue here.
461 * create the mappings
463 apanel.getStructureSelectionManager().setMapping(seq, chains,
464 pdbFilename, DataSourceType.FILE, getIProgressIndicator());
467 * alert the FeatureRenderer to show new (PDB RESNUM) features
469 if (apanel.getSeqPanel().seqCanvas.fr != null)
471 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
472 apanel.paintAlignment(true);
476 * add the sequences to any other viewers (of the same type) for this pdb
479 // JBPNOTE: this looks like a binding routine, rather than a gui routine
480 for (StructureViewerBase viewer : getViewersFor(null))
482 AAStructureBindingModel bindingModel = viewer.getBinding();
483 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
485 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
487 bindingModel.addSequence(pe, seq);
488 viewer.addAlignmentPanel(apanel);
490 * add it to the set of alignments used for colouring structure by
493 viewer.useAlignmentPanelForColourbyseq(apanel);
494 viewer.buildActionMenu();
495 apanel.getStructureSelectionManager().sequenceColoursChanged(
504 * Check if the PDB file is already loaded, if so offer to add it to the
511 * @return true if the user chooses to add to a viewer, or to cancel entirely
513 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
514 final AlignmentPanel apanel, String pdbId)
516 boolean finished = false;
517 String alreadyMapped = apanel.getStructureSelectionManager()
518 .alreadyMappedToFile(pdbId);
520 if (alreadyMapped != null)
523 * the PDB file is already loaded
525 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
526 MessageManager.formatMessage(
527 "label.pdb_entry_is_already_displayed",
528 new Object[] { pdbId }), MessageManager
530 "label.map_sequences_to_visible_window",
531 new Object[] { pdbId }),
532 JvOptionPane.YES_NO_CANCEL_OPTION);
533 if (option == JvOptionPane.CANCEL_OPTION)
537 else if (option == JvOptionPane.YES_OPTION)
539 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
546 void setChainMenuItems(List<String> chainNames)
548 chainMenu.removeAll();
549 if (chainNames == null || chainNames.isEmpty())
553 JMenuItem menuItem = new JMenuItem(
554 MessageManager.getString("label.all"));
555 menuItem.addActionListener(new ActionListener()
558 public void actionPerformed(ActionEvent evt)
560 allChainsSelected = true;
561 for (int i = 0; i < chainMenu.getItemCount(); i++)
563 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
565 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
568 showSelectedChains();
569 allChainsSelected = false;
573 chainMenu.add(menuItem);
575 for (String chain : chainNames)
577 menuItem = new JCheckBoxMenuItem(chain, true);
578 menuItem.addItemListener(new ItemListener()
581 public void itemStateChanged(ItemEvent evt)
583 if (!allChainsSelected)
585 showSelectedChains();
590 chainMenu.add(menuItem);
594 abstract void showSelectedChains();
597 * Action on selecting one of Jalview's registered colour schemes
600 public void changeColour_actionPerformed(String colourSchemeName)
602 AlignmentI al = getAlignmentPanel().av.getAlignment();
603 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
604 colourSchemeName, al, null);
605 getBinding().setJalviewColourScheme(cs);
609 * Builds the colour menu
611 protected void buildColourMenu()
613 colourMenu.removeAll();
614 AlignmentI al = getAlignmentPanel().av.getAlignment();
617 * add colour by sequence, by chain, by charge and cysteine
619 colourMenu.add(seqColour);
620 colourMenu.add(chainColour);
621 colourMenu.add(chargeColour);
622 chargeColour.setEnabled(!al.isNucleotide());
625 * add all 'simple' (per-residue) colour schemes registered to Jalview
627 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
631 * add 'colour by viewer' (menu item text is set in subclasses)
633 viewerColour.setSelected(false);
634 viewerColour.addActionListener(new ActionListener()
637 public void actionPerformed(ActionEvent actionEvent)
639 viewerColour_actionPerformed(actionEvent);
642 colourMenu.add(viewerColour);
645 * add 'set background colour'
647 JMenuItem backGround = new JMenuItem();
649 .setText(MessageManager.getString("action.background_colour"));
650 backGround.addActionListener(new ActionListener()
653 public void actionPerformed(ActionEvent actionEvent)
655 background_actionPerformed(actionEvent);
658 colourMenu.add(backGround);
661 * add colour buttons to a group so their selection is
662 * mutually exclusive (background colour is a separate option)
664 itemGroup.add(seqColour);
665 itemGroup.add(chainColour);
666 itemGroup.add(chargeColour);
667 itemGroup.add(viewerColour);
671 * Construct menu items
673 protected void initMenus()
675 AAStructureBindingModel binding = getBinding();
677 seqColour = new JRadioButtonMenuItem();
678 seqColour.setText(MessageManager.getString("action.by_sequence"));
679 seqColour.setName(ViewerColour.BySequence.name());
680 seqColour.setSelected(binding.isColourBySequence());
681 seqColour.addActionListener(new ActionListener()
684 public void actionPerformed(ActionEvent actionEvent)
686 seqColour_actionPerformed(actionEvent);
690 chainColour = new JRadioButtonMenuItem();
691 chainColour.setText(MessageManager.getString("action.by_chain"));
692 chainColour.setName(ViewerColour.ByChain.name());
693 chainColour.addActionListener(new ActionListener()
696 public void actionPerformed(ActionEvent actionEvent)
698 chainColour_actionPerformed(actionEvent);
702 chargeColour = new JRadioButtonMenuItem();
703 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
704 chargeColour.setName(ViewerColour.ChargeCysteine.name());
705 chargeColour.addActionListener(new ActionListener()
708 public void actionPerformed(ActionEvent actionEvent)
710 chargeColour_actionPerformed(actionEvent);
714 viewerColour = new JRadioButtonMenuItem();
715 // text is set in overrides of this method
716 viewerColour.setName(ViewerColour.ByViewer.name());
717 viewerColour.setSelected(!binding.isColourBySequence());
719 if (_colourwith == null)
721 _colourwith = new Vector<AlignmentPanel>();
723 if (_alignwith == null)
725 _alignwith = new Vector<AlignmentPanel>();
728 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
729 MessageManager.getString("label.colour_by"), this, _colourwith,
733 public void itemStateChanged(ItemEvent e)
735 if (!seqColour.isSelected())
741 // update the Chimera display now.
742 seqColour_actionPerformed(null);
746 viewMenu.add(seqColourBy);
748 final ItemListener handler = new ItemListener()
751 public void itemStateChanged(ItemEvent e)
753 alignStructs.setEnabled(!_alignwith.isEmpty());
754 alignStructs.setToolTipText(MessageManager.formatMessage(
755 "label.align_structures_using_linked_alignment_views",
759 viewSelectionMenu = new ViewSelectionMenu(
760 MessageManager.getString("label.superpose_with"), this,
761 _alignwith, handler);
762 handler.itemStateChanged(null);
763 viewerActionMenu.add(viewSelectionMenu, 0);
764 viewerActionMenu.addMenuListener(new MenuListener()
767 public void menuSelected(MenuEvent e)
769 handler.itemStateChanged(null);
773 public void menuDeselected(MenuEvent e)
778 public void menuCanceled(MenuEvent e)
787 public void setJalviewColourScheme(ColourSchemeI cs) {
788 getBinding().setJalviewColourScheme(cs);
792 * Sends commands to the structure viewer to superimpose structures based on
793 * currently associated alignments. May optionally return an error message for
797 protected String alignStructs_actionPerformed(
798 ActionEvent actionEvent)
800 return alignStructs_withAllAlignPanels();
803 protected String alignStructs_withAllAlignPanels()
805 if (getAlignmentPanel() == null)
810 if (_alignwith.size() == 0)
812 _alignwith.add(getAlignmentPanel());
818 AlignmentI[] als = new Alignment[_alignwith.size()];
819 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
820 int[] alm = new int[_alignwith.size()];
823 for (AlignmentPanel ap : _alignwith)
825 als[a] = ap.av.getAlignment();
827 alc[a++] = ap.av.getAlignment().getHiddenColumns();
829 reply = getBinding().superposeStructures(als, alm, alc);
832 String text = MessageManager.formatMessage(
833 "error.superposition_failed", reply);
834 statusBar.setText(text);
836 } catch (Exception e)
838 StringBuffer sp = new StringBuffer();
839 for (AlignmentPanel ap : _alignwith)
841 sp.append("'" + ap.alignFrame.getTitle() + "' ");
843 Cache.log.info("Couldn't align structures with the " + sp.toString()
844 + "associated alignment panels.", e);
850 public void background_actionPerformed(ActionEvent actionEvent)
852 Color col = JColorChooser.showDialog(this,
853 MessageManager.getString("label.select_background_colour"),
857 getBinding().setBackgroundColour(col);
861 public void viewerColour_actionPerformed(ActionEvent actionEvent)
863 if (viewerColour.isSelected())
865 // disable automatic sequence colouring.
866 getBinding().setColourBySequence(false);
870 public void chainColour_actionPerformed(ActionEvent actionEvent)
872 chainColour.setSelected(true);
873 getBinding().colourByChain();
876 public void chargeColour_actionPerformed(ActionEvent actionEvent)
878 chargeColour.setSelected(true);
879 getBinding().colourByCharge();
882 public void seqColour_actionPerformed(ActionEvent actionEvent)
884 AAStructureBindingModel binding = getBinding();
885 binding.setColourBySequence(seqColour.isSelected());
886 if (_colourwith == null)
888 _colourwith = new Vector<AlignmentPanel>();
890 if (binding.isColourBySequence())
892 if (!binding.isLoadingFromArchive())
894 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
896 // Make the currently displayed alignment panel the associated view
897 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
900 // Set the colour using the current view for the associated alignframe
901 for (AlignmentPanel ap : _colourwith)
903 binding.colourBySequence(ap);
908 public void pdbFile_actionPerformed(ActionEvent actionEvent)
910 JalviewFileChooser chooser = new JalviewFileChooser(
911 Cache.getProperty("LAST_DIRECTORY"));
913 chooser.setFileView(new JalviewFileView());
914 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
915 chooser.setToolTipText(MessageManager.getString("action.save"));
917 int value = chooser.showSaveDialog(this);
919 if (value == JalviewFileChooser.APPROVE_OPTION)
921 BufferedReader in = null;
924 // TODO: cope with multiple PDB files in view
925 in = new BufferedReader(
926 new FileReader(getBinding().getStructureFiles()[0]));
927 File outFile = chooser.getSelectedFile();
929 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
931 while ((data = in.readLine()) != null)
933 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
939 } catch (Exception ex)
941 ex.printStackTrace();
949 } catch (IOException e)
958 public void viewMapping_actionPerformed(ActionEvent actionEvent)
960 CutAndPasteTransfer cap = new CutAndPasteTransfer();
963 cap.appendText(getBinding().printMappings());
964 } catch (OutOfMemoryError e)
967 "composing sequence-structure alignments for display in text box.",
972 Desktop.addInternalFrame(cap,
973 MessageManager.getString("label.pdb_sequence_mapping"), 550,
977 protected abstract String getViewerName();
980 * Configures the title and menu items of the viewer panel.
982 public void updateTitleAndMenus()
984 AAStructureBindingModel binding = getBinding();
985 if (binding.hasFileLoadingError())
990 setChainMenuItems(binding.getChainNames());
992 this.setTitle(binding.getViewerTitle(getViewerName(), true));
995 * enable 'Superpose with' if more than one mapped structure
997 viewSelectionMenu.setEnabled(false);
998 if (getBinding().getStructureFiles().length > 1
999 && getBinding().getSequence().length > 1)
1001 viewSelectionMenu.setEnabled(true);
1005 * Show action menu if it has any enabled items
1007 viewerActionMenu.setVisible(false);
1008 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1010 if (viewerActionMenu.getItem(i).isEnabled())
1012 viewerActionMenu.setVisible(true);
1017 if (!binding.isLoadingFromArchive())
1019 seqColour_actionPerformed(null);