2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 import jalview.analysis.*;
23 import jalview.datamodel.*;
27 import jalview.jbgui.*;
29 import org.jibble.epsgraphics.*;
32 import java.awt.event.*;
33 import java.awt.image.*;
39 import javax.imageio.*;
41 import java.beans.PropertyChangeEvent;
50 public class TreePanel extends GTreePanel
54 TreeCanvas treeCanvas;
59 * Creates a new TreePanel object.
61 * @param av DOCUMENT ME!
62 * @param seqVector DOCUMENT ME!
63 * @param type DOCUMENT ME!
64 * @param pwtype DOCUMENT ME!
65 * @param s DOCUMENT ME!
66 * @param e DOCUMENT ME!
68 public TreePanel(AlignmentPanel ap, String type, String pwtype)
71 initTreePanel(ap, type, pwtype, null, null);
73 // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default
74 // showDistances(true);
78 * Creates a new TreePanel object.
80 * @param av DOCUMENT ME!
81 * @param seqVector DOCUMENT ME!
82 * @param newtree DOCUMENT ME!
83 * @param type DOCUMENT ME!
84 * @param pwtype DOCUMENT ME!
86 public TreePanel(AlignmentPanel ap,
92 initTreePanel(ap, type, pwtype, newtree, null);
95 public TreePanel(AlignmentPanel av,
98 NewickFile newtree, AlignmentView inputData) {
100 initTreePanel(av,type,pwtype,newtree,inputData);
103 public AlignmentI getAlignment()
105 return treeCanvas.av.getAlignment();
107 public AlignViewport getViewPort() {
108 return treeCanvas.av;
111 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
112 NewickFile newTree, AlignmentView inputData)
117 this.pwtype = pwtype;
119 treeCanvas = new TreeCanvas(ap, scrollPane);
120 scrollPane.setViewportView(treeCanvas);
122 av.addPropertyChangeListener(new java.beans.PropertyChangeListener()
124 public void propertyChange(PropertyChangeEvent evt)
126 if (evt.getPropertyName().equals("alignment"))
129 System.out.println("tree is null");
130 if(evt.getNewValue()==null)
131 System.out.println("new value is null");
133 tree.UpdatePlaceHolders( (Vector) evt.getNewValue());
143 TreeLoader tl = new TreeLoader(newTree);
144 if (inputData!=null) {
151 class TreeLoader extends Thread
154 jalview.datamodel.AlignmentView odata=null;
155 public TreeLoader(NewickFile newtree)
157 this.newtree = newtree;
160 // Must be outside run(), as Jalview2XML tries to
161 // update distance/bootstrap visibility at the same time
162 showBootstrap(newtree.HasBootstrap());
163 showDistances(newtree.HasDistances());
173 tree = new NJTree(av.alignment.getSequencesArray(),
176 tree = new NJTree(av.alignment.getSequencesArray(), odata, newtree);
178 if (!tree.hasOriginalSequenceData())
179 allowOriginalSeqData(false);
185 AlignmentView seqStrings = av.getAlignmentView(av.getSelectionGroup()!=null);
186 if(av.getSelectionGroup()==null)
189 end = av.alignment.getWidth();
190 seqs = av.alignment.getSequencesArray();
194 start = av.getSelectionGroup().getStartRes();
195 end = av.getSelectionGroup().getEndRes()+1;
196 seqs = av.getSelectionGroup().getSequencesInOrder(av.alignment);
199 tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
204 tree.reCount(tree.getTopNode());
205 tree.findHeight(tree.getTopNode());
206 treeCanvas.setTree(tree);
207 treeCanvas.repaint();
208 av.setCurrentTree(tree);
213 public void showDistances(boolean b)
215 treeCanvas.setShowDistances(b);
216 distanceMenu.setSelected(b);
219 public void showBootstrap(boolean b)
221 treeCanvas.setShowBootstrap(b);
222 bootstrapMenu.setSelected(b);
225 public void showPlaceholders(boolean b)
227 placeholdersMenu.setState(b);
228 treeCanvas.setMarkPlaceholders(b);
231 private void allowOriginalSeqData(boolean b) {
232 originalSeqData.setVisible(b);
240 * @return DOCUMENT ME!
242 public NJTree getTree()
251 * @param e DOCUMENT ME!
253 public void textbox_actionPerformed(ActionEvent e)
255 CutAndPasteTransfer cap = new CutAndPasteTransfer();
257 StringBuffer buffer = new StringBuffer();
259 if (type.equals("AV"))
261 buffer.append("Average distance tree using ");
265 buffer.append("Neighbour joining tree using ");
268 if (pwtype.equals("BL"))
270 buffer.append("BLOSUM62");
274 buffer.append("PID");
277 Desktop.addInternalFrame(cap, buffer.toString(), 500, 100);
279 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
280 cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance()));
286 * @param e DOCUMENT ME!
288 public void saveAsNewick_actionPerformed(ActionEvent e)
290 JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache.getProperty(
292 chooser.setFileView(new JalviewFileView());
293 chooser.setDialogTitle("Save tree as newick file");
294 chooser.setToolTipText("Save");
296 int value = chooser.showSaveDialog(null);
298 if (value == JalviewFileChooser.APPROVE_OPTION)
300 String choice = chooser.getSelectedFile().getPath();
301 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
302 chooser.getSelectedFile().getParent());
306 jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode());
307 String output = fout.print(tree.isHasBootstrap(), tree.isHasDistances(), tree.isHasRootDistance());
308 java.io.PrintWriter out = new java.io.PrintWriter(new java.io.FileWriter(
315 ex.printStackTrace();
323 * @param e DOCUMENT ME!
325 public void printMenu_actionPerformed(ActionEvent e)
327 //Putting in a thread avoids Swing painting problems
328 treeCanvas.startPrinting();
332 public void originalSeqData_actionPerformed(ActionEvent e)
334 if (!tree.hasOriginalSequenceData())
336 jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action.");
339 // decide if av alignment is sufficiently different to original data to warrant a new window to be created
340 // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs)
341 // or create a selection box around columns in alignment view
342 // test Alignment(SeqCigar[])
343 Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(av.
347 if (alAndColsel != null && alAndColsel[0]!=null)
349 // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]);
351 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
352 Alignment dataset = av.getAlignment().getDataset();
355 al.setDataset(dataset);
361 AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1],
362 AlignFrame.DEFAULT_WIDTH,
363 AlignFrame.DEFAULT_HEIGHT
366 //>>>This is a fix for the moment, until a better solution is found!!<<<
367 // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer());
369 // af.addSortByOrderMenuItem(ServiceName + " Ordering",
372 Desktop.addInternalFrame(af, "Original Data for " + this.title,
373 AlignFrame.DEFAULT_WIDTH,
374 AlignFrame.DEFAULT_HEIGHT);
383 * @param e DOCUMENT ME!
385 public void fitToWindow_actionPerformed(ActionEvent e)
387 treeCanvas.fitToWindow = fitToWindow.isSelected();
394 * @param e DOCUMENT ME!
396 public void font_actionPerformed(ActionEvent e)
398 if (treeCanvas == null)
403 new FontChooser(this);
406 public Font getTreeFont()
408 return treeCanvas.font;
411 public void setTreeFont(Font font)
414 treeCanvas.setFont(font);
420 * @param e DOCUMENT ME!
422 public void distanceMenu_actionPerformed(ActionEvent e)
424 treeCanvas.setShowDistances(distanceMenu.isSelected());
430 * @param e DOCUMENT ME!
432 public void bootstrapMenu_actionPerformed(ActionEvent e)
434 treeCanvas.setShowBootstrap(bootstrapMenu.isSelected());
440 * @param e DOCUMENT ME!
442 public void placeholdersMenu_actionPerformed(ActionEvent e)
444 treeCanvas.setMarkPlaceholders(placeholdersMenu.isSelected());
450 * @param e DOCUMENT ME!
452 public void epsTree_actionPerformed(ActionEvent e)
454 boolean accurateText = true;
456 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
459 // If we need to prompt, and if the GUI is visible then
460 // Prompt for EPS rendering style
461 if (renderStyle.equalsIgnoreCase("Prompt each time")
463 (System.getProperty("java.awt.headless") != null
464 && System.getProperty("java.awt.headless").equals("true")))
466 EPSOptions eps = new EPSOptions();
467 renderStyle = eps.getValue();
469 if (renderStyle==null || eps.cancelled)
475 if (renderStyle.equalsIgnoreCase("text"))
477 accurateText = false;
480 int width = treeCanvas.getWidth();
481 int height = treeCanvas.getHeight();
485 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
486 "LAST_DIRECTORY"), new String[] { "eps" },
487 new String[] { "Encapsulated Postscript" },
488 "Encapsulated Postscript");
489 chooser.setFileView(new jalview.io.JalviewFileView());
490 chooser.setDialogTitle("Create EPS file from tree");
491 chooser.setToolTipText("Save");
493 int value = chooser.showSaveDialog(this);
495 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
500 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
501 chooser.getSelectedFile().getParent());
503 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
504 EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width,
507 pg.setAccurateTextMode(accurateText);
509 treeCanvas.draw(pg, width, height);
516 ex.printStackTrace();
523 * @param e DOCUMENT ME!
525 public void pngTree_actionPerformed(ActionEvent e)
527 int width = treeCanvas.getWidth();
528 int height = treeCanvas.getHeight();
532 jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(
533 "LAST_DIRECTORY"), new String[] { "png" },
534 new String[] { "Portable network graphics" },
535 "Portable network graphics");
537 chooser.setFileView(new jalview.io.JalviewFileView());
538 chooser.setDialogTitle("Create PNG image from tree");
539 chooser.setToolTipText("Save");
541 int value = chooser.showSaveDialog(this);
543 if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)
548 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
549 chooser.getSelectedFile().getParent());
551 FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());
553 BufferedImage bi = new BufferedImage(width, height,
554 BufferedImage.TYPE_INT_RGB);
555 Graphics png = bi.getGraphics();
557 treeCanvas.draw(png, width, height);
559 ImageIO.write(bi, "png", out);
564 ex.printStackTrace();