Debian Codegraph @

src/dr/app/treespace/CladePlotter.java

raw symbols SCIP
1 /*
2 * CladePlotter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.treespace;
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.geom.Ellipse2D;
31 import java.awt.geom.Line2D;
32 import java.util.ArrayList;
33 import java.util.List;
34 import dr.app.treespace.CladeSystem.Clade;
35
36 /**
37 * @author Andrew Rambaut
38 * @version $Id$
39 */
40 public class CladePlotter extends JComponent {
41
42 public CladePlotter(final CladeSystem cladeSystem) {
43 setCladeSystem(cladeSystem);
44 }
45
46 public void setCladeSystem(final CladeSystem cladeSystem) {
47 this.cladeSystem = cladeSystem;
48
49 this.clades = cladeSystem.getClades();
50
51 List<Clade> tips = new ArrayList<Clade>();
52 for (Clade clade : clades) {
53 if (clade.bits.cardinality() == 1) {
54 tips.add(clade);
55 }
56 }
57
58 for (Clade clade : clades) {
59 if (clade.bits.cardinality() == tips.size()) {
60 rootClade = clade;
61 }
62 }
63
64 isCalibrated = false;
65 repaint();
66 }
67
68 public void paint(Graphics g) {
69 if (rootClade == null) {
70 return;
71 }
72
73 if (!isCalibrated) {
74 double[] yPosition = { 0.0 };
75
76 calibrateClade((Graphics2D)g, rootClade, yPosition);
77
78 scaleX = ((double)getWidth()) / rootClade.x;
79 scaleY = ((double)getHeight()) / yPosition[0];
80
81 isCalibrated = true;
82 }
83
84 paintClade((Graphics2D)g, rootClade);
85 }
86
87 private void paintClade(Graphics2D g2, Clade clade) {
88 if (clade.children != null) {
89 for (Clade child : clade.children.keySet()) {
90 paintClade(g2, child);
91 g2.setColor(Color.black);
92 g2.setStroke(new BasicStroke(1.0F));
93 g2.draw(new Line2D.Double(transformX(child.x), transformY(child.y), transformX(clade.x), transformY(clade.y)));
94 }
95 } else {
96 paintTip(g2, clade);
97 }
98
99 float x = transformX(clade.x);
100 float y = transformY(clade.y);
101
102 Shape node = new Ellipse2D.Float(x, y, (float)(nodeWidth * scaleX), (float)(nodeHeight * scaleY));
103 g2.setColor(Color.yellow);
104 g2.fill(node);
105 g2.setColor(Color.black);
106 g2.draw(node);
107 }
108
109 private void paintTip(Graphics2D g2, Clade clade) {
110 float x = transformX(clade.x);
111 float y = transformY(clade.y);
112
113 Shape node = new Ellipse2D.Float(x, y, (float)(nodeWidth * scaleX), (float)(nodeHeight * scaleY));
114 g2.setColor(Color.yellow);
115 g2.fill(node);
116 g2.setColor(Color.black);
117 g2.draw(node);
118 if (clade.label != null) {
119 g2.drawString(clade.label, x + xLabelOffset, yLabelOffset);
120 }
121 }
122
123 private double calibrateClade(Graphics2D g2, Clade clade, double[] yPosition) {
124 double y;
125 if (clade.children != null) {
126 y = 0.0;
127 for (Clade child : clade.children.keySet()) {
128 y += calibrateClade(g2, child, yPosition);
129 }
130
131 // todo: use an average weighted by support
132 y /= clade.children.keySet().size();
133 } else {
134 y = yPosition[0];
135 yPosition[0]++;
136 }
137 clade.x = (float)clade.bits.cardinality();
138 clade.y = (float)y;
139
140 return y;
141 }
142
143 private float transformX(double x) {
144 return (float)getWidth() - (float)(x * scaleX);
145 }
146
147 private float transformY(double y) {
148 return (float)(y * scaleY);
149 }
150
151 CladeSystem cladeSystem;
152 Clade rootClade;
153 List<Clade> clades;
154
155 private double nodeWidth = 5.0;
156 private double nodeHeight = 5.0;
157
158 private float xLabelOffset = 5.0F;
159 private float yLabelOffset = 5.0F;
160 private boolean isCalibrated = false;
161
162 private double scaleX = 1.0;
163 private double scaleY = 1.0;
164
165 }