2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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
40 extends JPanel implements MouseListener, Runnable,
41 Printable, MouseMotionListener
44 public static final String PLACEHOLDER = " * ";
46 JScrollPane scrollPane;
52 boolean fitToWindow = true;
53 boolean showDistances = false;
54 boolean showBootstrap = false;
55 boolean markPlaceholders = false;
62 Hashtable nameHash = new Hashtable();
63 Hashtable nodeHash = new Hashtable();
64 SequenceNode highlightNode;
66 boolean applyToAllViews = false;
69 * Creates a new TreeCanvas object.
71 * @param av DOCUMENT ME!
72 * @param tree DOCUMENT ME!
73 * @param scroller DOCUMENT ME!
74 * @param label DOCUMENT ME!
76 public TreeCanvas(TreePanel tp,
84 scrollPane = scroller;
85 addMouseListener(this);
86 addMouseMotionListener(this);
87 ToolTipManager.sharedInstance().registerComponent(this);
93 * @param sequence DOCUMENT ME!
95 public void treeSelectionChanged(SequenceI sequence)
97 AlignmentPanel[] aps = getAssociatedPanels();
99 for (int a = 0; a < aps.length; a++)
101 SequenceGroup selected = aps[a].av.getSelectionGroup();
103 if (selected == null)
105 selected = new SequenceGroup();
106 aps[a].av.setSelectionGroup(selected);
109 selected.setEndRes(aps[a].av.alignment.getWidth() - 1);
110 selected.addOrRemove(sequence, true);
117 * @param tree DOCUMENT ME!
119 public void setTree(NJTree tree)
122 tree.findHeight(tree.getTopNode());
124 // Now have to calculate longest name based on the leaves
125 Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
126 boolean has_placeholders = false;
129 for (int i = 0; i < leaves.size(); i++)
131 SequenceNode lf = (SequenceNode) leaves.elementAt(i);
133 if (lf.isPlaceholder())
135 has_placeholders = true;
138 if (longestName.length() < ( (Sequence) lf.element()).getName()
141 longestName = TreeCanvas.PLACEHOLDER +
142 ( (Sequence) lf.element()).getName();
146 setMarkPlaceholders(has_placeholders);
152 * @param g DOCUMENT ME!
153 * @param node DOCUMENT ME!
154 * @param chunk DOCUMENT ME!
155 * @param scale DOCUMENT ME!
156 * @param width DOCUMENT ME!
157 * @param offx DOCUMENT ME!
158 * @param offy DOCUMENT ME!
160 public void drawNode(Graphics g, SequenceNode node, float chunk,
161 float scale, int width, int offx, int offy)
168 if ( (node.left() == null) && (node.right() == null))
171 float height = node.height;
172 float dist = node.dist;
174 int xstart = (int) ( (height - dist) * scale) + offx;
175 int xend = (int) (height * scale) + offx;
177 int ypos = (int) (node.ycount * chunk) + offy;
179 if (node.element() instanceof SequenceI)
181 SequenceI seq = (SequenceI) ( (SequenceNode) node).element();
183 if (av.getSequenceColour(seq) == Color.white)
185 g.setColor(Color.black);
189 g.setColor(av.getSequenceColour(seq).darker());
194 g.setColor(Color.black);
197 // Draw horizontal line
198 g.drawLine(xstart, ypos, xend, ypos);
200 String nodeLabel = "";
202 if (showDistances && (node.dist > 0))
204 nodeLabel = new Format("%-.2f").form(node.dist);
211 nodeLabel = nodeLabel + " : ";
214 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
217 if (!nodeLabel.equals(""))
219 g.drawString(nodeLabel, xstart + 2, ypos - 2);
222 String name = (markPlaceholders && node.isPlaceholder())
223 ? (PLACEHOLDER + node.getName()) : node.getName();
225 int charWidth = fm.stringWidth(name) + 3;
226 int charHeight = font.getSize();
228 Rectangle rect = new Rectangle(xend + 10, ypos - charHeight / 2,
229 charWidth, charHeight);
231 nameHash.put( (SequenceI) node.element(), rect);
233 // Colour selected leaves differently
234 SequenceGroup selected = av.getSelectionGroup();
236 if ( (selected != null) &&
237 selected.getSequences(null).contains( (SequenceI) node.element()))
239 g.setColor(Color.gray);
241 g.fillRect(xend + 10, ypos - charHeight / 2, charWidth,
243 g.setColor(Color.white);
246 g.drawString(name, xend + 10, ypos + fm.getDescent());
247 g.setColor(Color.black);
251 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
253 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
256 float height = node.height;
257 float dist = node.dist;
259 int xstart = (int) ( (height - dist) * scale) + offx;
260 int xend = (int) (height * scale) + offx;
261 int ypos = (int) (node.ycount * chunk) + offy;
263 g.setColor( ( (SequenceNode) node).color.darker());
265 // Draw horizontal line
266 g.drawLine(xstart, ypos, xend, ypos);
267 if (node == highlightNode)
269 g.fillRect(xend - 3, ypos - 3, 6, 6);
273 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());
831 cs = ColourSchemeProperty.getColour(sequences,
832 av.alignment.getWidth(),
833 ColourSchemeProperty.
835 av.getGlobalColourScheme()));
838 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
839 av.getIgnoreGapsConsensus());
842 SequenceGroup sg = new SequenceGroup(sequences,
843 null, cs, true, true, false,
845 av.alignment.getWidth() - 1);
847 sg.setName("JTreeGroup:" + sg.hashCode());
849 AlignmentPanel[] aps = getAssociatedPanels();
850 for (int a = 0; a < aps.length; a++)
852 if (aps[a].av.getGlobalColourScheme() != null
853 && aps[a].av.getGlobalColourScheme().conservationApplied())
855 Conservation c = new Conservation("Group",
856 ResidueProperties.propHash, 3,
857 sg.getSequences(null),
858 sg.getStartRes(), sg.getEndRes());
861 c.verdict(false, aps[a].av.ConsPercGaps);
862 sg.cs.setConservation(c);
865 aps[a].av.alignment.addGroup(sg);
874 * @param state DOCUMENT ME!
876 public void setShowDistances(boolean state)
878 this.showDistances = state;
885 * @param state DOCUMENT ME!
887 public void setShowBootstrap(boolean state)
889 this.showBootstrap = state;
896 * @param state DOCUMENT ME!
898 public void setMarkPlaceholders(boolean state)
900 this.markPlaceholders = state;
904 AlignmentPanel[] getAssociatedPanels()
908 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
912 return new AlignmentPanel[]