| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 42 |
|
| 43 |
|
| 44 |
public class TreePlotter extends JComponent { |
| 45 |
|
| 46 |
private final static Color[] COLOUR_SCHEME = { |
| 47 |
new Color(0.879F, 0.261F, 0.262F), |
| 48 |
new Color(0.816F, 0.765F, 0.376F), |
| 49 |
new Color(0.26F, 0.141F, 0.632F), |
| 50 |
new Color(0.242F, 0.445F, 0.718F), |
| 51 |
new Color(0.592F, 0.669F, 0.295F), |
| 52 |
new Color(0.791F, 0.27F, 0.146F), |
| 53 |
new Color(0.359F, 0.425F, 0.833F), |
| 54 |
new Color(0.374F, 0.623F, 0.505F), |
| 55 |
new Color(0.785F, 0.585F, 0.209F), |
| 56 |
new Color(0.917F, 0.58F, 0.322F), |
| 57 |
new Color(0.64F, 0.46F, 0.28F), |
| 58 |
new Color(0.599F, 0.772F, 0.513F), |
| 59 |
new Color(0.551F, 0.242F, 0.598F), |
| 60 |
new Color(0.43F, 0.674F, 0.744F) |
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 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 |
|
| 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 |
|
| 172 |
|
| 173 |
|
| 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 |
|
| 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 |
|
| 190 |
|
| 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 |
|
| 194 |
|
| 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 |
} |