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.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.ItemEvent;
26 import java.awt.event.ItemListener;
27 import java.io.BufferedReader;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.FileReader;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.PrintWriter;
35 import java.util.ArrayList;
36 import java.util.List;
38 import java.util.Random;
40 import java.util.Vector;
42 import javax.swing.JCheckBoxMenuItem;
43 import javax.swing.JColorChooser;
44 import javax.swing.JInternalFrame;
45 import javax.swing.JMenu;
46 import javax.swing.JMenuItem;
47 import javax.swing.JOptionPane;
48 import javax.swing.event.InternalFrameAdapter;
49 import javax.swing.event.InternalFrameEvent;
50 import javax.swing.event.MenuEvent;
51 import javax.swing.event.MenuListener;
53 import jalview.bin.Cache;
54 import jalview.datamodel.Alignment;
55 import jalview.datamodel.AlignmentI;
56 import jalview.datamodel.ColumnSelection;
57 import jalview.datamodel.PDBEntry;
58 import jalview.datamodel.SequenceI;
59 import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
60 import jalview.gui.StructureViewer.ViewerType;
61 import jalview.io.AppletFormatAdapter;
62 import jalview.io.JalviewFileChooser;
63 import jalview.io.JalviewFileView;
64 import jalview.schemes.BuriedColourScheme;
65 import jalview.schemes.ColourSchemeI;
66 import jalview.schemes.HelixColourScheme;
67 import jalview.schemes.HydrophobicColourScheme;
68 import jalview.schemes.PurinePyrimidineColourScheme;
69 import jalview.schemes.StrandColourScheme;
70 import jalview.schemes.TaylorColourScheme;
71 import jalview.schemes.TurnColourScheme;
72 import jalview.schemes.ZappoColourScheme;
73 import jalview.structures.models.AAStructureBindingModel;
74 import jalview.util.MessageManager;
75 import jalview.util.Platform;
76 import jalview.ws.dbsources.Pdb;
79 * GUI elements for handling an external chimera display
84 public class ChimeraViewFrame extends StructureViewerBase
86 private JalviewChimeraBinding jmb;
88 private boolean allChainsSelected = false;
90 private boolean alignAddedStructures = false;
93 * state flag for PDB retrieval thread
95 private boolean _started = false;
97 private boolean addingStructures = false;
99 private IProgressIndicator progressBar = null;
102 * pdb retrieval thread.
104 private Thread worker = null;
107 * Path to Chimera session file. This is set when an open Jalview/Chimera
108 * session is saved, or on restore from a Jalview project (if it holds the
109 * filename of any saved Chimera sessions).
111 private String chimeraSessionFile = null;
113 private Random random = new Random();
116 * Initialise menu options.
118 private void initMenus()
120 viewerActionMenu.setText(MessageManager.getString("label.chimera"));
121 viewerColour.setText(MessageManager
122 .getString("label.colour_with_chimera"));
123 viewerColour.setToolTipText(MessageManager
124 .getString("label.let_chimera_manage_structure_colours"));
125 helpItem.setText(MessageManager.getString("label.chimera_help"));
126 seqColour.setSelected(jmb.isColourBySequence());
127 viewerColour.setSelected(!jmb.isColourBySequence());
128 if (_colourwith == null)
130 _colourwith = new Vector<AlignmentPanel>();
132 if (_alignwith == null)
134 _alignwith = new Vector<AlignmentPanel>();
137 // save As not yet implemented
138 savemenu.setVisible(false);
140 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
141 MessageManager.getString("label.colour_by"), this, _colourwith,
145 public void itemStateChanged(ItemEvent e)
147 if (!seqColour.isSelected())
153 // update the Chimera display now.
154 seqColour_actionPerformed(null);
158 viewMenu.add(seqColourBy);
159 viewMenu.add(fitToWindow);
161 final ItemListener handler;
162 JMenu alpanels = new ViewSelectionMenu(
163 MessageManager.getString("label.superpose_with"), this,
164 _alignwith, handler = new ItemListener()
167 public void itemStateChanged(ItemEvent e)
169 alignStructs.setEnabled(_alignwith.size() > 0);
170 alignStructs.setToolTipText(MessageManager
172 "label.align_structures_using_linked_alignment_views",
174 { new Integer(_alignwith.size()).toString() }));
177 handler.itemStateChanged(null);
178 viewerActionMenu.add(alpanels);
179 viewerActionMenu.addMenuListener(new MenuListener()
183 public void menuSelected(MenuEvent e)
185 handler.itemStateChanged(null);
189 public void menuDeselected(MenuEvent e)
191 // TODO Auto-generated method stub
195 public void menuCanceled(MenuEvent e)
197 // TODO Auto-generated method stub
203 * add a single PDB structure to a new or existing Chimera view
210 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
211 String[] chains, final AlignmentPanel ap)
216 * is the pdb file already loaded?
218 String pdbId = pdbentry.getId();
219 String alreadyMapped = ap.getStructureSelectionManager()
220 .alreadyMappedToFile(pdbId);
222 if (alreadyMapped != null)
224 int option = chooseAddSequencesToViewer(pdbId);
225 if (option == JOptionPane.CANCEL_OPTION)
229 if (option == JOptionPane.YES_OPTION)
231 addSequenceMappingsToStructure(seq, chains, ap, alreadyMapped);
237 * Check if there are other Chimera views involving this alignment and give
238 * user the option to add and align this molecule to one of them
240 List<ChimeraViewFrame> existingViews = getChimeraWindowsFor(ap);
241 for (ChimeraViewFrame view : existingViews)
243 // TODO: highlight view somehow
245 * JAL-1742 exclude view with this structure already mapped (don't offer
246 * to align chain B to chain A of the same structure)
248 if (view.hasPdbId(pdbId))
252 int option = chooseAlignStructureToViewer(pdbId, view);
253 if (option == JOptionPane.CANCEL_OPTION)
257 if (option == JOptionPane.YES_OPTION)
259 view.useAlignmentPanelForSuperposition(ap);
260 view.addStructure(pdbentry, seq, chains, true, ap.alignFrame);
266 * If the options above are declined or do not apply, open a new viewer
268 openNewChimera(ap, new PDBEntry[]
269 { pdbentry }, new SequenceI[][]
274 * Presents a dialog with the option to add an align a structure to an
275 * existing Chimera view
279 * @return YES, NO or CANCEL JOptionPane code
281 protected int chooseAlignStructureToViewer(String pdbId,
282 ChimeraViewFrame view)
284 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
285 MessageManager.formatMessage("label.add_pdbentry_to_view",
287 { pdbId, view.getTitle() }), MessageManager
288 .getString("label.align_to_existing_structure_view"),
289 JOptionPane.YES_NO_CANCEL_OPTION);
294 * Presents a dialog with the option to add sequences to a viewer which
295 * already has their structure open
298 * @return YES, NO or CANCEL JOptionPane code
300 protected int chooseAddSequencesToViewer(String pdbId)
302 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
303 MessageManager.formatMessage(
304 "label.pdb_entry_is_already_displayed", new Object[]
305 { pdbId }), MessageManager.formatMessage(
306 "label.map_sequences_to_visible_window", new Object[]
307 { pdbId }), JOptionPane.YES_NO_CANCEL_OPTION);
312 * Adds mappings for the given sequences to an already opened PDB structure,
313 * and updates any viewers that have the PDB file
320 protected void addSequenceMappingsToStructure(SequenceI[] seq,
321 String[] chains, final AlignmentPanel ap, String pdbFilename)
323 // TODO : Fix multiple seq to one chain issue here.
325 * create the mappings
327 ap.getStructureSelectionManager().setMapping(seq, chains, pdbFilename,
328 AppletFormatAdapter.FILE);
331 * alert the FeatureRenderer to show new (PDB RESNUM) features
333 if (ap.getSeqPanel().seqCanvas.fr != null)
335 ap.getSeqPanel().seqCanvas.fr.featuresAdded();
336 ap.paintAlignment(true);
340 * add the sequences to any other Chimera viewers for this pdb file
342 // JBPNOTE: this looks like a binding routine, rather than a gui routine
343 for (JInternalFrame frame : Desktop.instance.getAllFrames())
345 if (frame instanceof ChimeraViewFrame)
347 ChimeraViewFrame chimeraView = ((ChimeraViewFrame) frame);
348 for (int pe = 0; pe < chimeraView.jmb.getPdbCount(); pe++)
350 if (chimeraView.jmb.getPdbEntry(pe).getFile().equals(pdbFilename))
352 chimeraView.jmb.addSequence(pe, seq);
353 chimeraView.addAlignmentPanel(ap);
355 * add it to the set of alignments used for colouring structure by
358 chimeraView.useAlignmentPanelForColourbyseq(ap);
359 chimeraView.buildActionMenu();
360 ap.getStructureSelectionManager().sequenceColoursChanged(ap);
369 * Create a helper to manage progress bar display
371 protected void createProgressBar()
373 if (progressBar == null)
375 progressBar = new ProgressBar(statusPanel, statusBar);
379 protected boolean hasPdbId(String pdbId)
381 return jmb.hasPdbId(pdbId);
384 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
388 String[][] chains = extractChains(seqs);
389 jmb = new JalviewChimeraBindingModel(this,
390 ap.getStructureSelectionManager(), pdbentrys, seqs, chains,
392 addAlignmentPanel(ap);
393 useAlignmentPanelForColourbyseq(ap);
394 if (pdbentrys.length > 1)
396 alignAddedStructures = true;
397 useAlignmentPanelForSuperposition(ap);
399 jmb.setColourBySequence(true);
400 setSize(400, 400); // probably should be a configurable/dynamic default here
403 addingStructures = false;
404 worker = new Thread(this);
407 this.addInternalFrameListener(new InternalFrameAdapter()
409 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
418 * Retrieve chains for sequences by inspecting their PDB refs. The hope is
419 * that the first will be to the sequence's own chain. Really need a more
420 * managed way of doing this.
425 protected String[][] extractChains(SequenceI[][] seqs)
427 String[][] chains = new String[seqs.length][];
428 for (int i = 0; i < seqs.length; i++)
430 chains[i] = new String[seqs[i].length];
432 for (SequenceI seq : seqs[i])
435 if (seq.getDatasetSequence() != null)
437 Vector<PDBEntry> pdbrefs = seq.getDatasetSequence().getPDBId();
438 if (pdbrefs != null && pdbrefs.size() > 0)
440 chain = pdbrefs.get(0).getChainCode();
443 chains[i][seqno++] = chain;
450 * Create a new viewer from saved session state data including Chimera session
453 * @param chimeraSessionFile
457 * @param colourByChimera
458 * @param colourBySequence
461 public ChimeraViewFrame(String chimeraSessionFile,
462 AlignmentPanel alignPanel,
464 SequenceI[][] seqsArray, boolean colourByChimera,
465 boolean colourBySequence, String newViewId)
468 setViewId(newViewId);
469 this.chimeraSessionFile = chimeraSessionFile;
470 openNewChimera(alignPanel, pdbArray, seqsArray);
473 jmb.setColourBySequence(false);
474 seqColour.setSelected(false);
475 viewerColour.setSelected(true);
477 else if (colourBySequence)
479 jmb.setColourBySequence(true);
480 seqColour.setSelected(true);
481 viewerColour.setSelected(false);
486 * create a new viewer containing several structures superimposed using the
493 public ChimeraViewFrame(PDBEntry[] pe, SequenceI[][] seqs,
497 openNewChimera(ap, pe, seqs);
500 public ChimeraViewFrame(Map<PDBEntry, List<SequenceI>> toView,
501 AlignmentPanel alignPanel)
506 * Convert the map of sequences per pdb entry into the tied arrays expected
509 * TODO pass the Map down to openNewChimera and its callees instead
511 final Set<PDBEntry> pdbEntries = toView.keySet();
512 PDBEntry[] pdbs = pdbEntries.toArray(new PDBEntry[pdbEntries.size()]);
513 SequenceI[][] seqsForPdbs = new SequenceI[pdbEntries.size()][];
514 for (int i = 0; i < pdbs.length; i++)
516 final List<SequenceI> seqsForPdb = toView.get(pdbs[i]);
517 seqsForPdbs[i] = seqsForPdb.toArray(new SequenceI[seqsForPdb.size()]);
520 openNewChimera(alignPanel, pdbs, seqsForPdbs);
524 * add a new structure (with associated sequences and chains) to this viewer,
525 * retrieving it if necessary first.
532 * if true, new structure(s) will be align using associated alignment
534 private void addStructure(final PDBEntry pdbentry, final SequenceI[] seq,
535 final String[] chains, final boolean b,
536 final IProgressIndicator alignFrame)
538 if (pdbentry.getFile() == null)
540 if (worker != null && worker.isAlive())
542 // a retrieval is in progress, wait around and add ourselves to the
544 new Thread(new Runnable()
548 while (worker != null && worker.isAlive() && _started)
552 Thread.sleep(100 + ((int) Math.random() * 100));
554 } catch (Exception e)
559 // and call ourselves again.
560 addStructure(pdbentry, seq, chains, b, alignFrame);
566 // otherwise, start adding the structure.
567 jmb.addSequenceAndChain(new PDBEntry[]
568 { pdbentry }, new SequenceI[][]
569 { seq }, new String[][]
571 addingStructures = true;
573 alignAddedStructures = b;
574 // progressBar = alignFrame; // visual indication happens on caller frame.
575 (worker = new Thread(this)).start();
579 private List<ChimeraViewFrame> getChimeraWindowsFor(AlignmentPanel apanel)
581 List<ChimeraViewFrame> result = new ArrayList<ChimeraViewFrame>();
582 JInternalFrame[] frames = Desktop.instance.getAllFrames();
584 for (JInternalFrame frame : frames)
586 if (frame instanceof ChimeraViewFrame)
588 if (((StructureViewerBase) frame).isLinkedWith(apanel))
590 result.add((ChimeraViewFrame) frame);
598 * Launch Chimera. If we have a chimera session file name, send Chimera the
599 * command to open its saved session file.
603 jmb.setFinishedInit(false);
604 jalview.gui.Desktop.addInternalFrame(this, jmb.getViewerTitle("Chimera", true),
605 getBounds().width, getBounds().height);
607 if (!jmb.launchChimera())
612 MessageManager.getString("label.chimera_failed"),
613 MessageManager.getString("label.error_loading_file"),
614 JOptionPane.ERROR_MESSAGE);
619 if (this.chimeraSessionFile != null)
621 boolean opened = jmb.openSession(chimeraSessionFile);
625 .println("An error occurred opening Chimera session file "
626 + chimeraSessionFile);
629 jmb.setFinishedInit(true);
631 jmb.startChimeraListener();
635 * If the list is not empty, add menu items for 'All' and each individual
636 * chain to the "View | Show Chain" sub-menu. Multiple selections are allowed.
640 void setChainMenuItems(List<String> chainNames)
642 chainMenu.removeAll();
643 if (chainNames == null || chainNames.isEmpty())
647 JMenuItem menuItem = new JMenuItem(
648 MessageManager.getString("label.all"));
649 menuItem.addActionListener(new ActionListener()
651 public void actionPerformed(ActionEvent evt)
653 allChainsSelected = true;
654 for (int i = 0; i < chainMenu.getItemCount(); i++)
656 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
658 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
661 showSelectedChains();
662 allChainsSelected = false;
666 chainMenu.add(menuItem);
668 for (String chainName : chainNames)
670 menuItem = new JCheckBoxMenuItem(chainName, true);
671 menuItem.addItemListener(new ItemListener()
673 public void itemStateChanged(ItemEvent evt)
675 if (!allChainsSelected)
677 showSelectedChains();
682 chainMenu.add(menuItem);
687 * Show only the selected chain(s) in the viewer
689 void showSelectedChains()
691 List<String> toshow = new ArrayList<String>();
692 for (int i = 0; i < chainMenu.getItemCount(); i++)
694 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
696 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
697 if (item.isSelected())
699 toshow.add(item.getText());
703 jmb.showChains(toshow);
707 * Close down this instance of Jalview's Chimera viewer, giving the user the
708 * option to close the associated Chimera window (process). They may wish to
709 * keep it open until they have had an opportunity to save any work.
711 * @param closeChimera
712 * if true, close any linked Chimera process; if false, prompt first
714 public void closeViewer(boolean closeChimera)
716 if (jmb.isChimeraRunning())
720 String prompt = MessageManager.formatMessage(
721 "label.confirm_close_chimera", new Object[]
722 { jmb.getViewerTitle("Chimera", false) });
723 prompt = JvSwingUtils.wrapTooltip(true, prompt);
724 int confirm = JOptionPane.showConfirmDialog(this, prompt,
725 MessageManager.getString("label.close_viewer"),
726 JOptionPane.YES_NO_OPTION);
727 closeChimera = confirm == JOptionPane.YES_OPTION;
729 jmb.closeViewer(closeChimera);
731 setAlignmentPanel(null);
735 // TODO: check for memory leaks where instance isn't finalised because jmb
736 // holds a reference to the window
741 * Open any newly added PDB structures in Chimera, having first fetched data
742 * from PDB (if not already saved).
747 // todo - record which pdbids were successfully imported.
748 StringBuilder errormsgs = new StringBuilder(128);
749 StringBuilder files = new StringBuilder(128);
750 List<PDBEntry> filePDB = new ArrayList<PDBEntry>();
751 List<Integer> filePDBpos = new ArrayList<Integer>();
752 PDBEntry thePdbEntry = null;
755 String[] curfiles = jmb.getPdbFile(); // files currently in viewer
756 // TODO: replace with reference fetching/transfer code (validate PDBentry
758 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
761 thePdbEntry = jmb.getPdbEntry(pi);
762 if (thePdbEntry.getFile() == null)
765 * Retrieve PDB data, save to file, attach to PDBEntry
767 file = fetchPdbFile(thePdbEntry);
770 errormsgs.append("'" + thePdbEntry.getId() + "' ");
776 * Got file already - ignore if already loaded in Chimera.
778 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
780 if (curfiles != null && curfiles.length > 0)
782 addingStructures = true; // already files loaded.
783 for (int c = 0; c < curfiles.length; c++)
785 if (curfiles[c].equals(file))
795 filePDB.add(thePdbEntry);
796 filePDBpos.add(Integer.valueOf(pi));
797 files.append(" \"" + Platform.escapeString(file) + "\"");
800 } catch (OutOfMemoryError oomerror)
802 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
804 } catch (Exception ex)
806 ex.printStackTrace();
807 errormsgs.append("When retrieving pdbfiles for '"
808 + thePdbEntry.getId() + "'");
810 if (errormsgs.length() > 0)
813 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
814 .formatMessage("label.pdb_entries_couldnt_be_retrieved",
816 { errormsgs.toString() }), MessageManager
817 .getString("label.couldnt_load_file"),
818 JOptionPane.ERROR_MESSAGE);
821 if (files.length() > 0)
823 if (!addingStructures)
828 } catch (Exception ex)
830 Cache.log.error("Couldn't open Chimera viewer!", ex);
834 for (PDBEntry pe : filePDB)
837 if (pe.getFile() != null)
841 int pos = filePDBpos.get(num).intValue();
842 long startTime = startProgressBar("Chimera "
843 + MessageManager.getString("status.opening_file"));
845 jmb.addSequence(pos, jmb.getSequence()[pos]);
846 File fl = new File(pe.getFile());
847 String protocol = AppletFormatAdapter.URL;
852 protocol = AppletFormatAdapter.FILE;
854 } catch (Throwable e)
858 stopProgressBar("", startTime);
860 // Explicitly map to the filename used by Chimera ;
861 jmb.getSsm().setMapping(jmb.getSequence()[pos],
862 jmb.getChains()[pos],
865 } catch (OutOfMemoryError oomerror)
868 "When trying to open and map structures from Chimera!",
870 } catch (Exception ex)
872 Cache.log.error("Couldn't open " + pe.getFile()
873 + " in Chimera viewer!", ex);
876 Cache.log.debug("File locations are " + files);
880 jmb.setFinishedInit(true);
881 jmb.setLoadingFromArchive(false);
883 // refresh the sequence colours for the new structure(s)
884 for (AlignmentPanel ap : _colourwith)
886 jmb.updateColours(ap);
888 // do superposition if asked to
889 if (alignAddedStructures)
891 new Thread(new Runnable()
895 alignStructs_withAllAlignPanels();
898 alignAddedStructures = false;
900 addingStructures = false;
907 * Fetch PDB data and save to a local file. Returns the full path to the file,
908 * or null if fetch fails.
910 * @param processingEntry
914 private String fetchPdbFile(PDBEntry processingEntry) throws Exception
916 String filePath = null;
917 Pdb pdbclient = new Pdb();
918 AlignmentI pdbseq = null;
919 String pdbid = processingEntry.getId();
920 long handle = System.currentTimeMillis()
921 + Thread.currentThread().hashCode();
924 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
926 String msg = MessageManager.formatMessage("status.fetching_pdb",
929 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
930 // long hdl = startProgressBar(MessageManager.formatMessage(
931 // "status.fetching_pdb", new Object[]
935 pdbseq = pdbclient.getSequenceRecords(pdbid);
936 } catch (OutOfMemoryError oomerror)
938 new OOMWarning("Retrieving PDB id " + pdbid, oomerror);
942 + MessageManager.getString("label.state_completed");
943 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
944 // stopProgressBar(msg, hdl);
947 * If PDB data were saved and are not invalid (empty alignment), return the
950 if (pdbseq != null && pdbseq.getHeight() > 0)
952 // just use the file name from the first sequence's first PDBEntry
953 filePath = new File(pdbseq.getSequenceAt(0).getPDBId()
954 .elementAt(0).getFile()).getAbsolutePath();
955 processingEntry.setFile(filePath);
961 * Convenience method to update the progress bar if there is one. Be sure to
962 * call stopProgressBar with the returned handle to remove the message.
967 public long startProgressBar(String msg)
969 // TODO would rather have startProgress/stopProgress as the
970 // IProgressIndicator interface
971 long tm = random.nextLong();
972 if (progressBar != null)
974 progressBar.setProgressBar(msg, tm);
980 * End the progress bar with the specified handle, leaving a message (if not
981 * null) on the status bar
986 public void stopProgressBar(String msg, long handle)
988 if (progressBar != null)
990 progressBar.setProgressBar(msg, handle);
995 public void pdbFile_actionPerformed(ActionEvent actionEvent)
997 JalviewFileChooser chooser = new JalviewFileChooser(
998 jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
1000 chooser.setFileView(new JalviewFileView());
1001 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
1002 chooser.setToolTipText(MessageManager.getString("action.save"));
1004 int value = chooser.showSaveDialog(this);
1006 if (value == JalviewFileChooser.APPROVE_OPTION)
1008 BufferedReader in = null;
1011 // TODO: cope with multiple PDB files in view
1012 in = new BufferedReader(new FileReader(jmb.getPdbFile()[0]));
1013 File outFile = chooser.getSelectedFile();
1015 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1017 while ((data = in.readLine()) != null)
1019 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1025 } catch (Exception ex)
1027 ex.printStackTrace();
1035 } catch (IOException e)
1037 e.printStackTrace();
1045 public void viewMapping_actionPerformed(ActionEvent actionEvent)
1047 jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
1050 cap.appendText(jmb.printMappings());
1051 } catch (OutOfMemoryError e)
1054 "composing sequence-structure alignments for display in text box.",
1059 jalview.gui.Desktop.addInternalFrame(cap,
1060 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1065 public void eps_actionPerformed(ActionEvent e)
1069 .getString("error.eps_generation_not_implemented"));
1073 public void png_actionPerformed(ActionEvent e)
1077 .getString("error.png_generation_not_implemented"));
1081 public void viewerColour_actionPerformed(ActionEvent actionEvent)
1083 if (viewerColour.isSelected())
1085 // disable automatic sequence colouring.
1086 jmb.setColourBySequence(false);
1091 public void seqColour_actionPerformed(ActionEvent actionEvent)
1093 jmb.setColourBySequence(seqColour.isSelected());
1094 if (_colourwith == null)
1096 _colourwith = new Vector<AlignmentPanel>();
1098 if (jmb.isColourBySequence())
1100 if (!jmb.isLoadingFromArchive())
1102 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
1104 // Make the currently displayed alignment panel the associated view
1105 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
1108 // Set the colour using the current view for the associated alignframe
1109 for (AlignmentPanel ap : _colourwith)
1111 jmb.colourBySequence(ap.av.isShowSequenceFeatures(), ap);
1117 public void chainColour_actionPerformed(ActionEvent actionEvent)
1119 chainColour.setSelected(true);
1120 jmb.colourByChain();
1124 public void chargeColour_actionPerformed(ActionEvent actionEvent)
1126 chargeColour.setSelected(true);
1127 jmb.colourByCharge();
1131 public void zappoColour_actionPerformed(ActionEvent actionEvent)
1133 zappoColour.setSelected(true);
1134 jmb.setJalviewColourScheme(new ZappoColourScheme());
1138 public void taylorColour_actionPerformed(ActionEvent actionEvent)
1140 taylorColour.setSelected(true);
1141 jmb.setJalviewColourScheme(new TaylorColourScheme());
1145 public void hydroColour_actionPerformed(ActionEvent actionEvent)
1147 hydroColour.setSelected(true);
1148 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
1152 public void helixColour_actionPerformed(ActionEvent actionEvent)
1154 helixColour.setSelected(true);
1155 jmb.setJalviewColourScheme(new HelixColourScheme());
1159 public void strandColour_actionPerformed(ActionEvent actionEvent)
1161 strandColour.setSelected(true);
1162 jmb.setJalviewColourScheme(new StrandColourScheme());
1166 public void turnColour_actionPerformed(ActionEvent actionEvent)
1168 turnColour.setSelected(true);
1169 jmb.setJalviewColourScheme(new TurnColourScheme());
1173 public void buriedColour_actionPerformed(ActionEvent actionEvent)
1175 buriedColour.setSelected(true);
1176 jmb.setJalviewColourScheme(new BuriedColourScheme());
1180 public void purinePyrimidineColour_actionPerformed(ActionEvent actionEvent)
1182 setJalviewColourScheme(new PurinePyrimidineColourScheme());
1186 public void userColour_actionPerformed(ActionEvent actionEvent)
1188 userColour.setSelected(true);
1189 new UserDefinedColours(this, null);
1193 public void backGround_actionPerformed(ActionEvent actionEvent)
1195 java.awt.Color col = JColorChooser
1196 .showDialog(this, MessageManager
1197 .getString("label.select_backgroud_colour"), null);
1200 jmb.setBackgroundColour(col);
1205 public void showHelp_actionPerformed(ActionEvent actionEvent)
1209 jalview.util.BrowserLauncher
1210 .openURL("https://www.cgl.ucsf.edu/chimera/docs/UsersGuide");
1211 } catch (Exception ex)
1216 public void updateTitleAndMenus()
1218 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
1223 setChainMenuItems(jmb.getChainNames());
1225 this.setTitle(jmb.getViewerTitle("Chimera", true));
1226 if (jmb.getPdbFile().length > 1 && jmb.getSequence().length > 1)
1228 viewerActionMenu.setVisible(true);
1230 if (!jmb.isLoadingFromArchive())
1232 seqColour_actionPerformed(null);
1240 * jalview.jbgui.GStructureViewer#alignStructs_actionPerformed(java.awt.event
1244 protected void alignStructs_actionPerformed(ActionEvent actionEvent)
1246 alignStructs_withAllAlignPanels();
1249 private void alignStructs_withAllAlignPanels()
1251 if (getAlignmentPanel() == null)
1256 if (_alignwith.size() == 0)
1258 _alignwith.add(getAlignmentPanel());
1263 AlignmentI[] als = new Alignment[_alignwith.size()];
1264 ColumnSelection[] alc = new ColumnSelection[_alignwith.size()];
1265 int[] alm = new int[_alignwith.size()];
1268 for (AlignmentPanel ap : _alignwith)
1270 als[a] = ap.av.getAlignment();
1272 alc[a++] = ap.av.getColumnSelection();
1274 jmb.superposeStructures(als, alm, alc);
1275 } catch (Exception e)
1277 StringBuffer sp = new StringBuffer();
1278 for (AlignmentPanel ap : _alignwith)
1280 sp.append("'" + ap.alignFrame.getTitle() + "' ");
1282 Cache.log.info("Couldn't align structures with the " + sp.toString()
1283 + "associated alignment panels.", e);
1287 public void setJalviewColourScheme(ColourSchemeI ucs)
1289 jmb.setJalviewColourScheme(ucs);
1296 * @return first alignment panel displaying given alignment, or the default
1299 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
1301 for (AlignmentPanel ap : getAllAlignmentPanels())
1303 if (ap.av.getAlignment() == alignment)
1308 return getAlignmentPanel();
1312 public AAStructureBindingModel getBinding()
1318 * Ask Chimera to save its session to the designated file path, or to a
1319 * temporary file if the path is null. Returns the file path if successful,
1325 protected String saveSession(String filepath)
1327 String pathUsed = filepath;
1330 if (pathUsed == null)
1332 File tempFile = File.createTempFile("chimera", ".py");
1333 tempFile.deleteOnExit();
1334 pathUsed = tempFile.getPath();
1336 boolean result = jmb.saveSession(pathUsed);
1339 this.chimeraSessionFile = pathUsed;
1342 } catch (IOException e)
1349 * Returns a string representing the state of the Chimera session. This is
1350 * done by requesting Chimera to save its session to a temporary file, then
1351 * reading the file contents. Returns an empty string on any error.
1354 public String getStateInfo()
1356 String sessionFile = saveSession(null);
1357 if (sessionFile == null)
1361 InputStream is = null;
1364 File f = new File(sessionFile);
1365 byte[] bytes = new byte[(int) f.length()];
1366 is = new FileInputStream(sessionFile);
1368 return new String(bytes);
1369 } catch (IOException e)
1379 } catch (IOException e)
1385 // return this.chimeraSessionFile == null ? "" : chimeraSessionFile;
1389 protected void fitToWindow_actionPerformed()
1395 public ViewerType getViewerType()
1397 return ViewerType.CHIMERA;