2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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
21 import jalview.analysis.*;
23 import jalview.datamodel.*;
25 import jalview.schemes.*;
27 import jalview.util.*;
30 import java.awt.event.*;
31 import java.awt.print.*;
44 public class TreeCanvas extends JPanel implements MouseListener, Runnable,
45 Printable, MouseMotionListener
48 public static final String PLACEHOLDER = " * ";
50 JScrollPane scrollPane;
55 boolean fitToWindow = true;
56 boolean showDistances = false;
57 boolean showBootstrap = false;
58 boolean markPlaceholders = false;
65 Hashtable nameHash = new Hashtable();
66 Hashtable nodeHash = new Hashtable();
67 SequenceNode highlightNode;
70 * Creates a new TreeCanvas object.
72 * @param av DOCUMENT ME!
73 * @param tree DOCUMENT ME!
74 * @param scroller DOCUMENT ME!
75 * @param label DOCUMENT ME!
77 public TreeCanvas(AlignmentPanel ap, JScrollPane scroller)
82 scrollPane = scroller;
83 addMouseListener(this);
84 addMouseMotionListener(this);
85 PaintRefresher.Register(this, ap.av.getSequenceSetId());
86 ToolTipManager.sharedInstance().registerComponent(this);
93 * @param sequence DOCUMENT ME!
95 public void treeSelectionChanged(SequenceI sequence)
97 SequenceGroup selected = av.getSelectionGroup();
101 selected = new SequenceGroup();
102 av.setSelectionGroup(selected);
105 selected.setEndRes(av.alignment.getWidth()-1);
106 selected.addOrRemove(sequence, true);
112 * @param tree DOCUMENT ME!
114 public void setTree(NJTree tree)
117 tree.findHeight(tree.getTopNode());
119 // Now have to calculate longest name based on the leaves
120 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
121 boolean has_placeholders = false;
124 for (int i = 0; i < leaves.size(); i++)
126 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
128 if (lf.isPlaceholder())
130 has_placeholders = true;
133 if (longestName.length() < ( (Sequence) lf.element()).getName()
136 longestName = TreeCanvas.PLACEHOLDER +
137 ( (Sequence) lf.element()).getName();
141 setMarkPlaceholders(has_placeholders);
147 * @param g DOCUMENT ME!
148 * @param node DOCUMENT ME!
149 * @param chunk DOCUMENT ME!
150 * @param scale DOCUMENT ME!
151 * @param width DOCUMENT ME!
152 * @param offx DOCUMENT ME!
153 * @param offy DOCUMENT ME!
155 public void drawNode(Graphics g, SequenceNode node, float chunk,
156 float scale, int width, int offx, int offy)
163 if ((node.left() == null) && (node.right() == null))
166 float height = node.height;
167 float dist = node.dist;
169 int xstart = (int) ((height - dist) * scale) + offx;
170 int xend = (int) (height * scale) + offx;
172 int ypos = (int) (node.ycount * chunk) + offy;
174 if (node.element() instanceof SequenceI)
176 SequenceI seq = (SequenceI)((SequenceNode) node).element();
178 if (av.getSequenceColour(seq) == Color.white)
180 g.setColor(Color.black);
184 g.setColor(av.getSequenceColour(seq).darker());
189 g.setColor(Color.black);
192 // Draw horizontal line
193 g.drawLine(xstart, ypos, xend, ypos);
195 String nodeLabel = "";
197 if (showDistances && (node.dist > 0))
199 nodeLabel = new Format("%-.2f").form(node.dist);
206 nodeLabel = nodeLabel + " : ";
209 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
212 if (!nodeLabel.equals(""))
214 g.drawString(nodeLabel, xstart+2, ypos - 2);
217 String name = (markPlaceholders && node.isPlaceholder())
218 ? (PLACEHOLDER + node.getName()) : node.getName();
220 int charWidth = fm.stringWidth(name) + 3;
221 int charHeight = font.getSize();
223 Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2,
224 charWidth, charHeight);
226 nameHash.put((SequenceI) node.element(), rect);
228 // Colour selected leaves differently
229 SequenceGroup selected = av.getSelectionGroup();
231 if ((selected != null) &&
232 selected.getSequences(false).contains((SequenceI) node.element()))
234 g.setColor(Color.gray);
236 g.fillRect(xend + 10, ypos-charHeight/2, charWidth,
238 g.setColor(Color.white);
241 g.drawString(name, xend + 10, ypos+fm.getDescent());
242 g.setColor(Color.black);
246 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
248 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
251 float height = node.height;
252 float dist = node.dist;
254 int xstart = (int) ((height - dist) * scale) + offx;
255 int xend = (int) (height * scale) + offx;
256 int ypos = (int) (node.ycount * chunk) + offy;
258 g.setColor(((SequenceNode) node).color.darker());
260 // Draw horizontal line
261 g.drawLine(xstart, ypos, xend, ypos);
262 if (node == highlightNode)
263 g.fillRect(xend - 3, ypos - 3, 6, 6);
265 g.fillRect(xend - 2, ypos - 2, 4, 4);
267 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk) +
269 int yend = (int) (((SequenceNode) node.right()).ycount * chunk) +
272 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
273 nodeHash.put(node, pos);
275 g.drawLine((int) (height * scale) + offx, ystart,
276 (int) (height * scale) + offx, yend);
278 if (showDistances && (node.dist > 0))
280 g.drawString(new Format("%-.2f").form(node.dist).trim(), xstart+2,
289 * @param x DOCUMENT ME!
290 * @param y DOCUMENT ME!
292 * @return DOCUMENT ME!
294 public Object findElement(int x, int y)
296 Enumeration keys = nameHash.keys();
298 while (keys.hasMoreElements())
300 Object ob = keys.nextElement();
301 Rectangle rect = (Rectangle) nameHash.get(ob);
303 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
304 (y <= (rect.y + rect.height)))
310 keys = nodeHash.keys();
312 while (keys.hasMoreElements())
314 Object ob = keys.nextElement();
315 Rectangle rect = (Rectangle) nodeHash.get(ob);
317 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
318 (y <= (rect.y + rect.height)))
330 * @param pickBox DOCUMENT ME!
332 public void pickNodes(Rectangle pickBox)
334 int width = getWidth();
335 int height = getHeight();
337 SequenceNode top = tree.getTopNode();
339 float wscale = (float) ((width * .8) - (offx * 2)) / tree.getMaxHeight();
343 top.count = ((SequenceNode) top.left()).count +
344 ((SequenceNode) top.right()).count;
347 float chunk = (float) (height - (offy)) / top.count;
349 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
355 * @param pickBox DOCUMENT ME!
356 * @param node DOCUMENT ME!
357 * @param chunk DOCUMENT ME!
358 * @param scale DOCUMENT ME!
359 * @param width DOCUMENT ME!
360 * @param offx DOCUMENT ME!
361 * @param offy DOCUMENT ME!
363 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
364 float scale, int width, int offx, int offy)
371 if ((node.left() == null) && (node.right() == null))
373 float height = node.height;
374 float dist = node.dist;
376 int xstart = (int) ((height - dist) * scale) + offx;
377 int xend = (int) (height * scale) + offx;
379 int ypos = (int) (node.ycount * chunk) + offy;
381 if (pickBox.contains(new Point(xend, ypos)))
383 if (node.element() instanceof SequenceI)
385 SequenceI seq = (SequenceI) node.element();
386 SequenceGroup sg = av.getSelectionGroup();
390 sg.addOrRemove(seq, true);
397 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
399 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
407 * @param node DOCUMENT ME!
408 * @param c DOCUMENT ME!
410 public void setColor(SequenceNode node, Color c)
417 if ((node.left() == null) && (node.right() == null))
421 if (node.element() instanceof SequenceI)
423 av.setSequenceColour((SequenceI) node.element(), c);
429 setColor((SequenceNode) node.left(), c);
430 setColor((SequenceNode) node.right(), c);
439 Thread thread = new Thread(this);
443 // put printing in a thread to avoid painting problems
446 PrinterJob printJob = PrinterJob.getPrinterJob();
447 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
449 printJob.setPrintable(this, pf);
451 if (printJob.printDialog())
457 catch (Exception PrintException)
459 PrintException.printStackTrace();
467 * @param pg DOCUMENT ME!
468 * @param pf DOCUMENT ME!
469 * @param pi DOCUMENT ME!
471 * @return DOCUMENT ME!
473 * @throws PrinterException DOCUMENT ME!
475 public int print(Graphics pg, PageFormat pf, int pi)
476 throws PrinterException
479 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
481 int pwidth = (int) pf.getImageableWidth();
482 int pheight = (int) pf.getImageableHeight();
484 int noPages = getHeight() / pheight;
488 return Printable.NO_SUCH_PAGE;
491 if (pwidth > getWidth())
498 if (pheight > getHeight())
500 pheight = getHeight();
507 FontMetrics fm = pg.getFontMetrics(font);
508 int height = fm.getHeight() * nameHash.size();
509 pg.translate(0, -pi * pheight);
510 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
512 // translate number of pages,
513 // height is screen size as this is the
514 // non overlapping text size
518 draw(pg, pwidth, pheight);
520 return Printable.PAGE_EXISTS;
526 * @param g DOCUMENT ME!
528 public void paintComponent(Graphics g)
530 super.paintComponent(g);
535 g.drawString("Calculating tree....", 20, getHeight()/2);
539 fm = g.getFontMetrics(font);
541 if (nameHash.size() == 0)
548 (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
551 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
552 setPreferredSize(null);
556 setPreferredSize(new Dimension(scrollPane.getWidth(),
557 fm.getHeight() * nameHash.size()));
558 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
561 scrollPane.revalidate();
569 * @param fontSize DOCUMENT ME!
571 public void setFont(Font font)
580 * @param g1 DOCUMENT ME!
581 * @param width DOCUMENT ME!
582 * @param height DOCUMENT ME!
584 public void draw(Graphics g1, int width, int height)
586 Graphics2D g2 = (Graphics2D) g1;
587 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
588 RenderingHints.VALUE_ANTIALIAS_ON);
589 g2.setColor(Color.white);
590 g2.fillRect(0, 0, width, height);
594 offy = font.getSize()+10;
596 fm = g2.getFontMetrics(font);
598 labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
600 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
602 SequenceNode top = tree.getTopNode();
606 top.count = ((SequenceNode) top.left()).count +
607 ((SequenceNode) top.right()).count;
610 float chunk = (float) (height - (offy)) / top.count;
612 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
616 if (av.getCurrentTree() == tree)
618 g2.setColor(Color.red);
622 g2.setColor(Color.gray);
625 int x = (int) ((threshold * (float) (getWidth() - labelLength -
626 (2 * offx))) + offx);
628 g2.drawLine(x, 0, x, getHeight());
635 * @param e DOCUMENT ME!
637 public void mouseReleased(MouseEvent e)
644 * @param e DOCUMENT ME!
646 public void mouseEntered(MouseEvent e)
653 * @param e DOCUMENT ME!
655 public void mouseExited(MouseEvent e)
662 * @param e DOCUMENT ME!
664 public void mouseClicked(MouseEvent evt)
666 if(highlightNode!=null)
668 if (SwingUtilities.isRightMouseButton(evt))
670 Color col = JColorChooser.showDialog(this, "Select Sub-Tree Colour",
671 highlightNode.color);
673 setColor(highlightNode, col);
676 if(evt.getClickCount()>1)
678 tree.swapNodes(highlightNode);
679 tree.reCount(tree.getTopNode());
680 tree.findHeight(tree.getTopNode());
684 Vector leaves = new Vector();
685 tree.findLeaves(highlightNode, leaves);
687 for (int i = 0; i < leaves.size(); i++)
690 (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element();
691 treeSelectionChanged(seq);
695 PaintRefresher.Refresh(this, av.getSequenceSetId());
702 public void mouseMoved(MouseEvent evt)
704 av.setCurrentTree(tree);
706 Object ob = findElement(evt.getX(), evt.getY());
708 if (ob instanceof SequenceNode)
710 highlightNode = (SequenceNode) ob;
712 "<html>Left click to select leaves"
713 + "<br>Double-click to invert leaves"
714 + "<br>Right click to change colour");
720 if (highlightNode != null)
722 highlightNode = null;
723 setToolTipText(null);
729 public void mouseDragged(MouseEvent ect)
735 * @param e DOCUMENT ME!
737 public void mousePressed(MouseEvent e)
739 av.setCurrentTree(tree);
744 Object ob = findElement(x, y);
746 if (ob instanceof SequenceI)
748 treeSelectionChanged( (Sequence) ob);
749 PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
753 else if( !(ob instanceof SequenceNode) )
756 if (tree.getMaxHeight() != 0)
758 threshold = (float) (x - offx) / (float) (getWidth() -
759 labelLength - (2 * offx));
761 tree.getGroups().removeAllElements();
762 tree.groupNodes(tree.getTopNode(), threshold);
763 setColor(tree.getTopNode(), Color.black);
765 av.setSelectionGroup(null);
766 av.alignment.deleteAllGroups();
767 av.sequenceColours=null;
772 PaintRefresher.Refresh(this, ap.av.getSequenceSetId());
781 for (int i = 0; i < tree.getGroups().size(); i++)
783 Color col = new Color( (int) (Math.random() * 255),
784 (int) (Math.random() * 255),
785 (int) (Math.random() * 255));
786 setColor( (SequenceNode) tree.getGroups().elementAt(i),
789 Vector l = tree.findLeaves( (SequenceNode) tree.getGroups()
793 Vector sequences = new Vector();
795 for (int j = 0; j < l.size(); j++)
797 SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
799 if (!sequences.contains(s1))
801 sequences.addElement(s1);
805 ColourSchemeI cs = null;
807 if (av.getGlobalColourScheme() != null)
809 if (av.getGlobalColourScheme() instanceof UserColourScheme)
811 cs = new UserColourScheme(
812 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
816 cs = ColourSchemeProperty.getColour(sequences,
817 av.alignment.getWidth(),
818 ColourSchemeProperty.
820 av.getGlobalColourScheme()));
822 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
823 av.getIgnoreGapsConsensus());
826 SequenceGroup sg = new SequenceGroup(sequences,
827 "TreeGroup", cs, true, true, false,
829 av.alignment.getWidth() - 1);
831 if (av.getGlobalColourScheme() != null
832 && av.getGlobalColourScheme().conservationApplied())
834 Conservation c = new Conservation("Group",
835 ResidueProperties.propHash, 3,
836 sg.getSequences(false),
837 sg.getStartRes(), sg.getEndRes());
840 c.verdict(false, av.ConsPercGaps);
841 sg.cs.setConservation(c);
844 av.alignment.addGroup(sg);
852 * @param state DOCUMENT ME!
854 public void setShowDistances(boolean state)
856 this.showDistances = state;
863 * @param state DOCUMENT ME!
865 public void setShowBootstrap(boolean state)
867 this.showBootstrap = state;
874 * @param state DOCUMENT ME!
876 public void setMarkPlaceholders(boolean state)
878 this.markPlaceholders = state;