3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
6 import java.util.ArrayList;
9 import javax.swing.JInternalFrame;
10 import javax.swing.JMenuItem;
11 import javax.swing.event.InternalFrameAdapter;
12 import javax.swing.event.InternalFrameEvent;
14 import jalview.api.AlignmentViewPanel;
15 import jalview.api.FeatureRenderer;
16 import jalview.bin.Cache;
17 import jalview.datamodel.PDBEntry;
18 import jalview.datamodel.SequenceI;
19 import jalview.gui.StructureViewer.ViewerType;
20 import jalview.io.DataSourceType;
21 import jalview.io.StructureFile;
22 import jalview.structures.models.AAStructureBindingModel;
23 import jalview.util.MessageManager;
25 public class PymolViewer extends StructureViewerBase
27 private static final int myWidth = 500;
29 private static final int myHeight = 150;
31 private PymolBindingModel binding;
33 private String pymolSessionFile;
40 * closeViewer will decide whether or not to close this frame
41 * depending on whether user chooses to Cancel or not
43 setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
46 public PymolViewer(PDBEntry pdb, SequenceI[] seqs, Object object,
50 openNewPymol(ap, new PDBEntry[] { pdb },
55 public PymolViewer(PDBEntry[] pe, boolean alignAdded, SequenceI[][] seqs,
59 setAlignAddedStructures(alignAdded);
60 openNewPymol(ap, pe, seqs);
64 * Constructor given a session file to be restored
70 * @param colourByPymol
71 * @param colourBySequence
74 public PymolViewer(String sessionFile, AlignmentPanel alignPanel,
75 PDBEntry[] pdbArray, SequenceI[][] seqsArray,
76 boolean colourByPymol, boolean colourBySequence, String newViewId)
78 // TODO convert to base/factory class method
81 this.pymolSessionFile = sessionFile;
82 openNewPymol(alignPanel, pdbArray, seqsArray);
85 binding.setColourBySequence(false);
86 seqColour.setSelected(false);
87 viewerColour.setSelected(true);
89 else if (colourBySequence)
91 binding.setColourBySequence(true);
92 seqColour.setSelected(true);
93 viewerColour.setSelected(false);
97 private void openNewPymol(AlignmentPanel ap, PDBEntry[] pe,
101 binding = new PymolBindingModel(this, ap.getStructureSelectionManager(),
103 addAlignmentPanel(ap);
104 useAlignmentPanelForColourbyseq(ap);
108 useAlignmentPanelForSuperposition(ap);
110 binding.setColourBySequence(true);
111 setSize(myWidth, myHeight);
113 viewerActionMenu.setText("PyMOL");
114 updateTitleAndMenus();
116 addingStructures = false;
117 worker = new Thread(this);
120 this.addInternalFrameListener(new InternalFrameAdapter()
123 public void internalFrameClosing(
124 InternalFrameEvent internalFrameEvent)
133 * Create a helper to manage progress bar display
135 protected void createProgressBar()
137 if (getProgressIndicator() == null)
139 setProgressIndicator(new ProgressBar(statusPanel, statusBar));
146 // todo pull up much of this
148 StringBuilder errormsgs = new StringBuilder(128);
149 List<PDBEntry> filePDB = new ArrayList<>();
150 List<Integer> filePDBpos = new ArrayList<>();
151 String[] curfiles = binding.getStructureFiles(); // files currently in viewer
152 for (int pi = 0; pi < binding.getPdbCount(); pi++)
155 PDBEntry thePdbEntry = binding.getPdbEntry(pi);
156 if (thePdbEntry.getFile() == null)
159 * Retrieve PDB data, save to file, attach to PDBEntry
161 file = fetchPdbFile(thePdbEntry);
164 errormsgs.append("'" + thePdbEntry.getId() + "' ");
172 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
174 // todo - skip if already loaded in PyMOL
178 filePDB.add(thePdbEntry);
179 filePDBpos.add(Integer.valueOf(pi));
183 if (!filePDB.isEmpty())
186 * at least one structure to add to viewer
188 binding.setFinishedInit(false);
189 if (!addingStructures)
194 } catch (Exception ex)
196 Cache.log.error("Couldn't open PyMOL viewer!", ex);
200 for (PDBEntry pe : filePDB)
203 if (pe.getFile() != null)
207 int pos = filePDBpos.get(num).intValue();
208 long startTime = startProgressBar(getViewerName() + " "
209 + MessageManager.getString("status.opening_file_for")
211 binding.openFile(pe);
212 binding.addSequence(pos, binding.getSequence()[pos]);
213 File fl = new File(pe.getFile());
214 DataSourceType protocol = DataSourceType.URL;
219 protocol = DataSourceType.FILE;
221 } catch (Throwable e)
225 stopProgressBar("", startTime);
228 StructureFile pdb = binding.getSsm().setMapping(
229 binding.getSequence()[pos], binding.getChains()[pos],
230 pe.getFile(), protocol,
231 getProgressIndicator());
232 binding.stashFoundChains(pdb, pe.getFile());
233 } catch (Exception ex)
236 "Couldn't open " + pe.getFile() + " in Chimera viewer!",
240 // Cache.log.debug("File locations are " + files);
245 binding.refreshGUI();
246 binding.setFinishedInit(true);
247 binding.setLoadingFromArchive(false);
250 * ensure that any newly discovered features (e.g. RESNUM)
251 * are added to any open feature settings dialog
253 FeatureRenderer fr = getBinding().getFeatureRenderer(null);
259 // refresh the sequence colours for the new structure(s)
260 for (AlignmentViewPanel ap : _colourwith)
262 binding.updateColours(ap);
264 // do superposition if asked to
265 if (alignAddedStructures)
267 new Thread(new Runnable()
272 alignStructsWithAllAlignPanels();
276 addingStructures = false;
284 * Launch PyMOL. If we have a session file name, send PyMOL the command to
285 * open its saved session file.
289 Desktop.addInternalFrame(this,
290 binding.getViewerTitle(getViewerName(), true),
291 getBounds().width, getBounds().height);
293 if (!binding.launchPymol())
295 JvOptionPane.showMessageDialog(Desktop.desktop,
296 MessageManager.formatMessage("label.open_viewer_failed",
298 MessageManager.getString("label.error_loading_file"),
299 JvOptionPane.ERROR_MESSAGE);
304 if (this.pymolSessionFile != null)
306 boolean opened = binding.openSession(pymolSessionFile);
309 System.err.println("An error occurred opening PyMOL session file "
313 // binding.startPymolListener();
317 public AAStructureBindingModel getBinding()
323 public ViewerType getViewerType()
325 return ViewerType.PYMOL;
329 protected String getViewerName()
335 protected void initMenus()
339 savemenu.setVisible(false); // not yet implemented
340 viewMenu.add(fitToWindow);
342 JMenuItem writeFeatures = new JMenuItem(
343 MessageManager.getString("label.create_viewer_attributes"));
344 writeFeatures.setToolTipText(MessageManager
345 .getString("label.create_viewer_attributes_tip"));
346 writeFeatures.addActionListener(new ActionListener()
349 public void actionPerformed(ActionEvent e)
351 sendFeaturesToPymol();
354 viewerActionMenu.add(writeFeatures);
357 protected void sendFeaturesToPymol()
359 int count = binding.sendFeaturesToViewer(getAlignmentPanel());
361 MessageManager.formatMessage("label.attributes_set", count));