2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
24 import java.awt.print.*;
27 import jalview.analysis.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.util.*;
38 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
39 Printable, MouseMotionListener
42 public static final String PLACEHOLDER = " * ";
46 JScrollPane scrollPane;
58 boolean fitToWindow = true;
60 boolean showDistances = false;
62 boolean showBootstrap = false;
64 boolean markPlaceholders = false;
76 Hashtable nameHash = new Hashtable();
78 Hashtable nodeHash = new Hashtable();
80 SequenceNode highlightNode;
82 boolean applyToAllViews = false;
85 * Creates a new TreeCanvas object.
96 public TreeCanvas(TreePanel tp, AlignmentPanel ap, JScrollPane scroller)
102 scrollPane = scroller;
103 addMouseListener(this);
104 addMouseMotionListener(this);
105 ToolTipManager.sharedInstance().registerComponent(this);
114 public void treeSelectionChanged(SequenceI sequence)
116 AlignmentPanel[] aps = getAssociatedPanels();
118 for (int a = 0; a < aps.length; a++)
120 SequenceGroup selected = aps[a].av.getSelectionGroup();
122 if (selected == null)
124 selected = new SequenceGroup();
125 aps[a].av.setSelectionGroup(selected);
128 selected.setEndRes(aps[a].av.getAlignment().getWidth() - 1);
129 selected.addOrRemove(sequence, true);
139 public void setTree(NJTree tree)
142 tree.findHeight(tree.getTopNode());
144 // Now have to calculate longest name based on the leaves
145 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
146 boolean has_placeholders = false;
149 for (int i = 0; i < leaves.size(); i++)
151 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
153 if (lf.isPlaceholder())
155 has_placeholders = true;
158 if (longestName.length() < ((Sequence) lf.element()).getName()
161 longestName = TreeCanvas.PLACEHOLDER
162 + ((Sequence) lf.element()).getName();
166 setMarkPlaceholders(has_placeholders);
187 public void drawNode(Graphics g, SequenceNode node, float chunk,
188 float scale, int width, int offx, int offy)
195 if ((node.left() == null) && (node.right() == null))
198 float height = node.height;
199 float dist = node.dist;
201 int xstart = (int) ((height - dist) * scale) + offx;
202 int xend = (int) (height * scale) + offx;
204 int ypos = (int) (node.ycount * chunk) + offy;
206 if (node.element() instanceof SequenceI)
208 SequenceI seq = (SequenceI) ((SequenceNode) node).element();
210 if (av.getSequenceColour(seq) == Color.white)
212 g.setColor(Color.black);
216 g.setColor(av.getSequenceColour(seq).darker());
221 g.setColor(Color.black);
224 // Draw horizontal line
225 g.drawLine(xstart, ypos, xend, ypos);
227 String nodeLabel = "";
229 if (showDistances && (node.dist > 0))
231 nodeLabel = new Format("%-.2f").form(node.dist);
234 if (showBootstrap && node.bootstrap > -1)
238 nodeLabel = nodeLabel + " : ";
241 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
244 if (!nodeLabel.equals(""))
246 g.drawString(nodeLabel, xstart + 2, ypos - 2);
249 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
250 .getName()) : node.getName();
252 int charWidth = fm.stringWidth(name) + 3;
253 int charHeight = font.getSize();
255 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
256 charWidth, charHeight);
258 nameHash.put((SequenceI) node.element(), rect);
260 // Colour selected leaves differently
261 SequenceGroup selected = av.getSelectionGroup();
263 if ((selected != null)
264 && selected.getSequences(null).contains(
265 (SequenceI) node.element()))
267 g.setColor(Color.gray);
269 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
270 g.setColor(Color.white);
273 g.drawString(name, xend + 10, ypos + fm.getDescent());
274 g.setColor(Color.black);
278 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
280 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
283 float height = node.height;
284 float dist = node.dist;
286 int xstart = (int) ((height - dist) * scale) + offx;
287 int xend = (int) (height * scale) + offx;
288 int ypos = (int) (node.ycount * chunk) + offy;
290 g.setColor(((SequenceNode) node).color.darker());
292 // Draw horizontal line
293 g.drawLine(xstart, ypos, xend, ypos);
294 if (node == highlightNode)
296 g.fillRect(xend - 3, ypos - 3, 6, 6);
300 g.fillRect(xend - 2, ypos - 2, 4, 4);
303 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
305 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
308 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
309 nodeHash.put(node, pos);
311 g.drawLine((int) (height * scale) + offx, ystart,
312 (int) (height * scale) + offx, yend);
314 String nodeLabel = "";
316 if (showDistances && (node.dist > 0))
318 nodeLabel = new Format("%-.2f").form(node.dist);
321 if (showBootstrap && node.bootstrap > -1)
325 nodeLabel = nodeLabel + " : ";
328 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
331 if (!nodeLabel.equals(""))
333 g.drawString(nodeLabel, xstart + 2, ypos - 2);
346 * @return DOCUMENT ME!
348 public Object findElement(int x, int y)
350 Enumeration keys = nameHash.keys();
352 while (keys.hasMoreElements())
354 Object ob = keys.nextElement();
355 Rectangle rect = (Rectangle) nameHash.get(ob);
357 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
358 && (y <= (rect.y + rect.height)))
364 keys = nodeHash.keys();
366 while (keys.hasMoreElements())
368 Object ob = keys.nextElement();
369 Rectangle rect = (Rectangle) nodeHash.get(ob);
371 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
372 && (y <= (rect.y + rect.height)))
387 public void pickNodes(Rectangle pickBox)
389 int width = getWidth();
390 int height = getHeight();
392 SequenceNode top = tree.getTopNode();
394 float wscale = (float) ((width * .8) - (offx * 2))
395 / tree.getMaxHeight();
399 top.count = ((SequenceNode) top.left()).count
400 + ((SequenceNode) top.right()).count;
403 float chunk = (float) (height - (offy)) / top.count;
405 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
426 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
427 float scale, int width, int offx, int offy)
434 if ((node.left() == null) && (node.right() == null))
436 float height = node.height;
437 float dist = node.dist;
439 int xstart = (int) ((height - dist) * scale) + offx;
440 int xend = (int) (height * scale) + offx;
442 int ypos = (int) (node.ycount * chunk) + offy;
444 if (pickBox.contains(new Point(xend, ypos)))
446 if (node.element() instanceof SequenceI)
448 SequenceI seq = (SequenceI) node.element();
449 SequenceGroup sg = av.getSelectionGroup();
453 sg.addOrRemove(seq, true);
460 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
462 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
475 public void setColor(SequenceNode node, Color c)
482 if ((node.left() == null) && (node.right() == null)) // TODO: internal node
486 if (node.element() instanceof SequenceI)
488 AlignmentPanel[] aps = getAssociatedPanels();
491 for (int a = 0; a < aps.length; a++)
493 aps[a].av.setSequenceColour((SequenceI) node.element(), c);
501 setColor((SequenceNode) node.left(), c);
502 setColor((SequenceNode) node.right(), c);
511 Thread thread = new Thread(this);
515 // put printing in a thread to avoid painting problems
518 PrinterJob printJob = PrinterJob.getPrinterJob();
519 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
521 printJob.setPrintable(this, pf);
523 if (printJob.printDialog())
528 } catch (Exception PrintException)
530 PrintException.printStackTrace();
545 * @return DOCUMENT ME!
547 * @throws PrinterException
550 public int print(Graphics pg, PageFormat pf, int pi)
551 throws PrinterException
554 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
556 int pwidth = (int) pf.getImageableWidth();
557 int pheight = (int) pf.getImageableHeight();
559 int noPages = getHeight() / pheight;
563 return Printable.NO_SUCH_PAGE;
566 if (pwidth > getWidth())
573 if (pheight > getHeight())
575 pheight = getHeight();
582 FontMetrics fm = pg.getFontMetrics(font);
583 int height = fm.getHeight() * nameHash.size();
584 pg.translate(0, -pi * pheight);
585 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
587 // translate number of pages,
588 // height is screen size as this is the
589 // non overlapping text size
593 draw(pg, pwidth, pheight);
595 return Printable.PAGE_EXISTS;
604 public void paintComponent(Graphics g)
606 super.paintComponent(g);
611 g.drawString("Calculating tree....", 20, getHeight() / 2);
615 fm = g.getFontMetrics(font);
617 if (nameHash.size() == 0)
623 || (!fitToWindow && (scrollPane.getHeight() > ((fm
624 .getHeight() * nameHash.size()) + offy))))
626 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
627 setPreferredSize(null);
631 setPreferredSize(new Dimension(scrollPane.getWidth(),
632 fm.getHeight() * nameHash.size()));
633 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
636 scrollPane.revalidate();
646 public void setFont(Font font)
662 public void draw(Graphics g1, int width, int height)
664 Graphics2D g2 = (Graphics2D) g1;
665 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
666 RenderingHints.VALUE_ANTIALIAS_ON);
667 g2.setColor(Color.white);
668 g2.fillRect(0, 0, width, height);
672 offy = font.getSize() + 10;
674 fm = g2.getFontMetrics(font);
676 labelLength = fm.stringWidth(longestName) + 20; // 20 allows for scrollbar
678 float wscale = (float) (width - labelLength - (offx * 2))
679 / tree.getMaxHeight();
681 SequenceNode top = tree.getTopNode();
685 top.count = ((SequenceNode) top.left()).count
686 + ((SequenceNode) top.right()).count;
689 float chunk = (float) (height - (offy)) / top.count;
691 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
695 if (av.getCurrentTree() == tree)
697 g2.setColor(Color.red);
701 g2.setColor(Color.gray);
704 int x = (int) ((threshold * (float) (getWidth() - labelLength - (2 * offx))) + offx);
706 g2.drawLine(x, 0, x, getHeight());
716 public void mouseReleased(MouseEvent e)
726 public void mouseEntered(MouseEvent e)
736 public void mouseExited(MouseEvent e)
746 public void mouseClicked(MouseEvent evt)
748 if (highlightNode != null)
750 if (SwingUtilities.isRightMouseButton(evt))
752 Color col = JColorChooser.showDialog(this,
753 "Select Sub-Tree Colour", highlightNode.color);
756 setColor(highlightNode, col);
759 else if (evt.getClickCount() > 1)
761 tree.swapNodes(highlightNode);
762 tree.reCount(tree.getTopNode());
763 tree.findHeight(tree.getTopNode());
767 Vector leaves = new Vector();
768 tree.findLeaves(highlightNode, leaves);
770 for (int i = 0; i < leaves.size(); i++)
772 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
774 treeSelectionChanged(seq);
779 PaintRefresher.Refresh(tp, av.getSequenceSetId());
784 public void mouseMoved(MouseEvent evt)
786 av.setCurrentTree(tree);
788 Object ob = findElement(evt.getX(), evt.getY());
790 if (ob instanceof SequenceNode)
792 highlightNode = (SequenceNode) ob;
793 this.setToolTipText("<html>Left click to select leaves"
794 + "<br>Double-click to invert leaves"
795 + "<br>Right click to change colour");
801 if (highlightNode != null)
803 highlightNode = null;
804 setToolTipText(null);
810 public void mouseDragged(MouseEvent ect)
820 public void mousePressed(MouseEvent e)
822 av.setCurrentTree(tree);
827 Object ob = findElement(x, y);
829 if (ob instanceof SequenceI)
831 treeSelectionChanged((Sequence) ob);
832 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
837 else if (!(ob instanceof SequenceNode))
840 if (tree.getMaxHeight() != 0)
842 threshold = (float) (x - offx)
843 / (float) (getWidth() - labelLength - (2 * offx));
845 tree.getGroups().removeAllElements();
846 tree.groupNodes(tree.getTopNode(), threshold);
847 setColor(tree.getTopNode(), Color.black);
849 AlignmentPanel[] aps = getAssociatedPanels();
851 for (int a = 0; a < aps.length; a++)
853 aps[a].av.setSelectionGroup(null);
854 aps[a].av.getAlignment().deleteAllGroups();
855 aps[a].av.sequenceColours = null;
860 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
868 AlignmentPanel[] aps = getAssociatedPanels();
869 for (int i = 0; i < tree.getGroups().size(); i++)
871 Color col = new Color((int) (Math.random() * 255),
872 (int) (Math.random() * 255), (int) (Math.random() * 255));
873 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
875 Vector l = tree.findLeaves(
876 (SequenceNode) tree.getGroups().elementAt(i), new Vector());
878 Vector sequences = new Vector();
880 for (int j = 0; j < l.size(); j++)
882 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
885 if (!sequences.contains(s1))
887 sequences.addElement(s1);
891 ColourSchemeI cs = null;
892 SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
893 false, 0, av.getAlignment().getWidth() - 1);
895 if (av.getGlobalColourScheme() != null)
897 if (av.getGlobalColourScheme() instanceof UserColourScheme)
899 cs = new UserColourScheme(
900 ((UserColourScheme) av.getGlobalColourScheme())
906 cs = ColourSchemeProperty.getColour(sg, ColourSchemeProperty
907 .getColourName(av.getGlobalColourScheme()));
909 // cs is null if shading is an annotationColourGradient
912 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
913 av.getIgnoreGapsConsensus());
917 // sg.recalcConservation();
918 sg.setName("JTreeGroup:" + sg.hashCode());
920 for (int a = 0; a < aps.length; a++)
922 if (aps[a].av.getGlobalColourScheme() != null
923 && aps[a].av.getGlobalColourScheme().conservationApplied())
925 Conservation c = new Conservation("Group",
926 ResidueProperties.propHash, 3, sg.getSequences(null),
927 sg.getStartRes(), sg.getEndRes());
930 c.verdict(false, aps[a].av.getConsPercGaps());
931 sg.cs.setConservation(c);
934 aps[a].av.getAlignment().addGroup(new SequenceGroup(sg));
937 // notify the panel to redo any group specific stuff.
938 for (int a = 0; a < aps.length; a++)
940 aps[a].updateAnnotation();
941 // TODO: JAL-868 - need to ensure view colour change message is broadcast
942 // to any Jmols listening in
953 public void setShowDistances(boolean state)
955 this.showDistances = state;
965 public void setShowBootstrap(boolean state)
967 this.showBootstrap = state;
977 public void setMarkPlaceholders(boolean state)
979 this.markPlaceholders = state;
983 AlignmentPanel[] getAssociatedPanels()
987 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
991 return new AlignmentPanel[]