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,
48 public static final String PLACEHOLDER = " * ";
50 JScrollPane scrollPane;
54 boolean fitToWindow = true;
55 boolean showDistances = false;
56 boolean showBootstrap = false;
57 boolean markPlaceholders = false;
64 //RubberbandRectangle rubberband;
66 Hashtable nameHash = new Hashtable();
67 Hashtable nodeHash = new Hashtable();
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(AlignViewport av, JScrollPane scroller)
81 scrollPane = scroller;
82 addMouseListener(this);
83 PaintRefresher.Register(this, av.alignment);
90 * @param sequence DOCUMENT ME!
92 public void TreeSelectionChanged(Sequence sequence)
94 SequenceGroup selected = av.getSelectionGroup();
98 selected = new SequenceGroup();
99 av.setSelectionGroup(selected);
102 selected.setEndRes(av.alignment.getWidth()-1);
103 selected.addOrRemove(sequence, true);
105 PaintRefresher.Refresh(this, av.alignment);
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 if (((SequenceI) ((SequenceNode) node).element()).getColor() == Color.white)
178 g.setColor(Color.black);
182 g.setColor(((SequenceI) ((SequenceNode) node).element()).getColor()
188 g.setColor(Color.black);
191 // Draw horizontal line
192 g.drawLine(xstart, ypos, xend, ypos);
194 String nodeLabel = "";
196 if (showDistances && (node.dist > 0))
198 nodeLabel = new Format("%-.2f").form(node.dist);
205 nodeLabel = nodeLabel + " : ";
208 nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
211 if (!nodeLabel.equals(""))
213 g.drawString(nodeLabel, xstart+2, ypos - 2);
216 String name = (markPlaceholders && node.isPlaceholder())
217 ? (PLACEHOLDER + node.getName()) : node.getName();
219 int charWidth = fm.stringWidth(name) + 3;
220 int charHeight = font.getSize();
222 Rectangle rect = new Rectangle(xend+10, ypos-charHeight/2,
223 charWidth, charHeight);
225 nameHash.put((SequenceI) node.element(), rect);
227 // Colour selected leaves differently
228 SequenceGroup selected = av.getSelectionGroup();
230 if ((selected != null) &&
231 selected.getSequences(false).contains((SequenceI) node.element()))
233 g.setColor(Color.gray);
235 g.fillRect(xend + 10, ypos-charHeight/2, charWidth,
237 g.setColor(Color.white);
240 g.drawString(name, xend + 10, ypos+fm.getDescent());
241 g.setColor(Color.black);
245 drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
247 drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
250 float height = node.height;
251 float dist = node.dist;
253 int xstart = (int) ((height - dist) * scale) + offx;
254 int xend = (int) (height * scale) + offx;
255 int ypos = (int) (node.ycount * chunk) + offy;
257 g.setColor(((SequenceNode) node).color.darker());
259 // Draw horizontal line
260 g.drawLine(xstart, ypos, xend, ypos);
261 g.fillRect(xend - 2, ypos - 2, 4, 4);
263 int ystart = (int) (((SequenceNode) node.left()).ycount * chunk) +
265 int yend = (int) (((SequenceNode) node.right()).ycount * chunk) +
268 Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
269 nodeHash.put(node, pos);
271 g.drawLine((int) (height * scale) + offx, ystart,
272 (int) (height * scale) + offx, yend);
274 if (showDistances && (node.dist > 0))
276 g.drawString(new Format("%-.2f").form(node.dist).trim(), xstart+2,
285 * @param x DOCUMENT ME!
286 * @param y DOCUMENT ME!
288 * @return DOCUMENT ME!
290 public Object findElement(int x, int y)
292 Enumeration keys = nameHash.keys();
294 while (keys.hasMoreElements())
296 Object ob = keys.nextElement();
297 Rectangle rect = (Rectangle) nameHash.get(ob);
299 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
300 (y <= (rect.y + rect.height)))
306 keys = nodeHash.keys();
308 while (keys.hasMoreElements())
310 Object ob = keys.nextElement();
311 Rectangle rect = (Rectangle) nodeHash.get(ob);
313 if ((x >= rect.x) && (x <= (rect.x + rect.width)) && (y >= rect.y) &&
314 (y <= (rect.y + rect.height)))
326 * @param pickBox DOCUMENT ME!
328 public void pickNodes(Rectangle pickBox)
330 int width = getWidth();
331 int height = getHeight();
333 SequenceNode top = tree.getTopNode();
335 float wscale = (float) ((width * .8) - (offx * 2)) / tree.getMaxHeight();
339 top.count = ((SequenceNode) top.left()).count +
340 ((SequenceNode) top.right()).count;
343 float chunk = (float) (height - (offy)) / top.count;
345 pickNode(pickBox, top, chunk, wscale, width, offx, offy);
351 * @param pickBox DOCUMENT ME!
352 * @param node DOCUMENT ME!
353 * @param chunk DOCUMENT ME!
354 * @param scale DOCUMENT ME!
355 * @param width DOCUMENT ME!
356 * @param offx DOCUMENT ME!
357 * @param offy DOCUMENT ME!
359 public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
360 float scale, int width, int offx, int offy)
367 if ((node.left() == null) && (node.right() == null))
369 float height = node.height;
370 float dist = node.dist;
372 int xstart = (int) ((height - dist) * scale) + offx;
373 int xend = (int) (height * scale) + offx;
375 int ypos = (int) (node.ycount * chunk) + offy;
377 if (pickBox.contains(new Point(xend, ypos)))
379 if (node.element() instanceof SequenceI)
381 SequenceI seq = (SequenceI) node.element();
382 SequenceGroup sg = av.getSelectionGroup();
386 sg.addOrRemove(seq, true);
393 pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
395 pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
403 * @param node DOCUMENT ME!
404 * @param c DOCUMENT ME!
406 public void setColor(SequenceNode node, Color c)
413 if ((node.left() == null) && (node.right() == null))
417 if (node.element() instanceof SequenceI)
419 ((SequenceI) node.element()).setColor(c);
425 setColor((SequenceNode) node.left(), c);
426 setColor((SequenceNode) node.right(), c);
435 Thread thread = new Thread(this);
439 // put printing in a thread to avoid painting problems
442 PrinterJob printJob = PrinterJob.getPrinterJob();
443 PageFormat pf = printJob.pageDialog(printJob.defaultPage());
445 printJob.setPrintable(this, pf);
447 if (printJob.printDialog())
453 catch (Exception PrintException)
455 PrintException.printStackTrace();
463 * @param pg DOCUMENT ME!
464 * @param pf DOCUMENT ME!
465 * @param pi DOCUMENT ME!
467 * @return DOCUMENT ME!
469 * @throws PrinterException DOCUMENT ME!
471 public int print(Graphics pg, PageFormat pf, int pi)
472 throws PrinterException
475 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
477 int pwidth = (int) pf.getImageableWidth();
478 int pheight = (int) pf.getImageableHeight();
480 int noPages = getHeight() / pheight;
484 return Printable.NO_SUCH_PAGE;
487 if (pwidth > getWidth())
494 if (pheight > getHeight())
496 pheight = getHeight();
503 FontMetrics fm = pg.getFontMetrics(font);
504 int height = fm.getHeight() * nameHash.size();
505 pg.translate(0, -pi * pheight);
506 pg.setClip(0, pi * pheight, pwidth, (pi * pheight) + pheight);
508 // translate number of pages,
509 // height is screen size as this is the
510 // non overlapping text size
514 draw(pg, pwidth, pheight);
516 return Printable.PAGE_EXISTS;
522 * @param g DOCUMENT ME!
524 public void paintComponent(Graphics g)
526 super.paintComponent(g);
531 g.drawString("Calculating tree....", 20, getHeight()/2);
535 fm = g.getFontMetrics(font);
537 if (nameHash.size() == 0)
544 (scrollPane.getHeight() > ( (fm.getHeight() * nameHash.size()) +
547 draw(g, scrollPane.getWidth(), scrollPane.getHeight());
548 setPreferredSize(null);
552 setPreferredSize(new Dimension(scrollPane.getWidth(),
553 fm.getHeight() * nameHash.size()));
554 draw(g, scrollPane.getWidth(), fm.getHeight() * nameHash.size());
557 scrollPane.revalidate();
565 * @param fontSize DOCUMENT ME!
567 public void setFont(Font font)
576 * @param g1 DOCUMENT ME!
577 * @param width DOCUMENT ME!
578 * @param height DOCUMENT ME!
580 public void draw(Graphics g1, int width, int height)
582 Graphics2D g2 = (Graphics2D) g1;
583 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
584 RenderingHints.VALUE_ANTIALIAS_ON);
585 g2.setColor(Color.white);
586 g2.fillRect(0, 0, width, height);
590 offy = font.getSize()+10;
592 fm = g2.getFontMetrics(font);
594 labelLength = fm.stringWidth(longestName) + 20; //20 allows for scrollbar
596 float wscale = (float) (width - labelLength - (offx * 2)) / tree.getMaxHeight();
598 SequenceNode top = tree.getTopNode();
602 top.count = ((SequenceNode) top.left()).count +
603 ((SequenceNode) top.right()).count;
606 float chunk = (float) (height - (offy)) / top.count;
608 drawNode(g2, tree.getTopNode(), chunk, wscale, width, offx, offy);
612 if (av.getCurrentTree() == tree)
614 g2.setColor(Color.red);
618 g2.setColor(Color.gray);
621 int x = (int) ((threshold * (float) (getWidth() - labelLength -
622 (2 * offx))) + offx);
624 g2.drawLine(x, 0, x, getHeight());
631 * @param e DOCUMENT ME!
633 public void mouseReleased(MouseEvent e)
640 * @param e DOCUMENT ME!
642 public void mouseEntered(MouseEvent e)
649 * @param e DOCUMENT ME!
651 public void mouseExited(MouseEvent e)
658 * @param e DOCUMENT ME!
660 public void mouseClicked(MouseEvent e)
667 * @param e DOCUMENT ME!
669 public void mousePressed(MouseEvent e)
671 av.setCurrentTree(tree);
676 Object ob = findElement(x, y);
678 if (ob instanceof SequenceI)
680 TreeSelectionChanged((Sequence) ob);
685 else if (ob instanceof SequenceNode)
687 SequenceNode tmpnode = (SequenceNode) ob;
688 tree.swapNodes(tmpnode);
689 tree.reCount(tree.getTopNode());
690 tree.findHeight(tree.getTopNode());
695 if (tree.getMaxHeight() != 0)
697 threshold = (float) (x - offx) / (float) (getWidth() -
698 labelLength - (2 * offx));
700 tree.getGroups().removeAllElements();
701 tree.groupNodes(tree.getTopNode(), threshold);
702 setColor(tree.getTopNode(), Color.black);
704 av.setSelectionGroup(null);
705 av.alignment.deleteAllGroups();
707 for (int i = 0; i < tree.getGroups().size(); i++)
709 Color col = new Color((int) (Math.random() * 255),
710 (int) (Math.random() * 255),
711 (int) (Math.random() * 255));
712 setColor((SequenceNode) tree.getGroups().elementAt(i),
715 Vector l = tree.findLeaves((SequenceNode) tree.getGroups()
719 Vector sequences = new Vector();
721 for (int j = 0; j < l.size(); j++)
723 SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j)).element();
725 if (!sequences.contains(s1))
727 sequences.addElement(s1);
731 ColourSchemeI cs = null;
733 if (av.getGlobalColourScheme() != null)
735 if (av.getGlobalColourScheme() instanceof UserColourScheme)
737 cs = new UserColourScheme(
738 ( (UserColourScheme) av.getGlobalColourScheme()).getColours());
742 cs = ColourSchemeProperty.getColour(sequences,
743 av.alignment.getWidth(),
744 ColourSchemeProperty.getColourName(
745 av.getGlobalColourScheme()));
747 cs.setThreshold(av.getGlobalColourScheme().getThreshold(),
748 av.getIgnoreGapsConsensus());
752 SequenceGroup sg = new SequenceGroup(sequences,
753 "TreeGroup", cs, true, true, false, 0,
754 av.alignment.getWidth()-1);
757 if ( av.getGlobalColourScheme()!=null
758 && av.getGlobalColourScheme().conservationApplied())
760 Conservation c = new Conservation("Group",
761 ResidueProperties.propHash, 3,
762 sg.getSequences(false),
763 sg.getStartRes(), sg.getEndRes());
766 c.verdict(false, av.ConsPercGaps);
767 sg.cs.setConservation(c);
770 av.alignment.addGroup(sg);
775 PaintRefresher.Refresh(this, av.alignment);
782 * @param state DOCUMENT ME!
784 public void setShowDistances(boolean state)
786 this.showDistances = state;
793 * @param state DOCUMENT ME!
795 public void setShowBootstrap(boolean state)
797 this.showBootstrap = state;
804 * @param state DOCUMENT ME!
806 public void setMarkPlaceholders(boolean state)
808 this.markPlaceholders = state;