2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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, MouseMotionListener
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 Hashtable nameHash = new Hashtable();
54 Hashtable nodeHash = new Hashtable();
56 SequenceNode highlightNode;
58 public TreeCanvas(AlignViewport av, ScrollPane scroller)
62 scrollPane = scroller;
63 addMouseListener(this);
64 addMouseMotionListener(this);
67 PaintRefresher.Register(this, av.getSequenceSetId());
70 public void treeSelectionChanged(SequenceI sequence)
72 SequenceGroup selected = av.getSelectionGroup();
75 selected = new SequenceGroup();
76 av.setSelectionGroup(selected);
79 selected.setEndRes(av.alignment.getWidth() - 1);
80 selected.addOrRemove(sequence, true);
83 public void setTree(NJTree tree)
86 tree.findHeight(tree.getTopNode());
88 // Now have to calculate longest name based on the leaves
89 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
90 boolean has_placeholders = false;
93 for (int i = 0; i < leaves.size(); i++)
95 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
97 if (lf.isPlaceholder())
99 has_placeholders = true;
102 if (longestName.length() < ( (Sequence) lf.element()).getName()
105 longestName = TreeCanvas.PLACEHOLDER +
106 ( (Sequence) lf.element()).getName();
110 setMarkPlaceholders(has_placeholders);
113 public void drawNode(Graphics g, SequenceNode node, float chunk, float scale,
114 int width, int offx, int offy)
121 if (node.left() == null && node.right() == null)
125 float height = node.height;
126 float dist = node.dist;
128 int xstart = (int) ( (height - dist) * scale) + offx;
129 int xend = (int) (height * scale) + offx;
131 int ypos = (int) (node.ycount * chunk) + offy;
133 if (node.element() instanceof SequenceI)
135 SequenceI seq = (SequenceI) ( (SequenceNode) node).element();
137 if (av.getSequenceColour(seq) == Color.white)
139 g.setColor(Color.black);
143 g.setColor(av.getSequenceColour(seq).darker());
149 g.setColor(Color.black);
152 // Draw horizontal line
153 g.drawLine(xstart, ypos, xend, ypos);
155 String nodeLabel = "";
156 if (showDistances && node.dist > 0)
158 nodeLabel = new Format("%-.2f").form(node.dist);
164 nodeLabel = nodeLabel + " : ";
166 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
168 if (!nodeLabel.equals(""))
170 g.drawString(nodeLabel, xstart + 2, ypos - 2);
173 String name = (markPlaceholders && node.isPlaceholder()) ?
174 (PLACEHOLDER + node.getName()) : node.getName();
175 FontMetrics fm = g.getFontMetrics(font);
176 int charWidth = fm.stringWidth(name) + 3;
177 int charHeight = fm.getHeight();
179 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
180 charWidth, charHeight);
182 nameHash.put( (SequenceI) node.element(), rect);
184 // Colour selected leaves differently
185 SequenceGroup selected = av.getSelectionGroup();
186 if (selected != null &&
187 selected.getSequences(null).contains( (SequenceI) node.element()))
189 g.setColor(Color.gray);
191 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
192 g.setColor(Color.white);
194 g.drawString(name, xend + 10, ypos);
195 g.setColor(Color.black);
199 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx, offy);
200 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx, offy);
202 float height = node.height;
203 float dist = node.dist;
205 int xstart = (int) ( (height - dist) * scale) + offx;
206 int xend = (int) (height * scale) + offx;
207 int ypos = (int) (node.ycount * chunk) + offy;
209 g.setColor( ( (SequenceNode) node).color.darker());
211 // Draw horizontal line
212 g.drawLine(xstart, ypos, xend, ypos);
213 if (node == highlightNode)
215 g.fillRect(xend - 3, ypos - 3, 6, 6);
219 g.fillRect(xend - 2, ypos - 2, 4, 4);
222 int ystart = (int) ( ( (SequenceNode) node.left()).ycount * chunk) + offy;
223 int yend = (int) ( ( (SequenceNode) node.right()).ycount * chunk) + offy;
225 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
226 nodeHash.put(node, pos);
228 g.drawLine( (int) (height * scale) + offx, ystart,
229 (int) (height * scale) + offx, yend);
231 String nodeLabel = "";
233 if (showDistances && (node.dist > 0))
235 nodeLabel = new Format("%-.2f").form(node.dist);
242 nodeLabel = nodeLabel + " : ";
245 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
248 if (!nodeLabel.equals(""))
250 g.drawString(nodeLabel, xstart + 2, ypos - 2);
256 public Object findElement(int x, int y)
258 Enumeration keys = nameHash.keys();
260 while (keys.hasMoreElements())
262 Object ob = keys.nextElement();
263 Rectangle rect = (Rectangle) nameHash.get(ob);
265 if (x >= rect.x && x <= (rect.x + rect.width) &&
266 y >= rect.y && y <= (rect.y + rect.height))
271 keys = nodeHash.keys();
273 while (keys.hasMoreElements())
275 Object ob = keys.nextElement();
276 Rectangle rect = (Rectangle) nodeHash.get(ob);
278 if (x >= rect.x && x <= (rect.x + rect.width) &&
279 y >= rect.y && y <= (rect.y + rect.height))
288 public void pickNodes(Rectangle pickBox)
290 int width = getSize().width;
291 int height = getSize().height;
293 SequenceNode top = tree.getTopNode();
295 float wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight()
299 top.count = ( (SequenceNode) top.left()).count +
300 ( (SequenceNode) top.right()).count;
302 float chunk = (float) (height - offy) / top.count;
304 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
307 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
308 float scale, int width, int offx, int offy)
315 if (node.left() == null && node.right() == null)
317 float height = node.height;
318 //float dist = node.dist;
320 //int xstart = (int) ( (height - dist) * scale) + offx;
321 int xend = (int) (height * scale) + offx;
323 int ypos = (int) (node.ycount * chunk) + offy;
325 if (pickBox.contains(new Point(xend, ypos)))
327 if (node.element() instanceof SequenceI)
329 SequenceI seq = (SequenceI) node.element();
330 SequenceGroup sg = av.getSelectionGroup();
333 sg.addOrRemove(seq, true);
340 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width, offx,
342 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width, offx,
347 public void setColor(SequenceNode node, Color c)
354 if (node.left() == null && node.right() == null)
358 if (node.element() instanceof SequenceI)
360 av.setSequenceColour( (SequenceI) node.element(), c);
366 setColor( (SequenceNode) node.left(), c);
367 setColor( (SequenceNode) node.right(), c);
371 public void update(Graphics g)
376 public void paint(Graphics g)
383 if (nameHash.size() == 0)
388 int width = scrollPane.getSize().width;
389 int height = scrollPane.getSize().height;
392 height = g.getFontMetrics(font).getHeight() * nameHash.size();
395 if (getSize().width > width)
397 setSize(new Dimension(width, height));
398 scrollPane.validate();
402 setSize(new Dimension(width, height));
406 draw(g, width, height);
410 public void draw(Graphics g, int width, int height)
412 offy = font.getSize() + 10;
414 g.setColor(Color.white);
415 g.fillRect(0, 0, width, height);
417 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; //20 allows for scrollbar
419 float wscale = (float) (width - labelLength - offx * 2) / tree.getMaxHeight();
421 SequenceNode top = tree.getTopNode();
425 top.count = ( (SequenceNode) top.left()).count +
426 ( (SequenceNode) top.right()).count;
428 float chunk = (float) (height - offy) / top.count;
430 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
434 if (av.getCurrentTree() == tree)
436 g.setColor(Color.red);
440 g.setColor(Color.gray);
443 int x = (int) (threshold *
444 (float) (getSize().width - labelLength - 2 * offx) + offx);
446 g.drawLine(x, 0, x, getSize().height);
451 public void mouseReleased(MouseEvent e)
454 public void mouseEntered(MouseEvent e)
457 public void mouseExited(MouseEvent e)
460 public void mouseClicked(MouseEvent evt)
462 if (highlightNode != null)
464 if (evt.getClickCount() > 1)
466 tree.swapNodes(highlightNode);
467 tree.reCount(tree.getTopNode());
468 tree.findHeight(tree.getTopNode());
472 Vector leaves = new Vector();
473 tree.findLeaves(highlightNode, leaves);
475 for (int i = 0; i < leaves.size(); i++)
478 (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element();
479 treeSelectionChanged(seq);
483 PaintRefresher.Refresh(this, av.getSequenceSetId());
488 public void mouseDragged(MouseEvent ect)
491 public void mouseMoved(MouseEvent evt)
493 av.setCurrentTree(tree);
495 Object ob = findElement(evt.getX(), evt.getY());
497 if (ob instanceof SequenceNode)
499 highlightNode = (SequenceNode) ob;
504 if (highlightNode != null)
506 highlightNode = null;
512 public void mousePressed(MouseEvent e)
514 av.setCurrentTree(tree);
519 Object ob = findElement(x, y);
521 if (ob instanceof SequenceI)
523 treeSelectionChanged( (Sequence) ob);
524 PaintRefresher.Refresh(this, av.getSequenceSetId());
528 else if (! (ob instanceof SequenceNode))
532 if (tree.getMaxHeight() != 0)
534 threshold = (float) (x - offx) /
535 (float) (getSize().width - labelLength - 2 * offx);
537 tree.getGroups().removeAllElements();
538 tree.groupNodes(tree.getTopNode(), threshold);
539 setColor(tree.getTopNode(), Color.black);
541 av.setSelectionGroup(null);
542 av.alignment.deleteAllGroups();
543 av.sequenceColours = null;
550 PaintRefresher.Refresh(this, av.getSequenceSetId());
557 for (int i = 0; i < tree.getGroups().size(); i++)
560 Color col = new Color( (int) (Math.random() * 255),
561 (int) (Math.random() * 255),
562 (int) (Math.random() * 255));
563 setColor( (SequenceNode) tree.getGroups().elementAt(i), col.brighter());
565 Vector l = tree.findLeaves( (SequenceNode) tree.getGroups().elementAt(
568 Vector sequences = new Vector();
569 for (int j = 0; j < l.size(); j++)
571 SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
572 if (!sequences.contains(s1))
574 sequences.addElement(s1);
578 ColourSchemeI cs = null;
580 if (av.getGlobalColourScheme() != null)
582 if (av.getGlobalColourScheme() instanceof UserColourScheme)
584 cs = new UserColourScheme(
585 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
590 cs = ColourSchemeProperty.getColour(sequences,
591 av.alignment.getWidth(),
592 ColourSchemeProperty.
594 av.getGlobalColourScheme()));
597 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
598 av.getIgnoreGapsConsensus());
601 SequenceGroup sg = new SequenceGroup(sequences, "",
604 av.alignment.getWidth() - 1);
606 sg.setName("JTreeGroup:" + sg.hashCode());
608 if (av.getGlobalColourScheme() != null
609 && av.getGlobalColourScheme().conservationApplied())
611 Conservation c = new Conservation("Group",
612 ResidueProperties.propHash, 3,
613 sg.getSequences(null),
618 c.verdict(false, av.ConsPercGaps);
619 cs.setConservation(c);
625 av.alignment.addGroup(sg);
631 public void setShowDistances(boolean state)
633 this.showDistances = state;
637 public void setShowBootstrap(boolean state)
639 this.showBootstrap = state;
643 public void setMarkPlaceholders(boolean state)
645 this.markPlaceholders = state;