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 java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.ItemEvent;
28 import java.awt.event.ItemListener;
29 import java.beans.PropertyVetoException;
30 import java.io.BufferedReader;
32 import java.io.FileOutputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.PrintWriter;
36 import java.util.ArrayList;
37 import java.util.List;
39 import java.util.Random;
40 import java.util.Vector;
42 import javax.swing.ButtonGroup;
43 import javax.swing.JCheckBoxMenuItem;
44 import javax.swing.JMenu;
45 import javax.swing.JMenuItem;
46 import javax.swing.JRadioButtonMenuItem;
47 import javax.swing.event.MenuEvent;
48 import javax.swing.event.MenuListener;
50 import jalview.api.AlignmentViewPanel;
51 import jalview.api.structures.JalviewStructureDisplayI;
52 import jalview.bin.Cache;
53 import jalview.bin.Console;
54 import jalview.datamodel.AlignmentI;
55 import jalview.datamodel.PDBEntry;
56 import jalview.datamodel.SequenceI;
57 import jalview.gui.JalviewColourChooser.ColourChooserListener;
58 import jalview.gui.StructureViewer.ViewerType;
59 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
60 import jalview.io.DataSourceType;
61 import jalview.io.JalviewFileChooser;
62 import jalview.io.JalviewFileView;
63 import jalview.jbgui.GStructureViewer;
64 import jalview.schemes.ColourSchemeI;
65 import jalview.schemes.ColourSchemes;
66 import jalview.structure.StructureMapping;
67 import jalview.structures.models.AAStructureBindingModel;
68 import jalview.util.BrowserLauncher;
69 import jalview.util.MessageManager;
70 import jalview.ws.dbsources.EBIAlfaFold;
71 import jalview.ws.dbsources.Pdb;
72 import jalview.ws.utils.UrlDownloadClient;
75 * Base class with common functionality for JMol, Chimera or other structure
81 public abstract class StructureViewerBase extends GStructureViewer
82 implements Runnable, ViewSetProvider
85 * names for colour options (additional to Jalview colour schemes)
89 BySequence, ByChain, ChargeCysteine, ByViewer
93 * Singleton list of all (open) instances of structureViewerBase
94 * TODO: JAL-3362 - review and adopt the swingJS-safe singleton pattern so each structure viewer base instance is kept to its own JalviewJS parent
96 private static List<JalviewStructureDisplayI> svbs = new ArrayList<>();
100 * @return list with all existing StructureViewers instance
102 public static List<JalviewStructureDisplayI> getAllStructureViewerBases()
104 List<JalviewStructureDisplayI> goodSvbs = new ArrayList<>();
105 for (JalviewStructureDisplayI s : svbs)
107 if (s != null && !goodSvbs.contains(s))
116 * list of sequenceSet ids associated with the view
118 protected List<String> _aps = new ArrayList<>();
121 * list of alignment panels to use for superposition
123 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
126 * list of alignment panels that are used for colouring structures by aligned
129 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
131 private String viewId = null;
133 private AlignmentPanel ap;
135 protected boolean alignAddedStructures = false;
137 protected volatile boolean _started = false;
139 protected volatile boolean addingStructures = false;
141 protected Thread worker = null;
143 protected boolean allChainsSelected = false;
145 protected boolean allHetatmBeingSelected = false;
147 protected JMenu viewSelectionMenu;
150 * set after sequence colouring has been applied for this structure viewer.
151 * used to determine if the final sequence/structure mapping has been
154 protected volatile boolean seqColoursApplied = false;
156 private IProgressIndicator progressBar = null;
158 private Random random = new Random();
161 * Default constructor
163 public StructureViewerBase()
171 * @return true if added structures should be aligned to existing one(s)
174 public boolean isAlignAddedStructures()
176 return alignAddedStructures;
182 * if added structures should be aligned to existing one(s)
185 public void setAlignAddedStructures(boolean alignAdded)
187 alignAddedStructures = alignAdded;
191 * called by the binding model to indicate when adding structures is happening
192 * or has been completed
194 * @param addingStructures
196 public synchronized void setAddingStructures(boolean addingStructures)
198 this.addingStructures = addingStructures;
204 * @return true if this Jmol instance is linked with the given alignPanel
206 public boolean isLinkedWith(AlignmentPanel ap2)
208 return _aps.contains(ap2.av.getSequenceSetId());
212 public boolean isUsedforaligment(AlignmentViewPanel ap2)
215 return (_alignwith != null) && _alignwith.contains(ap2);
219 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
221 return (_colourwith != null) && _colourwith.contains(ap2);
226 * @return TRUE if the view is NOT being coloured by the alignment colours.
228 public boolean isColouredByViewer()
230 return !getBinding().isColourBySequence();
233 public String getViewId()
237 viewId = System.currentTimeMillis() + "." + this.hashCode();
242 protected void setViewId(String viewId)
244 this.viewId = viewId;
247 protected void buildActionMenu()
249 if (_alignwith == null)
251 _alignwith = new Vector<>();
253 if (_alignwith.size() == 0 && ap != null)
258 // TODO: refactor to allow concrete classes to register buttons for adding
260 // currently have to override to add buttons back in after they are cleared
262 for (Component c : viewerActionMenu.getMenuComponents())
264 if (c != alignStructs)
266 viewerActionMenu.remove((JMenuItem) c);
272 public AlignmentPanel getAlignmentPanel()
277 protected void setAlignmentPanel(AlignmentPanel alp)
283 public AlignmentPanel[] getAllAlignmentPanels()
285 AlignmentPanel[] t, list = new AlignmentPanel[0];
286 for (String setid : _aps)
288 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
291 t = new AlignmentPanel[list.length + panels.length];
292 System.arraycopy(list, 0, t, 0, list.length);
293 System.arraycopy(panels, 0, t, list.length, panels.length);
302 * set the primary alignmentPanel reference and add another alignPanel to the
303 * list of ones to use for colouring and aligning
307 public void addAlignmentPanel(AlignmentPanel nap)
309 if (getAlignmentPanel() == null)
311 setAlignmentPanel(nap);
313 if (!_aps.contains(nap.av.getSequenceSetId()))
315 _aps.add(nap.av.getSequenceSetId());
320 * remove any references held to the given alignment panel
325 public void removeAlignmentPanel(AlignmentViewPanel nap)
329 _alignwith.remove(nap);
330 _colourwith.remove(nap);
331 if (getAlignmentPanel() == nap)
333 setAlignmentPanel(null);
334 for (AlignmentPanel aps : getAllAlignmentPanels())
338 setAlignmentPanel(aps);
343 } catch (Exception ex)
346 if (getAlignmentPanel() != null)
352 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
354 addAlignmentPanel(nap);
355 if (!_alignwith.contains(nap))
361 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
363 if (_alignwith.contains(nap))
365 _alignwith.remove(nap);
369 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
370 boolean enableColourBySeq)
372 useAlignmentPanelForColourbyseq(nap);
373 getBinding().setColourBySequence(enableColourBySeq);
374 seqColour.setSelected(enableColourBySeq);
375 viewerColour.setSelected(!enableColourBySeq);
378 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
380 addAlignmentPanel(nap);
381 if (!_colourwith.contains(nap))
383 _colourwith.add(nap);
387 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
389 if (_colourwith.contains(nap))
391 _colourwith.remove(nap);
395 public abstract ViewerType getViewerType();
398 * add a new structure (with associated sequences and chains) to this viewer,
399 * retrieving it if necessary first.
405 * if true, new structure(s) will be aligned using associated
409 protected void addStructure(final PDBEntry pdbentry,
410 final SequenceI[] seqs, final String[] chains,
411 final IProgressIndicator alignFrame)
413 if (pdbentry.getFile() == null)
415 if (worker != null && worker.isAlive())
417 // a retrieval is in progress, wait around and add ourselves to the
419 new Thread(new Runnable()
424 while (worker != null && worker.isAlive() && _started)
428 Thread.sleep(100 + ((int) Math.random() * 100));
430 } catch (Exception e)
434 // and call ourselves again.
435 addStructure(pdbentry, seqs, chains, alignFrame);
441 // otherwise, start adding the structure.
442 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
444 { seqs }, new String[][] { chains });
445 addingStructures = true;
447 worker = new Thread(this);
452 protected boolean hasPdbId(String pdbId)
454 return getBinding().hasPdbId(pdbId);
458 * Returns a list of any viewer of the instantiated type. The list is
459 * restricted to those linked to the given alignment panel if it is not null.
461 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
463 return Desktop.instance.getStructureViewers(alp, this.getClass());
467 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
468 String[] chains, final AlignmentViewPanel apanel, String pdbId)
471 * JAL-1742 exclude view with this structure already mapped (don't offer
472 * to align chain B to chain A of the same structure); code may defend
473 * against this possibility before we reach here
479 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
482 useAlignmentPanelForSuperposition(alignPanel);
483 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
487 * Adds mappings for the given sequences to an already opened PDB structure,
488 * and updates any viewers that have the PDB file
495 public void addSequenceMappingsToStructure(SequenceI[] seq,
496 String[] chains, final AlignmentViewPanel alpanel,
499 AlignmentPanel apanel = (AlignmentPanel) alpanel;
501 // TODO : Fix multiple seq to one chain issue here.
503 * create the mappings
505 apanel.getStructureSelectionManager().setMapping(seq, chains,
506 pdbFilename, DataSourceType.FILE, getProgressIndicator());
509 * alert the FeatureRenderer to show new (PDB RESNUM) features
511 if (apanel.getSeqPanel().seqCanvas.fr != null)
513 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
514 // note - we don't do a refresh for structure here because we do it
515 // explicitly for all panels later on
516 apanel.paintAlignment(true, false);
520 * add the sequences to any other viewers (of the same type) for this pdb
523 // JBPNOTE: this looks like a binding routine, rather than a gui routine
524 for (StructureViewerBase viewer : getViewersFor(null))
526 AAStructureBindingModel bindingModel = viewer.getBinding();
527 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
529 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
531 bindingModel.addSequence(pe, seq);
532 viewer.addAlignmentPanel(apanel);
534 * add it to the set of alignments used for colouring structure by
537 viewer.useAlignmentPanelForColourbyseq(apanel);
538 viewer.buildActionMenu();
539 apanel.getStructureSelectionManager()
540 .sequenceColoursChanged(apanel);
548 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
549 final AlignmentViewPanel apanel, String pdbId)
551 String alreadyMapped = apanel.getStructureSelectionManager()
552 .alreadyMappedToFile(pdbId);
554 if (alreadyMapped == null)
559 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
563 void setChainMenuItems(List<String> chainNames)
565 chainMenu.removeAll();
566 if (chainNames == null || chainNames.isEmpty())
570 JMenuItem menuItem = new JMenuItem(
571 MessageManager.getString("label.all"));
572 menuItem.addActionListener(new ActionListener()
575 public void actionPerformed(ActionEvent evt)
577 allChainsSelected = true;
578 for (int i = 0; i < chainMenu.getItemCount(); i++)
580 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
582 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
585 showSelectedChains();
586 allChainsSelected = false;
590 chainMenu.add(menuItem);
592 for (String chain : chainNames)
594 menuItem = new JCheckBoxMenuItem(chain, true);
595 menuItem.addItemListener(new ItemListener()
598 public void itemStateChanged(ItemEvent evt)
600 if (!allChainsSelected)
602 showSelectedChains();
607 chainMenu.add(menuItem);
610 void setHetatmMenuItems(Map<String,String> hetatmNames)
612 hetatmMenu.removeAll();
613 if (hetatmNames == null || hetatmNames.isEmpty())
615 hetatmMenu.setVisible(false);
618 hetatmMenu.setVisible(true);
619 allHetatmBeingSelected=false;
620 JMenuItem allMenuItem = new JMenuItem(
621 MessageManager.getString("label.all"));
622 JMenuItem noneMenuItem = new JMenuItem(
623 MessageManager.getString("label.none"));
624 allMenuItem.addActionListener(new ActionListener()
627 public void actionPerformed(ActionEvent e) {
629 allHetatmBeingSelected=true;
630 // Toggle state of everything - on
631 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
633 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
635 ((JCheckBoxMenuItem) hetatmMenu.getItem(i)).setSelected(true);
638 allHetatmBeingSelected=false;
639 showSelectedHetatms();
643 noneMenuItem.addActionListener(new ActionListener()
646 public void actionPerformed(ActionEvent e) {
648 allHetatmBeingSelected=true;
649 // Toggle state of everything off
650 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
652 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
654 ((JCheckBoxMenuItem) hetatmMenu.getItem(i)).setSelected(false);
657 allHetatmBeingSelected=false;
658 showSelectedHetatms();
661 hetatmMenu.add(noneMenuItem);
662 hetatmMenu.add(allMenuItem);
664 for (Map.Entry<String, String> chain : hetatmNames.entrySet())
666 JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(chain.getKey(), false);
667 menuItem.setToolTipText(chain.getValue());
668 menuItem.addItemListener(new ItemListener()
671 public void itemStateChanged(ItemEvent evt)
673 if (!allHetatmBeingSelected)
675 // update viewer only when we were clicked, not programmatically
677 showSelectedHetatms();
682 hetatmMenu.add(menuItem);
687 * Action on selecting one of Jalview's registered colour schemes
690 public void changeColour_actionPerformed(String colourSchemeName)
692 AlignmentI al = getAlignmentPanel().av.getAlignment();
693 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
694 colourSchemeName, getAlignmentPanel().av, al, null);
695 getBinding().colourByJalviewColourScheme(cs);
699 * Builds the colour menu
701 protected void buildColourMenu()
703 colourMenu.removeAll();
704 AlignmentI al = getAlignmentPanel().av.getAlignment();
707 * add colour by sequence, by chain, by charge and cysteine
709 colourMenu.add(seqColour);
710 colourMenu.add(chainColour);
711 colourMenu.add(chargeColour);
712 chargeColour.setEnabled(!al.isNucleotide());
715 * add all 'simple' (per-residue) colour schemes registered to Jalview
717 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
721 * add 'colour by viewer' (menu item text is set in subclasses)
723 viewerColour.setSelected(false);
724 viewerColour.addActionListener(new ActionListener()
727 public void actionPerformed(ActionEvent actionEvent)
729 viewerColour_actionPerformed();
732 colourMenu.add(viewerColour);
735 * add 'set background colour'
737 JMenuItem backGround = new JMenuItem();
739 .setText(MessageManager.getString("action.background_colour"));
740 backGround.addActionListener(new ActionListener()
743 public void actionPerformed(ActionEvent actionEvent)
745 background_actionPerformed();
748 colourMenu.add(backGround);
751 * add colour buttons to a group so their selection is
752 * mutually exclusive (background colour is a separate option)
754 itemGroup.add(seqColour);
755 itemGroup.add(chainColour);
756 itemGroup.add(chargeColour);
757 itemGroup.add(viewerColour);
761 * Construct menu items
763 protected void initMenus()
765 AAStructureBindingModel binding = getBinding();
767 seqColour = new JRadioButtonMenuItem();
768 seqColour.setText(MessageManager.getString("action.by_sequence"));
769 seqColour.setName(ViewerColour.BySequence.name());
770 seqColour.setSelected(binding.isColourBySequence());
771 seqColour.addActionListener(new ActionListener()
774 public void actionPerformed(ActionEvent actionEvent)
776 seqColour_actionPerformed();
780 chainColour = new JRadioButtonMenuItem();
781 chainColour.setText(MessageManager.getString("action.by_chain"));
782 chainColour.setName(ViewerColour.ByChain.name());
783 chainColour.addActionListener(new ActionListener()
786 public void actionPerformed(ActionEvent actionEvent)
788 chainColour_actionPerformed();
792 chargeColour = new JRadioButtonMenuItem();
793 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
794 chargeColour.setName(ViewerColour.ChargeCysteine.name());
795 chargeColour.addActionListener(new ActionListener()
798 public void actionPerformed(ActionEvent actionEvent)
800 chargeColour_actionPerformed();
804 viewerColour = new JRadioButtonMenuItem();
806 .setText(MessageManager.getString("label.colour_with_viewer"));
807 viewerColour.setToolTipText(MessageManager
808 .getString("label.let_viewer_manage_structure_colours"));
809 viewerColour.setName(ViewerColour.ByViewer.name());
810 viewerColour.setSelected(!binding.isColourBySequence());
812 if (_colourwith == null)
814 _colourwith = new Vector<>();
816 if (_alignwith == null)
818 _alignwith = new Vector<>();
821 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
822 MessageManager.getString("label.colour_by"), this, _colourwith,
826 public void itemStateChanged(ItemEvent e)
828 if (!seqColour.isSelected())
834 // update the viewer display now.
835 seqColour_actionPerformed();
839 viewMenu.add(seqColourBy);
841 final ItemListener handler = new ItemListener()
844 public void itemStateChanged(ItemEvent e)
846 if (_alignwith.isEmpty())
848 alignStructs.setEnabled(false);
849 alignStructs.setToolTipText(null);
853 alignStructs.setEnabled(true);
854 alignStructs.setToolTipText(MessageManager.formatMessage(
855 "label.align_structures_using_linked_alignment_views",
860 viewSelectionMenu = new ViewSelectionMenu(
861 MessageManager.getString("label.superpose_with"), this,
862 _alignwith, handler);
863 handler.itemStateChanged(null);
864 viewerActionMenu.add(viewSelectionMenu, 0);
865 viewerActionMenu.addMenuListener(new MenuListener()
868 public void menuSelected(MenuEvent e)
870 handler.itemStateChanged(null);
874 public void menuDeselected(MenuEvent e)
879 public void menuCanceled(MenuEvent e)
884 viewerActionMenu.setText(getViewerName());
885 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
892 * Sends commands to the structure viewer to superimpose structures based on
893 * currently associated alignments. May optionally return an error message for
897 protected String alignStructsWithAllAlignPanels()
899 if (getAlignmentPanel() == null)
904 if (_alignwith.size() == 0)
906 _alignwith.add(getAlignmentPanel());
912 reply = getBinding().superposeStructures(_alignwith);
913 if (reply != null && !reply.isEmpty())
915 String text = MessageManager
916 .formatMessage("error.superposition_failed", reply);
917 statusBar.setText(text);
919 } catch (Exception e)
921 StringBuffer sp = new StringBuffer();
922 for (AlignmentViewPanel alignPanel : _alignwith)
924 sp.append("'" + alignPanel.getViewName() + "' ");
926 Console.info("Couldn't align structures with the " + sp.toString()
927 + "associated alignment panels.", e);
933 * Opens a colour chooser dialog, and applies the chosen colour to the
934 * background of the structure viewer
937 public void background_actionPerformed()
939 String ttl = MessageManager.getString("label.select_background_colour");
940 ColourChooserListener listener = new ColourChooserListener()
943 public void colourSelected(Color c)
945 getBinding().setBackgroundColour(c);
948 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
952 public void viewerColour_actionPerformed()
954 if (viewerColour.isSelected())
956 // disable automatic sequence colouring.
957 getBinding().setColourBySequence(false);
962 public void chainColour_actionPerformed()
964 chainColour.setSelected(true);
965 getBinding().colourByChain();
969 public void chargeColour_actionPerformed()
971 chargeColour.setSelected(true);
972 getBinding().colourByCharge();
976 public void seqColour_actionPerformed()
978 AAStructureBindingModel binding = getBinding();
979 binding.setColourBySequence(seqColour.isSelected());
980 if (_colourwith == null)
982 _colourwith = new Vector<>();
984 if (binding.isColourBySequence())
986 if (!binding.isLoadingFromArchive())
988 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
990 // Make the currently displayed alignment panel the associated view
991 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
994 // Set the colour using the current view for the associated alignframe
995 for (AlignmentViewPanel alignPanel : _colourwith)
997 binding.colourBySequence(alignPanel);
999 seqColoursApplied = true;
1004 public void pdbFile_actionPerformed()
1006 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
1007 JalviewFileChooser chooser = new JalviewFileChooser(
1008 Cache.getProperty("LAST_DIRECTORY"));
1010 chooser.setFileView(new JalviewFileView());
1011 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
1012 chooser.setToolTipText(MessageManager.getString("action.save"));
1014 int value = chooser.showSaveDialog(this);
1016 if (value == JalviewFileChooser.APPROVE_OPTION)
1018 BufferedReader in = null;
1021 // TODO: cope with multiple PDB files in view
1022 in = new BufferedReader(
1023 new FileReader(getBinding().getStructureFiles()[0]));
1024 File outFile = chooser.getSelectedFile();
1026 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1028 while ((data = in.readLine()) != null)
1030 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1036 } catch (Exception ex)
1038 ex.printStackTrace();
1046 } catch (IOException e)
1056 public void viewMapping_actionPerformed()
1058 CutAndPasteTransfer cap = new CutAndPasteTransfer();
1061 cap.appendText(getBinding().printMappings());
1062 } catch (OutOfMemoryError e)
1065 "composing sequence-structure alignments for display in text box.",
1070 Desktop.addInternalFrame(cap,
1071 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1075 protected abstract String getViewerName();
1078 * Configures the title and menu items of the viewer panel.
1081 public void updateTitleAndMenus()
1083 AAStructureBindingModel binding = getBinding();
1084 if (binding.hasFileLoadingError())
1089 setChainMenuItems(binding.getChainNames());
1090 setHetatmMenuItems(binding.getHetatmNames());
1092 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1095 * enable 'Superpose with' if more than one mapped structure
1097 viewSelectionMenu.setEnabled(false);
1098 if (getBinding().getMappedStructureCount() > 1
1099 && getBinding().getSequence().length > 1)
1101 viewSelectionMenu.setEnabled(true);
1105 * Show action menu if it has any enabled items
1107 viewerActionMenu.setVisible(false);
1108 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1110 if (viewerActionMenu.getItem(i).isEnabled())
1112 viewerActionMenu.setVisible(true);
1117 if (!binding.isLoadingFromArchive())
1119 seqColour_actionPerformed();
1124 public String toString()
1130 public boolean hasMapping()
1132 if (worker != null && (addingStructures || _started))
1136 if (getBinding() == null)
1138 if (_aps == null || _aps.size() == 0)
1140 // viewer has been closed, but we did at some point run.
1145 String[] pdbids = getBinding().getStructureFiles();
1151 for (String pdbid : pdbids)
1153 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1154 if (sm != null && sm.length > 0 && sm[0] != null)
1159 // only return true if there is a mapping for every structure file we have
1161 if (p == 0 || p != pdbids.length)
1165 // and that coloring has been applied
1166 return seqColoursApplied;
1170 public void raiseViewer()
1176 public long startProgressBar(String msg)
1178 // TODO would rather have startProgress/stopProgress as the
1179 // IProgressIndicator interface
1180 long tm = random.nextLong();
1181 if (progressBar != null)
1183 progressBar.setProgressBar(msg, tm);
1189 public void stopProgressBar(String msg, long handle)
1191 if (progressBar != null)
1193 progressBar.setProgressBar(msg, handle);
1197 protected IProgressIndicator getProgressIndicator()
1202 protected void setProgressIndicator(IProgressIndicator pi)
1207 public void setProgressMessage(String message, long id)
1209 if (progressBar != null)
1211 progressBar.setProgressBar(message, id);
1216 public void showConsole(boolean show)
1218 // default does nothing
1222 * Show only the selected chain(s) in the viewer
1224 protected void showSelectedChains()
1226 List<String> toshow = new ArrayList<>();
1227 for (int i = 0; i < chainMenu.getItemCount(); i++)
1229 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1231 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1232 if (item.isSelected())
1234 toshow.add(item.getText());
1238 getBinding().showChains(toshow);
1241 * Display selected hetatms in viewer
1243 protected void showSelectedHetatms()
1245 List<String> toshow = new ArrayList<>();
1246 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
1248 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
1250 JCheckBoxMenuItem item = (JCheckBoxMenuItem) hetatmMenu.getItem(i);
1251 if (item.isSelected())
1253 toshow.add(item.getText());
1257 getBinding().showHetatms(toshow);
1260 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1261 * saved file path if successful, or null if not.
1263 * @param processingEntry
1266 protected String fetchPdbFile(PDBEntry processingEntry)
1268 String filePath = null;
1269 Pdb pdbclient = new Pdb();
1270 EBIAlfaFold afclient = new EBIAlfaFold();
1271 AlignmentI pdbseq = null;
1272 String pdbid = processingEntry.getId();
1273 long handle = System.currentTimeMillis()
1274 + Thread.currentThread().hashCode();
1277 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1279 String msg = MessageManager.formatMessage("status.fetching_pdb",
1282 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1283 // long hdl = startProgressBar(MessageManager.formatMessage(
1284 // "status.fetching_pdb", new Object[]
1288 if (afclient.isValidReference(pdbid))
1290 pdbseq = afclient.getSequenceRecords(pdbid,
1291 processingEntry.getRetrievalUrl());
1295 if (processingEntry.hasRetrievalUrl())
1297 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1298 .replace("%", "__");
1300 // retrieve from URL to new local tmpfile
1301 File tmpFile = File.createTempFile(safePDBId,
1302 "." + (PDBEntry.Type.MMCIF.toString().equals(
1303 processingEntry.getType().toString()) ? "cif"
1305 String fromUrl = processingEntry.getRetrievalUrl();
1306 UrlDownloadClient.download(fromUrl, tmpFile);
1308 // may not need this check ?
1309 String file = tmpFile.getAbsolutePath();
1312 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1313 tmpFile, pdbid, null, null, null);
1318 pdbseq = pdbclient.getSequenceRecords(pdbid);
1321 } catch (Exception e)
1323 jalview.bin.Console.errPrintln(
1324 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1327 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1328 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1329 // stopProgressBar(msg, hdl);
1332 * If PDB data were saved and are not invalid (empty alignment), return the
1335 if (pdbseq != null && pdbseq.getHeight() > 0)
1337 // just use the file name from the first sequence's first PDBEntry
1338 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1339 .elementAt(0).getFile()).getAbsolutePath();
1340 processingEntry.setFile(filePath);
1346 * If supported, saves the state of the structure viewer to a temporary file
1347 * and returns the file, else returns null
1351 public File saveSession()
1353 if (getBinding() == null)
1357 File session = getBinding().saveSession();
1358 long l = session.length();
1365 } catch (InterruptedException e)
1368 long nextl = session.length();
1374 } while (--wait > 0);
1378 private static boolean quitClose = false;
1380 public static void setQuitClose(boolean b)
1386 public boolean stillRunning()
1388 AAStructureBindingModel binding = getBinding();
1389 return binding != null && binding.isViewerRunning();
1393 * Close down this instance of Jalview's Chimera viewer, giving the user the
1394 * option to close the associated Chimera window (process). They may wish to
1395 * keep it open until they have had an opportunity to save any work.
1398 * if true, close any linked Chimera process; if false, prompt first
1401 public void closeViewer(boolean forceClose)
1403 AAStructureBindingModel binding = getBinding();
1408 String viewerName = getViewerName();
1410 int confirm = JvOptionPane.CANCEL_OPTION;
1411 if (QuitHandler.quitting())
1413 // already asked about closing external windows
1414 confirm = quitClose ? JvOptionPane.YES_OPTION
1415 : JvOptionPane.NO_OPTION;
1419 String prompt = MessageManager
1420 .formatMessage("label.confirm_close_viewer", new Object[]
1421 { binding.getViewerTitle(viewerName, false),
1423 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1424 String title = MessageManager.getString("label.close_viewer");
1425 confirm = showCloseDialog(title, prompt);
1429 * abort closure if user hits escape or Cancel
1431 if (confirm == JvOptionPane.CANCEL_OPTION
1432 || confirm == JvOptionPane.CLOSED_OPTION)
1434 // abort possible quit handling if CANCEL chosen
1435 if (confirm == JvOptionPane.CANCEL_OPTION)
1439 // this is a bit futile
1440 this.setClosed(false);
1441 } catch (PropertyVetoException e)
1444 QuitHandler.abortQuit();
1448 forceClose = confirm == JvOptionPane.YES_OPTION;
1451 if (binding != null)
1453 binding.closeViewer(forceClose);
1455 setAlignmentPanel(null);
1458 _colourwith.clear();
1459 // TODO: check for memory leaks where instance isn't finalised because jmb
1460 // holds a reference to the window
1465 } catch (Throwable t)
1467 Console.info("Unexpected exception when deregistering structure viewer",t);
1472 private int showCloseDialog(final String title, final String prompt)
1474 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1475 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1476 MessageManager.getString("label.close_viewer"),
1477 JvOptionPane.YES_NO_CANCEL_OPTION,
1478 JvOptionPane.WARNING_MESSAGE);
1479 return confirmResponse;
1483 public void showHelp_actionPerformed()
1489 String url = getBinding().getHelpURL();
1492 BrowserLauncher.openURL(url);
1496 catch (IOException ex)
1499 .println("Show " + getViewerName() + " failed with: "
1506 public boolean hasViewerActionsMenu()
1508 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1509 && viewerActionMenu.getItemCount() > 0
1510 && viewerActionMenu.isVisible();