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.NJTree;
24 import jalview.analysis.scoremodels.ScoreModels;
25 import jalview.analysis.scoremodels.SimilarityParams;
26 import jalview.api.analysis.ScoreModelI;
27 import jalview.api.analysis.ViewBasedAnalysisI;
28 import jalview.bin.Cache;
29 import jalview.datamodel.Alignment;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.SequenceI;
32 import jalview.io.NewickFile;
33 import jalview.util.MessageManager;
35 import java.awt.BorderLayout;
36 import java.awt.CheckboxMenuItem;
37 import java.awt.Color;
39 import java.awt.MenuBar;
40 import java.awt.MenuItem;
41 import java.awt.ScrollPane;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
47 public class TreePanel extends EmbmenuFrame implements ActionListener,
60 TreeCanvas treeCanvas;
68 public NJTree getTree()
74 public void finalize() throws Throwable
82 * Creates a new TreePanel object.
97 public TreePanel(AlignmentPanel ap, String type, String pwtype)
102 this.setMenuBar(jMenuBar1);
103 } catch (Exception ex)
105 ex.printStackTrace();
108 initTreePanel(ap, type, pwtype, null);
112 * Creates a new TreePanel object.
125 public TreePanel(AlignmentPanel ap, String type, String pwtype,
131 this.setMenuBar(jMenuBar1);
132 } catch (Exception e)
137 initTreePanel(ap, type, pwtype, newtree);
140 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
147 this.pwtype = pwtype;
149 treeCanvas = new TreeCanvas(ap, scrollPane);
150 TreeLoader tl = new TreeLoader(newTree);
152 embedMenuIfNeeded(treeCanvas);
153 scrollPane.add(treeCanvas, BorderLayout.CENTER);
156 void showOriginalData()
158 // decide if av alignment is sufficiently different to original data to
159 // warrant a new window to be created
160 // create new alignmnt window with hidden regions (unhiding hidden regions
161 // yields unaligned seqs)
162 // or create a selection box around columns in alignment view
163 // test Alignment(SeqCigar[])
164 if (tree.seqData != null)
169 // we try to get the associated view's gap character
170 // but this may fail if the view was closed...
171 gc = av.getGapCharacter();
172 } catch (Exception ex)
176 Object[] alAndColsel = tree.seqData
177 .getAlignmentAndColumnSelection(gc);
179 if (alAndColsel != null && alAndColsel[0] != null)
181 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
182 AlignFrame af = new AlignFrame(al, av.applet,
183 "Original Data for Tree", false);
185 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
190 System.out.println("Original Tree Data not available");
194 class TreeLoader extends Thread
198 jalview.datamodel.AlignmentView odata = null;
200 public TreeLoader(NewickFile newtree)
202 this.newtree = newtree;
212 tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
216 tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
223 ScoreModelI sm = configureScoreModel(pwtype);
224 tree = new NJTree(av, type, sm, SimilarityParams.Jalview);
227 tree.reCount(tree.getTopNode());
228 tree.findHeight(tree.getTopNode());
229 treeCanvas.setTree(tree);
232 // Set default view, paying lip service to any overriding tree view
233 // parameter settings
234 boolean showDist = newtree.HasDistances()
235 && av.applet.getDefaultParameter("showTreeDistances",
236 newtree.HasDistances());
237 boolean showBoots = newtree.HasBootstrap()
238 && av.applet.getDefaultParameter("showTreeBootstraps",
239 newtree.HasBootstrap());
240 distanceMenu.setState(showDist);
241 bootstrapMenu.setState(showBoots);
242 treeCanvas.setShowBootstrap(showBoots);
243 treeCanvas.setShowDistances(showDist);
244 treeCanvas.setMarkPlaceholders(av.applet.getDefaultParameter(
245 "showUnlinkedTreeNodes", false));
248 treeCanvas.repaint();
250 av.setCurrentTree(tree);
256 public void actionPerformed(ActionEvent evt)
258 if (evt.getSource() == newickOutput)
260 newickOutput_actionPerformed();
262 else if (evt.getSource() == fontSize)
264 fontSize_actionPerformed();
266 else if (evt.getSource() == inputData)
273 public void itemStateChanged(ItemEvent evt)
275 if (evt.getSource() == fitToWindow)
277 treeCanvas.fitToWindow = fitToWindow.getState();
280 else if (evt.getSource() == distanceMenu)
282 treeCanvas.setShowDistances(distanceMenu.getState());
285 else if (evt.getSource() == bootstrapMenu)
287 treeCanvas.setShowBootstrap(bootstrapMenu.getState());
290 else if (evt.getSource() == placeholdersMenu)
292 treeCanvas.setMarkPlaceholders(placeholdersMenu.getState());
295 treeCanvas.repaint();
298 public void newickOutput_actionPerformed()
300 jalview.io.NewickFile fout = new jalview.io.NewickFile(
302 String output = fout.print(false, true);
303 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
305 java.awt.Frame frame = new java.awt.Frame();
307 jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100);
310 public java.awt.Font getTreeFont()
312 return treeCanvas.font;
315 public void setTreeFont(java.awt.Font font)
317 treeCanvas.font = font;
318 treeCanvas.repaint();
321 protected void fontSize_actionPerformed()
323 if (treeCanvas == null)
328 new FontChooser(this);
331 BorderLayout borderLayout1 = new BorderLayout();
333 protected ScrollPane scrollPane = new ScrollPane();
335 MenuBar jMenuBar1 = new MenuBar();
337 Menu jMenu2 = new Menu();
339 protected MenuItem fontSize = new MenuItem();
341 protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem();
343 protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem();
345 protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem();
347 protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem();
349 Menu fileMenu = new Menu();
351 MenuItem newickOutput = new MenuItem();
353 MenuItem inputData = new MenuItem();
355 private void jbInit() throws Exception
357 setLayout(borderLayout1);
358 this.setBackground(Color.white);
359 this.setFont(new java.awt.Font("Verdana", 0, 12));
360 jMenu2.setLabel(MessageManager.getString("action.view"));
361 fontSize.setLabel(MessageManager.getString("action.font"));
362 fontSize.addActionListener(this);
363 bootstrapMenu.setLabel(MessageManager
364 .getString("label.show_bootstrap_values"));
365 bootstrapMenu.addItemListener(this);
366 distanceMenu.setLabel(MessageManager.getString("label.show_distances"));
367 distanceMenu.addItemListener(this);
368 placeholdersMenu.setLabel(MessageManager
369 .getString("label.mark_unassociated_leaves"));
370 placeholdersMenu.addItemListener(this);
371 fitToWindow.setState(true);
372 fitToWindow.setLabel(MessageManager.getString("label.fit_to_window"));
373 fitToWindow.addItemListener(this);
374 fileMenu.setLabel(MessageManager.getString("action.file"));
375 newickOutput.setLabel(MessageManager.getString("label.newick_format"));
376 newickOutput.addActionListener(this);
377 inputData.setLabel(MessageManager.getString("label.input_data"));
379 add(scrollPane, BorderLayout.CENTER);
380 jMenuBar1.add(fileMenu);
381 jMenuBar1.add(jMenu2);
382 jMenu2.add(fitToWindow);
383 jMenu2.add(fontSize);
384 jMenu2.add(distanceMenu);
385 jMenu2.add(bootstrapMenu);
386 jMenu2.add(placeholdersMenu);
387 fileMenu.add(newickOutput);
388 fileMenu.add(inputData);
389 inputData.addActionListener(this);
393 * Gets the score model for the given name. If the score model is one that
394 * requires to get state data from the current view, allow it to do so
399 protected ScoreModelI configureScoreModel(String modelName)
401 ScoreModelI sm = ScoreModels.getInstance().forName(modelName);
402 if (sm instanceof ViewBasedAnalysisI)
406 sm = sm.getClass().newInstance();
407 ((ViewBasedAnalysisI) sm).configureFromAlignmentView(treeCanvas.ap);
408 } catch (Exception q)
410 Cache.log.error("Couldn't create a scoremodel instance for "