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;
38 import java.util.Random;
39 import java.util.Vector;
41 import javax.swing.ButtonGroup;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JRadioButtonMenuItem;
46 import javax.swing.event.MenuEvent;
47 import javax.swing.event.MenuListener;
49 import jalview.api.AlignmentViewPanel;
50 import jalview.api.structures.JalviewStructureDisplayI;
51 import jalview.bin.Cache;
52 import jalview.bin.Console;
53 import jalview.datamodel.AlignmentI;
54 import jalview.datamodel.PDBEntry;
55 import jalview.datamodel.SequenceI;
56 import jalview.gui.JalviewColourChooser.ColourChooserListener;
57 import jalview.gui.StructureViewer.ViewerType;
58 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
59 import jalview.io.DataSourceType;
60 import jalview.io.JalviewFileChooser;
61 import jalview.io.JalviewFileView;
62 import jalview.jbgui.GStructureViewer;
63 import jalview.schemes.ColourSchemeI;
64 import jalview.schemes.ColourSchemes;
65 import jalview.structure.StructureMapping;
66 import jalview.structures.models.AAStructureBindingModel;
67 import jalview.util.BrowserLauncher;
68 import jalview.util.MessageManager;
69 import jalview.ws.dbsources.EBIAlfaFold;
70 import jalview.ws.dbsources.Pdb;
71 import jalview.ws.utils.UrlDownloadClient;
74 * Base class with common functionality for JMol, Chimera or other structure
80 public abstract class StructureViewerBase extends GStructureViewer
81 implements Runnable, ViewSetProvider
84 * names for colour options (additional to Jalview colour schemes)
88 BySequence, ByChain, ChargeCysteine, ByViewer
92 * Singleton list of all (open) instances of structureViewerBase
93 * TODO: JAL-3362 - review and adopt the swingJS-safe singleton pattern so each structure viewer base instance is kept to its own JalviewJS parent
95 private static List<JalviewStructureDisplayI> svbs = new ArrayList<>();
99 * @return list with all existing StructureViewers instance
101 public static List<JalviewStructureDisplayI> getAllStructureViewerBases()
103 List<JalviewStructureDisplayI> goodSvbs = new ArrayList<>();
104 for (JalviewStructureDisplayI s : svbs)
106 if (s != null && !goodSvbs.contains(s))
115 * list of sequenceSet ids associated with the view
117 protected List<String> _aps = new ArrayList<>();
120 * list of alignment panels to use for superposition
122 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
125 * list of alignment panels that are used for colouring structures by aligned
128 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
130 private String viewId = null;
132 private AlignmentPanel ap;
134 protected boolean alignAddedStructures = false;
136 protected volatile boolean _started = false;
138 protected volatile boolean addingStructures = false;
140 protected Thread worker = null;
142 protected boolean allChainsSelected = false;
144 protected boolean allHetatmBeingSelected = false;
146 protected JMenu viewSelectionMenu;
149 * set after sequence colouring has been applied for this structure viewer.
150 * used to determine if the final sequence/structure mapping has been
153 protected volatile boolean seqColoursApplied = false;
155 private IProgressIndicator progressBar = null;
157 private Random random = new Random();
160 * Default constructor
162 public StructureViewerBase()
170 * @return true if added structures should be aligned to existing one(s)
173 public boolean isAlignAddedStructures()
175 return alignAddedStructures;
181 * if added structures should be aligned to existing one(s)
184 public void setAlignAddedStructures(boolean alignAdded)
186 alignAddedStructures = alignAdded;
190 * called by the binding model to indicate when adding structures is happening
191 * or has been completed
193 * @param addingStructures
195 public synchronized void setAddingStructures(boolean addingStructures)
197 this.addingStructures = addingStructures;
203 * @return true if this Jmol instance is linked with the given alignPanel
205 public boolean isLinkedWith(AlignmentPanel ap2)
207 return _aps.contains(ap2.av.getSequenceSetId());
211 public boolean isUsedforaligment(AlignmentViewPanel ap2)
214 return (_alignwith != null) && _alignwith.contains(ap2);
218 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
220 return (_colourwith != null) && _colourwith.contains(ap2);
225 * @return TRUE if the view is NOT being coloured by the alignment colours.
227 public boolean isColouredByViewer()
229 return !getBinding().isColourBySequence();
232 public String getViewId()
236 viewId = System.currentTimeMillis() + "." + this.hashCode();
241 protected void setViewId(String viewId)
243 this.viewId = viewId;
246 protected void buildActionMenu()
248 if (_alignwith == null)
250 _alignwith = new Vector<>();
252 if (_alignwith.size() == 0 && ap != null)
257 // TODO: refactor to allow concrete classes to register buttons for adding
259 // currently have to override to add buttons back in after they are cleared
261 for (Component c : viewerActionMenu.getMenuComponents())
263 if (c != alignStructs)
265 viewerActionMenu.remove((JMenuItem) c);
271 public AlignmentPanel getAlignmentPanel()
276 protected void setAlignmentPanel(AlignmentPanel alp)
282 public AlignmentPanel[] getAllAlignmentPanels()
284 AlignmentPanel[] t, list = new AlignmentPanel[0];
285 for (String setid : _aps)
287 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
290 t = new AlignmentPanel[list.length + panels.length];
291 System.arraycopy(list, 0, t, 0, list.length);
292 System.arraycopy(panels, 0, t, list.length, panels.length);
301 * set the primary alignmentPanel reference and add another alignPanel to the
302 * list of ones to use for colouring and aligning
306 public void addAlignmentPanel(AlignmentPanel nap)
308 if (getAlignmentPanel() == null)
310 setAlignmentPanel(nap);
312 if (!_aps.contains(nap.av.getSequenceSetId()))
314 _aps.add(nap.av.getSequenceSetId());
319 * remove any references held to the given alignment panel
324 public void removeAlignmentPanel(AlignmentViewPanel nap)
328 _alignwith.remove(nap);
329 _colourwith.remove(nap);
330 if (getAlignmentPanel() == nap)
332 setAlignmentPanel(null);
333 for (AlignmentPanel aps : getAllAlignmentPanels())
337 setAlignmentPanel(aps);
342 } catch (Exception ex)
345 if (getAlignmentPanel() != null)
351 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
353 addAlignmentPanel(nap);
354 if (!_alignwith.contains(nap))
360 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
362 if (_alignwith.contains(nap))
364 _alignwith.remove(nap);
368 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
369 boolean enableColourBySeq)
371 useAlignmentPanelForColourbyseq(nap);
372 getBinding().setColourBySequence(enableColourBySeq);
373 seqColour.setSelected(enableColourBySeq);
374 viewerColour.setSelected(!enableColourBySeq);
377 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
379 addAlignmentPanel(nap);
380 if (!_colourwith.contains(nap))
382 _colourwith.add(nap);
386 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
388 if (_colourwith.contains(nap))
390 _colourwith.remove(nap);
394 public abstract ViewerType getViewerType();
397 * add a new structure (with associated sequences and chains) to this viewer,
398 * retrieving it if necessary first.
404 * if true, new structure(s) will be aligned using associated
408 protected void addStructure(final PDBEntry pdbentry,
409 final SequenceI[] seqs, final String[] chains,
410 final IProgressIndicator alignFrame)
412 if (pdbentry.getFile() == null)
414 if (worker != null && worker.isAlive())
416 // a retrieval is in progress, wait around and add ourselves to the
418 new Thread(new Runnable()
423 while (worker != null && worker.isAlive() && _started)
427 Thread.sleep(100 + ((int) Math.random() * 100));
429 } catch (Exception e)
433 // and call ourselves again.
434 addStructure(pdbentry, seqs, chains, alignFrame);
440 // otherwise, start adding the structure.
441 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
443 { seqs }, new String[][] { chains });
444 addingStructures = true;
446 worker = new Thread(this);
451 protected boolean hasPdbId(String pdbId)
453 return getBinding().hasPdbId(pdbId);
457 * Returns a list of any viewer of the instantiated type. The list is
458 * restricted to those linked to the given alignment panel if it is not null.
460 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
462 return Desktop.instance.getStructureViewers(alp, this.getClass());
466 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
467 String[] chains, final AlignmentViewPanel apanel, String pdbId)
470 * JAL-1742 exclude view with this structure already mapped (don't offer
471 * to align chain B to chain A of the same structure); code may defend
472 * against this possibility before we reach here
478 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
481 useAlignmentPanelForSuperposition(alignPanel);
482 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
486 * Adds mappings for the given sequences to an already opened PDB structure,
487 * and updates any viewers that have the PDB file
494 public void addSequenceMappingsToStructure(SequenceI[] seq,
495 String[] chains, final AlignmentViewPanel alpanel,
498 AlignmentPanel apanel = (AlignmentPanel) alpanel;
500 // TODO : Fix multiple seq to one chain issue here.
502 * create the mappings
504 apanel.getStructureSelectionManager().setMapping(seq, chains,
505 pdbFilename, DataSourceType.FILE, getProgressIndicator());
508 * alert the FeatureRenderer to show new (PDB RESNUM) features
510 if (apanel.getSeqPanel().seqCanvas.fr != null)
512 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
513 // note - we don't do a refresh for structure here because we do it
514 // explicitly for all panels later on
515 apanel.paintAlignment(true, false);
519 * add the sequences to any other viewers (of the same type) for this pdb
522 // JBPNOTE: this looks like a binding routine, rather than a gui routine
523 for (StructureViewerBase viewer : getViewersFor(null))
525 AAStructureBindingModel bindingModel = viewer.getBinding();
526 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
528 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
530 bindingModel.addSequence(pe, seq);
531 viewer.addAlignmentPanel(apanel);
533 * add it to the set of alignments used for colouring structure by
536 viewer.useAlignmentPanelForColourbyseq(apanel);
537 viewer.buildActionMenu();
538 apanel.getStructureSelectionManager()
539 .sequenceColoursChanged(apanel);
547 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
548 final AlignmentViewPanel apanel, String pdbId)
550 String alreadyMapped = apanel.getStructureSelectionManager()
551 .alreadyMappedToFile(pdbId);
553 if (alreadyMapped == null)
558 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
562 void setChainMenuItems(List<String> chainNames)
564 chainMenu.removeAll();
565 if (chainNames == null || chainNames.isEmpty())
569 JMenuItem menuItem = new JMenuItem(
570 MessageManager.getString("label.all"));
571 menuItem.addActionListener(new ActionListener()
574 public void actionPerformed(ActionEvent evt)
576 allChainsSelected = true;
577 for (int i = 0; i < chainMenu.getItemCount(); i++)
579 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
581 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
584 showSelectedChains();
585 allChainsSelected = false;
589 chainMenu.add(menuItem);
591 for (String chain : chainNames)
593 menuItem = new JCheckBoxMenuItem(chain, true);
594 menuItem.addItemListener(new ItemListener()
597 public void itemStateChanged(ItemEvent evt)
599 if (!allChainsSelected)
601 showSelectedChains();
606 chainMenu.add(menuItem);
609 void setHetatmMenuItems(List<String> hetatmNames)
611 hetatmMenu.removeAll();
612 if (hetatmNames == null || hetatmNames.isEmpty())
614 hetatmMenu.setVisible(false);
617 hetatmMenu.setVisible(true);
618 allHetatmBeingSelected=false;
619 JMenuItem allMenuItem = new JMenuItem(
620 MessageManager.getString("label.all"));
621 JMenuItem noneMenuItem = new JMenuItem(
622 MessageManager.getString("label.none"));
623 allMenuItem.addActionListener(new ActionListener()
626 public void actionPerformed(ActionEvent e) {
628 allHetatmBeingSelected=true;
629 // Toggle state of everything - on
630 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
632 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
634 ((JCheckBoxMenuItem) hetatmMenu.getItem(i)).setSelected(true);
637 allHetatmBeingSelected=false;
638 showSelectedHetatms();
642 noneMenuItem.addActionListener(new ActionListener()
645 public void actionPerformed(ActionEvent e) {
647 allHetatmBeingSelected=true;
648 // Toggle state of everything off
649 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
651 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
653 ((JCheckBoxMenuItem) hetatmMenu.getItem(i)).setSelected(false);
656 allHetatmBeingSelected=false;
657 showSelectedHetatms();
660 hetatmMenu.add(noneMenuItem);
661 hetatmMenu.add(allMenuItem);
663 for (String chain : hetatmNames)
665 JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(chain, false);
666 menuItem.addItemListener(new ItemListener()
669 public void itemStateChanged(ItemEvent evt)
671 if (!allHetatmBeingSelected)
673 // update viewer only when we were clicked, not programmatically
675 showSelectedHetatms();
680 hetatmMenu.add(menuItem);
685 * Action on selecting one of Jalview's registered colour schemes
688 public void changeColour_actionPerformed(String colourSchemeName)
690 AlignmentI al = getAlignmentPanel().av.getAlignment();
691 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
692 colourSchemeName, getAlignmentPanel().av, al, null);
693 getBinding().colourByJalviewColourScheme(cs);
697 * Builds the colour menu
699 protected void buildColourMenu()
701 colourMenu.removeAll();
702 AlignmentI al = getAlignmentPanel().av.getAlignment();
705 * add colour by sequence, by chain, by charge and cysteine
707 colourMenu.add(seqColour);
708 colourMenu.add(chainColour);
709 colourMenu.add(chargeColour);
710 chargeColour.setEnabled(!al.isNucleotide());
713 * add all 'simple' (per-residue) colour schemes registered to Jalview
715 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
719 * add 'colour by viewer' (menu item text is set in subclasses)
721 viewerColour.setSelected(false);
722 viewerColour.addActionListener(new ActionListener()
725 public void actionPerformed(ActionEvent actionEvent)
727 viewerColour_actionPerformed();
730 colourMenu.add(viewerColour);
733 * add 'set background colour'
735 JMenuItem backGround = new JMenuItem();
737 .setText(MessageManager.getString("action.background_colour"));
738 backGround.addActionListener(new ActionListener()
741 public void actionPerformed(ActionEvent actionEvent)
743 background_actionPerformed();
746 colourMenu.add(backGround);
749 * add colour buttons to a group so their selection is
750 * mutually exclusive (background colour is a separate option)
752 itemGroup.add(seqColour);
753 itemGroup.add(chainColour);
754 itemGroup.add(chargeColour);
755 itemGroup.add(viewerColour);
759 * Construct menu items
761 protected void initMenus()
763 AAStructureBindingModel binding = getBinding();
765 seqColour = new JRadioButtonMenuItem();
766 seqColour.setText(MessageManager.getString("action.by_sequence"));
767 seqColour.setName(ViewerColour.BySequence.name());
768 seqColour.setSelected(binding.isColourBySequence());
769 seqColour.addActionListener(new ActionListener()
772 public void actionPerformed(ActionEvent actionEvent)
774 seqColour_actionPerformed();
778 chainColour = new JRadioButtonMenuItem();
779 chainColour.setText(MessageManager.getString("action.by_chain"));
780 chainColour.setName(ViewerColour.ByChain.name());
781 chainColour.addActionListener(new ActionListener()
784 public void actionPerformed(ActionEvent actionEvent)
786 chainColour_actionPerformed();
790 chargeColour = new JRadioButtonMenuItem();
791 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
792 chargeColour.setName(ViewerColour.ChargeCysteine.name());
793 chargeColour.addActionListener(new ActionListener()
796 public void actionPerformed(ActionEvent actionEvent)
798 chargeColour_actionPerformed();
802 viewerColour = new JRadioButtonMenuItem();
804 .setText(MessageManager.getString("label.colour_with_viewer"));
805 viewerColour.setToolTipText(MessageManager
806 .getString("label.let_viewer_manage_structure_colours"));
807 viewerColour.setName(ViewerColour.ByViewer.name());
808 viewerColour.setSelected(!binding.isColourBySequence());
810 if (_colourwith == null)
812 _colourwith = new Vector<>();
814 if (_alignwith == null)
816 _alignwith = new Vector<>();
819 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
820 MessageManager.getString("label.colour_by"), this, _colourwith,
824 public void itemStateChanged(ItemEvent e)
826 if (!seqColour.isSelected())
832 // update the viewer display now.
833 seqColour_actionPerformed();
837 viewMenu.add(seqColourBy);
839 final ItemListener handler = new ItemListener()
842 public void itemStateChanged(ItemEvent e)
844 if (_alignwith.isEmpty())
846 alignStructs.setEnabled(false);
847 alignStructs.setToolTipText(null);
851 alignStructs.setEnabled(true);
852 alignStructs.setToolTipText(MessageManager.formatMessage(
853 "label.align_structures_using_linked_alignment_views",
858 viewSelectionMenu = new ViewSelectionMenu(
859 MessageManager.getString("label.superpose_with"), this,
860 _alignwith, handler);
861 handler.itemStateChanged(null);
862 viewerActionMenu.add(viewSelectionMenu, 0);
863 viewerActionMenu.addMenuListener(new MenuListener()
866 public void menuSelected(MenuEvent e)
868 handler.itemStateChanged(null);
872 public void menuDeselected(MenuEvent e)
877 public void menuCanceled(MenuEvent e)
882 viewerActionMenu.setText(getViewerName());
883 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
890 * Sends commands to the structure viewer to superimpose structures based on
891 * currently associated alignments. May optionally return an error message for
895 protected String alignStructsWithAllAlignPanels()
897 if (getAlignmentPanel() == null)
902 if (_alignwith.size() == 0)
904 _alignwith.add(getAlignmentPanel());
910 reply = getBinding().superposeStructures(_alignwith);
911 if (reply != null && !reply.isEmpty())
913 String text = MessageManager
914 .formatMessage("error.superposition_failed", reply);
915 statusBar.setText(text);
917 } catch (Exception e)
919 StringBuffer sp = new StringBuffer();
920 for (AlignmentViewPanel alignPanel : _alignwith)
922 sp.append("'" + alignPanel.getViewName() + "' ");
924 Console.info("Couldn't align structures with the " + sp.toString()
925 + "associated alignment panels.", e);
931 * Opens a colour chooser dialog, and applies the chosen colour to the
932 * background of the structure viewer
935 public void background_actionPerformed()
937 String ttl = MessageManager.getString("label.select_background_colour");
938 ColourChooserListener listener = new ColourChooserListener()
941 public void colourSelected(Color c)
943 getBinding().setBackgroundColour(c);
946 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
950 public void viewerColour_actionPerformed()
952 if (viewerColour.isSelected())
954 // disable automatic sequence colouring.
955 getBinding().setColourBySequence(false);
960 public void chainColour_actionPerformed()
962 chainColour.setSelected(true);
963 getBinding().colourByChain();
967 public void chargeColour_actionPerformed()
969 chargeColour.setSelected(true);
970 getBinding().colourByCharge();
974 public void seqColour_actionPerformed()
976 AAStructureBindingModel binding = getBinding();
977 binding.setColourBySequence(seqColour.isSelected());
978 if (_colourwith == null)
980 _colourwith = new Vector<>();
982 if (binding.isColourBySequence())
984 if (!binding.isLoadingFromArchive())
986 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
988 // Make the currently displayed alignment panel the associated view
989 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
992 // Set the colour using the current view for the associated alignframe
993 for (AlignmentViewPanel alignPanel : _colourwith)
995 binding.colourBySequence(alignPanel);
997 seqColoursApplied = true;
1002 public void pdbFile_actionPerformed()
1004 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
1005 JalviewFileChooser chooser = new JalviewFileChooser(
1006 Cache.getProperty("LAST_DIRECTORY"));
1008 chooser.setFileView(new JalviewFileView());
1009 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
1010 chooser.setToolTipText(MessageManager.getString("action.save"));
1012 int value = chooser.showSaveDialog(this);
1014 if (value == JalviewFileChooser.APPROVE_OPTION)
1016 BufferedReader in = null;
1019 // TODO: cope with multiple PDB files in view
1020 in = new BufferedReader(
1021 new FileReader(getBinding().getStructureFiles()[0]));
1022 File outFile = chooser.getSelectedFile();
1024 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1026 while ((data = in.readLine()) != null)
1028 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1034 } catch (Exception ex)
1036 ex.printStackTrace();
1044 } catch (IOException e)
1054 public void viewMapping_actionPerformed()
1056 CutAndPasteTransfer cap = new CutAndPasteTransfer();
1059 cap.appendText(getBinding().printMappings());
1060 } catch (OutOfMemoryError e)
1063 "composing sequence-structure alignments for display in text box.",
1068 Desktop.addInternalFrame(cap,
1069 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1073 protected abstract String getViewerName();
1076 * Configures the title and menu items of the viewer panel.
1079 public void updateTitleAndMenus()
1081 AAStructureBindingModel binding = getBinding();
1082 if (binding.hasFileLoadingError())
1087 setChainMenuItems(binding.getChainNames());
1088 setHetatmMenuItems(binding.getHetatmNames());
1090 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1093 * enable 'Superpose with' if more than one mapped structure
1095 viewSelectionMenu.setEnabled(false);
1096 if (getBinding().getMappedStructureCount() > 1
1097 && getBinding().getSequence().length > 1)
1099 viewSelectionMenu.setEnabled(true);
1103 * Show action menu if it has any enabled items
1105 viewerActionMenu.setVisible(false);
1106 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1108 if (viewerActionMenu.getItem(i).isEnabled())
1110 viewerActionMenu.setVisible(true);
1115 if (!binding.isLoadingFromArchive())
1117 seqColour_actionPerformed();
1122 public String toString()
1128 public boolean hasMapping()
1130 if (worker != null && (addingStructures || _started))
1134 if (getBinding() == null)
1136 if (_aps == null || _aps.size() == 0)
1138 // viewer has been closed, but we did at some point run.
1143 String[] pdbids = getBinding().getStructureFiles();
1149 for (String pdbid : pdbids)
1151 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1152 if (sm != null && sm.length > 0 && sm[0] != null)
1157 // only return true if there is a mapping for every structure file we have
1159 if (p == 0 || p != pdbids.length)
1163 // and that coloring has been applied
1164 return seqColoursApplied;
1168 public void raiseViewer()
1174 public long startProgressBar(String msg)
1176 // TODO would rather have startProgress/stopProgress as the
1177 // IProgressIndicator interface
1178 long tm = random.nextLong();
1179 if (progressBar != null)
1181 progressBar.setProgressBar(msg, tm);
1187 public void stopProgressBar(String msg, long handle)
1189 if (progressBar != null)
1191 progressBar.setProgressBar(msg, handle);
1195 protected IProgressIndicator getProgressIndicator()
1200 protected void setProgressIndicator(IProgressIndicator pi)
1205 public void setProgressMessage(String message, long id)
1207 if (progressBar != null)
1209 progressBar.setProgressBar(message, id);
1214 public void showConsole(boolean show)
1216 // default does nothing
1220 * Show only the selected chain(s) in the viewer
1222 protected void showSelectedChains()
1224 List<String> toshow = new ArrayList<>();
1225 for (int i = 0; i < chainMenu.getItemCount(); i++)
1227 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1229 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1230 if (item.isSelected())
1232 toshow.add(item.getText());
1236 getBinding().showChains(toshow);
1239 * Display selected hetatms in viewer
1241 protected void showSelectedHetatms()
1243 List<String> toshow = new ArrayList<>();
1244 for (int i = 0; i < hetatmMenu.getItemCount(); i++)
1246 if (hetatmMenu.getItem(i) instanceof JCheckBoxMenuItem)
1248 JCheckBoxMenuItem item = (JCheckBoxMenuItem) hetatmMenu.getItem(i);
1249 if (item.isSelected())
1251 toshow.add(item.getText());
1255 getBinding().showHetatms(toshow);
1258 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1259 * saved file path if successful, or null if not.
1261 * @param processingEntry
1264 protected String fetchPdbFile(PDBEntry processingEntry)
1266 String filePath = null;
1267 Pdb pdbclient = new Pdb();
1268 EBIAlfaFold afclient = new EBIAlfaFold();
1269 AlignmentI pdbseq = null;
1270 String pdbid = processingEntry.getId();
1271 long handle = System.currentTimeMillis()
1272 + Thread.currentThread().hashCode();
1275 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1277 String msg = MessageManager.formatMessage("status.fetching_pdb",
1280 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1281 // long hdl = startProgressBar(MessageManager.formatMessage(
1282 // "status.fetching_pdb", new Object[]
1286 if (afclient.isValidReference(pdbid))
1288 pdbseq = afclient.getSequenceRecords(pdbid,
1289 processingEntry.getRetrievalUrl());
1293 if (processingEntry.hasRetrievalUrl())
1295 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1296 .replace("%", "__");
1298 // retrieve from URL to new local tmpfile
1299 File tmpFile = File.createTempFile(safePDBId,
1300 "." + (PDBEntry.Type.MMCIF.toString().equals(
1301 processingEntry.getType().toString()) ? "cif"
1303 String fromUrl = processingEntry.getRetrievalUrl();
1304 UrlDownloadClient.download(fromUrl, tmpFile);
1306 // may not need this check ?
1307 String file = tmpFile.getAbsolutePath();
1310 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1311 tmpFile, pdbid, null, null, null);
1316 pdbseq = pdbclient.getSequenceRecords(pdbid);
1319 } catch (Exception e)
1321 jalview.bin.Console.errPrintln(
1322 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1325 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1326 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1327 // stopProgressBar(msg, hdl);
1330 * If PDB data were saved and are not invalid (empty alignment), return the
1333 if (pdbseq != null && pdbseq.getHeight() > 0)
1335 // just use the file name from the first sequence's first PDBEntry
1336 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1337 .elementAt(0).getFile()).getAbsolutePath();
1338 processingEntry.setFile(filePath);
1344 * If supported, saves the state of the structure viewer to a temporary file
1345 * and returns the file, else returns null
1349 public File saveSession()
1351 if (getBinding() == null)
1355 File session = getBinding().saveSession();
1356 long l = session.length();
1363 } catch (InterruptedException e)
1366 long nextl = session.length();
1372 } while (--wait > 0);
1376 private static boolean quitClose = false;
1378 public static void setQuitClose(boolean b)
1384 public boolean stillRunning()
1386 AAStructureBindingModel binding = getBinding();
1387 return binding != null && binding.isViewerRunning();
1391 * Close down this instance of Jalview's Chimera viewer, giving the user the
1392 * option to close the associated Chimera window (process). They may wish to
1393 * keep it open until they have had an opportunity to save any work.
1396 * if true, close any linked Chimera process; if false, prompt first
1399 public void closeViewer(boolean forceClose)
1401 AAStructureBindingModel binding = getBinding();
1406 String viewerName = getViewerName();
1408 int confirm = JvOptionPane.CANCEL_OPTION;
1409 if (QuitHandler.quitting())
1411 // already asked about closing external windows
1412 confirm = quitClose ? JvOptionPane.YES_OPTION
1413 : JvOptionPane.NO_OPTION;
1417 String prompt = MessageManager
1418 .formatMessage("label.confirm_close_viewer", new Object[]
1419 { binding.getViewerTitle(viewerName, false),
1421 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1422 String title = MessageManager.getString("label.close_viewer");
1423 confirm = showCloseDialog(title, prompt);
1427 * abort closure if user hits escape or Cancel
1429 if (confirm == JvOptionPane.CANCEL_OPTION
1430 || confirm == JvOptionPane.CLOSED_OPTION)
1432 // abort possible quit handling if CANCEL chosen
1433 if (confirm == JvOptionPane.CANCEL_OPTION)
1437 // this is a bit futile
1438 this.setClosed(false);
1439 } catch (PropertyVetoException e)
1442 QuitHandler.abortQuit();
1446 forceClose = confirm == JvOptionPane.YES_OPTION;
1449 if (binding != null)
1451 binding.closeViewer(forceClose);
1453 setAlignmentPanel(null);
1456 _colourwith.clear();
1457 // TODO: check for memory leaks where instance isn't finalised because jmb
1458 // holds a reference to the window
1463 } catch (Throwable t)
1465 Console.info("Unexpected exception when deregistering structure viewer",t);
1470 private int showCloseDialog(final String title, final String prompt)
1472 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1473 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1474 MessageManager.getString("label.close_viewer"),
1475 JvOptionPane.YES_NO_CANCEL_OPTION,
1476 JvOptionPane.WARNING_MESSAGE);
1477 return confirmResponse;
1481 public void showHelp_actionPerformed()
1487 String url = getBinding().getHelpURL();
1490 BrowserLauncher.openURL(url);
1494 catch (IOException ex)
1497 .println("Show " + getViewerName() + " failed with: "
1504 public boolean hasViewerActionsMenu()
1506 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1507 && viewerActionMenu.getItemCount() > 0
1508 && viewerActionMenu.isVisible();