Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
TreePanel | 48 | 91 | 27 | ||
TreePanel.TreeLoader | 173 | 20 | 5 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | * | |
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. | |
20 | */ | |
21 | package jalview.appletgui; | |
22 | ||
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.datamodel.Alignment; | |
31 | import jalview.datamodel.HiddenColumns; | |
32 | import jalview.datamodel.SequenceI; | |
33 | import jalview.io.NewickFile; | |
34 | import jalview.util.MessageManager; | |
35 | ||
36 | import java.awt.BorderLayout; | |
37 | import java.awt.CheckboxMenuItem; | |
38 | import java.awt.Color; | |
39 | import java.awt.Menu; | |
40 | import java.awt.MenuBar; | |
41 | import java.awt.MenuItem; | |
42 | import java.awt.ScrollPane; | |
43 | import java.awt.event.ActionEvent; | |
44 | import java.awt.event.ActionListener; | |
45 | import java.awt.event.ItemEvent; | |
46 | import java.awt.event.ItemListener; | |
47 | ||
48 | public class TreePanel extends EmbmenuFrame | |
49 | implements ActionListener, ItemListener, AutoCloseable | |
50 | { | |
51 | SequenceI[] seq; | |
52 | ||
53 | String type; | |
54 | ||
55 | String pwtype; | |
56 | ||
57 | int start; | |
58 | ||
59 | int end; | |
60 | ||
61 | TreeCanvas treeCanvas; | |
62 | ||
63 | TreeModel tree; | |
64 | ||
65 | AlignmentPanel ap; | |
66 | ||
67 | AlignViewport av; | |
68 | ||
69 | 0 | public TreeModel getTree() |
70 | { | |
71 | 0 | return tree; |
72 | } | |
73 | ||
74 | 0 | @Override |
75 | public void close() | |
76 | { | |
77 | 0 | ap = null; |
78 | 0 | av = null; |
79 | 0 | super.close(); |
80 | } | |
81 | ||
82 | /** | |
83 | * Creates a new TreePanel object. | |
84 | */ | |
85 | 0 | public TreePanel(AlignmentPanel alignPanel, String type, String pwtype) |
86 | { | |
87 | 0 | try |
88 | { | |
89 | 0 | jbInit(); |
90 | 0 | this.setMenuBar(jMenuBar1); |
91 | } catch (Exception ex) | |
92 | { | |
93 | 0 | ex.printStackTrace(); |
94 | } | |
95 | ||
96 | 0 | initTreePanel(alignPanel, type, pwtype, null); |
97 | } | |
98 | ||
99 | /** | |
100 | * Creates a new TreePanel object. | |
101 | * | |
102 | */ | |
103 | 0 | public TreePanel(AlignmentPanel ap, String type, String pwtype, |
104 | NewickFile newtree) | |
105 | { | |
106 | 0 | try |
107 | { | |
108 | 0 | jbInit(); |
109 | 0 | this.setMenuBar(jMenuBar1); |
110 | } catch (Exception e) | |
111 | { | |
112 | 0 | e.printStackTrace(); |
113 | } | |
114 | ||
115 | 0 | initTreePanel(ap, type, pwtype, newtree); |
116 | } | |
117 | ||
118 | 0 | void initTreePanel(AlignmentPanel ap, String type, String pwtype, |
119 | NewickFile newTree) | |
120 | { | |
121 | ||
122 | 0 | this.ap = ap; |
123 | 0 | this.av = ap.av; |
124 | 0 | this.type = type; |
125 | 0 | this.pwtype = pwtype; |
126 | ||
127 | 0 | treeCanvas = new TreeCanvas(ap, scrollPane); |
128 | 0 | TreeLoader tl = new TreeLoader(newTree); |
129 | 0 | tl.start(); |
130 | 0 | embedMenuIfNeeded(treeCanvas); |
131 | 0 | scrollPane.add(treeCanvas, BorderLayout.CENTER); |
132 | } | |
133 | ||
134 | 0 | void showOriginalData() |
135 | { | |
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 | 0 | if (tree.getOriginalData() != null) |
143 | { | |
144 | 0 | char gc = '-'; |
145 | 0 | try |
146 | { | |
147 | // we try to get the associated view's gap character | |
148 | // but this may fail if the view was closed... | |
149 | 0 | gc = av.getGapCharacter(); |
150 | } catch (Exception ex) | |
151 | { | |
152 | } | |
153 | ||
154 | 0 | Object[] alAndColsel = tree.getOriginalData() |
155 | .getAlignmentAndHiddenColumns(gc); | |
156 | ||
157 | 0 | if (alAndColsel != null && alAndColsel[0] != null) |
158 | { | |
159 | 0 | Alignment al = new Alignment((SequenceI[]) alAndColsel[0]); |
160 | 0 | AlignFrame af = new AlignFrame(al, av.applet, |
161 | "Original Data for Tree", false); | |
162 | ||
163 | 0 | af.viewport.getAlignment() |
164 | .setHiddenColumns((HiddenColumns) alAndColsel[1]); | |
165 | } | |
166 | } | |
167 | else | |
168 | { | |
169 | 0 | jalview.bin.Console.outPrintln("Original Tree Data not available"); |
170 | } | |
171 | } | |
172 | ||
173 | class TreeLoader extends Thread | |
174 | { | |
175 | NewickFile newtree; | |
176 | ||
177 | jalview.datamodel.AlignmentView odata = null; | |
178 | ||
179 | 0 | public TreeLoader(NewickFile newtree) |
180 | { | |
181 | 0 | this.newtree = newtree; |
182 | } | |
183 | ||
184 | 0 | @Override |
185 | public void run() | |
186 | { | |
187 | 0 | if (newtree != null) |
188 | { | |
189 | 0 | tree = new TreeModel(av.getAlignment().getSequencesArray(), odata, |
190 | newtree,null); | |
191 | } | |
192 | else | |
193 | { | |
194 | 0 | ScoreModelI sm1 = ScoreModels.getInstance().getScoreModel(pwtype, |
195 | treeCanvas.ap); | |
196 | 0 | ScoreModelI sm = sm1; |
197 | 0 | TreeBuilder njtree = type.equals(TreeBuilder.NEIGHBOUR_JOINING) |
198 | ? new NJTree(av, sm, SimilarityParams.Jalview) | |
199 | : new AverageDistanceTree(av, sm, SimilarityParams.Jalview); | |
200 | 0 | tree = new TreeModel(njtree); |
201 | } | |
202 | ||
203 | 0 | tree.reCount(tree.getTopNode()); |
204 | 0 | tree.findHeight(tree.getTopNode()); |
205 | 0 | treeCanvas.setTree(tree); |
206 | 0 | if (newtree != null) |
207 | { | |
208 | // Set default view, paying lip service to any overriding tree view | |
209 | // parameter settings | |
210 | 0 | boolean showDist = newtree.HasDistances() |
211 | && av.applet.getDefaultParameter("showTreeDistances", | |
212 | newtree.HasDistances()); | |
213 | 0 | boolean showBoots = newtree.HasBootstrap() |
214 | && av.applet.getDefaultParameter("showTreeBootstraps", | |
215 | newtree.HasBootstrap()); | |
216 | 0 | distanceMenu.setState(showDist); |
217 | 0 | bootstrapMenu.setState(showBoots); |
218 | 0 | treeCanvas.setShowBootstrap(showBoots); |
219 | 0 | treeCanvas.setShowDistances(showDist); |
220 | 0 | treeCanvas.setMarkPlaceholders(av.applet |
221 | .getDefaultParameter("showUnlinkedTreeNodes", false)); | |
222 | } | |
223 | ||
224 | 0 | treeCanvas.repaint(); |
225 | ||
226 | 0 | av.setCurrentTree(tree); |
227 | ||
228 | } | |
229 | } | |
230 | ||
231 | 0 | @Override |
232 | public void actionPerformed(ActionEvent evt) | |
233 | { | |
234 | 0 | if (evt.getSource() == newickOutput) |
235 | { | |
236 | 0 | newickOutput_actionPerformed(); |
237 | } | |
238 | 0 | else if (evt.getSource() == fontSize) |
239 | { | |
240 | 0 | fontSize_actionPerformed(); |
241 | } | |
242 | 0 | else if (evt.getSource() == inputData) |
243 | { | |
244 | 0 | showOriginalData(); |
245 | } | |
246 | } | |
247 | ||
248 | 0 | @Override |
249 | public void itemStateChanged(ItemEvent evt) | |
250 | { | |
251 | 0 | if (evt.getSource() == fitToWindow) |
252 | { | |
253 | 0 | treeCanvas.fitToWindow = fitToWindow.getState(); |
254 | } | |
255 | ||
256 | 0 | else if (evt.getSource() == distanceMenu) |
257 | { | |
258 | 0 | treeCanvas.setShowDistances(distanceMenu.getState()); |
259 | } | |
260 | ||
261 | 0 | else if (evt.getSource() == bootstrapMenu) |
262 | { | |
263 | 0 | treeCanvas.setShowBootstrap(bootstrapMenu.getState()); |
264 | } | |
265 | ||
266 | 0 | else if (evt.getSource() == placeholdersMenu) |
267 | { | |
268 | 0 | treeCanvas.setMarkPlaceholders(placeholdersMenu.getState()); |
269 | } | |
270 | ||
271 | 0 | treeCanvas.repaint(); |
272 | } | |
273 | ||
274 | 0 | public void newickOutput_actionPerformed() |
275 | { | |
276 | 0 | jalview.io.NewickFile fout = new jalview.io.NewickFile( |
277 | tree.getTopNode()); | |
278 | 0 | String output = fout.print(false, true); |
279 | 0 | CutAndPasteTransfer cap = new CutAndPasteTransfer(false, null); |
280 | 0 | cap.setText(output); |
281 | 0 | java.awt.Frame frame = new java.awt.Frame(); |
282 | 0 | frame.add(cap); |
283 | 0 | jalview.bin.JalviewLite.addFrame(frame, type + " " + pwtype, 500, 100); |
284 | } | |
285 | ||
286 | 0 | public java.awt.Font getTreeFont() |
287 | { | |
288 | 0 | return treeCanvas.font; |
289 | } | |
290 | ||
291 | 0 | public void setTreeFont(java.awt.Font font) |
292 | { | |
293 | 0 | treeCanvas.font = font; |
294 | 0 | treeCanvas.repaint(); |
295 | } | |
296 | ||
297 | 0 | protected void fontSize_actionPerformed() |
298 | { | |
299 | 0 | if (treeCanvas == null) |
300 | { | |
301 | 0 | return; |
302 | } | |
303 | ||
304 | 0 | new FontChooser(this); |
305 | } | |
306 | ||
307 | BorderLayout borderLayout1 = new BorderLayout(); | |
308 | ||
309 | protected ScrollPane scrollPane = new ScrollPane(); | |
310 | ||
311 | MenuBar jMenuBar1 = new MenuBar(); | |
312 | ||
313 | Menu jMenu2 = new Menu(); | |
314 | ||
315 | protected MenuItem fontSize = new MenuItem(); | |
316 | ||
317 | protected CheckboxMenuItem bootstrapMenu = new CheckboxMenuItem(); | |
318 | ||
319 | protected CheckboxMenuItem distanceMenu = new CheckboxMenuItem(); | |
320 | ||
321 | protected CheckboxMenuItem placeholdersMenu = new CheckboxMenuItem(); | |
322 | ||
323 | protected CheckboxMenuItem fitToWindow = new CheckboxMenuItem(); | |
324 | ||
325 | Menu fileMenu = new Menu(); | |
326 | ||
327 | MenuItem newickOutput = new MenuItem(); | |
328 | ||
329 | MenuItem inputData = new MenuItem(); | |
330 | ||
331 | 0 | private void jbInit() throws Exception |
332 | { | |
333 | 0 | setLayout(borderLayout1); |
334 | 0 | this.setBackground(Color.white); |
335 | 0 | this.setFont(new java.awt.Font("Verdana", 0, 12)); |
336 | 0 | jMenu2.setLabel(MessageManager.getString("action.view")); |
337 | 0 | fontSize.setLabel(MessageManager.getString("action.font")); |
338 | 0 | fontSize.addActionListener(this); |
339 | 0 | bootstrapMenu.setLabel( |
340 | MessageManager.getString("label.show_bootstrap_values")); | |
341 | 0 | bootstrapMenu.addItemListener(this); |
342 | 0 | distanceMenu.setLabel(MessageManager.getString("label.show_distances")); |
343 | 0 | distanceMenu.addItemListener(this); |
344 | 0 | placeholdersMenu.setLabel( |
345 | MessageManager.getString("label.mark_unassociated_leaves")); | |
346 | 0 | placeholdersMenu.addItemListener(this); |
347 | 0 | fitToWindow.setState(true); |
348 | 0 | fitToWindow.setLabel(MessageManager.getString("label.fit_to_window")); |
349 | 0 | fitToWindow.addItemListener(this); |
350 | 0 | fileMenu.setLabel(MessageManager.getString("action.file")); |
351 | 0 | newickOutput.setLabel(MessageManager.getString("label.newick_format")); |
352 | 0 | newickOutput.addActionListener(this); |
353 | 0 | inputData.setLabel(MessageManager.getString("label.input_data")); |
354 | ||
355 | 0 | add(scrollPane, BorderLayout.CENTER); |
356 | 0 | jMenuBar1.add(fileMenu); |
357 | 0 | jMenuBar1.add(jMenu2); |
358 | 0 | jMenu2.add(fitToWindow); |
359 | 0 | jMenu2.add(fontSize); |
360 | 0 | jMenu2.add(distanceMenu); |
361 | 0 | jMenu2.add(bootstrapMenu); |
362 | 0 | jMenu2.add(placeholdersMenu); |
363 | 0 | fileMenu.add(newickOutput); |
364 | 0 | fileMenu.add(inputData); |
365 | 0 | inputData.addActionListener(this); |
366 | } | |
367 | ||
368 | } |