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.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.Vector;
53 public class TreeCanvas extends Panel implements MouseListener,
58 ScrollPane scrollPane;
62 public static final String PLACEHOLDER = " * ";
66 boolean fitToWindow = true;
68 boolean showDistances = false;
70 boolean showBootstrap = false;
72 boolean markPlaceholders = false;
84 Hashtable nameHash = new Hashtable();
86 Hashtable nodeHash = new Hashtable();
88 SequenceNode highlightNode;
92 public TreeCanvas(AlignmentPanel ap, ScrollPane scroller)
97 scrollPane = scroller;
98 addMouseListener(this);
99 addMouseMotionListener(this);
102 PaintRefresher.Register(this, av.getSequenceSetId());
105 public void treeSelectionChanged(SequenceI sequence)
107 SequenceGroup selected = av.getSelectionGroup();
108 if (selected == null)
110 selected = new SequenceGroup();
111 av.setSelectionGroup(selected);
114 selected.setEndRes(av.getAlignment().getWidth() - 1);
115 selected.addOrRemove(sequence, true);
118 public void setTree(NJTree tree)
121 tree.findHeight(tree.getTopNode());
123 // Now have to calculate longest name based on the leaves
124 Vector<SequenceNode> leaves = tree.findLeaves(tree.getTopNode());
125 boolean has_placeholders = false;
128 for (int i = 0; i < leaves.size(); i++)
130 SequenceNode lf = leaves.elementAt(i);
132 if (lf.isPlaceholder())
134 has_placeholders = true;
137 if (longestName.length() < ((Sequence) lf.element()).getName()
140 longestName = TreeCanvas.PLACEHOLDER
141 + ((Sequence) lf.element()).getName();
145 setMarkPlaceholders(has_placeholders);
148 public void drawNode(Graphics g, SequenceNode node, float chunk,
149 float scale, int width, int offx, int offy)
156 if (node.left() == null && node.right() == null)
160 float height = node.height;
161 float dist = node.dist;
163 int xstart = (int) ((height - dist) * scale) + offx;
164 int xend = (int) (height * scale) + offx;
166 int ypos = (int) (node.ycount * chunk) + offy;
168 if (node.element() instanceof SequenceI)
170 SequenceI seq = (SequenceI) node.element();
172 if (av.getSequenceColour(seq) == Color.white)
174 g.setColor(Color.black);
178 g.setColor(av.getSequenceColour(seq).darker());
184 g.setColor(Color.black);
187 // Draw horizontal line
188 g.drawLine(xstart, ypos, xend, ypos);
190 String nodeLabel = "";
191 if (showDistances && node.dist > 0)
193 nodeLabel = new Format("%-.2f").form(node.dist);
197 int btstrap = node.getBootstrap();
202 nodeLabel = nodeLabel + " : ";
204 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
207 if (!nodeLabel.equals(""))
209 g.drawString(nodeLabel, xstart + 2, ypos - 2);
212 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
213 .getName()) : node.getName();
214 FontMetrics fm = g.getFontMetrics(font);
215 int charWidth = fm.stringWidth(name) + 3;
216 int charHeight = fm.getHeight();
218 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
219 charWidth, charHeight);
221 nameHash.put(node.element(), rect);
223 // Colour selected leaves differently
224 SequenceGroup selected = av.getSelectionGroup();
226 && selected.getSequences(null).contains(node.element()))
228 g.setColor(Color.gray);
230 g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
231 g.setColor(Color.white);
233 g.drawString(name, xend + 10, ypos);
234 g.setColor(Color.black);
238 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
240 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
243 float height = node.height;
244 float dist = node.dist;
246 int xstart = (int) ((height - dist) * scale) + offx;
247 int xend = (int) (height * scale) + offx;
248 int ypos = (int) (node.ycount * chunk) + offy;
250 g.setColor(node.color.darker());
252 // Draw horizontal line
253 g.drawLine(xstart, ypos, xend, ypos);
254 if (node == highlightNode)
256 g.fillRect(xend - 3, ypos - 3, 6, 6);
260 g.fillRect(xend - 2, ypos - 2, 4, 4);
263 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
265 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
268 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
269 nodeHash.put(node, pos);
271 g.drawLine((int) (height * scale) + offx, ystart,
272 (int) (height * scale) + offx, yend);
274 String nodeLabel = "";
276 if (showDistances && (node.dist > 0))
278 nodeLabel = new Format("%-.2f").form(node.dist);
283 int btstrap = node.getBootstrap();
288 nodeLabel = nodeLabel + " : ";
290 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
294 if (!nodeLabel.equals(""))
296 g.drawString(nodeLabel, xstart + 2, ypos - 2);
302 public Object findElement(int x, int y)
304 Enumeration keys = nameHash.keys();
306 while (keys.hasMoreElements())
308 Object ob = keys.nextElement();
309 Rectangle rect = (Rectangle) nameHash.get(ob);
311 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
312 && y <= (rect.y + rect.height))
317 keys = nodeHash.keys();
319 while (keys.hasMoreElements())
321 Object ob = keys.nextElement();
322 Rectangle rect = (Rectangle) nodeHash.get(ob);
324 if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
325 && y <= (rect.y + rect.height))
334 public void pickNodes(Rectangle pickBox)
336 int width = getSize().width;
337 int height = getSize().height;
339 SequenceNode top = tree.getTopNode();
341 float wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight();
344 top.count = ((SequenceNode) top.left()).count
345 + ((SequenceNode) top.right()).count;
347 float chunk = (float) (height - offy) / top.count;
349 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
352 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
353 float scale, int width, int offx, int offy)
360 if (node.left() == null && node.right() == null)
362 float height = node.height;
363 // float dist = node.dist;
365 // int xstart = (int) ( (height - dist) * scale) + offx;
366 int xend = (int) (height * scale) + offx;
368 int ypos = (int) (node.ycount * chunk) + offy;
370 if (pickBox.contains(new Point(xend, ypos)))
372 if (node.element() instanceof SequenceI)
374 SequenceI seq = (SequenceI) node.element();
375 SequenceGroup sg = av.getSelectionGroup();
378 sg.addOrRemove(seq, true);
385 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
387 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
392 public void setColor(SequenceNode node, Color c)
399 if (node.left() == null && node.right() == null)
403 if (node.element() instanceof SequenceI)
405 av.setSequenceColour((SequenceI) node.element(), c);
411 setColor((SequenceNode) node.left(), c);
412 setColor((SequenceNode) node.right(), c);
417 public void update(Graphics g)
423 public void paint(Graphics g)
430 if (nameHash.size() == 0)
435 int width = scrollPane.getSize().width;
436 int height = scrollPane.getSize().height;
439 height = g.getFontMetrics(font).getHeight() * nameHash.size();
442 if (getSize().width > width)
444 setSize(new Dimension(width, height));
445 scrollPane.validate();
449 setSize(new Dimension(width, height));
452 draw(g, width, height);
456 public void draw(Graphics g, int width, int height)
458 offy = font.getSize() + 10;
460 g.setColor(Color.white);
461 g.fillRect(0, 0, width, height);
463 labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; // 20
468 float wscale = (width - labelLength - offx * 2) / tree.getMaxHeight();
470 SequenceNode top = tree.getTopNode();
474 top.count = ((SequenceNode) top.left()).count
475 + ((SequenceNode) top.right()).count;
477 float chunk = (float) (height - offy) / top.count;
479 drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
483 if (av.getCurrentTree() == tree)
485 g.setColor(Color.red);
489 g.setColor(Color.gray);
492 int x = (int) (threshold * (getSize().width - labelLength - 2 * offx) + offx);
494 g.drawLine(x, 0, x, getSize().height);
500 public void mouseReleased(MouseEvent e)
505 public void mouseEntered(MouseEvent e)
510 public void mouseExited(MouseEvent e)
515 public void mouseClicked(MouseEvent evt)
517 if (highlightNode != null)
519 if (evt.getClickCount() > 1)
521 tree.swapNodes(highlightNode);
522 tree.reCount(tree.getTopNode());
523 tree.findHeight(tree.getTopNode());
527 Vector<SequenceNode> leaves = tree.findLeaves(highlightNode);
529 for (int i = 0; i < leaves.size(); i++)
531 SequenceI seq = (SequenceI) leaves.elementAt(i).element();
532 treeSelectionChanged(seq);
536 PaintRefresher.Refresh(this, av.getSequenceSetId());
543 public void mouseDragged(MouseEvent ect)
548 public void mouseMoved(MouseEvent evt)
550 av.setCurrentTree(tree);
552 Object ob = findElement(evt.getX(), evt.getY());
554 if (ob instanceof SequenceNode)
556 highlightNode = (SequenceNode) ob;
561 if (highlightNode != null)
563 highlightNode = null;
570 public void mousePressed(MouseEvent e)
572 av.setCurrentTree(tree);
577 Object ob = findElement(x, y);
579 if (ob instanceof SequenceI)
581 treeSelectionChanged((Sequence) ob);
582 PaintRefresher.Refresh(this, av.getSequenceSetId());
587 else if (!(ob instanceof SequenceNode))
591 if (tree.getMaxHeight() != 0)
593 threshold = (float) (x - offx)
594 / (float) (getSize().width - labelLength - 2 * offx);
596 tree.getGroups().removeAllElements();
597 tree.groupNodes(tree.getTopNode(), threshold);
598 setColor(tree.getTopNode(), Color.black);
600 av.setSelectionGroup(null);
601 av.getAlignment().deleteAllGroups();
602 av.clearSequenceColours();
603 final AlignViewportI codingComplement = av.getCodingComplement();
604 if (codingComplement != null)
606 codingComplement.setSelectionGroup(null);
607 codingComplement.getAlignment().deleteAllGroups();
608 codingComplement.clearSequenceColours();
616 PaintRefresher.Refresh(this, av.getSequenceSetId());
623 for (int i = 0; i < tree.getGroups().size(); i++)
626 Color col = new Color((int) (Math.random() * 255),
627 (int) (Math.random() * 255), (int) (Math.random() * 255));
628 setColor(tree.getGroups().elementAt(i), col.brighter());
630 Vector<SequenceNode> l = tree.findLeaves(tree.getGroups()
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;