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.TreeModel;
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.ColourSchemeI;
31 import jalview.schemes.ColourSchemeProperty;
32 import jalview.schemes.UserColourScheme;
33 import jalview.util.Format;
34 import jalview.util.MappingUtils;
35 import jalview.viewmodel.AlignmentViewport;
37 import java.awt.Color;
38 import java.awt.Dimension;
40 import java.awt.FontMetrics;
41 import java.awt.Graphics;
42 import java.awt.Panel;
43 import java.awt.Point;
44 import java.awt.Rectangle;
45 import java.awt.ScrollPane;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
49 import java.util.Enumeration;
50 import java.util.Hashtable;
51 import java.util.List;
52 import java.util.Vector;
54 public class TreeCanvas extends Panel
55 implements MouseListener, MouseMotionListener
59 ScrollPane scrollPane;
63 public static final String PLACEHOLDER = " * ";
67 boolean fitToWindow = true;
69 boolean showDistances = false;
71 boolean showBootstrap = false;
73 boolean markPlaceholders = false;
85 Hashtable nameHash = new Hashtable();
87 Hashtable nodeHash = new Hashtable();
89 SequenceNode highlightNode;
93 public TreeCanvas(AlignmentPanel ap, ScrollPane scroller)
98 scrollPane = scroller;
99 addMouseListener(this);
100 addMouseMotionListener(this);
103 PaintRefresher.Register(this, av.getSequenceSetId());
106 public void treeSelectionChanged(SequenceI sequence)
108 SequenceGroup selected = av.getSelectionGroup();
109 if (selected == null)
111 selected = new SequenceGroup();
112 av.setSelectionGroup(selected);
115 selected.setEndRes(av.getAlignment().getWidth() - 1);
116 selected.addOrRemove(sequence, true);
119 public void setTree(TreeModel tree2)
122 tree2.findHeight(tree2.getTopNode());
124 // Now have to calculate longest name based on the leaves
125 Vector<SequenceNode> leaves = tree2.findLeaves(tree2.getTopNode());
126 boolean has_placeholders = false;
129 for (int i = 0; i < leaves.size(); i++)
131 SequenceNode lf = leaves.elementAt(i);
133 if (lf.isPlaceholder())
135 has_placeholders = true;
138 if (longestName.length() < ((Sequence) lf.element()).getName()
141 longestName = TreeCanvas.PLACEHOLDER
142 + ((Sequence) lf.element()).getName();
146 setMarkPlaceholders(has_placeholders);
149 public void drawNode(Graphics g, SequenceNode node, float chunk,
150 double scale, int width, int offx, int offy)
157 if (node.left() == null && node.right() == null)
161 double height = node.height;
162 double dist = node.dist;
164 int xstart = (int) ((height - dist) * scale) + offx;
165 int xend = (int) (height * scale) + offx;
167 int ypos = (int) (node.ycount * chunk) + offy;
169 if (node.element() instanceof SequenceI)
171 SequenceI seq = (SequenceI) node.element();
173 if (av.getSequenceColour(seq) == Color.white)
175 g.setColor(Color.black);
179 g.setColor(av.getSequenceColour(seq).darker());
185 g.setColor(Color.black);
188 // Draw horizontal line
189 g.drawLine(xstart, ypos, xend, ypos);
191 String nodeLabel = "";
192 if (showDistances && node.dist > 0)
194 nodeLabel = new Format("%-.2f").form(node.dist);
198 int btstrap = node.getBootstrap();
203 nodeLabel = nodeLabel + " : ";
205 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
208 if (!nodeLabel.equals(""))
210 g.drawString(nodeLabel, xstart + 2, ypos - 2);
213 String name = (markPlaceholders && node.isPlaceholder())
214 ? (PLACEHOLDER + node.getName())
216 FontMetrics fm = g.getFontMetrics(font);
217 int charWidth = fm.stringWidth(name) + 3;
218 int charHeight = fm.getHeight();
220 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
221 charWidth, charHeight);
223 nameHash.put(node.element(), rect);
225 // Colour selected leaves differently
226 SequenceGroup selected = av.getSelectionGroup();
228 && selected.getSequences(null).contains(node.element()))
230 g.setColor(Color.gray);
232 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
233 g.setColor(Color.white);
235 g.drawString(name, xend + 10, ypos);
236 g.setColor(Color.black);
240 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
242 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
245 double height = node.height;
246 double dist = node.dist;
248 int xstart = (int) ((height - dist) * scale) + offx;
249 int xend = (int) (height * scale) + offx;
250 int ypos = (int) (node.ycount * chunk) + offy;
252 g.setColor(node.color.darker());
254 // Draw horizontal line
255 g.drawLine(xstart, ypos, xend, ypos);
256 if (node == highlightNode)
258 g.fillRect(xend - 3, ypos - 3, 6, 6);
262 g.fillRect(xend - 2, ypos - 2, 4, 4);
265 int ystart = (int) (node.left() == null ? 0
266 : (((SequenceNode) node.left()).ycount * chunk)) + offy;
267 int yend = (int) (node.right() == null ? 0
268 : (((SequenceNode) node.right()).ycount * chunk)) + offy;
270 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
271 nodeHash.put(node, pos);
273 g.drawLine((int) (height * scale) + offx, ystart,
274 (int) (height * scale) + offx, yend);
276 String nodeLabel = "";
278 if (showDistances && (node.dist > 0))
280 nodeLabel = new Format("%-.2f").form(node.dist);
285 int btstrap = node.getBootstrap();
290 nodeLabel = nodeLabel + " : ";
292 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
296 if (!nodeLabel.equals(""))
298 g.drawString(nodeLabel, xstart + 2, ypos - 2);
304 public Object findElement(int x, int y)
306 Enumeration keys = nameHash.keys();
308 while (keys.hasMoreElements())
310 Object ob = keys.nextElement();
311 Rectangle rect = (Rectangle) nameHash.get(ob);
313 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
314 && y <= (rect.y + rect.height))
319 keys = nodeHash.keys();
321 while (keys.hasMoreElements())
323 Object ob = keys.nextElement();
324 Rectangle rect = (Rectangle) nodeHash.get(ob);
326 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
327 && y <= (rect.y + rect.height))
336 public void pickNodes(Rectangle pickBox)
338 int width = getSize().width;
339 int height = getSize().height;
341 SequenceNode top = tree.getTopNode();
343 double wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight();
346 top.count = ((SequenceNode) top.left()).count
347 + ((SequenceNode) top.right()).count;
349 float chunk = (float) (height - offy) / top.count;
351 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
354 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
355 double scale, int width, int offx, int offy)
362 if (node.left() == null && node.right() == null)
364 double height = node.height;
365 // float dist = node.dist;
367 // int xstart = (int) ( (height - dist) * scale) + offx;
368 int xend = (int) (height * scale) + offx;
370 int ypos = (int) (node.ycount * chunk) + offy;
372 if (pickBox.contains(new Point(xend, ypos)))
374 if (node.element() instanceof SequenceI)
376 SequenceI seq = (SequenceI) node.element();
377 SequenceGroup sg = av.getSelectionGroup();
380 sg.addOrRemove(seq, true);
387 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
389 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
394 public void setColor(SequenceNode node, Color c)
401 if (node.left() == null && node.right() == null)
405 if (node.element() instanceof SequenceI)
407 av.setSequenceColour((SequenceI) node.element(), c);
413 setColor((SequenceNode) node.left(), c);
414 setColor((SequenceNode) node.right(), c);
419 public void update(Graphics g)
425 public void paint(Graphics g)
432 if (nameHash.size() == 0)
437 int width = scrollPane.getSize().width;
438 int height = scrollPane.getSize().height;
441 height = g.getFontMetrics(font).getHeight() * nameHash.size();
444 if (getSize().width > width)
446 setSize(new Dimension(width, height));
447 scrollPane.validate();
451 setSize(new Dimension(width, height));
454 draw(g, width, height);
458 public void draw(Graphics g, int width, int height)
460 offy = font.getSize() + 10;
462 g.setColor(Color.white);
463 g.fillRect(0, 0, width, height);
465 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; // 20
470 double wscale = (width - labelLength - offx * 2) / tree.getMaxHeight();
472 SequenceNode top = tree.getTopNode();
476 top.count = ((SequenceNode) top.left()).count
477 + ((SequenceNode) top.right()).count;
479 float chunk = (float) (height - offy) / top.count;
481 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
485 if (av.getCurrentTree() == tree)
487 g.setColor(Color.red);
491 g.setColor(Color.gray);
494 int x = (int) (threshold * (getSize().width - labelLength - 2 * offx)
497 g.drawLine(x, 0, x, getSize().height);
503 public void mouseReleased(MouseEvent e)
508 public void mouseEntered(MouseEvent e)
513 public void mouseExited(MouseEvent e)
518 public void mouseClicked(MouseEvent evt)
520 if (highlightNode != null)
522 if (evt.getClickCount() > 1)
524 tree.swapNodes(highlightNode);
525 tree.reCount(tree.getTopNode());
526 tree.findHeight(tree.getTopNode());
530 Vector<SequenceNode> leaves = tree.findLeaves(highlightNode);
532 for (int i = 0; i < leaves.size(); i++)
534 SequenceI seq = (SequenceI) leaves.elementAt(i).element();
535 treeSelectionChanged(seq);
539 PaintRefresher.Refresh(this, av.getSequenceSetId());
546 public void mouseDragged(MouseEvent ect)
551 public void mouseMoved(MouseEvent evt)
553 av.setCurrentTree(tree);
555 Object ob = findElement(evt.getX(), evt.getY());
557 if (ob instanceof SequenceNode)
559 highlightNode = (SequenceNode) ob;
564 if (highlightNode != null)
566 highlightNode = null;
573 public void mousePressed(MouseEvent e)
575 av.setCurrentTree(tree);
580 Object ob = findElement(x, y);
582 if (ob instanceof SequenceI)
584 treeSelectionChanged((Sequence) ob);
585 PaintRefresher.Refresh(this, av.getSequenceSetId());
590 else if (!(ob instanceof SequenceNode))
594 if (tree.getMaxHeight() != 0)
596 threshold = (float) (x - offx)
597 / (float) (getSize().width - labelLength - 2 * offx);
599 List<SequenceNode> groups = tree.groupNodes(threshold);
600 setColor(tree.getTopNode(), Color.black);
602 av.setSelectionGroup(null);
603 av.getAlignment().deleteAllGroups();
604 av.clearSequenceColours();
605 final AlignViewportI codingComplement = av.getCodingComplement();
606 if (codingComplement != null)
608 codingComplement.setSelectionGroup(null);
609 codingComplement.getAlignment().deleteAllGroups();
610 codingComplement.clearSequenceColours();
613 colourGroups(groups);
618 PaintRefresher.Refresh(this, av.getSequenceSetId());
623 void colourGroups(List<SequenceNode> groups)
625 for (int i = 0; i < groups.size(); i++)
628 Color col = new Color((int) (Math.random() * 255),
629 (int) (Math.random() * 255), (int) (Math.random() * 255));
630 setColor(groups.get(i), col.brighter());
632 Vector<SequenceNode> l = tree.findLeaves(groups.get(i));
634 Vector<SequenceI> sequences = new Vector<SequenceI>();
635 for (int j = 0; j < l.size(); j++)
637 SequenceI s1 = (SequenceI) l.elementAt(j).element();
638 if (!sequences.contains(s1))
640 sequences.addElement(s1);
644 ColourSchemeI cs = null;
646 SequenceGroup sg = new SequenceGroup(sequences, "", cs, true, true,
647 false, 0, av.getAlignment().getWidth() - 1);
649 if (av.getGlobalColourScheme() != null)
651 if (av.getGlobalColourScheme() instanceof UserColourScheme)
653 cs = new UserColourScheme(
654 ((UserColourScheme) av.getGlobalColourScheme())
660 cs = ColourSchemeProperty.getColourScheme(sg, ColourSchemeProperty
661 .getColourName(av.getGlobalColourScheme()));
663 // cs is null if shading is an annotationColourGradient
666 // cs.setThreshold(av.getViewportColourScheme().getThreshold(),
667 // av.isIgnoreGapsConsensus());
670 // TODO: cs used to be initialized with a sequence collection and
671 // recalcConservation called automatically
672 // instead we set it manually - recalc called after updateAnnotation
673 sg.setColourScheme(cs);
674 sg.getGroupColourScheme().setThreshold(
675 av.getResidueShading().getThreshold(),
676 av.isIgnoreGapsConsensus());
678 sg.setName("JTreeGroup:" + sg.hashCode());
680 if (av.getGlobalColourScheme() != null
681 && av.getResidueShading().conservationApplied())
683 Conservation c = new Conservation("Group", sg.getSequences(null),
684 sg.getStartRes(), sg.getEndRes());
687 c.verdict(false, av.getConsPercGaps());
689 sg.setColourScheme(cs);
690 sg.getGroupColourScheme().setConservation(c);
693 av.getAlignment().addGroup(sg);
695 // TODO this is duplicated with gui TreeCanvas - refactor
696 av.getAlignment().addGroup(sg);
697 final AlignViewportI codingComplement = av.getCodingComplement();
698 if (codingComplement != null)
700 SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, av,
702 if (mappedGroup.getSequences().size() > 0)
704 codingComplement.getAlignment().addGroup(mappedGroup);
705 for (SequenceI seq : mappedGroup.getSequences())
707 // TODO why does gui require col.brighter() here??
708 codingComplement.setSequenceColour(seq, col);
714 ap.updateAnnotation();
715 if (av.getCodingComplement() != null)
717 ((AlignmentViewport) av.getCodingComplement()).firePropertyChange(
718 "alignment", null, ap.av.getAlignment().getSequences());
722 public void setShowDistances(boolean state)
724 this.showDistances = state;
728 public void setShowBootstrap(boolean state)
730 this.showBootstrap = state;
734 public void setMarkPlaceholders(boolean state)
736 this.markPlaceholders = state;