2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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.alignment.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
253 int charWidth = fm.stringWidth(name) + 3;
254 int charHeight = font.getSize();
256 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
257 charWidth, charHeight);
259 nameHash.put((SequenceI) node.element(), rect);
261 // Colour selected leaves differently
262 SequenceGroup selected = av.getSelectionGroup();
264 if ((selected != null)
265 && selected.getSequences(null).contains(
266 (SequenceI) node.element()))
268 g.setColor(Color.gray);
270 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth, charHeight);
271 g.setColor(Color.white);
274 g.drawString(name, xend + 10, ypos + fm.getDescent());
275 g.setColor(Color.black);
279 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
281 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
284 float height = node.height;
285 float dist = node.dist;
287 int xstart = (int) ((height - dist) * scale) + offx;
288 int xend = (int) (height * scale) + offx;
289 int ypos = (int) (node.ycount * chunk) + offy;
291 g.setColor(((SequenceNode) node).color.darker());
293 // Draw horizontal line
294 g.drawLine(xstart, ypos, xend, ypos);
295 if (node == highlightNode)
297 g.fillRect(xend - 3, ypos - 3, 6, 6);
301 g.fillRect(xend - 2, ypos - 2, 4, 4);
304 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk)
306 int yend = (int) (((SequenceNode) node.right()).ycount * chunk)
309 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
310 nodeHash.put(node, pos);
312 g.drawLine((int) (height * scale) + offx, ystart,
313 (int) (height * scale) + offx, yend);
315 String nodeLabel = "";
317 if (showDistances && (node.dist > 0))
319 nodeLabel = new Format("%-.2f").form(node.dist);
322 if (showBootstrap && node.bootstrap > -1)
326 nodeLabel = nodeLabel + " : ";
329 nodeLabel = nodeLabel + String.valueOf(node.bootstrap);
332 if (!nodeLabel.equals(""))
334 g.drawString(nodeLabel, xstart + 2, ypos - 2);
347 * @return DOCUMENT ME!
349 public Object findElement(int x, int y)
351 Enumeration keys = nameHash.keys();
353 while (keys.hasMoreElements())
355 Object ob = keys.nextElement();
356 Rectangle rect = (Rectangle) nameHash.get(ob);
358 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
359 && (y <= (rect.y + rect.height)))
365 keys = nodeHash.keys();
367 while (keys.hasMoreElements())
369 Object ob = keys.nextElement();
370 Rectangle rect = (Rectangle) nodeHash.get(ob);
372 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y)
373 && (y <= (rect.y + rect.height)))
388 public void pickNodes(Rectangle pickBox)
390 int width = getWidth();
391 int height = getHeight();
393 SequenceNode top = tree.getTopNode();
395 float wscale = (float) ((width * .8) - (offx * 2))
396 / tree.getMaxHeight();
400 top.count = ((SequenceNode) top.left()).count
401 + ((SequenceNode) top.right()).count;
404 float chunk = (float) (height - (offy)) / top.count;
406 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
427 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
428 float scale, int width, int offx, int offy)
435 if ((node.left() == null) && (node.right() == null))
437 float height = node.height;
438 float dist = node.dist;
440 int xstart = (int) ((height - dist) * scale) + offx;
441 int xend = (int) (height * scale) + offx;
443 int ypos = (int) (node.ycount * chunk) + offy;
445 if (pickBox.contains(new Point(xend, ypos)))
447 if (node.element() instanceof SequenceI)
449 SequenceI seq = (SequenceI) node.element();
450 SequenceGroup sg = av.getSelectionGroup();
454 sg.addOrRemove(seq, true);
461 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
463 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
476 public void setColor(SequenceNode node, Color c)
483 if ((node.left() == null) && (node.right() == null)) // TODO: internal node
487 if (node.element() instanceof SequenceI)
489 AlignmentPanel[] aps = getAssociatedPanels();
490 for (int a = 0; a < aps.length; a++)
492 aps[a].av.setSequenceColour((SequenceI) node.element(), c);
499 setColor((SequenceNode) node.left(), c);
500 setColor((SequenceNode) node.right(), c);
509 Thread thread = new Thread(this);
513 // put printing in a thread to avoid painting problems
516 PrinterJob printJob = PrinterJob.getPrinterJob();
517 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
519 printJob.setPrintable(this, pf);
521 if (printJob.printDialog())
526 } catch (Exception PrintException)
528 PrintException.printStackTrace();
543 * @return DOCUMENT ME!
545 * @throws PrinterException
548 public int print(Graphics pg, PageFormat pf, int pi)
549 throws PrinterException
552 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
554 int pwidth = (int) pf.getImageableWidth();
555 int pheight = (int) pf.getImageableHeight();
557 int noPages = getHeight() / pheight;
561 return Printable.NO_SUCH_PAGE;
564 if (pwidth > getWidth())
571 if (pheight > getHeight())
573 pheight = getHeight();
580 FontMetrics fm = pg.getFontMetrics(font);
581 int height = fm.getHeight() * nameHash.size();
582 pg.translate(0, -pi * pheight);
583 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
585 // translate number of pages,
586 // height is screen size as this is the
587 // non overlapping text size
591 draw(pg, pwidth, pheight);
593 return Printable.PAGE_EXISTS;
602 public void paintComponent(Graphics g)
604 super.paintComponent(g);
609 g.drawString("Calculating tree....", 20, getHeight() / 2);
613 fm = g.getFontMetrics(font);
615 if (nameHash.size() == 0)
621 || (!fitToWindow && (scrollPane.getHeight() > ((fm
622 .getHeight() * nameHash.size()) + offy))))
624 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
625 setPreferredSize(null);
629 setPreferredSize(new Dimension(scrollPane.getWidth(), fm
632 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
635 scrollPane.revalidate();
645 public void setFont(Font font)
661 public void draw(Graphics g1, int width, int height)
663 Graphics2D g2 = (Graphics2D) g1;
664 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
665 RenderingHints.VALUE_ANTIALIAS_ON);
666 g2.setColor(Color.white);
667 g2.fillRect(0, 0, width, height);
671 offy = font.getSize() + 10;
673 fm = g2.getFontMetrics(font);
675 labelLength = fm.stringWidth(longestName) + 20; // 20 allows for scrollbar
677 float wscale = (float) (width - labelLength - (offx * 2))
678 / tree.getMaxHeight();
680 SequenceNode top = tree.getTopNode();
684 top.count = ((SequenceNode) top.left()).count
685 + ((SequenceNode) top.right()).count;
688 float chunk = (float) (height - (offy)) / top.count;
690 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
694 if (av.getCurrentTree() == tree)
696 g2.setColor(Color.red);
700 g2.setColor(Color.gray);
703 int x = (int) ((threshold * (float) (getWidth() - labelLength - (2 * offx))) + offx);
705 g2.drawLine(x, 0, x, getHeight());
715 public void mouseReleased(MouseEvent e)
725 public void mouseEntered(MouseEvent e)
735 public void mouseExited(MouseEvent e)
745 public void mouseClicked(MouseEvent evt)
747 if (highlightNode != null)
749 if (SwingUtilities.isRightMouseButton(evt))
751 Color col = JColorChooser.showDialog(this,
752 "Select Sub-Tree Colour", highlightNode.color);
755 setColor(highlightNode, col);
758 else if (evt.getClickCount() > 1)
760 tree.swapNodes(highlightNode);
761 tree.reCount(tree.getTopNode());
762 tree.findHeight(tree.getTopNode());
766 Vector leaves = new Vector();
767 tree.findLeaves(highlightNode, leaves);
769 for (int i = 0; i < leaves.size(); i++)
771 SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
773 treeSelectionChanged(seq);
778 PaintRefresher.Refresh(tp, av.getSequenceSetId());
783 public void mouseMoved(MouseEvent evt)
785 av.setCurrentTree(tree);
787 Object ob = findElement(evt.getX(), evt.getY());
789 if (ob instanceof SequenceNode)
791 highlightNode = (SequenceNode) ob;
792 this.setToolTipText("<html>Left click to select leaves"
793 + "<br>Double-click to invert leaves"
794 + "<br>Right click to change colour");
800 if (highlightNode != null)
802 highlightNode = null;
803 setToolTipText(null);
809 public void mouseDragged(MouseEvent ect)
819 public void mousePressed(MouseEvent e)
821 av.setCurrentTree(tree);
826 Object ob = findElement(x, y);
828 if (ob instanceof SequenceI)
830 treeSelectionChanged((Sequence) ob);
831 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
836 else if (!(ob instanceof SequenceNode))
839 if (tree.getMaxHeight() != 0)
841 threshold = (float) (x - offx)
842 / (float) (getWidth() - labelLength - (2 * offx));
844 tree.getGroups().removeAllElements();
845 tree.groupNodes(tree.getTopNode(), threshold);
846 setColor(tree.getTopNode(), Color.black);
848 AlignmentPanel[] aps = getAssociatedPanels();
850 for (int a = 0; a < aps.length; a++)
852 aps[a].av.setSelectionGroup(null);
853 aps[a].av.alignment.deleteAllGroups();
854 aps[a].av.sequenceColours = null;
859 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
867 for (int i = 0; i < tree.getGroups().size(); i++)
869 Color col = new Color((int) (Math.random() * 255), (int) (Math
870 .random() * 255), (int) (Math.random() * 255));
871 setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
873 Vector l = tree.findLeaves((SequenceNode) tree.getGroups().elementAt(
876 Vector sequences = new Vector();
878 for (int j = 0; j < l.size(); j++)
880 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
883 if (!sequences.contains(s1))
885 sequences.addElement(s1);
889 ColourSchemeI cs = null;
891 if (av.getGlobalColourScheme() != null)
893 if (av.getGlobalColourScheme() instanceof UserColourScheme)
895 cs = new UserColourScheme(((UserColourScheme) av
896 .getGlobalColourScheme()).getColours());
901 cs = ColourSchemeProperty.getColour(sequences, av.alignment
902 .getWidth(), ColourSchemeProperty.getColourName(av
903 .getGlobalColourScheme()));
906 cs.setThreshold(av.getGlobalColourScheme().getThreshold(), av
907 .getIgnoreGapsConsensus());
910 SequenceGroup sg = new SequenceGroup(sequences, null, cs, true, true,
911 false, 0, av.alignment.getWidth() - 1);
913 sg.setName("JTreeGroup:" + sg.hashCode());
915 AlignmentPanel[] aps = getAssociatedPanels();
916 for (int a = 0; a < aps.length; a++)
918 if (aps[a].av.getGlobalColourScheme() != null
919 && aps[a].av.getGlobalColourScheme().conservationApplied())
921 Conservation c = new Conservation("Group",
922 ResidueProperties.propHash, 3, sg.getSequences(null), sg
923 .getStartRes(), sg.getEndRes());
926 c.verdict(false, aps[a].av.ConsPercGaps);
927 sg.cs.setConservation(c);
930 aps[a].av.alignment.addGroup(sg);
942 public void setShowDistances(boolean state)
944 this.showDistances = state;
954 public void setShowBootstrap(boolean state)
956 this.showBootstrap = state;
966 public void setMarkPlaceholders(boolean state)
968 this.markPlaceholders = state;
972 AlignmentPanel[] getAssociatedPanels()
976 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
980 return new AlignmentPanel[]