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.ColumnSelection;
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();
313 * add a new structure (with associated sequences and chains) to this viewer,
314 * retrieving it if necessary first.
320 * if true, new structure(s) will be aligned using associated
324 protected void addStructure(final PDBEntry pdbentry,
325 final SequenceI[] seqs, final String[] chains,
326 final boolean align, final IProgressIndicator alignFrame)
328 if (pdbentry.getFile() == null)
330 if (worker != null && worker.isAlive())
332 // a retrieval is in progress, wait around and add ourselves to the
334 new Thread(new Runnable()
339 while (worker != null && worker.isAlive() && _started)
343 Thread.sleep(100 + ((int) Math.random() * 100));
345 } catch (Exception e)
349 // and call ourselves again.
350 addStructure(pdbentry, seqs, chains, align, alignFrame);
356 // otherwise, start adding the structure.
357 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
358 new SequenceI[][] { seqs }, new String[][] { chains });
359 addingStructures = true;
361 alignAddedStructures = align;
362 worker = new Thread(this);
368 * Presents a dialog with the option to add an align a structure to an
369 * existing structure view
373 * @return YES, NO or CANCEL JvOptionPane code
375 protected int chooseAlignStructureToViewer(String pdbId,
376 StructureViewerBase view)
378 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
379 MessageManager.formatMessage("label.add_pdbentry_to_view",
380 new Object[] { pdbId, view.getTitle() }),
382 .getString("label.align_to_existing_structure_view"),
383 JvOptionPane.YES_NO_CANCEL_OPTION);
387 protected boolean hasPdbId(String pdbId)
389 return getBinding().hasPdbId(pdbId);
392 protected abstract List<StructureViewerBase> getViewersFor(
396 * Check for any existing views involving this alignment and give user the
397 * option to add and align this molecule to one of them
404 * @return true if user adds to a view, or cancels entirely, else false
406 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
407 String[] chains, final AlignmentPanel apanel, String pdbId)
409 for (StructureViewerBase view : getViewersFor(apanel))
411 // TODO: highlight the view somehow
413 * JAL-1742 exclude view with this structure already mapped (don't offer
414 * to align chain B to chain A of the same structure)
416 if (view.hasPdbId(pdbId))
420 int option = chooseAlignStructureToViewer(pdbId, view);
421 if (option == JvOptionPane.CANCEL_OPTION)
425 else if (option == JvOptionPane.YES_OPTION)
427 view.useAlignmentPanelForSuperposition(apanel);
428 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
433 // NO_OPTION - offer the next viewer if any
438 * nothing offered and selected
444 * Adds mappings for the given sequences to an already opened PDB structure,
445 * and updates any viewers that have the PDB file
452 protected void addSequenceMappingsToStructure(SequenceI[] seq,
453 String[] chains, final AlignmentPanel apanel, String pdbFilename)
455 // TODO : Fix multiple seq to one chain issue here.
457 * create the mappings
459 apanel.getStructureSelectionManager().setMapping(seq, chains,
460 pdbFilename, DataSourceType.FILE);
463 * alert the FeatureRenderer to show new (PDB RESNUM) features
465 if (apanel.getSeqPanel().seqCanvas.fr != null)
467 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
468 apanel.paintAlignment(true);
472 * add the sequences to any other viewers (of the same type) for this pdb
475 // JBPNOTE: this looks like a binding routine, rather than a gui routine
476 for (StructureViewerBase viewer : getViewersFor(null))
478 AAStructureBindingModel bindingModel = viewer.getBinding();
479 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
481 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
483 bindingModel.addSequence(pe, seq);
484 viewer.addAlignmentPanel(apanel);
486 * add it to the set of alignments used for colouring structure by
489 viewer.useAlignmentPanelForColourbyseq(apanel);
490 viewer.buildActionMenu();
491 apanel.getStructureSelectionManager().sequenceColoursChanged(
500 * Check if the PDB file is already loaded, if so offer to add it to the
507 * @return true if the user chooses to add to a viewer, or to cancel entirely
509 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
510 final AlignmentPanel apanel, String pdbId)
512 boolean finished = false;
513 String alreadyMapped = apanel.getStructureSelectionManager()
514 .alreadyMappedToFile(pdbId);
516 if (alreadyMapped != null)
519 * the PDB file is already loaded
521 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
522 MessageManager.formatMessage(
523 "label.pdb_entry_is_already_displayed",
524 new Object[] { pdbId }), MessageManager
526 "label.map_sequences_to_visible_window",
527 new Object[] { pdbId }),
528 JvOptionPane.YES_NO_CANCEL_OPTION);
529 if (option == JvOptionPane.CANCEL_OPTION)
533 else if (option == JvOptionPane.YES_OPTION)
535 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
542 void setChainMenuItems(List<String> chainNames)
544 chainMenu.removeAll();
545 if (chainNames == null || chainNames.isEmpty())
549 JMenuItem menuItem = new JMenuItem(
550 MessageManager.getString("label.all"));
551 menuItem.addActionListener(new ActionListener()
554 public void actionPerformed(ActionEvent evt)
556 allChainsSelected = true;
557 for (int i = 0; i < chainMenu.getItemCount(); i++)
559 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
561 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
564 showSelectedChains();
565 allChainsSelected = false;
569 chainMenu.add(menuItem);
571 for (String chain : chainNames)
573 menuItem = new JCheckBoxMenuItem(chain, true);
574 menuItem.addItemListener(new ItemListener()
577 public void itemStateChanged(ItemEvent evt)
579 if (!allChainsSelected)
581 showSelectedChains();
586 chainMenu.add(menuItem);
590 abstract void showSelectedChains();
593 * Action on selecting one of Jalview's registered colour schemes
596 public void changeColour_actionPerformed(String colourSchemeName)
598 AlignmentI al = getAlignmentPanel().av.getAlignment();
599 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
600 colourSchemeName, al, null);
601 getBinding().setJalviewColourScheme(cs);
605 * Builds the colour menu
607 protected void buildColourMenu()
609 colourMenu.removeAll();
610 AlignmentI al = getAlignmentPanel().av.getAlignment();
613 * add colour by sequence, by chain, by charge and cysteine
615 colourMenu.add(seqColour);
616 colourMenu.add(chainColour);
617 colourMenu.add(chargeColour);
618 chargeColour.setEnabled(!al.isNucleotide());
621 * add all 'simple' (per-residue) colour schemes registered to Jalview
623 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
627 * add 'colour by viewer' (menu item text is set in subclasses)
629 viewerColour.setSelected(false);
630 viewerColour.addActionListener(new ActionListener()
633 public void actionPerformed(ActionEvent actionEvent)
635 viewerColour_actionPerformed(actionEvent);
638 colourMenu.add(viewerColour);
641 * add 'set background colour'
643 JMenuItem backGround = new JMenuItem();
645 .setText(MessageManager.getString("action.background_colour"));
646 backGround.addActionListener(new ActionListener()
649 public void actionPerformed(ActionEvent actionEvent)
651 background_actionPerformed(actionEvent);
654 colourMenu.add(backGround);
657 * add colour buttons to a group so their selection is
658 * mutually exclusive (background colour is a separate option)
660 itemGroup.add(seqColour);
661 itemGroup.add(chainColour);
662 itemGroup.add(chargeColour);
663 itemGroup.add(viewerColour);
667 * Construct menu items
669 protected void initMenus()
671 AAStructureBindingModel binding = getBinding();
673 seqColour = new JRadioButtonMenuItem();
674 seqColour.setText(MessageManager.getString("action.by_sequence"));
675 seqColour.setName(ViewerColour.BySequence.name());
676 seqColour.setSelected(binding.isColourBySequence());
677 seqColour.addActionListener(new ActionListener()
680 public void actionPerformed(ActionEvent actionEvent)
682 seqColour_actionPerformed(actionEvent);
686 chainColour = new JRadioButtonMenuItem();
687 chainColour.setText(MessageManager.getString("action.by_chain"));
688 chainColour.setName(ViewerColour.ByChain.name());
689 chainColour.addActionListener(new ActionListener()
692 public void actionPerformed(ActionEvent actionEvent)
694 chainColour_actionPerformed(actionEvent);
698 chargeColour = new JRadioButtonMenuItem();
699 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
700 chargeColour.setName(ViewerColour.ChargeCysteine.name());
701 chargeColour.addActionListener(new ActionListener()
704 public void actionPerformed(ActionEvent actionEvent)
706 chargeColour_actionPerformed(actionEvent);
710 viewerColour = new JRadioButtonMenuItem();
711 // text is set in overrides of this method
712 viewerColour.setName(ViewerColour.ByViewer.name());
713 viewerColour.setSelected(!binding.isColourBySequence());
715 if (_colourwith == null)
717 _colourwith = new Vector<AlignmentPanel>();
719 if (_alignwith == null)
721 _alignwith = new Vector<AlignmentPanel>();
724 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
725 MessageManager.getString("label.colour_by"), this, _colourwith,
729 public void itemStateChanged(ItemEvent e)
731 if (!seqColour.isSelected())
737 // update the Chimera display now.
738 seqColour_actionPerformed(null);
742 viewMenu.add(seqColourBy);
744 final ItemListener handler = new ItemListener()
747 public void itemStateChanged(ItemEvent e)
749 alignStructs.setEnabled(!_alignwith.isEmpty());
751 .setToolTipText(MessageManager
752 .getString("label.align_structures_using_linked_alignment_views"));
755 viewSelectionMenu = new ViewSelectionMenu(
756 MessageManager.getString("label.superpose_with"), this,
757 _alignwith, handler);
758 handler.itemStateChanged(null);
759 viewerActionMenu.add(viewSelectionMenu, 0);
760 viewerActionMenu.addMenuListener(new MenuListener()
763 public void menuSelected(MenuEvent e)
765 handler.itemStateChanged(null);
769 public void menuDeselected(MenuEvent e)
774 public void menuCanceled(MenuEvent e)
783 public void setJalviewColourScheme(ColourSchemeI cs) {
784 getBinding().setJalviewColourScheme(cs);
787 protected void alignStructs_actionPerformed(ActionEvent actionEvent)
789 alignStructs_withAllAlignPanels();
791 protected void alignStructs_withAllAlignPanels()
793 if (getAlignmentPanel() == null)
798 if (_alignwith.size() == 0)
800 _alignwith.add(getAlignmentPanel());
805 AlignmentI[] als = new Alignment[_alignwith.size()];
806 ColumnSelection[] alc = new ColumnSelection[_alignwith.size()];
807 int[] alm = new int[_alignwith.size()];
810 for (AlignmentPanel ap : _alignwith)
812 als[a] = ap.av.getAlignment();
814 alc[a++] = ap.av.getColumnSelection();
816 getBinding().superposeStructures(als, alm, alc);
817 } catch (Exception e)
819 StringBuffer sp = new StringBuffer();
820 for (AlignmentPanel ap : _alignwith)
822 sp.append("'" + ap.alignFrame.getTitle() + "' ");
824 Cache.log.info("Couldn't align structures with the " + sp.toString()
825 + "associated alignment panels.", e);
829 public void background_actionPerformed(ActionEvent actionEvent)
831 Color col = JColorChooser.showDialog(this,
832 MessageManager.getString("label.select_background_colour"),
836 getBinding().setBackgroundColour(col);
840 public void viewerColour_actionPerformed(ActionEvent actionEvent)
842 if (viewerColour.isSelected())
844 // disable automatic sequence colouring.
845 getBinding().setColourBySequence(false);
849 public void chainColour_actionPerformed(ActionEvent actionEvent)
851 chainColour.setSelected(true);
852 getBinding().colourByChain();
855 public void chargeColour_actionPerformed(ActionEvent actionEvent)
857 chargeColour.setSelected(true);
858 getBinding().colourByCharge();
861 public void seqColour_actionPerformed(ActionEvent actionEvent)
863 AAStructureBindingModel binding = getBinding();
864 binding.setColourBySequence(seqColour.isSelected());
865 if (_colourwith == null)
867 _colourwith = new Vector<AlignmentPanel>();
869 if (binding.isColourBySequence())
871 if (!binding.isLoadingFromArchive())
873 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
875 // Make the currently displayed alignment panel the associated view
876 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
879 // Set the colour using the current view for the associated alignframe
880 for (AlignmentPanel ap : _colourwith)
882 binding.colourBySequence(ap);
887 public void pdbFile_actionPerformed(ActionEvent actionEvent)
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().getPdbFile()[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)
937 public void viewMapping_actionPerformed(ActionEvent actionEvent)
939 CutAndPasteTransfer cap = new CutAndPasteTransfer();
942 cap.appendText(getBinding().printMappings());
943 } catch (OutOfMemoryError e)
946 "composing sequence-structure alignments for display in text box.",
951 Desktop.addInternalFrame(cap,
952 MessageManager.getString("label.pdb_sequence_mapping"), 550,
956 protected abstract String getViewerName();
957 public void updateTitleAndMenus()
959 AAStructureBindingModel binding = getBinding();
960 if (binding.hasFileLoadingError())
965 setChainMenuItems(binding.getChainNames());
967 this.setTitle(binding.getViewerTitle(getViewerName(), true));
968 if (binding.getPdbFile().length > 1 && binding.getSequence().length > 1)
970 viewerActionMenu.setVisible(true);
972 if (!binding.isLoadingFromArchive())
974 seqColour_actionPerformed(null);