JAL-2446 merged to spike branch
[jalview.git] / src / jalview / appletgui / TreeCanvas.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.analysis.Conservation;
24 import jalview.analysis.TreeModel;
25 import jalview.api.AlignViewportI;
26 import jalview.datamodel.Sequence;
27 import jalview.datamodel.SequenceGroup;
28 import jalview.datamodel.SequenceI;
29 import jalview.datamodel.SequenceNode;
30 import jalview.schemes.ColourSchemeI;
31 import jalview.schemes.ColourSchemeProperty;
32 import jalview.schemes.UserColourScheme;
33 import jalview.util.Format;
34 import jalview.util.MappingUtils;
35 import jalview.viewmodel.AlignmentViewport;
36
37 import java.awt.Color;
38 import java.awt.Dimension;
39 import java.awt.Font;
40 import java.awt.FontMetrics;
41 import java.awt.Graphics;
42 import java.awt.Panel;
43 import java.awt.Point;
44 import java.awt.Rectangle;
45 import java.awt.ScrollPane;
46 import java.awt.event.MouseEvent;
47 import java.awt.event.MouseListener;
48 import java.awt.event.MouseMotionListener;
49 import java.util.Enumeration;
50 import java.util.Hashtable;
51 import java.util.List;
52 import java.util.Vector;
53
54 public class TreeCanvas extends Panel implements MouseListener,
55         MouseMotionListener
56 {
57   TreeModel tree;
58
59   ScrollPane scrollPane;
60
61   AlignViewport av;
62
63   public static final String PLACEHOLDER = " * ";
64
65   Font font;
66
67   boolean fitToWindow = true;
68
69   boolean showDistances = false;
70
71   boolean showBootstrap = false;
72
73   boolean markPlaceholders = false;
74
75   int offx = 20;
76
77   int offy;
78
79   float threshold;
80
81   String longestName;
82
83   int labelLength = -1;
84
85   Hashtable nameHash = new Hashtable();
86
87   Hashtable nodeHash = new Hashtable();
88
89   SequenceNode highlightNode;
90
91   AlignmentPanel ap;
92
93   public TreeCanvas(AlignmentPanel ap, ScrollPane scroller)
94   {
95     this.ap = ap;
96     this.av = ap.av;
97     font = av.getFont();
98     scrollPane = scroller;
99     addMouseListener(this);
100     addMouseMotionListener(this);
101     setLayout(null);
102
103     PaintRefresher.Register(this, av.getSequenceSetId());
104   }
105
106   public void treeSelectionChanged(SequenceI sequence)
107   {
108     SequenceGroup selected = av.getSelectionGroup();
109     if (selected == null)
110     {
111       selected = new SequenceGroup();
112       av.setSelectionGroup(selected);
113     }
114
115     selected.setEndRes(av.getAlignment().getWidth() - 1);
116     selected.addOrRemove(sequence, true);
117   }
118
119   public void setTree(TreeModel tree2)
120   {
121     this.tree = tree2;
122     tree2.findHeight(tree2.getTopNode());
123
124     // Now have to calculate longest name based on the leaves
125     Vector<SequenceNode> leaves = tree2.findLeaves(tree2.getTopNode());
126     boolean has_placeholders = false;
127     longestName = "";
128
129     for (int i = 0; i < leaves.size(); i++)
130     {
131       SequenceNode lf = leaves.elementAt(i);
132
133       if (lf.isPlaceholder())
134       {
135         has_placeholders = true;
136       }
137
138       if (longestName.length() < ((Sequence) lf.element()).getName()
139               .length())
140       {
141         longestName = TreeCanvas.PLACEHOLDER
142                 + ((Sequence) lf.element()).getName();
143       }
144     }
145
146     setMarkPlaceholders(has_placeholders);
147   }
148
149   public void drawNode(Graphics g, SequenceNode node, float chunk,
150           double scale, int width, int offx, int offy)
151   {
152     if (node == null)
153     {
154       return;
155     }
156
157     if (node.left() == null && node.right() == null)
158     {
159       // Drawing leaf node
160
161       double height = node.height;
162       double dist = node.dist;
163
164       int xstart = (int) ((height - dist) * scale) + offx;
165       int xend = (int) (height * scale) + offx;
166
167       int ypos = (int) (node.ycount * chunk) + offy;
168
169       if (node.element() instanceof SequenceI)
170       {
171         SequenceI seq = (SequenceI) node.element();
172
173         if (av.getSequenceColour(seq) == Color.white)
174         {
175           g.setColor(Color.black);
176         }
177         else
178         {
179           g.setColor(av.getSequenceColour(seq).darker());
180         }
181
182       }
183       else
184       {
185         g.setColor(Color.black);
186       }
187
188       // Draw horizontal line
189       g.drawLine(xstart, ypos, xend, ypos);
190
191       String nodeLabel = "";
192       if (showDistances && node.dist > 0)
193       {
194         nodeLabel = new Format("%-.2f").form(node.dist);
195       }
196       if (showBootstrap)
197       {
198         int btstrap = node.getBootstrap();
199         if (btstrap > -1)
200         {
201           if (showDistances)
202           {
203             nodeLabel = nodeLabel + " : ";
204           }
205           nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
206         }
207       }
208       if (!nodeLabel.equals(""))
209       {
210         g.drawString(nodeLabel, xstart + 2, ypos - 2);
211       }
212
213       String name = (markPlaceholders && node.isPlaceholder()) ? (PLACEHOLDER + node
214               .getName()) : node.getName();
215       FontMetrics fm = g.getFontMetrics(font);
216       int charWidth = fm.stringWidth(name) + 3;
217       int charHeight = fm.getHeight();
218
219       Rectangle rect = new Rectangle(xend + 10, ypos - charHeight,
220               charWidth, charHeight);
221
222       nameHash.put(node.element(), rect);
223
224       // Colour selected leaves differently
225       SequenceGroup selected = av.getSelectionGroup();
226       if (selected != null
227               && selected.getSequences(null).contains(node.element()))
228       {
229         g.setColor(Color.gray);
230
231         g.fillRect(xend + 10, ypos - charHeight + 3, charWidth, charHeight);
232         g.setColor(Color.white);
233       }
234       g.drawString(name, xend + 10, ypos);
235       g.setColor(Color.black);
236     }
237     else
238     {
239       drawNode(g, (SequenceNode) node.left(), chunk, scale, width, offx,
240               offy);
241       drawNode(g, (SequenceNode) node.right(), chunk, scale, width, offx,
242               offy);
243
244       double height = node.height;
245       double dist = node.dist;
246
247       int xstart = (int) ((height - dist) * scale) + offx;
248       int xend = (int) (height * scale) + offx;
249       int ypos = (int) (node.ycount * chunk) + offy;
250
251       g.setColor(node.color.darker());
252
253       // Draw horizontal line
254       g.drawLine(xstart, ypos, xend, ypos);
255       if (node == highlightNode)
256       {
257         g.fillRect(xend - 3, ypos - 3, 6, 6);
258       }
259       else
260       {
261         g.fillRect(xend - 2, ypos - 2, 4, 4);
262       }
263
264       int ystart = (int) (node.left() == null ? 0 : (((SequenceNode) node
265               .left()).ycount * chunk))
266               + offy;
267       int yend = (int) (node.right() == null ? 0 : (((SequenceNode) node
268               .right()).ycount * chunk)) + offy;
269
270       Rectangle pos = new Rectangle(xend - 2, ypos - 2, 5, 5);
271       nodeHash.put(node, pos);
272
273       g.drawLine((int) (height * scale) + offx, ystart,
274               (int) (height * scale) + offx, yend);
275
276       String nodeLabel = "";
277
278       if (showDistances && (node.dist > 0))
279       {
280         nodeLabel = new Format("%-.2f").form(node.dist);
281       }
282
283       if (showBootstrap)
284       {
285         int btstrap = node.getBootstrap();
286         if (btstrap > -1)
287         {
288           if (showDistances)
289           {
290             nodeLabel = nodeLabel + " : ";
291           }
292           nodeLabel = nodeLabel + String.valueOf(node.getBootstrap());
293         }
294       }
295
296       if (!nodeLabel.equals(""))
297       {
298         g.drawString(nodeLabel, xstart + 2, ypos - 2);
299       }
300
301     }
302   }
303
304   public Object findElement(int x, int y)
305   {
306     Enumeration keys = nameHash.keys();
307
308     while (keys.hasMoreElements())
309     {
310       Object ob = keys.nextElement();
311       Rectangle rect = (Rectangle) nameHash.get(ob);
312
313       if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
314               && y <= (rect.y + rect.height))
315       {
316         return ob;
317       }
318     }
319     keys = nodeHash.keys();
320
321     while (keys.hasMoreElements())
322     {
323       Object ob = keys.nextElement();
324       Rectangle rect = (Rectangle) nodeHash.get(ob);
325
326       if (x >= rect.x && x <= (rect.x + rect.width) && y >= rect.y
327               && y <= (rect.y + rect.height))
328       {
329         return ob;
330       }
331     }
332     return null;
333
334   }
335
336   public void pickNodes(Rectangle pickBox)
337   {
338     int width = getSize().width;
339     int height = getSize().height;
340
341     SequenceNode top = tree.getTopNode();
342
343     double wscale = (float) (width * .8 - offx * 2) / tree.getMaxHeight();
344     if (top.count == 0)
345     {
346       top.count = ((SequenceNode) top.left()).count
347               + ((SequenceNode) top.right()).count;
348     }
349     float chunk = (float) (height - offy) / top.count;
350
351     pickNode(pickBox, top, chunk, wscale, width, offx, offy);
352   }
353
354   public void pickNode(Rectangle pickBox, SequenceNode node, float chunk,
355           double scale, int width, int offx, int offy)
356   {
357     if (node == null)
358     {
359       return;
360     }
361
362     if (node.left() == null && node.right() == null)
363     {
364       double height = node.height;
365       // float dist = node.dist;
366
367       // int xstart = (int) ( (height - dist) * scale) + offx;
368       int xend = (int) (height * scale) + offx;
369
370       int ypos = (int) (node.ycount * chunk) + offy;
371
372       if (pickBox.contains(new Point(xend, ypos)))
373       {
374         if (node.element() instanceof SequenceI)
375         {
376           SequenceI seq = (SequenceI) node.element();
377           SequenceGroup sg = av.getSelectionGroup();
378           if (sg != null)
379           {
380             sg.addOrRemove(seq, true);
381           }
382         }
383       }
384     }
385     else
386     {
387       pickNode(pickBox, (SequenceNode) node.left(), chunk, scale, width,
388               offx, offy);
389       pickNode(pickBox, (SequenceNode) node.right(), chunk, scale, width,
390               offx, offy);
391     }
392   }
393
394   public void setColor(SequenceNode node, Color c)
395   {
396     if (node == null)
397     {
398       return;
399     }
400
401     if (node.left() == null && node.right() == null)
402     {
403       node.color = c;
404
405       if (node.element() instanceof SequenceI)
406       {
407         av.setSequenceColour((SequenceI) node.element(), c);
408       }
409     }
410     else
411     {
412       node.color = c;
413       setColor((SequenceNode) node.left(), c);
414       setColor((SequenceNode) node.right(), c);
415     }
416   }
417
418   @Override
419   public void update(Graphics g)
420   {
421     paint(g);
422   }
423
424   @Override
425   public void paint(Graphics g)
426   {
427     if (tree == null)
428     {
429       return;
430     }
431
432     if (nameHash.size() == 0)
433     {
434       repaint();
435     }
436
437     int width = scrollPane.getSize().width;
438     int height = scrollPane.getSize().height;
439     if (!fitToWindow)
440     {
441       height = g.getFontMetrics(font).getHeight() * nameHash.size();
442     }
443
444     if (getSize().width > width)
445     {
446       setSize(new Dimension(width, height));
447       scrollPane.validate();
448       return;
449     }
450
451     setSize(new Dimension(width, height));
452
453     g.setFont(font);
454     draw(g, width, height);
455     validate();
456   }
457
458   public void draw(Graphics g, int width, int height)
459   {
460     offy = font.getSize() + 10;
461
462     g.setColor(Color.white);
463     g.fillRect(0, 0, width, height);
464
465     labelLength = g.getFontMetrics(font).stringWidth(longestName) + 20; // 20
466     // allows
467     // for
468     // scrollbar
469
470     double wscale = (width - labelLength - offx * 2) / tree.getMaxHeight();
471
472     SequenceNode top = tree.getTopNode();
473
474     if (top.count == 0)
475     {
476       top.count = ((SequenceNode) top.left()).count
477               + ((SequenceNode) top.right()).count;
478     }
479     float chunk = (float) (height - offy) / top.count;
480
481     drawNode(g, tree.getTopNode(), chunk, wscale, width, offx, offy);
482
483     if (threshold != 0)
484     {
485       if (av.getCurrentTree() == tree)
486       {
487         g.setColor(Color.red);
488       }
489       else
490       {
491         g.setColor(Color.gray);
492       }
493
494       int x = (int) (threshold * (getSize().width - labelLength - 2 * offx) + offx);
495
496       g.drawLine(x, 0, x, getSize().height);
497     }
498
499   }
500
501   @Override
502   public void mouseReleased(MouseEvent e)
503   {
504   }
505
506   @Override
507   public void mouseEntered(MouseEvent e)
508   {
509   }
510
511   @Override
512   public void mouseExited(MouseEvent e)
513   {
514   }
515
516   @Override
517   public void mouseClicked(MouseEvent evt)
518   {
519     if (highlightNode != null)
520     {
521       if (evt.getClickCount() > 1)
522       {
523         tree.swapNodes(highlightNode);
524         tree.reCount(tree.getTopNode());
525         tree.findHeight(tree.getTopNode());
526       }
527       else
528       {
529         Vector<SequenceNode> leaves = tree.findLeaves(highlightNode);
530
531         for (int i = 0; i < leaves.size(); i++)
532         {
533           SequenceI seq = (SequenceI) leaves.elementAt(i).element();
534           treeSelectionChanged(seq);
535         }
536       }
537
538       PaintRefresher.Refresh(this, av.getSequenceSetId());
539       repaint();
540       av.sendSelection();
541     }
542   }
543
544   @Override
545   public void mouseDragged(MouseEvent ect)
546   {
547   }
548
549   @Override
550   public void mouseMoved(MouseEvent evt)
551   {
552     av.setCurrentTree(tree);
553
554     Object ob = findElement(evt.getX(), evt.getY());
555
556     if (ob instanceof SequenceNode)
557     {
558       highlightNode = (SequenceNode) ob;
559       repaint();
560     }
561     else
562     {
563       if (highlightNode != null)
564       {
565         highlightNode = null;
566         repaint();
567       }
568     }
569   }
570
571   @Override
572   public void mousePressed(MouseEvent e)
573   {
574     av.setCurrentTree(tree);
575
576     int x = e.getX();
577     int y = e.getY();
578
579     Object ob = findElement(x, y);
580
581     if (ob instanceof SequenceI)
582     {
583       treeSelectionChanged((Sequence) ob);
584       PaintRefresher.Refresh(this, av.getSequenceSetId());
585       repaint();
586       av.sendSelection();
587       return;
588     }
589     else if (!(ob instanceof SequenceNode))
590     {
591       // Find threshold
592
593       if (tree.getMaxHeight() != 0)
594       {
595         threshold = (float) (x - offx)
596                 / (float) (getSize().width - labelLength - 2 * offx);
597
598         List<SequenceNode> groups = tree.groupNodes(threshold);
599         setColor(tree.getTopNode(), Color.black);
600
601         av.setSelectionGroup(null);
602         av.getAlignment().deleteAllGroups();
603         av.clearSequenceColours();
604         final AlignViewportI codingComplement = av.getCodingComplement();
605         if (codingComplement != null)
606         {
607           codingComplement.setSelectionGroup(null);
608           codingComplement.getAlignment().deleteAllGroups();
609           codingComplement.clearSequenceColours();
610         }
611
612         colourGroups(groups);
613
614       }
615     }
616
617     PaintRefresher.Refresh(this, av.getSequenceSetId());
618     repaint();
619
620   }
621
622   void colourGroups(List<SequenceNode> groups)
623   {
624     for (int i = 0; i < groups.size(); i++)
625     {
626
627       Color col = new Color((int) (Math.random() * 255),
628               (int) (Math.random() * 255), (int) (Math.random() * 255));
629       setColor(groups.get(i), col.brighter());
630
631       Vector<SequenceNode> l = tree.findLeaves(groups.get(i));
632
633       Vector<SequenceI> sequences = new Vector<SequenceI>();
634       for (int j = 0; j < l.size(); j++)
635       {
636         SequenceI s1 = (SequenceI) l.elementAt(j).element();
637         if (!sequences.contains(s1))
638         {
639           sequences.addElement(s1);
640         }
641       }
642
643       ColourSchemeI cs = null;
644
645       SequenceGroup sg = new SequenceGroup(sequences, "", cs, true, true,
646               false, 0, av.getAlignment().getWidth() - 1);
647
648       if (av.getGlobalColourScheme() != null)
649       {
650         if (av.getGlobalColourScheme() instanceof UserColourScheme)
651         {
652           cs = new UserColourScheme(
653                   ((UserColourScheme) av.getGlobalColourScheme())
654                           .getColours());
655
656         }
657         else
658         {
659           cs = ColourSchemeProperty.getColourScheme(sg,
660                   ColourSchemeProperty.getColourName(av
661                           .getGlobalColourScheme()));
662         }
663         // cs is null if shading is an annotationColourGradient
664         // if (cs != null)
665         // {
666         // cs.setThreshold(av.getViewportColourScheme().getThreshold(),
667         // av.isIgnoreGapsConsensus());
668         // }
669       }
670       // TODO: cs used to be initialized with a sequence collection and
671       // recalcConservation called automatically
672       // instead we set it manually - recalc called after updateAnnotation
673       sg.setColourScheme(cs);
674       sg.getGroupColourScheme().setThreshold(
675               av.getResidueShading().getThreshold(),
676               av.isIgnoreGapsConsensus());
677
678       sg.setName("JTreeGroup:" + sg.hashCode());
679       sg.setIdColour(col);
680       if (av.getGlobalColourScheme() != null
681               && av.getResidueShading().conservationApplied())
682       {
683         Conservation c = new Conservation("Group", sg.getSequences(null),
684                 sg.getStartRes(), sg.getEndRes());
685
686         c.calculate();
687         c.verdict(false, av.getConsPercGaps());
688
689         sg.setColourScheme(cs);
690         sg.getGroupColourScheme().setConservation(c);
691       }
692
693       av.getAlignment().addGroup(sg);
694
695       // TODO this is duplicated with gui TreeCanvas - refactor
696       av.getAlignment().addGroup(sg);
697       final AlignViewportI codingComplement = av.getCodingComplement();
698       if (codingComplement != null)
699       {
700         SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg, av,
701                 codingComplement);
702         if (mappedGroup.getSequences().size() > 0)
703         {
704           codingComplement.getAlignment().addGroup(mappedGroup);
705           for (SequenceI seq : mappedGroup.getSequences())
706           {
707             // TODO why does gui require col.brighter() here??
708             codingComplement.setSequenceColour(seq, col);
709           }
710         }
711       }
712
713     }
714     ap.updateAnnotation();
715     if (av.getCodingComplement() != null)
716     {
717       ((AlignmentViewport) av.getCodingComplement()).firePropertyChange(
718               "alignment", null, ap.av.getAlignment().getSequences());
719     }
720   }
721
722   public void setShowDistances(boolean state)
723   {
724     this.showDistances = state;
725     repaint();
726   }
727
728   public void setShowBootstrap(boolean state)
729   {
730     this.showBootstrap = state;
731     repaint();
732   }
733
734   public void setMarkPlaceholders(boolean state)
735   {
736     this.markPlaceholders = state;
737     repaint();
738   }
739
740 }