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;
56 boolean fitToWindow = true;
57 boolean showDistances = false;
58 boolean showBootstrap = false;
59 boolean markPlaceholders = false;
66 Hashtable nameHash = new Hashtable();
67 Hashtable nodeHash = new Hashtable();
68 SequenceNode highlightNode;
70 boolean applyToAllViews = false;
73 * Creates a new TreeCanvas object.
75 * @param av DOCUMENT ME!
76 * @param tree DOCUMENT ME!
77 * @param scroller DOCUMENT ME!
78 * @param label DOCUMENT ME!
80 public TreeCanvas(TreePanel tp,
88 scrollPane = scroller;
89 addMouseListener(this);
90 addMouseMotionListener(this);
91 ToolTipManager.sharedInstance().registerComponent(this);
97 * @param sequence DOCUMENT ME!
99 public void treeSelectionChanged(SequenceI sequence)
101 AlignmentPanel[] aps = getAssociatedPanels();
103 for (int a = 0; a < aps.length; a++)
105 SequenceGroup selected = aps[a].av.getSelectionGroup();
107 if (selected == null)
109 selected = new SequenceGroup();
110 aps[a].av.setSelectionGroup(selected);
113 selected.setEndRes(aps[a].av.alignment.getWidth() - 1);
114 selected.addOrRemove(sequence, true);
121 * @param tree DOCUMENT ME!
123 public void setTree(NJTree tree)
126 tree.findHeight(tree.getTopNode());
128 // Now have to calculate longest name based on the leaves
129 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
130 boolean has_placeholders = false;
133 for (int i = 0; i < leaves.size(); i++)
135 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
137 if (lf.isPlaceholder())
139 has_placeholders = true;
142 if (longestName.length() < ( (Sequence) lf.element()).getName()
145 longestName = TreeCanvas.PLACEHOLDER +
146 ( (Sequence) lf.element()).getName();
150 setMarkPlaceholders(has_placeholders);
156 * @param g DOCUMENT ME!
157 * @param node DOCUMENT ME!
158 * @param chunk DOCUMENT ME!
159 * @param scale DOCUMENT ME!
160 * @param width DOCUMENT ME!
161 * @param offx DOCUMENT ME!
162 * @param offy DOCUMENT ME!
164 public void drawNode(Graphics g, SequenceNode node, float chunk,
165 float scale, int width, int offx, int offy)
172 if ( (node.left() == null) && (node.right() == null))
175 float height = node.height;
176 float dist = node.dist;
178 int xstart = (int) ( (height - dist) * scale) + offx;
179 int xend = (int) (height * scale) + offx;
181 int ypos = (int) (node.ycount * chunk) + offy;
183 if (node.element() instanceof SequenceI)
185 SequenceI seq = (SequenceI) ( (SequenceNode) node).element();
187 if (av.getSequenceColour(seq) == Color.white)
189 g.setColor(Color.black);
193 g.setColor(av.getSequenceColour(seq).darker());
198 g.setColor(Color.black);
201 // Draw horizontal line
202 g.drawLine(xstart, ypos, xend, ypos);
204 String nodeLabel = "";
206 if (showDistances && (node.dist > 0))
208 nodeLabel = new Format("%-.2f").form(node.dist);
215 nodeLabel = nodeLabel + " : ";
218 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
221 if (!nodeLabel.equals(""))
223 g.drawString(nodeLabel, xstart + 2, ypos - 2);
226 String name = (markPlaceholders && node.isPlaceholder())
227 ? (PLACEHOLDER + node.getName()) : node.getName();
229 int charWidth = fm.stringWidth(name) + 3;
230 int charHeight = font.getSize();
232 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
233 charWidth, charHeight);
235 nameHash.put( (SequenceI) node.element(), rect);
237 // Colour selected leaves differently
238 SequenceGroup selected = av.getSelectionGroup();
240 if ( (selected != null) &&
241 selected.getSequences(false).contains( (SequenceI) node.element()))
243 g.setColor(Color.gray);
245 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth,
247 g.setColor(Color.white);
250 g.drawString(name, xend + 10, ypos + fm.getDescent());
251 g.setColor(Color.black);
255 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
257 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
260 float height = node.height;
261 float dist = node.dist;
263 int xstart = (int) ( (height - dist) * scale) + offx;
264 int xend = (int) (height * scale) + offx;
265 int ypos = (int) (node.ycount * chunk) + offy;
267 g.setColor( ( (SequenceNode) node).color.darker());
269 // Draw horizontal line
270 g.drawLine(xstart, ypos, xend, ypos);
271 if (node == highlightNode)
272 g.fillRect(xend - 3, ypos - 3, 6, 6);
274 g.fillRect(xend - 2, ypos - 2, 4, 4);
276 int ystart = (int) ( ( (SequenceNode) node.left()).ycount * chunk) +
278 int yend = (int) ( ( (SequenceNode) node.right()).ycount * chunk) +
281 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
282 nodeHash.put(node, pos);
284 g.drawLine( (int) (height * scale) + offx, ystart,
285 (int) (height * scale) + offx, yend);
287 if (showDistances && (node.dist > 0))
289 g.drawString(new Format("%-.2f").form(node.dist).trim(), xstart + 2,
298 * @param x DOCUMENT ME!
299 * @param y DOCUMENT ME!
301 * @return DOCUMENT ME!
303 public Object findElement(int x, int y)
305 Enumeration keys = nameHash.keys();
307 while (keys.hasMoreElements())
309 Object ob = keys.nextElement();
310 Rectangle rect = (Rectangle) nameHash.get(ob);
312 if ( (x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
313 (y <= (rect.y + rect.height)))
319 keys = nodeHash.keys();
321 while (keys.hasMoreElements())
323 Object ob = keys.nextElement();
324 Rectangle rect = (Rectangle) nodeHash.get(ob);
326 if ( (x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
327 (y <= (rect.y + rect.height)))
339 * @param pickBox DOCUMENT ME!
341 public void pickNodes(Rectangle pickBox)
343 int width = getWidth();
344 int height = getHeight();
346 SequenceNode top = tree.getTopNode();
348 float wscale = (float) ( (width * .8) - (offx * 2)) / tree.getMaxHeight();
352 top.count = ( (SequenceNode) top.left()).count +
353 ( (SequenceNode) top.right()).count;
356 float chunk = (float) (height - (offy)) / top.count;
358 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
364 * @param pickBox DOCUMENT ME!
365 * @param node DOCUMENT ME!
366 * @param chunk DOCUMENT ME!
367 * @param scale DOCUMENT ME!
368 * @param width DOCUMENT ME!
369 * @param offx DOCUMENT ME!
370 * @param offy DOCUMENT ME!
372 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
373 float scale, int width, int offx, int offy)
380 if ( (node.left() == null) && (node.right() == null))
382 float height = node.height;
383 float dist = node.dist;
385 int xstart = (int) ( (height - dist) * scale) + offx;
386 int xend = (int) (height * scale) + offx;
388 int ypos = (int) (node.ycount * chunk) + offy;
390 if (pickBox.contains(new Point(xend, ypos)))
392 if (node.element() instanceof SequenceI)
394 SequenceI seq = (SequenceI) node.element();
395 SequenceGroup sg = av.getSelectionGroup();
399 sg.addOrRemove(seq, true);
406 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
408 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
416 * @param node DOCUMENT ME!
417 * @param c DOCUMENT ME!
419 public void setColor(SequenceNode node, Color c)
426 if ( (node.left() == null) && (node.right() == null))
430 if (node.element() instanceof SequenceI)
432 AlignmentPanel[] aps = getAssociatedPanels();
433 for (int a = 0; a < aps.length; a++)
435 aps[a].av.setSequenceColour( (SequenceI) node.element(), c);
442 setColor( (SequenceNode) node.left(), c);
443 setColor( (SequenceNode) node.right(), c);
452 Thread thread = new Thread(this);
456 // put printing in a thread to avoid painting problems
459 PrinterJob printJob = PrinterJob.getPrinterJob();
460 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
462 printJob.setPrintable(this, pf);
464 if (printJob.printDialog())
470 catch (Exception PrintException)
472 PrintException.printStackTrace();
480 * @param pg DOCUMENT ME!
481 * @param pf DOCUMENT ME!
482 * @param pi DOCUMENT ME!
484 * @return DOCUMENT ME!
486 * @throws PrinterException DOCUMENT ME!
488 public int print(Graphics pg, PageFormat pf, int pi)
489 throws PrinterException
492 pg.translate( (int) pf.getImageableX(), (int) pf.getImageableY());
494 int pwidth = (int) pf.getImageableWidth();
495 int pheight = (int) pf.getImageableHeight();
497 int noPages = getHeight() / pheight;
501 return Printable.NO_SUCH_PAGE;
504 if (pwidth > getWidth())
511 if (pheight > getHeight())
513 pheight = getHeight();
520 FontMetrics fm = pg.getFontMetrics(font);
521 int height = fm.getHeight() * nameHash.size();
522 pg.translate(0, -pi * pheight);
523 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
525 // translate number of pages,
526 // height is screen size as this is the
527 // non overlapping text size
531 draw(pg, pwidth, pheight);
533 return Printable.PAGE_EXISTS;
539 * @param g DOCUMENT ME!
541 public void paintComponent(Graphics g)
543 super.paintComponent(g);
548 g.drawString("Calculating tree....", 20, getHeight() / 2);
552 fm = g.getFontMetrics(font);
554 if (nameHash.size() == 0)
561 (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
564 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
565 setPreferredSize(null);
569 setPreferredSize(new Dimension(scrollPane.getWidth(),
570 fm.getHeight() * nameHash.size()));
571 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
574 scrollPane.revalidate();
581 * @param fontSize DOCUMENT ME!
583 public void setFont(Font font)
592 * @param g1 DOCUMENT ME!
593 * @param width DOCUMENT ME!
594 * @param height DOCUMENT ME!
596 public void draw(Graphics g1, int width, int height)
598 Graphics2D g2 = (Graphics2D) g1;
599 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
600 RenderingHints.VALUE_ANTIALIAS_ON);
601 g2.setColor(Color.white);
602 g2.fillRect(0, 0, width, height);
606 offy = font.getSize() + 10;
608 fm = g2.getFontMetrics(font);
610 labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
612 float wscale = (float) (width - labelLength - (offx * 2)) /
615 SequenceNode top = tree.getTopNode();
619 top.count = ( (SequenceNode) top.left()).count +
620 ( (SequenceNode) top.right()).count;
623 float chunk = (float) (height - (offy)) / top.count;
625 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
629 if (av.getCurrentTree() == tree)
631 g2.setColor(Color.red);
635 g2.setColor(Color.gray);
638 int x = (int) ( (threshold * (float) (getWidth() - labelLength -
639 (2 * offx))) + offx);
641 g2.drawLine(x, 0, x, getHeight());
648 * @param e DOCUMENT ME!
650 public void mouseReleased(MouseEvent e)
657 * @param e DOCUMENT ME!
659 public void mouseEntered(MouseEvent e)
666 * @param e DOCUMENT ME!
668 public void mouseExited(MouseEvent e)
675 * @param e DOCUMENT ME!
677 public void mouseClicked(MouseEvent evt)
679 if (highlightNode != null)
681 if (SwingUtilities.isRightMouseButton(evt))
683 Color col = JColorChooser.showDialog(this, "Select Sub-Tree Colour",
684 highlightNode.color);
686 setColor(highlightNode, col);
689 if (evt.getClickCount() > 1)
691 tree.swapNodes(highlightNode);
692 tree.reCount(tree.getTopNode());
693 tree.findHeight(tree.getTopNode());
697 Vector leaves = new Vector();
698 tree.findLeaves(highlightNode, leaves);
700 for (int i = 0; i < leaves.size(); i++)
703 (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element();
704 treeSelectionChanged(seq);
708 PaintRefresher.Refresh(tp, av.getSequenceSetId());
713 public void mouseMoved(MouseEvent evt)
715 av.setCurrentTree(tree);
717 Object ob = findElement(evt.getX(), evt.getY());
719 if (ob instanceof SequenceNode)
721 highlightNode = (SequenceNode) ob;
723 "<html>Left click to select leaves"
724 + "<br>Double-click to invert leaves"
725 + "<br>Right click to change colour");
731 if (highlightNode != null)
733 highlightNode = null;
734 setToolTipText(null);
740 public void mouseDragged(MouseEvent ect)
746 * @param e DOCUMENT ME!
748 public void mousePressed(MouseEvent e)
750 av.setCurrentTree(tree);
755 Object ob = findElement(x, y);
757 if (ob instanceof SequenceI)
759 treeSelectionChanged( (Sequence) ob);
760 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
764 else if (! (ob instanceof SequenceNode))
767 if (tree.getMaxHeight() != 0)
769 threshold = (float) (x - offx) / (float) (getWidth() -
770 labelLength - (2 * offx));
772 tree.getGroups().removeAllElements();
773 tree.groupNodes(tree.getTopNode(), threshold);
774 setColor(tree.getTopNode(), Color.black);
776 AlignmentPanel[] aps = getAssociatedPanels();
778 for (int a = 0; a < aps.length; a++)
780 aps[a].av.setSelectionGroup(null);
781 aps[a].av.alignment.deleteAllGroups();
782 aps[a].av.sequenceColours = null;
787 PaintRefresher.Refresh(tp, ap.av.getSequenceSetId());
795 for (int i = 0; i < tree.getGroups().size(); i++)
797 Color col = new Color( (int) (Math.random() * 255),
798 (int) (Math.random() * 255),
799 (int) (Math.random() * 255));
800 setColor( (SequenceNode) tree.getGroups().elementAt(i),
803 Vector l = tree.findLeaves( (SequenceNode) tree.getGroups()
807 Vector sequences = new Vector();
809 for (int j = 0; j < l.size(); j++)
811 SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element();
813 if (!sequences.contains(s1))
815 sequences.addElement(s1);
819 ColourSchemeI cs = null;
821 if (av.getGlobalColourScheme() != null)
823 if (av.getGlobalColourScheme() instanceof UserColourScheme)
825 cs = new UserColourScheme(
826 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
830 cs = ColourSchemeProperty.getColour(sequences,
831 av.alignment.getWidth(),
832 ColourSchemeProperty.
834 av.getGlobalColourScheme()));
836 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
837 av.getIgnoreGapsConsensus());
840 SequenceGroup sg = new SequenceGroup(sequences,
841 "TreeGroup", cs, true, true, false,
843 av.alignment.getWidth() - 1);
845 AlignmentPanel[] aps = getAssociatedPanels();
846 for (int a = 0; a < aps.length; a++)
848 if (aps[a].av.getGlobalColourScheme() != null
849 && aps[a].av.getGlobalColourScheme().conservationApplied())
851 Conservation c = new Conservation("Group",
852 ResidueProperties.propHash, 3,
853 sg.getSequences(false),
854 sg.getStartRes(), sg.getEndRes());
857 c.verdict(false, aps[a].av.ConsPercGaps);
858 sg.cs.setConservation(c);
861 aps[a].av.alignment.addGroup(sg);
870 * @param state DOCUMENT ME!
872 public void setShowDistances(boolean state)
874 this.showDistances = state;
881 * @param state DOCUMENT ME!
883 public void setShowBootstrap(boolean state)
885 this.showBootstrap = state;
892 * @param state DOCUMENT ME!
894 public void setMarkPlaceholders(boolean state)
896 this.markPlaceholders = state;
900 AlignmentPanel[] getAssociatedPanels()
904 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
907 return new AlignmentPanel[]{ap};