2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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.*;
22 import java.util.Hashtable;
24 import jalview.analysis.*;
25 import jalview.datamodel.*;
28 public class TreePanel extends EmbmenuFrame implements ActionListener,
41 TreeCanvas treeCanvas;
48 public NJTree getTree()
53 public void finalize() throws Throwable {
60 * Creates a new TreePanel object.
75 public TreePanel(AlignmentPanel ap, String type, String pwtype)
80 this.setMenuBar(jMenuBar1);
81 } catch (Exception ex)
86 initTreePanel(ap, type, pwtype, null);
90 * Creates a new TreePanel object.
103 public TreePanel(AlignmentPanel ap, String type, String pwtype,
109 this.setMenuBar(jMenuBar1);
110 } catch (Exception e)
115 initTreePanel(ap, type, pwtype, newtree);
118 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
125 this.pwtype = pwtype;
127 treeCanvas = new TreeCanvas(ap, scrollPane);
128 TreeLoader tl = new TreeLoader(newTree);
130 embedMenuIfNeeded(treeCanvas);
131 scrollPane.add(treeCanvas, BorderLayout.CENTER);
134 void showOriginalData()
136 // decide if av alignment is sufficiently different to original data to
137 // warrant a new window to be created
138 // create new alignmnt window with hidden regions (unhiding hidden regions
139 // yields unaligned seqs)
140 // or create a selection box around columns in alignment view
141 // test Alignment(SeqCigar[])
142 if (tree.seqData != null)
147 // we try to get the associated view's gap character
148 // but this may fail if the view was closed...
149 gc = av.getGapCharacter();
150 } catch (Exception ex)
154 Object[] alAndColsel = tree.seqData
155 .getAlignmentAndColumnSelection(gc);
157 if (alAndColsel != null && alAndColsel[0] != null)
159 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
160 AlignFrame af = new AlignFrame(al, av.applet,
161 "Original Data for Tree", false);
163 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
168 System.out.println("Original Tree Data not available");
172 class TreeLoader extends Thread
176 jalview.datamodel.AlignmentView odata = null;
178 public TreeLoader(NewickFile newtree)
180 this.newtree = newtree;
189 tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
193 tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
202 AlignmentView seqStrings = av.getAlignmentView(av
203 .getSelectionGroup() != null);
204 if (av.getSelectionGroup() == null)
207 end = av.getAlignment().getWidth();
208 seqs = av.getAlignment().getSequencesArray();
212 start = av.getSelectionGroup().getStartRes();
213 end = av.getSelectionGroup().getEndRes() + 1;
214 seqs = av.getSelectionGroup().getSequencesInOrder(av.getAlignment());
217 tree = new NJTree(seqs, seqStrings, type, pwtype, start, end);
220 tree.reCount(tree.getTopNode());
221 tree.findHeight(tree.getTopNode());
222 treeCanvas.setTree(tree);
225 // Set default view, paying lip service to any overriding tree view
226 // parameter settings
227 boolean showDist = newtree.HasDistances()
228 && av.applet.getDefaultParameter("showTreeDistances",
229 newtree.HasDistances());
230 boolean showBoots = newtree.HasBootstrap()
231 && av.applet.getDefaultParameter("showTreeBootstraps",
232 newtree.HasBootstrap());
233 distanceMenu.setState(showDist);
234 bootstrapMenu.setState(showBoots);
235 treeCanvas.setShowBootstrap(showBoots);
236 treeCanvas.setShowDistances(showDist);
237 treeCanvas.setMarkPlaceholders(av.applet.getDefaultParameter(
238 "showUnlinkedTreeNodes", false));
241 treeCanvas.repaint();
243 av.setCurrentTree(tree);
248 public void actionPerformed(ActionEvent evt)
250 if (evt.getSource() == newickOutput)
252 newickOutput_actionPerformed();
254 else if (evt.getSource() == fontSize)
256 fontSize_actionPerformed();
258 else if (evt.getSource() == inputData)
264 public void itemStateChanged(ItemEvent evt)
266 if (evt.getSource() == fitToWindow)
268 treeCanvas.fitToWindow = fitToWindow.getState();
271 else if (evt.getSource() == distanceMenu)
273 treeCanvas.setShowDistances(distanceMenu.getState());
276 else if (evt.getSource() == bootstrapMenu)
278 treeCanvas.setShowBootstrap(bootstrapMenu.getState());
281 else if (evt.getSource() == placeholdersMenu)
283 treeCanvas.setMarkPlaceholders(placeholdersMenu.getState());
286 treeCanvas.repaint();
289 public void newickOutput_actionPerformed()
291 jalview.io.NewickFile fout = new jalview.io.NewickFile(
293 String output = fout.print(false, true);
294 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
296 java.awt.Frame frame = new java.awt.Frame();
298 jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100);
301 public java.awt.Font getTreeFont()
303 return treeCanvas.font;
306 public void setTreeFont(java.awt.Font font)
308 treeCanvas.font = font;
309 treeCanvas.repaint();
312 protected void fontSize_actionPerformed()
314 if (treeCanvas == null)
319 new FontChooser(this);
322 BorderLayout borderLayout1 = new BorderLayout();
324 protected ScrollPane scrollPane = new ScrollPane();
326 MenuBar jMenuBar1 = new MenuBar();
328 Menu jMenu2 = new Menu();
330 protected MenuItem fontSize = new MenuItem();
332 protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem();
334 protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem();
336 protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem();
338 protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem();
340 Menu fileMenu = new Menu();
342 MenuItem newickOutput = new MenuItem();
344 MenuItem inputData = new MenuItem();
346 private void jbInit() throws Exception
348 setLayout(borderLayout1);
349 this.setBackground(Color.white);
350 this.setFont(new java.awt.Font("Verdana", 0, 12));
351 jMenu2.setLabel("View");
352 fontSize.setLabel("Font...");
353 fontSize.addActionListener(this);
354 bootstrapMenu.setLabel("Show Bootstrap Values");
355 bootstrapMenu.addItemListener(this);
356 distanceMenu.setLabel("Show Distances");
357 distanceMenu.addItemListener(this);
358 placeholdersMenu.setLabel("Mark Unassociated Leaves");
359 placeholdersMenu.addItemListener(this);
360 fitToWindow.setState(true);
361 fitToWindow.setLabel("Fit To Window");
362 fitToWindow.addItemListener(this);
363 fileMenu.setLabel("File");
364 newickOutput.setLabel("Newick Format");
365 newickOutput.addActionListener(this);
366 inputData.setLabel("Input Data...");
368 add(scrollPane, BorderLayout.CENTER);
369 jMenuBar1.add(fileMenu);
370 jMenuBar1.add(jMenu2);
371 jMenu2.add(fitToWindow);
372 jMenu2.add(fontSize);
373 jMenu2.add(distanceMenu);
374 jMenu2.add(bootstrapMenu);
375 jMenu2.add(placeholdersMenu);
376 fileMenu.add(newickOutput);
377 fileMenu.add(inputData);
378 inputData.addActionListener(this);