2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
25 import java.awt.print.*;
28 import jalview.analysis.*;
29 import jalview.datamodel.*;
30 import jalview.schemes.*;
31 import jalview.util.*;
39 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
40 Printable, MouseMotionListener
43 public static final String PLACEHOLDER = " * ";
47 JScrollPane scrollPane;
59 boolean fitToWindow = true;
61 boolean showDistances = false;
63 boolean showBootstrap = false;
65 boolean markPlaceholders = false;
77 Hashtable nameHash = new Hashtable();
79 Hashtable nodeHash = new Hashtable();
81 SequenceNode highlightNode;
83 boolean applyToAllViews = false;
86 * Creates a new TreeCanvas object.
97 public TreeCanvas(TreePanel tp, AlignmentPanel ap, JScrollPane scroller)
103 scrollPane = scroller;
104 addMouseListener(this);
105 addMouseMotionListener(this);
106 ToolTipManager.sharedInstance().registerComponent(this);
115 public void treeSelectionChanged(SequenceI sequence)
117 AlignmentPanel[] aps = getAssociatedPanels();
119 for (int a = 0; a < aps.length; a++)
121 SequenceGroup selected = aps[a].av.getSelectionGroup();
123 if (selected == null)
125 selected = new SequenceGroup();
126 aps[a].av.setSelectionGroup(selected);
129 selected.setEndRes(aps[a].av.alignment.getWidth() - 1);
130 selected.addOrRemove(sequence, true);
140 public void setTree(NJTree tree)
143 tree.findHeight(tree.getTopNode());
145 // Now have to calculate longest name based on the leaves
146 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
147 boolean has_placeholders = false;
150 for (int i = 0; i < leaves.size(); i++)
152 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
154 if (lf.isPlaceholder())
156 has_placeholders = true;
159 if (longestName.length() < ((Sequence) lf.element()).getName()
162 longestName = TreeCanvas.PLACEHOLDER
163 + ((Sequence) lf.element()).getName();
167 setMarkPlaceholders(has_placeholders);
188 public void drawNode(Graphics g, SequenceNode node, float chunk,
189 float scale, int width, int offx, int offy)
196 if ((node.left() == null) && (node.right() == null))
199 float height = node.height;
200 float dist = node.dist;
202 int xstart = (int) ((height - dist) * scale) + offx;
203 int xend = (int) (height * scale) + offx;
205 int ypos = (int) (node.ycount * chunk) + offy;
207 if (node.element() instanceof SequenceI)
209 SequenceI seq = (SequenceI) ((SequenceNode) node).element();
211 if (av.getSequenceColour(seq) == Color.white)
213 g.setColor(Color.black);
217 g.setColor(av.getSequenceColour(seq).darker());
222 g.setColor(Color.black);
225 // Draw horizontal line
226 g.drawLine(xstart, ypos, xend, ypos);
228 String nodeLabel = "";
230 if (showDistances && (node.dist > 0))
232 nodeLabel = new Format("%-.2f").form(node.dist);
235 if (showBootstrap && node.bootstrap > -1)
239 nodeLabel = nodeLabel + " : ";
242 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
245 if (!nodeLabel.equals(""))
247 g.drawString(nodeLabel, xstart + 2, ypos - 2);
250 String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
254 int charWidth = fm.stringWidth(name) + 3;
255 int charHeight = font.getSize();
257 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
258 charWidth, charHeight);
260 nameHash.put((SequenceI) node.element(), rect);
262 // Colour selected leaves differently
263 SequenceGroup selected = av.getSelectionGroup();
265 if ((selected != null)
266 && selected.getSequences(null).contains(
267 (SequenceI) node.element()))
269 g.setColor(Color.gray);
271 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
272 g.setColor(Color.white);
275 g.drawString(name, xend + 10, ypos + fm.getDescent());
276 g.setColor(Color.black);
280 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
282 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
285 float height = node.height;
286 float dist = node.dist;
288 int xstart = (int) ((height - dist) * scale) + offx;
289 int xend = (int) (height * scale) + offx;
290 int ypos = (int) (node.ycount * chunk) + offy;
292 g.setColor(((SequenceNode) node).color.darker());
294 // Draw horizontal line
295 g.drawLine(xstart, ypos, xend, ypos);
296 if (node == highlightNode)
298 g.fillRect(xend - 3, ypos - 3, 6, 6);
302 g.fillRect(xend - 2, ypos - 2, 4, 4);
305 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
307 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
310 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
311 nodeHash.put(node, pos);
313 g.drawLine((int) (height * scale) + offx, ystart,
314 (int) (height * scale) + offx, yend);
316 String nodeLabel = "";
318 if (showDistances && (node.dist > 0))
320 nodeLabel = new Format("%-.2f").form(node.dist);
323 if (showBootstrap && node.bootstrap > -1)
327 nodeLabel = nodeLabel + " : ";
330 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
333 if (!nodeLabel.equals(""))
335 g.drawString(nodeLabel, xstart + 2, ypos - 2);
348 * @return DOCUMENT ME!
350 public Object findElement(int x, int y)
352 Enumeration keys = nameHash.keys();
354 while (keys.hasMoreElements())
356 Object ob = keys.nextElement();
357 Rectangle rect = (Rectangle) nameHash.get(ob);
359 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
360 && (y <= (rect.y + rect.height)))
366 keys = nodeHash.keys();
368 while (keys.hasMoreElements())
370 Object ob = keys.nextElement();
371 Rectangle rect = (Rectangle) nodeHash.get(ob);
373 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
374 && (y <= (rect.y + rect.height)))
389 public void pickNodes(Rectangle pickBox)
391 int width = getWidth();
392 int height = getHeight();
394 SequenceNode top = tree.getTopNode();
396 float wscale = (float) ((width * .8) - (offx * 2))
397 / tree.getMaxHeight();
401 top.count = ((SequenceNode) top.left()).count
402 + ((SequenceNode) top.right()).count;
405 float chunk = (float) (height - (offy)) / top.count;
407 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
428 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
429 float scale, int width, int offx, int offy)
436 if ((node.left() == null) && (node.right() == null))
438 float height = node.height;
439 float dist = node.dist;
441 int xstart = (int) ((height - dist) * scale) + offx;
442 int xend = (int) (height * scale) + offx;
444 int ypos = (int) (node.ycount * chunk) + offy;
446 if (pickBox.contains(new Point(xend, ypos)))
448 if (node.element() instanceof SequenceI)
450 SequenceI seq = (SequenceI) node.element();
451 SequenceGroup sg = av.getSelectionGroup();
455 sg.addOrRemove(seq, true);
462 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
464 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
477 public void setColor(SequenceNode node, Color c)
484 if ((node.left() == null) && (node.right() == null)) // TODO: internal node
488 if (node.element() instanceof SequenceI)
490 AlignmentPanel[] aps = getAssociatedPanels();
491 for (int a = 0; a < aps.length; a++)
493 aps[a].av.setSequenceColour((SequenceI) node.element(), c);
500 setColor((SequenceNode) node.left(), c);
501 setColor((SequenceNode) node.right(), c);
510 Thread thread = new Thread(this);
514 // put printing in a thread to avoid painting problems
517 PrinterJob printJob = PrinterJob.getPrinterJob();
518 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
520 printJob.setPrintable(this, pf);
522 if (printJob.printDialog())
527 } catch (Exception PrintException)
529 PrintException.printStackTrace();
544 * @return DOCUMENT ME!
546 * @throws PrinterException
549 public int print(Graphics pg, PageFormat pf, int pi)
550 throws PrinterException
553 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
555 int pwidth = (int) pf.getImageableWidth();
556 int pheight = (int) pf.getImageableHeight();
558 int noPages = getHeight() / pheight;
562 return Printable.NO_SUCH_PAGE;
565 if (pwidth > getWidth())
572 if (pheight > getHeight())
574 pheight = getHeight();
581 FontMetrics fm = pg.getFontMetrics(font);
582 int height = fm.getHeight() * nameHash.size();
583 pg.translate(0, -pi * pheight);
584 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
586 // translate number of pages,
587 // height is screen size as this is the
588 // non overlapping text size
592 draw(pg, pwidth, pheight);
594 return Printable.PAGE_EXISTS;
603 public void paintComponent(Graphics g)
605 super.paintComponent(g);
610 g.drawString("Calculating tree....", 20, getHeight() / 2);
614 fm = g.getFontMetrics(font);
616 if (nameHash.size() == 0)
622 || (!fitToWindow && (scrollPane.getHeight() > ((fm
623 .getHeight() * nameHash.size()) + offy))))
625 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
626 setPreferredSize(null);
630 setPreferredSize(new Dimension(scrollPane.getWidth(), fm
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.alignment.deleteAllGroups();
855 aps[a].av.sequenceColours = null;
860 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
868 for (int i = 0; i < tree.getGroups().size(); i++)
870 Color col = new Color((int) (Math.random() * 255), (int) (Math
871 .random() * 255), (int) (Math.random() * 255));
872 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
874 Vector l = tree.findLeaves((SequenceNode) tree.getGroups().elementAt(
877 Vector sequences = new Vector();
879 for (int j = 0; j < l.size(); j++)
881 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
884 if (!sequences.contains(s1))
886 sequences.addElement(s1);
890 ColourSchemeI cs = null;
892 if (av.getGlobalColourScheme() != null)
894 if (av.getGlobalColourScheme() instanceof UserColourScheme)
896 cs = new UserColourScheme(((UserColourScheme) av
897 .getGlobalColourScheme()).getColours());
902 cs = ColourSchemeProperty.getColour(sequences, av.alignment
903 .getWidth(), ColourSchemeProperty.getColourName(av
904 .getGlobalColourScheme()));
907 cs.setThreshold(av.getGlobalColourScheme().getThreshold(), av
908 .getIgnoreGapsConsensus());
911 SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
912 false, 0, av.alignment.getWidth() - 1);
914 sg.setName("JTreeGroup:" + sg.hashCode());
916 AlignmentPanel[] aps = getAssociatedPanels();
917 for (int a = 0; a < aps.length; a++)
919 if (aps[a].av.getGlobalColourScheme() != null
920 && aps[a].av.getGlobalColourScheme().conservationApplied())
922 Conservation c = new Conservation("Group",
923 ResidueProperties.propHash, 3, sg.getSequences(null), sg
924 .getStartRes(), sg.getEndRes());
927 c.verdict(false, aps[a].av.ConsPercGaps);
928 sg.cs.setConservation(c);
931 aps[a].av.alignment.addGroup(sg);
943 public void setShowDistances(boolean state)
945 this.showDistances = state;
955 public void setShowBootstrap(boolean state)
957 this.showBootstrap = state;
967 public void setMarkPlaceholders(boolean state)
969 this.markPlaceholders = state;
973 AlignmentPanel[] getAssociatedPanels()
977 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
981 return new AlignmentPanel[]