| 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.gui.tree; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
|
| 32 |
import java.awt.*; |
| 33 |
import java.awt.geom.Line2D; |
| 34 |
import java.awt.geom.Point2D; |
| 35 |
import java.awt.geom.Rectangle2D; |
| 36 |
import java.util.HashMap; |
| 37 |
import java.util.Map; |
| 38 |
|
| 39 |
public class SquareTreePainter implements TreePainter { |
| 40 |
|
| 41 |
protected Stroke lineStroke = new BasicStroke((float)2.0); |
| 42 |
protected Paint linePaint = Color.black; |
| 43 |
|
| 44 |
protected Stroke hilightStroke = new BasicStroke((float)2.0); |
| 45 |
protected Paint hilightPaint = Color.blue; |
| 46 |
|
| 47 |
protected int maxFontSize = 12; |
| 48 |
|
| 49 |
protected Font labelFont = new Font("Helvetica", Font.PLAIN, 12); |
| 50 |
protected Paint labelPaint = Color.black; |
| 51 |
|
| 52 |
protected Font hilightLabelFont = new Font("Helvetica", Font.PLAIN, 12); |
| 53 |
protected Paint hilightLabelPaint = Color.blue; |
| 54 |
|
| 55 |
final int boxPlotSize = 3; |
| 56 |
|
| 57 |
public SquareTreePainter() { |
| 58 |
this.rememberYPositions = false; |
| 59 |
} |
| 60 |
|
| 61 |
public SquareTreePainter(boolean rememberYPositions) { |
| 62 |
this.rememberYPositions = rememberYPositions; |
| 63 |
} |
| 64 |
|
| 65 |
public SquareTreePainter(TaxonList taxonList) { |
| 66 |
|
| 67 |
if (taxonList != null) { |
| 68 |
|
| 69 |
|
| 70 |
double y = 0.5; |
| 71 |
for (int i = 0; i < taxonList.getTaxonCount(); i++) { |
| 72 |
yPositionMap.put(taxonList.getTaxonId(i), y); |
| 73 |
y += 1.0; |
| 74 |
} |
| 75 |
} |
| 76 |
this.rememberYPositions = true; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public void setLineStyle(Stroke lineStroke, Paint linePaint) { |
| 83 |
this.lineStroke = lineStroke; |
| 84 |
this.linePaint = linePaint; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void setLinePaint(Paint linePaint) { |
| 91 |
this.linePaint = linePaint; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public void setHilightStyle(Stroke hilightStroke, Paint hilightPaint) { |
| 99 |
this.hilightStroke = hilightStroke; |
| 100 |
this.hilightPaint = hilightPaint; |
| 101 |
} |
| 102 |
|
| 103 |
public void setFontSize(int size) { |
| 104 |
maxFontSize = size; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public void setLabelStyle(Font labelFont, Paint labelPaint) { |
| 111 |
this.labelFont = labelFont; |
| 112 |
this.labelPaint = labelPaint; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void setHilightLabelStyle(Font hilightLabelFont, Paint hilightLabelPaint) { |
| 119 |
this.hilightLabelFont = hilightLabelFont; |
| 120 |
this.hilightLabelPaint = hilightLabelPaint; |
| 121 |
} |
| 122 |
|
| 123 |
public void setUserDefinedHeight(double height) { |
| 124 |
this.userDefinedHeight = height; |
| 125 |
} |
| 126 |
|
| 127 |
public void drawLabels(boolean drawLabels) { |
| 128 |
this.drawLabels = drawLabels; |
| 129 |
} |
| 130 |
|
| 131 |
public void drawHorizontals(boolean drawHorizontals) { |
| 132 |
this.drawHorizontals = drawHorizontals; |
| 133 |
} |
| 134 |
|
| 135 |
public void drawVerticals(boolean drawVerticals) { |
| 136 |
this.drawVerticals = drawVerticals; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void paintTree(Graphics2D g2, Dimension size, Tree tree) { |
| 143 |
if (tree == null) return; |
| 144 |
|
| 145 |
int n = tree.getNodeCount(); |
| 146 |
if (nodeRectVert == null || nodeRectVert.length != n) { |
| 147 |
nodeRectVert = new Rectangle2D[n]; |
| 148 |
nodeRectHoriz = new Rectangle2D[n]; |
| 149 |
} |
| 150 |
|
| 151 |
scaleY = ((double)size.height) / (tree.getExternalNodeCount()); |
| 152 |
|
| 153 |
double maxLabelHeight = scaleY; |
| 154 |
|
| 155 |
int fontSize = maxFontSize + 1; |
| 156 |
do { |
| 157 |
fontSize --; |
| 158 |
labelFont = new Font("Helvetica", Font.PLAIN, fontSize); |
| 159 |
g2.setFont(labelFont); |
| 160 |
} while (fontSize > 1 && |
| 161 |
g2.getFontMetrics().getAscent() |
| 162 |
|
| 163 |
> maxLabelHeight); |
| 164 |
|
| 165 |
hilightLabelFont = new Font("Helvetica", Font.PLAIN, fontSize); |
| 166 |
|
| 167 |
double maxLabelWidth = getMaxLabelWidth(g2, tree); |
| 168 |
|
| 169 |
currentY = 0.5; |
| 170 |
|
| 171 |
treeHeight = tree.getNodeHeight(tree.getRoot()); |
| 172 |
double height; |
| 173 |
if (userDefinedHeight < 0.0) { |
| 174 |
height = treeHeight; |
| 175 |
} else { |
| 176 |
height = userDefinedHeight; |
| 177 |
} |
| 178 |
|
| 179 |
scaleX = ((double)size.width - 4 - maxLabelWidth) / (height * 1.02); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
currentY = 0.5; |
| 186 |
paintBoxPlot(g2, tree, tree.getRoot(), treeHeight, false); |
| 187 |
currentY = 0.5; |
| 188 |
paintBoxPlot(g2, tree, tree.getRoot(), treeHeight, false); |
| 189 |
currentY = 0.5; |
| 190 |
paintNode(g2, tree, tree.getRoot(), (treeHeight * 1.02), treeHeight, false); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private double paintNode(Graphics2D g2, Tree tree, NodeRef node, |
| 199 |
double x0, double x1, boolean hilight) { |
| 200 |
|
| 201 |
double y; |
| 202 |
|
| 203 |
double ix0 = convertX(x0); |
| 204 |
double ix1 = convertX(x1); |
| 205 |
double iy; |
| 206 |
|
| 207 |
if (tree.getNodeAttribute(node, "selected") != null) { |
| 208 |
hilight = true; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if (tree.isExternal(node)) { |
| 220 |
|
| 221 |
if (rememberYPositions) { |
| 222 |
|
| 223 |
String taxonId = tree.getNodeTaxon(node).getId(); |
| 224 |
Double pos = yPositionMap.get(taxonId); |
| 225 |
if (pos != null) { |
| 226 |
y = pos; |
| 227 |
} else { |
| 228 |
y = currentY; |
| 229 |
currentY += 1.0; |
| 230 |
yPositionMap.put(taxonId, y); |
| 231 |
} |
| 232 |
} else { |
| 233 |
y = currentY; |
| 234 |
currentY += 1.0; |
| 235 |
} |
| 236 |
|
| 237 |
if (hilight) { |
| 238 |
g2.setPaint(hilightLabelPaint); |
| 239 |
g2.setFont(hilightLabelFont); |
| 240 |
} else { |
| 241 |
g2.setPaint(labelPaint); |
| 242 |
g2.setFont(labelFont); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
String label = tree.getTaxonId(node.getNumber()); |
| 247 |
double labelWidth = g2.getFontMetrics().stringWidth(label); |
| 248 |
double labelHeight = g2.getFontMetrics().getAscent(); |
| 249 |
double labelOffset = labelHeight / 2; |
| 250 |
|
| 251 |
iy = convertY(y); |
| 252 |
|
| 253 |
if (label != null && label.length() > 0 && drawLabels) { |
| 254 |
g2.drawString(label, (float)(ix1 + 4), (float)(iy + labelOffset)); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
nodeRectVert[node.getNumber()] = |
| 259 |
new Rectangle.Double(ix1 + 4, iy, labelWidth, labelHeight); |
| 260 |
|
| 261 |
if (hilight) { |
| 262 |
g2.setPaint(hilightPaint); |
| 263 |
g2.setStroke(hilightStroke); |
| 264 |
} else { |
| 265 |
|
| 266 |
if (colorAttribute != null) { |
| 267 |
Paint c = (Color)tree.getNodeAttribute(node,colorAttribute); |
| 268 |
if (c == null) c = linePaint; |
| 269 |
g2.setPaint(c); |
| 270 |
} else { |
| 271 |
g2.setPaint(linePaint); |
| 272 |
} |
| 273 |
if (lineAttribute != null) { |
| 274 |
Stroke stroke = (Stroke)tree.getNodeAttribute(node,lineAttribute); |
| 275 |
if (stroke == null) stroke = lineStroke; |
| 276 |
g2.setStroke(stroke); |
| 277 |
} else g2.setStroke(lineStroke); |
| 278 |
} |
| 279 |
|
| 280 |
} else { |
| 281 |
double y0, y1; |
| 282 |
|
| 283 |
NodeRef child = tree.getChild(node, 0); |
| 284 |
double length = tree.getNodeHeight(node) - tree.getNodeHeight(child); |
| 285 |
|
| 286 |
y0 = paintNode(g2, tree, child, x1, x1-length, hilight); |
| 287 |
y1 = y0; |
| 288 |
|
| 289 |
for (int i = 1; i < tree.getChildCount(node); i++) { |
| 290 |
child = tree.getChild(node, i); |
| 291 |
length = tree.getNodeHeight(node) - tree.getNodeHeight(child); |
| 292 |
|
| 293 |
y1 = paintNode(g2, tree, child, x1, x1-length, hilight); |
| 294 |
} |
| 295 |
|
| 296 |
double iy0 = convertY(y0); |
| 297 |
double iy1 = convertY(y1); |
| 298 |
|
| 299 |
if (hilight) { |
| 300 |
g2.setPaint(hilightPaint); |
| 301 |
g2.setStroke(hilightStroke); |
| 302 |
} else { |
| 303 |
|
| 304 |
if (colorAttribute != null) { |
| 305 |
Paint c = (Color)tree.getNodeAttribute(node,colorAttribute); |
| 306 |
if (c == null) c = linePaint; |
| 307 |
g2.setPaint(c); |
| 308 |
} else { |
| 309 |
g2.setPaint(linePaint); |
| 310 |
} |
| 311 |
if (lineAttribute != null) { |
| 312 |
Stroke stroke = (Stroke)tree.getNodeAttribute(node,lineAttribute); |
| 313 |
if (stroke == null) stroke = lineStroke; |
| 314 |
g2.setStroke(stroke); |
| 315 |
} else g2.setStroke(lineStroke); |
| 316 |
} |
| 317 |
|
| 318 |
if (drawHorizontals) { |
| 319 |
Line2D line = new Line2D.Double(ix1, iy0, ix1, iy1); |
| 320 |
g2.draw(line); |
| 321 |
} |
| 322 |
|
| 323 |
nodeRectVert[node.getNumber()] = new Rectangle.Double(ix1-2, iy0-2, 5, (iy1 - iy0) + 4); |
| 324 |
|
| 325 |
y = (y1 + y0) / 2; |
| 326 |
iy = convertY(y); |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
if (drawVerticals) { |
| 331 |
Line2D line = new Line2D.Double(ix0, iy, ix1, iy); |
| 332 |
g2.draw(line); |
| 333 |
} |
| 334 |
|
| 335 |
nodeRectHoriz[node.getNumber()] = new Rectangle.Double(ix0-2, iy-2, (ix1 - ix0) + 4, 5); |
| 336 |
|
| 337 |
|
| 338 |
if (shapeAttribute != null) { |
| 339 |
Shape shape = (Shape)tree.getNodeAttribute(node,shapeAttribute); |
| 340 |
if (shape != null) { |
| 341 |
Rectangle bounds = shape.getBounds(); |
| 342 |
double tx = ix1-bounds.getWidth()/2.0; |
| 343 |
double ty = iy-bounds.getHeight()/2.0; |
| 344 |
g2.translate(tx,ty); |
| 345 |
g2.fill(shape); |
| 346 |
g2.translate(-tx,-ty); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (labelAttribute != null) { |
| 351 |
Object label = tree.getNodeAttribute(node,labelAttribute); |
| 352 |
if (label != null) { |
| 353 |
Color c = g2.getColor(); |
| 354 |
Font f = g2.getFont(); |
| 355 |
Font fsmall = f.deriveFont(f.getSize()-1.0f); |
| 356 |
g2.setFont(fsmall); |
| 357 |
String labelString = label.toString(); |
| 358 |
int width = g2.getFontMetrics().stringWidth(labelString); |
| 359 |
g2.setColor(textColor); |
| 360 |
g2.drawString(labelString,(float)(ix1-width-1.0),(float)(iy-2.0)); |
| 361 |
|
| 362 |
|
| 363 |
g2.setColor(c); |
| 364 |
g2.setFont(f); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
return y; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
private double paintBoxPlot(Graphics2D g2, Tree tree, NodeRef node, double x1, boolean fill) { |
| 376 |
|
| 377 |
double y; |
| 378 |
|
| 379 |
double iy; |
| 380 |
|
| 381 |
if (tree.isExternal(node)) { |
| 382 |
|
| 383 |
if (rememberYPositions) { |
| 384 |
|
| 385 |
String taxonId = tree.getNodeTaxon(node).getId(); |
| 386 |
Double pos = yPositionMap.get(taxonId); |
| 387 |
if (pos != null) { |
| 388 |
y = pos; |
| 389 |
} else { |
| 390 |
y = currentY; |
| 391 |
currentY += 1.0; |
| 392 |
yPositionMap.put(taxonId, y); |
| 393 |
} |
| 394 |
} else { |
| 395 |
y = currentY; |
| 396 |
currentY += 1.0; |
| 397 |
} |
| 398 |
|
| 399 |
iy = convertY(y); |
| 400 |
|
| 401 |
} else { |
| 402 |
double y0, y1; |
| 403 |
|
| 404 |
NodeRef child = tree.getChild(node, 0); |
| 405 |
double length = tree.getNodeHeight(node) - tree.getNodeHeight(child); |
| 406 |
|
| 407 |
y0 = paintBoxPlot(g2, tree, child, x1-length, fill); |
| 408 |
y1 = y0; |
| 409 |
|
| 410 |
for (int i = 1; i < tree.getChildCount(node); i++) { |
| 411 |
child = tree.getChild(node, i); |
| 412 |
length = tree.getNodeHeight(node) - tree.getNodeHeight(child); |
| 413 |
|
| 414 |
y1 = paintBoxPlot(g2, tree, child, x1-length, fill); |
| 415 |
} |
| 416 |
|
| 417 |
y = (y1 + y0) / 2; |
| 418 |
iy = convertY(y); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
Double mean = (Double)tree.getNodeAttribute(node, "nodeHeight.mean"); |
| 423 |
if (mean != null) { |
| 424 |
if (tree.isRoot(node)) { |
| 425 |
System.out.println(mean.doubleValue()); |
| 426 |
} |
| 427 |
|
| 428 |
Double hpdUpper = (Double)tree.getNodeAttribute(node, "nodeHeight.hpdUpper"); |
| 429 |
Double hpdLower = (Double)tree.getNodeAttribute(node, "nodeHeight.hpdLower"); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
double upperX = convertX(hpdUpper); |
| 438 |
double lowerX = convertX(hpdLower); |
| 439 |
|
| 440 |
|
| 441 |
g2.setStroke(lineStroke); |
| 442 |
if (fill) { |
| 443 |
g2.setColor(Color.white); |
| 444 |
g2.fill(new Rectangle2D.Double(upperX, iy-boxPlotSize,lowerX-upperX,2*boxPlotSize)); |
| 445 |
} |
| 446 |
g2.setColor(Color.gray); |
| 447 |
g2.draw(new Rectangle2D.Double(upperX, iy-boxPlotSize,lowerX-upperX,2*boxPlotSize)); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
} |
| 454 |
|
| 455 |
return y; |
| 456 |
} |
| 457 |
|
| 458 |
private double convertX(double x) { |
| 459 |
return ((treeHeight*1.02)-x) * scaleX; |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
private double convertY(double y) { |
| 464 |
return y * scaleY; |
| 465 |
|
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
private double getMaxLabelWidth(Graphics2D g, Tree tree) { |
| 472 |
double maxLabelWidth = 0.0; |
| 473 |
for (int i = 0; i < tree.getTaxonCount(); i++) { |
| 474 |
String label = tree.getTaxonId(i); |
| 475 |
double labelWidth = g.getFontMetrics().stringWidth(label); |
| 476 |
if (labelWidth > maxLabelWidth) |
| 477 |
maxLabelWidth = labelWidth; |
| 478 |
} |
| 479 |
return maxLabelWidth; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public final int findNodeAtPoint(Point2D point) { |
| 486 |
if (nodeRectVert == null || nodeRectHoriz == null) return -1; |
| 487 |
|
| 488 |
int i = 0; |
| 489 |
double x = point.getX(); |
| 490 |
double y = point.getY(); |
| 491 |
while (i < nodeRectVert.length) { |
| 492 |
if (nodeRectVert[i].contains(x, y) || |
| 493 |
nodeRectHoriz[i].contains(x, y) ) |
| 494 |
return i; |
| 495 |
else |
| 496 |
i++; |
| 497 |
} |
| 498 |
|
| 499 |
return -1; |
| 500 |
} |
| 501 |
|
| 502 |
public void setColorAttribute(String s) { |
| 503 |
colorAttribute = s; |
| 504 |
} |
| 505 |
|
| 506 |
public void setLineAttribute(String s) { |
| 507 |
lineAttribute = s; |
| 508 |
} |
| 509 |
|
| 510 |
public void setShapeAttribute(String s) { |
| 511 |
shapeAttribute = s; |
| 512 |
} |
| 513 |
|
| 514 |
public void setLabelAttribute(String s) { |
| 515 |
labelAttribute = s; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
private double scaleX, scaleY, currentY; |
| 521 |
private double treeHeight; |
| 522 |
|
| 523 |
private Rectangle2D[] nodeRectVert; |
| 524 |
private Rectangle2D[] nodeRectHoriz; |
| 525 |
|
| 526 |
private final Color textColor = Color.black; |
| 527 |
private final Map<String, Double> yPositionMap = new HashMap<String, Double>(); |
| 528 |
private boolean rememberYPositions = false; |
| 529 |
private double userDefinedHeight = -1.0; |
| 530 |
private boolean drawLabels = true; |
| 531 |
private boolean drawHorizontals = true; |
| 532 |
private boolean drawVerticals = true; |
| 533 |
|
| 534 |
private String colorAttribute = null; |
| 535 |
private String lineAttribute = null; |
| 536 |
private String shapeAttribute = null; |
| 537 |
private String labelAttribute = null; |
| 538 |
|
| 539 |
} |