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 jalview.bin.Cache;
24 import jalview.datamodel.Alignment;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.ColumnSelection;
27 import jalview.datamodel.PDBEntry;
28 import jalview.datamodel.SequenceI;
29 import jalview.ext.rbvi.chimera.JalviewChimeraBinding;
30 import jalview.gui.StructureViewer.ViewerType;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.JalviewFileChooser;
33 import jalview.io.JalviewFileView;
34 import jalview.schemes.BuriedColourScheme;
35 import jalview.schemes.ColourSchemeI;
36 import jalview.schemes.HelixColourScheme;
37 import jalview.schemes.HydrophobicColourScheme;
38 import jalview.schemes.PurinePyrimidineColourScheme;
39 import jalview.schemes.StrandColourScheme;
40 import jalview.schemes.TaylorColourScheme;
41 import jalview.schemes.TurnColourScheme;
42 import jalview.schemes.ZappoColourScheme;
43 import jalview.structures.models.AAStructureBindingModel;
44 import jalview.util.BrowserLauncher;
45 import jalview.util.MessageManager;
46 import jalview.util.Platform;
47 import jalview.ws.dbsources.Pdb;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.ItemEvent;
52 import java.awt.event.ItemListener;
53 import java.io.BufferedReader;
55 import java.io.FileInputStream;
56 import java.io.FileOutputStream;
57 import java.io.FileReader;
58 import java.io.IOException;
59 import java.io.InputStream;
60 import java.io.PrintWriter;
61 import java.util.ArrayList;
62 import java.util.List;
64 import java.util.Random;
66 import java.util.Vector;
68 import javax.swing.JCheckBoxMenuItem;
69 import javax.swing.JColorChooser;
70 import javax.swing.JInternalFrame;
71 import javax.swing.JMenu;
72 import javax.swing.JMenuItem;
73 import javax.swing.JOptionPane;
74 import javax.swing.event.InternalFrameAdapter;
75 import javax.swing.event.InternalFrameEvent;
76 import javax.swing.event.MenuEvent;
77 import javax.swing.event.MenuListener;
80 * GUI elements for handling an external chimera display
85 public class ChimeraViewFrame extends StructureViewerBase
87 private JalviewChimeraBinding jmb;
89 private boolean allChainsSelected = false;
91 private boolean alignAddedStructures = false;
94 * state flag for PDB retrieval thread
96 private boolean _started = false;
98 private boolean addingStructures = false;
100 private IProgressIndicator progressBar = null;
103 * pdb retrieval thread.
105 private Thread worker = null;
108 * Path to Chimera session file. This is set when an open Jalview/Chimera
109 * session is saved, or on restore from a Jalview project (if it holds the
110 * filename of any saved Chimera sessions).
112 private String chimeraSessionFile = null;
114 private Random random = new Random();
117 * Initialise menu options.
119 private void initMenus()
121 viewerActionMenu.setText(MessageManager.getString("label.chimera"));
122 viewerColour.setText(MessageManager
123 .getString("label.colour_with_chimera"));
124 viewerColour.setToolTipText(MessageManager
125 .getString("label.let_chimera_manage_structure_colours"));
126 helpItem.setText(MessageManager.getString("label.chimera_help"));
127 seqColour.setSelected(jmb.isColourBySequence());
128 viewerColour.setSelected(!jmb.isColourBySequence());
129 if (_colourwith == null)
131 _colourwith = new Vector<AlignmentPanel>();
133 if (_alignwith == null)
135 _alignwith = new Vector<AlignmentPanel>();
138 // save As not yet implemented
139 savemenu.setVisible(false);
141 ViewSelectionMenu seqColourBy = new ViewSelectionMenu(
142 MessageManager.getString("label.colour_by"), this, _colourwith,
146 public void itemStateChanged(ItemEvent e)
148 if (!seqColour.isSelected())
154 // update the Chimera display now.
155 seqColour_actionPerformed(null);
159 viewMenu.add(seqColourBy);
160 viewMenu.add(fitToWindow);
162 final ItemListener handler;
163 JMenu alpanels = new ViewSelectionMenu(
164 MessageManager.getString("label.superpose_with"), this,
165 _alignwith, handler = new ItemListener()
168 public void itemStateChanged(ItemEvent e)
170 alignStructs.setEnabled(_alignwith.size() > 0);
171 alignStructs.setToolTipText(MessageManager
173 "label.align_structures_using_linked_alignment_views",
175 { new Integer(_alignwith.size()).toString() }));
178 handler.itemStateChanged(null);
179 viewerActionMenu.add(alpanels);
180 viewerActionMenu.addMenuListener(new MenuListener()
184 public void menuSelected(MenuEvent e)
186 handler.itemStateChanged(null);
190 public void menuDeselected(MenuEvent e)
192 // TODO Auto-generated method stub
196 public void menuCanceled(MenuEvent e)
198 // TODO Auto-generated method stub
204 * add a single PDB structure to a new or existing Chimera view
211 public ChimeraViewFrame(PDBEntry pdbentry, SequenceI[] seq,
212 String[] chains, final AlignmentPanel ap)
217 * is the pdb file already loaded?
219 String pdbId = pdbentry.getId();
220 String alreadyMapped = ap.getStructureSelectionManager()
221 .alreadyMappedToFile(pdbId);
223 if (alreadyMapped != null)
225 int option = chooseAddSequencesToViewer(pdbId);
226 if (option == JOptionPane.CANCEL_OPTION)
230 if (option == JOptionPane.YES_OPTION)
232 addSequenceMappingsToStructure(seq, chains, ap, alreadyMapped);
238 * Check if there are other Chimera views involving this alignment and give
239 * user the option to add and align this molecule to one of them
241 List<ChimeraViewFrame> existingViews = getChimeraWindowsFor(ap);
242 for (ChimeraViewFrame view : existingViews)
244 // TODO: highlight view somehow
246 * JAL-1742 exclude view with this structure already mapped (don't offer
247 * to align chain B to chain A of the same structure)
249 if (view.hasPdbId(pdbId))
253 int option = chooseAlignStructureToViewer(pdbId, view);
254 if (option == JOptionPane.CANCEL_OPTION)
258 if (option == JOptionPane.YES_OPTION)
260 view.useAlignmentPanelForSuperposition(ap);
261 view.addStructure(pdbentry, seq, chains, true, ap.alignFrame);
267 * If the options above are declined or do not apply, open a new viewer
269 openNewChimera(ap, new PDBEntry[]
270 { pdbentry }, new SequenceI[][]
275 * Presents a dialog with the option to add an align a structure to an
276 * existing Chimera view
280 * @return YES, NO or CANCEL JOptionPane code
282 protected int chooseAlignStructureToViewer(String pdbId,
283 ChimeraViewFrame view)
285 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
286 MessageManager.formatMessage("label.add_pdbentry_to_view",
288 { pdbId, view.getTitle() }), MessageManager
289 .getString("label.align_to_existing_structure_view"),
290 JOptionPane.YES_NO_CANCEL_OPTION);
295 * Presents a dialog with the option to add sequences to a viewer which
296 * already has their structure open
299 * @return YES, NO or CANCEL JOptionPane code
301 protected int chooseAddSequencesToViewer(String pdbId)
303 int option = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
304 MessageManager.formatMessage(
305 "label.pdb_entry_is_already_displayed", new Object[]
306 { pdbId }), MessageManager.formatMessage(
307 "label.map_sequences_to_visible_window", new Object[]
308 { pdbId }), JOptionPane.YES_NO_CANCEL_OPTION);
313 * Adds mappings for the given sequences to an already opened PDB structure,
314 * and updates any viewers that have the PDB file
321 protected void addSequenceMappingsToStructure(SequenceI[] seq,
322 String[] chains, final AlignmentPanel ap, String pdbFilename)
324 // TODO : Fix multiple seq to one chain issue here.
326 * create the mappings
328 ap.getStructureSelectionManager().setMapping(seq, chains, pdbFilename,
329 AppletFormatAdapter.FILE);
332 * alert the FeatureRenderer to show new (PDB RESNUM) features
334 if (ap.getSeqPanel().seqCanvas.fr != null)
336 ap.getSeqPanel().seqCanvas.fr.featuresAdded();
337 ap.paintAlignment(true);
341 * add the sequences to any other Chimera viewers for this pdb file
343 // JBPNOTE: this looks like a binding routine, rather than a gui routine
344 for (JInternalFrame frame : Desktop.instance.getAllFrames())
346 if (frame instanceof ChimeraViewFrame)
348 ChimeraViewFrame chimeraView = ((ChimeraViewFrame) frame);
349 for (int pe = 0; pe < chimeraView.jmb.getPdbCount(); pe++)
351 if (chimeraView.jmb.getPdbEntry(pe).getFile().equals(pdbFilename))
353 chimeraView.jmb.addSequence(pe, seq);
354 chimeraView.addAlignmentPanel(ap);
356 * add it to the set of alignments used for colouring structure by
359 chimeraView.useAlignmentPanelForColourbyseq(ap);
360 chimeraView.buildActionMenu();
361 ap.getStructureSelectionManager().sequenceColoursChanged(ap);
370 * Create a helper to manage progress bar display
372 protected void createProgressBar()
374 if (progressBar == null)
376 progressBar = new ProgressBar(statusPanel, statusBar);
380 protected boolean hasPdbId(String pdbId)
382 return jmb.hasPdbId(pdbId);
385 private void openNewChimera(AlignmentPanel ap, PDBEntry[] pdbentrys,
389 String[][] chains = extractChains(seqs);
390 jmb = new JalviewChimeraBindingModel(this,
391 ap.getStructureSelectionManager(), pdbentrys, seqs, chains,
393 addAlignmentPanel(ap);
394 useAlignmentPanelForColourbyseq(ap);
395 if (pdbentrys.length > 1)
397 alignAddedStructures = true;
398 useAlignmentPanelForSuperposition(ap);
400 jmb.setColourBySequence(true);
401 setSize(400, 400); // probably should be a configurable/dynamic default here
404 addingStructures = false;
405 worker = new Thread(this);
408 this.addInternalFrameListener(new InternalFrameAdapter()
410 public void internalFrameClosing(InternalFrameEvent internalFrameEvent)
419 * Retrieve chains for sequences by inspecting their PDB refs. The hope is
420 * that the first will be to the sequence's own chain. Really need a more
421 * managed way of doing this.
426 protected String[][] extractChains(SequenceI[][] seqs)
428 String[][] chains = new String[seqs.length][];
429 for (int i = 0; i < seqs.length; i++)
431 chains[i] = new String[seqs[i].length];
433 for (SequenceI seq : seqs[i])
436 if (seq.getDatasetSequence() != null)
438 Vector<PDBEntry> pdbrefs = seq.getDatasetSequence().getPDBId();
439 if (pdbrefs != null && pdbrefs.size() > 0)
441 chain = pdbrefs.get(0).getChainCode();
444 chains[i][seqno++] = chain;
451 * Create a new viewer from saved session state data including Chimera session
454 * @param chimeraSessionFile
458 * @param colourByChimera
459 * @param colourBySequence
462 public ChimeraViewFrame(String chimeraSessionFile,
463 AlignmentPanel alignPanel,
465 SequenceI[][] seqsArray, boolean colourByChimera,
466 boolean colourBySequence, String newViewId)
469 setViewId(newViewId);
470 this.chimeraSessionFile = chimeraSessionFile;
471 openNewChimera(alignPanel, pdbArray, seqsArray);
474 jmb.setColourBySequence(false);
475 seqColour.setSelected(false);
476 viewerColour.setSelected(true);
478 else if (colourBySequence)
480 jmb.setColourBySequence(true);
481 seqColour.setSelected(true);
482 viewerColour.setSelected(false);
487 * create a new viewer containing several structures superimposed using the
494 public ChimeraViewFrame(PDBEntry[] pe, SequenceI[][] seqs,
498 openNewChimera(ap, pe, seqs);
501 public ChimeraViewFrame(Map<PDBEntry, List<SequenceI>> toView,
502 AlignmentPanel alignPanel)
507 * Convert the map of sequences per pdb entry into the tied arrays expected
510 * TODO pass the Map down to openNewChimera and its callees instead
512 final Set<PDBEntry> pdbEntries = toView.keySet();
513 PDBEntry[] pdbs = pdbEntries.toArray(new PDBEntry[pdbEntries.size()]);
514 SequenceI[][] seqsForPdbs = new SequenceI[pdbEntries.size()][];
515 for (int i = 0; i < pdbs.length; i++)
517 final List<SequenceI> seqsForPdb = toView.get(pdbs[i]);
518 seqsForPdbs[i] = seqsForPdb.toArray(new SequenceI[seqsForPdb.size()]);
521 openNewChimera(alignPanel, pdbs, seqsForPdbs);
525 * add a new structure (with associated sequences and chains) to this viewer,
526 * retrieving it if necessary first.
533 * if true, new structure(s) will be align using associated alignment
535 private void addStructure(final PDBEntry pdbentry, final SequenceI[] seq,
536 final String[] chains, final boolean b,
537 final IProgressIndicator alignFrame)
539 if (pdbentry.getFile() == null)
541 if (worker != null && worker.isAlive())
543 // a retrieval is in progress, wait around and add ourselves to the
545 new Thread(new Runnable()
549 while (worker != null && worker.isAlive() && _started)
553 Thread.sleep(100 + ((int) Math.random() * 100));
555 } catch (Exception e)
560 // and call ourselves again.
561 addStructure(pdbentry, seq, chains, b, alignFrame);
567 // otherwise, start adding the structure.
568 jmb.addSequenceAndChain(new PDBEntry[]
569 { pdbentry }, new SequenceI[][]
570 { seq }, new String[][]
572 addingStructures = true;
574 alignAddedStructures = b;
575 // progressBar = alignFrame; // visual indication happens on caller frame.
576 (worker = new Thread(this)).start();
580 private List<ChimeraViewFrame> getChimeraWindowsFor(AlignmentPanel apanel)
582 List<ChimeraViewFrame> result = new ArrayList<ChimeraViewFrame>();
583 JInternalFrame[] frames = Desktop.instance.getAllFrames();
585 for (JInternalFrame frame : frames)
587 if (frame instanceof ChimeraViewFrame)
589 if (((StructureViewerBase) frame).isLinkedWith(apanel))
591 result.add((ChimeraViewFrame) frame);
599 * Launch Chimera. If we have a chimera session file name, send Chimera the
600 * command to open its saved session file.
604 jmb.setFinishedInit(false);
605 Desktop.addInternalFrame(this, jmb.getViewerTitle("Chimera", true),
606 getBounds().width, getBounds().height);
608 if (!jmb.launchChimera())
613 MessageManager.getString("label.chimera_failed"),
614 MessageManager.getString("label.error_loading_file"),
615 JOptionPane.ERROR_MESSAGE);
620 if (this.chimeraSessionFile != null)
622 boolean opened = jmb.openSession(chimeraSessionFile);
626 .println("An error occurred opening Chimera session file "
627 + chimeraSessionFile);
630 jmb.setFinishedInit(true);
632 jmb.startChimeraListener();
636 * If the list is not empty, add menu items for 'All' and each individual
637 * chain to the "View | Show Chain" sub-menu. Multiple selections are allowed.
641 void setChainMenuItems(List<String> chainNames)
643 chainMenu.removeAll();
644 if (chainNames == null || chainNames.isEmpty())
648 JMenuItem menuItem = new JMenuItem(
649 MessageManager.getString("label.all"));
650 menuItem.addActionListener(new ActionListener()
652 public void actionPerformed(ActionEvent evt)
654 allChainsSelected = true;
655 for (int i = 0; i < chainMenu.getItemCount(); i++)
657 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
659 ((JCheckBoxMenuItem) chainMenu.getItem(i)).setSelected(true);
662 showSelectedChains();
663 allChainsSelected = false;
667 chainMenu.add(menuItem);
669 for (String chainName : chainNames)
671 menuItem = new JCheckBoxMenuItem(chainName, true);
672 menuItem.addItemListener(new ItemListener()
674 public void itemStateChanged(ItemEvent evt)
676 if (!allChainsSelected)
678 showSelectedChains();
683 chainMenu.add(menuItem);
688 * Show only the selected chain(s) in the viewer
690 void showSelectedChains()
692 List<String> toshow = new ArrayList<String>();
693 for (int i = 0; i < chainMenu.getItemCount(); i++)
695 if (chainMenu.getItem(i) instanceof JCheckBoxMenuItem)
697 JCheckBoxMenuItem item = (JCheckBoxMenuItem) chainMenu.getItem(i);
698 if (item.isSelected())
700 toshow.add(item.getText());
704 jmb.showChains(toshow);
708 * Close down this instance of Jalview's Chimera viewer, giving the user the
709 * option to close the associated Chimera window (process). They may wish to
710 * keep it open until they have had an opportunity to save any work.
712 * @param closeChimera
713 * if true, close any linked Chimera process; if false, prompt first
715 public void closeViewer(boolean closeChimera)
717 if (jmb.isChimeraRunning())
721 String prompt = MessageManager.formatMessage(
722 "label.confirm_close_chimera", new Object[]
723 { jmb.getViewerTitle("Chimera", false) });
724 prompt = JvSwingUtils.wrapTooltip(true, prompt);
725 int confirm = JOptionPane.showConfirmDialog(this, prompt,
726 MessageManager.getString("label.close_viewer"),
727 JOptionPane.YES_NO_OPTION);
728 closeChimera = confirm == JOptionPane.YES_OPTION;
730 jmb.closeViewer(closeChimera);
732 setAlignmentPanel(null);
736 // TODO: check for memory leaks where instance isn't finalised because jmb
737 // holds a reference to the window
742 * Open any newly added PDB structures in Chimera, having first fetched data
743 * from PDB (if not already saved).
748 // todo - record which pdbids were successfully imported.
749 StringBuilder errormsgs = new StringBuilder(128);
750 StringBuilder files = new StringBuilder(128);
751 List<PDBEntry> filePDB = new ArrayList<PDBEntry>();
752 List<Integer> filePDBpos = new ArrayList<Integer>();
753 PDBEntry thePdbEntry = null;
756 String[] curfiles = jmb.getPdbFile(); // files currently in viewer
757 // TODO: replace with reference fetching/transfer code (validate PDBentry
759 for (int pi = 0; pi < jmb.getPdbCount(); pi++)
762 thePdbEntry = jmb.getPdbEntry(pi);
763 if (thePdbEntry.getFile() == null)
766 * Retrieve PDB data, save to file, attach to PDBEntry
768 file = fetchPdbFile(thePdbEntry);
771 errormsgs.append("'" + thePdbEntry.getId() + "' ");
777 * Got file already - ignore if already loaded in Chimera.
779 file = new File(thePdbEntry.getFile()).getAbsoluteFile()
781 if (curfiles != null && curfiles.length > 0)
783 addingStructures = true; // already files loaded.
784 for (int c = 0; c < curfiles.length; c++)
786 if (curfiles[c].equals(file))
796 filePDB.add(thePdbEntry);
797 filePDBpos.add(Integer.valueOf(pi));
798 files.append(" \"" + Platform.escapeString(file) + "\"");
801 } catch (OutOfMemoryError oomerror)
803 new OOMWarning("Retrieving PDB files: " + thePdbEntry.getId(),
805 } catch (Exception ex)
807 ex.printStackTrace();
808 errormsgs.append("When retrieving pdbfiles for '"
809 + thePdbEntry.getId() + "'");
811 if (errormsgs.length() > 0)
814 JOptionPane.showInternalMessageDialog(Desktop.desktop, MessageManager
815 .formatMessage("label.pdb_entries_couldnt_be_retrieved",
817 { errormsgs.toString() }), MessageManager
818 .getString("label.couldnt_load_file"),
819 JOptionPane.ERROR_MESSAGE);
822 if (files.length() > 0)
824 if (!addingStructures)
829 } catch (Exception ex)
831 Cache.log.error("Couldn't open Chimera viewer!", ex);
835 for (PDBEntry pe : filePDB)
838 if (pe.getFile() != null)
842 int pos = filePDBpos.get(num).intValue();
843 long startTime = startProgressBar("Chimera "
844 + MessageManager.getString("status.opening_file"));
846 jmb.addSequence(pos, jmb.getSequence()[pos]);
847 File fl = new File(pe.getFile());
848 String protocol = AppletFormatAdapter.URL;
853 protocol = AppletFormatAdapter.FILE;
855 } catch (Throwable e)
859 stopProgressBar("", startTime);
861 // Explicitly map to the filename used by Chimera ;
862 jmb.getSsm().setMapping(jmb.getSequence()[pos],
863 jmb.getChains()[pos],
866 } catch (OutOfMemoryError oomerror)
869 "When trying to open and map structures from Chimera!",
871 } catch (Exception ex)
873 Cache.log.error("Couldn't open " + pe.getFile()
874 + " in Chimera viewer!", ex);
877 Cache.log.debug("File locations are " + files);
881 jmb.setFinishedInit(true);
882 jmb.setLoadingFromArchive(false);
884 // refresh the sequence colours for the new structure(s)
885 for (AlignmentPanel ap : _colourwith)
887 jmb.updateColours(ap);
889 // do superposition if asked to
890 if (alignAddedStructures)
892 new Thread(new Runnable()
896 alignStructs_withAllAlignPanels();
899 alignAddedStructures = false;
901 addingStructures = false;
908 * Fetch PDB data and save to a local file. Returns the full path to the file,
909 * or null if fetch fails.
911 * @param processingEntry
915 private String fetchPdbFile(PDBEntry processingEntry) throws Exception
917 String filePath = null;
918 Pdb pdbclient = new Pdb();
919 AlignmentI pdbseq = null;
920 String pdbid = processingEntry.getId();
921 long handle = System.currentTimeMillis()
922 + Thread.currentThread().hashCode();
925 * Write 'fetching PDB' progress on AlignFrame as we are not yet visible
927 String msg = MessageManager.formatMessage("status.fetching_pdb",
930 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
931 // long hdl = startProgressBar(MessageManager.formatMessage(
932 // "status.fetching_pdb", new Object[]
936 pdbseq = pdbclient.getSequenceRecords(pdbid);
937 } catch (OutOfMemoryError oomerror)
939 new OOMWarning("Retrieving PDB id " + pdbid, oomerror);
943 + MessageManager.getString("label.state_completed");
944 getAlignmentPanel().alignFrame.setProgressBar(msg, handle);
945 // stopProgressBar(msg, hdl);
948 * If PDB data were saved and are not invalid (empty alignment), return the
951 if (pdbseq != null && pdbseq.getHeight() > 0)
953 // just use the file name from the first sequence's first PDBEntry
954 filePath = new File(pdbseq.getSequenceAt(0).getPDBId()
955 .elementAt(0).getFile()).getAbsolutePath();
956 processingEntry.setFile(filePath);
962 * Convenience method to update the progress bar if there is one. Be sure to
963 * call stopProgressBar with the returned handle to remove the message.
968 public long startProgressBar(String msg)
970 // TODO would rather have startProgress/stopProgress as the
971 // IProgressIndicator interface
972 long tm = random.nextLong();
973 if (progressBar != null)
975 progressBar.setProgressBar(msg, tm);
981 * End the progress bar with the specified handle, leaving a message (if not
982 * null) on the status bar
987 public void stopProgressBar(String msg, long handle)
989 if (progressBar != null)
991 progressBar.setProgressBar(msg, handle);
996 public void pdbFile_actionPerformed(ActionEvent actionEvent)
998 JalviewFileChooser chooser = new JalviewFileChooser(
999 Cache.getProperty("LAST_DIRECTORY"));
1001 chooser.setFileView(new JalviewFileView());
1002 chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
1003 chooser.setToolTipText(MessageManager.getString("action.save"));
1005 int value = chooser.showSaveDialog(this);
1007 if (value == JalviewFileChooser.APPROVE_OPTION)
1009 BufferedReader in = null;
1012 // TODO: cope with multiple PDB files in view
1013 in = new BufferedReader(new FileReader(jmb.getPdbFile()[0]));
1014 File outFile = chooser.getSelectedFile();
1016 PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
1018 while ((data = in.readLine()) != null)
1020 if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
1026 } catch (Exception ex)
1028 ex.printStackTrace();
1036 } catch (IOException e)
1038 e.printStackTrace();
1046 public void viewMapping_actionPerformed(ActionEvent actionEvent)
1048 CutAndPasteTransfer cap = new CutAndPasteTransfer();
1051 cap.appendText(jmb.printMappings());
1052 } catch (OutOfMemoryError e)
1055 "composing sequence-structure alignments for display in text box.",
1060 Desktop.addInternalFrame(cap,
1061 MessageManager.getString("label.pdb_sequence_mapping"), 550,
1066 public void eps_actionPerformed(ActionEvent e)
1070 .getString("error.eps_generation_not_implemented"));
1074 public void png_actionPerformed(ActionEvent e)
1078 .getString("error.png_generation_not_implemented"));
1082 public void viewerColour_actionPerformed(ActionEvent actionEvent)
1084 if (viewerColour.isSelected())
1086 // disable automatic sequence colouring.
1087 jmb.setColourBySequence(false);
1092 public void seqColour_actionPerformed(ActionEvent actionEvent)
1094 jmb.setColourBySequence(seqColour.isSelected());
1095 if (_colourwith == null)
1097 _colourwith = new Vector<AlignmentPanel>();
1099 if (jmb.isColourBySequence())
1101 if (!jmb.isLoadingFromArchive())
1103 if (_colourwith.size() == 0 && getAlignmentPanel() != null)
1105 // Make the currently displayed alignment panel the associated view
1106 _colourwith.add(getAlignmentPanel().alignFrame.alignPanel);
1109 // Set the colour using the current view for the associated alignframe
1110 for (AlignmentPanel ap : _colourwith)
1112 jmb.colourBySequence(ap.av.isShowSequenceFeatures(), ap);
1118 public void chainColour_actionPerformed(ActionEvent actionEvent)
1120 chainColour.setSelected(true);
1121 jmb.colourByChain();
1125 public void chargeColour_actionPerformed(ActionEvent actionEvent)
1127 chargeColour.setSelected(true);
1128 jmb.colourByCharge();
1132 public void zappoColour_actionPerformed(ActionEvent actionEvent)
1134 zappoColour.setSelected(true);
1135 jmb.setJalviewColourScheme(new ZappoColourScheme());
1139 public void taylorColour_actionPerformed(ActionEvent actionEvent)
1141 taylorColour.setSelected(true);
1142 jmb.setJalviewColourScheme(new TaylorColourScheme());
1146 public void hydroColour_actionPerformed(ActionEvent actionEvent)
1148 hydroColour.setSelected(true);
1149 jmb.setJalviewColourScheme(new HydrophobicColourScheme());
1153 public void helixColour_actionPerformed(ActionEvent actionEvent)
1155 helixColour.setSelected(true);
1156 jmb.setJalviewColourScheme(new HelixColourScheme());
1160 public void strandColour_actionPerformed(ActionEvent actionEvent)
1162 strandColour.setSelected(true);
1163 jmb.setJalviewColourScheme(new StrandColourScheme());
1167 public void turnColour_actionPerformed(ActionEvent actionEvent)
1169 turnColour.setSelected(true);
1170 jmb.setJalviewColourScheme(new TurnColourScheme());
1174 public void buriedColour_actionPerformed(ActionEvent actionEvent)
1176 buriedColour.setSelected(true);
1177 jmb.setJalviewColourScheme(new BuriedColourScheme());
1181 public void purinePyrimidineColour_actionPerformed(ActionEvent actionEvent)
1183 setJalviewColourScheme(new PurinePyrimidineColourScheme());
1187 public void userColour_actionPerformed(ActionEvent actionEvent)
1189 userColour.setSelected(true);
1190 new UserDefinedColours(this, null);
1194 public void backGround_actionPerformed(ActionEvent actionEvent)
1196 java.awt.Color col = JColorChooser
1197 .showDialog(this, MessageManager
1198 .getString("label.select_backgroud_colour"), null);
1201 jmb.setBackgroundColour(col);
1206 public void showHelp_actionPerformed(ActionEvent actionEvent)
1211 .openURL("https://www.cgl.ucsf.edu/chimera/docs/UsersGuide");
1212 } catch (Exception ex)
1217 public void updateTitleAndMenus()
1219 if (jmb.fileLoadingError != null && jmb.fileLoadingError.length() > 0)
1224 setChainMenuItems(jmb.getChainNames());
1226 this.setTitle(jmb.getViewerTitle("Chimera", true));
1227 if (jmb.getPdbFile().length > 1 && jmb.getSequence().length > 1)
1229 viewerActionMenu.setVisible(true);
1231 if (!jmb.isLoadingFromArchive())
1233 seqColour_actionPerformed(null);
1241 * jalview.jbgui.GStructureViewer#alignStructs_actionPerformed(java.awt.event
1245 protected void alignStructs_actionPerformed(ActionEvent actionEvent)
1247 alignStructs_withAllAlignPanels();
1250 private void alignStructs_withAllAlignPanels()
1252 if (getAlignmentPanel() == null)
1257 if (_alignwith.size() == 0)
1259 _alignwith.add(getAlignmentPanel());
1264 AlignmentI[] als = new Alignment[_alignwith.size()];
1265 ColumnSelection[] alc = new ColumnSelection[_alignwith.size()];
1266 int[] alm = new int[_alignwith.size()];
1269 for (AlignmentPanel ap : _alignwith)
1271 als[a] = ap.av.getAlignment();
1273 alc[a++] = ap.av.getColumnSelection();
1275 jmb.superposeStructures(als, alm, alc);
1276 } catch (Exception e)
1278 StringBuffer sp = new StringBuffer();
1279 for (AlignmentPanel ap : _alignwith)
1281 sp.append("'" + ap.alignFrame.getTitle() + "' ");
1283 Cache.log.info("Couldn't align structures with the " + sp.toString()
1284 + "associated alignment panels.", e);
1288 public void setJalviewColourScheme(ColourSchemeI ucs)
1290 jmb.setJalviewColourScheme(ucs);
1297 * @return first alignment panel displaying given alignment, or the default
1300 public AlignmentPanel getAlignmentPanelFor(AlignmentI alignment)
1302 for (AlignmentPanel ap : getAllAlignmentPanels())
1304 if (ap.av.getAlignment() == alignment)
1309 return getAlignmentPanel();
1313 public AAStructureBindingModel getBinding()
1319 * Ask Chimera to save its session to the designated file path, or to a
1320 * temporary file if the path is null. Returns the file path if successful,
1326 protected String saveSession(String filepath)
1328 String pathUsed = filepath;
1331 if (pathUsed == null)
1333 File tempFile = File.createTempFile("chimera", ".py");
1334 tempFile.deleteOnExit();
1335 pathUsed = tempFile.getPath();
1337 boolean result = jmb.saveSession(pathUsed);
1340 this.chimeraSessionFile = pathUsed;
1343 } catch (IOException e)
1350 * Returns a string representing the state of the Chimera session. This is
1351 * done by requesting Chimera to save its session to a temporary file, then
1352 * reading the file contents. Returns an empty string on any error.
1355 public String getStateInfo()
1357 String sessionFile = saveSession(null);
1358 if (sessionFile == null)
1362 InputStream is = null;
1365 File f = new File(sessionFile);
1366 byte[] bytes = new byte[(int) f.length()];
1367 is = new FileInputStream(sessionFile);
1369 return new String(bytes);
1370 } catch (IOException e)
1380 } catch (IOException e)
1389 protected void fitToWindow_actionPerformed()
1395 public ViewerType getViewerType()
1397 return ViewerType.CHIMERA;