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()
126 * @return true if this Jmol instance is linked with the given alignPanel
128 public boolean isLinkedWith(AlignmentPanel ap2)
130 return _aps.contains(ap2.av.getSequenceSetId());
133 public boolean isUsedforaligment(AlignmentPanel ap2)
136 return (_alignwith != null) && _alignwith.contains(ap2);
139 public boolean isUsedforcolourby(AlignmentPanel ap2)
141 return (_colourwith != null) && _colourwith.contains(ap2);
146 * @return TRUE if the view is NOT being coloured by the alignment colours.
148 public boolean isColouredByViewer()
150 return !getBinding().isColourBySequence();
153 public String getViewId()
157 viewId = System.currentTimeMillis() + "." + this.hashCode();
162 protected void setViewId(String viewId)
164 this.viewId = viewId;
167 public abstract String getStateInfo();
169 protected void buildActionMenu()
171 if (_alignwith == null)
173 _alignwith = new Vector<AlignmentPanel>();
175 if (_alignwith.size() == 0 && ap != null)
180 for (Component c : viewerActionMenu.getMenuComponents())
182 if (c != alignStructs)
184 viewerActionMenu.remove((JMenuItem) c);
189 public AlignmentPanel getAlignmentPanel()
194 protected void setAlignmentPanel(AlignmentPanel alp)
200 public AlignmentPanel[] getAllAlignmentPanels()
202 AlignmentPanel[] t, list = new AlignmentPanel[0];
203 for (String setid : _aps)
205 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
208 t = new AlignmentPanel[list.length + panels.length];
209 System.arraycopy(list, 0, t, 0, list.length);
210 System.arraycopy(panels, 0, t, list.length, panels.length);
219 * set the primary alignmentPanel reference and add another alignPanel to the
220 * list of ones to use for colouring and aligning
224 public void addAlignmentPanel(AlignmentPanel nap)
226 if (getAlignmentPanel() == null)
228 setAlignmentPanel(nap);
230 if (!_aps.contains(nap.av.getSequenceSetId()))
232 _aps.add(nap.av.getSequenceSetId());
237 * remove any references held to the given alignment panel
241 public void removeAlignmentPanel(AlignmentPanel nap)
245 _alignwith.remove(nap);
246 _colourwith.remove(nap);
247 if (getAlignmentPanel() == nap)
249 setAlignmentPanel(null);
250 for (AlignmentPanel aps : getAllAlignmentPanels())
254 setAlignmentPanel(aps);
259 } catch (Exception ex)
262 if (getAlignmentPanel() != null)
268 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
270 addAlignmentPanel(nap);
271 if (!_alignwith.contains(nap))
277 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
279 if (_alignwith.contains(nap))
281 _alignwith.remove(nap);
285 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
286 boolean enableColourBySeq)
288 useAlignmentPanelForColourbyseq(nap);
289 getBinding().setColourBySequence(enableColourBySeq);
290 seqColour.setSelected(enableColourBySeq);
291 viewerColour.setSelected(!enableColourBySeq);
294 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
296 addAlignmentPanel(nap);
297 if (!_colourwith.contains(nap))
299 _colourwith.add(nap);
303 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
305 if (_colourwith.contains(nap))
307 _colourwith.remove(nap);
311 public abstract ViewerType getViewerType();
314 * add a new structure (with associated sequences and chains) to this viewer,
315 * retrieving it if necessary first.
321 * if true, new structure(s) will be aligned using associated
325 protected void addStructure(final PDBEntry pdbentry,
326 final SequenceI[] seqs, final String[] chains,
327 final boolean align, final IProgressIndicator alignFrame)
329 if (pdbentry.getFile() == null)
331 if (worker != null && worker.isAlive())
333 // a retrieval is in progress, wait around and add ourselves to the
335 new Thread(new Runnable()
340 while (worker != null && worker.isAlive() && _started)
344 Thread.sleep(100 + ((int) Math.random() * 100));
346 } catch (Exception e)
350 // and call ourselves again.
351 addStructure(pdbentry, seqs, chains, align, alignFrame);
357 // otherwise, start adding the structure.
358 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
360 { seqs }, new String[][] { chains });
361 addingStructures = true;
363 alignAddedStructures = align;
364 worker = new Thread(this);
370 * Presents a dialog with the option to add an align a structure to an
371 * existing structure view
375 * @return YES, NO or CANCEL JvOptionPane code
377 protected int chooseAlignStructureToViewer(String pdbId,
378 StructureViewerBase view)
380 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
381 MessageManager.formatMessage("label.add_pdbentry_to_view",
383 { pdbId, view.getTitle() }),
385 .getString("label.align_to_existing_structure_view"),
386 JvOptionPane.YES_NO_CANCEL_OPTION);
390 protected boolean hasPdbId(String pdbId)
392 return getBinding().hasPdbId(pdbId);
395 protected abstract List<StructureViewerBase> getViewersFor(
399 * Check for any existing views involving this alignment and give user the
400 * option to add and align this molecule to one of them
407 * @return true if user adds to a view, or cancels entirely, else false
409 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
410 String[] chains, final AlignmentPanel apanel, String pdbId)
412 for (StructureViewerBase view : getViewersFor(apanel))
414 // TODO: highlight the view somehow
416 * JAL-1742 exclude view with this structure already mapped (don't offer
417 * to align chain B to chain A of the same structure)
419 if (view.hasPdbId(pdbId))
423 int option = chooseAlignStructureToViewer(pdbId, view);
424 if (option == JvOptionPane.CANCEL_OPTION)
428 else if (option == JvOptionPane.YES_OPTION)
430 view.useAlignmentPanelForSuperposition(apanel);
431 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
436 // NO_OPTION - offer the next viewer if any
441 * nothing offered and selected
447 * Adds mappings for the given sequences to an already opened PDB structure,
448 * and updates any viewers that have the PDB file
455 protected void addSequenceMappingsToStructure(SequenceI[] seq,
456 String[] chains, final AlignmentPanel apanel, String pdbFilename)
458 // TODO : Fix multiple seq to one chain issue here.
460 * create the mappings
462 apanel.getStructureSelectionManager().setMapping(seq, chains,
463 pdbFilename, DataSourceType.FILE);
466 * alert the FeatureRenderer to show new (PDB RESNUM) features
468 if (apanel.getSeqPanel().seqCanvas.fr != null)
470 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
471 apanel.paintAlignment(true);
475 * add the sequences to any other viewers (of the same type) for this pdb
478 // JBPNOTE: this looks like a binding routine, rather than a gui routine
479 for (StructureViewerBase viewer : getViewersFor(null))
481 AAStructureBindingModel bindingModel = viewer.getBinding();
482 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
484 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
486 bindingModel.addSequence(pe, seq);
487 viewer.addAlignmentPanel(apanel);
489 * add it to the set of alignments used for colouring structure by
492 viewer.useAlignmentPanelForColourbyseq(apanel);
493 viewer.buildActionMenu();
494 apanel.getStructureSelectionManager()
495 .sequenceColoursChanged(apanel);
503 * Check if the PDB file is already loaded, if so offer to add it to the
510 * @return true if the user chooses to add to a viewer, or to cancel entirely
512 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
513 final AlignmentPanel apanel, String pdbId)
515 boolean finished = false;
516 String alreadyMapped = apanel.getStructureSelectionManager()
517 .alreadyMappedToFile(pdbId);
519 if (alreadyMapped != null)
522 * the PDB file is already loaded
524 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
525 MessageManager.formatMessage(
526 "label.pdb_entry_is_already_displayed", new Object[]
528 MessageManager.formatMessage(
529 "label.map_sequences_to_visible_window", new Object[]
531 JvOptionPane.YES_NO_CANCEL_OPTION);
532 if (option == JvOptionPane.CANCEL_OPTION)
536 else if (option == JvOptionPane.YES_OPTION)
538 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
545 void setChainMenuItems(List<String> chainNames)
547 chainMenu.removeAll();
548 if (chainNames == null || chainNames.isEmpty())
552 JMenuItem menuItem = new JMenuItem(
553 MessageManager.getString("label.all"));
554 menuItem.addActionListener(new ActionListener()
557 public void actionPerformed(ActionEvent evt)
559 allChainsSelected = true;
560 for (int i = 0; i < chainMenu.getItemCount(); i++)
562 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
564 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
567 showSelectedChains();
568 allChainsSelected = false;
572 chainMenu.add(menuItem);
574 for (String chain : chainNames)
576 menuItem = new JCheckBoxMenuItem(chain, true);
577 menuItem.addItemListener(new ItemListener()
580 public void itemStateChanged(ItemEvent evt)
582 if (!allChainsSelected)
584 showSelectedChains();
589 chainMenu.add(menuItem);
593 abstract void showSelectedChains();
596 * Action on selecting one of Jalview's registered colour schemes
599 public void changeColour_actionPerformed(String colourSchemeName)
601 AlignmentI al = getAlignmentPanel().av.getAlignment();
602 ColourSchemeI cs = ColourSchemes.getInstance()
603 .getColourScheme(colourSchemeName, al, null);
604 getBinding().setJalviewColourScheme(cs);
608 * Builds the colour menu
610 protected void buildColourMenu()
612 colourMenu.removeAll();
613 AlignmentI al = getAlignmentPanel().av.getAlignment();
616 * add colour by sequence, by chain, by charge and cysteine
618 colourMenu.add(seqColour);
619 colourMenu.add(chainColour);
620 colourMenu.add(chargeColour);
621 chargeColour.setEnabled(!al.isNucleotide());
624 * add all 'simple' (per-residue) colour schemes registered to Jalview
626 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
630 * add 'colour by viewer' (menu item text is set in subclasses)
632 viewerColour.setSelected(false);
633 viewerColour.addActionListener(new ActionListener()
636 public void actionPerformed(ActionEvent actionEvent)
638 viewerColour_actionPerformed(actionEvent);
641 colourMenu.add(viewerColour);
644 * add 'set background colour'
646 JMenuItem backGround = new JMenuItem();
648 .setText(MessageManager.getString("action.background_colour"));
649 backGround.addActionListener(new ActionListener()
652 public void actionPerformed(ActionEvent actionEvent)
654 background_actionPerformed(actionEvent);
657 colourMenu.add(backGround);
660 * add colour buttons to a group so their selection is
661 * mutually exclusive (background colour is a separate option)
663 itemGroup.add(seqColour);
664 itemGroup.add(chainColour);
665 itemGroup.add(chargeColour);
666 itemGroup.add(viewerColour);
670 * Construct menu items
672 protected void initMenus()
674 AAStructureBindingModel binding = getBinding();
676 seqColour = new JRadioButtonMenuItem();
677 seqColour.setText(MessageManager.getString("action.by_sequence"));
678 seqColour.setName(ViewerColour.BySequence.name());
679 seqColour.setSelected(binding.isColourBySequence());
680 seqColour.addActionListener(new ActionListener()
683 public void actionPerformed(ActionEvent actionEvent)
685 seqColour_actionPerformed(actionEvent);
689 chainColour = new JRadioButtonMenuItem();
690 chainColour.setText(MessageManager.getString("action.by_chain"));
691 chainColour.setName(ViewerColour.ByChain.name());
692 chainColour.addActionListener(new ActionListener()
695 public void actionPerformed(ActionEvent actionEvent)
697 chainColour_actionPerformed(actionEvent);
701 chargeColour = new JRadioButtonMenuItem();
702 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
703 chargeColour.setName(ViewerColour.ChargeCysteine.name());
704 chargeColour.addActionListener(new ActionListener()
707 public void actionPerformed(ActionEvent actionEvent)
709 chargeColour_actionPerformed(actionEvent);
713 viewerColour = new JRadioButtonMenuItem();
714 // text is set in overrides of this method
715 viewerColour.setName(ViewerColour.ByViewer.name());
716 viewerColour.setSelected(!binding.isColourBySequence());
718 if (_colourwith == null)
720 _colourwith = new Vector<AlignmentPanel>();
722 if (_alignwith == null)
724 _alignwith = new Vector<AlignmentPanel>();
727 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
728 MessageManager.getString("label.colour_by"), this, _colourwith,
732 public void itemStateChanged(ItemEvent e)
734 if (!seqColour.isSelected())
740 // update the Chimera display now.
741 seqColour_actionPerformed(null);
745 viewMenu.add(seqColourBy);
747 final ItemListener handler = new ItemListener()
750 public void itemStateChanged(ItemEvent e)
752 alignStructs.setEnabled(!_alignwith.isEmpty());
753 alignStructs.setToolTipText(MessageManager.formatMessage(
754 "label.align_structures_using_linked_alignment_views",
758 viewSelectionMenu = new ViewSelectionMenu(
759 MessageManager.getString("label.superpose_with"), this,
760 _alignwith, handler);
761 handler.itemStateChanged(null);
762 viewerActionMenu.add(viewSelectionMenu, 0);
763 viewerActionMenu.addMenuListener(new MenuListener()
766 public void menuSelected(MenuEvent e)
768 handler.itemStateChanged(null);
772 public void menuDeselected(MenuEvent e)
777 public void menuCanceled(MenuEvent e)
786 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(ActionEvent actionEvent)
799 return alignStructs_withAllAlignPanels();
802 protected String alignStructs_withAllAlignPanels()
804 if (getAlignmentPanel() == null)
809 if (_alignwith.size() == 0)
811 _alignwith.add(getAlignmentPanel());
817 AlignmentI[] als = new Alignment[_alignwith.size()];
818 HiddenColumns[] alc = new HiddenColumns[_alignwith.size()];
819 int[] alm = new int[_alignwith.size()];
822 for (AlignmentPanel ap : _alignwith)
824 als[a] = ap.av.getAlignment();
826 alc[a++] = ap.av.getAlignment().getHiddenColumns();
828 reply = getBinding().superposeStructures(als, alm, alc);
831 String text = MessageManager
832 .formatMessage("error.superposition_failed", reply);
833 statusBar.setText(text);
835 } catch (Exception e)
837 StringBuffer sp = new StringBuffer();
838 for (AlignmentPanel ap : _alignwith)
840 sp.append("'" + ap.alignFrame.getTitle() + "' ");
842 Cache.log.info("Couldn't align structures with the " + sp.toString()
843 + "associated alignment panels.", e);
849 public void background_actionPerformed(ActionEvent actionEvent)
851 Color col = JColorChooser.showDialog(this,
852 MessageManager.getString("label.select_background_colour"),
856 getBinding().setBackgroundColour(col);
861 public void viewerColour_actionPerformed(ActionEvent actionEvent)
863 if (viewerColour.isSelected())
865 // disable automatic sequence colouring.
866 getBinding().setColourBySequence(false);
871 public void chainColour_actionPerformed(ActionEvent actionEvent)
873 chainColour.setSelected(true);
874 getBinding().colourByChain();
878 public void chargeColour_actionPerformed(ActionEvent actionEvent)
880 chargeColour.setSelected(true);
881 getBinding().colourByCharge();
885 public void seqColour_actionPerformed(ActionEvent actionEvent)
887 AAStructureBindingModel binding = getBinding();
888 binding.setColourBySequence(seqColour.isSelected());
889 if (_colourwith == null)
891 _colourwith = new Vector<AlignmentPanel>();
893 if (binding.isColourBySequence())
895 if (!binding.isLoadingFromArchive())
897 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
899 // Make the currently displayed alignment panel the associated view
900 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
903 // Set the colour using the current view for the associated alignframe
904 for (AlignmentPanel ap : _colourwith)
906 binding.colourBySequence(ap);
912 public void pdbFile_actionPerformed(ActionEvent actionEvent)
914 JalviewFileChooser chooser = new JalviewFileChooser(
915 Cache.getProperty("LAST_DIRECTORY"));
917 chooser.setFileView(new JalviewFileView());
918 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
919 chooser.setToolTipText(MessageManager.getString("action.save"));
921 int value = chooser.showSaveDialog(this);
923 if (value == JalviewFileChooser.APPROVE_OPTION)
925 BufferedReader in = null;
928 // TODO: cope with multiple PDB files in view
929 in = new BufferedReader(
930 new FileReader(getBinding().getStructureFiles()[0]));
931 File outFile = chooser.getSelectedFile();
933 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
935 while ((data = in.readLine()) != null)
937 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
943 } catch (Exception ex)
945 ex.printStackTrace();
953 } catch (IOException e)
963 public void viewMapping_actionPerformed(ActionEvent actionEvent)
965 CutAndPasteTransfer cap = new CutAndPasteTransfer();
968 cap.appendText(getBinding().printMappings());
969 } catch (OutOfMemoryError e)
972 "composing sequence-structure alignments for display in text box.",
977 Desktop.addInternalFrame(cap,
978 MessageManager.getString("label.pdb_sequence_mapping"), 550,
982 protected abstract String getViewerName();
985 * Configures the title and menu items of the viewer panel.
987 public void updateTitleAndMenus()
989 AAStructureBindingModel binding = getBinding();
990 if (binding.hasFileLoadingError())
995 setChainMenuItems(binding.getChainNames());
997 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1000 * enable 'Superpose with' if more than one mapped structure
1002 viewSelectionMenu.setEnabled(false);
1003 if (getBinding().getStructureFiles().length > 1
1004 && getBinding().getSequence().length > 1)
1006 viewSelectionMenu.setEnabled(true);
1010 * Show action menu if it has any enabled items
1012 viewerActionMenu.setVisible(false);
1013 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1015 if (viewerActionMenu.getItem(i).isEnabled())
1017 viewerActionMenu.setVisible(true);
1022 if (!binding.isLoadingFromArchive())
1024 seqColour_actionPerformed(null);