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.io.BufferedReader;
31 import java.io.FileOutputStream;
32 import java.io.FileReader;
33 import java.io.IOException;
34 import java.io.PrintWriter;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Random;
38 import java.util.Vector;
40 import javax.swing.ButtonGroup;
41 import javax.swing.JCheckBoxMenuItem;
42 import javax.swing.JMenu;
43 import javax.swing.JMenuItem;
44 import javax.swing.JRadioButtonMenuItem;
45 import javax.swing.event.MenuEvent;
46 import javax.swing.event.MenuListener;
48 import jalview.api.AlignmentViewPanel;
49 import jalview.bin.Cache;
50 import jalview.bin.Console;
51 import jalview.datamodel.AlignmentI;
52 import jalview.datamodel.PDBEntry;
53 import jalview.datamodel.SequenceI;
54 import jalview.gui.JalviewColourChooser.ColourChooserListener;
55 import jalview.gui.StructureViewer.ViewerType;
56 import jalview.gui.ViewSelectionMenu.ViewSetProvider;
57 import jalview.io.DataSourceType;
58 import jalview.io.JalviewFileChooser;
59 import jalview.io.JalviewFileView;
60 import jalview.jbgui.GStructureViewer;
61 import jalview.schemes.ColourSchemeI;
62 import jalview.schemes.ColourSchemes;
63 import jalview.structure.StructureMapping;
64 import jalview.structures.models.AAStructureBindingModel;
65 import jalview.util.BrowserLauncher;
66 import jalview.util.MessageManager;
67 import jalview.ws.dbsources.EBIAlfaFold;
68 import jalview.ws.dbsources.Pdb;
69 import jalview.ws.utils.UrlDownloadClient;
72 * Base class with common functionality for JMol, Chimera or other structure
78 public abstract class StructureViewerBase extends GStructureViewer
79 implements Runnable, ViewSetProvider
82 * names for colour options (additional to Jalview colour schemes)
86 BySequence, ByChain, ChargeCysteine, ByViewer
90 * list of sequenceSet ids associated with the view
92 protected List<String> _aps = new ArrayList<>();
95 * list of alignment panels to use for superposition
97 protected Vector<AlignmentViewPanel> _alignwith = new Vector<>();
100 * list of alignment panels that are used for colouring structures by aligned
103 protected Vector<AlignmentViewPanel> _colourwith = new Vector<>();
105 private String viewId = null;
107 private AlignmentPanel ap;
109 protected boolean alignAddedStructures = false;
111 protected volatile boolean _started = false;
113 protected volatile boolean addingStructures = false;
115 protected Thread worker = null;
117 protected boolean allChainsSelected = false;
119 protected JMenu viewSelectionMenu;
122 * set after sequence colouring has been applied for this structure viewer.
123 * used to determine if the final sequence/structure mapping has been
126 protected volatile boolean seqColoursApplied = false;
128 private IProgressIndicator progressBar = null;
130 private Random random = new Random();
133 * Default constructor
135 public StructureViewerBase()
141 * @return true if added structures should be aligned to existing one(s)
144 public boolean isAlignAddedStructures()
146 return alignAddedStructures;
152 * if added structures should be aligned to existing one(s)
155 public void setAlignAddedStructures(boolean alignAdded)
157 alignAddedStructures = alignAdded;
161 * called by the binding model to indicate when adding structures is happening
162 * or has been completed
164 * @param addingStructures
166 public synchronized void setAddingStructures(boolean addingStructures)
168 this.addingStructures = addingStructures;
174 * @return true if this Jmol instance is linked with the given alignPanel
176 public boolean isLinkedWith(AlignmentPanel ap2)
178 return _aps.contains(ap2.av.getSequenceSetId());
181 public boolean isUsedforaligment(AlignmentViewPanel ap2)
184 return (_alignwith != null) && _alignwith.contains(ap2);
188 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
190 return (_colourwith != null) && _colourwith.contains(ap2);
195 * @return TRUE if the view is NOT being coloured by the alignment colours.
197 public boolean isColouredByViewer()
199 return !getBinding().isColourBySequence();
202 public String getViewId()
206 viewId = System.currentTimeMillis() + "." + this.hashCode();
211 protected void setViewId(String viewId)
213 this.viewId = viewId;
216 protected void buildActionMenu()
218 if (_alignwith == null)
220 _alignwith = new Vector<>();
222 if (_alignwith.size() == 0 && ap != null)
227 // TODO: refactor to allow concrete classes to register buttons for adding
229 // currently have to override to add buttons back in after they are cleared
231 for (Component c : viewerActionMenu.getMenuComponents())
233 if (c != alignStructs)
235 viewerActionMenu.remove((JMenuItem) c);
241 public AlignmentPanel getAlignmentPanel()
246 protected void setAlignmentPanel(AlignmentPanel alp)
252 public AlignmentPanel[] getAllAlignmentPanels()
254 AlignmentPanel[] t, list = new AlignmentPanel[0];
255 for (String setid : _aps)
257 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
260 t = new AlignmentPanel[list.length + panels.length];
261 System.arraycopy(list, 0, t, 0, list.length);
262 System.arraycopy(panels, 0, t, list.length, panels.length);
271 * set the primary alignmentPanel reference and add another alignPanel to the
272 * list of ones to use for colouring and aligning
276 public void addAlignmentPanel(AlignmentPanel nap)
278 if (getAlignmentPanel() == null)
280 setAlignmentPanel(nap);
282 if (!_aps.contains(nap.av.getSequenceSetId()))
284 _aps.add(nap.av.getSequenceSetId());
289 * remove any references held to the given alignment panel
294 public void removeAlignmentPanel(AlignmentViewPanel nap)
298 _alignwith.remove(nap);
299 _colourwith.remove(nap);
300 if (getAlignmentPanel() == nap)
302 setAlignmentPanel(null);
303 for (AlignmentPanel aps : getAllAlignmentPanels())
307 setAlignmentPanel(aps);
312 } catch (Exception ex)
315 if (getAlignmentPanel() != null)
321 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
323 addAlignmentPanel(nap);
324 if (!_alignwith.contains(nap))
330 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
332 if (_alignwith.contains(nap))
334 _alignwith.remove(nap);
338 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
339 boolean enableColourBySeq)
341 useAlignmentPanelForColourbyseq(nap);
342 getBinding().setColourBySequence(enableColourBySeq);
343 seqColour.setSelected(enableColourBySeq);
344 viewerColour.setSelected(!enableColourBySeq);
347 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
349 addAlignmentPanel(nap);
350 if (!_colourwith.contains(nap))
352 _colourwith.add(nap);
356 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
358 if (_colourwith.contains(nap))
360 _colourwith.remove(nap);
364 public abstract ViewerType getViewerType();
367 * add a new structure (with associated sequences and chains) to this viewer,
368 * retrieving it if necessary first.
374 * if true, new structure(s) will be aligned using associated
378 protected void addStructure(final PDBEntry pdbentry,
379 final SequenceI[] seqs, final String[] chains,
380 final IProgressIndicator alignFrame)
382 if (pdbentry.getFile() == null)
384 if (worker != null && worker.isAlive())
386 // a retrieval is in progress, wait around and add ourselves to the
388 new Thread(new Runnable()
393 while (worker != null && worker.isAlive() && _started)
397 Thread.sleep(100 + ((int) Math.random() * 100));
399 } catch (Exception e)
403 // and call ourselves again.
404 addStructure(pdbentry, seqs, chains, alignFrame);
410 // otherwise, start adding the structure.
411 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
413 { seqs }, new String[][] { chains });
414 addingStructures = true;
416 worker = new Thread(this);
421 protected boolean hasPdbId(String pdbId)
423 return getBinding().hasPdbId(pdbId);
427 * Returns a list of any viewer of the instantiated type. The list is
428 * restricted to those linked to the given alignment panel if it is not null.
430 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
432 return Desktop.getInstance().getStructureViewers(alp, this.getClass());
436 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
437 String[] chains, final AlignmentViewPanel apanel, String pdbId)
440 * JAL-1742 exclude view with this structure already mapped (don't offer
441 * to align chain B to chain A of the same structure); code may defend
442 * against this possibility before we reach here
448 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
451 useAlignmentPanelForSuperposition(alignPanel);
452 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
456 * Adds mappings for the given sequences to an already opened PDB structure,
457 * and updates any viewers that have the PDB file
464 public void addSequenceMappingsToStructure(SequenceI[] seq,
465 String[] chains, final AlignmentViewPanel alpanel,
468 AlignmentPanel apanel = (AlignmentPanel) alpanel;
470 // TODO : Fix multiple seq to one chain issue here.
472 * create the mappings
474 apanel.getStructureSelectionManager().setMapping(seq, chains,
475 pdbFilename, DataSourceType.FILE, getProgressIndicator());
478 * alert the FeatureRenderer to show new (PDB RESNUM) features
480 if (apanel.getSeqPanel().seqCanvas.fr != null)
482 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
483 // note - we don't do a refresh for structure here because we do it
484 // explicitly for all panels later on
485 apanel.paintAlignment(true, false);
489 * add the sequences to any other viewers (of the same type) for this pdb
492 // JBPNOTE: this looks like a binding routine, rather than a gui routine
493 for (StructureViewerBase viewer : getViewersFor(null))
495 AAStructureBindingModel bindingModel = viewer.getBinding();
496 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
498 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
500 bindingModel.addSequence(pe, seq);
501 viewer.addAlignmentPanel(apanel);
503 * add it to the set of alignments used for colouring structure by
506 viewer.useAlignmentPanelForColourbyseq(apanel);
507 viewer.buildActionMenu();
508 apanel.getStructureSelectionManager()
509 .sequenceColoursChanged(apanel);
517 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
518 final AlignmentViewPanel apanel, String pdbId)
520 String alreadyMapped = apanel.getStructureSelectionManager()
521 .alreadyMappedToFile(pdbId);
523 if (alreadyMapped == null)
528 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
532 void setChainMenuItems(List<String> chainNames)
534 chainMenu.removeAll();
535 if (chainNames == null || chainNames.isEmpty())
539 JMenuItem menuItem = new JMenuItem(
540 MessageManager.getString("label.all"));
541 menuItem.addActionListener(new ActionListener()
544 public void actionPerformed(ActionEvent evt)
546 allChainsSelected = true;
547 for (int i = 0; i < chainMenu.getItemCount(); i++)
549 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
551 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
554 showSelectedChains();
555 allChainsSelected = false;
559 chainMenu.add(menuItem);
561 for (String chain : chainNames)
563 menuItem = new JCheckBoxMenuItem(chain, true);
564 menuItem.addItemListener(new ItemListener()
567 public void itemStateChanged(ItemEvent evt)
569 if (!allChainsSelected)
571 showSelectedChains();
576 chainMenu.add(menuItem);
581 * Action on selecting one of Jalview's registered colour schemes
584 public void changeColour_actionPerformed(String colourSchemeName)
586 AlignmentI al = getAlignmentPanel().av.getAlignment();
587 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
588 colourSchemeName, getAlignmentPanel().av, al, null);
589 getBinding().colourByJalviewColourScheme(cs);
593 * Builds the colour menu
595 protected void buildColourMenu()
597 colourMenu.removeAll();
598 AlignmentI al = getAlignmentPanel().av.getAlignment();
601 * add colour by sequence, by chain, by charge and cysteine
603 colourMenu.add(seqColour);
604 colourMenu.add(chainColour);
605 colourMenu.add(chargeColour);
606 chargeColour.setEnabled(!al.isNucleotide());
609 * add all 'simple' (per-residue) colour schemes registered to Jalview
611 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
615 * add 'colour by viewer' (menu item text is set in subclasses)
617 viewerColour.setSelected(false);
618 viewerColour.addActionListener(new ActionListener()
621 public void actionPerformed(ActionEvent actionEvent)
623 viewerColour_actionPerformed();
626 colourMenu.add(viewerColour);
629 * add 'set background colour'
631 JMenuItem backGround = new JMenuItem();
633 .setText(MessageManager.getString("action.background_colour"));
634 backGround.addActionListener(new ActionListener()
637 public void actionPerformed(ActionEvent actionEvent)
639 background_actionPerformed();
642 colourMenu.add(backGround);
645 * add colour buttons to a group so their selection is
646 * mutually exclusive (background colour is a separate option)
648 itemGroup.add(seqColour);
649 itemGroup.add(chainColour);
650 itemGroup.add(chargeColour);
651 itemGroup.add(viewerColour);
655 * Construct menu items
657 protected void initMenus()
659 AAStructureBindingModel binding = getBinding();
661 seqColour = new JRadioButtonMenuItem();
662 seqColour.setText(MessageManager.getString("action.by_sequence"));
663 seqColour.setName(ViewerColour.BySequence.name());
664 seqColour.setSelected(binding.isColourBySequence());
665 seqColour.addActionListener(new ActionListener()
668 public void actionPerformed(ActionEvent actionEvent)
670 seqColour_actionPerformed();
674 chainColour = new JRadioButtonMenuItem();
675 chainColour.setText(MessageManager.getString("action.by_chain"));
676 chainColour.setName(ViewerColour.ByChain.name());
677 chainColour.addActionListener(new ActionListener()
680 public void actionPerformed(ActionEvent actionEvent)
682 chainColour_actionPerformed();
686 chargeColour = new JRadioButtonMenuItem();
687 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
688 chargeColour.setName(ViewerColour.ChargeCysteine.name());
689 chargeColour.addActionListener(new ActionListener()
692 public void actionPerformed(ActionEvent actionEvent)
694 chargeColour_actionPerformed();
698 viewerColour = new JRadioButtonMenuItem();
700 .setText(MessageManager.getString("label.colour_with_viewer"));
701 viewerColour.setToolTipText(MessageManager
702 .getString("label.let_viewer_manage_structure_colours"));
703 viewerColour.setName(ViewerColour.ByViewer.name());
704 viewerColour.setSelected(!binding.isColourBySequence());
706 if (_colourwith == null)
708 _colourwith = new Vector<>();
710 if (_alignwith == null)
712 _alignwith = new Vector<>();
715 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
716 MessageManager.getString("label.colour_by"), this, _colourwith,
720 public void itemStateChanged(ItemEvent e)
722 if (!seqColour.isSelected())
728 // update the viewer display now.
729 seqColour_actionPerformed();
733 viewMenu.add(seqColourBy);
735 final ItemListener handler = new ItemListener()
738 public void itemStateChanged(ItemEvent e)
740 if (_alignwith.isEmpty())
742 alignStructs.setEnabled(false);
743 alignStructs.setToolTipText(null);
747 alignStructs.setEnabled(true);
748 alignStructs.setToolTipText(MessageManager.formatMessage(
749 "label.align_structures_using_linked_alignment_views",
754 viewSelectionMenu = new ViewSelectionMenu(
755 MessageManager.getString("label.superpose_with"), this,
756 _alignwith, handler);
757 handler.itemStateChanged(null);
758 viewerActionMenu.add(viewSelectionMenu, 0);
759 viewerActionMenu.addMenuListener(new MenuListener()
762 public void menuSelected(MenuEvent e)
764 handler.itemStateChanged(null);
768 public void menuDeselected(MenuEvent e)
773 public void menuCanceled(MenuEvent e)
778 viewerActionMenu.setText(getViewerName());
779 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
786 * Sends commands to the structure viewer to superimpose structures based on
787 * currently associated alignments. May optionally return an error message for
791 protected String alignStructsWithAllAlignPanels()
793 if (getAlignmentPanel() == null)
798 if (_alignwith.size() == 0)
800 _alignwith.add(getAlignmentPanel());
806 reply = getBinding().superposeStructures(_alignwith);
807 if (reply != null && !reply.isEmpty())
809 String text = MessageManager
810 .formatMessage("error.superposition_failed", reply);
811 statusBar.setText(text);
813 } catch (Exception e)
815 StringBuffer sp = new StringBuffer();
816 for (AlignmentViewPanel alignPanel : _alignwith)
818 sp.append("'" + alignPanel.getViewName() + "' ");
820 Console.info("Couldn't align structures with the " + sp.toString()
821 + "associated alignment panels.", e);
827 * Opens a colour chooser dialog, and applies the chosen colour to the
828 * background of the structure viewer
831 public void background_actionPerformed()
833 String ttl = MessageManager.getString("label.select_background_colour");
834 ColourChooserListener listener = new ColourChooserListener()
837 public void colourSelected(Color c)
839 getBinding().setBackgroundColour(c);
842 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
846 public void viewerColour_actionPerformed()
848 if (viewerColour.isSelected())
850 // disable automatic sequence colouring.
851 getBinding().setColourBySequence(false);
856 public void chainColour_actionPerformed()
858 chainColour.setSelected(true);
859 getBinding().colourByChain();
863 public void chargeColour_actionPerformed()
865 chargeColour.setSelected(true);
866 getBinding().colourByCharge();
870 public void seqColour_actionPerformed()
872 AAStructureBindingModel binding = getBinding();
873 binding.setColourBySequence(seqColour.isSelected());
874 if (_colourwith == null)
876 _colourwith = new Vector<>();
878 if (binding.isColourBySequence())
880 if (!binding.isLoadingFromArchive())
882 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
884 // Make the currently displayed alignment panel the associated view
885 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
888 // Set the colour using the current view for the associated alignframe
889 for (AlignmentViewPanel alignPanel : _colourwith)
891 binding.colourBySequence(alignPanel);
893 seqColoursApplied = true;
898 public void pdbFile_actionPerformed()
900 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
901 JalviewFileChooser chooser = new JalviewFileChooser(
902 Cache.getProperty("LAST_DIRECTORY"));
904 chooser.setFileView(new JalviewFileView());
905 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
906 chooser.setToolTipText(MessageManager.getString("action.save"));
908 int value = chooser.showSaveDialog(this);
910 if (value == JalviewFileChooser.APPROVE_OPTION)
912 BufferedReader in = null;
915 // TODO: cope with multiple PDB files in view
916 in = new BufferedReader(
917 new FileReader(getBinding().getStructureFiles()[0]));
918 File outFile = chooser.getSelectedFile();
920 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
922 while ((data = in.readLine()) != null)
924 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
930 } catch (Exception ex)
932 ex.printStackTrace();
940 } catch (IOException e)
950 public void viewMapping_actionPerformed()
952 CutAndPasteTransfer cap = new CutAndPasteTransfer();
955 cap.appendText(getBinding().printMappings());
956 } catch (OutOfMemoryError e)
959 "composing sequence-structure alignments for display in text box.",
964 Desktop.addInternalFrame(cap,
965 MessageManager.getString("label.pdb_sequence_mapping"), 550,
969 protected abstract String getViewerName();
972 * Configures the title and menu items of the viewer panel.
975 public void updateTitleAndMenus()
977 AAStructureBindingModel binding = getBinding();
978 if (binding.hasFileLoadingError())
983 setChainMenuItems(binding.getChainNames());
985 this.setTitle(binding.getViewerTitle(getViewerName(), true));
988 * enable 'Superpose with' if more than one mapped structure
990 viewSelectionMenu.setEnabled(false);
991 if (getBinding().getMappedStructureCount() > 1
992 && getBinding().getSequence().length > 1)
994 viewSelectionMenu.setEnabled(true);
998 * Show action menu if it has any enabled items
1000 viewerActionMenu.setVisible(false);
1001 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1003 if (viewerActionMenu.getItem(i).isEnabled())
1005 viewerActionMenu.setVisible(true);
1010 if (!binding.isLoadingFromArchive())
1012 seqColour_actionPerformed();
1017 public String toString()
1023 public boolean hasMapping()
1025 if (worker != null && (addingStructures || _started))
1029 if (getBinding() == null)
1031 if (_aps == null || _aps.size() == 0)
1033 // viewer has been closed, but we did at some point run.
1038 String[] pdbids = getBinding().getStructureFiles();
1044 for (String pdbid : pdbids)
1046 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1047 if (sm != null && sm.length > 0 && sm[0] != null)
1052 // only return true if there is a mapping for every structure file we have
1054 if (p == 0 || p != pdbids.length)
1058 // and that coloring has been applied
1059 return seqColoursApplied;
1063 public void raiseViewer()
1069 public long startProgressBar(String msg)
1071 // TODO would rather have startProgress/stopProgress as the
1072 // IProgressIndicator interface
1073 long tm = random.nextLong();
1074 if (progressBar != null)
1076 progressBar.setProgressBar(msg, tm);
1082 public void stopProgressBar(String msg, long handle)
1084 if (progressBar != null)
1086 progressBar.setProgressBar(msg, handle);
1090 protected IProgressIndicator getProgressIndicator()
1095 protected void setProgressIndicator(IProgressIndicator pi)
1100 public void setProgressMessage(String message, long id)
1102 if (progressBar != null)
1104 progressBar.setProgressBar(message, id);
1109 public void showConsole(boolean show)
1111 // default does nothing
1115 * Show only the selected chain(s) in the viewer
1117 protected void showSelectedChains()
1119 List<String> toshow = new ArrayList<>();
1120 for (int i = 0; i < chainMenu.getItemCount(); i++)
1122 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1124 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1125 if (item.isSelected())
1127 toshow.add(item.getText());
1131 getBinding().showChains(toshow);
1135 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1136 * saved file path if successful, or null if not.
1138 * @param processingEntry
1141 protected String fetchPdbFile(PDBEntry processingEntry)
1143 String filePath = null;
1144 Pdb pdbclient = new Pdb();
1145 EBIAlfaFold afclient = new EBIAlfaFold();
1146 AlignmentI pdbseq = null;
1147 String pdbid = processingEntry.getId();
1148 long handle = System.currentTimeMillis()
1149 + Thread.currentThread().hashCode();
1152 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1154 String msg = MessageManager.formatMessage("status.fetching_pdb",
1157 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1158 // long hdl = startProgressBar(MessageManager.formatMessage(
1159 // "status.fetching_pdb", new Object[]
1163 if (afclient.isValidReference(pdbid))
1165 pdbseq = afclient.getSequenceRecords(pdbid,
1166 processingEntry.getRetrievalUrl());
1170 if (processingEntry.hasRetrievalUrl())
1172 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1173 .replace("%", "__");
1175 // retrieve from URL to new local tmpfile
1176 File tmpFile = File.createTempFile(safePDBId,
1177 "." + (PDBEntry.Type.MMCIF.toString().equals(
1178 processingEntry.getType().toString()) ? "cif"
1180 String fromUrl = processingEntry.getRetrievalUrl();
1181 UrlDownloadClient.download(fromUrl, tmpFile);
1183 // may not need this check ?
1184 String file = tmpFile.getAbsolutePath();
1187 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1188 tmpFile, pdbid, null, null, null);
1193 pdbseq = pdbclient.getSequenceRecords(pdbid);
1196 } catch (Exception e)
1199 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1202 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1203 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1204 // stopProgressBar(msg, hdl);
1207 * If PDB data were saved and are not invalid (empty alignment), return the
1210 if (pdbseq != null && pdbseq.getHeight() > 0)
1212 // just use the file name from the first sequence's first PDBEntry
1213 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1214 .elementAt(0).getFile()).getAbsolutePath();
1215 processingEntry.setFile(filePath);
1221 * If supported, saves the state of the structure viewer to a temporary file
1222 * and returns the file, else returns null
1226 public File saveSession()
1228 if (getBinding() == null)
1232 File session = getBinding().saveSession();
1233 long l = session.length();
1240 } catch (InterruptedException e)
1243 long nextl = session.length();
1249 } while (--wait > 0);
1254 * Close down this instance of Jalview's Chimera viewer, giving the user the
1255 * option to close the associated Chimera window (process). They may wish to
1256 * keep it open until they have had an opportunity to save any work.
1259 * if true, close any linked Chimera process; if false, prompt first
1262 public void closeViewer(boolean forceClose)
1264 AAStructureBindingModel binding = getBinding();
1265 if (binding != null && binding.isViewerRunning())
1269 String viewerName = getViewerName();
1270 String prompt = MessageManager
1271 .formatMessage("label.confirm_close_viewer", new Object[]
1272 { binding.getViewerTitle(viewerName, false), viewerName });
1273 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1274 int confirm = JvOptionPane.showConfirmDialog(this, prompt,
1275 MessageManager.getString("label.close_viewer"),
1276 JvOptionPane.YES_NO_CANCEL_OPTION);
1278 * abort closure if user hits escape or Cancel
1280 if (confirm == JvOptionPane.CANCEL_OPTION
1281 || confirm == JvOptionPane.CLOSED_OPTION)
1285 forceClose = confirm == JvOptionPane.YES_OPTION;
1288 if (binding != null)
1290 binding.closeViewer(forceClose);
1292 setAlignmentPanel(null);
1295 _colourwith.clear();
1296 // TODO: check for memory leaks where instance isn't finalised because jmb
1297 // holds a reference to the window
1303 public void showHelp_actionPerformed()
1309 String url = getBinding().getHelpURL();
1312 BrowserLauncher.openURL(url);
1316 catch (IOException ex)
1319 .println("Show " + getViewerName() + " failed with: "
1326 public boolean hasViewerActionsMenu()
1328 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1329 && viewerActionMenu.getItemCount() > 0
1330 && viewerActionMenu.isVisible();