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.api.analysis.ScoreModelI;
25 import jalview.api.analysis.ViewBasedAnalysisI;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentView;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.NewickFile;
31 import jalview.schemes.ResidueProperties;
32 import jalview.util.MessageManager;
34 import java.awt.BorderLayout;
35 import java.awt.CheckboxMenuItem;
36 import java.awt.Color;
38 import java.awt.MenuBar;
39 import java.awt.MenuItem;
40 import java.awt.ScrollPane;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.awt.event.ItemEvent;
44 import java.awt.event.ItemListener;
46 public class TreePanel extends EmbmenuFrame implements ActionListener,
59 TreeCanvas treeCanvas;
67 public NJTree getTree()
72 public void finalize() throws Throwable
80 * Creates a new TreePanel object.
95 public TreePanel(AlignmentPanel ap, String type, String pwtype)
100 this.setMenuBar(jMenuBar1);
101 } catch (Exception ex)
103 ex.printStackTrace();
106 initTreePanel(ap, type, pwtype, null);
110 * Creates a new TreePanel object.
123 public TreePanel(AlignmentPanel ap, String type, String pwtype,
129 this.setMenuBar(jMenuBar1);
130 } catch (Exception e)
135 initTreePanel(ap, type, pwtype, newtree);
138 void initTreePanel(AlignmentPanel ap, String type, String pwtype,
145 this.pwtype = pwtype;
147 treeCanvas = new TreeCanvas(ap, scrollPane);
148 TreeLoader tl = new TreeLoader(newTree);
150 embedMenuIfNeeded(treeCanvas);
151 scrollPane.add(treeCanvas, BorderLayout.CENTER);
154 void showOriginalData()
156 // decide if av alignment is sufficiently different to original data to
157 // warrant a new window to be created
158 // create new alignmnt window with hidden regions (unhiding hidden regions
159 // yields unaligned seqs)
160 // or create a selection box around columns in alignment view
161 // test Alignment(SeqCigar[])
162 if (tree.seqData != null)
167 // we try to get the associated view's gap character
168 // but this may fail if the view was closed...
169 gc = av.getGapCharacter();
170 } catch (Exception ex)
174 Object[] alAndColsel = tree.seqData
175 .getAlignmentAndColumnSelection(gc);
177 if (alAndColsel != null && alAndColsel[0] != null)
179 Alignment al = new Alignment((SequenceI[]) alAndColsel[0]);
180 AlignFrame af = new AlignFrame(al, av.applet,
181 "Original Data for Tree", false);
183 af.viewport.setHiddenColumns((ColumnSelection) alAndColsel[1]);
188 System.out.println("Original Tree Data not available");
192 class TreeLoader extends Thread
196 jalview.datamodel.AlignmentView odata = null;
198 public TreeLoader(NewickFile newtree)
200 this.newtree = newtree;
209 tree = new NJTree(av.getAlignment().getSequencesArray(), newtree);
213 tree = new NJTree(av.getAlignment().getSequencesArray(), odata,
222 boolean selview = av.getSelectionGroup() != null
223 && av.getSelectionGroup().getSize() > 1;
224 AlignmentView seqStrings = av.getAlignmentView(selview);
228 end = av.getAlignment().getWidth();
229 seqs = av.getAlignment().getSequencesArray();
233 start = av.getSelectionGroup().getStartRes();
234 end = av.getSelectionGroup().getEndRes() + 1;
235 seqs = av.getSelectionGroup().getSequencesInOrder(
238 ScoreModelI sm = ResidueProperties.getScoreModel(pwtype);
239 if (sm instanceof ViewBasedAnalysisI)
243 sm = sm.getClass().newInstance();
244 ((ViewBasedAnalysisI) sm)
245 .configureFromAlignmentView(treeCanvas.ap);
246 } catch (Exception q)
248 System.err.println("Couldn't create a scoremodel instance for "
252 tree = new NJTree(seqs, seqStrings, type, pwtype, sm, start, end);
256 tree = new NJTree(seqs, seqStrings, type, pwtype, null, start,
261 tree.reCount(tree.getTopNode());
262 tree.findHeight(tree.getTopNode());
263 treeCanvas.setTree(tree);
266 // Set default view, paying lip service to any overriding tree view
267 // parameter settings
268 boolean showDist = newtree.HasDistances()
269 && av.applet.getDefaultParameter("showTreeDistances",
270 newtree.HasDistances());
271 boolean showBoots = newtree.HasBootstrap()
272 && av.applet.getDefaultParameter("showTreeBootstraps",
273 newtree.HasBootstrap());
274 distanceMenu.setState(showDist);
275 bootstrapMenu.setState(showBoots);
276 treeCanvas.setShowBootstrap(showBoots);
277 treeCanvas.setShowDistances(showDist);
278 treeCanvas.setMarkPlaceholders(av.applet.getDefaultParameter(
279 "showUnlinkedTreeNodes", false));
282 treeCanvas.repaint();
284 av.setCurrentTree(tree);
289 public void actionPerformed(ActionEvent evt)
291 if (evt.getSource() == newickOutput)
293 newickOutput_actionPerformed();
295 else if (evt.getSource() == fontSize)
297 fontSize_actionPerformed();
299 else if (evt.getSource() == inputData)
305 public void itemStateChanged(ItemEvent evt)
307 if (evt.getSource() == fitToWindow)
309 treeCanvas.fitToWindow = fitToWindow.getState();
312 else if (evt.getSource() == distanceMenu)
314 treeCanvas.setShowDistances(distanceMenu.getState());
317 else if (evt.getSource() == bootstrapMenu)
319 treeCanvas.setShowBootstrap(bootstrapMenu.getState());
322 else if (evt.getSource() == placeholdersMenu)
324 treeCanvas.setMarkPlaceholders(placeholdersMenu.getState());
327 treeCanvas.repaint();
330 public void newickOutput_actionPerformed()
332 jalview.io.NewickFile fout = new jalview.io.NewickFile(
334 String output = fout.print(false, true);
335 CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null);
337 java.awt.Frame frame = new java.awt.Frame();
339 jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100);
342 public java.awt.Font getTreeFont()
344 return treeCanvas.font;
347 public void setTreeFont(java.awt.Font font)
349 treeCanvas.font = font;
350 treeCanvas.repaint();
353 protected void fontSize_actionPerformed()
355 if (treeCanvas == null)
360 new FontChooser(this);
363 BorderLayout borderLayout1 = new BorderLayout();
365 protected ScrollPane scrollPane = new ScrollPane();
367 MenuBar jMenuBar1 = new MenuBar();
369 Menu jMenu2 = new Menu();
371 protected MenuItem fontSize = new MenuItem();
373 protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem();
375 protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem();
377 protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem();
379 protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem();
381 Menu fileMenu = new Menu();
383 MenuItem newickOutput = new MenuItem();
385 MenuItem inputData = new MenuItem();
387 private void jbInit() throws Exception
389 setLayout(borderLayout1);
390 this.setBackground(Color.white);
391 this.setFont(new java.awt.Font("Verdana", 0, 12));
392 jMenu2.setLabel(MessageManager.getString("action.view"));
393 fontSize.setLabel(MessageManager.getString("action.font"));
394 fontSize.addActionListener(this);
395 bootstrapMenu.setLabel(MessageManager
396 .getString("label.show_bootstrap_values"));
397 bootstrapMenu.addItemListener(this);
398 distanceMenu.setLabel(MessageManager.getString("label.show_distances"));
399 distanceMenu.addItemListener(this);
400 placeholdersMenu.setLabel(MessageManager
401 .getString("label.mark_unassociated_leaves"));
402 placeholdersMenu.addItemListener(this);
403 fitToWindow.setState(true);
404 fitToWindow.setLabel(MessageManager.getString("label.fit_to_window"));
405 fitToWindow.addItemListener(this);
406 fileMenu.setLabel(MessageManager.getString("action.file"));
407 newickOutput.setLabel(MessageManager.getString("label.newick_format"));
408 newickOutput.addActionListener(this);
409 inputData.setLabel(MessageManager.getString("label.input_data"));
411 add(scrollPane, BorderLayout.CENTER);
412 jMenuBar1.add(fileMenu);
413 jMenuBar1.add(jMenu2);
414 jMenu2.add(fitToWindow);
415 jMenu2.add(fontSize);
416 jMenu2.add(distanceMenu);
417 jMenu2.add(bootstrapMenu);
418 jMenu2.add(placeholdersMenu);
419 fileMenu.add(newickOutput);
420 fileMenu.add(inputData);
421 inputData.addActionListener(this);