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()
142 * @return true if added structures should be aligned to existing one(s)
145 public boolean isAlignAddedStructures()
147 return alignAddedStructures;
153 * if added structures should be aligned to existing one(s)
156 public void setAlignAddedStructures(boolean alignAdded)
158 alignAddedStructures = alignAdded;
162 * called by the binding model to indicate when adding structures is happening
163 * or has been completed
165 * @param addingStructures
167 public synchronized void setAddingStructures(boolean addingStructures)
169 this.addingStructures = addingStructures;
175 * @return true if this Jmol instance is linked with the given alignPanel
177 public boolean isLinkedWith(AlignmentPanel ap2)
179 return _aps.contains(ap2.av.getSequenceSetId());
182 public boolean isUsedforaligment(AlignmentViewPanel ap2)
185 return (_alignwith != null) && _alignwith.contains(ap2);
189 public boolean isUsedForColourBy(AlignmentViewPanel ap2)
191 return (_colourwith != null) && _colourwith.contains(ap2);
196 * @return TRUE if the view is NOT being coloured by the alignment colours.
198 public boolean isColouredByViewer()
200 return !getBinding().isColourBySequence();
203 public String getViewId()
207 viewId = System.currentTimeMillis() + "." + this.hashCode();
212 protected void setViewId(String viewId)
214 this.viewId = viewId;
217 protected void buildActionMenu()
219 if (_alignwith == null)
221 _alignwith = new Vector<>();
223 if (_alignwith.size() == 0 && ap != null)
228 // TODO: refactor to allow concrete classes to register buttons for adding
230 // currently have to override to add buttons back in after they are cleared
232 for (Component c : viewerActionMenu.getMenuComponents())
234 if (c != alignStructs)
236 viewerActionMenu.remove((JMenuItem) c);
242 public AlignmentPanel getAlignmentPanel()
247 protected void setAlignmentPanel(AlignmentPanel alp)
253 public AlignmentPanel[] getAllAlignmentPanels()
255 AlignmentPanel[] t, list = new AlignmentPanel[0];
256 for (String setid : _aps)
258 AlignmentPanel[] panels = PaintRefresher.getAssociatedPanels(setid);
261 t = new AlignmentPanel[list.length + panels.length];
262 System.arraycopy(list, 0, t, 0, list.length);
263 System.arraycopy(panels, 0, t, list.length, panels.length);
272 * set the primary alignmentPanel reference and add another alignPanel to the
273 * list of ones to use for colouring and aligning
277 public void addAlignmentPanel(AlignmentPanel nap)
279 if (getAlignmentPanel() == null)
281 setAlignmentPanel(nap);
283 if (!_aps.contains(nap.av.getSequenceSetId()))
285 _aps.add(nap.av.getSequenceSetId());
290 * remove any references held to the given alignment panel
295 public void removeAlignmentPanel(AlignmentViewPanel nap)
299 _alignwith.remove(nap);
300 _colourwith.remove(nap);
301 if (getAlignmentPanel() == nap)
303 setAlignmentPanel(null);
304 for (AlignmentPanel aps : getAllAlignmentPanels())
308 setAlignmentPanel(aps);
313 } catch (Exception ex)
316 if (getAlignmentPanel() != null)
322 public void useAlignmentPanelForSuperposition(AlignmentPanel nap)
324 addAlignmentPanel(nap);
325 if (!_alignwith.contains(nap))
331 public void excludeAlignmentPanelForSuperposition(AlignmentPanel nap)
333 if (_alignwith.contains(nap))
335 _alignwith.remove(nap);
339 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap,
340 boolean enableColourBySeq)
342 useAlignmentPanelForColourbyseq(nap);
343 getBinding().setColourBySequence(enableColourBySeq);
344 seqColour.setSelected(enableColourBySeq);
345 viewerColour.setSelected(!enableColourBySeq);
348 public void useAlignmentPanelForColourbyseq(AlignmentPanel nap)
350 addAlignmentPanel(nap);
351 if (!_colourwith.contains(nap))
353 _colourwith.add(nap);
357 public void excludeAlignmentPanelForColourbyseq(AlignmentPanel nap)
359 if (_colourwith.contains(nap))
361 _colourwith.remove(nap);
365 public abstract ViewerType getViewerType();
368 * add a new structure (with associated sequences and chains) to this viewer,
369 * retrieving it if necessary first.
375 * if true, new structure(s) will be aligned using associated
379 protected void addStructure(final PDBEntry pdbentry,
380 final SequenceI[] seqs, final String[] chains,
381 final IProgressIndicator alignFrame)
383 if (pdbentry.getFile() == null)
385 if (worker != null && worker.isAlive())
387 // a retrieval is in progress, wait around and add ourselves to the
389 new Thread(new Runnable()
394 while (worker != null && worker.isAlive() && _started)
398 Thread.sleep(100 + ((int) Math.random() * 100));
400 } catch (Exception e)
404 // and call ourselves again.
405 addStructure(pdbentry, seqs, chains, alignFrame);
411 // otherwise, start adding the structure.
412 getBinding().addSequenceAndChain(new PDBEntry[] { pdbentry },
414 { seqs }, new String[][] { chains });
415 addingStructures = true;
417 worker = new Thread(this);
422 protected boolean hasPdbId(String pdbId)
424 return getBinding().hasPdbId(pdbId);
428 * Returns a list of any viewer of the instantiated type. The list is
429 * restricted to those linked to the given alignment panel if it is not null.
431 protected List<StructureViewerBase> getViewersFor(AlignmentPanel alp)
433 return Desktop.instance.getStructureViewers(alp, this.getClass());
437 public void addToExistingViewer(PDBEntry pdbentry, SequenceI[] seq,
438 String[] chains, final AlignmentViewPanel apanel, String pdbId)
441 * JAL-1742 exclude view with this structure already mapped (don't offer
442 * to align chain B to chain A of the same structure); code may defend
443 * against this possibility before we reach here
449 AlignmentPanel alignPanel = (AlignmentPanel) apanel; // Implementation error
452 useAlignmentPanelForSuperposition(alignPanel);
453 addStructure(pdbentry, seq, chains, alignPanel.alignFrame);
457 * Adds mappings for the given sequences to an already opened PDB structure,
458 * and updates any viewers that have the PDB file
465 public void addSequenceMappingsToStructure(SequenceI[] seq,
466 String[] chains, final AlignmentViewPanel alpanel,
469 AlignmentPanel apanel = (AlignmentPanel) alpanel;
471 // TODO : Fix multiple seq to one chain issue here.
473 * create the mappings
475 apanel.getStructureSelectionManager().setMapping(seq, chains,
476 pdbFilename, DataSourceType.FILE, getProgressIndicator());
479 * alert the FeatureRenderer to show new (PDB RESNUM) features
481 if (apanel.getSeqPanel().seqCanvas.fr != null)
483 apanel.getSeqPanel().seqCanvas.fr.featuresAdded();
484 // note - we don't do a refresh for structure here because we do it
485 // explicitly for all panels later on
486 apanel.paintAlignment(true, false);
490 * add the sequences to any other viewers (of the same type) for this pdb
493 // JBPNOTE: this looks like a binding routine, rather than a gui routine
494 for (StructureViewerBase viewer : getViewersFor(null))
496 AAStructureBindingModel bindingModel = viewer.getBinding();
497 for (int pe = 0; pe < bindingModel.getPdbCount(); pe++)
499 if (bindingModel.getPdbEntry(pe).getFile().equals(pdbFilename))
501 bindingModel.addSequence(pe, seq);
502 viewer.addAlignmentPanel(apanel);
504 * add it to the set of alignments used for colouring structure by
507 viewer.useAlignmentPanelForColourbyseq(apanel);
508 viewer.buildActionMenu();
509 apanel.getStructureSelectionManager()
510 .sequenceColoursChanged(apanel);
518 public boolean addAlreadyLoadedFile(SequenceI[] seq, String[] chains,
519 final AlignmentViewPanel apanel, String pdbId)
521 String alreadyMapped = apanel.getStructureSelectionManager()
522 .alreadyMappedToFile(pdbId);
524 if (alreadyMapped == null)
529 addSequenceMappingsToStructure(seq, chains, apanel, alreadyMapped);
533 void setChainMenuItems(List<String> chainNames)
535 chainMenu.removeAll();
536 if (chainNames == null || chainNames.isEmpty())
540 JMenuItem menuItem = new JMenuItem(
541 MessageManager.getString("label.all"));
542 menuItem.addActionListener(new ActionListener()
545 public void actionPerformed(ActionEvent evt)
547 allChainsSelected = true;
548 for (int i = 0; i < chainMenu.getItemCount(); i++)
550 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
552 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
555 showSelectedChains();
556 allChainsSelected = false;
560 chainMenu.add(menuItem);
562 for (String chain : chainNames)
564 menuItem = new JCheckBoxMenuItem(chain, true);
565 menuItem.addItemListener(new ItemListener()
568 public void itemStateChanged(ItemEvent evt)
570 if (!allChainsSelected)
572 showSelectedChains();
577 chainMenu.add(menuItem);
582 * Action on selecting one of Jalview's registered colour schemes
585 public void changeColour_actionPerformed(String colourSchemeName)
587 AlignmentI al = getAlignmentPanel().av.getAlignment();
588 ColourSchemeI cs = ColourSchemes.getInstance().getColourScheme(
589 colourSchemeName, getAlignmentPanel().av, al, null);
590 getBinding().colourByJalviewColourScheme(cs);
594 * Builds the colour menu
596 protected void buildColourMenu()
598 colourMenu.removeAll();
599 AlignmentI al = getAlignmentPanel().av.getAlignment();
602 * add colour by sequence, by chain, by charge and cysteine
604 colourMenu.add(seqColour);
605 colourMenu.add(chainColour);
606 colourMenu.add(chargeColour);
607 chargeColour.setEnabled(!al.isNucleotide());
610 * add all 'simple' (per-residue) colour schemes registered to Jalview
612 ButtonGroup itemGroup = ColourMenuHelper.addMenuItems(colourMenu, this,
616 * add 'colour by viewer' (menu item text is set in subclasses)
618 viewerColour.setSelected(false);
619 viewerColour.addActionListener(new ActionListener()
622 public void actionPerformed(ActionEvent actionEvent)
624 viewerColour_actionPerformed();
627 colourMenu.add(viewerColour);
630 * add 'set background colour'
632 JMenuItem backGround = new JMenuItem();
634 .setText(MessageManager.getString("action.background_colour"));
635 backGround.addActionListener(new ActionListener()
638 public void actionPerformed(ActionEvent actionEvent)
640 background_actionPerformed();
643 colourMenu.add(backGround);
646 * add colour buttons to a group so their selection is
647 * mutually exclusive (background colour is a separate option)
649 itemGroup.add(seqColour);
650 itemGroup.add(chainColour);
651 itemGroup.add(chargeColour);
652 itemGroup.add(viewerColour);
656 * Construct menu items
658 protected void initMenus()
660 AAStructureBindingModel binding = getBinding();
662 seqColour = new JRadioButtonMenuItem();
663 seqColour.setText(MessageManager.getString("action.by_sequence"));
664 seqColour.setName(ViewerColour.BySequence.name());
665 seqColour.setSelected(binding.isColourBySequence());
666 seqColour.addActionListener(new ActionListener()
669 public void actionPerformed(ActionEvent actionEvent)
671 seqColour_actionPerformed();
675 chainColour = new JRadioButtonMenuItem();
676 chainColour.setText(MessageManager.getString("action.by_chain"));
677 chainColour.setName(ViewerColour.ByChain.name());
678 chainColour.addActionListener(new ActionListener()
681 public void actionPerformed(ActionEvent actionEvent)
683 chainColour_actionPerformed();
687 chargeColour = new JRadioButtonMenuItem();
688 chargeColour.setText(MessageManager.getString("label.charge_cysteine"));
689 chargeColour.setName(ViewerColour.ChargeCysteine.name());
690 chargeColour.addActionListener(new ActionListener()
693 public void actionPerformed(ActionEvent actionEvent)
695 chargeColour_actionPerformed();
699 viewerColour = new JRadioButtonMenuItem();
701 .setText(MessageManager.getString("label.colour_with_viewer"));
702 viewerColour.setToolTipText(MessageManager
703 .getString("label.let_viewer_manage_structure_colours"));
704 viewerColour.setName(ViewerColour.ByViewer.name());
705 viewerColour.setSelected(!binding.isColourBySequence());
707 if (_colourwith == null)
709 _colourwith = new Vector<>();
711 if (_alignwith == null)
713 _alignwith = new Vector<>();
716 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
717 MessageManager.getString("label.colour_by"), this, _colourwith,
721 public void itemStateChanged(ItemEvent e)
723 if (!seqColour.isSelected())
729 // update the viewer display now.
730 seqColour_actionPerformed();
734 viewMenu.add(seqColourBy);
736 final ItemListener handler = new ItemListener()
739 public void itemStateChanged(ItemEvent e)
741 if (_alignwith.isEmpty())
743 alignStructs.setEnabled(false);
744 alignStructs.setToolTipText(null);
748 alignStructs.setEnabled(true);
749 alignStructs.setToolTipText(MessageManager.formatMessage(
750 "label.align_structures_using_linked_alignment_views",
755 viewSelectionMenu = new ViewSelectionMenu(
756 MessageManager.getString("label.superpose_with"), this,
757 _alignwith, handler);
758 handler.itemStateChanged(null);
759 viewerActionMenu.add(viewSelectionMenu, 0);
760 viewerActionMenu.addMenuListener(new MenuListener()
763 public void menuSelected(MenuEvent e)
765 handler.itemStateChanged(null);
769 public void menuDeselected(MenuEvent e)
774 public void menuCanceled(MenuEvent e)
779 viewerActionMenu.setText(getViewerName());
780 helpItem.setText(MessageManager.formatMessage("label.viewer_help",
787 * Sends commands to the structure viewer to superimpose structures based on
788 * currently associated alignments. May optionally return an error message for
792 protected String alignStructsWithAllAlignPanels()
794 if (getAlignmentPanel() == null)
799 if (_alignwith.size() == 0)
801 _alignwith.add(getAlignmentPanel());
807 reply = getBinding().superposeStructures(_alignwith);
808 if (reply != null && !reply.isEmpty())
810 String text = MessageManager
811 .formatMessage("error.superposition_failed", reply);
812 statusBar.setText(text);
814 } catch (Exception e)
816 StringBuffer sp = new StringBuffer();
817 for (AlignmentViewPanel alignPanel : _alignwith)
819 sp.append("'" + alignPanel.getViewName() + "' ");
821 Console.info("Couldn't align structures with the " + sp.toString()
822 + "associated alignment panels.", e);
828 * Opens a colour chooser dialog, and applies the chosen colour to the
829 * background of the structure viewer
832 public void background_actionPerformed()
834 String ttl = MessageManager.getString("label.select_background_colour");
835 ColourChooserListener listener = new ColourChooserListener()
838 public void colourSelected(Color c)
840 getBinding().setBackgroundColour(c);
843 JalviewColourChooser.showColourChooser(this, ttl, null, listener);
847 public void viewerColour_actionPerformed()
849 if (viewerColour.isSelected())
851 // disable automatic sequence colouring.
852 getBinding().setColourBySequence(false);
857 public void chainColour_actionPerformed()
859 chainColour.setSelected(true);
860 getBinding().colourByChain();
864 public void chargeColour_actionPerformed()
866 chargeColour.setSelected(true);
867 getBinding().colourByCharge();
871 public void seqColour_actionPerformed()
873 AAStructureBindingModel binding = getBinding();
874 binding.setColourBySequence(seqColour.isSelected());
875 if (_colourwith == null)
877 _colourwith = new Vector<>();
879 if (binding.isColourBySequence())
881 if (!binding.isLoadingFromArchive())
883 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
885 // Make the currently displayed alignment panel the associated view
886 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
889 // Set the colour using the current view for the associated alignframe
890 for (AlignmentViewPanel alignPanel : _colourwith)
892 binding.colourBySequence(alignPanel);
894 seqColoursApplied = true;
899 public void pdbFile_actionPerformed()
901 // TODO: JAL-3048 not needed for Jalview-JS - save PDB file
902 JalviewFileChooser chooser = new JalviewFileChooser(
903 Cache.getProperty("LAST_DIRECTORY"));
905 chooser.setFileView(new JalviewFileView());
906 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
907 chooser.setToolTipText(MessageManager.getString("action.save"));
909 int value = chooser.showSaveDialog(this);
911 if (value == JalviewFileChooser.APPROVE_OPTION)
913 BufferedReader in = null;
916 // TODO: cope with multiple PDB files in view
917 in = new BufferedReader(
918 new FileReader(getBinding().getStructureFiles()[0]));
919 File outFile = chooser.getSelectedFile();
921 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
923 while ((data = in.readLine()) != null)
925 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
931 } catch (Exception ex)
933 ex.printStackTrace();
941 } catch (IOException e)
951 public void viewMapping_actionPerformed()
953 CutAndPasteTransfer cap = new CutAndPasteTransfer();
956 cap.appendText(getBinding().printMappings());
957 } catch (OutOfMemoryError e)
960 "composing sequence-structure alignments for display in text box.",
965 Desktop.addInternalFrame(cap,
966 MessageManager.getString("label.pdb_sequence_mapping"), 550,
970 protected abstract String getViewerName();
973 * Configures the title and menu items of the viewer panel.
976 public void updateTitleAndMenus()
978 AAStructureBindingModel binding = getBinding();
979 if (binding.hasFileLoadingError())
984 setChainMenuItems(binding.getChainNames());
986 this.setTitle(binding.getViewerTitle(getViewerName(), true));
989 * enable 'Superpose with' if more than one mapped structure
991 viewSelectionMenu.setEnabled(false);
992 if (getBinding().getMappedStructureCount() > 1
993 && getBinding().getSequence().length > 1)
995 viewSelectionMenu.setEnabled(true);
999 * Show action menu if it has any enabled items
1001 viewerActionMenu.setVisible(false);
1002 for (int i = 0; i < viewerActionMenu.getItemCount(); i++)
1004 if (viewerActionMenu.getItem(i).isEnabled())
1006 viewerActionMenu.setVisible(true);
1011 if (!binding.isLoadingFromArchive())
1013 seqColour_actionPerformed();
1018 public String toString()
1024 public boolean hasMapping()
1026 if (worker != null && (addingStructures || _started))
1030 if (getBinding() == null)
1032 if (_aps == null || _aps.size() == 0)
1034 // viewer has been closed, but we did at some point run.
1039 String[] pdbids = getBinding().getStructureFiles();
1045 for (String pdbid : pdbids)
1047 StructureMapping sm[] = getBinding().getSsm().getMapping(pdbid);
1048 if (sm != null && sm.length > 0 && sm[0] != null)
1053 // only return true if there is a mapping for every structure file we have
1055 if (p == 0 || p != pdbids.length)
1059 // and that coloring has been applied
1060 return seqColoursApplied;
1064 public void raiseViewer()
1070 public long startProgressBar(String msg)
1072 // TODO would rather have startProgress/stopProgress as the
1073 // IProgressIndicator interface
1074 long tm = random.nextLong();
1075 if (progressBar != null)
1077 progressBar.setProgressBar(msg, tm);
1083 public void stopProgressBar(String msg, long handle)
1085 if (progressBar != null)
1087 progressBar.setProgressBar(msg, handle);
1091 protected IProgressIndicator getProgressIndicator()
1096 protected void setProgressIndicator(IProgressIndicator pi)
1101 public void setProgressMessage(String message, long id)
1103 if (progressBar != null)
1105 progressBar.setProgressBar(message, id);
1110 public void showConsole(boolean show)
1112 // default does nothing
1116 * Show only the selected chain(s) in the viewer
1118 protected void showSelectedChains()
1120 List<String> toshow = new ArrayList<>();
1121 for (int i = 0; i < chainMenu.getItemCount(); i++)
1123 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
1125 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
1126 if (item.isSelected())
1128 toshow.add(item.getText());
1132 getBinding().showChains(toshow);
1136 * Tries to fetch a PDB file and save to a temporary local file. Returns the
1137 * saved file path if successful, or null if not.
1139 * @param processingEntry
1142 protected String fetchPdbFile(PDBEntry processingEntry)
1144 String filePath = null;
1145 Pdb pdbclient = new Pdb();
1146 EBIAlfaFold afclient = new EBIAlfaFold();
1147 AlignmentI pdbseq = null;
1148 String pdbid = processingEntry.getId();
1149 long handle = System.currentTimeMillis()
1150 + Thread.currentThread().hashCode();
1153 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
1155 String msg = MessageManager.formatMessage("status.fetching_pdb",
1158 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1159 // long hdl = startProgressBar(MessageManager.formatMessage(
1160 // "status.fetching_pdb", new Object[]
1164 if (afclient.isValidReference(pdbid))
1166 pdbseq = afclient.getSequenceRecords(pdbid,
1167 processingEntry.getRetrievalUrl());
1171 if (processingEntry.hasRetrievalUrl())
1173 String safePDBId = java.net.URLEncoder.encode(pdbid, "UTF-8")
1174 .replace("%", "__");
1176 // retrieve from URL to new local tmpfile
1177 File tmpFile = File.createTempFile(safePDBId,
1178 "." + (PDBEntry.Type.MMCIF.toString().equals(
1179 processingEntry.getType().toString()) ? "cif"
1181 String fromUrl = processingEntry.getRetrievalUrl();
1182 UrlDownloadClient.download(fromUrl, tmpFile);
1184 // may not need this check ?
1185 String file = tmpFile.getAbsolutePath();
1188 pdbseq = EBIAlfaFold.importDownloadedStructureFromUrl(fromUrl,
1189 tmpFile, pdbid, null, null, null);
1194 pdbseq = pdbclient.getSequenceRecords(pdbid);
1197 } catch (Exception e)
1200 "Error retrieving PDB id " + pdbid + ": " + e.getMessage());
1203 msg = pdbid + " " + MessageManager.getString("label.state_completed");
1204 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
1205 // stopProgressBar(msg, hdl);
1208 * If PDB data were saved and are not invalid (empty alignment), return the
1211 if (pdbseq != null && pdbseq.getHeight() > 0)
1213 // just use the file name from the first sequence's first PDBEntry
1214 filePath = new File(pdbseq.getSequenceAt(0).getAllPDBEntries()
1215 .elementAt(0).getFile()).getAbsolutePath();
1216 processingEntry.setFile(filePath);
1222 * If supported, saves the state of the structure viewer to a temporary file
1223 * and returns the file, else returns null
1227 public File saveSession()
1229 if (getBinding() == null)
1233 File session = getBinding().saveSession();
1234 long l = session.length();
1241 } catch (InterruptedException e)
1244 long nextl = session.length();
1250 } while (--wait > 0);
1255 * Close down this instance of Jalview's Chimera viewer, giving the user the
1256 * option to close the associated Chimera window (process). They may wish to
1257 * keep it open until they have had an opportunity to save any work.
1260 * if true, close any linked Chimera process; if false, prompt first
1263 public void closeViewer(boolean forceClose)
1265 AAStructureBindingModel binding = getBinding();
1266 if (binding != null && binding.isViewerRunning())
1270 String viewerName = getViewerName();
1271 String prompt = MessageManager
1272 .formatMessage("label.confirm_close_viewer", new Object[]
1273 { binding.getViewerTitle(viewerName, false), viewerName });
1274 prompt = JvSwingUtils.wrapTooltip(true, prompt);
1275 int confirm = JvOptionPane.showConfirmDialog(this, prompt,
1276 MessageManager.getString("label.close_viewer"),
1277 JvOptionPane.YES_NO_CANCEL_OPTION);
1279 * abort closure if user hits escape or Cancel
1281 if (confirm == JvOptionPane.CANCEL_OPTION
1282 || confirm == JvOptionPane.CLOSED_OPTION)
1284 // abort possible quit handling if CANCEL chosen
1285 if (confirm == JvOptionPane.CANCEL_OPTION)
1286 QuitHandler.abortQuit();
1289 forceClose = confirm == JvOptionPane.YES_OPTION;
1292 if (binding != null)
1294 binding.closeViewer(forceClose);
1296 setAlignmentPanel(null);
1299 _colourwith.clear();
1300 // TODO: check for memory leaks where instance isn't finalised because jmb
1301 // holds a reference to the window
1307 public void showHelp_actionPerformed()
1313 String url = getBinding().getHelpURL();
1316 BrowserLauncher.openURL(url);
1320 catch (IOException ex)
1323 .println("Show " + getViewerName() + " failed with: "
1330 public boolean hasViewerActionsMenu()
1332 return viewerActionMenu != null && viewerActionMenu.isEnabled()
1333 && viewerActionMenu.getItemCount() > 0
1334 && viewerActionMenu.isVisible();