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 implements MouseListener,
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()) ? (PLACEHOLDER + node
214 .getName()) : node.getName();
215 FontMetrics fm = g.getFontMetrics(font);
216 int charWidth = fm.stringWidth(name) + 3;
217 int charHeight = fm.getHeight();
219 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
220 charWidth, charHeight);
222 nameHash.put(node.element(), rect);
224 // Colour selected leaves differently
225 SequenceGroup selected = av.getSelectionGroup();
227 && selected.getSequences(null).contains(node.element()))
229 g.setColor(Color.gray);
231 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
232 g.setColor(Color.white);
234 g.drawString(name, xend + 10, ypos);
235 g.setColor(Color.black);
239 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
241 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
244 double height = node.height;
245 double dist = node.dist;
247 int xstart = (int) ((height - dist) * scale) + offx;
248 int xend = (int) (height * scale) + offx;
249 int ypos = (int) (node.ycount * chunk) + offy;
251 g.setColor(node.color.darker());
253 // Draw horizontal line
254 g.drawLine(xstart, ypos, xend, ypos);
255 if (node == highlightNode)
257 g.fillRect(xend - 3, ypos - 3, 6, 6);
261 g.fillRect(xend - 2, ypos - 2, 4, 4);
264 int ystart = (int) (node.left() == null ? 0 : (((SequenceNode) node
265 .left()).ycount * chunk))
267 int yend = (int) (node.right() == null ? 0 : (((SequenceNode) node
268 .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) + offx);
496 g.drawLine(x, 0, x, getSize().height);
502 public void mouseReleased(MouseEvent e)
507 public void mouseEntered(MouseEvent e)
512 public void mouseExited(MouseEvent e)
517 public void mouseClicked(MouseEvent evt)
519 if (highlightNode != null)
521 if (evt.getClickCount() > 1)
523 tree.swapNodes(highlightNode);
524 tree.reCount(tree.getTopNode());
525 tree.findHeight(tree.getTopNode());
529 Vector<SequenceNode> leaves = tree.findLeaves(highlightNode);
531 for (int i = 0; i < leaves.size(); i++)
533 SequenceI seq = (SequenceI) leaves.elementAt(i).element();
534 treeSelectionChanged(seq);
538 PaintRefresher.Refresh(this, av.getSequenceSetId());
545 public void mouseDragged(MouseEvent ect)
550 public void mouseMoved(MouseEvent evt)
552 av.setCurrentTree(tree);
554 Object ob = findElement(evt.getX(), evt.getY());
556 if (ob instanceof SequenceNode)
558 highlightNode = (SequenceNode) ob;
563 if (highlightNode != null)
565 highlightNode = null;
572 public void mousePressed(MouseEvent e)
574 av.setCurrentTree(tree);
579 Object ob = findElement(x, y);
581 if (ob instanceof SequenceI)
583 treeSelectionChanged((Sequence) ob);
584 PaintRefresher.Refresh(this, av.getSequenceSetId());
589 else if (!(ob instanceof SequenceNode))
593 if (tree.getMaxHeight() != 0)
595 threshold = (float) (x - offx)
596 / (float) (getSize().width - labelLength - 2 * offx);
598 List<SequenceNode> groups = tree.groupNodes(threshold);
599 setColor(tree.getTopNode(), Color.black);
601 av.setSelectionGroup(null);
602 av.getAlignment().deleteAllGroups();
603 av.clearSequenceColours();
604 final AlignViewportI codingComplement = av.getCodingComplement();
605 if (codingComplement != null)
607 codingComplement.setSelectionGroup(null);
608 codingComplement.getAlignment().deleteAllGroups();
609 codingComplement.clearSequenceColours();
612 colourGroups(groups);
617 PaintRefresher.Refresh(this, av.getSequenceSetId());
622 void colourGroups(List<SequenceNode> groups)
624 for (int i = 0; i < groups.size(); i++)
627 Color col = new Color((int) (Math.random() * 255),
628 (int) (Math.random() * 255), (int) (Math.random() * 255));
629 setColor(groups.get(i), col.brighter());
631 Vector<SequenceNode> l = tree.findLeaves(groups.get(i));
633 Vector<SequenceI> sequences = new Vector<SequenceI>();
634 for (int j = 0; j < l.size(); j++)
636 SequenceI s1 = (SequenceI) l.elementAt(j).element();
637 if (!sequences.contains(s1))
639 sequences.addElement(s1);
643 ColourSchemeI cs = null;
645 SequenceGroup sg = new SequenceGroup(sequences, "", cs, true, true,
646 false, 0, av.getAlignment().getWidth() - 1);
648 if (av.getGlobalColourScheme() != null)
650 if (av.getGlobalColourScheme() instanceof UserColourScheme)
652 cs = new UserColourScheme(
653 ((UserColourScheme) av.getGlobalColourScheme())
659 cs = ColourSchemeProperty.getColourScheme(sg,
660 ColourSchemeProperty.getColourName(av
661 .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;