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.
21 package jalview.appletgui;
23 import jalview.analysis.AverageDistanceTree;
24 import jalview.analysis.NJTree;
25 import jalview.analysis.TreeBuilder;
26 import jalview.analysis.TreeModel;
27 import jalview.analysis.scoremodels.ScoreModels;
28 import jalview.analysis.scoremodels.SimilarityParams;
29 import jalview.api.analysis.ScoreModelI;
30 import jalview.api.analysis.ViewBasedAnalysisI;
31 import jalview.bin.Cache;
32 import jalview.datamodel.Alignment;
33 import jalview.datamodel.ColumnSelection;
34 import jalview.datamodel.SequenceI;
35 import jalview.io.NewickFile;
36 import jalview.util.MessageManager;
38 import java.awt.BorderLayout;
39 import java.awt.CheckboxMenuItem;
40 import java.awt.Color;
42 import java.awt.MenuBar;
43 import java.awt.MenuItem;
44 import java.awt.ScrollPane;
45 import java.awt.event.ActionEvent;
46 import java.awt.event.ActionListener;
47 import java.awt.event.ItemEvent;
48 import java.awt.event.ItemListener;
50 public class TreePanel extends EmbmenuFrame implements ActionListener,
63 TreeCanvas treeCanvas;
71 public TreeModel getTree()
77 public void finalize() throws Throwable
85 * Creates a new TreePanel object.
100 public TreePanel(AlignmentPanel ap, String type, String pwtype)
105 this.setMenuBar(jMenuBar1);
106 } catch (Exception ex)
108 ex.printStackTrace();
111 initTreePanel(ap, type, pwtype, null);
115 * Creates a new TreePanel object.
128 public TreePanel(AlignmentPanel ap, String type, String pwtype,
134 this.setMenuBar(jMenuBar1);
135 } catch (Exception e)
140 initTreePanel(ap, type, pwtype, newtree);
143 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
150 this.pwtype = pwtype;
152 treeCanvas = new TreeCanvas(ap, scrollPane);
153 TreeLoader tl = new TreeLoader(newTree);
155 embedMenuIfNeeded(treeCanvas);
156 scrollPane.add(treeCanvas, BorderLayout.CENTER);
159 void showOriginalData()
161 // decide if av alignment is sufficiently different to original data to
162 // warrant a new window to be created
163 // create new alignmnt window with hidden regions (unhiding hidden regions
164 // yields unaligned seqs)
165 // or create a selection box around columns in alignment view
166 // test Alignment(SeqCigar[])
167 if (tree.getOriginalData() != null)
172 // we try to get the associated view's gap character
173 // but this may fail if the view was closed...
174 gc = av.getGapCharacter();
175 } catch (Exception ex)
179 Object[] alAndColsel = tree.getOriginalData()
180 .getAlignmentAndColumnSelection(gc);
182 if (alAndColsel != null && alAndColsel[0] != null)
184 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
185 AlignFrame af = new AlignFrame(al, av.applet,
186 "Original Data for Tree", false);
188 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
193 System.out.println("Original Tree Data not available");
197 class TreeLoader extends Thread
201 jalview.datamodel.AlignmentView odata = null;
203 public TreeLoader(NewickFile newtree)
205 this.newtree = newtree;
213 tree = new TreeModel(av.getAlignment().getSequencesArray(), odata,
218 ScoreModelI sm = configureScoreModel(pwtype);
219 TreeBuilder njtree = type.equals(TreeBuilder.NEIGHBOUR_JOINING) ? new NJTree(
220 av, sm, SimilarityParams.Jalview)
221 : new AverageDistanceTree(av, sm, SimilarityParams.Jalview);
222 tree = new TreeModel(njtree);
225 tree.reCount(tree.getTopNode());
226 tree.findHeight(tree.getTopNode());
227 treeCanvas.setTree(tree);
230 // Set default view, paying lip service to any overriding tree view
231 // parameter settings
232 boolean showDist = newtree.HasDistances()
233 && av.applet.getDefaultParameter("showTreeDistances",
234 newtree.HasDistances());
235 boolean showBoots = newtree.HasBootstrap()
236 && av.applet.getDefaultParameter("showTreeBootstraps",
237 newtree.HasBootstrap());
238 distanceMenu.setState(showDist);
239 bootstrapMenu.setState(showBoots);
240 treeCanvas.setShowBootstrap(showBoots);
241 treeCanvas.setShowDistances(showDist);
242 treeCanvas.setMarkPlaceholders(av.applet.getDefaultParameter(
243 "showUnlinkedTreeNodes", false));
246 treeCanvas.repaint();
248 av.setCurrentTree(tree);
254 public void actionPerformed(ActionEvent evt)
256 if (evt.getSource() == newickOutput)
258 newickOutput_actionPerformed();
260 else if (evt.getSource() == fontSize)
262 fontSize_actionPerformed();
264 else if (evt.getSource() == inputData)
271 public void itemStateChanged(ItemEvent evt)
273 if (evt.getSource() == fitToWindow)
275 treeCanvas.fitToWindow = fitToWindow.getState();
278 else if (evt.getSource() == distanceMenu)
280 treeCanvas.setShowDistances(distanceMenu.getState());
283 else if (evt.getSource() == bootstrapMenu)
285 treeCanvas.setShowBootstrap(bootstrapMenu.getState());
288 else if (evt.getSource() == placeholdersMenu)
290 treeCanvas.setMarkPlaceholders(placeholdersMenu.getState());
293 treeCanvas.repaint();
296 public void newickOutput_actionPerformed()
298 jalview.io.NewickFile fout = new jalview.io.NewickFile(
300 String output = fout.print(false, true);
301 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
303 java.awt.Frame frame = new java.awt.Frame();
305 jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100);
308 public java.awt.Font getTreeFont()
310 return treeCanvas.font;
313 public void setTreeFont(java.awt.Font font)
315 treeCanvas.font = font;
316 treeCanvas.repaint();
319 protected void fontSize_actionPerformed()
321 if (treeCanvas == null)
326 new FontChooser(this);
329 BorderLayout borderLayout1 = new BorderLayout();
331 protected ScrollPane scrollPane = new ScrollPane();
333 MenuBar jMenuBar1 = new MenuBar();
335 Menu jMenu2 = new Menu();
337 protected MenuItem fontSize = new MenuItem();
339 protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem();
341 protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem();
343 protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem();
345 protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem();
347 Menu fileMenu = new Menu();
349 MenuItem newickOutput = new MenuItem();
351 MenuItem inputData = new MenuItem();
353 private void jbInit() throws Exception
355 setLayout(borderLayout1);
356 this.setBackground(Color.white);
357 this.setFont(new java.awt.Font("Verdana", 0, 12));
358 jMenu2.setLabel(MessageManager.getString("action.view"));
359 fontSize.setLabel(MessageManager.getString("action.font"));
360 fontSize.addActionListener(this);
361 bootstrapMenu.setLabel(MessageManager
362 .getString("label.show_bootstrap_values"));
363 bootstrapMenu.addItemListener(this);
364 distanceMenu.setLabel(MessageManager.getString("label.show_distances"));
365 distanceMenu.addItemListener(this);
366 placeholdersMenu.setLabel(MessageManager
367 .getString("label.mark_unassociated_leaves"));
368 placeholdersMenu.addItemListener(this);
369 fitToWindow.setState(true);
370 fitToWindow.setLabel(MessageManager.getString("label.fit_to_window"));
371 fitToWindow.addItemListener(this);
372 fileMenu.setLabel(MessageManager.getString("action.file"));
373 newickOutput.setLabel(MessageManager.getString("label.newick_format"));
374 newickOutput.addActionListener(this);
375 inputData.setLabel(MessageManager.getString("label.input_data"));
377 add(scrollPane, BorderLayout.CENTER);
378 jMenuBar1.add(fileMenu);
379 jMenuBar1.add(jMenu2);
380 jMenu2.add(fitToWindow);
381 jMenu2.add(fontSize);
382 jMenu2.add(distanceMenu);
383 jMenu2.add(bootstrapMenu);
384 jMenu2.add(placeholdersMenu);
385 fileMenu.add(newickOutput);
386 fileMenu.add(inputData);
387 inputData.addActionListener(this);
391 * Gets the score model for the given name. If the score model is one that
392 * requires to get state data from the current view, allow it to do so
397 protected ScoreModelI configureScoreModel(String modelName)
399 ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
400 if (sm instanceof ViewBasedAnalysisI)
404 sm = sm.getClass().newInstance();
405 ((ViewBasedAnalysisI) sm).configureFromAlignmentView(treeCanvas.ap);
406 } catch (Exception q)
408 Cache.log.error("Couldn't create a scoremodel instance for "