2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 package jalview.appletgui;
25 import java.awt.event.*;
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.util.*;
32 public class TreeCanvas
33 extends Panel implements MouseListener
36 ScrollPane scrollPane;
38 public static final String PLACEHOLDER = " * ";
40 boolean fitToWindow = true;
41 boolean showDistances = false;
42 boolean showBootstrap = false;
43 boolean markPlaceholders = false;
53 //RubberbandRectangle rubberband;
57 Hashtable nameHash = new Hashtable();
58 Hashtable nodeHash = new Hashtable();
60 public TreeCanvas(AlignViewport av, ScrollPane scroller)
64 scrollPane = scroller;
65 addMouseListener(this);
68 PaintRefresher.Register(this, av.alignment);
71 public void TreeSelectionChanged(Sequence sequence)
73 SequenceGroup selected = av.getSelectionGroup();
76 selected = new SequenceGroup();
77 av.setSelectionGroup(selected);
80 selected.setEndRes(av.alignment.getWidth()-1);
81 selected.addOrRemove(sequence, true);
83 PaintRefresher.Refresh(this, av.alignment);
87 public void setTree(NJTree tree)
90 tree.findHeight(tree.getTopNode());
92 // Now have to calculate longest name based on the leaves
93 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
94 boolean has_placeholders = false;
97 for (int i = 0; i < leaves.size(); i++)
99 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
101 if (lf.isPlaceholder())
103 has_placeholders = true;
106 if (longestName.length() < ( (Sequence) lf.element()).getName()
109 longestName = TreeCanvas.PLACEHOLDER +
110 ( (Sequence) lf.element()).getName();
114 setMarkPlaceholders(has_placeholders);
117 public void drawNode(Graphics g, SequenceNode node, float chunk, float scale,
118 int width, int offx, int offy)
125 if (node.left() == null && node.right() == null)
129 float height = node.height;
130 float dist = node.dist;
132 int xstart = (int) ( (height - dist) * scale) + offx;
133 int xend = (int) (height * scale) + offx;
135 int ypos = (int) (node.ycount * chunk) + offy;
137 if (node.element() instanceof SequenceI)
139 if ( ( (SequenceI) ( (SequenceNode) node).element()).getColor() ==
142 g.setColor(Color.black);
146 g.setColor( ( (SequenceI) ( (SequenceNode) node).element()).getColor().
153 g.setColor(Color.black);
156 // Draw horizontal line
157 g.drawLine(xstart, ypos, xend, ypos);
159 String nodeLabel = "";
160 if (showDistances && node.dist > 0)
162 nodeLabel = new Format("%-.2f").form(node.dist);
168 nodeLabel = nodeLabel + " : ";
170 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
172 if (!nodeLabel.equals(""))
174 g.drawString(nodeLabel, xstart+2, ypos - 2);
177 String name = (markPlaceholders && node.isPlaceholder()) ?
178 (PLACEHOLDER + node.getName()) : node.getName();
179 FontMetrics fm = g.getFontMetrics(font);
180 int charWidth = fm.stringWidth(name) + 3;
181 int charHeight = fm.getHeight();
183 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
184 charWidth, charHeight);
186 nameHash.put( (SequenceI) node.element(), rect);
188 // Colour selected leaves differently
189 SequenceGroup selected = av.getSelectionGroup();
190 if (selected != null &&
191 selected.getSequences(false).contains( (SequenceI) node.element()))
193 g.setColor(Color.gray);
195 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
196 g.setColor(Color.white);
198 g.drawString(name, xend + 10, ypos);
199 g.setColor(Color.black);
203 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx, offy);
204 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx, offy);
206 float height = node.height;
207 float dist = node.dist;
209 int xstart = (int) ( (height - dist) * scale) + offx;
210 int xend = (int) (height * scale) + offx;
211 int ypos = (int) (node.ycount * chunk) + offy;
213 g.setColor( ( (SequenceNode) node).color.darker());
215 // Draw horizontal line
216 g.drawLine(xstart, ypos, xend, ypos);
217 g.fillRect(xend - 2, ypos - 2, 4, 4);
219 int ystart = (int) ( ( (SequenceNode) node.left()).ycount * chunk) + offy;
220 int yend = (int) ( ( (SequenceNode) node.right()).ycount * chunk) + offy;
222 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
223 nodeHash.put(node, pos);
225 g.drawLine( (int) (height * scale) + offx, ystart,
226 (int) (height * scale) + offx, yend);
228 if (showDistances && node.dist > 0)
230 g.drawString(new Format("%-.2f").form(node.dist), xstart+2, ypos - 2);
236 public Object findElement(int x, int y)
238 Enumeration keys = nameHash.keys();
240 while (keys.hasMoreElements())
242 Object ob = keys.nextElement();
243 Rectangle rect = (Rectangle) nameHash.get(ob);
245 if (x >= rect.x && x <= (rect.x + rect.width) &&
246 y >= rect.y && y <= (rect.y + rect.height))
251 keys = nodeHash.keys();
253 while (keys.hasMoreElements())
255 Object ob = keys.nextElement();
256 Rectangle rect = (Rectangle) nodeHash.get(ob);
258 if (x >= rect.x && x <= (rect.x + rect.width) &&
259 y >= rect.y && y <= (rect.y + rect.height))
268 public void pickNodes(Rectangle pickBox)
270 int width = getSize().width;
271 int height = getSize().height;
273 SequenceNode top = tree.getTopNode();
275 float wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight()
279 top.count = ( (SequenceNode) top.left()).count +
280 ( (SequenceNode) top.right()).count;
282 float chunk = (float) (height - offy) / top.count;
284 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
287 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
288 float scale, int width, int offx, int offy)
295 if (node.left() == null && node.right() == null)
297 float height = node.height;
298 //float dist = node.dist;
300 //int xstart = (int) ( (height - dist) * scale) + offx;
301 int xend = (int) (height * scale) + offx;
303 int ypos = (int) (node.ycount * chunk) + offy;
305 if (pickBox.contains(new Point(xend, ypos)))
307 if (node.element() instanceof SequenceI)
309 SequenceI seq = (SequenceI) node.element();
310 SequenceGroup sg = av.getSelectionGroup();
313 sg.addOrRemove(seq, true);
320 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width, offx,
322 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width, offx,
327 public void setColor(SequenceNode node, Color c)
334 if (node.left() == null && node.right() == null)
338 if (node.element() instanceof SequenceI)
340 ( (SequenceI) node.element()).setColor(c);
346 setColor( (SequenceNode) node.left(), c);
347 setColor( (SequenceNode) node.right(), c);
351 public void paint(Graphics g)
360 FontMetrics fm = g.getFontMetrics(font);
362 if (nameHash.size() == 0)
369 scrollPane.getSize().height > fm.getHeight() * nameHash.size() + offy))
371 draw(g, scrollPane.getSize().width, scrollPane.getSize().height);
375 setSize(new Dimension(scrollPane.getSize().width,
376 fm.getHeight() * nameHash.size()));
377 draw(g, scrollPane.getSize().width, fm.getHeight() * nameHash.size());
380 scrollPane.validate();
384 public void draw(Graphics g, int width, int height)
386 offy = font.getSize()+10;
388 g.setColor(Color.white);
389 g.fillRect(0, 0, width, height);
391 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar
393 float wscale = (float) (width - labelLength - offx * 2) / tree.getMaxHeight();
395 SequenceNode top = tree.getTopNode();
399 top.count = ( (SequenceNode) top.left()).count +
400 ( (SequenceNode) top.right()).count;
402 float chunk = (float) (height - offy) / top.count;
404 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
408 if (av.getCurrentTree() == tree)
410 g.setColor(Color.red);
414 g.setColor(Color.gray);
417 int x = (int) (threshold *
418 (float) (getSize().width - labelLength - 2 * offx) + offx);
420 g.drawLine(x, 0, x, getSize().height);
425 public void mouseReleased(MouseEvent e)
428 public void mouseEntered(MouseEvent e)
431 public void mouseExited(MouseEvent e)
434 public void mouseClicked(MouseEvent e)
438 public void mousePressed(MouseEvent e)
440 av.setCurrentTree(tree);
445 Object ob = findElement(x, y);
447 if (ob instanceof SequenceI)
449 TreeSelectionChanged( (Sequence) ob);
454 else if (ob instanceof SequenceNode)
456 SequenceNode tmpnode = (SequenceNode) ob;
457 tree.swapNodes(tmpnode);
458 tree.reCount(tree.getTopNode());
459 tree.findHeight(tree.getTopNode());
465 if (tree.getMaxHeight() != 0)
467 threshold = (float) (x - offx) /
468 (float) (getSize().width - labelLength - 2 * offx);
470 tree.getGroups().removeAllElements();
471 tree.groupNodes(tree.getTopNode(), threshold);
472 setColor(tree.getTopNode(), Color.black);
474 av.setSelectionGroup(null);
475 av.alignment.deleteAllGroups();
477 for (int i = 0; i < tree.getGroups().size(); i++)
480 Color col = new Color( (int) (Math.random() * 255),
481 (int) (Math.random() * 255),
482 (int) (Math.random() * 255));
483 setColor( (SequenceNode) tree.getGroups().elementAt(i), col.brighter());
485 Vector l = tree.findLeaves( (SequenceNode) tree.getGroups().elementAt(
488 Vector sequences = new Vector();
489 for (int j = 0; j < l.size(); j++)
491 SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
492 if(!sequences.contains(s1))
493 sequences.addElement(s1);
496 ColourSchemeI cs = null;
498 if (av.getGlobalColourScheme() != null)
500 if (av.getGlobalColourScheme() instanceof UserColourScheme)
502 cs = new UserColourScheme(
503 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
507 cs = ColourSchemeProperty.getColour(sequences,
508 av.alignment.getWidth(),
509 ColourSchemeProperty.getColourName(
510 av.getGlobalColourScheme()));
512 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
513 av.getIgnoreGapsConsensus());
516 SequenceGroup sg = new SequenceGroup(sequences, "TreeGroup",
518 false, 0, av.alignment.getWidth()-1);
521 if ( av.getGlobalColourScheme()!=null
522 && av.getGlobalColourScheme().conservationApplied())
524 Conservation c = new Conservation("Group",
525 ResidueProperties.propHash, 3,
526 sg.getSequences(false),
531 c.verdict(false, av.ConsPercGaps);
532 cs.setConservation(c);
538 av.alignment.addGroup(sg);
544 PaintRefresher.Refresh(this, av.alignment);
549 public void setShowDistances(boolean state)
551 this.showDistances = state;
555 public void setShowBootstrap(boolean state)
557 this.showBootstrap = state;
561 public void setMarkPlaceholders(boolean state)
563 this.markPlaceholders = state;