Debian Codegraph @

src/dr/app/treespace/TreePlotter.java

raw symbols SCIP
1 /*
2 * TreePlotter.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 dr.app.treespace.CladeSystem.Clade;
29 import jebl.evolution.graphs.Node;
30 import jebl.evolution.trees.RootedTree;
31 import jebl.math.Random;
32
33 import javax.swing.*;
34 import java.awt.*;
35 import java.awt.geom.*;
36 import java.awt.image.ColorModel;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 /**
41 * @author Andrew Rambaut
42 * @version $Id$
43 */
44 public class TreePlotter extends JComponent {
45
46 private final static Color[] COLOUR_SCHEME = {
47 new Color(0.879F, 0.261F, 0.262F), //Africa,
48 new Color(0.816F, 0.765F, 0.376F), //USA,
49 new Color(0.26F, 0.141F, 0.632F), //Taiwan,
50 new Color(0.242F, 0.445F, 0.718F), //China,
51 new Color(0.592F, 0.669F, 0.295F), //Russia,
52 new Color(0.791F, 0.27F, 0.146F), //Oceania,
53 new Color(0.359F, 0.425F, 0.833F), //Asia,
54 new Color(0.374F, 0.623F, 0.505F), //Japan,
55 new Color(0.785F, 0.585F, 0.209F), //Mexico,
56 new Color(0.917F, 0.58F, 0.322F), //South America,
57 new Color(0.64F, 0.46F, 0.28F), //Canada,
58 new Color(0.599F, 0.772F, 0.513F), //Europe,
59 new Color(0.551F, 0.242F, 0.598F), //Southeast Asia,
60 new Color(0.43F, 0.674F, 0.744F) //South Korea
61 };
62
63 public TreePlotter() {
64 setTrees(treeLineages);
65 }
66
67 public TreePlotter(final TreeLineages treeLineages) {
68 setTrees(treeLineages);
69 }
70
71 public void setTrees(final TreeLineages treeLineages) {
72 this.treeLineages = treeLineages;
73
74 isCalibrated = false;
75 repaint();
76 }
77
78 public void paint(Graphics g) {
79 if (treeLineages == null || treeLineages.getRootLineages().size() == 0) {
80 return;
81 }
82
83 if (!isCalibrated) {
84 // scaleX = (getBounds().getWidth() - borderWidth - borderWidth) / treeLineages.getMaxWidth();
85 // scaleY = (getBounds().getHeight() - borderWidth - borderWidth) / (treeLineages.getMaxHeight() - 1.0);
86 // offsetX = borderWidth;
87 // offsetY = borderWidth;
88
89 scaleX = (getBounds().getWidth() - borderWidth - borderWidth) / treeLineages.getAntigenicBounds().getWidth();
90 scaleY = (getBounds().getHeight() - topBorder - bottomBorder) / treeLineages.getAntigenicBounds().getHeight();
91 offsetX = borderWidth - (float)((treeLineages.getAntigenicBounds().getX() * scaleX));
92 offsetY = bottomBorder - (float)((treeLineages.getAntigenicBounds().getY() * scaleY));
93 // isCalibrated = true;
94 }
95
96 ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
97 float count = 0.0F;
98 for (TreeLineages.Lineage root : treeLineages.getRootLineages()) {
99 float hue = count / treeLineages.getRootLineages().size();
100 // paintTreeLineages((Graphics2D)g, hue, root);
101 paintAntigenicLineages((Graphics2D)g, hue, root);
102
103 count += 1.0F;
104 }
105 }
106
107 private void paintTreeLineages(Graphics2D g2, float hue, TreeLineages.Lineage lineage) {
108 double originX = treeLineages.getTreeBounds().getWidth() - lineage.dx;
109 double y1 = lineage.dy;
110
111 paintLineage(g2, hue, lineage.child1, originX, 0.0, y1);
112 paintLineage(g2, hue, lineage.child2, originX, 0.0, y1);
113 }
114
115 private void paintLineage(Graphics2D g2, float hue, TreeLineages.Lineage lineage, double originX, double x, double y) {
116 double x1 = lineage.dx;
117 double y1 = lineage.dy;
118
119 if (lineage.child1 != null) {
120 paintLineage(g2, hue, lineage.child1, originX, x1, y1);
121 paintLineage(g2, hue, lineage.child2, originX, x1, y1);
122 } else {
123 paintTip(g2, lineage, originX, x1, y1);
124 }
125
126 // g2.setColor(Color.getHSBColor(hue, 1.0F, 1.0F));
127 // g2.setColor(new Color(0.0F, 0.0F, 0.0F, 0.1F));
128 // g2.setColor(COLOUR_SCHEME[lineage.state]);
129 g2.setColor(new Color(COLOUR_SCHEME[lineage.state].getRed(), COLOUR_SCHEME[lineage.state].getGreen(), COLOUR_SCHEME[lineage.state].getBlue(), 32));
130
131 float thickness = 0.25F;
132
133 thickness = 2.5F * (Math.min((float)lineage.tipCount, 10.0F) / 10.0F);
134
135 g2.setStroke(new BasicStroke(thickness));
136 // Shape curve = new Line2D.Double(transformX(x), transformY(y), transformX(x1), transformY(y1));
137
138 double xp1 = transformX(originX + x);
139 double yp1 = transformY(y);
140 double xp2 = transformX(originX + x1);
141 double yp2 = transformY(y1);
142 double xc = (xp1 + xp2) / 2.0;
143
144 Shape curve = new CubicCurve2D.Double(xp1, yp1, xc, yp1, xc, yp2, xp2, yp2);
145
146 g2.draw(curve);
147
148 }
149
150 private void paintAntigenicLineages(Graphics2D g2, float hue, TreeLineages.Lineage lineage) {
151 double originX = treeLineages.getTreeBounds().getWidth() - lineage.dx;
152 double x1 = lineage.ax;
153 double y1 = lineage.ay;
154
155 paintAntigenicLineage(g2, hue, lineage.child1, originX, x1, y1);
156 paintAntigenicLineage(g2, hue, lineage.child2, originX, x1, y1);
157 }
158
159
160 private void paintAntigenicLineage(Graphics2D g2, float hue, TreeLineages.Lineage lineage, double originX, double x, double y) {
161 double x1 = lineage.ax;
162 double y1 = lineage.ay;
163
164 if (lineage.child1 != null) {
165 paintAntigenicLineage(g2, hue, lineage.child1, originX, x1, y1);
166 paintAntigenicLineage(g2, hue, lineage.child2, originX, x1, y1);
167 } else {
168 paintTip(g2, lineage, originX, x1, y1);
169 }
170
171 // g2.setColor(Color.getHSBColor(hue, 1.0F, 1.0F));
172 // g2.setColor(new Color(0.0F, 0.0F, 0.0F, 0.1F));
173 // g2.setColor(COLOUR_SCHEME[lineage.state]);
174 g2.setColor(new Color(COLOUR_SCHEME[lineage.state].getRed(), COLOUR_SCHEME[lineage.state].getGreen(), COLOUR_SCHEME[lineage.state].getBlue(), 32));
175
176 float thickness = 0.25F;
177
178 // thickness = 2.5F * (Math.min((float)lineage.tipCount, 10.0F) / 10.0F);
179
180 Shape curve = new Line2D.Double(transformX(originX + x), transformY(y), transformX(originX + x1), transformY(y1));
181
182
183 g2.draw(curve);
184
185 }
186
187 private void paintTip(Graphics2D g2, TreeLineages.Lineage lineage, double originX, double x, double y) {
188 Shape node = new Ellipse2D.Float((float)transformX(originX + x) - nodeWidth * 0.5F, (float)transformY(y) - nodeWidth * 0.5F, nodeWidth, nodeWidth);
189 // g2.setColor(Color.getHSBColor(((float)lineage.tipNumber) / (float)treeLineages.getMaxHeight(), 0.5F, 1.0F));
190 // g2.setColor(COLOUR_SCHEME[lineage.state]);
191 g2.setColor(new Color(COLOUR_SCHEME[lineage.state].getRed(), COLOUR_SCHEME[lineage.state].getGreen(), COLOUR_SCHEME[lineage.state].getBlue(), 32));
192 g2.fill(node);
193 // g2.setColor(Color.black);
194 // g2.draw(node);
195 }
196
197 private double transformX(double x) {
198 return (x * scaleX) + offsetX;
199 }
200
201 private double transformY(double y) {
202 return (y * scaleY) + offsetY;
203 }
204
205 private TreeLineages treeLineages;
206
207 private float xLabelOffset = 5.0F;
208 private float yLabelOffset = 5.0F;
209 private boolean isCalibrated = false;
210
211 private double scaleX = 1.0;
212 private double scaleY = 1.0;
213
214 private float nodeWidth = 2.0F;
215 private float topBorder = -450.0F;
216 private float bottomBorder = -350.0F;
217 private float borderWidth = 10.0F;
218
219 private float offsetX = 5.0F;
220 private float offsetY = 5.0F;
221 }