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.Conservation;
24 import jalview.analysis.NJTree;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequenceNode;
30 import jalview.schemes.Colour;
31 import jalview.schemes.ColourSchemeI;
32 import jalview.schemes.ColourSchemeProperty;
33 import jalview.schemes.ResidueProperties;
34 import jalview.schemes.UserColourScheme;
35 import jalview.util.ColorUtils;
36 import jalview.util.Format;
37 import jalview.util.MappingUtils;
38 import jalview.viewmodel.AlignmentViewport;
40 import java.awt.Color;
41 import java.awt.Dimension;
43 import java.awt.FontMetrics;
44 import java.awt.Graphics;
45 import java.awt.Panel;
46 import java.awt.Point;
47 import java.awt.Rectangle;
48 import java.awt.ScrollPane;
49 import java.awt.event.MouseEvent;
50 import java.awt.event.MouseListener;
51 import java.awt.event.MouseMotionListener;
52 import java.util.Enumeration;
53 import java.util.Hashtable;
54 import java.util.Vector;
56 public class TreeCanvas extends Panel implements MouseListener,
61 ScrollPane scrollPane;
65 public static final String PLACEHOLDER = " * ";
69 boolean fitToWindow = true;
71 boolean showDistances = false;
73 boolean showBootstrap = false;
75 boolean markPlaceholders = false;
87 Hashtable nameHash = new Hashtable();
89 Hashtable nodeHash = new Hashtable();
91 SequenceNode highlightNode;
95 public TreeCanvas(AlignmentPanel ap, ScrollPane scroller)
100 scrollPane = scroller;
101 addMouseListener(this);
102 addMouseMotionListener(this);
105 PaintRefresher.Register(this, av.getSequenceSetId());
108 public void treeSelectionChanged(SequenceI sequence)
110 SequenceGroup selected = av.getSelectionGroup();
111 if (selected == null)
113 selected = new SequenceGroup();
114 av.setSelectionGroup(selected);
117 selected.setEndRes(av.getAlignment().getWidth() - 1);
118 selected.addOrRemove(sequence, true);
121 public void setTree(NJTree tree)
124 tree.findHeight(tree.getTopNode());
126 // Now have to calculate longest name based on the leaves
127 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
128 boolean has_placeholders = false;
131 for (int i = 0; i < leaves.size(); i++)
133 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
135 if (lf.isPlaceholder())
137 has_placeholders = true;
140 if (longestName.length() < ((Sequence) lf.element()).getName()
143 longestName = TreeCanvas.PLACEHOLDER
144 + ((Sequence) lf.element()).getName();
148 setMarkPlaceholders(has_placeholders);
151 public void drawNode(Graphics g, SequenceNode node, float chunk,
152 float scale, int width, int offx, int offy)
159 if (node.left() == null && node.right() == null)
163 float height = node.height;
164 float dist = node.dist;
166 int xstart = (int) ((height - dist) * scale) + offx;
167 int xend = (int) (height * scale) + offx;
169 int ypos = (int) (node.ycount * chunk) + offy;
171 if (node.element() instanceof SequenceI)
173 SequenceI seq = (SequenceI) node.element();
175 if (av.getSequenceColour(seq) == Color.white)
177 g.setColor(Color.black);
181 g.setColor(ColorUtils.getColor(av.getSequenceColour(seq))
188 g.setColor(Color.black);
191 // Draw horizontal line
192 g.drawLine(xstart, ypos, xend, ypos);
194 String nodeLabel = "";
195 if (showDistances && node.dist > 0)
197 nodeLabel = new Format("%-.2f").form(node.dist);
201 int btstrap = node.getBootstrap();
206 nodeLabel = nodeLabel + " : ";
208 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
211 if (!nodeLabel.equals(""))
213 g.drawString(nodeLabel, xstart + 2, ypos - 2);
216 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
217 .getName()) : node.getName();
218 FontMetrics fm = g.getFontMetrics(font);
219 int charWidth = fm.stringWidth(name) + 3;
220 int charHeight = fm.getHeight();
222 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
223 charWidth, charHeight);
225 nameHash.put(node.element(), rect);
227 // Colour selected leaves differently
228 SequenceGroup selected = av.getSelectionGroup();
230 && selected.getSequences(null).contains(node.element()))
232 g.setColor(Color.gray);
234 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
235 g.setColor(Color.white);
237 g.drawString(name, xend + 10, ypos);
238 g.setColor(Color.black);
242 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
244 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
247 float height = node.height;
248 float dist = node.dist;
250 int xstart = (int) ((height - dist) * scale) + offx;
251 int xend = (int) (height * scale) + offx;
252 int ypos = (int) (node.ycount * chunk) + offy;
254 g.setColor(node.color.darker());
256 // Draw horizontal line
257 g.drawLine(xstart, ypos, xend, ypos);
258 if (node == highlightNode)
260 g.fillRect(xend - 3, ypos - 3, 6, 6);
264 g.fillRect(xend - 2, ypos - 2, 4, 4);
267 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
269 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
272 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
273 nodeHash.put(node, pos);
275 g.drawLine((int) (height * scale) + offx, ystart,
276 (int) (height * scale) + offx, yend);
278 String nodeLabel = "";
280 if (showDistances && (node.dist > 0))
282 nodeLabel = new Format("%-.2f").form(node.dist);
287 int btstrap = node.getBootstrap();
292 nodeLabel = nodeLabel + " : ";
294 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
298 if (!nodeLabel.equals(""))
300 g.drawString(nodeLabel, xstart + 2, ypos - 2);
306 public Object findElement(int x, int y)
308 Enumeration keys = nameHash.keys();
310 while (keys.hasMoreElements())
312 Object ob = keys.nextElement();
313 Rectangle rect = (Rectangle) nameHash.get(ob);
315 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
316 && y <= (rect.y + rect.height))
321 keys = nodeHash.keys();
323 while (keys.hasMoreElements())
325 Object ob = keys.nextElement();
326 Rectangle rect = (Rectangle) nodeHash.get(ob);
328 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
329 && y <= (rect.y + rect.height))
338 public void pickNodes(Rectangle pickBox)
340 int width = getSize().width;
341 int height = getSize().height;
343 SequenceNode top = tree.getTopNode();
345 float wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight();
348 top.count = ((SequenceNode) top.left()).count
349 + ((SequenceNode) top.right()).count;
351 float chunk = (float) (height - offy) / top.count;
353 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
356 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
357 float scale, int width, int offx, int offy)
364 if (node.left() == null && node.right() == null)
366 float height = node.height;
367 // float dist = node.dist;
369 // int xstart = (int) ( (height - dist) * scale) + offx;
370 int xend = (int) (height * scale) + offx;
372 int ypos = (int) (node.ycount * chunk) + offy;
374 if (pickBox.contains(new Point(xend, ypos)))
376 if (node.element() instanceof SequenceI)
378 SequenceI seq = (SequenceI) node.element();
379 SequenceGroup sg = av.getSelectionGroup();
382 sg.addOrRemove(seq, true);
389 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
391 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
396 public void setColor(SequenceNode node, Color c)
403 if (node.left() == null && node.right() == null)
407 if (node.element() instanceof SequenceI)
409 av.setSequenceColour((SequenceI) node.element(), new Colour(c));
415 setColor((SequenceNode) node.left(), c);
416 setColor((SequenceNode) node.right(), c);
421 public void update(Graphics g)
427 public void paint(Graphics g)
434 if (nameHash.size() == 0)
439 int width = scrollPane.getSize().width;
440 int height = scrollPane.getSize().height;
443 height = g.getFontMetrics(font).getHeight() * nameHash.size();
446 if (getSize().width > width)
448 setSize(new Dimension(width, height));
449 scrollPane.validate();
453 setSize(new Dimension(width, height));
456 draw(g, width, height);
460 public void draw(Graphics g, int width, int height)
462 offy = font.getSize() + 10;
464 g.setColor(Color.white);
465 g.fillRect(0, 0, width, height);
467 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; // 20
472 float wscale = (width - labelLength - offx * 2) / tree.getMaxHeight();
474 SequenceNode top = tree.getTopNode();
478 top.count = ((SequenceNode) top.left()).count
479 + ((SequenceNode) top.right()).count;
481 float chunk = (float) (height - offy) / top.count;
483 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
487 if (av.getCurrentTree() == tree)
489 g.setColor(Color.red);
493 g.setColor(Color.gray);
496 int x = (int) (threshold * (getSize().width - labelLength - 2 * offx) + offx);
498 g.drawLine(x, 0, x, getSize().height);
504 public void mouseReleased(MouseEvent e)
509 public void mouseEntered(MouseEvent e)
514 public void mouseExited(MouseEvent e)
519 public void mouseClicked(MouseEvent evt)
521 if (highlightNode != null)
523 if (evt.getClickCount() > 1)
525 tree.swapNodes(highlightNode);
526 tree.reCount(tree.getTopNode());
527 tree.findHeight(tree.getTopNode());
531 Vector leaves = new Vector();
532 tree.findLeaves(highlightNode, leaves);
534 for (int i = 0; i < leaves.size(); i++)
536 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
538 treeSelectionChanged(seq);
542 PaintRefresher.Refresh(this, av.getSequenceSetId());
549 public void mouseDragged(MouseEvent ect)
554 public void mouseMoved(MouseEvent evt)
556 av.setCurrentTree(tree);
558 Object ob = findElement(evt.getX(), evt.getY());
560 if (ob instanceof SequenceNode)
562 highlightNode = (SequenceNode) ob;
567 if (highlightNode != null)
569 highlightNode = null;
576 public void mousePressed(MouseEvent e)
578 av.setCurrentTree(tree);
583 Object ob = findElement(x, y);
585 if (ob instanceof SequenceI)
587 treeSelectionChanged((Sequence) ob);
588 PaintRefresher.Refresh(this, av.getSequenceSetId());
593 else if (!(ob instanceof SequenceNode))
597 if (tree.getMaxHeight() != 0)
599 threshold = (float) (x - offx)
600 / (float) (getSize().width - labelLength - 2 * offx);
602 tree.getGroups().removeAllElements();
603 tree.groupNodes(tree.getTopNode(), threshold);
604 setColor(tree.getTopNode(), Color.black);
606 av.setSelectionGroup(null);
607 av.getAlignment().deleteAllGroups();
608 av.clearSequenceColours();
609 final AlignViewportI codingComplement = av.getCodingComplement();
610 if (codingComplement != null)
612 codingComplement.setSelectionGroup(null);
613 codingComplement.getAlignment().deleteAllGroups();
614 codingComplement.clearSequenceColours();
622 PaintRefresher.Refresh(this, av.getSequenceSetId());
629 for (int i = 0; i < tree.getGroups().size(); i++)
632 Color col = new Color((int) (Math.random() * 255),
633 (int) (Math.random() * 255), (int) (Math.random() * 255));
634 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
636 Vector l = tree.findLeaves(
637 (SequenceNode) tree.getGroups().elementAt(i), new Vector());
639 Vector sequences = new Vector();
640 for (int j = 0; j < l.size(); j++)
642 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
644 if (!sequences.contains(s1))
646 sequences.addElement(s1);
650 ColourSchemeI cs = null;
652 SequenceGroup sg = new SequenceGroup(sequences, "", cs, true, true,
653 false, 0, av.getAlignment().getWidth() - 1);
655 if (av.getGlobalColourScheme() != null)
657 if (av.getGlobalColourScheme() instanceof UserColourScheme)
659 cs = new UserColourScheme(
660 ((UserColourScheme) av.getGlobalColourScheme())
666 cs = ColourSchemeProperty.getColour(sg, ColourSchemeProperty
667 .getColourName(av.getGlobalColourScheme()));
669 // cs is null if shading is an annotationColourGradient
672 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
673 av.isIgnoreGapsConsensus());
676 // TODO: cs used to be initialized with a sequence collection and
677 // recalcConservation called automatically
678 // instead we set it manually - recalc called after updateAnnotation
681 sg.setName("JTreeGroup:" + sg.hashCode());
683 if (av.getGlobalColourScheme() != null
684 && av.getGlobalColourScheme().conservationApplied())
686 Conservation c = new Conservation("Group",
687 ResidueProperties.propHash, 3, sg.getSequences(null),
688 sg.getStartRes(), sg.getEndRes());
691 c.verdict(false, av.getConsPercGaps());
692 cs.setConservation(c);
698 av.getAlignment().addGroup(sg);
700 // TODO this is duplicated with gui TreeCanvas - refactor
701 av.getAlignment().addGroup(sg);
702 final AlignViewportI codingComplement = av.getCodingComplement();
703 if (codingComplement != null)
705 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, av,
707 if (mappedGroup.getSequences().size() > 0)
709 codingComplement.getAlignment().addGroup(mappedGroup);
710 for (SequenceI seq : mappedGroup.getSequences())
712 // TODO why does gui require col.brighter() here??
713 codingComplement.setSequenceColour(seq, new Colour(col));
719 ap.updateAnnotation();
720 if (av.getCodingComplement() != null)
722 ((AlignmentViewport) av.getCodingComplement()).firePropertyChange(
723 "alignment", null, ap.av.getAlignment().getSequences());
727 public void setShowDistances(boolean state)
729 this.showDistances = state;
733 public void setShowBootstrap(boolean state)
735 this.showBootstrap = state;
739 public void setMarkPlaceholders(boolean state)
741 this.markPlaceholders = state;