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;
114 * Default constructor
116 public StructureViewerBase()
123 * @return true if this Jmol instance is linked with the given alignPanel
125 public boolean isLinkedWith(AlignmentPanel ap2)
127 return _aps.contains(ap2.av.getSequenceSetId());
130 public boolean isUsedforaligment(AlignmentPanel ap2)
133 return (_alignwith != null) && _alignwith.contains(ap2);
136 public boolean isUsedforcolourby(AlignmentPanel ap2)
138 return (_colourwith != null) && _colourwith.contains(ap2);
143 * @return TRUE if the view is NOT being coloured by the alignment colours.
145 public boolean isColouredByViewer()
147 return !getBinding().isColourBySequence();
150 public String getViewId()
154 viewId = System.currentTimeMillis() + "." + this.hashCode();
159 protected void setViewId(String viewId)
161 this.viewId = viewId;
164 public abstract String getStateInfo();
166 protected void buildActionMenu()
168 if (_alignwith == null)
170 _alignwith = new Vector<AlignmentPanel>();
172 if (_alignwith.size() == 0 && ap != null)
177 for (Component c : viewerActionMenu.getMenuComponents())
179 if (c != alignStructs)
181 viewerActionMenu.remove((JMenuItem) c);
186 public AlignmentPanel getAlignmentPanel()
191 protected void setAlignmentPanel(AlignmentPanel alp)
197 public AlignmentPanel[] getAllAlignmentPanels()
199 AlignmentPanel[] t, list = new AlignmentPanel[0];
200 for (String setid : _aps)
202 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
205 t = new AlignmentPanel[list.length + panels.length];
206 System.arraycopy(list, 0, t, 0, list.length);
207 System.arraycopy(panels, 0, t, list.length, panels.length);
216 * set the primary alignmentPanel reference and add another alignPanel to the
217 * list of ones to use for colouring and aligning
221 public void addAlignmentPanel(AlignmentPanel nap)
223 if (getAlignmentPanel() == null)
225 setAlignmentPanel(nap);
227 if (!_aps.contains(nap.av.getSequenceSetId()))
229 _aps.add(nap.av.getSequenceSetId());
234 * remove any references held to the given alignment panel
238 public void removeAlignmentPanel(AlignmentPanel nap)
242 _alignwith.remove(nap);
243 _colourwith.remove(nap);
244 if (getAlignmentPanel() == nap)
246 setAlignmentPanel(null);
247 for (AlignmentPanel aps : getAllAlignmentPanels())
251 setAlignmentPanel(aps);
256 } catch (Exception ex)
259 if (getAlignmentPanel() != null)
265 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
267 addAlignmentPanel(nap);
268 if (!_alignwith.contains(nap))
274 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
276 if (_alignwith.contains(nap))
278 _alignwith.remove(nap);
282 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
283 boolean enableColourBySeq)
285 useAlignmentPanelForColourbyseq(nap);
286 getBinding().setColourBySequence(enableColourBySeq);
287 seqColour.setSelected(enableColourBySeq);
288 viewerColour.setSelected(!enableColourBySeq);
291 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
293 addAlignmentPanel(nap);
294 if (!_colourwith.contains(nap))
296 _colourwith.add(nap);
300 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
302 if (_colourwith.contains(nap))
304 _colourwith.remove(nap);
308 public abstract ViewerType getViewerType();
311 * add a new structure (with associated sequences and chains) to this viewer,
312 * retrieving it if necessary first.
318 * if true, new structure(s) will be aligned using associated
322 protected void addStructure(final PDBEntry pdbentry,
323 final SequenceI[] seqs, final String[] chains,
324 final boolean align, final IProgressIndicator alignFrame)
326 if (pdbentry.getFile() == null)
328 if (worker != null && worker.isAlive())
330 // a retrieval is in progress, wait around and add ourselves to the
332 new Thread(new Runnable()
337 while (worker != null && worker.isAlive() && _started)
341 Thread.sleep(100 + ((int) Math.random() * 100));
343 } catch (Exception e)
347 // and call ourselves again.
348 addStructure(pdbentry, seqs, chains, align, alignFrame);
354 // otherwise, start adding the structure.
355 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
356 new SequenceI[][] { seqs }, new String[][] { chains });
357 addingStructures = true;
359 alignAddedStructures = align;
360 worker = new Thread(this);
366 * Presents a dialog with the option to add an align a structure to an
367 * existing structure view
371 * @return YES, NO or CANCEL JvOptionPane code
373 protected int chooseAlignStructureToViewer(String pdbId,
374 StructureViewerBase view)
376 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
377 MessageManager.formatMessage("label.add_pdbentry_to_view",
378 new Object[] { pdbId, view.getTitle() }),
380 .getString("label.align_to_existing_structure_view"),
381 JvOptionPane.YES_NO_CANCEL_OPTION);
385 protected boolean hasPdbId(String pdbId)
387 return getBinding().hasPdbId(pdbId);
390 protected abstract List<StructureViewerBase> getViewersFor(
394 * Check for any existing views involving this alignment and give user the
395 * option to add and align this molecule to one of them
402 * @return true if user adds to a view, or cancels entirely, else false
404 protected boolean addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
405 String[] chains, final AlignmentPanel apanel, String pdbId)
407 for (StructureViewerBase view : getViewersFor(apanel))
409 // TODO: highlight the view somehow
411 * JAL-1742 exclude view with this structure already mapped (don't offer
412 * to align chain B to chain A of the same structure)
414 if (view.hasPdbId(pdbId))
418 int option = chooseAlignStructureToViewer(pdbId, view);
419 if (option == JvOptionPane.CANCEL_OPTION)
423 else if (option == JvOptionPane.YES_OPTION)
425 view.useAlignmentPanelForSuperposition(apanel);
426 view.addStructure(pdbentry, seq, chains, true, apanel.alignFrame);
431 // NO_OPTION - offer the next viewer if any
436 * nothing offered and selected
442 * Adds mappings for the given sequences to an already opened PDB structure,
443 * and updates any viewers that have the PDB file
450 protected void addSequenceMappingsToStructure(SequenceI[] seq,
451 String[] chains, final AlignmentPanel apanel, String pdbFilename)
453 // TODO : Fix multiple seq to one chain issue here.
455 * create the mappings
457 apanel.getStructureSelectionManager().setMapping(seq, chains,
458 pdbFilename, DataSourceType.FILE);
461 * alert the FeatureRenderer to show new (PDB RESNUM) features
463 if (apanel.getSeqPanel().seqCanvas.fr != null)
465 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
466 apanel.paintAlignment(true);
470 * add the sequences to any other viewers (of the same type) for this pdb
473 // JBPNOTE: this looks like a binding routine, rather than a gui routine
474 for (StructureViewerBase viewer : getViewersFor(null))
476 AAStructureBindingModel bindingModel = viewer.getBinding();
477 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
479 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
481 bindingModel.addSequence(pe, seq);
482 viewer.addAlignmentPanel(apanel);
484 * add it to the set of alignments used for colouring structure by
487 viewer.useAlignmentPanelForColourbyseq(apanel);
488 viewer.buildActionMenu();
489 apanel.getStructureSelectionManager().sequenceColoursChanged(
498 * Check if the PDB file is already loaded, if so offer to add it to the
505 * @return true if the user chooses to add to a viewer, or to cancel entirely
507 protected boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
508 final AlignmentPanel apanel, String pdbId)
510 boolean finished = false;
511 String alreadyMapped = apanel.getStructureSelectionManager()
512 .alreadyMappedToFile(pdbId);
514 if (alreadyMapped != null)
517 * the PDB file is already loaded
519 int option = JvOptionPane.showInternalConfirmDialog(Desktop.desktop,
520 MessageManager.formatMessage(
521 "label.pdb_entry_is_already_displayed",
522 new Object[] { pdbId }), MessageManager
524 "label.map_sequences_to_visible_window",
525 new Object[] { pdbId }),
526 JvOptionPane.YES_NO_CANCEL_OPTION);
527 if (option == JvOptionPane.CANCEL_OPTION)
531 else if (option == JvOptionPane.YES_OPTION)
533 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
540 void setChainMenuItems(List<String> chainNames)
542 chainMenu.removeAll();
543 if (chainNames == null || chainNames.isEmpty())
547 JMenuItem menuItem = new JMenuItem(
548 MessageManager.getString("label.all"));
549 menuItem.addActionListener(new ActionListener()
552 public void actionPerformed(ActionEvent evt)
554 allChainsSelected = true;
555 for (int i = 0; i < chainMenu.getItemCount(); i++)
557 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
559 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
562 showSelectedChains();
563 allChainsSelected = false;
567 chainMenu.add(menuItem);
569 for (String chain : chainNames)
571 menuItem = new JCheckBoxMenuItem(chain, true);
572 menuItem.addItemListener(new ItemListener()
575 public void itemStateChanged(ItemEvent evt)
577 if (!allChainsSelected)
579 showSelectedChains();
584 chainMenu.add(menuItem);
588 abstract void showSelectedChains();
591 * Action on selecting one of Jalview's registered colour schemes
594 public void changeColour_actionPerformed(String colourSchemeName)
596 AlignmentI al = getAlignmentPanel().av.getAlignment();
597 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
598 colourSchemeName, al, null);
599 getBinding().setJalviewColourScheme(cs);
603 * Builds the colour menu
605 protected void buildColourMenu()
607 colourMenu.removeAll();
608 AlignmentI al = getAlignmentPanel().av.getAlignment();
611 * add colour by sequence, by chain, by charge and cysteine
613 colourMenu.add(seqColour);
614 colourMenu.add(chainColour);
615 colourMenu.add(chargeColour);
616 chargeColour.setEnabled(!al.isNucleotide());
619 * add all 'simple' (per-residue) colour schemes registered to Jalview
621 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
625 * add 'colour by viewer' (menu item text is set in subclasses)
627 viewerColour.setSelected(false);
628 viewerColour.addActionListener(new ActionListener()
631 public void actionPerformed(ActionEvent actionEvent)
633 viewerColour_actionPerformed(actionEvent);
636 colourMenu.add(viewerColour);
639 * add 'set background colour'
641 JMenuItem backGround = new JMenuItem();
643 .setText(MessageManager.getString("action.background_colour"));
644 backGround.addActionListener(new ActionListener()
647 public void actionPerformed(ActionEvent actionEvent)
649 background_actionPerformed(actionEvent);
652 colourMenu.add(backGround);
655 * add colour buttons to a group so their selection is
656 * mutually exclusive (background colour is a separate option)
658 itemGroup.add(seqColour);
659 itemGroup.add(chainColour);
660 itemGroup.add(chargeColour);
661 itemGroup.add(viewerColour);
665 * Construct menu items
667 protected void initMenus()
669 AAStructureBindingModel binding = getBinding();
671 seqColour = new JRadioButtonMenuItem();
672 seqColour.setText(MessageManager.getString("action.by_sequence"));
673 seqColour.setName(ViewerColour.BySequence.name());
674 seqColour.setSelected(binding.isColourBySequence());
675 seqColour.addActionListener(new ActionListener()
678 public void actionPerformed(ActionEvent actionEvent)
680 seqColour_actionPerformed(actionEvent);
684 chainColour = new JRadioButtonMenuItem();
685 chainColour.setText(MessageManager.getString("action.by_chain"));
686 chainColour.setName(ViewerColour.ByChain.name());
687 chainColour.addActionListener(new ActionListener()
690 public void actionPerformed(ActionEvent actionEvent)
692 chainColour_actionPerformed(actionEvent);
696 chargeColour = new JRadioButtonMenuItem();
697 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
698 chargeColour.setName(ViewerColour.ChargeCysteine.name());
699 chargeColour.addActionListener(new ActionListener()
702 public void actionPerformed(ActionEvent actionEvent)
704 chargeColour_actionPerformed(actionEvent);
708 viewerColour = new JRadioButtonMenuItem();
709 // text is set in overrides of this method
710 viewerColour.setName(ViewerColour.ByViewer.name());
711 viewerColour.setSelected(!binding.isColourBySequence());
713 if (_colourwith == null)
715 _colourwith = new Vector<AlignmentPanel>();
717 if (_alignwith == null)
719 _alignwith = new Vector<AlignmentPanel>();
722 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
723 MessageManager.getString("label.colour_by"), this, _colourwith,
727 public void itemStateChanged(ItemEvent e)
729 if (!seqColour.isSelected())
735 // update the Chimera display now.
736 seqColour_actionPerformed(null);
740 viewMenu.add(seqColourBy);
742 final ItemListener handler = new ItemListener()
745 public void itemStateChanged(ItemEvent e)
747 alignStructs.setEnabled(_alignwith.size() > 0);
748 alignStructs.setToolTipText(MessageManager.formatMessage(
749 "label.align_structures_using_linked_alignment_views",
750 new String[] { String.valueOf(_alignwith.size()) }));
753 JMenu alpanels = new ViewSelectionMenu(
754 MessageManager.getString("label.superpose_with"), this,
755 _alignwith, handler);
756 handler.itemStateChanged(null);
757 viewerActionMenu.add(alpanels);
758 viewerActionMenu.addMenuListener(new MenuListener()
761 public void menuSelected(MenuEvent e)
763 handler.itemStateChanged(null);
767 public void menuDeselected(MenuEvent e)
772 public void menuCanceled(MenuEvent e)
781 public void setJalviewColourScheme(ColourSchemeI cs) {
782 getBinding().setJalviewColourScheme(cs);
785 protected void alignStructs_actionPerformed(ActionEvent actionEvent)
787 alignStructs_withAllAlignPanels();
789 protected void alignStructs_withAllAlignPanels()
791 if (getAlignmentPanel() == null)
796 if (_alignwith.size() == 0)
798 _alignwith.add(getAlignmentPanel());
803 AlignmentI[] als = new Alignment[_alignwith.size()];
804 ColumnSelection[] alc = new ColumnSelection[_alignwith.size()];
805 int[] alm = new int[_alignwith.size()];
808 for (AlignmentPanel ap : _alignwith)
810 als[a] = ap.av.getAlignment();
812 alc[a++] = ap.av.getColumnSelection();
814 getBinding().superposeStructures(als, alm, alc);
815 } catch (Exception e)
817 StringBuffer sp = new StringBuffer();
818 for (AlignmentPanel ap : _alignwith)
820 sp.append("'" + ap.alignFrame.getTitle() + "' ");
822 Cache.log.info("Couldn't align structures with the " + sp.toString()
823 + "associated alignment panels.", e);
827 public void background_actionPerformed(ActionEvent actionEvent)
829 Color col = JColorChooser.showDialog(this,
830 MessageManager.getString("label.select_background_colour"),
834 getBinding().setBackgroundColour(col);
838 public void viewerColour_actionPerformed(ActionEvent actionEvent)
840 if (viewerColour.isSelected())
842 // disable automatic sequence colouring.
843 getBinding().setColourBySequence(false);
847 public void chainColour_actionPerformed(ActionEvent actionEvent)
849 chainColour.setSelected(true);
850 getBinding().colourByChain();
853 public void chargeColour_actionPerformed(ActionEvent actionEvent)
855 chargeColour.setSelected(true);
856 getBinding().colourByCharge();
859 public void seqColour_actionPerformed(ActionEvent actionEvent)
861 AAStructureBindingModel binding = getBinding();
862 binding.setColourBySequence(seqColour.isSelected());
863 if (_colourwith == null)
865 _colourwith = new Vector<AlignmentPanel>();
867 if (binding.isColourBySequence())
869 if (!binding.isLoadingFromArchive())
871 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
873 // Make the currently displayed alignment panel the associated view
874 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
877 // Set the colour using the current view for the associated alignframe
878 for (AlignmentPanel ap : _colourwith)
880 binding.colourBySequence(ap);
885 public void pdbFile_actionPerformed(ActionEvent actionEvent)
887 JalviewFileChooser chooser = new JalviewFileChooser(
888 Cache.getProperty("LAST_DIRECTORY"));
890 chooser.setFileView(new JalviewFileView());
891 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
892 chooser.setToolTipText(MessageManager.getString("action.save"));
894 int value = chooser.showSaveDialog(this);
896 if (value == JalviewFileChooser.APPROVE_OPTION)
898 BufferedReader in = null;
901 // TODO: cope with multiple PDB files in view
902 in = new BufferedReader(
903 new FileReader(getBinding().getPdbFile()[0]));
904 File outFile = chooser.getSelectedFile();
906 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
908 while ((data = in.readLine()) != null)
910 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
916 } catch (Exception ex)
918 ex.printStackTrace();
926 } catch (IOException e)
935 public void viewMapping_actionPerformed(ActionEvent actionEvent)
937 CutAndPasteTransfer cap = new CutAndPasteTransfer();
940 cap.appendText(getBinding().printMappings());
941 } catch (OutOfMemoryError e)
944 "composing sequence-structure alignments for display in text box.",
949 Desktop.addInternalFrame(cap,
950 MessageManager.getString("label.pdb_sequence_mapping"), 550,
954 protected abstract String getViewerName();
955 public void updateTitleAndMenus()
957 AAStructureBindingModel binding = getBinding();
958 if (binding.hasFileLoadingError())
963 setChainMenuItems(binding.getChainNames());
965 this.setTitle(binding.getViewerTitle(getViewerName(), true));
966 if (binding.getPdbFile().length > 1 && binding.getSequence().length > 1)
968 viewerActionMenu.setVisible(true);
970 if (!binding.isLoadingFromArchive())
972 seqColour_actionPerformed(null);