2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
21 import java.awt.event.*;
23 import jalview.analysis.*;
24 import jalview.datamodel.*;
27 public class TreePanel extends EmbmenuFrame implements ActionListener,
40 TreeCanvas treeCanvas;
48 public NJTree getTree()
53 public void finalize() throws Throwable
61 * Creates a new TreePanel object.
76 public TreePanel(AlignmentPanel ap, String type, String pwtype)
81 this.setMenuBar(jMenuBar1);
82 } catch (Exception ex)
87 initTreePanel(ap, type, pwtype, null);
91 * Creates a new TreePanel object.
104 public TreePanel(AlignmentPanel ap, String type, String pwtype,
110 this.setMenuBar(jMenuBar1);
111 } catch (Exception e)
116 initTreePanel(ap, type, pwtype, newtree);
119 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
126 this.pwtype = pwtype;
128 treeCanvas = new TreeCanvas(ap, scrollPane);
129 TreeLoader tl = new TreeLoader(newTree);
131 embedMenuIfNeeded(treeCanvas);
132 scrollPane.add(treeCanvas, BorderLayout.CENTER);
135 void showOriginalData()
137 // decide if av alignment is sufficiently different to original data to
138 // warrant a new window to be created
139 // create new alignmnt window with hidden regions (unhiding hidden regions
140 // yields unaligned seqs)
141 // or create a selection box around columns in alignment view
142 // test Alignment(SeqCigar[])
143 if (tree.seqData != null)
148 // we try to get the associated view's gap character
149 // but this may fail if the view was closed...
150 gc = av.getGapCharacter();
151 } catch (Exception ex)
155 Object[] alAndColsel = tree.seqData
156 .getAlignmentAndColumnSelection(gc);
158 if (alAndColsel != null && alAndColsel[0] != null)
160 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
161 AlignFrame af = new AlignFrame(al, av.applet,
162 "Original Data for Tree", false);
164 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
169 System.out.println("Original Tree Data not available");
173 class TreeLoader extends Thread
177 jalview.datamodel.AlignmentView odata = null;
179 public TreeLoader(NewickFile newtree)
181 this.newtree = newtree;
190 tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
194 tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
203 boolean selview = (av.getSelectionGroup() != null)
204 && (av.getSelectionGroup().getSize() > 1);
205 AlignmentView seqStrings = av.getAlignmentView(selview);
209 end = av.getAlignment().getWidth();
210 seqs = av.getAlignment().getSequencesArray();
214 start = av.getSelectionGroup().getStartRes();
215 end = av.getSelectionGroup().getEndRes() + 1;
216 seqs = av.getSelectionGroup().getSequencesInOrder(
220 tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
223 tree.reCount(tree.getTopNode());
224 tree.findHeight(tree.getTopNode());
225 treeCanvas.setTree(tree);
228 // Set default view, paying lip service to any overriding tree view
229 // parameter settings
230 boolean showDist = newtree.HasDistances()
231 && av.applet.getDefaultParameter("showTreeDistances",
232 newtree.HasDistances());
233 boolean showBoots = newtree.HasBootstrap()
234 && av.applet.getDefaultParameter("showTreeBootstraps",
235 newtree.HasBootstrap());
236 distanceMenu.setState(showDist);
237 bootstrapMenu.setState(showBoots);
238 treeCanvas.setShowBootstrap(showBoots);
239 treeCanvas.setShowDistances(showDist);
240 treeCanvas.setMarkPlaceholders(av.applet.getDefaultParameter(
241 "showUnlinkedTreeNodes", false));
244 treeCanvas.repaint();
246 av.setCurrentTree(tree);
251 public void actionPerformed(ActionEvent evt)
253 if (evt.getSource() == newickOutput)
255 newickOutput_actionPerformed();
257 else if (evt.getSource() == fontSize)
259 fontSize_actionPerformed();
261 else if (evt.getSource() == inputData)
267 public void itemStateChanged(ItemEvent evt)
269 if (evt.getSource() == fitToWindow)
271 treeCanvas.fitToWindow = fitToWindow.getState();
274 else if (evt.getSource() == distanceMenu)
276 treeCanvas.setShowDistances(distanceMenu.getState());
279 else if (evt.getSource() == bootstrapMenu)
281 treeCanvas.setShowBootstrap(bootstrapMenu.getState());
284 else if (evt.getSource() == placeholdersMenu)
286 treeCanvas.setMarkPlaceholders(placeholdersMenu.getState());
289 treeCanvas.repaint();
292 public void newickOutput_actionPerformed()
294 jalview.io.NewickFile fout = new jalview.io.NewickFile(
296 String output = fout.print(false, true);
297 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
299 java.awt.Frame frame = new java.awt.Frame();
301 jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100);
304 public java.awt.Font getTreeFont()
306 return treeCanvas.font;
309 public void setTreeFont(java.awt.Font font)
311 treeCanvas.font = font;
312 treeCanvas.repaint();
315 protected void fontSize_actionPerformed()
317 if (treeCanvas == null)
322 new FontChooser(this);
325 BorderLayout borderLayout1 = new BorderLayout();
327 protected ScrollPane scrollPane = new ScrollPane();
329 MenuBar jMenuBar1 = new MenuBar();
331 Menu jMenu2 = new Menu();
333 protected MenuItem fontSize = new MenuItem();
335 protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem();
337 protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem();
339 protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem();
341 protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem();
343 Menu fileMenu = new Menu();
345 MenuItem newickOutput = new MenuItem();
347 MenuItem inputData = new MenuItem();
349 private void jbInit() throws Exception
351 setLayout(borderLayout1);
352 this.setBackground(Color.white);
353 this.setFont(new java.awt.Font("Verdana", 0, 12));
354 jMenu2.setLabel("View");
355 fontSize.setLabel("Font...");
356 fontSize.addActionListener(this);
357 bootstrapMenu.setLabel("Show Bootstrap Values");
358 bootstrapMenu.addItemListener(this);
359 distanceMenu.setLabel("Show Distances");
360 distanceMenu.addItemListener(this);
361 placeholdersMenu.setLabel("Mark Unassociated Leaves");
362 placeholdersMenu.addItemListener(this);
363 fitToWindow.setState(true);
364 fitToWindow.setLabel("Fit To Window");
365 fitToWindow.addItemListener(this);
366 fileMenu.setLabel("File");
367 newickOutput.setLabel("Newick Format");
368 newickOutput.addActionListener(this);
369 inputData.setLabel("Input Data...");
371 add(scrollPane, BorderLayout.CENTER);
372 jMenuBar1.add(fileMenu);
373 jMenuBar1.add(jMenu2);
374 jMenu2.add(fitToWindow);
375 jMenu2.add(fontSize);
376 jMenu2.add(distanceMenu);
377 jMenu2.add(bootstrapMenu);
378 jMenu2.add(placeholdersMenu);
379 fileMenu.add(newickOutput);
380 fileMenu.add(inputData);
381 inputData.addActionListener(this);