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
91 * list of sequenceSet ids associated with the view
93 protected List<String> _aps = new ArrayList<>();
96 * list of alignment panels to use for superposition
98 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
101 * list of alignment panels that are used for colouring structures by aligned
104 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
106 private String viewId = null;
108 private AlignmentPanel ap;
110 protected boolean alignAddedStructures = false;
112 protected volatile boolean _started = false;
114 protected volatile boolean addingStructures = false;
116 protected Thread worker = null;
118 protected boolean allChainsSelected = false;
120 protected JMenu viewSelectionMenu;
123 * set after sequence colouring has been applied for this structure viewer.
124 * used to determine if the final sequence/structure mapping has been
127 protected volatile boolean seqColoursApplied = false;
129 private IProgressIndicator progressBar = null;
131 private Random random = new Random();
134 * Default constructor
136 public StructureViewerBase()
143 * @return true if added structures should be aligned to existing one(s)
146 public boolean isAlignAddedStructures()
148 return alignAddedStructures;
154 * if added structures should be aligned to existing one(s)
157 public void setAlignAddedStructures(boolean alignAdded)
159 alignAddedStructures = alignAdded;
163 * called by the binding model to indicate when adding structures is happening
164 * or has been completed
166 * @param addingStructures
168 public synchronized void setAddingStructures(boolean addingStructures)
170 this.addingStructures = addingStructures;
176 * @return true if this Jmol instance is linked with the given alignPanel
178 public boolean isLinkedWith(AlignmentPanel ap2)
180 return _aps.contains(ap2.av.getSequenceSetId());
183 public boolean isUsedforaligment(AlignmentViewPanel ap2)
186 return (_alignwith != null) && _alignwith.contains(ap2);
190 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
192 return (_colourwith != null) && _colourwith.contains(ap2);
197 * @return TRUE if the view is NOT being coloured by the alignment colours.
199 public boolean isColouredByViewer()
201 return !getBinding().isColourBySequence();
204 public String getViewId()
208 viewId = System.currentTimeMillis() + "." + this.hashCode();
213 protected void setViewId(String viewId)
215 this.viewId = viewId;
218 protected void buildActionMenu()
220 if (_alignwith == null)
222 _alignwith = new Vector<>();
224 if (_alignwith.size() == 0 && ap != null)
229 // TODO: refactor to allow concrete classes to register buttons for adding
231 // currently have to override to add buttons back in after they are cleared
233 for (Component c : viewerActionMenu.getMenuComponents())
235 if (c != alignStructs)
237 viewerActionMenu.remove((JMenuItem) c);
243 public AlignmentPanel getAlignmentPanel()
248 protected void setAlignmentPanel(AlignmentPanel alp)
254 public AlignmentPanel[] getAllAlignmentPanels()
256 AlignmentPanel[] t, list = new AlignmentPanel[0];
257 for (String setid : _aps)
259 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
262 t = new AlignmentPanel[list.length + panels.length];
263 System.arraycopy(list, 0, t, 0, list.length);
264 System.arraycopy(panels, 0, t, list.length, panels.length);
273 * set the primary alignmentPanel reference and add another alignPanel to the
274 * list of ones to use for colouring and aligning
278 public void addAlignmentPanel(AlignmentPanel nap)
280 if (getAlignmentPanel() == null)
282 setAlignmentPanel(nap);
284 if (!_aps.contains(nap.av.getSequenceSetId()))
286 _aps.add(nap.av.getSequenceSetId());
291 * remove any references held to the given alignment panel
296 public void removeAlignmentPanel(AlignmentViewPanel nap)
300 _alignwith.remove(nap);
301 _colourwith.remove(nap);
302 if (getAlignmentPanel() == nap)
304 setAlignmentPanel(null);
305 for (AlignmentPanel aps : getAllAlignmentPanels())
309 setAlignmentPanel(aps);
314 } catch (Exception ex)
317 if (getAlignmentPanel() != null)
323 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
325 addAlignmentPanel(nap);
326 if (!_alignwith.contains(nap))
332 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
334 if (_alignwith.contains(nap))
336 _alignwith.remove(nap);
340 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
341 boolean enableColourBySeq)
343 useAlignmentPanelForColourbyseq(nap);
344 getBinding().setColourBySequence(enableColourBySeq);
345 seqColour.setSelected(enableColourBySeq);
346 viewerColour.setSelected(!enableColourBySeq);
349 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
351 addAlignmentPanel(nap);
352 if (!_colourwith.contains(nap))
354 _colourwith.add(nap);
358 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
360 if (_colourwith.contains(nap))
362 _colourwith.remove(nap);
366 public abstract ViewerType getViewerType();
369 * add a new structure (with associated sequences and chains) to this viewer,
370 * retrieving it if necessary first.
376 * if true, new structure(s) will be aligned using associated
380 protected void addStructure(final PDBEntry pdbentry,
381 final SequenceI[] seqs, final String[] chains,
382 final IProgressIndicator alignFrame)
384 if (pdbentry.getFile() == null)
386 if (worker != null && worker.isAlive())
388 // a retrieval is in progress, wait around and add ourselves to the
390 new Thread(new Runnable()
395 while (worker != null && worker.isAlive() && _started)
399 Thread.sleep(100 + ((int) Math.random() * 100));
401 } catch (Exception e)
405 // and call ourselves again.
406 addStructure(pdbentry, seqs, chains, alignFrame);
412 // otherwise, start adding the structure.
413 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
415 { seqs }, new String[][] { chains });
416 addingStructures = true;
418 worker = new Thread(this);
423 protected boolean hasPdbId(String pdbId)
425 return getBinding().hasPdbId(pdbId);
429 * Returns a list of any viewer of the instantiated type. The list is
430 * restricted to those linked to the given alignment panel if it is not null.
432 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
434 return Desktop.instance.getStructureViewers(alp, this.getClass());
438 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
439 String[] chains, final AlignmentViewPanel apanel, String pdbId)
442 * JAL-1742 exclude view with this structure already mapped (don't offer
443 * to align chain B to chain A of the same structure); code may defend
444 * against this possibility before we reach here
450 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
453 useAlignmentPanelForSuperposition(alignPanel);
454 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
458 * Adds mappings for the given sequences to an already opened PDB structure,
459 * and updates any viewers that have the PDB file
466 public void addSequenceMappingsToStructure(SequenceI[] seq,
467 String[] chains, final AlignmentViewPanel alpanel,
470 AlignmentPanel apanel = (AlignmentPanel) alpanel;
472 // TODO : Fix multiple seq to one chain issue here.
474 * create the mappings
476 apanel.getStructureSelectionManager().setMapping(seq, chains,
477 pdbFilename, DataSourceType.FILE, getProgressIndicator());
480 * alert the FeatureRenderer to show new (PDB RESNUM) features
482 if (apanel.getSeqPanel().seqCanvas.fr != null)
484 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
485 // note - we don't do a refresh for structure here because we do it
486 // explicitly for all panels later on
487 apanel.paintAlignment(true, false);
491 * add the sequences to any other viewers (of the same type) for this pdb
494 // JBPNOTE: this looks like a binding routine, rather than a gui routine
495 for (StructureViewerBase viewer : getViewersFor(null))
497 AAStructureBindingModel bindingModel = viewer.getBinding();
498 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
500 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
502 bindingModel.addSequence(pe, seq);
503 viewer.addAlignmentPanel(apanel);
505 * add it to the set of alignments used for colouring structure by
508 viewer.useAlignmentPanelForColourbyseq(apanel);
509 viewer.buildActionMenu();
510 apanel.getStructureSelectionManager()
511 .sequenceColoursChanged(apanel);
519 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
520 final AlignmentViewPanel apanel, String pdbId)
522 String alreadyMapped = apanel.getStructureSelectionManager()
523 .alreadyMappedToFile(pdbId);
525 if (alreadyMapped == null)
530 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
534 void setChainMenuItems(List<String> chainNames)
536 chainMenu.removeAll();
537 if (chainNames == null || chainNames.isEmpty())
541 JMenuItem menuItem = new JMenuItem(
542 MessageManager.getString("label.all"));
543 menuItem.addActionListener(new ActionListener()
546 public void actionPerformed(ActionEvent evt)
548 allChainsSelected = true;
549 for (int i = 0; i < chainMenu.getItemCount(); i++)
551 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
553 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
556 showSelectedChains();
557 allChainsSelected = false;
561 chainMenu.add(menuItem);
563 for (String chain : chainNames)
565 menuItem = new JCheckBoxMenuItem(chain, true);
566 menuItem.addItemListener(new ItemListener()
569 public void itemStateChanged(ItemEvent evt)
571 if (!allChainsSelected)
573 showSelectedChains();
578 chainMenu.add(menuItem);
583 * Action on selecting one of Jalview's registered colour schemes
586 public void changeColour_actionPerformed(String colourSchemeName)
588 AlignmentI al = getAlignmentPanel().av.getAlignment();
589 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
590 colourSchemeName, getAlignmentPanel().av, al, null);
591 getBinding().colourByJalviewColourScheme(cs);
595 * Builds the colour menu
597 protected void buildColourMenu()
599 colourMenu.removeAll();
600 AlignmentI al = getAlignmentPanel().av.getAlignment();
603 * add colour by sequence, by chain, by charge and cysteine
605 colourMenu.add(seqColour);
606 colourMenu.add(chainColour);
607 colourMenu.add(chargeColour);
608 chargeColour.setEnabled(!al.isNucleotide());
611 * add all 'simple' (per-residue) colour schemes registered to Jalview
613 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
617 * add 'colour by viewer' (menu item text is set in subclasses)
619 viewerColour.setSelected(false);
620 viewerColour.addActionListener(new ActionListener()
623 public void actionPerformed(ActionEvent actionEvent)
625 viewerColour_actionPerformed();
628 colourMenu.add(viewerColour);
631 * add 'set background colour'
633 JMenuItem backGround = new JMenuItem();
635 .setText(MessageManager.getString("action.background_colour"));
636 backGround.addActionListener(new ActionListener()
639 public void actionPerformed(ActionEvent actionEvent)
641 background_actionPerformed();
644 colourMenu.add(backGround);
647 * add colour buttons to a group so their selection is
648 * mutually exclusive (background colour is a separate option)
650 itemGroup.add(seqColour);
651 itemGroup.add(chainColour);
652 itemGroup.add(chargeColour);
653 itemGroup.add(viewerColour);
657 * Construct menu items
659 protected void initMenus()
661 AAStructureBindingModel binding = getBinding();
663 seqColour = new JRadioButtonMenuItem();
664 seqColour.setText(MessageManager.getString("action.by_sequence"));
665 seqColour.setName(ViewerColour.BySequence.name());
666 seqColour.setSelected(binding.isColourBySequence());
667 seqColour.addActionListener(new ActionListener()
670 public void actionPerformed(ActionEvent actionEvent)
672 seqColour_actionPerformed();
676 chainColour = new JRadioButtonMenuItem();
677 chainColour.setText(MessageManager.getString("action.by_chain"));
678 chainColour.setName(ViewerColour.ByChain.name());
679 chainColour.addActionListener(new ActionListener()
682 public void actionPerformed(ActionEvent actionEvent)
684 chainColour_actionPerformed();
688 chargeColour = new JRadioButtonMenuItem();
689 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
690 chargeColour.setName(ViewerColour.ChargeCysteine.name());
691 chargeColour.addActionListener(new ActionListener()
694 public void actionPerformed(ActionEvent actionEvent)
696 chargeColour_actionPerformed();
700 viewerColour = new JRadioButtonMenuItem();
702 .setText(MessageManager.getString("label.colour_with_viewer"));
703 viewerColour.setToolTipText(MessageManager
704 .getString("label.let_viewer_manage_structure_colours"));
705 viewerColour.setName(ViewerColour.ByViewer.name());
706 viewerColour.setSelected(!binding.isColourBySequence());
708 if (_colourwith == null)
710 _colourwith = new Vector<>();
712 if (_alignwith == null)
714 _alignwith = new Vector<>();
717 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
718 MessageManager.getString("label.colour_by"), this, _colourwith,
722 public void itemStateChanged(ItemEvent e)
724 if (!seqColour.isSelected())
730 // update the viewer display now.
731 seqColour_actionPerformed();
735 viewMenu.add(seqColourBy);
737 final ItemListener handler = new ItemListener()
740 public void itemStateChanged(ItemEvent e)
742 if (_alignwith.isEmpty())
744 alignStructs.setEnabled(false);
745 alignStructs.setToolTipText(null);
749 alignStructs.setEnabled(true);
750 alignStructs.setToolTipText(MessageManager.formatMessage(
751 "label.align_structures_using_linked_alignment_views",
756 viewSelectionMenu = new ViewSelectionMenu(
757 MessageManager.getString("label.superpose_with"), this,
758 _alignwith, handler);
759 handler.itemStateChanged(null);
760 viewerActionMenu.add(viewSelectionMenu, 0);
761 viewerActionMenu.addMenuListener(new MenuListener()
764 public void menuSelected(MenuEvent e)
766 handler.itemStateChanged(null);
770 public void menuDeselected(MenuEvent e)
775 public void menuCanceled(MenuEvent e)
780 viewerActionMenu.setText(getViewerName());
781 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
788 * Sends commands to the structure viewer to superimpose structures based on
789 * currently associated alignments. May optionally return an error message for
793 protected String alignStructsWithAllAlignPanels()
795 if (getAlignmentPanel() == null)
800 if (_alignwith.size() == 0)
802 _alignwith.add(getAlignmentPanel());
808 reply = getBinding().superposeStructures(_alignwith);
809 if (reply != null && !reply.isEmpty())
811 String text = MessageManager
812 .formatMessage("error.superposition_failed", reply);
813 statusBar.setText(text);
815 } catch (Exception e)
817 StringBuffer sp = new StringBuffer();
818 for (AlignmentViewPanel alignPanel : _alignwith)
820 sp.append("'" + alignPanel.getViewName() + "' ");
822 Console.info("Couldn't align structures with the " + sp.toString()
823 + "associated alignment panels.", e);
829 * Opens a colour chooser dialog, and applies the chosen colour to the
830 * background of the structure viewer
833 public void background_actionPerformed()
835 String ttl = MessageManager.getString("label.select_background_colour");
836 ColourChooserListener listener = new ColourChooserListener()
839 public void colourSelected(Color c)
841 getBinding().setBackgroundColour(c);
844 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
848 public void viewerColour_actionPerformed()
850 if (viewerColour.isSelected())
852 // disable automatic sequence colouring.
853 getBinding().setColourBySequence(false);
858 public void chainColour_actionPerformed()
860 chainColour.setSelected(true);
861 getBinding().colourByChain();
865 public void chargeColour_actionPerformed()
867 chargeColour.setSelected(true);
868 getBinding().colourByCharge();
872 public void seqColour_actionPerformed()
874 AAStructureBindingModel binding = getBinding();
875 binding.setColourBySequence(seqColour.isSelected());
876 if (_colourwith == null)
878 _colourwith = new Vector<>();
880 if (binding.isColourBySequence())
882 if (!binding.isLoadingFromArchive())
884 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
886 // Make the currently displayed alignment panel the associated view
887 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
890 // Set the colour using the current view for the associated alignframe
891 for (AlignmentViewPanel alignPanel : _colourwith)
893 binding.colourBySequence(alignPanel);
895 seqColoursApplied = true;
900 public void pdbFile_actionPerformed()
902 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
903 JalviewFileChooser chooser = new JalviewFileChooser(
904 Cache.getProperty("LAST_DIRECTORY"));
906 chooser.setFileView(new JalviewFileView());
907 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
908 chooser.setToolTipText(MessageManager.getString("action.save"));
910 int value = chooser.showSaveDialog(this);
912 if (value == JalviewFileChooser.APPROVE_OPTION)
914 BufferedReader in = null;
917 // TODO: cope with multiple PDB files in view
918 in = new BufferedReader(
919 new FileReader(getBinding().getStructureFiles()[0]));
920 File outFile = chooser.getSelectedFile();
922 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
924 while ((data = in.readLine()) != null)
926 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
932 } catch (Exception ex)
934 ex.printStackTrace();
942 } catch (IOException e)
952 public void viewMapping_actionPerformed()
954 CutAndPasteTransfer cap = new CutAndPasteTransfer();
957 cap.appendText(getBinding().printMappings());
958 } catch (OutOfMemoryError e)
961 "composing sequence-structure alignments for display in text box.",
966 Desktop.addInternalFrame(cap,
967 MessageManager.getString("label.pdb_sequence_mapping"), 550,
971 protected abstract String getViewerName();
974 * Configures the title and menu items of the viewer panel.
977 public void updateTitleAndMenus()
979 AAStructureBindingModel binding = getBinding();
980 if (binding.hasFileLoadingError())
985 setChainMenuItems(binding.getChainNames());
987 this.setTitle(binding.getViewerTitle(getViewerName(), true));
990 * enable 'Superpose with' if more than one mapped structure
992 viewSelectionMenu.setEnabled(false);
993 if (getBinding().getMappedStructureCount() > 1
994 && getBinding().getSequence().length > 1)
996 viewSelectionMenu.setEnabled(true);
1000 * Show action menu if it has any enabled items
1002 viewerActionMenu.setVisible(false);
1003 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1005 if (viewerActionMenu.getItem(i).isEnabled())
1007 viewerActionMenu.setVisible(true);
1012 if (!binding.isLoadingFromArchive())
1014 seqColour_actionPerformed();
1019 public String toString()
1025 public boolean hasMapping()
1027 if (worker != null && (addingStructures || _started))
1031 if (getBinding() == null)
1033 if (_aps == null || _aps.size() == 0)
1035 // viewer has been closed, but we did at some point run.
1040 String[] pdbids = getBinding().getStructureFiles();
1046 for (String pdbid : pdbids)
1048 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1049 if (sm != null && sm.length > 0 && sm[0] != null)
1054 // only return true if there is a mapping for every structure file we have
1056 if (p == 0 || p != pdbids.length)
1060 // and that coloring has been applied
1061 return seqColoursApplied;
1065 public void raiseViewer()
1071 public long startProgressBar(String msg)
1073 // TODO would rather have startProgress/stopProgress as the
1074 // IProgressIndicator interface
1075 long tm = random.nextLong();
1076 if (progressBar != null)
1078 progressBar.setProgressBar(msg, tm);
1084 public void stopProgressBar(String msg, long handle)
1086 if (progressBar != null)
1088 progressBar.setProgressBar(msg, handle);
1092 protected IProgressIndicator getProgressIndicator()
1097 protected void setProgressIndicator(IProgressIndicator pi)
1102 public void setProgressMessage(String message, long id)
1104 if (progressBar != null)
1106 progressBar.setProgressBar(message, id);
1111 public void showConsole(boolean show)
1113 // default does nothing
1117 * Show only the selected chain(s) in the viewer
1119 protected void showSelectedChains()
1121 List<String> toshow = new ArrayList<>();
1122 for (int i = 0; i < chainMenu.getItemCount(); i++)
1124 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1126 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1127 if (item.isSelected())
1129 toshow.add(item.getText());
1133 getBinding().showChains(toshow);
1137 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1138 * saved file path if successful, or null if not.
1140 * @param processingEntry
1143 protected String fetchPdbFile(PDBEntry processingEntry)
1145 String filePath = null;
1146 Pdb pdbclient = new Pdb();
1147 EBIAlfaFold afclient = new EBIAlfaFold();
1148 AlignmentI pdbseq = null;
1149 String pdbid = processingEntry.getId();
1150 long handle = System.currentTimeMillis()
1151 + Thread.currentThread().hashCode();
1154 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1156 String msg = MessageManager.formatMessage("status.fetching_pdb",
1159 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1160 // long hdl = startProgressBar(MessageManager.formatMessage(
1161 // "status.fetching_pdb", new Object[]
1165 if (afclient.isValidReference(pdbid))
1167 pdbseq = afclient.getSequenceRecords(pdbid,
1168 processingEntry.getRetrievalUrl());
1172 if (processingEntry.hasRetrievalUrl())
1174 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1175 .replace("%", "__");
1177 // retrieve from URL to new local tmpfile
1178 File tmpFile = File.createTempFile(safePDBId,
1179 "." + (PDBEntry.Type.MMCIF.toString().equals(
1180 processingEntry.getType().toString()) ? "cif"
1182 String fromUrl = processingEntry.getRetrievalUrl();
1183 UrlDownloadClient.download(fromUrl, tmpFile);
1185 // may not need this check ?
1186 String file = tmpFile.getAbsolutePath();
1189 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1190 tmpFile, pdbid, null, null, null);
1195 pdbseq = pdbclient.getSequenceRecords(pdbid);
1198 } catch (Exception e)
1201 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1204 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1205 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1206 // stopProgressBar(msg, hdl);
1209 * If PDB data were saved and are not invalid (empty alignment), return the
1212 if (pdbseq != null && pdbseq.getHeight() > 0)
1214 // just use the file name from the first sequence's first PDBEntry
1215 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1216 .elementAt(0).getFile()).getAbsolutePath();
1217 processingEntry.setFile(filePath);
1223 * If supported, saves the state of the structure viewer to a temporary file
1224 * and returns the file, else returns null
1228 public File saveSession()
1230 if (getBinding() == null)
1234 File session = getBinding().saveSession();
1235 long l = session.length();
1242 } catch (InterruptedException e)
1245 long nextl = session.length();
1251 } while (--wait > 0);
1255 private static boolean quitClose = false;
1257 public static void setQuitClose(boolean b)
1263 public boolean stillRunning()
1265 AAStructureBindingModel binding = getBinding();
1266 return binding != null && binding.isViewerRunning();
1270 * Close down this instance of Jalview's Chimera viewer, giving the user the
1271 * option to close the associated Chimera window (process). They may wish to
1272 * keep it open until they have had an opportunity to save any work.
1275 * if true, close any linked Chimera process; if false, prompt first
1278 public void closeViewer(boolean forceClose)
1280 AAStructureBindingModel binding = getBinding();
1285 String viewerName = getViewerName();
1287 int confirm = JvOptionPane.CANCEL_OPTION;
1288 if (QuitHandler.quitting())
1290 // already asked about closing external windows
1291 confirm = quitClose ? JvOptionPane.YES_OPTION
1292 : JvOptionPane.NO_OPTION;
1296 String prompt = MessageManager
1297 .formatMessage("label.confirm_close_viewer", new Object[]
1298 { binding.getViewerTitle(viewerName, false),
1300 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1301 String title = MessageManager.getString("label.close_viewer");
1302 confirm = showCloseDialog(title, prompt);
1306 * abort closure if user hits escape or Cancel
1308 if (confirm == JvOptionPane.CANCEL_OPTION
1309 || confirm == JvOptionPane.CLOSED_OPTION)
1311 // abort possible quit handling if CANCEL chosen
1312 if (confirm == JvOptionPane.CANCEL_OPTION)
1316 // this is a bit futile
1317 this.setClosed(false);
1318 } catch (PropertyVetoException e)
1321 QuitHandler.abortQuit();
1325 forceClose = confirm == JvOptionPane.YES_OPTION;
1328 if (binding != null)
1330 binding.closeViewer(forceClose);
1332 setAlignmentPanel(null);
1335 _colourwith.clear();
1336 // TODO: check for memory leaks where instance isn't finalised because jmb
1337 // holds a reference to the window
1342 private int showCloseDialog(final String title, final String prompt)
1344 int confirmResponse = JvOptionPane.CANCEL_OPTION;
1345 confirmResponse = JvOptionPane.showConfirmDialog(this, prompt,
1346 MessageManager.getString("label.close_viewer"),
1347 JvOptionPane.YES_NO_CANCEL_OPTION,
1348 JvOptionPane.WARNING_MESSAGE);
1349 return confirmResponse;
1353 public void showHelp_actionPerformed()
1359 String url = getBinding().getHelpURL();
1362 BrowserLauncher.openURL(url);
1366 catch (IOException ex)
1369 .println("Show " + getViewerName() + " failed with: "
1376 public boolean hasViewerActionsMenu()
1378 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1379 && viewerActionMenu.getItemCount() > 0
1380 && viewerActionMenu.isVisible();