2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28 import jalview.util.*;
30 public class TreeCanvas extends Panel implements MouseListener,
35 ScrollPane scrollPane;
39 public static final String PLACEHOLDER = " * ";
43 boolean fitToWindow = true;
45 boolean showDistances = false;
47 boolean showBootstrap = false;
49 boolean markPlaceholders = false;
61 Hashtable nameHash = new Hashtable();
63 Hashtable nodeHash = new Hashtable();
65 SequenceNode highlightNode;
67 public TreeCanvas(AlignViewport av, ScrollPane scroller)
71 scrollPane = scroller;
72 addMouseListener(this);
73 addMouseMotionListener(this);
76 PaintRefresher.Register(this, av.getSequenceSetId());
79 public void treeSelectionChanged(SequenceI sequence)
81 SequenceGroup selected = av.getSelectionGroup();
84 selected = new SequenceGroup();
85 av.setSelectionGroup(selected);
88 selected.setEndRes(av.alignment.getWidth() - 1);
89 selected.addOrRemove(sequence, true);
92 public void setTree(NJTree tree)
95 tree.findHeight(tree.getTopNode());
97 // Now have to calculate longest name based on the leaves
98 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
99 boolean has_placeholders = false;
102 for (int i = 0; i < leaves.size(); i++)
104 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
106 if (lf.isPlaceholder())
108 has_placeholders = true;
111 if (longestName.length() < ((Sequence) lf.element()).getName()
114 longestName = TreeCanvas.PLACEHOLDER
115 + ((Sequence) lf.element()).getName();
119 setMarkPlaceholders(has_placeholders);
122 public void drawNode(Graphics g, SequenceNode node, float chunk,
123 float scale, int width, int offx, int offy)
130 if (node.left() == null && node.right() == null)
134 float height = node.height;
135 float dist = node.dist;
137 int xstart = (int) ((height - dist) * scale) + offx;
138 int xend = (int) (height * scale) + offx;
140 int ypos = (int) (node.ycount * chunk) + offy;
142 if (node.element() instanceof SequenceI)
144 SequenceI seq = (SequenceI) ((SequenceNode) node).element();
146 if (av.getSequenceColour(seq) == Color.white)
148 g.setColor(Color.black);
152 g.setColor(av.getSequenceColour(seq).darker());
158 g.setColor(Color.black);
161 // Draw horizontal line
162 g.drawLine(xstart, ypos, xend, ypos);
164 String nodeLabel = "";
165 if (showDistances && node.dist > 0)
167 nodeLabel = new Format("%-.2f").form(node.dist);
171 int btstrap = node.getBootstrap();
176 nodeLabel = nodeLabel + " : ";
178 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
181 if (!nodeLabel.equals(""))
183 g.drawString(nodeLabel, xstart + 2, ypos - 2);
186 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
187 .getName()) : node.getName();
188 FontMetrics fm = g.getFontMetrics(font);
189 int charWidth = fm.stringWidth(name) + 3;
190 int charHeight = fm.getHeight();
192 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
193 charWidth, charHeight);
195 nameHash.put((SequenceI) node.element(), rect);
197 // Colour selected leaves differently
198 SequenceGroup selected = av.getSelectionGroup();
200 && selected.getSequences(null).contains(
201 (SequenceI) node.element()))
203 g.setColor(Color.gray);
205 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
206 g.setColor(Color.white);
208 g.drawString(name, xend + 10, ypos);
209 g.setColor(Color.black);
213 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
215 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
218 float height = node.height;
219 float dist = node.dist;
221 int xstart = (int) ((height - dist) * scale) + offx;
222 int xend = (int) (height * scale) + offx;
223 int ypos = (int) (node.ycount * chunk) + offy;
225 g.setColor(((SequenceNode) node).color.darker());
227 // Draw horizontal line
228 g.drawLine(xstart, ypos, xend, ypos);
229 if (node == highlightNode)
231 g.fillRect(xend - 3, ypos - 3, 6, 6);
235 g.fillRect(xend - 2, ypos - 2, 4, 4);
238 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
240 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
243 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
244 nodeHash.put(node, pos);
246 g.drawLine((int) (height * scale) + offx, ystart,
247 (int) (height * scale) + offx, yend);
249 String nodeLabel = "";
251 if (showDistances && (node.dist > 0))
253 nodeLabel = new Format("%-.2f").form(node.dist);
258 int btstrap = node.getBootstrap();
263 nodeLabel = nodeLabel + " : ";
265 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
269 if (!nodeLabel.equals(""))
271 g.drawString(nodeLabel, xstart + 2, ypos - 2);
277 public Object findElement(int x, int y)
279 Enumeration keys = nameHash.keys();
281 while (keys.hasMoreElements())
283 Object ob = keys.nextElement();
284 Rectangle rect = (Rectangle) nameHash.get(ob);
286 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
287 && y <= (rect.y + rect.height))
292 keys = nodeHash.keys();
294 while (keys.hasMoreElements())
296 Object ob = keys.nextElement();
297 Rectangle rect = (Rectangle) nodeHash.get(ob);
299 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
300 && y <= (rect.y + rect.height))
309 public void pickNodes(Rectangle pickBox)
311 int width = getSize().width;
312 int height = getSize().height;
314 SequenceNode top = tree.getTopNode();
316 float wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight();
319 top.count = ((SequenceNode) top.left()).count
320 + ((SequenceNode) top.right()).count;
322 float chunk = (float) (height - offy) / top.count;
324 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
327 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
328 float scale, int width, int offx, int offy)
335 if (node.left() == null && node.right() == null)
337 float height = node.height;
338 // float dist = node.dist;
340 // int xstart = (int) ( (height - dist) * scale) + offx;
341 int xend = (int) (height * scale) + offx;
343 int ypos = (int) (node.ycount * chunk) + offy;
345 if (pickBox.contains(new Point(xend, ypos)))
347 if (node.element() instanceof SequenceI)
349 SequenceI seq = (SequenceI) node.element();
350 SequenceGroup sg = av.getSelectionGroup();
353 sg.addOrRemove(seq, true);
360 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
362 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
367 public void setColor(SequenceNode node, Color c)
374 if (node.left() == null && node.right() == null)
378 if (node.element() instanceof SequenceI)
380 av.setSequenceColour((SequenceI) node.element(), c);
386 setColor((SequenceNode) node.left(), c);
387 setColor((SequenceNode) node.right(), c);
391 public void update(Graphics g)
396 public void paint(Graphics g)
403 if (nameHash.size() == 0)
408 int width = scrollPane.getSize().width;
409 int height = scrollPane.getSize().height;
412 height = g.getFontMetrics(font).getHeight() * nameHash.size();
415 if (getSize().width > width)
417 setSize(new Dimension(width, height));
418 scrollPane.validate();
422 setSize(new Dimension(width, height));
425 draw(g, width, height);
429 public void draw(Graphics g, int width, int height)
431 offy = font.getSize() + 10;
433 g.setColor(Color.white);
434 g.fillRect(0, 0, width, height);
436 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; // 20
441 float wscale = (float) (width - labelLength - offx * 2)
442 / tree.getMaxHeight();
444 SequenceNode top = tree.getTopNode();
448 top.count = ((SequenceNode) top.left()).count
449 + ((SequenceNode) top.right()).count;
451 float chunk = (float) (height - offy) / top.count;
453 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
457 if (av.getCurrentTree() == tree)
459 g.setColor(Color.red);
463 g.setColor(Color.gray);
466 int x = (int) (threshold
467 * (float) (getSize().width - labelLength - 2 * offx) + offx);
469 g.drawLine(x, 0, x, getSize().height);
474 public void mouseReleased(MouseEvent e)
478 public void mouseEntered(MouseEvent e)
482 public void mouseExited(MouseEvent e)
486 public void mouseClicked(MouseEvent evt)
488 if (highlightNode != null)
490 if (evt.getClickCount() > 1)
492 tree.swapNodes(highlightNode);
493 tree.reCount(tree.getTopNode());
494 tree.findHeight(tree.getTopNode());
498 Vector leaves = new Vector();
499 tree.findLeaves(highlightNode, leaves);
501 for (int i = 0; i < leaves.size(); i++)
503 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
505 treeSelectionChanged(seq);
509 PaintRefresher.Refresh(this, av.getSequenceSetId());
514 public void mouseDragged(MouseEvent ect)
518 public void mouseMoved(MouseEvent evt)
520 av.setCurrentTree(tree);
522 Object ob = findElement(evt.getX(), evt.getY());
524 if (ob instanceof SequenceNode)
526 highlightNode = (SequenceNode) ob;
531 if (highlightNode != null)
533 highlightNode = null;
539 public void mousePressed(MouseEvent e)
541 av.setCurrentTree(tree);
546 Object ob = findElement(x, y);
548 if (ob instanceof SequenceI)
550 treeSelectionChanged((Sequence) ob);
551 PaintRefresher.Refresh(this, av.getSequenceSetId());
555 else if (!(ob instanceof SequenceNode))
559 if (tree.getMaxHeight() != 0)
561 threshold = (float) (x - offx)
562 / (float) (getSize().width - labelLength - 2 * offx);
564 tree.getGroups().removeAllElements();
565 tree.groupNodes(tree.getTopNode(), threshold);
566 setColor(tree.getTopNode(), Color.black);
568 av.setSelectionGroup(null);
569 av.alignment.deleteAllGroups();
570 av.sequenceColours = null;
577 PaintRefresher.Refresh(this, av.getSequenceSetId());
584 for (int i = 0; i < tree.getGroups().size(); i++)
587 Color col = new Color((int) (Math.random() * 255),
588 (int) (Math.random() * 255), (int) (Math.random() * 255));
589 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
591 Vector l = tree.findLeaves(
592 (SequenceNode) tree.getGroups().elementAt(i), new Vector());
594 Vector sequences = new Vector();
595 for (int j = 0; j < l.size(); j++)
597 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
599 if (!sequences.contains(s1))
601 sequences.addElement(s1);
605 ColourSchemeI cs = null;
607 if (av.getGlobalColourScheme() != null)
609 if (av.getGlobalColourScheme() instanceof UserColourScheme)
611 cs = new UserColourScheme(
612 ((UserColourScheme) av.getGlobalColourScheme())
618 cs = ColourSchemeProperty.getColour(sequences, av.alignment
619 .getWidth(), ColourSchemeProperty.getColourName(av
620 .getGlobalColourScheme()));
623 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
624 av.getIgnoreGapsConsensus());
627 SequenceGroup sg = new SequenceGroup(sequences, "", cs, true, true,
628 false, 0, av.alignment.getWidth() - 1);
630 sg.setName("JTreeGroup:" + sg.hashCode());
632 if (av.getGlobalColourScheme() != null
633 && av.getGlobalColourScheme().conservationApplied())
635 Conservation c = new Conservation("Group",
636 ResidueProperties.propHash, 3, sg.getSequences(null),
637 sg.getStartRes(), sg.getEndRes());
640 c.verdict(false, av.ConsPercGaps);
641 cs.setConservation(c);
647 av.alignment.addGroup(sg);
653 public void setShowDistances(boolean state)
655 this.showDistances = state;
659 public void setShowBootstrap(boolean state)
661 this.showBootstrap = state;
665 public void setMarkPlaceholders(boolean state)
667 this.markPlaceholders = state;