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.IdUtils;
70 import jalview.util.IdUtils.IdType;
71 import jalview.util.MessageManager;
72 import jalview.ws.dbsources.EBIAlfaFold;
73 import jalview.ws.dbsources.Pdb;
74 import jalview.ws.utils.UrlDownloadClient;
77 * Base class with common functionality for JMol, Chimera or other structure
83 public abstract class StructureViewerBase extends GStructureViewer
84 implements Runnable, ViewSetProvider
87 * names for colour options (additional to Jalview colour schemes)
91 BySequence, ByChain, ChargeCysteine, ByViewer
95 * Singleton list of all (open) instances of structureViewerBase TODO:
96 * JAL-3362 - review and adopt the swingJS-safe singleton pattern so each
97 * structure viewer base instance is kept to its own JalviewJS parent
99 private static List<JalviewStructureDisplayI> svbs = new ArrayList<>();
103 * @return list with all existing StructureViewers instance
105 public static List<JalviewStructureDisplayI> getAllStructureViewerBases()
107 List<JalviewStructureDisplayI> goodSvbs = new ArrayList<>();
108 for (JalviewStructureDisplayI s : svbs)
110 if (s != null && !goodSvbs.contains(s))
119 * list of sequenceSet ids associated with the view
121 protected List<String> _aps = new ArrayList<>();
124 * list of alignment panels to use for superposition
126 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
129 * list of alignment panels that are used for colouring structures by aligned
132 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
134 private String viewId = null;
136 private AlignmentPanel ap;
138 protected boolean alignAddedStructures = false;
140 protected volatile boolean _started = false;
142 protected volatile boolean addingStructures = false;
144 protected Thread worker = null;
146 protected boolean allChainsSelected = false;
148 protected boolean allHetatmBeingSelected = false;
150 protected JMenu viewSelectionMenu;
153 * set after sequence colouring has been applied for this structure viewer.
154 * used to determine if the final sequence/structure mapping has been
157 protected volatile boolean seqColoursApplied = false;
159 private IProgressIndicator progressBar = null;
161 private Random random = new Random();
164 * Default constructor
166 public StructureViewerBase()
174 * @return true if added structures should be aligned to existing one(s)
177 public boolean isAlignAddedStructures()
179 return alignAddedStructures;
185 * if added structures should be aligned to existing one(s)
188 public void setAlignAddedStructures(boolean alignAdded)
190 alignAddedStructures = alignAdded;
194 * called by the binding model to indicate when adding structures is happening
195 * or has been completed
197 * @param addingStructures
199 public synchronized void setAddingStructures(boolean addingStructures)
201 this.addingStructures = addingStructures;
207 * @return true if this Jmol instance is linked with the given alignPanel
209 public boolean isLinkedWith(AlignmentPanel ap2)
211 return _aps.contains(ap2.av.getSequenceSetId());
215 public boolean isUsedforaligment(AlignmentViewPanel ap2)
218 return (_alignwith != null) && _alignwith.contains(ap2);
222 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
224 return (_colourwith != null) && _colourwith.contains(ap2);
229 * @return TRUE if the view is NOT being coloured by the alignment colours.
232 public boolean isColouredByViewer()
234 return !getBinding().isColourBySequence();
238 public String getViewId()
242 viewId = System.currentTimeMillis() + "." + this.hashCode();
247 protected void setViewId(String viewId)
249 this.viewId = viewId;
252 protected void buildActionMenu()
254 if (_alignwith == null)
256 _alignwith = new Vector<>();
258 if (_alignwith.size() == 0 && ap != null)
263 // TODO: refactor to allow concrete classes to register buttons for adding
265 // currently have to override to add buttons back in after they are cleared
267 for (Component c : viewerActionMenu.getMenuComponents())
269 if (c != alignStructs)
271 viewerActionMenu.remove((JMenuItem) c);
277 public AlignmentPanel getAlignmentPanel()
282 protected void setAlignmentPanel(AlignmentPanel alp)
288 public AlignmentPanel[] getAllAlignmentPanels()
290 AlignmentPanel[] t, list = new AlignmentPanel[0];
291 for (String setid : _aps)
293 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
296 t = new AlignmentPanel[list.length + panels.length];
297 System.arraycopy(list, 0, t, 0, list.length);
298 System.arraycopy(panels, 0, t, list.length, panels.length);
307 * set the primary alignmentPanel reference and add another alignPanel to the
308 * list of ones to use for colouring and aligning
312 public void addAlignmentPanel(AlignmentPanel nap)
314 if (getAlignmentPanel() == null)
316 setAlignmentPanel(nap);
318 if (!_aps.contains(nap.av.getSequenceSetId()))
320 _aps.add(nap.av.getSequenceSetId());
325 * remove any references held to the given alignment panel
330 public void removeAlignmentPanel(AlignmentViewPanel nap)
334 _alignwith.remove(nap);
335 _colourwith.remove(nap);
336 if (getAlignmentPanel() == nap)
338 setAlignmentPanel(null);
339 for (AlignmentPanel aps : getAllAlignmentPanels())
343 setAlignmentPanel(aps);
348 } catch (Exception ex)
351 if (getAlignmentPanel() != null)
357 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
359 addAlignmentPanel(nap);
360 if (!_alignwith.contains(nap))
366 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
368 if (_alignwith.contains(nap))
370 _alignwith.remove(nap);
374 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
375 boolean enableColourBySeq)
377 useAlignmentPanelForColourbyseq(nap);
378 getBinding().setColourBySequence(enableColourBySeq);
379 seqColour.setSelected(enableColourBySeq);
380 viewerColour.setSelected(!enableColourBySeq);
383 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
385 addAlignmentPanel(nap);
386 if (!_colourwith.contains(nap))
388 _colourwith.add(nap);
392 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
394 if (_colourwith.contains(nap))
396 _colourwith.remove(nap);
401 public abstract ViewerType getViewerType();
404 * add a new structure (with associated sequences and chains) to this viewer,
405 * retrieving it if necessary first.
411 * if true, new structure(s) will be aligned using associated
415 protected void addStructure(final PDBEntry pdbentry,
416 final SequenceI[] seqs, final String[] chains,
417 final IProgressIndicator alignFrame)
419 if (pdbentry.getFile() == null)
421 if (worker != null && worker.isAlive())
423 // a retrieval is in progress, wait around and add ourselves to the
425 new Thread(new Runnable()
430 while (worker != null && worker.isAlive() && _started)
434 Thread.sleep(100 + ((int) Math.random() * 100));
436 } catch (Exception e)
440 // and call ourselves again.
441 addStructure(pdbentry, seqs, chains, alignFrame);
447 // otherwise, start adding the structure.
448 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
450 { seqs }, new String[][] { chains });
451 addingStructures = true;
453 worker = new Thread(this);
458 protected boolean hasPdbId(String pdbId)
460 return getBinding().hasPdbId(pdbId);
464 * Returns a list of any viewer of the instantiated type. The list is
465 * restricted to those linked to the given alignment panel if it is not null.
467 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
469 return Desktop.instance.getStructureViewers(alp, this.getClass());
473 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
474 String[] chains, final AlignmentViewPanel apanel, String pdbId)
477 * JAL-1742 exclude view with this structure already mapped (don't offer
478 * to align chain B to chain A of the same structure); code may defend
479 * against this possibility before we reach here
485 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
488 useAlignmentPanelForSuperposition(alignPanel);
489 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
493 * Adds mappings for the given sequences to an already opened PDB structure,
494 * and updates any viewers that have the PDB file
501 public void addSequenceMappingsToStructure(SequenceI[] seq,
502 String[] chains, final AlignmentViewPanel alpanel,
505 AlignmentPanel apanel = (AlignmentPanel) alpanel;
507 // TODO : Fix multiple seq to one chain issue here.
509 * create the mappings
511 apanel.getStructureSelectionManager().setMapping(seq, chains,
512 pdbFilename, DataSourceType.FILE, getProgressIndicator());
515 * alert the FeatureRenderer to show new (PDB RESNUM) features
517 if (apanel.getSeqPanel().seqCanvas.fr != null)
519 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
520 // note - we don't do a refresh for structure here because we do it
521 // explicitly for all panels later on
522 apanel.paintAlignment(true, false);
526 * add the sequences to any other viewers (of the same type) for this pdb
529 // JBPNOTE: this looks like a binding routine, rather than a gui routine
530 for (StructureViewerBase viewer : getViewersFor(null))
532 AAStructureBindingModel bindingModel = viewer.getBinding();
533 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
535 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
537 bindingModel.addSequence(pe, seq);
538 viewer.addAlignmentPanel(apanel);
540 * add it to the set of alignments used for colouring structure by
543 viewer.useAlignmentPanelForColourbyseq(apanel);
544 viewer.buildActionMenu();
545 apanel.getStructureSelectionManager()
546 .sequenceColoursChanged(apanel);
554 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
555 final AlignmentViewPanel apanel, String pdbId)
557 String alreadyMapped = apanel.getStructureSelectionManager()
558 .alreadyMappedToFile(pdbId);
560 if (alreadyMapped == null)
565 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
569 void setChainMenuItems(List<String> chainNames)
571 chainMenu.removeAll();
572 if (chainNames == null || chainNames.isEmpty())
576 JMenuItem menuItem = new JMenuItem(
577 MessageManager.getString("label.all"));
578 menuItem.addActionListener(new ActionListener()
581 public void actionPerformed(ActionEvent evt)
583 allChainsSelected = true;
584 for (int i = 0; i < chainMenu.getItemCount(); i++)
586 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
588 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
591 showSelectedChains();
592 allChainsSelected = false;
596 chainMenu.add(menuItem);
598 for (String chain : chainNames)
600 menuItem = new JCheckBoxMenuItem(chain, true);
601 menuItem.addItemListener(new ItemListener()
604 public void itemStateChanged(ItemEvent evt)
606 if (!allChainsSelected)
608 showSelectedChains();
613 chainMenu.add(menuItem);
617 void setHetatmMenuItems(Map<String, String> hetatmNames)
619 hetatmMenu.removeAll();
620 if (hetatmNames == null || hetatmNames.isEmpty())
622 hetatmMenu.setVisible(false);
625 hetatmMenu.setVisible(true);
626 allHetatmBeingSelected = false;
627 JMenuItem allMenuItem = new JMenuItem(
628 MessageManager.getString("label.all"));
629 JMenuItem noneMenuItem = new JMenuItem(
630 MessageManager.getString("label.none"));
631 allMenuItem.addActionListener(new ActionListener()
634 public void actionPerformed(ActionEvent e)
637 allHetatmBeingSelected = true;
638 // Toggle state of everything - on
639 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
641 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
643 ((JCheckBoxMenuItem) hetatmMenu.getItem(i)).setSelected(true);
646 allHetatmBeingSelected = false;
647 showSelectedHetatms();
652 noneMenuItem.addActionListener(new ActionListener()
655 public void actionPerformed(ActionEvent e)
658 allHetatmBeingSelected = true;
659 // Toggle state of everything off
660 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
662 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
664 ((JCheckBoxMenuItem) hetatmMenu.getItem(i))
668 allHetatmBeingSelected = false;
669 showSelectedHetatms();
673 hetatmMenu.add(noneMenuItem);
674 hetatmMenu.add(allMenuItem);
676 for (Map.Entry<String, String> chain : hetatmNames.entrySet())
678 JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(chain.getKey(),
680 menuItem.setToolTipText(chain.getValue());
681 menuItem.addItemListener(new ItemListener()
684 public void itemStateChanged(ItemEvent evt)
686 if (!allHetatmBeingSelected)
688 // update viewer only when we were clicked, not programmatically
690 showSelectedHetatms();
695 hetatmMenu.add(menuItem);
700 * Action on selecting one of Jalview's registered colour schemes
703 public void changeColour_actionPerformed(String colourSchemeName)
705 AlignmentI al = getAlignmentPanel().av.getAlignment();
706 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
707 colourSchemeName, getAlignmentPanel().av, al, null);
708 getBinding().colourByJalviewColourScheme(cs);
712 * Builds the colour menu
714 protected void buildColourMenu()
716 colourMenu.removeAll();
717 AlignmentI al = getAlignmentPanel().av.getAlignment();
720 * add colour by sequence, by chain, by charge and cysteine
722 colourMenu.add(seqColour);
723 colourMenu.add(chainColour);
724 colourMenu.add(chargeColour);
725 chargeColour.setEnabled(!al.isNucleotide());
728 * add all 'simple' (per-residue) colour schemes registered to Jalview
730 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
734 * add 'colour by viewer' (menu item text is set in subclasses)
736 viewerColour.setSelected(false);
737 viewerColour.addActionListener(new ActionListener()
740 public void actionPerformed(ActionEvent actionEvent)
742 viewerColour_actionPerformed();
745 colourMenu.add(viewerColour);
748 * add 'set background colour'
750 JMenuItem backGround = new JMenuItem();
752 .setText(MessageManager.getString("action.background_colour"));
753 backGround.addActionListener(new ActionListener()
756 public void actionPerformed(ActionEvent actionEvent)
758 background_actionPerformed();
761 colourMenu.add(backGround);
764 * add colour buttons to a group so their selection is
765 * mutually exclusive (background colour is a separate option)
767 itemGroup.add(seqColour);
768 itemGroup.add(chainColour);
769 itemGroup.add(chargeColour);
770 itemGroup.add(viewerColour);
774 * Construct menu items
776 protected void initMenus()
778 AAStructureBindingModel binding = getBinding();
780 seqColour = new JRadioButtonMenuItem();
781 seqColour.setText(MessageManager.getString("action.by_sequence"));
782 seqColour.setName(ViewerColour.BySequence.name());
783 seqColour.setSelected(binding.isColourBySequence());
784 seqColour.addActionListener(new ActionListener()
787 public void actionPerformed(ActionEvent actionEvent)
789 seqColour_actionPerformed();
793 chainColour = new JRadioButtonMenuItem();
794 chainColour.setText(MessageManager.getString("action.by_chain"));
795 chainColour.setName(ViewerColour.ByChain.name());
796 chainColour.addActionListener(new ActionListener()
799 public void actionPerformed(ActionEvent actionEvent)
801 chainColour_actionPerformed();
805 chargeColour = new JRadioButtonMenuItem();
806 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
807 chargeColour.setName(ViewerColour.ChargeCysteine.name());
808 chargeColour.addActionListener(new ActionListener()
811 public void actionPerformed(ActionEvent actionEvent)
813 chargeColour_actionPerformed();
817 viewerColour = new JRadioButtonMenuItem();
819 .setText(MessageManager.getString("label.colour_with_viewer"));
820 viewerColour.setToolTipText(MessageManager
821 .getString("label.let_viewer_manage_structure_colours"));
822 viewerColour.setName(ViewerColour.ByViewer.name());
823 viewerColour.setSelected(!binding.isColourBySequence());
825 if (_colourwith == null)
827 _colourwith = new Vector<>();
829 if (_alignwith == null)
831 _alignwith = new Vector<>();
834 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
835 MessageManager.getString("label.colour_by"), this, _colourwith,
839 public void itemStateChanged(ItemEvent e)
841 if (!seqColour.isSelected())
847 // update the viewer display now.
848 seqColour_actionPerformed();
852 viewMenu.add(seqColourBy);
854 final ItemListener handler = new ItemListener()
857 public void itemStateChanged(ItemEvent e)
859 if (_alignwith.isEmpty())
861 alignStructs.setEnabled(false);
862 alignStructs.setToolTipText(null);
866 alignStructs.setEnabled(true);
867 alignStructs.setToolTipText(MessageManager.formatMessage(
868 "label.align_structures_using_linked_alignment_views",
873 viewSelectionMenu = new ViewSelectionMenu(
874 MessageManager.getString("label.superpose_with"), this,
875 _alignwith, handler);
876 handler.itemStateChanged(null);
877 viewerActionMenu.add(viewSelectionMenu, 0);
878 viewerActionMenu.addMenuListener(new MenuListener()
881 public void menuSelected(MenuEvent e)
883 handler.itemStateChanged(null);
887 public void menuDeselected(MenuEvent e)
892 public void menuCanceled(MenuEvent e)
897 viewerActionMenu.setText(getViewerName());
898 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
905 * Sends commands to the structure viewer to superimpose structures based on
906 * currently associated alignments. May optionally return an error message for
910 protected String alignStructsWithAllAlignPanels()
912 if (getAlignmentPanel() == null)
917 if (_alignwith.size() == 0)
919 _alignwith.add(getAlignmentPanel());
925 reply = getBinding().superposeStructures(_alignwith);
926 if (reply != null && !reply.isEmpty())
928 String text = MessageManager
929 .formatMessage("error.superposition_failed", reply);
930 statusBar.setText(text);
932 } catch (Exception e)
934 StringBuffer sp = new StringBuffer();
935 for (AlignmentViewPanel alignPanel : _alignwith)
937 sp.append("'" + alignPanel.getViewName() + "' ");
939 Console.info("Couldn't align structures with the " + sp.toString()
940 + "associated alignment panels.", e);
946 * Opens a colour chooser dialog, and applies the chosen colour to the
947 * background of the structure viewer
950 public void background_actionPerformed()
952 String ttl = MessageManager.getString("label.select_background_colour");
953 ColourChooserListener listener = new ColourChooserListener()
956 public void colourSelected(Color c)
958 getBinding().setBackgroundColour(c);
961 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
965 public void viewerColour_actionPerformed()
967 if (viewerColour.isSelected())
969 // disable automatic sequence colouring.
970 getBinding().setColourBySequence(false);
975 public void chainColour_actionPerformed()
977 chainColour.setSelected(true);
978 getBinding().colourByChain();
982 public void chargeColour_actionPerformed()
984 chargeColour.setSelected(true);
985 getBinding().colourByCharge();
989 public void seqColour_actionPerformed()
991 AAStructureBindingModel binding = getBinding();
992 binding.setColourBySequence(seqColour.isSelected());
993 if (_colourwith == null)
995 _colourwith = new Vector<>();
997 if (binding.isColourBySequence())
999 if (!binding.isLoadingFromArchive())
1001 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
1003 // Make the currently displayed alignment panel the associated view
1004 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
1007 // Set the colour using the current view for the associated alignframe
1008 for (AlignmentViewPanel alignPanel : _colourwith)
1010 binding.colourBySequence(alignPanel);
1012 seqColoursApplied = true;
1017 public void pdbFile_actionPerformed()
1019 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
1020 JalviewFileChooser chooser = new JalviewFileChooser(
1021 Cache.getProperty("LAST_DIRECTORY"));
1023 chooser.setFileView(new JalviewFileView());
1024 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
1025 chooser.setToolTipText(MessageManager.getString("action.save"));
1027 int value = chooser.showSaveDialog(this);
1029 if (value == JalviewFileChooser.APPROVE_OPTION)
1031 BufferedReader in = null;
1034 // TODO: cope with multiple PDB files in view
1035 in = new BufferedReader(
1036 new FileReader(getBinding().getStructureFiles()[0]));
1037 File outFile = chooser.getSelectedFile();
1039 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1041 while ((data = in.readLine()) != null)
1043 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1049 } catch (Exception ex)
1051 ex.printStackTrace();
1059 } catch (IOException e)
1069 public void viewMapping_actionPerformed()
1071 CutAndPasteTransfer cap = new CutAndPasteTransfer();
1074 cap.appendText(getBinding().printMappings());
1075 } catch (OutOfMemoryError e)
1078 "composing sequence-structure alignments for display in text box.",
1083 Desktop.addInternalFrame(cap,
1084 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1088 protected abstract String getViewerName();
1091 * Configures the title and menu items of the viewer panel.
1094 public void updateTitleAndMenus()
1096 AAStructureBindingModel binding = getBinding();
1097 if (binding.hasFileLoadingError())
1102 setChainMenuItems(binding.getChainNames());
1103 setHetatmMenuItems(binding.getHetatmNames());
1105 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1108 * enable 'Superpose with' if more than one mapped structure
1110 viewSelectionMenu.setEnabled(false);
1111 if (getBinding().getMappedStructureCount() > 1
1112 && getBinding().getSequence().length > 1)
1114 viewSelectionMenu.setEnabled(true);
1118 * Show action menu if it has any enabled items
1120 viewerActionMenu.setVisible(false);
1121 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1123 if (viewerActionMenu.getItem(i).isEnabled())
1125 viewerActionMenu.setVisible(true);
1130 if (!binding.isLoadingFromArchive())
1132 seqColour_actionPerformed();
1137 public String toString()
1143 public boolean hasMapping()
1145 if (worker != null && (addingStructures || _started))
1149 if (getBinding() == null)
1151 if (_aps == null || _aps.size() == 0)
1153 // viewer has been closed, but we did at some point run.
1158 String[] pdbids = getBinding().getStructureFiles();
1164 for (String pdbid : pdbids)
1166 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1167 if (sm != null && sm.length > 0 && sm[0] != null)
1172 // only return true if there is a mapping for every structure file we have
1174 if (p == 0 || p != pdbids.length)
1178 // and that coloring has been applied
1179 return seqColoursApplied;
1183 public void raiseViewer()
1189 public long startProgressBar(String msg)
1191 // TODO would rather have startProgress/stopProgress as the
1192 // IProgressIndicator interface
1193 long tm = IdUtils.newId(IdType.PROGRESS);
1194 if (progressBar != null)
1196 progressBar.setProgressBar(msg, tm);
1202 public void stopProgressBar(String msg, long handle)
1204 if (progressBar != null)
1206 progressBar.setProgressBar(msg, handle);
1210 protected IProgressIndicator getProgressIndicator()
1215 protected void setProgressIndicator(IProgressIndicator pi)
1220 public void setProgressMessage(String message, long id)
1222 if (progressBar != null)
1224 progressBar.setProgressBar(message, id);
1229 public void showConsole(boolean show)
1231 // default does nothing
1235 * Show only the selected chain(s) in the viewer
1237 protected void showSelectedChains()
1239 List<String> toshow = new ArrayList<>();
1240 for (int i = 0; i < chainMenu.getItemCount(); i++)
1242 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1244 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1245 if (item.isSelected())
1247 toshow.add(item.getText());
1251 getBinding().showChains(toshow);
1255 * Display selected hetatms in viewer
1257 protected void showSelectedHetatms()
1259 List<String> toshow = new ArrayList<>();
1260 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
1262 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
1264 JCheckBoxMenuItem item = (JCheckBoxMenuItem) hetatmMenu.getItem(i);
1265 if (item.isSelected())
1267 toshow.add(item.getText());
1271 getBinding().showHetatms(toshow);
1275 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1276 * saved file path if successful, or null if not.
1278 * @param processingEntry
1281 protected String fetchPdbFile(PDBEntry processingEntry)
1283 String filePath = null;
1284 Pdb pdbclient = new Pdb();
1285 EBIAlfaFold afclient = new EBIAlfaFold();
1286 AlignmentI pdbseq = null;
1287 String pdbid = processingEntry.getId();
1288 long handle = IdUtils.newId(IdType.PROGRESS);
1291 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1293 String msg = MessageManager.formatMessage("status.fetching_pdb",
1296 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1297 // long hdl = startProgressBar(MessageManager.formatMessage(
1298 // "status.fetching_pdb", new Object[]
1302 if (afclient.isValidReference(pdbid))
1304 pdbseq = afclient.getSequenceRecords(pdbid,
1305 processingEntry.getRetrievalUrl());
1309 if (processingEntry.hasRetrievalUrl())
1311 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1312 .replace("%", "__");
1314 // retrieve from URL to new local tmpfile
1315 File tmpFile = File.createTempFile(safePDBId,
1316 "." + (PDBEntry.Type.MMCIF.toString().equals(
1317 processingEntry.getType().toString()) ? "cif"
1319 String fromUrl = processingEntry.getRetrievalUrl();
1320 UrlDownloadClient.download(fromUrl, tmpFile);
1322 // may not need this check ?
1323 String file = tmpFile.getAbsolutePath();
1326 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1327 tmpFile, pdbid, null, null, null);
1332 pdbseq = pdbclient.getSequenceRecords(pdbid);
1335 } catch (Exception e)
1337 jalview.bin.Console.errPrintln(
1338 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1341 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1342 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1343 // stopProgressBar(msg, hdl);
1346 * If PDB data were saved and are not invalid (empty alignment), return the
1349 if (pdbseq != null && pdbseq.getHeight() > 0)
1351 // just use the file name from the first sequence's first PDBEntry
1352 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1353 .elementAt(0).getFile()).getAbsolutePath();
1354 processingEntry.setFile(filePath);
1360 * If supported, saves the state of the structure viewer to a temporary file
1361 * and returns the file, else returns null
1366 public File saveSession()
1368 if (getBinding() == null)
1372 File session = getBinding().saveSession();
1373 long l = session.length();
1380 } catch (InterruptedException e)
1383 long nextl = session.length();
1389 } while (--wait > 0);
1393 private static boolean quitClose = false;
1395 public static void setQuitClose(boolean b)
1401 public boolean stillRunning()
1403 AAStructureBindingModel binding = getBinding();
1404 return binding != null && binding.isViewerRunning();
1408 * Close down this instance of Jalview's Chimera viewer, giving the user the
1409 * option to close the associated Chimera window (process). They may wish to
1410 * keep it open until they have had an opportunity to save any work.
1413 * if true, close any linked Chimera process; if false, prompt first
1416 public void closeViewer(boolean forceClose)
1418 AAStructureBindingModel binding = getBinding();
1423 String viewerName = getViewerName();
1425 int confirm = JvOptionPane.CANCEL_OPTION;
1426 if (QuitHandler.quitting())
1428 // already asked about closing external windows
1429 confirm = quitClose ? JvOptionPane.YES_OPTION
1430 : JvOptionPane.NO_OPTION;
1434 String prompt = MessageManager
1435 .formatMessage("label.confirm_close_viewer", new Object[]
1436 { binding.getViewerTitle(viewerName, false),
1438 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1439 String title = MessageManager.getString("label.close_viewer");
1440 confirm = showCloseDialog(title, prompt);
1444 * abort closure if user hits escape or Cancel
1446 if (confirm == JvOptionPane.CANCEL_OPTION
1447 || confirm == JvOptionPane.CLOSED_OPTION)
1449 // abort possible quit handling if CANCEL chosen
1450 if (confirm == JvOptionPane.CANCEL_OPTION)
1454 // this is a bit futile
1455 this.setClosed(false);
1456 } catch (PropertyVetoException e)
1459 QuitHandler.abortQuit();
1463 forceClose = confirm == JvOptionPane.YES_OPTION;
1466 if (binding != null)
1468 binding.closeViewer(forceClose);
1470 setAlignmentPanel(null);
1473 _colourwith.clear();
1474 // TODO: check for memory leaks where instance isn't finalised because jmb
1475 // holds a reference to the window
1481 } catch (Throwable t)
1484 "Unexpected exception when deregistering structure viewer",
1490 private int showCloseDialog(final String title, final String prompt)
1492 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1493 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1494 MessageManager.getString("label.close_viewer"),
1495 JvOptionPane.YES_NO_CANCEL_OPTION,
1496 JvOptionPane.WARNING_MESSAGE);
1497 return confirmResponse;
1501 public void showHelp_actionPerformed()
1507 String url = getBinding().getHelpURL();
1510 BrowserLauncher.openURL(url);
1514 catch (IOException ex)
1517 .println("Show " + getViewerName() + " failed with: "
1524 public boolean hasViewerActionsMenu()
1526 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1527 && viewerActionMenu.getItemCount() > 0
1528 && viewerActionMenu.isVisible();