X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FTreeCanvas.java;h=c38b1a5d5a1aa0564c401169e4f8a3a33c5646d2;hb=cbd8cafe5f1d302cf490d34a96f8231659afd402;hp=b00d6cc7bb45a5cd76bcdc58a9119ecc00862b76;hpb=4ebc6d5b362bc093c39312aa1a69836e3dd6ae84;p=jalview.git diff --git a/src/jalview/appletgui/TreeCanvas.java b/src/jalview/appletgui/TreeCanvas.java index b00d6cc..c38b1a5 100755 --- a/src/jalview/appletgui/TreeCanvas.java +++ b/src/jalview/appletgui/TreeCanvas.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -30,50 +30,44 @@ import jalview.schemes.*; import jalview.util.*; public class TreeCanvas - extends Panel implements MouseListener + extends Panel implements MouseListener, MouseMotionListener { NJTree tree; ScrollPane scrollPane; AlignViewport av; public static final String PLACEHOLDER = " * "; Font font; - int fontSize = 12; - boolean fitToWindow = true; boolean showDistances = false; boolean showBootstrap = false; boolean markPlaceholders = false; int offx = 20; - int offy = 20; + int offy; float threshold; String longestName; int labelLength = -1; - //RubberbandRectangle rubberband; - - Vector listeners; - Hashtable nameHash = new Hashtable(); Hashtable nodeHash = new Hashtable(); - public TreeCanvas(AlignViewport av, NJTree tree, ScrollPane scroller, - String label) + SequenceNode highlightNode; + + public TreeCanvas(AlignViewport av, ScrollPane scroller) { this.av = av; - this.tree = tree; + font = av.getFont(); scrollPane = scroller; addMouseListener(this); - tree.findHeight(tree.getTopNode()); - longestName = label; + addMouseMotionListener(this); setLayout(null); - PaintRefresher.Register(this); + PaintRefresher.Register(this, av.getSequenceSetId()); } - public void TreeSelectionChanged(Sequence sequence) + public void treeSelectionChanged(SequenceI sequence) { SequenceGroup selected = av.getSelectionGroup(); if (selected == null) @@ -82,17 +76,38 @@ public class TreeCanvas av.setSelectionGroup(selected); } - selected.setEndRes(av.alignment.getWidth()); + selected.setEndRes(av.alignment.getWidth() - 1); selected.addOrRemove(sequence, true); - - PaintRefresher.Refresh(this); - repaint(); } public void setTree(NJTree tree) { this.tree = tree; tree.findHeight(tree.getTopNode()); + + // Now have to calculate longest name based on the leaves + Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector()); + boolean has_placeholders = false; + longestName = ""; + + for (int i = 0; i < leaves.size(); i++) + { + SequenceNode lf = (SequenceNode) leaves.elementAt(i); + + if (lf.isPlaceholder()) + { + has_placeholders = true; + } + + if (longestName.length() < ( (Sequence) lf.element()).getName() + .length()) + { + longestName = TreeCanvas.PLACEHOLDER + + ( (Sequence) lf.element()).getName(); + } + } + + setMarkPlaceholders(has_placeholders); } public void drawNode(Graphics g, SequenceNode node, float chunk, float scale, @@ -117,15 +132,15 @@ public class TreeCanvas if (node.element() instanceof SequenceI) { - if ( ( (SequenceI) ( (SequenceNode) node).element()).getColor() == - Color.white) + SequenceI seq = (SequenceI) ( (SequenceNode) node).element(); + + if (av.getSequenceColour(seq) == Color.white) { g.setColor(Color.black); } else { - g.setColor( ( (SequenceI) ( (SequenceNode) node).element()).getColor(). - darker()); + g.setColor(av.getSequenceColour(seq).darker()); } } @@ -140,7 +155,7 @@ public class TreeCanvas String nodeLabel = ""; if (showDistances && node.dist > 0) { - nodeLabel = new Format("%5.2f").form(node.dist); + nodeLabel = new Format("%-.2f").form(node.dist); } if (showBootstrap) { @@ -152,7 +167,7 @@ public class TreeCanvas } if (!nodeLabel.equals("")) { - g.drawString(nodeLabel, xstart, ypos - 10); + g.drawString(nodeLabel, xstart + 2, ypos - 2); } String name = (markPlaceholders && node.isPlaceholder()) ? @@ -161,7 +176,7 @@ public class TreeCanvas int charWidth = fm.stringWidth(name) + 3; int charHeight = fm.getHeight(); - Rectangle rect = new Rectangle(xend + 20, ypos - charHeight, + Rectangle rect = new Rectangle(xend + 10, ypos - charHeight, charWidth, charHeight); nameHash.put( (SequenceI) node.element(), rect); @@ -169,7 +184,7 @@ public class TreeCanvas // Colour selected leaves differently SequenceGroup selected = av.getSelectionGroup(); if (selected != null && - selected.sequences.contains( (SequenceI) node.element())) + selected.getSequences(null).contains( (SequenceI) node.element())) { g.setColor(Color.gray); @@ -195,7 +210,14 @@ public class TreeCanvas // Draw horizontal line g.drawLine(xstart, ypos, xend, ypos); - g.fillRect(xend - 2, ypos - 2, 4, 4); + if (node == highlightNode) + { + g.fillRect(xend - 3, ypos - 3, 6, 6); + } + else + { + g.fillRect(xend - 2, ypos - 2, 4, 4); + } int ystart = (int) ( ( (SequenceNode) node.left()).ycount * chunk) + offy; int yend = (int) ( ( (SequenceNode) node.right()).ycount * chunk) + offy; @@ -208,7 +230,7 @@ public class TreeCanvas if (showDistances && node.dist > 0) { - g.drawString(new Format("%5.2f").form(node.dist), xstart, ypos - 5); + g.drawString(new Format("%-.2f").form(node.dist), xstart + 2, ypos - 2); } } @@ -260,7 +282,7 @@ public class TreeCanvas top.count = ( (SequenceNode) top.left()).count + ( (SequenceNode) top.right()).count; } - float chunk = (float) (height - offy * 2) / top.count; + float chunk = (float) (height - offy) / top.count; pickNode(pickBox, top, chunk, wscale, width, offx, offy); } @@ -276,9 +298,9 @@ public class TreeCanvas if (node.left() == null && node.right() == null) { float height = node.height; - float dist = node.dist; + //float dist = node.dist; - int xstart = (int) ( (height - dist) * scale) + offx; + //int xstart = (int) ( (height - dist) * scale) + offx; int xend = (int) (height * scale) + offx; int ypos = (int) (node.ycount * chunk) + offy; @@ -318,7 +340,7 @@ public class TreeCanvas if (node.element() instanceof SequenceI) { - ( (SequenceI) node.element()).setColor(c); + av.setSequenceColour( (SequenceI) node.element(), c); } } else @@ -329,48 +351,48 @@ public class TreeCanvas } } - public void paint(Graphics g) + public void update(Graphics g) { + paint(g); + } - font = new Font("Verdana", Font.PLAIN, fontSize); - g.setFont(font); - - FontMetrics fm = g.getFontMetrics(font); + public void paint(Graphics g) + { + if (tree == null) + { + return; + } if (nameHash.size() == 0) { repaint(); } - if (fitToWindow || - (!fitToWindow && - scrollPane.getSize().height > fm.getHeight() * nameHash.size() + offy)) + int width = scrollPane.getSize().width; + int height = scrollPane.getSize().height; + if (!fitToWindow) { - draw(g, scrollPane.getSize().width, scrollPane.getSize().height); + height = g.getFontMetrics(font).getHeight() * nameHash.size(); } - else + + if (getSize().width > width) { - setSize(new Dimension(scrollPane.getSize().width, - fm.getHeight() * nameHash.size())); - draw(g, scrollPane.getSize().width, fm.getHeight() * nameHash.size()); + setSize(new Dimension(width, height)); + scrollPane.validate(); + return; } - scrollPane.validate(); - } + setSize(new Dimension(width, height)); - public int getFontSize() - { - return fontSize; - } + g.setFont(font); + + draw(g, width, height); - public void setFontSize(int fontSize) - { - this.fontSize = fontSize; - repaint(); } public void draw(Graphics g, int width, int height) { + offy = font.getSize() + 10; g.setColor(Color.white); g.fillRect(0, 0, width, height); @@ -386,7 +408,7 @@ public class TreeCanvas top.count = ( (SequenceNode) top.left()).count + ( (SequenceNode) top.right()).count; } - float chunk = (float) (height - offy * 2) / top.count; + float chunk = (float) (height - offy) / top.count; drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy); @@ -418,13 +440,60 @@ public class TreeCanvas public void mouseExited(MouseEvent e) {} - public void mouseClicked(MouseEvent e) + public void mouseClicked(MouseEvent evt) + { + if (highlightNode != null) + { + if (evt.getClickCount() > 1) + { + tree.swapNodes(highlightNode); + tree.reCount(tree.getTopNode()); + tree.findHeight(tree.getTopNode()); + } + else + { + Vector leaves = new Vector(); + tree.findLeaves(highlightNode, leaves); + + for (int i = 0; i < leaves.size(); i++) + { + SequenceI seq = + (SequenceI) ( (SequenceNode) leaves.elementAt(i)).element(); + treeSelectionChanged(seq); + } + } + + PaintRefresher.Refresh(this, av.getSequenceSetId()); + repaint(); + } + } + + public void mouseDragged(MouseEvent ect) + {} + + public void mouseMoved(MouseEvent evt) { + av.setCurrentTree(tree); + + Object ob = findElement(evt.getX(), evt.getY()); + + if (ob instanceof SequenceNode) + { + highlightNode = (SequenceNode) ob; + repaint(); + } + else + { + if (highlightNode != null) + { + highlightNode = null; + repaint(); + } + } } public void mousePressed(MouseEvent e) { - av.setCurrentTree(tree); int x = e.getX(); @@ -434,19 +503,12 @@ public class TreeCanvas if (ob instanceof SequenceI) { - TreeSelectionChanged( (Sequence) ob); + treeSelectionChanged( (Sequence) ob); + PaintRefresher.Refresh(this, av.getSequenceSetId()); repaint(); return; - } - else if (ob instanceof SequenceNode) - { - SequenceNode tmpnode = (SequenceNode) ob; - tree.swapNodes(tmpnode); - tree.reCount(tree.getTopNode()); - tree.findHeight(tree.getTopNode()); - } - else + else if (! (ob instanceof SequenceNode)) { // Find threshold @@ -461,50 +523,91 @@ public class TreeCanvas av.setSelectionGroup(null); av.alignment.deleteAllGroups(); + av.sequenceColours = null; - for (int i = 0; i < tree.getGroups().size(); i++) - { + colourGroups(); - Color col = new Color( (int) (Math.random() * 255), - (int) (Math.random() * 255), - (int) (Math.random() * 255)); - setColor( (SequenceNode) tree.getGroups().elementAt(i), col.brighter()); + } + } - Vector l = tree.findLeaves( (SequenceNode) tree.getGroups().elementAt( - i), new Vector()); - SequenceGroup sg = new SequenceGroup("TreeGroup", av.getGlobalColourScheme(), true, true, false, - 0, av.alignment.getWidth()); - for (int j = 0; j < l.size(); j++) - { - SequenceNode sn = (SequenceNode) l.elementAt(j); - sg.addSequence( (Sequence) sn.element(), false); - } + PaintRefresher.Refresh(this, av.getSequenceSetId()); + repaint(); - if (av.getGlobalColourScheme() instanceof ConservationColourScheme) - { - ConservationColourScheme ccs = (ConservationColourScheme) av. - getGlobalColourScheme(); - Conservation c = new Conservation("Group", - ResidueProperties.propHash, 3, - sg.sequences, sg.getStartRes(), - sg.getEndRes()); + } - c.calculate(); - c.verdict(false, av.ConsPercGaps); - ccs = new ConservationColourScheme(c, ccs.cs); + void colourGroups() + { + for (int i = 0; i < tree.getGroups().size(); i++) + { - sg.cs = ccs; + Color col = new Color( (int) (Math.random() * 255), + (int) (Math.random() * 255), + (int) (Math.random() * 255)); + setColor( (SequenceNode) tree.getGroups().elementAt(i), col.brighter()); - } + Vector l = tree.findLeaves( (SequenceNode) tree.getGroups().elementAt( + i), new Vector()); + + Vector sequences = new Vector(); + for (int j = 0; j < l.size(); j++) + { + SequenceI s1 = (SequenceI) ( (SequenceNode) l.elementAt(j)).element(); + if (!sequences.contains(s1)) + { + sequences.addElement(s1); + } + } - av.alignment.addGroup(sg); + ColourSchemeI cs = null; + if (av.getGlobalColourScheme() != null) + { + if (av.getGlobalColourScheme() instanceof UserColourScheme) + { + cs = new UserColourScheme( + ( (UserColourScheme) av.getGlobalColourScheme()).getColours()); + + } + else + { + cs = ColourSchemeProperty.getColour(sequences, + av.alignment.getWidth(), + ColourSchemeProperty. + getColourName( + av.getGlobalColourScheme())); } + + cs.setThreshold(av.getGlobalColourScheme().getThreshold(), + av.getIgnoreGapsConsensus()); } - } - PaintRefresher.Refresh(this); - repaint(); + SequenceGroup sg = new SequenceGroup(sequences, "", + cs, true, true, + false, 0, + av.alignment.getWidth() - 1); + + sg.setName("JTreeGroup:" + sg.hashCode()); + + if (av.getGlobalColourScheme() != null + && av.getGlobalColourScheme().conservationApplied()) + { + Conservation c = new Conservation("Group", + ResidueProperties.propHash, 3, + sg.getSequences(null), + sg.getStartRes(), + sg.getEndRes()); + + c.calculate(); + c.verdict(false, av.ConsPercGaps); + cs.setConservation(c); + + sg.cs = cs; + + } + + av.alignment.addGroup(sg); + + } }