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.bin.Cache;
51 import jalview.bin.Console;
52 import jalview.datamodel.AlignmentI;
53 import jalview.datamodel.PDBEntry;
54 import jalview.datamodel.SequenceI;
55 import jalview.gui.JalviewColourChooser.ColourChooserListener;
56 import jalview.gui.StructureViewer.ViewerType;
57 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
58 import jalview.io.DataSourceType;
59 import jalview.io.JalviewFileChooser;
60 import jalview.io.JalviewFileView;
61 import jalview.jbgui.GStructureViewer;
62 import jalview.schemes.ColourSchemeI;
63 import jalview.schemes.ColourSchemes;
64 import jalview.structure.StructureMapping;
65 import jalview.structures.models.AAStructureBindingModel;
66 import jalview.util.BrowserLauncher;
67 import jalview.util.MessageManager;
68 import jalview.ws.dbsources.EBIAlfaFold;
69 import jalview.ws.dbsources.Pdb;
70 import jalview.ws.utils.UrlDownloadClient;
73 * Base class with common functionality for JMol, Chimera or other structure
79 public abstract class StructureViewerBase extends GStructureViewer
80 implements Runnable, ViewSetProvider
83 * names for colour options (additional to Jalview colour schemes)
87 BySequence, ByChain, ChargeCysteine, ByViewer
90 private static List<StructureViewerBase> svbs = new ArrayList<>();
92 public static List<StructureViewerBase> getAllStructureViewerBases()
94 List<StructureViewerBase> goodSvbs = new ArrayList<>();
95 for (StructureViewerBase s : svbs)
97 if (s != null && !goodSvbs.contains(s))
106 * list of sequenceSet ids associated with the view
108 protected List<String> _aps = new ArrayList<>();
111 * list of alignment panels to use for superposition
113 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
116 * list of alignment panels that are used for colouring structures by aligned
119 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
121 private String viewId = null;
123 private AlignmentPanel ap;
125 protected boolean alignAddedStructures = false;
127 protected volatile boolean _started = false;
129 protected volatile boolean addingStructures = false;
131 protected Thread worker = null;
133 protected boolean allChainsSelected = false;
135 protected JMenu viewSelectionMenu;
138 * set after sequence colouring has been applied for this structure viewer.
139 * used to determine if the final sequence/structure mapping has been
142 protected volatile boolean seqColoursApplied = false;
144 private IProgressIndicator progressBar = null;
146 private Random random = new Random();
149 * Default constructor
151 public StructureViewerBase()
159 * @return true if added structures should be aligned to existing one(s)
162 public boolean isAlignAddedStructures()
164 return alignAddedStructures;
170 * if added structures should be aligned to existing one(s)
173 public void setAlignAddedStructures(boolean alignAdded)
175 alignAddedStructures = alignAdded;
179 * called by the binding model to indicate when adding structures is happening
180 * or has been completed
182 * @param addingStructures
184 public synchronized void setAddingStructures(boolean addingStructures)
186 this.addingStructures = addingStructures;
192 * @return true if this Jmol instance is linked with the given alignPanel
194 public boolean isLinkedWith(AlignmentPanel ap2)
196 return _aps.contains(ap2.av.getSequenceSetId());
199 public boolean isUsedforaligment(AlignmentViewPanel ap2)
202 return (_alignwith != null) && _alignwith.contains(ap2);
206 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
208 return (_colourwith != null) && _colourwith.contains(ap2);
213 * @return TRUE if the view is NOT being coloured by the alignment colours.
215 public boolean isColouredByViewer()
217 return !getBinding().isColourBySequence();
220 public String getViewId()
224 viewId = System.currentTimeMillis() + "." + this.hashCode();
229 protected void setViewId(String viewId)
231 this.viewId = viewId;
234 protected void buildActionMenu()
236 if (_alignwith == null)
238 _alignwith = new Vector<>();
240 if (_alignwith.size() == 0 && ap != null)
245 // TODO: refactor to allow concrete classes to register buttons for adding
247 // currently have to override to add buttons back in after they are cleared
249 for (Component c : viewerActionMenu.getMenuComponents())
251 if (c != alignStructs)
253 viewerActionMenu.remove((JMenuItem) c);
259 public AlignmentPanel getAlignmentPanel()
264 protected void setAlignmentPanel(AlignmentPanel alp)
270 public AlignmentPanel[] getAllAlignmentPanels()
272 AlignmentPanel[] t, list = new AlignmentPanel[0];
273 for (String setid : _aps)
275 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
278 t = new AlignmentPanel[list.length + panels.length];
279 System.arraycopy(list, 0, t, 0, list.length);
280 System.arraycopy(panels, 0, t, list.length, panels.length);
289 * set the primary alignmentPanel reference and add another alignPanel to the
290 * list of ones to use for colouring and aligning
294 public void addAlignmentPanel(AlignmentPanel nap)
296 if (getAlignmentPanel() == null)
298 setAlignmentPanel(nap);
300 if (!_aps.contains(nap.av.getSequenceSetId()))
302 _aps.add(nap.av.getSequenceSetId());
307 * remove any references held to the given alignment panel
312 public void removeAlignmentPanel(AlignmentViewPanel nap)
316 _alignwith.remove(nap);
317 _colourwith.remove(nap);
318 if (getAlignmentPanel() == nap)
320 setAlignmentPanel(null);
321 for (AlignmentPanel aps : getAllAlignmentPanels())
325 setAlignmentPanel(aps);
330 } catch (Exception ex)
333 if (getAlignmentPanel() != null)
339 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
341 addAlignmentPanel(nap);
342 if (!_alignwith.contains(nap))
348 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
350 if (_alignwith.contains(nap))
352 _alignwith.remove(nap);
356 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
357 boolean enableColourBySeq)
359 useAlignmentPanelForColourbyseq(nap);
360 getBinding().setColourBySequence(enableColourBySeq);
361 seqColour.setSelected(enableColourBySeq);
362 viewerColour.setSelected(!enableColourBySeq);
365 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
367 addAlignmentPanel(nap);
368 if (!_colourwith.contains(nap))
370 _colourwith.add(nap);
374 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
376 if (_colourwith.contains(nap))
378 _colourwith.remove(nap);
382 public abstract ViewerType getViewerType();
385 * add a new structure (with associated sequences and chains) to this viewer,
386 * retrieving it if necessary first.
392 * if true, new structure(s) will be aligned using associated
396 protected void addStructure(final PDBEntry pdbentry,
397 final SequenceI[] seqs, final String[] chains,
398 final IProgressIndicator alignFrame)
400 if (pdbentry.getFile() == null)
402 if (worker != null && worker.isAlive())
404 // a retrieval is in progress, wait around and add ourselves to the
406 new Thread(new Runnable()
411 while (worker != null && worker.isAlive() && _started)
415 Thread.sleep(100 + ((int) Math.random() * 100));
417 } catch (Exception e)
421 // and call ourselves again.
422 addStructure(pdbentry, seqs, chains, alignFrame);
428 // otherwise, start adding the structure.
429 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
431 { seqs }, new String[][] { chains });
432 addingStructures = true;
434 worker = new Thread(this);
439 protected boolean hasPdbId(String pdbId)
441 return getBinding().hasPdbId(pdbId);
445 * Returns a list of any viewer of the instantiated type. The list is
446 * restricted to those linked to the given alignment panel if it is not null.
448 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
450 return Desktop.instance.getStructureViewers(alp, this.getClass());
454 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
455 String[] chains, final AlignmentViewPanel apanel, String pdbId)
458 * JAL-1742 exclude view with this structure already mapped (don't offer
459 * to align chain B to chain A of the same structure); code may defend
460 * against this possibility before we reach here
466 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
469 useAlignmentPanelForSuperposition(alignPanel);
470 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
474 * Adds mappings for the given sequences to an already opened PDB structure,
475 * and updates any viewers that have the PDB file
482 public void addSequenceMappingsToStructure(SequenceI[] seq,
483 String[] chains, final AlignmentViewPanel alpanel,
486 AlignmentPanel apanel = (AlignmentPanel) alpanel;
488 // TODO : Fix multiple seq to one chain issue here.
490 * create the mappings
492 apanel.getStructureSelectionManager().setMapping(seq, chains,
493 pdbFilename, DataSourceType.FILE, getProgressIndicator());
496 * alert the FeatureRenderer to show new (PDB RESNUM) features
498 if (apanel.getSeqPanel().seqCanvas.fr != null)
500 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
501 // note - we don't do a refresh for structure here because we do it
502 // explicitly for all panels later on
503 apanel.paintAlignment(true, false);
507 * add the sequences to any other viewers (of the same type) for this pdb
510 // JBPNOTE: this looks like a binding routine, rather than a gui routine
511 for (StructureViewerBase viewer : getViewersFor(null))
513 AAStructureBindingModel bindingModel = viewer.getBinding();
514 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
516 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
518 bindingModel.addSequence(pe, seq);
519 viewer.addAlignmentPanel(apanel);
521 * add it to the set of alignments used for colouring structure by
524 viewer.useAlignmentPanelForColourbyseq(apanel);
525 viewer.buildActionMenu();
526 apanel.getStructureSelectionManager()
527 .sequenceColoursChanged(apanel);
535 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
536 final AlignmentViewPanel apanel, String pdbId)
538 String alreadyMapped = apanel.getStructureSelectionManager()
539 .alreadyMappedToFile(pdbId);
541 if (alreadyMapped == null)
546 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
550 void setChainMenuItems(List<String> chainNames)
552 chainMenu.removeAll();
553 if (chainNames == null || chainNames.isEmpty())
557 JMenuItem menuItem = new JMenuItem(
558 MessageManager.getString("label.all"));
559 menuItem.addActionListener(new ActionListener()
562 public void actionPerformed(ActionEvent evt)
564 allChainsSelected = true;
565 for (int i = 0; i < chainMenu.getItemCount(); i++)
567 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
569 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
572 showSelectedChains();
573 allChainsSelected = false;
577 chainMenu.add(menuItem);
579 for (String chain : chainNames)
581 menuItem = new JCheckBoxMenuItem(chain, true);
582 menuItem.addItemListener(new ItemListener()
585 public void itemStateChanged(ItemEvent evt)
587 if (!allChainsSelected)
589 showSelectedChains();
594 chainMenu.add(menuItem);
599 * Action on selecting one of Jalview's registered colour schemes
602 public void changeColour_actionPerformed(String colourSchemeName)
604 AlignmentI al = getAlignmentPanel().av.getAlignment();
605 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
606 colourSchemeName, getAlignmentPanel().av, al, null);
607 getBinding().colourByJalviewColourScheme(cs);
611 * Builds the colour menu
613 protected void buildColourMenu()
615 colourMenu.removeAll();
616 AlignmentI al = getAlignmentPanel().av.getAlignment();
619 * add colour by sequence, by chain, by charge and cysteine
621 colourMenu.add(seqColour);
622 colourMenu.add(chainColour);
623 colourMenu.add(chargeColour);
624 chargeColour.setEnabled(!al.isNucleotide());
627 * add all 'simple' (per-residue) colour schemes registered to Jalview
629 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
633 * add 'colour by viewer' (menu item text is set in subclasses)
635 viewerColour.setSelected(false);
636 viewerColour.addActionListener(new ActionListener()
639 public void actionPerformed(ActionEvent actionEvent)
641 viewerColour_actionPerformed();
644 colourMenu.add(viewerColour);
647 * add 'set background colour'
649 JMenuItem backGround = new JMenuItem();
651 .setText(MessageManager.getString("action.background_colour"));
652 backGround.addActionListener(new ActionListener()
655 public void actionPerformed(ActionEvent actionEvent)
657 background_actionPerformed();
660 colourMenu.add(backGround);
663 * add colour buttons to a group so their selection is
664 * mutually exclusive (background colour is a separate option)
666 itemGroup.add(seqColour);
667 itemGroup.add(chainColour);
668 itemGroup.add(chargeColour);
669 itemGroup.add(viewerColour);
673 * Construct menu items
675 protected void initMenus()
677 AAStructureBindingModel binding = getBinding();
679 seqColour = new JRadioButtonMenuItem();
680 seqColour.setText(MessageManager.getString("action.by_sequence"));
681 seqColour.setName(ViewerColour.BySequence.name());
682 seqColour.setSelected(binding.isColourBySequence());
683 seqColour.addActionListener(new ActionListener()
686 public void actionPerformed(ActionEvent actionEvent)
688 seqColour_actionPerformed();
692 chainColour = new JRadioButtonMenuItem();
693 chainColour.setText(MessageManager.getString("action.by_chain"));
694 chainColour.setName(ViewerColour.ByChain.name());
695 chainColour.addActionListener(new ActionListener()
698 public void actionPerformed(ActionEvent actionEvent)
700 chainColour_actionPerformed();
704 chargeColour = new JRadioButtonMenuItem();
705 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
706 chargeColour.setName(ViewerColour.ChargeCysteine.name());
707 chargeColour.addActionListener(new ActionListener()
710 public void actionPerformed(ActionEvent actionEvent)
712 chargeColour_actionPerformed();
716 viewerColour = new JRadioButtonMenuItem();
718 .setText(MessageManager.getString("label.colour_with_viewer"));
719 viewerColour.setToolTipText(MessageManager
720 .getString("label.let_viewer_manage_structure_colours"));
721 viewerColour.setName(ViewerColour.ByViewer.name());
722 viewerColour.setSelected(!binding.isColourBySequence());
724 if (_colourwith == null)
726 _colourwith = new Vector<>();
728 if (_alignwith == null)
730 _alignwith = new Vector<>();
733 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
734 MessageManager.getString("label.colour_by"), this, _colourwith,
738 public void itemStateChanged(ItemEvent e)
740 if (!seqColour.isSelected())
746 // update the viewer display now.
747 seqColour_actionPerformed();
751 viewMenu.add(seqColourBy);
753 final ItemListener handler = new ItemListener()
756 public void itemStateChanged(ItemEvent e)
758 if (_alignwith.isEmpty())
760 alignStructs.setEnabled(false);
761 alignStructs.setToolTipText(null);
765 alignStructs.setEnabled(true);
766 alignStructs.setToolTipText(MessageManager.formatMessage(
767 "label.align_structures_using_linked_alignment_views",
772 viewSelectionMenu = new ViewSelectionMenu(
773 MessageManager.getString("label.superpose_with"), this,
774 _alignwith, handler);
775 handler.itemStateChanged(null);
776 viewerActionMenu.add(viewSelectionMenu, 0);
777 viewerActionMenu.addMenuListener(new MenuListener()
780 public void menuSelected(MenuEvent e)
782 handler.itemStateChanged(null);
786 public void menuDeselected(MenuEvent e)
791 public void menuCanceled(MenuEvent e)
796 viewerActionMenu.setText(getViewerName());
797 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
804 * Sends commands to the structure viewer to superimpose structures based on
805 * currently associated alignments. May optionally return an error message for
809 protected String alignStructsWithAllAlignPanels()
811 if (getAlignmentPanel() == null)
816 if (_alignwith.size() == 0)
818 _alignwith.add(getAlignmentPanel());
824 reply = getBinding().superposeStructures(_alignwith);
825 if (reply != null && !reply.isEmpty())
827 String text = MessageManager
828 .formatMessage("error.superposition_failed", reply);
829 statusBar.setText(text);
831 } catch (Exception e)
833 StringBuffer sp = new StringBuffer();
834 for (AlignmentViewPanel alignPanel : _alignwith)
836 sp.append("'" + alignPanel.getViewName() + "' ");
838 Console.info("Couldn't align structures with the " + sp.toString()
839 + "associated alignment panels.", e);
845 * Opens a colour chooser dialog, and applies the chosen colour to the
846 * background of the structure viewer
849 public void background_actionPerformed()
851 String ttl = MessageManager.getString("label.select_background_colour");
852 ColourChooserListener listener = new ColourChooserListener()
855 public void colourSelected(Color c)
857 getBinding().setBackgroundColour(c);
860 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
864 public void viewerColour_actionPerformed()
866 if (viewerColour.isSelected())
868 // disable automatic sequence colouring.
869 getBinding().setColourBySequence(false);
874 public void chainColour_actionPerformed()
876 chainColour.setSelected(true);
877 getBinding().colourByChain();
881 public void chargeColour_actionPerformed()
883 chargeColour.setSelected(true);
884 getBinding().colourByCharge();
888 public void seqColour_actionPerformed()
890 AAStructureBindingModel binding = getBinding();
891 binding.setColourBySequence(seqColour.isSelected());
892 if (_colourwith == null)
894 _colourwith = new Vector<>();
896 if (binding.isColourBySequence())
898 if (!binding.isLoadingFromArchive())
900 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
902 // Make the currently displayed alignment panel the associated view
903 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
906 // Set the colour using the current view for the associated alignframe
907 for (AlignmentViewPanel alignPanel : _colourwith)
909 binding.colourBySequence(alignPanel);
911 seqColoursApplied = true;
916 public void pdbFile_actionPerformed()
918 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
919 JalviewFileChooser chooser = new JalviewFileChooser(
920 Cache.getProperty("LAST_DIRECTORY"));
922 chooser.setFileView(new JalviewFileView());
923 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
924 chooser.setToolTipText(MessageManager.getString("action.save"));
926 int value = chooser.showSaveDialog(this);
928 if (value == JalviewFileChooser.APPROVE_OPTION)
930 BufferedReader in = null;
933 // TODO: cope with multiple PDB files in view
934 in = new BufferedReader(
935 new FileReader(getBinding().getStructureFiles()[0]));
936 File outFile = chooser.getSelectedFile();
938 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
940 while ((data = in.readLine()) != null)
942 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
948 } catch (Exception ex)
950 ex.printStackTrace();
958 } catch (IOException e)
968 public void viewMapping_actionPerformed()
970 CutAndPasteTransfer cap = new CutAndPasteTransfer();
973 cap.appendText(getBinding().printMappings());
974 } catch (OutOfMemoryError e)
977 "composing sequence-structure alignments for display in text box.",
982 Desktop.addInternalFrame(cap,
983 MessageManager.getString("label.pdb_sequence_mapping"), 550,
987 protected abstract String getViewerName();
990 * Configures the title and menu items of the viewer panel.
993 public void updateTitleAndMenus()
995 AAStructureBindingModel binding = getBinding();
996 if (binding.hasFileLoadingError())
1001 setChainMenuItems(binding.getChainNames());
1003 this.setTitle(binding.getViewerTitle(getViewerName(), true));
1006 * enable 'Superpose with' if more than one mapped structure
1008 viewSelectionMenu.setEnabled(false);
1009 if (getBinding().getMappedStructureCount() > 1
1010 && getBinding().getSequence().length > 1)
1012 viewSelectionMenu.setEnabled(true);
1016 * Show action menu if it has any enabled items
1018 viewerActionMenu.setVisible(false);
1019 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1021 if (viewerActionMenu.getItem(i).isEnabled())
1023 viewerActionMenu.setVisible(true);
1028 if (!binding.isLoadingFromArchive())
1030 seqColour_actionPerformed();
1035 public String toString()
1041 public boolean hasMapping()
1043 if (worker != null && (addingStructures || _started))
1047 if (getBinding() == null)
1049 if (_aps == null || _aps.size() == 0)
1051 // viewer has been closed, but we did at some point run.
1056 String[] pdbids = getBinding().getStructureFiles();
1062 for (String pdbid : pdbids)
1064 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1065 if (sm != null && sm.length > 0 && sm[0] != null)
1070 // only return true if there is a mapping for every structure file we have
1072 if (p == 0 || p != pdbids.length)
1076 // and that coloring has been applied
1077 return seqColoursApplied;
1081 public void raiseViewer()
1087 public long startProgressBar(String msg)
1089 // TODO would rather have startProgress/stopProgress as the
1090 // IProgressIndicator interface
1091 long tm = random.nextLong();
1092 if (progressBar != null)
1094 progressBar.setProgressBar(msg, tm);
1100 public void stopProgressBar(String msg, long handle)
1102 if (progressBar != null)
1104 progressBar.setProgressBar(msg, handle);
1108 protected IProgressIndicator getProgressIndicator()
1113 protected void setProgressIndicator(IProgressIndicator pi)
1118 public void setProgressMessage(String message, long id)
1120 if (progressBar != null)
1122 progressBar.setProgressBar(message, id);
1127 public void showConsole(boolean show)
1129 // default does nothing
1133 * Show only the selected chain(s) in the viewer
1135 protected void showSelectedChains()
1137 List<String> toshow = new ArrayList<>();
1138 for (int i = 0; i < chainMenu.getItemCount(); i++)
1140 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1142 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1143 if (item.isSelected())
1145 toshow.add(item.getText());
1149 getBinding().showChains(toshow);
1153 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1154 * saved file path if successful, or null if not.
1156 * @param processingEntry
1159 protected String fetchPdbFile(PDBEntry processingEntry)
1161 String filePath = null;
1162 Pdb pdbclient = new Pdb();
1163 EBIAlfaFold afclient = new EBIAlfaFold();
1164 AlignmentI pdbseq = null;
1165 String pdbid = processingEntry.getId();
1166 long handle = System.currentTimeMillis()
1167 + Thread.currentThread().hashCode();
1170 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1172 String msg = MessageManager.formatMessage("status.fetching_pdb",
1175 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1176 // long hdl = startProgressBar(MessageManager.formatMessage(
1177 // "status.fetching_pdb", new Object[]
1181 if (afclient.isValidReference(pdbid))
1183 pdbseq = afclient.getSequenceRecords(pdbid,
1184 processingEntry.getRetrievalUrl());
1188 if (processingEntry.hasRetrievalUrl())
1190 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1191 .replace("%", "__");
1193 // retrieve from URL to new local tmpfile
1194 File tmpFile = File.createTempFile(safePDBId,
1195 "." + (PDBEntry.Type.MMCIF.toString().equals(
1196 processingEntry.getType().toString()) ? "cif"
1198 String fromUrl = processingEntry.getRetrievalUrl();
1199 UrlDownloadClient.download(fromUrl, tmpFile);
1201 // may not need this check ?
1202 String file = tmpFile.getAbsolutePath();
1205 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1206 tmpFile, pdbid, null, null, null);
1211 pdbseq = pdbclient.getSequenceRecords(pdbid);
1214 } catch (Exception e)
1216 jalview.bin.Console.errPrintln(
1217 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1220 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1221 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1222 // stopProgressBar(msg, hdl);
1225 * If PDB data were saved and are not invalid (empty alignment), return the
1228 if (pdbseq != null && pdbseq.getHeight() > 0)
1230 // just use the file name from the first sequence's first PDBEntry
1231 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1232 .elementAt(0).getFile()).getAbsolutePath();
1233 processingEntry.setFile(filePath);
1239 * If supported, saves the state of the structure viewer to a temporary file
1240 * and returns the file, else returns null
1244 public File saveSession()
1246 if (getBinding() == null)
1250 File session = getBinding().saveSession();
1251 long l = session.length();
1258 } catch (InterruptedException e)
1261 long nextl = session.length();
1267 } while (--wait > 0);
1271 private static boolean quitClose = false;
1273 public static void setQuitClose(boolean b)
1279 public boolean stillRunning()
1281 AAStructureBindingModel binding = getBinding();
1282 return binding != null && binding.isViewerRunning();
1286 * Close down this instance of Jalview's Chimera viewer, giving the user the
1287 * option to close the associated Chimera window (process). They may wish to
1288 * keep it open until they have had an opportunity to save any work.
1291 * if true, close any linked Chimera process; if false, prompt first
1294 public void closeViewer(boolean forceClose)
1296 AAStructureBindingModel binding = getBinding();
1301 String viewerName = getViewerName();
1303 int confirm = JvOptionPane.CANCEL_OPTION;
1304 if (QuitHandler.quitting())
1306 // already asked about closing external windows
1307 confirm = quitClose ? JvOptionPane.YES_OPTION
1308 : JvOptionPane.NO_OPTION;
1312 String prompt = MessageManager
1313 .formatMessage("label.confirm_close_viewer", new Object[]
1314 { binding.getViewerTitle(viewerName, false),
1316 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1317 String title = MessageManager.getString("label.close_viewer");
1318 confirm = showCloseDialog(title, prompt);
1322 * abort closure if user hits escape or Cancel
1324 if (confirm == JvOptionPane.CANCEL_OPTION
1325 || confirm == JvOptionPane.CLOSED_OPTION)
1327 // abort possible quit handling if CANCEL chosen
1328 if (confirm == JvOptionPane.CANCEL_OPTION)
1332 // this is a bit futile
1333 this.setClosed(false);
1334 } catch (PropertyVetoException e)
1337 QuitHandler.abortQuit();
1341 forceClose = confirm == JvOptionPane.YES_OPTION;
1344 if (binding != null)
1346 binding.closeViewer(forceClose);
1348 setAlignmentPanel(null);
1351 _colourwith.clear();
1352 // TODO: check for memory leaks where instance isn't finalised because jmb
1353 // holds a reference to the window
1358 private int showCloseDialog(final String title, final String prompt)
1360 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1361 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1362 MessageManager.getString("label.close_viewer"),
1363 JvOptionPane.YES_NO_CANCEL_OPTION,
1364 JvOptionPane.WARNING_MESSAGE);
1365 return confirmResponse;
1369 public void showHelp_actionPerformed()
1375 String url = getBinding().getHelpURL();
1378 BrowserLauncher.openURL(url);
1382 catch (IOException ex)
1385 .println("Show " + getViewerName() + " failed with: "
1392 public boolean hasViewerActionsMenu()
1394 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1395 && viewerActionMenu.getItemCount() > 0
1396 && viewerActionMenu.isVisible();