2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
23 import jalview.analysis.Conservation;
24 import jalview.analysis.NJTree;
25 import jalview.datamodel.Sequence;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.datamodel.SequenceNode;
29 import jalview.schemes.ColourSchemeI;
30 import jalview.schemes.ColourSchemeProperty;
31 import jalview.schemes.ResidueProperties;
32 import jalview.schemes.UserColourScheme;
33 import jalview.util.Format;
34 import jalview.util.MessageManager;
36 import java.awt.Color;
37 import java.awt.Dimension;
39 import java.awt.FontMetrics;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 import java.awt.Point;
43 import java.awt.Rectangle;
44 import java.awt.RenderingHints;
45 import java.awt.event.MouseEvent;
46 import java.awt.event.MouseListener;
47 import java.awt.event.MouseMotionListener;
48 import java.awt.print.PageFormat;
49 import java.awt.print.Printable;
50 import java.awt.print.PrinterException;
51 import java.awt.print.PrinterJob;
52 import java.util.Enumeration;
53 import java.util.Hashtable;
54 import java.util.Vector;
56 import javax.swing.JColorChooser;
57 import javax.swing.JPanel;
58 import javax.swing.JScrollPane;
59 import javax.swing.SwingUtilities;
60 import javax.swing.ToolTipManager;
68 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
69 Printable, MouseMotionListener
72 public static final String PLACEHOLDER = " * ";
76 JScrollPane scrollPane;
88 boolean fitToWindow = true;
90 boolean showDistances = false;
92 boolean showBootstrap = false;
94 boolean markPlaceholders = false;
104 int labelLength = -1;
106 Hashtable nameHash = new Hashtable();
108 Hashtable nodeHash = new Hashtable();
110 SequenceNode highlightNode;
112 boolean applyToAllViews = false;
115 * Creates a new TreeCanvas object.
126 public TreeCanvas(TreePanel tp, AlignmentPanel ap, JScrollPane scroller)
132 scrollPane = scroller;
133 addMouseListener(this);
134 addMouseMotionListener(this);
135 ToolTipManager.sharedInstance().registerComponent(this);
144 public void treeSelectionChanged(SequenceI sequence)
146 AlignmentPanel[] aps = getAssociatedPanels();
148 for (int a = 0; a < aps.length; a++)
150 SequenceGroup selected = aps[a].av.getSelectionGroup();
152 if (selected == null)
154 selected = new SequenceGroup();
155 aps[a].av.setSelectionGroup(selected);
158 selected.setEndRes(aps[a].av.getAlignment().getWidth() - 1);
159 selected.addOrRemove(sequence, true);
169 public void setTree(NJTree tree)
172 tree.findHeight(tree.getTopNode());
174 // Now have to calculate longest name based on the leaves
175 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
176 boolean has_placeholders = false;
179 for (int i = 0; i < leaves.size(); i++)
181 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
183 if (lf.isPlaceholder())
185 has_placeholders = true;
188 if (longestName.length() < ((Sequence) lf.element()).getName()
191 longestName = TreeCanvas.PLACEHOLDER
192 + ((Sequence) lf.element()).getName();
196 setMarkPlaceholders(has_placeholders);
217 public void drawNode(Graphics g, SequenceNode node, float chunk,
218 float scale, int width, int offx, int offy)
225 if ((node.left() == null) && (node.right() == null))
228 float height = node.height;
229 float dist = node.dist;
231 int xstart = (int) ((height - dist) * scale) + offx;
232 int xend = (int) (height * scale) + offx;
234 int ypos = (int) (node.ycount * chunk) + offy;
236 if (node.element() instanceof SequenceI)
238 SequenceI seq = (SequenceI) node.element();
240 if (av.getSequenceColour(seq) == Color.white)
242 g.setColor(Color.black);
246 g.setColor(av.getSequenceColour(seq).darker());
251 g.setColor(Color.black);
254 // Draw horizontal line
255 g.drawLine(xstart, ypos, xend, ypos);
257 String nodeLabel = "";
259 if (showDistances && (node.dist > 0))
261 nodeLabel = new Format("%-.2f").form(node.dist);
264 if (showBootstrap && node.bootstrap > -1)
268 nodeLabel = nodeLabel + " : ";
271 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
274 if (!nodeLabel.equals(""))
276 g.drawString(nodeLabel, xstart + 2, ypos - 2);
279 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
280 .getName()) : node.getName();
282 int charWidth = fm.stringWidth(name) + 3;
283 int charHeight = font.getSize();
285 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
286 charWidth, charHeight);
288 nameHash.put(node.element(), rect);
290 // Colour selected leaves differently
291 SequenceGroup selected = av.getSelectionGroup();
293 if ((selected != null)
294 && selected.getSequences(null).contains(
297 g.setColor(Color.gray);
299 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
300 g.setColor(Color.white);
303 g.drawString(name, xend + 10, ypos + fm.getDescent());
304 g.setColor(Color.black);
308 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
310 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
313 float height = node.height;
314 float dist = node.dist;
316 int xstart = (int) ((height - dist) * scale) + offx;
317 int xend = (int) (height * scale) + offx;
318 int ypos = (int) (node.ycount * chunk) + offy;
320 g.setColor(node.color.darker());
322 // Draw horizontal line
323 g.drawLine(xstart, ypos, xend, ypos);
324 if (node == highlightNode)
326 g.fillRect(xend - 3, ypos - 3, 6, 6);
330 g.fillRect(xend - 2, ypos - 2, 4, 4);
333 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
335 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
338 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
339 nodeHash.put(node, pos);
341 g.drawLine((int) (height * scale) + offx, ystart,
342 (int) (height * scale) + offx, yend);
344 String nodeLabel = "";
346 if (showDistances && (node.dist > 0))
348 nodeLabel = new Format("%-.2f").form(node.dist);
351 if (showBootstrap && node.bootstrap > -1)
355 nodeLabel = nodeLabel + " : ";
358 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
361 if (!nodeLabel.equals(""))
363 g.drawString(nodeLabel, xstart + 2, ypos - 2);
376 * @return DOCUMENT ME!
378 public Object findElement(int x, int y)
380 Enumeration keys = nameHash.keys();
382 while (keys.hasMoreElements())
384 Object ob = keys.nextElement();
385 Rectangle rect = (Rectangle) nameHash.get(ob);
387 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
388 && (y <= (rect.y + rect.height)))
394 keys = nodeHash.keys();
396 while (keys.hasMoreElements())
398 Object ob = keys.nextElement();
399 Rectangle rect = (Rectangle) nodeHash.get(ob);
401 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
402 && (y <= (rect.y + rect.height)))
417 public void pickNodes(Rectangle pickBox)
419 int width = getWidth();
420 int height = getHeight();
422 SequenceNode top = tree.getTopNode();
424 float wscale = (float) ((width * .8) - (offx * 2))
425 / tree.getMaxHeight();
429 top.count = ((SequenceNode) top.left()).count
430 + ((SequenceNode) top.right()).count;
433 float chunk = (float) (height - (offy)) / top.count;
435 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
456 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
457 float scale, int width, int offx, int offy)
464 if ((node.left() == null) && (node.right() == null))
466 float height = node.height;
467 float dist = node.dist;
469 int xstart = (int) ((height - dist) * scale) + offx;
470 int xend = (int) (height * scale) + offx;
472 int ypos = (int) (node.ycount * chunk) + offy;
474 if (pickBox.contains(new Point(xend, ypos)))
476 if (node.element() instanceof SequenceI)
478 SequenceI seq = (SequenceI) node.element();
479 SequenceGroup sg = av.getSelectionGroup();
483 sg.addOrRemove(seq, true);
490 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
492 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
505 public void setColor(SequenceNode node, Color c)
512 if ((node.left() == null) && (node.right() == null)) // TODO: internal node
516 if (node.element() instanceof SequenceI)
518 AlignmentPanel[] aps = getAssociatedPanels();
521 for (int a = 0; a < aps.length; a++)
523 aps[a].av.setSequenceColour((SequenceI) node.element(), c);
531 setColor((SequenceNode) node.left(), c);
532 setColor((SequenceNode) node.right(), c);
541 Thread thread = new Thread(this);
545 // put printing in a thread to avoid painting problems
548 PrinterJob printJob = PrinterJob.getPrinterJob();
549 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
551 printJob.setPrintable(this, pf);
553 if (printJob.printDialog())
558 } catch (Exception PrintException)
560 PrintException.printStackTrace();
575 * @return DOCUMENT ME!
577 * @throws PrinterException
580 public int print(Graphics pg, PageFormat pf, int pi)
581 throws PrinterException
584 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
586 int pwidth = (int) pf.getImageableWidth();
587 int pheight = (int) pf.getImageableHeight();
589 int noPages = getHeight() / pheight;
593 return Printable.NO_SUCH_PAGE;
596 if (pwidth > getWidth())
603 if (pheight > getHeight())
605 pheight = getHeight();
612 FontMetrics fm = pg.getFontMetrics(font);
613 int height = fm.getHeight() * nameHash.size();
614 pg.translate(0, -pi * pheight);
615 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
617 // translate number of pages,
618 // height is screen size as this is the
619 // non overlapping text size
623 draw(pg, pwidth, pheight);
625 return Printable.PAGE_EXISTS;
634 public void paintComponent(Graphics g)
636 super.paintComponent(g);
641 g.drawString(MessageManager.getString("label.calculating_tree")
642 + "....", 20, getHeight() / 2);
646 fm = g.getFontMetrics(font);
648 if (nameHash.size() == 0)
654 || (!fitToWindow && (scrollPane.getHeight() > ((fm
655 .getHeight() * nameHash.size()) + offy))))
657 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
658 setPreferredSize(null);
662 setPreferredSize(new Dimension(scrollPane.getWidth(),
663 fm.getHeight() * nameHash.size()));
664 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
667 scrollPane.revalidate();
677 public void setFont(Font font)
693 public void draw(Graphics g1, int width, int height)
695 Graphics2D g2 = (Graphics2D) g1;
696 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
697 RenderingHints.VALUE_ANTIALIAS_ON);
698 g2.setColor(Color.white);
699 g2.fillRect(0, 0, width, height);
702 if (longestName == null || tree == null)
704 g2.drawString("Calculating tree.", 20, 20);
706 offy = font.getSize() + 10;
708 fm = g2.getFontMetrics(font);
710 labelLength = fm.stringWidth(longestName) + 20; // 20 allows for scrollbar
712 float wscale = (width - labelLength - (offx * 2))
713 / tree.getMaxHeight();
715 SequenceNode top = tree.getTopNode();
719 top.count = ((SequenceNode) top.left()).count
720 + ((SequenceNode) top.right()).count;
723 float chunk = (float) (height - (offy)) / top.count;
725 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
729 if (av.getCurrentTree() == tree)
731 g2.setColor(Color.red);
735 g2.setColor(Color.gray);
738 int x = (int) ((threshold * (getWidth() - labelLength - (2 * offx))) + offx);
740 g2.drawLine(x, 0, x, getHeight());
750 public void mouseReleased(MouseEvent e)
760 public void mouseEntered(MouseEvent e)
770 public void mouseExited(MouseEvent e)
780 public void mouseClicked(MouseEvent evt)
782 if (highlightNode != null)
784 if (SwingUtilities.isRightMouseButton(evt))
786 Color col = JColorChooser.showDialog(this,
787 MessageManager.getString("label.select_subtree_colour"), highlightNode.color);
790 setColor(highlightNode, col);
793 else if (evt.getClickCount() > 1)
795 tree.swapNodes(highlightNode);
796 tree.reCount(tree.getTopNode());
797 tree.findHeight(tree.getTopNode());
801 Vector leaves = new Vector();
802 tree.findLeaves(highlightNode, leaves);
804 for (int i = 0; i < leaves.size(); i++)
806 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
808 treeSelectionChanged(seq);
813 PaintRefresher.Refresh(tp, av.getSequenceSetId());
818 public void mouseMoved(MouseEvent evt)
820 av.setCurrentTree(tree);
822 Object ob = findElement(evt.getX(), evt.getY());
824 if (ob instanceof SequenceNode)
826 highlightNode = (SequenceNode) ob;
827 this.setToolTipText("<html>"
828 + MessageManager.getString("label.highlightnode"));
834 if (highlightNode != null)
836 highlightNode = null;
837 setToolTipText(null);
843 public void mouseDragged(MouseEvent ect)
853 public void mousePressed(MouseEvent e)
855 av.setCurrentTree(tree);
860 Object ob = findElement(x, y);
862 if (ob instanceof SequenceI)
864 treeSelectionChanged((Sequence) ob);
865 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
870 else if (!(ob instanceof SequenceNode))
873 if (tree.getMaxHeight() != 0)
875 threshold = (float) (x - offx)
876 / (float) (getWidth() - labelLength - (2 * offx));
878 tree.getGroups().removeAllElements();
879 tree.groupNodes(tree.getTopNode(), threshold);
880 setColor(tree.getTopNode(), Color.black);
882 AlignmentPanel[] aps = getAssociatedPanels();
884 for (int a = 0; a < aps.length; a++)
886 aps[a].av.setSelectionGroup(null);
887 aps[a].av.getAlignment().deleteAllGroups();
888 aps[a].av.clearSequenceColours();
893 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
901 AlignmentPanel[] aps = getAssociatedPanels();
902 for (int i = 0; i < tree.getGroups().size(); i++)
904 Color col = new Color((int) (Math.random() * 255),
905 (int) (Math.random() * 255), (int) (Math.random() * 255));
906 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
908 Vector l = tree.findLeaves(
909 (SequenceNode) tree.getGroups().elementAt(i), new Vector());
911 Vector sequences = new Vector();
913 for (int j = 0; j < l.size(); j++)
915 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
918 if (!sequences.contains(s1))
920 sequences.addElement(s1);
924 ColourSchemeI cs = null;
925 SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
926 false, 0, av.getAlignment().getWidth() - 1);
928 if (av.getGlobalColourScheme() != null)
930 if (av.getGlobalColourScheme() instanceof UserColourScheme)
932 cs = new UserColourScheme(
933 ((UserColourScheme) av.getGlobalColourScheme())
939 cs = ColourSchemeProperty.getColour(sg, ColourSchemeProperty
940 .getColourName(av.getGlobalColourScheme()));
942 // cs is null if shading is an annotationColourGradient
945 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
946 av.isIgnoreGapsConsensus());
950 // sg.recalcConservation();
951 sg.setName("JTreeGroup:" + sg.hashCode());
953 for (int a = 0; a < aps.length; a++)
955 if (aps[a].av.getGlobalColourScheme() != null
956 && aps[a].av.getGlobalColourScheme().conservationApplied())
958 Conservation c = new Conservation("Group",
959 ResidueProperties.propHash, 3, sg.getSequences(null),
960 sg.getStartRes(), sg.getEndRes());
963 c.verdict(false, aps[a].av.getConsPercGaps());
964 sg.cs.setConservation(c);
967 aps[a].av.getAlignment().addGroup(new SequenceGroup(sg));
970 // notify the panel to redo any group specific stuff.
971 for (int a = 0; a < aps.length; a++)
973 aps[a].updateAnnotation();
974 // TODO: JAL-868 - need to ensure view colour change message is broadcast
975 // to any Jmols listening in
986 public void setShowDistances(boolean state)
988 this.showDistances = state;
998 public void setShowBootstrap(boolean state)
1000 this.showBootstrap = state;
1010 public void setMarkPlaceholders(boolean state)
1012 this.markPlaceholders = state;
1016 AlignmentPanel[] getAssociatedPanels()
1018 if (applyToAllViews)
1020 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
1024 return new AlignmentPanel[]