Debian Codegraph @

src/dr/app/phylogeography/tools/ContinuousKML.java

raw symbols SCIP
1 /*
2 * ContinuousKML.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.phylogeography.tools;
27
28 import dr.stats.DiscreteStatistics;
29 import jebl.evolution.trees.RootedTree;
30 import jebl.evolution.graphs.Node;
31
32 /**
33 * @author Philippe Lemey
34 * @author Andrew Rambaut
35 * @author Marc A. Suchard
36 */
37 public class ContinuousKML {
38
39 //input related variables
40 RootedTree treeToExport;
41 String traitName;
42 String latitudeName;
43 String longitudeName;
44 String documentName;
45 boolean tipsSampled = true; //in case the tips are sampled within a polygon, we would like to see their contours
46
47 // variables shared by rectangle, triangle and surfacetree
48 double branchWidthConstant = 3.0; // the width of branches will be posterior*branchWidthMultiplier+branchWidthConstant
49 double branchWidthMultiplier = 5.0;
50
51 // variables shared by rectangle and triangle
52 double plotHeight = 2000000;
53 double fraction = 0.05; // additional fraction of the total plotHeight for the root branch
54
55 // rectangle tree variables
56 boolean iniVisi_RT = false; // initial visibility of rectangle tree
57 boolean usePosterior_RT = false; // use posterior probabilities for branch width
58 double branchWidth_RT = 5.0; // branch width if posterior probabilities are not used
59 boolean useRates_RT = true; // use rates to color branch
60 boolean useLogRates_RT = false; // use rates to color branch
61 boolean useHeights_RT = false; // use heights (time) to color branches
62 String startBranchColor_RT = "ffffff"; //blue=B36600
63 String endBranchColor_RT = "0000FF"; //red=0000FF
64 String branchColor_RT = "ffffff"; // branch color if color range based on rates is not used
65 double opacity_RT = 1.0;
66 double branchChop_RT = 1.0;
67
68 // triangle tree variables
69 boolean iniVisi_TT = false; // initial visibility of rectangle tree
70 boolean usePosterior_TT = false; // use posterior probabilities for branch width
71 double branchWidth_TT = 5.0; // branch width if posterior probabilities are not used
72 boolean useRates_TT = true; // use rates to color branch
73 boolean useLogRates_TT = false; // use rates to color branch
74 String startBranchColor_TT = "0000FF"; //blue=B36600
75 String endBranchColor_TT = "ffffff"; //red=0000FF
76 String branchColor_TT = "ffffff"; // branch color if color range based on rates is not used
77 double opacity_TT = 1.0;
78
79 // surface tree variables
80 double divider = 10; // this is to chop up the branches of the surface tree in 'divider' segments
81 boolean iniVisi_ST = true; // initial visibility of rectangle tree
82 boolean usePosterior_ST = false; // use posterior probabilities for branch width
83 double branchWidth_ST = 6.0; // branch width if posterior probabilities are not used
84 boolean useHeights_ST = true; // use heights (time) to color branches
85 String startBranchColor_ST = "FF00FF"; //"00FF00"=green,"FF00FF"=magenta startpoint is the youngest node
86 String endBranchColor_ST = "FBC042"; //"00F1D6"=yellow,blue="B36600",carribean="FBC042"
87 String branchColor_ST = "ffffff"; // branch color if rates are not used
88 double opacity_ST = 1.0;
89 boolean arcBranches = true; // branches are arcs with heights proportional to the time length of the branches
90 double altitudeFactor = 100; // this is the factor with which to multiply the time of the branch to get the altitude for that branch in the surface Tree
91
92 // tree slice variables
93 String startBranchColor_TS = "00FF00"; //green, startpoint is the youngest node
94 String endBranchColor_TS = "00F1D6"; //yellow
95
96 // taxa variables
97 boolean iniVisi_taxa = false;
98
99 // spade variables
100 boolean iniVisi_spades = false;
101 boolean useHeights_spades = true; // use heights (time) to color branches
102 String startSpadeColor = "00FF00"; //green, startpoint is the youngest node
103 String endSpadeColor = "00F1D6"; //yellow
104 String spadeColor = "ffffff"; // branch color if heights are not used
105 double opacity_spades = 1.0;
106 //specifically for tips
107 boolean iniVisi_tipSpades = false;
108 boolean useHeights_tipSpades = true; // use heights (time) to color branches
109 String startTipSpadeColor = "00F1D6"; //green, startpoint is the youngest node
110 String endTipSpadeColor = "00FF00"; //yellow
111 String tipSpadeColor = "ffffff"; // branch color if heights are not used
112 double opacity_tipSpades = 1.0;
113
114 // general location HPD variables
115 String locationHPDpercentage = "80%";
116
117 // contour variables
118 boolean iniVisi_contours = false;
119 boolean useHeights_contours = true; // use heights (time) to color branches
120 String startContourColor = "ffffff"; //green, startpoint is the youngest node
121 String endContourColor = "0000FF"; //yellow
122 String contourColor = "ffffff"; // branch color if heights are not used
123 double opacity_contours = 0.6;
124 //specifically for tips
125 boolean iniVisi_tipContours = false;
126 boolean useHeights_tipContours = true; // use heights (time) to color branches
127 String startTipContourColor = "00F1D6"; //green, startpoint is the youngest node
128 String endTipContourColor = "00FF00"; //yellow
129 String tipContourColor = "ffffff"; // branch color if heights are not used
130 double opacity_tipContours = 0.6;
131
132 // ground spade variables
133 boolean iniVisi_groundSpades = false;
134 boolean useHeights_groundSpades = true; // use heights (time) to color branches
135 String startgroundSpadeColor = "00FF00"; //green, startpoint is the youngest node
136 String endgroundSpadeColor = "00F1D6"; //yellow
137 String groundSpadeColor = "ffffff"; // branch color if heights are not used
138 double opacity_groundSpades = 0.6;
139 //specifically for tips
140 boolean iniVisi_groundTipSpades = false;
141 boolean useHeights_groundTipSpades = true; // use heights (time) to color branches
142 String startgroundTipSpadeColor = "00FF00"; //green, startpoint is the youngest node
143 String endgroundTipSpadeColor = "00F1D6"; //yellow
144 String groundTipSpadeColor = "ffffff"; // branch color if heights are not used
145 double opacity_groundTipSpades = 0.6;
146
147 // ground contour variables
148 boolean contoursAndNotSpades = true;
149 boolean iniVisi_groundContours = true;
150 boolean useHeights_groundContours = true; // use heights (time) to color branches
151 String startgroundContourColor = "00FF00"; //green, startpoint is the youngest node
152 String endgroundContourColor = "00F1D6"; //yellow
153 String groundContourColor = "ffffff"; // branch color if rates are not used
154 double opacity_groundContours = 0.5;
155 //specifically for tips
156 boolean iniVisi_groundTipContours = true;
157 boolean useHeights_groundTipContours = true; // use heights (time) to color branches
158 String startgroundTipContourColor = "00FF00"; //green, startpoint is the youngest node
159 String endgroundTipContourColor = "00F1D6"; //yellow
160 String groundTipContourColor = "ffffff"; // branch color if rates are not used
161 double opacity_groundTipContours = 1.0;
162
163 // diamond variables
164 boolean iniVisi_diamonds = false;
165
166 //projections variables
167 boolean iniVisi_projections = false;
168
169 // additional variables
170 double[] rateMinMaxMedian; // used to calibrate the color range for the branches
171 double[] heightMinAndMax; // used to calibrate the color range for branches or node hpd polygons
172 double mostRecentDate; // required to convert heights to calendar dates
173 boolean ancient = false;
174
175 //everything is written to separate buffers, and than collected in structured KML document by compileBuffer
176 StringBuffer rectangleTreeBuffer = new StringBuffer();
177 StringBuffer triangleTreeBuffer = new StringBuffer();
178 StringBuffer spadeBuffer = new StringBuffer();
179 StringBuffer spadeTipsBuffer = new StringBuffer();
180 StringBuffer groundSpadeBuffer = new StringBuffer();
181 StringBuffer groundSpadeTipsBuffer = new StringBuffer();
182 StringBuffer contourBuffer = new StringBuffer();
183 StringBuffer contourTipsBuffer = new StringBuffer();
184 StringBuffer groundContourBuffer = new StringBuffer();
185 StringBuffer groundContourTipsBuffer = new StringBuffer();
186 StringBuffer diamondBuffer = new StringBuffer();
187 StringBuffer diamondTipsBuffer = new StringBuffer();
188 StringBuffer taxaBuffer = new StringBuffer();
189 StringBuffer surfaceTreeBuffer = new StringBuffer();
190 StringBuffer projectionsBuffer = new StringBuffer();
191 StringBuffer styleBuffer = new StringBuffer();
192 StringBuffer treeSliceBuffer = new StringBuffer();
193 StringBuffer contourSliceBuffer = new StringBuffer();
194
195 double totalHPDarea;
196
197 public ContinuousKML(){
198 }
199
200 public ContinuousKML(RootedTree tree, String name, double height, double date, String coordinateName){
201 treeToExport = tree;
202 plotHeight = height;
203 documentName = name;
204 mostRecentDate = date;
205 latitudeName = coordinateName+"1";
206 longitudeName = coordinateName+"2";
207 traitName = coordinateName;
208
209 Node rootNode = treeToExport.getRootNode();
210
211 if ((Object)rootNode.getAttribute(traitName+"_"+locationHPDpercentage+"HPD_modality") != null) {
212 contoursAndNotSpades = true;
213 }
214
215 if (mostRecentDate - treeToExport.getHeight(treeToExport.getRootNode()) < 0) {
216 ancient = true;
217 }
218
219 }
220
221 public void writeTreeToKML() {
222
223 double scaleFactor = plotHeight / treeToExport.getHeight(treeToExport.getRootNode());
224
225 heightMinAndMax = getHeightMinAndMax(treeToExport);
226 if (useRates_RT || useRates_TT) {
227 rateMinMaxMedian = getRateMinMaxMedian(treeToExport, useLogRates_TT);
228 }
229
230 // convert initial visibility booleans to int (0 = false, 1 = true)
231 int visibility_RT;
232 if (iniVisi_RT) { visibility_RT = 1; } else { visibility_RT = 0; }
233 int visibility_TT;
234 if (iniVisi_TT) { visibility_TT = 1; } else { visibility_TT = 0; }
235 int visibility_ST;
236 if (iniVisi_ST) { visibility_ST = 1; } else { visibility_ST = 0; }
237 int visibility_taxa;
238 if (iniVisi_taxa) { visibility_taxa = 1; } else { visibility_taxa = 0; }
239
240
241 //
242
243 int nodeNumber = 0;
244 for (Node node : treeToExport.getNodes()) {
245 nodeNumber++;
246 Double longitude = (Double)node.getAttribute(longitudeName);
247 Double latitude = (Double)node.getAttribute(latitudeName);
248 double altitude = (treeToExport.getHeight(node)*scaleFactor);
249
250 if (!treeToExport.isRoot(node)) {
251
252 Node parentNode = treeToExport.getParent(node);
253 Double parentLongitude = (Double)parentNode.getAttribute(longitudeName);
254
255 if (parentLongitude == null) {
256 throw new RuntimeException("Longitude attribute, " + latitudeName + ", not found in tree");
257 }
258
259 Double parentLatitude = (Double)parentNode.getAttribute(latitudeName);
260
261 if (parentLatitude == null) {
262 throw new RuntimeException("Latitude attribute, " + latitudeName + ", not found in tree");
263 }
264
265 double parentAltitude = (treeToExport.getHeight(parentNode)*scaleFactor);
266
267 if (plotHeight > 0) {
268
269 rectangleTreeBuffer.append("\t\t<Placemark>\r");
270
271 rectangleTreeBuffer.append("\t\t\t<visibility>"+visibility_RT+"</visibility>\r");
272
273 rectangleTreeBuffer.append("\t\t\t<name>rectangleTreeBranch"+ nodeNumber +"</name>\r");
274 rectangleTreeBuffer.append("\t\t\t<styleUrl>#rectangleTreeBranch"+ nodeNumber +"_style</styleUrl>\r");
275 rectangleTreeBuffer.append("\t\t\t<LineString>\r");
276
277 rectangleTreeBuffer.append("\t\t\t\t<tessellate>1</tessellate>\r");
278 rectangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
279
280 rectangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
281 rectangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitude+"\r");
282 rectangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+parentAltitude+"\r");
283 if (branchChop_RT > 0) {
284 double longInterval = parentLongitude - longitude;
285 double latInterval = parentLatitude - latitude;
286 double steps;
287 double longStepSize;
288 double latStepSize;
289
290 if (Math.abs(longInterval) > Math.abs(latInterval)) {
291
292 steps = (Math.abs(longInterval)/branchChop_RT) - 1;
293 if (longInterval > 0) {
294 longStepSize = branchChop_RT;
295 } else {
296 longStepSize = -branchChop_RT;
297 }
298 latStepSize = latInterval/(steps + 1);
299
300 } else {
301
302 steps = (Math.abs(latInterval)/branchChop_RT) - 1;
303 if (latInterval > 0) {
304 latStepSize = branchChop_RT;
305 } else {
306 latStepSize = -branchChop_RT;
307 }
308 longStepSize = longInterval/(steps + 1);
309 }
310
311 for (int x = 0; x < steps; x++) {
312 rectangleTreeBuffer.append("\t\t\t\t\t"+(longitude + (x+1)*longStepSize)+","+(latitude + (x+1)*latStepSize)+","+parentAltitude+"\r");
313 }
314 }
315
316 rectangleTreeBuffer.append("\t\t\t\t\t"+parentLongitude+","+parentLatitude+","+parentAltitude+"\r");
317 rectangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
318
319 rectangleTreeBuffer.append("\t\t\t</LineString>\r");
320
321 rectangleTreeBuffer.append("\t\t</Placemark>\r");
322
323 triangleTreeBuffer.append("\t\t<Placemark>\r");
324 triangleTreeBuffer.append("\t\t\t<visibility>"+visibility_TT+"</visibility>\r");
325
326 triangleTreeBuffer.append("\t\t\t<name>triangleTreeBranch"+ nodeNumber +"</name>\r");
327 triangleTreeBuffer.append("\t\t\t<styleUrl>#triangleTreeBranch"+ nodeNumber +"_style</styleUrl>\r");
328 triangleTreeBuffer.append("\t\t\t<LineString>\r");
329
330 triangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
331
332 triangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
333 triangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitude+"\r");
334 triangleTreeBuffer.append("\t\t\t\t\t"+parentLongitude+","+parentLatitude+","+parentAltitude+"\r");
335 triangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
336
337 triangleTreeBuffer.append("\t\t\t</LineString>\r");
338 triangleTreeBuffer.append("\t\t</Placemark>\r");
339 }
340
341 //variables required for chopping up the branches of the surface Tree
342 double maxAltitude = (treeToExport.getHeight(parentNode) - treeToExport.getHeight(node))*altitudeFactor;
343 double latitudeDifference = parentLatitude - latitude;
344 double longitudeDifference = parentLongitude - longitude;
345
346 surfaceTreeBuffer.append("\t<Folder>\r");
347
348 surfaceTreeBuffer.append("\t\t\t<name>surfaceTreeBranch"+ nodeNumber +"</name>\r");
349 for (int a = 0; a < divider; a ++) {
350
351 surfaceTreeBuffer.append("\t\t<Placemark>\r");
352
353 surfaceTreeBuffer.append("\t\t\t<visibility>"+visibility_ST+"</visibility>\r");
354 surfaceTreeBuffer.append("\t\t\t<name>surfaceTreeBranch"+ nodeNumber +"_part"+(a+1)+"</name>\r");
355
356 surfaceTreeBuffer.append("\t\t\t<TimeSpan>\r");
357 //convert height of the branch segment to a real date (based on th date for the most recent sample)
358 double date = mostRecentDate - (treeToExport.getHeight(node) + (a + 1) *
359 ((treeToExport.getHeight(parentNode) - (treeToExport.getHeight(node)))/divider));
360 String[] yearMonthDay = convertToYearMonthDay(date);
361 if (ancient) {
362 surfaceTreeBuffer.append("\t\t\t\t<begin>"+Math.round(date)+"</begin>\r");
363 } else {
364 surfaceTreeBuffer.append("\t\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r");
365 }
366 surfaceTreeBuffer.append("\t\t\t</TimeSpan>\r");
367
368 surfaceTreeBuffer.append("\t\t\t<styleUrl>#surfaceTreeBranch"+ nodeNumber +"_part"+(a+1)+"_style</styleUrl>\r");
369 // surfaceTreeBuffer.append("\t\t\t<styleUrl>#surfaceTreeBranch"+nodeNumber+"_style</styleUrl>\r");
370 surfaceTreeBuffer.append("\t\t\t<LineString>\r");
371
372 if (arcBranches) {
373 surfaceTreeBuffer.append("\t\t\t\t<altitudeMode>absolute</altitudeMode>\r");
374 surfaceTreeBuffer.append("\t\t\t\t<tessellate>1</tessellate>\r");
375 } else {
376 surfaceTreeBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r");
377 }
378
379 surfaceTreeBuffer.append("\t\t\t\t<coordinates>\r");
380 surfaceTreeBuffer.append("\t\t\t\t\t"+(longitude+a*(longitudeDifference/divider))+","+
381 (latitude+a*(latitudeDifference/divider))+","+
382 (maxAltitude*Math.sin(Math.acos(1 - a*(1.0/(divider/2.0)))))+"\r");
383 surfaceTreeBuffer.append("\t\t\t\t\t"+(longitude+(a+1)*(longitudeDifference/divider))+","+
384 (latitude+(a+1)*(latitudeDifference/divider))+","+
385 (maxAltitude*Math.sin(Math.acos(1 - (a+1)*(1.0/(divider/2.0)))))+"\r");
386 surfaceTreeBuffer.append("\t\t\t\t</coordinates>\r");
387
388 surfaceTreeBuffer.append("\t\t\t</LineString>\r");
389 surfaceTreeBuffer.append("\t\t</Placemark>\r");
390
391 styleBuffer.append("\t<Style id=\"surfaceTreeBranch"+ nodeNumber +"_part"+(a+1)+"_style\">\r");
392 styleBuffer.append("\t\t<LineStyle>\r");
393 if (usePosterior_ST) {
394 if (treeToExport.isExternal(node)) {
395 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
396 } else {
397 Double posterior = (Double)node.getAttribute("posterior");
398 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+posterior*branchWidthMultiplier)+"</width>\r");
399 }
400 } else {
401 styleBuffer.append("\t\t\t<width>"+branchWidth_ST+"</width>\r");
402 }
403 if (useHeights_ST){
404 styleBuffer.append("\t\t\t<color>"+"FF"+getKMLColor((treeToExport.getHeight(node) + (a + 1) *
405 ((treeToExport.getHeight(parentNode) - (treeToExport.getHeight(node)))/divider)),
406 heightMinAndMax, startBranchColor_ST, endBranchColor_ST)+"</color>\r");
407 } else {
408 styleBuffer.append("\t\t\t<color>"+"FF"+branchColor_ST+"</color>\r");
409 }
410 styleBuffer.append("\t\t</LineStyle>\r");
411 styleBuffer.append("\t</Style>\r");
412
413 }
414 surfaceTreeBuffer.append("\t</Folder>\r");
415
416 //this writes placemarks on the surface for the taxa, and writes out projections from the tips to the surface
417 if (treeToExport.isExternal(node)) {
418
419 taxaBuffer.append("\t\t\t<Placemark>\r");
420
421 taxaBuffer.append("\t\t\t<visibility>"+visibility_taxa+"</visibility>\r");
422
423 taxaBuffer.append("\t\t\t\t<name>"+treeToExport.getTaxon(node).getName()+"</name>\r");
424
425 taxaBuffer.append("\t\t\t\t<Point>\r");
426 taxaBuffer.append("\t\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
427 taxaBuffer.append("\t\t\t\t\t<coordinates>"+longitude+","+latitude+","+"0</coordinates>\r");
428 taxaBuffer.append("\t\t\t\t</Point>\r");
429
430 taxaBuffer.append("\t\t\t</Placemark>\r");
431
432 if (treeToExport.getHeight(node) > 0) {
433
434 projectionsBuffer.append("\t\t\t<LineString>\r");
435 projectionsBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
436
437 projectionsBuffer.append("\t\t\t\t<coordinates>\r");
438 projectionsBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitude+"\r");
439 projectionsBuffer.append("\t\t\t\t\t"+longitude+","+latitude+",0\r");
440 projectionsBuffer.append("\t\t\t\t</coordinates>\r");
441
442 projectionsBuffer.append("\t\t\t</LineString>\r");
443 }
444
445 styleBuffer.append("\t<Style id=\"rectangleTreeBranch"+ nodeNumber +"_style\">\r");
446 styleBuffer.append("\t\t<LineStyle>\r");
447 if (usePosterior_RT) {
448 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
449 } else {
450 styleBuffer.append("\t\t\t<width>"+branchWidth_RT+"</width>\r");
451 }
452 if (useRates_RT || useLogRates_RT) {
453 double rate = 0;
454 if (useRates_RT) {
455 rate = (Double)node.getAttribute("rate");
456 } else if (useLogRates_RT) {
457 rate = Math.log((Double)node.getAttribute("rate"));
458 }
459 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+
460 getKMLColor(rate, rateMinMaxMedian, startBranchColor_RT,
461 endBranchColor_RT)+"</color>\r");
462 } else if (useHeights_RT) {
463 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+
464 getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_RT,
465 endBranchColor_RT)+"</color>\r");
466 } else {
467 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+branchColor_RT+"</color>\r");
468 }
469 styleBuffer.append("\t\t</LineStyle>\r");
470 styleBuffer.append("\t</Style>\r");
471
472 styleBuffer.append("\t<Style id=\"triangleTreeBranch"+ nodeNumber +"_style\">\r");
473 styleBuffer.append("\t\t<LineStyle>\r");
474 if (usePosterior_TT) {
475 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
476 } else {
477 styleBuffer.append("\t\t\t<width>"+branchWidth_TT+"</width>\r");
478 }
479 if (useRates_TT || useLogRates_TT) {
480 double rate = 0;
481 if (useRates_TT) {
482 rate = (Double)node.getAttribute("rate");
483 } else if (useLogRates_TT) {
484 rate = Math.log((Double)node.getAttribute("rate"));
485 }
486 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+
487 getKMLColor(rate, rateMinMaxMedian, startBranchColor_TT,
488 endBranchColor_TT)+"</color>\r");
489 } else {
490 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+branchColor_TT+"</color>\r");
491 }
492 styleBuffer.append("\t\t</LineStyle>\r");
493 styleBuffer.append("\t</Style>\r");
494
495 // line style for surface external branches, based on posterior
496 styleBuffer.append("\t<Style id=\"surfaceTreeBranch"+ nodeNumber +"_style\">\r");
497 styleBuffer.append("\t\t<LineStyle>\r");
498 if (usePosterior_ST) {
499 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
500 } else {
501 styleBuffer.append("\t\t\t<width>"+branchWidth_ST+"</width>\r");
502 }
503 if (useHeights_ST){
504 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_ST)+getKMLColor(treeToExport.getHeight(node),
505 heightMinAndMax, startBranchColor_ST, endBranchColor_ST)+"</color>\r");
506 } else {
507 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_ST)+branchColor_ST+"</color>\r");
508 }
509 styleBuffer.append("\t\t</LineStyle>\r");
510 styleBuffer.append("\t</Style>\r");
511
512 if (tipsSampled) {
513 if (contoursAndNotSpades) {
514 appendContour(contourTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
515 appendContour(groundContourTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
516 } else {
517 appendSpade(spadeTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
518 appendSpade(groundSpadeTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
519 appendDiamond(diamondTipsBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
520 }
521 // spade/contour style for tips
522 //contour
523 if (contoursAndNotSpades) {
524 styleBuffer.append("\t<Style id=\"contour"+ nodeNumber +"_style\">\r");
525 styleBuffer.append("\t\t<LineStyle>\r");
526 styleBuffer.append("\t\t\t<width>1.5</width>\r");
527 if (useHeights_tipContours) {
528 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipContours)+getKMLColor(treeToExport.getHeight(node),
529 heightMinAndMax, startTipContourColor, endTipContourColor)+"</color>\r");
530 } else {
531 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipContours)+tipContourColor+"</color>\r");
532 }
533 styleBuffer.append("\t\t</LineStyle>\r");
534 styleBuffer.append("\t\t<PolyStyle>\r");
535 if (useHeights_tipContours) {
536 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipContours)+getKMLColor(treeToExport.getHeight(node),
537 heightMinAndMax, startTipContourColor, endTipContourColor)+"</color>\r");
538 } else {
539 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipContours)+tipContourColor+"</color>\r");
540 }
541 styleBuffer.append("\t\t</PolyStyle>\r");
542 styleBuffer.append("\t</Style>\r");
543 //spade
544 } else {
545 styleBuffer.append("\t<Style id=\"spade"+ nodeNumber +"_style\">\r");
546 styleBuffer.append("\t\t<LineStyle>\r");
547 styleBuffer.append("\t\t\t<width>1.5</width>\r");
548 if (useHeights_tipSpades) {
549 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipSpades)+getKMLColor(treeToExport.getHeight(node),
550 heightMinAndMax, startTipSpadeColor, endTipSpadeColor)+"</color>\r");
551 } else {
552 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipSpades)+tipSpadeColor+"</color>\r");
553 }
554 styleBuffer.append("\t\t</LineStyle>\r");
555 styleBuffer.append("\t\t<PolyStyle>\r");
556 if (useHeights_tipSpades) {
557 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipSpades)+getKMLColor(treeToExport.getHeight(node),
558 heightMinAndMax, startTipSpadeColor, endTipSpadeColor)+"</color>\r");
559 } else {
560 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_tipSpades)+tipSpadeColor+"</color>\r");
561 }
562 styleBuffer.append("\t\t</PolyStyle>\r");
563 styleBuffer.append("\t</Style>\r");
564 }
565
566 // ground spade/contour style for internal nodes
567 //contour
568 if (contoursAndNotSpades) {
569 styleBuffer.append("\t<Style id=\"groundContour"+ nodeNumber +"_style\">\r");
570 styleBuffer.append("\t\t<LineStyle>\r");
571 styleBuffer.append("\t\t\t<width>0.5</width>\r");
572 styleBuffer.append("\t\t</LineStyle>\r");
573 styleBuffer.append("\t\t<PolyStyle>\r");
574 if (useHeights_groundTipSpades) {
575 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundTipContours)+getKMLColor(treeToExport.getHeight(node),
576 heightMinAndMax, startgroundTipContourColor, endgroundTipContourColor)+"</color>\r");
577 } else {
578 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundTipContours)+groundTipContourColor+"</color>\r");
579 }
580 styleBuffer.append("\t\t\t<outline>0</outline>\r");
581 styleBuffer.append("\t\t</PolyStyle>\r");
582 styleBuffer.append("\t</Style>\r");
583 //spade
584 } else {
585 styleBuffer.append("\t<Style id=\"groundSpade"+ nodeNumber +"_style\">\r");
586 styleBuffer.append("\t\t<LineStyle>\r");
587 styleBuffer.append("\t\t\t<width>0.5</width>\r");
588 styleBuffer.append("\t\t</LineStyle>\r");
589 styleBuffer.append("\t\t<PolyStyle>\r");
590 if (useHeights_groundTipSpades) {
591 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundTipSpades)+getKMLColor(treeToExport.getHeight(node),
592 heightMinAndMax, startgroundTipSpadeColor, endgroundTipSpadeColor)+"</color>\r");
593 } else {
594 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundTipSpades)+groundTipSpadeColor+"</color>\r");
595 }
596 styleBuffer.append("\t\t\t<outline>0</outline>\r");
597 styleBuffer.append("\t\t</PolyStyle>\r");
598 styleBuffer.append("\t</Style>\r");
599 }
600
601 totalHPDarea += getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
602 }
603
604 } else {
605
606 if (contoursAndNotSpades) {
607 appendContour(contourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
608 appendContour(groundContourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
609 } else {
610 appendSpade(spadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
611 appendSpade(groundSpadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
612 appendDiamond(diamondBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
613 }
614
615 Double posterior = (Double)node.getAttribute("posterior");
616
617 // line style for internal rectangleTree branches, based on posterior
618 styleBuffer.append("\t<Style id=\"rectangleTreeBranch"+ nodeNumber +"_style\">\r");
619 styleBuffer.append("\t\t<LineStyle>\r");
620 if (usePosterior_RT) {
621 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+posterior*branchWidthMultiplier)+"</width>\r");
622 } else {
623 styleBuffer.append("\t\t\t<width>"+branchWidth_RT+"</width>\r");
624 }
625 if (useRates_RT || useLogRates_RT) {
626 double rate = 0;
627 if (useRates_RT) {
628 rate = (Double)node.getAttribute("rate");
629 } else if (useLogRates_RT) {
630 rate = Math.log((Double)node.getAttribute("rate"));
631 }
632 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+getKMLColor(rate,rateMinMaxMedian,
633 startBranchColor_RT, endBranchColor_RT)+"</color>\r");
634 } else if (useHeights_RT) {
635 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+
636 getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_RT,
637 endBranchColor_RT)+"</color>\r");
638 } else {
639 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+branchColor_RT+"</color>\r");
640 }
641 styleBuffer.append("\t\t</LineStyle>\r");
642 styleBuffer.append("\t</Style>\r");
643
644 // line style for internal nodes, based on posterior
645 styleBuffer.append("\t<Style id=\"triangleTreeBranch"+ nodeNumber +"_style\">\r");
646 styleBuffer.append("\t\t<LineStyle>\r");
647 if (usePosterior_TT) {
648 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+posterior*branchWidthMultiplier)+"</width>\r");
649 } else {
650 styleBuffer.append("\t\t\t<width>"+branchWidth_TT+"</width>\r");
651 }
652 if (useRates_TT || useLogRates_TT) {
653 double rate = 0;
654 if (useRates_TT) {
655 rate = (Double)node.getAttribute("rate");
656 } else if (useLogRates_TT) {
657 rate = Math.log((Double)node.getAttribute("rate"));
658 }
659 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+getKMLColor(rate,rateMinMaxMedian,
660 startBranchColor_TT, endBranchColor_TT)+"</color>\r");
661 } else {
662 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+branchColor_TT+"</color>\r");
663 }
664 styleBuffer.append("\t\t</LineStyle>\r");
665 styleBuffer.append("\t</Style>\r");
666
667 // line style for surface internal branches, based on posterior
668 styleBuffer.append("\t<Style id=\"surfaceTreeBranch"+ nodeNumber +"_style\">\r");
669 styleBuffer.append("\t\t<LineStyle>\r");
670 if (usePosterior_ST) {
671 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+posterior*branchWidthMultiplier)+"</width>\r");
672 } else {
673 styleBuffer.append("\t\t\t<width>"+branchWidth_ST+"</width>\r");
674 }
675 if (useHeights_ST){
676 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_ST)+getKMLColor(treeToExport.getHeight(node),
677 heightMinAndMax, startBranchColor_ST, endBranchColor_ST)+"</color>\r");
678 } else {
679 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_ST)+branchColor_ST+"</color>\r");
680 }
681 styleBuffer.append("\t\t</LineStyle>\r");
682 styleBuffer.append("\t</Style>\r");
683
684 // spade/contour style for internal nodes
685 //contour
686 if (contoursAndNotSpades) {
687 styleBuffer.append("\t<Style id=\"contour"+ nodeNumber +"_style\">\r");
688 styleBuffer.append("\t\t<LineStyle>\r");
689 styleBuffer.append("\t\t\t<width>1.5</width>\r");
690 if (useHeights_contours) {
691 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+getKMLColor(treeToExport.getHeight(node),
692 heightMinAndMax, startContourColor, endContourColor)+"</color>\r");
693 } else {
694 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+contourColor+"</color>\r");
695 }
696 styleBuffer.append("\t\t</LineStyle>\r");
697 styleBuffer.append("\t\t<PolyStyle>\r");
698 if (useHeights_contours) {
699 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+getKMLColor(treeToExport.getHeight(node),
700 heightMinAndMax, startContourColor, endContourColor)+"</color>\r");
701 } else {
702 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+contourColor+"</color>\r");
703 }
704 styleBuffer.append("\t\t</PolyStyle>\r");
705 styleBuffer.append("\t</Style>\r");
706 //spade
707 } else {
708 styleBuffer.append("\t<Style id=\"spade"+ nodeNumber +"_style\">\r");
709 styleBuffer.append("\t\t<LineStyle>\r");
710 styleBuffer.append("\t\t\t<width>1.5</width>\r");
711 if (useHeights_spades) {
712 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+getKMLColor(treeToExport.getHeight(node),
713 heightMinAndMax, startSpadeColor, endSpadeColor)+"</color>\r");
714 } else {
715 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+spadeColor+"</color>\r");
716 }
717 styleBuffer.append("\t\t</LineStyle>\r");
718 styleBuffer.append("\t\t<PolyStyle>\r");
719 if (useHeights_spades) {
720 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+getKMLColor(treeToExport.getHeight(node),
721 heightMinAndMax, startSpadeColor, endSpadeColor)+"</color>\r");
722 } else {
723 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+spadeColor+"</color>\r");
724 }
725 styleBuffer.append("\t\t</PolyStyle>\r");
726 styleBuffer.append("\t</Style>\r");
727 }
728
729 // ground spade/contour style for internal nodes
730 //contour
731 if (contoursAndNotSpades) {
732 styleBuffer.append("\t<Style id=\"groundContour"+ nodeNumber +"_style\">\r");
733 styleBuffer.append("\t\t<LineStyle>\r");
734 styleBuffer.append("\t\t\t<width>0.5</width>\r");
735 styleBuffer.append("\t\t</LineStyle>\r");
736 styleBuffer.append("\t\t<PolyStyle>\r");
737 if (useHeights_groundSpades) {
738 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundContours)+getKMLColor(treeToExport.getHeight(node),
739 heightMinAndMax, startgroundContourColor, endgroundContourColor)+"</color>\r");
740 } else {
741 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundContours)+groundContourColor+"</color>\r");
742 }
743 styleBuffer.append("\t\t\t<outline>0</outline>\r");
744 styleBuffer.append("\t\t</PolyStyle>\r");
745 styleBuffer.append("\t</Style>\r");
746 //spade
747 } else {
748 styleBuffer.append("\t<Style id=\"groundSpade"+ nodeNumber +"_style\">\r");
749 styleBuffer.append("\t\t<LineStyle>\r");
750 styleBuffer.append("\t\t\t<width>0.5</width>\r");
751 styleBuffer.append("\t\t</LineStyle>\r");
752 styleBuffer.append("\t\t<PolyStyle>\r");
753 if (useHeights_groundSpades) {
754 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundSpades)+getKMLColor(treeToExport.getHeight(node),
755 heightMinAndMax, startgroundSpadeColor, endgroundSpadeColor)+"</color>\r");
756 } else {
757 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundSpades)+groundSpadeColor+"</color>\r");
758 }
759 styleBuffer.append("\t\t\t<outline>0</outline>\r");
760 styleBuffer.append("\t\t</PolyStyle>\r");
761 styleBuffer.append("\t</Style>\r");
762 }
763 totalHPDarea += getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
764 }
765
766 // write a ancestral branch to the root with length that is a precentage of the total height
767 } else {
768
769 rectangleTreeBuffer.append("\t\t<Placemark>\r");
770 triangleTreeBuffer.append("\t\t<Placemark>\r");
771
772 // by putting a Span on this root branch, this will be the oldest time. So, everything else will appear later in the time animation. (if not, the root spade would already be there)
773 double date = mostRecentDate - (treeToExport.getHeight(node) + (fraction*treeToExport.getHeight(node)));
774 String[] yearMonthDay = convertToYearMonthDay(date);
775 rectangleTreeBuffer.append("\t\t\t<TimeSpan>\r");
776 triangleTreeBuffer.append("\t\t\t<TimeSpan>\r");
777 if (ancient) {
778 rectangleTreeBuffer.append("\t\t\t\t<begin>"+Math.round(date)+"</begin>\r");
779 triangleTreeBuffer.append("\t\t\t\t<begin>"+Math.round(date)+"</begin>\r");
780 } else {
781 rectangleTreeBuffer.append("\t\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r");
782 triangleTreeBuffer.append("\t\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r");
783 }
784 rectangleTreeBuffer.append("\t\t\t</TimeSpan>\r");
785 triangleTreeBuffer.append("\t\t\t</TimeSpan>\r");
786
787 rectangleTreeBuffer.append("\t\t\t<visibility>"+visibility_RT+"</visibility>\r");
788 triangleTreeBuffer.append("\t\t\t<visibility>"+visibility_TT+"</visibility>\r");
789 rectangleTreeBuffer.append("\t\t\t<name>rectangleTreeBranch"+ nodeNumber +"</name>\r");
790 triangleTreeBuffer.append("\t\t\t<name>triangleTreeBranch"+ nodeNumber +"</name>\r");
791 rectangleTreeBuffer.append("\t\t\t<styleUrl>#rectangleTreeBranch"+ nodeNumber +"_style</styleUrl>\r");
792 triangleTreeBuffer.append("\t\t\t<styleUrl>#triangleTreeBranch"+ nodeNumber +"_style</styleUrl>\r");
793 rectangleTreeBuffer.append("\t\t\t<LineString>\r");
794 triangleTreeBuffer.append("\t\t\t<LineString>\r");
795 rectangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
796 triangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
797 rectangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
798 triangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
799
800 rectangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitude+"\r");
801 triangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitude+"\r");
802 rectangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+(altitude+(fraction*altitude))+"\r");
803 triangleTreeBuffer.append("\t\t\t\t\t"+longitude+","+latitude+","+(altitude+(fraction*altitude))+"\r");
804
805 rectangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
806 triangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
807 rectangleTreeBuffer.append("\t\t\t</LineString>\r");
808 triangleTreeBuffer.append("\t\t\t</LineString>\r");
809 rectangleTreeBuffer.append("\t\t</Placemark>\r");
810 triangleTreeBuffer.append("\t\t</Placemark>\r");
811
812 if (contoursAndNotSpades) {
813 appendContour(contourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
814 appendContour(groundContourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
815 } else {
816 appendSpade(spadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
817 appendSpade(groundSpadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
818 appendDiamond(diamondBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
819 }
820
821 // style for the rectangle root branch
822 styleBuffer.append("\t<Style id=\"rectangleTreeBranch"+ nodeNumber +"_style\">\r");
823 styleBuffer.append("\t\t<LineStyle>\r");
824 if (usePosterior_RT) {
825 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
826 } else {
827 styleBuffer.append("\t\t\t<width>"+branchWidth_RT+"</width>\r");
828 }
829 if (useRates_RT){
830 // the root branch gets the color for the median rate
831 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+getKMLColor(rateMinMaxMedian[2],
832 rateMinMaxMedian, startBranchColor_RT, endBranchColor_RT)+"</color>\r");
833 } else {
834 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_RT)+branchColor_RT+"</color>\r");
835 }
836 styleBuffer.append("\t\t</LineStyle>\r");
837 styleBuffer.append("\t</Style>\r");
838
839 // style for the triangle root branch
840 styleBuffer.append("\t<Style id=\"triangleTreeBranch"+ nodeNumber +"_style\">\r");
841 styleBuffer.append("\t\t<LineStyle>\r");
842 if (usePosterior_RT) {
843 styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+branchWidthMultiplier)+"</width>\r");
844 } else {
845 styleBuffer.append("\t\t\t<width>"+branchWidth_TT+"</width>\r");
846 }
847 if (useRates_TT){
848 // the root branch gets the color for the median rate
849 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+getKMLColor(rateMinMaxMedian[2],
850 rateMinMaxMedian, startBranchColor_TT, endBranchColor_TT)+"</color>\r");
851 } else {
852 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_TT)+branchColor_TT+"</color>\r");
853 }
854 styleBuffer.append("\t\t</LineStyle>\r");
855 styleBuffer.append("\t</Style>\r");
856
857 // style for the root spade/contour based on height
858 if (contoursAndNotSpades) {
859 styleBuffer.append("\t<Style id=\"contour"+ nodeNumber +"_style\">\r");
860 styleBuffer.append("\t\t<LineStyle>\r");
861 styleBuffer.append("\t\t\t<width>1.5</width>\r");
862 if (useHeights_contours) {
863 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+getKMLColor(treeToExport.getHeight(node),
864 heightMinAndMax, startContourColor, endContourColor)+"</color>\r");
865 } else {
866 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+contourColor+"</color>\r");
867 }
868 styleBuffer.append("\t\t</LineStyle>\r");
869 styleBuffer.append("\t\t<PolyStyle>\r");
870 if (useHeights_contours) {
871 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+getKMLColor(treeToExport.getHeight(node),
872 heightMinAndMax, startContourColor, endContourColor)+"</color>\r");
873 } else {
874 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_contours)+contourColor+"</color>\r");
875 }
876 styleBuffer.append("\t\t</PolyStyle>\r");
877 styleBuffer.append("\t</Style>\r");
878 } else {
879 styleBuffer.append("\t<Style id=\"spade"+ nodeNumber +"_style\">\r");
880 styleBuffer.append("\t\t<LineStyle>\r");
881 styleBuffer.append("\t\t\t<width>1.5</width>\r");
882 if (useHeights_spades) {
883 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+getKMLColor(treeToExport.getHeight(node),
884 heightMinAndMax, startSpadeColor, endSpadeColor)+"</color>\r");
885 } else {
886 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+spadeColor+"</color>\r");
887 }
888 styleBuffer.append("\t\t</LineStyle>\r");
889 styleBuffer.append("\t\t<PolyStyle>\r");
890 if (useHeights_spades) {
891 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+getKMLColor(treeToExport.getHeight(node),
892 heightMinAndMax, startSpadeColor, endSpadeColor)+"</color>\r");
893 } else {
894 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_spades)+spadeColor+"</color>\r");
895 }
896 styleBuffer.append("\t\t</PolyStyle>\r");
897 styleBuffer.append("\t</Style>\r");
898 }
899
900 // ground spade/contour style for root
901 if (contoursAndNotSpades) {
902 styleBuffer.append("\t<Style id=\"groundContour"+ nodeNumber +"_style\">\r");
903 styleBuffer.append("\t\t<LineStyle>\r");
904 styleBuffer.append("\t\t\t<width>0.5</width>\r");
905 styleBuffer.append("\t\t</LineStyle>\r");
906 styleBuffer.append("\t\t<PolyStyle>\r");
907 if (useHeights_groundContours) {
908 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundContours)+getKMLColor(treeToExport.getHeight(node),
909 heightMinAndMax, startgroundContourColor, endgroundContourColor)+"</color>\r");
910 } else {
911 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundContours)+groundContourColor+"</color>\r");
912 }
913 styleBuffer.append("\t\t\t<outline>0</outline>");
914 styleBuffer.append("\t\t</PolyStyle>\r");
915 styleBuffer.append("\t</Style>\r");
916 } else {
917 styleBuffer.append("\t<Style id=\"groundSpade"+ nodeNumber +"_style\">\r");
918 styleBuffer.append("\t\t<LineStyle>\r");
919 styleBuffer.append("\t\t\t<width>0.5</width>\r");
920 styleBuffer.append("\t\t</LineStyle>\r");
921 styleBuffer.append("\t\t<PolyStyle>\r");
922 if (useHeights_groundSpades) {
923 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundSpades)+getKMLColor(treeToExport.getHeight(node),
924 heightMinAndMax, startgroundSpadeColor, endgroundSpadeColor)+"</color>\r");
925 } else {
926 styleBuffer.append("\t\t\t<color>"+getOpacity(opacity_groundSpades)+groundSpadeColor+"</color>\r");
927 }
928 styleBuffer.append("\t\t\t<outline>0</outline>");
929 styleBuffer.append("\t\t</PolyStyle>\r");
930 styleBuffer.append("\t</Style>\r");
931 }
932 double rootHPDArea = getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
933 totalHPDarea += rootHPDArea;
934 System.out.println("root HPD area is "+rootHPDArea+" degrees");
935
936
937 }
938
939 }
940 System.out.println("total HPD area is "+totalHPDarea+" degrees");
941 }
942
943 public void writeTreeToKML(double time, double treeSliceBranchWidth, boolean showBranchAtMidPoint) {
944
945 heightMinAndMax = getHeightMinAndMax(treeToExport);
946
947 treeSliceBuffer.append("\t<Folder>\r");
948 treeSliceBuffer.append("\t\t\t<name>tree"+ time +"</name>\r");
949 contourSliceBuffer.append("\t<Folder>\r");
950 contourSliceBuffer.append("\t\t\t<name>contours"+ time +"</name>\r");
951
952
953 int nodeNumber = 0;
954 for (Node node : treeToExport.getNodes()) {
955
956 Double longitude = (Double)node.getAttribute(longitudeName);
957 Double latitude = (Double)node.getAttribute(latitudeName);
958 double nodeHeight = treeToExport.getHeight(node);
959
960 if (!treeToExport.isRoot(node)) {
961
962 Node parentNode = treeToExport.getParent(node);
963 Double parentLongitude = (Double)parentNode.getAttribute(longitudeName);
964 Double parentLatitude = (Double)parentNode.getAttribute(latitudeName);
965 double parentHeight = treeToExport.getHeight(parentNode);
966
967 if ((parentHeight > time) && (nodeHeight <= time)) {
968 //extrapolate lat/long
969 if (!showBranchAtMidPoint) {
970
971 latitude = parentLatitude + (latitude-parentLatitude)*((parentHeight-time)/(parentHeight-nodeHeight));
972 longitude = parentLongitude + (longitude-parentLongitude)*((parentHeight-time)/(parentHeight-nodeHeight));
973 }
974 }
975 if (((parentHeight > time) && !(showBranchAtMidPoint)) || (showBranchAtMidPoint && (time < ((nodeHeight+((parentHeight-nodeHeight)/2.0)))))) {
976
977 treeSliceBuffer.append("\t\t<Placemark>\r");
978
979 treeSliceBuffer.append("\t\t\t<name>treeSliceBranch"+ nodeNumber + "_" + time + "</name>\r");
980 treeSliceBuffer.append("\t\t\t<styleUrl>#treeSliceBranch"+ nodeNumber + "_" + time + "_style</styleUrl>\r");
981 treeSliceBuffer.append("\t\t\t<LineString>\r");
982 treeSliceBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r");
983 treeSliceBuffer.append("\t\t\t\t<coordinates>\r");
984 treeSliceBuffer.append("\t\t\t\t\t"+longitude+","+latitude+",0\r");
985 treeSliceBuffer.append("\t\t\t\t\t"+parentLongitude+","+parentLatitude+",0\r");
986 treeSliceBuffer.append("\t\t\t\t</coordinates>\r");
987
988 treeSliceBuffer.append("\t\t\t</LineString>\r");
989 treeSliceBuffer.append("\t\t</Placemark>\r");
990
991
992 styleBuffer.append("\t<Style id=\"treeSliceBranch"+ nodeNumber + "_" + time + "_style\">\r");
993 styleBuffer.append("\t\t<LineStyle>\r");
994 styleBuffer.append("\t\t\t<width>"+treeSliceBranchWidth+"</width>\r");
995 styleBuffer.append("\t\t\t<color>"+"FF"+ ContinuousKML.getKMLColor((nodeHeight+((parentHeight-nodeHeight)/2.0)),
996 heightMinAndMax, startBranchColor_TS, endBranchColor_TS)+"</color>\r");
997 styleBuffer.append("\t\t</LineStyle>\r");
998 styleBuffer.append("\t</Style>\r");
999
1000 appendContour(contourSliceBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
1001
1002 }
1003
1004 }
1005 nodeNumber ++;
1006 }
1007 treeSliceBuffer.append("\t</Folder>\r");
1008 contourSliceBuffer.append("\t</Folder>\r");
1009
1010 }
1011
1012 public void compileBuffer(StringBuffer buffer, boolean makeTreeSlices) {
1013
1014 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r");
1015 buffer.append("<kml xmlns=\"http://earth.google.com/kml/2.2\">\r");
1016
1017 buffer.append("<Document>\r");
1018
1019 buffer.append("\t<name>"+documentName+"</name>\r");
1020 buffer.append(styleBuffer);
1021
1022 if (!makeTreeSlices) {
1023
1024 buffer.append("\t<Style id=\"diamondStyle\">\r");
1025 buffer.append("\t\t<LineStyle>\r");
1026 buffer.append("\t\t\t<width>0.5</width>\r");
1027 buffer.append("\t\t</LineStyle>\r");
1028 buffer.append("\t\t<PolyStyle>\r");
1029 buffer.append("\t\t\t<color>7d00ffff</color>\r");
1030 buffer.append("\t\t\t<fill>0</fill>\r");
1031 buffer.append("\t\t</PolyStyle>\r");
1032 buffer.append("\t</Style>\r");
1033
1034 buffer.append("\t<Folder>\r");
1035 buffer.append("\t<name>triangle tree</name>\r");
1036 buffer.append("\t<description>tree out of surface with node heights proportional to time</description>\r");
1037 buffer.append(triangleTreeBuffer);
1038 buffer.append("\t</Folder>\r");
1039
1040 buffer.append("\t<Folder>\r");
1041 buffer.append("\t<name>rectangle tree</name>\r");
1042 buffer.append("\t<description>tree out of surface with branch lengths (and node heights) proportional to time</description>\r");
1043
1044 buffer.append(rectangleTreeBuffer);
1045 buffer.append("\t</Folder>\r");
1046
1047 buffer.append("\t<Folder>\r");
1048 buffer.append("\t<name>surface tree</name>\r");
1049 buffer.append("\t<description>tree on the surface interconnecting inferred and sampled locations</description>\r");
1050 buffer.append(surfaceTreeBuffer);
1051 buffer.append("\t</Folder>\r");
1052
1053 int visibility_projections;
1054 if (iniVisi_projections) { visibility_projections = 1; } else { visibility_projections = 0; }
1055
1056 buffer.append("\t<Placemark>\r");
1057 buffer.append("\t\t<name>projections</name>\r");
1058 buffer.append("\t\t<description>projections from tips to surface</description>\r");
1059 buffer.append("\t\t<visibility>"+visibility_projections+"</visibility>\r");
1060 buffer.append("\t\t<MultiGeometry>\r");
1061 buffer.append(projectionsBuffer);
1062 buffer.append("\t\t</MultiGeometry>\r");
1063 buffer.append("\t</Placemark>\r");
1064
1065 buffer.append("\t<Folder>\r");
1066 buffer.append("\t\t<name>Taxon labels</name>\r");
1067 buffer.append("\t\t<description>Taxon Labels</description>\r");
1068 buffer.append(taxaBuffer);
1069 buffer.append("\t</Folder>\r");
1070
1071 //writes a folder with credible intervals in the form of polygons
1072 buffer.append("\t<Folder>\r");
1073 buffer.append("\t<name>polygon HPDs</name>\r");
1074 buffer.append("\t<description>various polygons to represent credible intervals</description>\r");
1075
1076 if (contoursAndNotSpades) {
1077
1078 buffer.append("\t\t<Folder>\r");
1079 buffer.append("\t\t\t<name>contours HPDs</name>\r");
1080 buffer.append("\t\t\t<description>contour HPDs for internal nodes</description>\r");
1081 buffer.append(contourBuffer);
1082 buffer.append("\t\t</Folder>\r");
1083
1084 buffer.append("\t\t<Folder>\r");
1085 buffer.append("\t\t\t<name>surface contour HPDs</name>\r");
1086 buffer.append("\t\t\t<description>contour HPDs for internal nodes projected on the surface</description>\r");
1087 buffer.append(groundContourBuffer);
1088 buffer.append("\t\t</Folder>\r");
1089
1090 if (tipsSampled){
1091 buffer.append("\t\t<Folder>\r");
1092 buffer.append("\t\t\t<name>contours tip HPDs</name>\r");
1093 buffer.append("\t\t\t<description>contour HPDs for tips</description>\r");
1094 buffer.append(contourTipsBuffer);
1095 buffer.append("\t\t</Folder>\r");
1096
1097 buffer.append("\t\t<Folder>\r");
1098 buffer.append("\t\t\t<name>surface contour tip HPD</name>\r");
1099 buffer.append("\t\t\t<description>contour HPDs for tips projected on the surface</description>\r");
1100 buffer.append(groundContourTipsBuffer);
1101 buffer.append("\t\t</Folder>\r");
1102 }
1103
1104 } else {
1105
1106 buffer.append("\t\t<Folder>\r");
1107 buffer.append("\t\t\t<name>spade HPDs</name>\r");
1108 buffer.append("\t\t\t<description>longitude and latitude HPDs for internal nodes</description>\r");
1109 buffer.append(spadeBuffer);
1110 buffer.append("\t\t</Folder>\r");
1111
1112 buffer.append("\t\t<Folder>\r");
1113 buffer.append("\t\t\t<name>surface spade HPDs</name>\r");
1114 buffer.append("\t\t\t<description>longitude and latitude HPDs for internal nodes projected on the surface</description>\r");
1115 buffer.append(groundSpadeBuffer);
1116 buffer.append("\t\t</Folder>\r");
1117
1118
1119 buffer.append("\t\t<Folder>\r");
1120 buffer.append("\t\t\t<name>diamond HPDs</name>\r");
1121 buffer.append("\t\t\t<description>longitude and latitude and altitude HPDs for internal nodes</description>\r");
1122 buffer.append(diamondBuffer);
1123 buffer.append("\t\t</Folder>\r");
1124
1125 if (tipsSampled){
1126 buffer.append("\t\t<Folder>\r");
1127 buffer.append("\t\t\t<name>spade tip HPDs</name>\r");
1128 buffer.append("\t\t\t<description>longitude and latitude HPDs for tips</description>\r");
1129 buffer.append(spadeTipsBuffer);
1130 buffer.append("\t\t</Folder>\r");
1131
1132 buffer.append("\t\t<Folder>\r");
1133 buffer.append("\t\t\t<name>surface spade tip HPDs</name>\r");
1134 buffer.append("\t\t\t<description>longitude and latitude HPDs for tips projected on the surface</description>\r");
1135 buffer.append(groundSpadeTipsBuffer);
1136 buffer.append("\t\t</Folder>\r");
1137
1138
1139 buffer.append("\t\t<Folder>\r");
1140 buffer.append("\t\t\t<name>diamond tip HPDs</name>\r");
1141 buffer.append("\t\t\t<description>longitude and latitude and altitude HPDs for tips</description>\r");
1142 buffer.append(diamondTipsBuffer);
1143 buffer.append("\t\t</Folder>\r");
1144
1145 }
1146 }
1147
1148 buffer.append("\t\t</Folder>\r");
1149
1150 } else {
1151
1152 buffer.append(treeSliceBuffer);
1153 buffer.append("\t<Folder>\r");
1154 buffer.append("\t\t<name>Taxon labels</name>\r");
1155 buffer.append("\t\t<description>Taxon Labels</description>\r");
1156 buffer.append(taxaBuffer);
1157 buffer.append("\t</Folder>\r");
1158 buffer.append(contourSliceBuffer);
1159
1160
1161 }
1162
1163 buffer.append("</Document>\r");
1164
1165 buffer.append("</kml>");
1166
1167
1168 }
1169
1170 private static void appendSpade(StringBuffer buffer, RootedTree tree, Node node, int nodeNumber, double plotHeight, double mostRecentDate, String latitudeName, String longitudeName, boolean groundSpade, boolean initialVisibility, String spadeHPD, boolean ancient) {
1171
1172 int visibility;
1173 if (initialVisibility) {visibility = 1; } else { visibility = 0; }
1174
1175 double scaleFactor = plotHeight/tree.getHeight(tree.getRootNode());
1176 Double longitude = (Double)node.getAttribute(longitudeName);
1177 Double latitude = (Double)node.getAttribute(latitudeName);
1178 double altitude = (tree.getHeight(node)*scaleFactor);
1179 String altitudeMode;
1180 if (groundSpade) {
1181 altitude = 0;
1182 altitudeMode = "clampToGround";
1183 } else {
1184 altitudeMode = "relativeToGround";
1185 }
1186
1187 Object[] longitudeHPDs = (Object[])node.getAttribute(longitudeName+"_"+spadeHPD+"HPD");
1188 Object[] latitudeHPDs = (Object[])node.getAttribute(latitudeName+"_"+spadeHPD+"HPD");
1189
1190 buffer.append("\t\t<Placemark>\r");
1191
1192 buffer.append("\t\t\t<visibility>"+visibility+"</visibility>\r");
1193
1194 buffer.append("\t\t<TimeSpan>\r");
1195 double date = mostRecentDate - tree.getHeight(node);
1196 String[] yearMonthDay = convertToYearMonthDay(date);
1197 if (ancient) {
1198 buffer.append("\t\t\t<begin>"+Math.round(date)+"</begin>\r");
1199 } else {
1200 buffer.append("\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r");
1201 }
1202 buffer.append("\t\t</TimeSpan>\r");
1203
1204 if (groundSpade) {
1205 buffer.append("\t\t<styleUrl>#groundSpade"+nodeNumber+"_style</styleUrl>\r");
1206 } else {
1207 buffer.append("\t\t<styleUrl>#spade"+nodeNumber+"_style</styleUrl>\r");
1208 }
1209 buffer.append("\t\t<Polygon>\r");
1210 buffer.append("\t\t\t<altitudeMode>"+altitudeMode+"</altitudeMode>\r");
1211 if (groundSpade) {
1212 buffer.append("\t\t\t<tessellate>1</tessellate>\r");
1213 }
1214 buffer.append("\t\t\t<outerBoundaryIs>\r");
1215 buffer.append("\t\t\t\t<LinearRing>\r");
1216 buffer.append("\t\t\t\t\t<coordinates>\r");
1217
1218 buffer.append("\t\t\t\t\t"+longitude+","+latitudeHPDs[1]+","+altitude+"\r");
1219 buffer.append("\t\t\t\t\t"+longitudeHPDs[1]+","+latitude+","+altitude+"\r");
1220 buffer.append("\t\t\t\t\t"+longitude+","+latitudeHPDs[0]+","+altitude+"\r");
1221 buffer.append("\t\t\t\t\t"+longitudeHPDs[0]+","+latitude+","+altitude+"\r");
1222 buffer.append("\t\t\t\t\t"+longitude+","+latitudeHPDs[1]+","+altitude+"\r");
1223
1224 buffer.append("\t\t\t\t\t</coordinates>\r");
1225 buffer.append("\t\t\t\t</LinearRing>\r");
1226 buffer.append("\t\t\t</outerBoundaryIs>\r");
1227 buffer.append("\t\t</Polygon>\r");
1228 buffer.append("\t\t</Placemark>\r");
1229
1230 }
1231
1232 private static void appendContour(StringBuffer buffer, RootedTree tree, Node node, int nodeNumber, double plotHeight, double mostRecentDate, String latLongName, String latitudeName, String longitudeName, boolean groundContour, boolean initialVisibility, String contourHPD, boolean ancient) {
1233
1234 int visibility;
1235 if (initialVisibility) {visibility = 1; } else { visibility = 0; }
1236
1237 double scaleFactor = plotHeight/tree.getHeight(tree.getRootNode());
1238 double altitude = (tree.getHeight(node)*scaleFactor);
1239 String altitudeMode;
1240 if (groundContour) {
1241 altitude = 0;
1242 altitudeMode = "clampToGround";
1243 } else {
1244 altitudeMode = "relativeToGround";
1245 }
1246
1247 Object testAttribute = node.getAttribute(latLongName+"_"+contourHPD+"HPD_modality");
1248 if (testAttribute != null) {
1249
1250 int modality = ((Integer)node.getAttribute(latLongName+"_"+contourHPD+"HPD_modality")).intValue();
1251
1252 for (int x = 0; x < modality; x++) {
1253 Object[] longitudeHPDs = (Object[])node.getAttribute(longitudeName+"_"+contourHPD+"HPD_"+(x + 1));
1254 Object[] latitudeHPDs = (Object[])node.getAttribute(latitudeName+"_"+contourHPD+"HPD_"+(x + 1));
1255
1256 buffer.append("\t\t<Placemark>\r");
1257
1258 buffer.append("\t\t\t<visibility>"+visibility+"</visibility>\r");
1259
1260 buffer.append("\t\t<TimeSpan>\r");
1261 double date = mostRecentDate - tree.getHeight(node);
1262 String[] yearMonthDay = convertToYearMonthDay(date);
1263 if (ancient) {
1264 buffer.append("\t\t\t<begin>"+Math.round(date)+"</begin>\r");
1265 } else {
1266 buffer.append("\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r");
1267 }
1268 buffer.append("\t\t</TimeSpan>\r");
1269
1270 if (groundContour) {
1271 buffer.append("\t\t<styleUrl>#groundContour"+nodeNumber+"_style</styleUrl>\r");
1272 } else {
1273 buffer.append("\t\t<styleUrl>#contour"+nodeNumber+"_style</styleUrl>\r");
1274 }
1275 buffer.append("\t\t<Polygon>\r");
1276 buffer.append("\t\t\t<altitudeMode>"+altitudeMode+"</altitudeMode>\r");
1277 if (groundContour) {
1278 buffer.append("\t\t\t<tessellate>1</tessellate>\r");
1279 }
1280 buffer.append("\t\t\t<outerBoundaryIs>\r");
1281 buffer.append("\t\t\t\t<LinearRing>\r");
1282 buffer.append("\t\t\t\t\t<coordinates>\r");
1283
1284 for (int y = 0; y < longitudeHPDs.length; y++) {
1285
1286 buffer.append("\t\t\t\t\t"+longitudeHPDs[y]+","+latitudeHPDs[y]+","+altitude+"\r");
1287
1288 }
1289
1290 buffer.append("\t\t\t\t\t</coordinates>\r");
1291 buffer.append("\t\t\t\t</LinearRing>\r");
1292 buffer.append("\t\t\t</outerBoundaryIs>\r");
1293 buffer.append("\t\t</Polygon>\r");
1294 buffer.append("\t\t</Placemark>\r");
1295 }
1296 } else {
1297 System.err.print("found a node without location HPD contour!!\n");
1298 }
1299 }
1300
1301 private static void appendDiamond(StringBuffer buffer, RootedTree tree, Node node, int nodeNumber, double plotHeight, String latitudeName, String longitudeName, String diamondHPD, boolean initialVisibility) {
1302
1303 int visibility;
1304 if (initialVisibility) { visibility = 1; } else { visibility = 0; }
1305
1306 double scaleFactor = plotHeight/tree.getHeight(tree.getRootNode());
1307 Double longitude = (Double)node.getAttribute(longitudeName);
1308 Double latitude = (Double)node.getAttribute(latitudeName);
1309 double altitude = (tree.getHeight(node)*scaleFactor);
1310
1311 Object[] longitudeHPDs = (Object[])node.getAttribute(longitudeName+"_"+diamondHPD+"HPD");
1312 Object[] latitudeHPDs = (Object[])node.getAttribute(latitudeName+"_"+diamondHPD+"HPD");
1313 Object[] heightHPDs = (Object[])node.getAttribute("height_95%_HPD");
1314
1315 double longitudeHPDlower = (Double)longitudeHPDs[0];
1316 double longitudeHPDupper = (Double)longitudeHPDs[1];
1317
1318 double latitudeHPDlower = (Double)latitudeHPDs[0];
1319 double latitudeHPDupper = (Double)latitudeHPDs[1];
1320
1321 double heightHPDlower = (Double)heightHPDs[0];
1322 double altitudeHPDlower = heightHPDlower*scaleFactor;
1323 double heightHPDupper = (Double)heightHPDs[1];
1324 double altitudeHPDupper = heightHPDupper* scaleFactor;
1325
1326 buffer.append("\t\t<Folder>\r");
1327 buffer.append("\t\t\t<name>node"+nodeNumber+"_diamond</name>\r");
1328
1329 buffer.append("\t\t\t<Placemark>\r");
1330 buffer.append("\t\t\t<visibility>"+visibility+"</visibility>\r");
1331 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1332 buffer.append("\t\t\t<Polygon>\r");
1333 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1334 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1335
1336 buffer.append("\t\t\t\t\t<LinearRing>\r");
1337 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1338
1339 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDlower+"\r");
1340 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1341 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1342 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDlower+"\r");
1343
1344 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1345 buffer.append("\t\t\t\t\t</LinearRing>\r");
1346 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1347 buffer.append("\t\t\t</Polygon>\r");
1348 buffer.append("\t\t\t</Placemark>\r");
1349
1350 buffer.append("\t\t\t<Placemark>\r");
1351 buffer.append("\t\t\t<visibility>0</visibility>\r");
1352 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1353 buffer.append("\t\t\t<Polygon>\r");
1354 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1355 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1356
1357 buffer.append("\t\t\t\t\t<LinearRing>\r");
1358 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1359
1360 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1361 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1362 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDupper+"\r");
1363 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1364
1365 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1366 buffer.append("\t\t\t\t\t</LinearRing>\r");
1367 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1368 buffer.append("\t\t\t</Polygon>\r");
1369 buffer.append("\t\t\t</Placemark>\r");
1370
1371 buffer.append("\t\t\t<Placemark>\r");
1372 buffer.append("\t\t\t<visibility>0</visibility>\r");
1373 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1374 buffer.append("\t\t\t<Polygon>\r");
1375 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1376 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1377
1378 buffer.append("\t\t\t\t\t<LinearRing>\r");
1379 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1380
1381 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1382 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDupper+"\r");
1383 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1384 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1385
1386 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1387 buffer.append("\t\t\t\t\t</LinearRing>\r");
1388 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1389 buffer.append("\t\t\t</Polygon>\r");
1390 buffer.append("\t\t\t</Placemark>\r");
1391
1392
1393 buffer.append("\t\t\t<Placemark>\r");
1394 buffer.append("\t\t\t<visibility>0</visibility>\r");
1395 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1396 buffer.append("\t\t\t<Polygon>\r");
1397 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1398 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1399
1400 buffer.append("\t\t\t\t\t<LinearRing>\r");
1401 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1402
1403 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1404 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1405 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDlower+"\r");
1406 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDupper+","+altitude+"\r");
1407
1408 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1409 buffer.append("\t\t\t\t\t</LinearRing>\r");
1410 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1411 buffer.append("\t\t\t</Polygon>\r");
1412 buffer.append("\t\t\t</Placemark>\r");
1413
1414 buffer.append("\t\t\t<Placemark>\r");
1415 buffer.append("\t\t\t<visibility>0</visibility>\r");
1416 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1417 buffer.append("\t\t\t<Polygon>\r");
1418 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1419 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1420
1421 buffer.append("\t\t\t\t\t<LinearRing>\r");
1422 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1423
1424 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1425 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDlower+","+altitude+"\r");
1426 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDupper+"\r");
1427 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1428
1429 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1430 buffer.append("\t\t\t\t\t</LinearRing>\r");
1431 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1432 buffer.append("\t\t\t</Polygon>\r");
1433 buffer.append("\t\t\t</Placemark>\r");
1434
1435 buffer.append("\t\t\t<Placemark>\r");
1436 buffer.append("\t\t\t<visibility>0</visibility>\r");
1437 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1438 buffer.append("\t\t\t<Polygon>\r");
1439 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1440 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1441
1442 buffer.append("\t\t\t\t\t<LinearRing>\r");
1443 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1444
1445 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1446 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDlower+","+altitude+"\r");
1447 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDlower+"\r");
1448 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDupper+","+altitude+"\r");
1449
1450 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1451 buffer.append("\t\t\t\t\t</LinearRing>\r");
1452 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1453 buffer.append("\t\t\t</Polygon>\r");
1454 buffer.append("\t\t\t</Placemark>\r");
1455
1456 buffer.append("\t\t\t<Placemark>\r");
1457 buffer.append("\t\t\t<visibility>0</visibility>\r");
1458 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1459 buffer.append("\t\t\t<Polygon>\r");
1460 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1461 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1462
1463 buffer.append("\t\t\t\t\t<LinearRing>\r");
1464 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1465
1466 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1467 buffer.append("\t\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDlower+","+altitude+"\r");
1468 buffer.append("\t\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDupper+"\r");
1469 buffer.append("\t\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1470
1471 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1472 buffer.append("\t\t\t\t\t</LinearRing>\r");
1473 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1474 buffer.append("\t\t\t</Polygon>\r");
1475 buffer.append("\t\t\t</Placemark>\r");
1476
1477 buffer.append("\t\t\t<Placemark>\r");
1478 buffer.append("\t\t\t<visibility>0</visibility>\r");
1479 buffer.append("\t\t\t<styleUrl>#diamondStyle</styleUrl>\r");
1480 buffer.append("\t\t\t<Polygon>\r");
1481 buffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
1482 buffer.append("\t\t\t\t<outerBoundaryIs>\r");
1483
1484 buffer.append("\t\t\t\t\t<LinearRing>\r");
1485 buffer.append("\t\t\t\t\t\t<coordinates>\r");
1486
1487 buffer.append("\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1488 buffer.append("\t\t\t\t\t"+longitudeHPDupper+","+latitudeHPDlower+","+altitude+"\r");
1489 buffer.append("\t\t\t\t\t"+longitude+","+latitude+","+altitudeHPDlower+"\r");
1490 buffer.append("\t\t\t\t\t"+longitudeHPDlower+","+latitudeHPDlower+","+altitude+"\r");
1491
1492 buffer.append("\t\t\t\t\t\t</coordinates>\r");
1493 buffer.append("\t\t\t\t\t</LinearRing>\r");
1494 buffer.append("\t\t\t\t</outerBoundaryIs>\r");
1495 buffer.append("\t\t\t</Polygon>\r");
1496 buffer.append("\t\t\t</Placemark>\r");
1497
1498 buffer.append("\t\t</Folder>\r");
1499
1500 }
1501
1502 // get the coordinates of the root (to locate a placemark and its icon)
1503 private String getRootPointLocation(RootedTree tree, double plotHeight, String latitudeName, String longitudeName) {
1504
1505 Node root = tree.getRootNode();
1506
1507 return root.getAttribute(longitudeName)+","+root.getAttribute(latitudeName)+","+plotHeight;
1508 }
1509
1510 private double[] getRateMinMaxMedian(RootedTree tree, boolean log) {
1511
1512 double[] minMaxMedian = new double[3];
1513
1514 double[] rates = new double[(tree.getNodes().size() - 1)];
1515
1516 int counter = 0;
1517
1518 int i = 0;
1519 for (Node node : tree.getNodes()) {
1520
1521 if (!tree.isRoot(node)) {
1522
1523 if (log) {
1524 rates[counter] = Math.log((Double)node.getAttribute("rate"));
1525 } else {
1526 rates[counter] = (Double)node.getAttribute("rate");
1527 }
1528 counter ++;
1529
1530 }
1531 i++;
1532 }
1533
1534 double median = DiscreteStatistics.quantile(0.5, rates);
1535
1536 double max = 0.0;
1537 double min = Double.MAX_VALUE;
1538
1539 for (int j = 0; j < rates.length; j++) {
1540
1541
1542 if (rates[j] > max) {
1543 max = rates[j];
1544 }
1545 if (rates[j] < min) {
1546 min = rates[j];
1547 }
1548 }
1549 minMaxMedian[0] = min;
1550 minMaxMedian[1] = max;
1551 minMaxMedian[2] = median;
1552
1553 return minMaxMedian;
1554 }
1555
1556 private double[] getHeightMinAndMax(RootedTree tree) {
1557
1558 double[] minAndMax = new double[2];
1559
1560 double[] heights = new double[tree.getNodes().size()];
1561
1562 int i = 0;
1563 for (Node node : tree.getNodes()) {
1564 heights[i] = tree.getHeight(node);
1565 i++;
1566 }
1567
1568 double max = 0.0;
1569 double min = Double.MAX_VALUE;
1570
1571 for (int j = 0; j < heights.length; j++) {
1572
1573
1574 if (heights[j] > max) {
1575 max = heights[j];
1576 }
1577 if (heights[j] < min) {
1578 min = heights[j];
1579 }
1580 }
1581 minAndMax[0] = min;
1582 minAndMax[1] = max;
1583
1584 return minAndMax;
1585 }
1586
1587
1588 private static String[] convertToYearMonthDay(double fractionalDate) {
1589
1590 String[] yearMonthDay = new String[3];
1591
1592 int year = (int) fractionalDate;
1593 String yearString;
1594
1595 if (year < 10) {
1596 yearString = "000"+year;
1597 } else if (year < 100) {
1598 yearString = "00"+year;
1599 } else if (year < 1000) {
1600 yearString = "0"+year;
1601 } else {
1602 yearString = ""+year;
1603 }
1604
1605 yearMonthDay[0] = yearString;
1606
1607 double fractionalMonth = fractionalDate - year;
1608
1609 int month = (int) (12.0 * fractionalMonth);
1610 String monthString;
1611
1612 if (month < 10) {
1613 monthString = "0"+month;
1614 } else {
1615 monthString = ""+month;
1616 }
1617
1618 yearMonthDay[1] = monthString;
1619
1620 int day = (int) Math.round(30*(12*fractionalMonth - month));
1621 String dayString;
1622
1623 if (day < 10) {
1624 dayString = "0"+day;
1625 } else {
1626 dayString = ""+day;
1627 }
1628
1629 yearMonthDay[2] = dayString;
1630
1631 return yearMonthDay;
1632
1633 }
1634
1635 public static String getKMLColor(double value, double[] minMaxMedian, String startColor, String endColor) {
1636
1637 startColor = startColor.toLowerCase();
1638 String startBlue = startColor.substring(0,2);
1639 String startGreen = startColor.substring(2,4);
1640 String startRed = startColor.substring(4,6);
1641
1642 endColor = endColor.toLowerCase();
1643 String endBlue = endColor.substring(0,2);
1644 String endGreen = endColor.substring(2,4);
1645 String endRed = endColor.substring(4,6);
1646
1647 double proportion = (value - minMaxMedian[0])/(minMaxMedian[1] - minMaxMedian[0]);
1648
1649 // generate an array with hexadecimal code for each RGB entry number
1650 String[] colorTable = new String[256];
1651
1652 int colorTableCounter = 0;
1653
1654 for (int a = 0; a < 10; a++) {
1655
1656 for (int b = 0; b < 10; b++) {
1657
1658 colorTable[colorTableCounter] = a + "" + b;
1659 colorTableCounter ++;
1660 }
1661
1662 for(int c = (int)('a'); c<6+(int)('a'); c++) {
1663 colorTable[colorTableCounter] = a + "" + (char)c;
1664 colorTableCounter ++;
1665 }
1666
1667 }
1668 for(int d = (int)('a'); d<6+(int)('a'); d++) {
1669
1670 for (int e = 0; e < 10; e++) {
1671
1672 colorTable[colorTableCounter] = (char) d + "" + e;
1673 colorTableCounter ++;
1674 }
1675
1676 for(int f = (int)('a'); f<6+(int)('a'); f++) {
1677 colorTable[colorTableCounter] = (char) d + "" + (char) f;
1678 colorTableCounter ++;
1679 }
1680
1681 }
1682
1683
1684 int startBlueInt = 0;
1685 int startGreenInt = 0;
1686 int startRedInt = 0;
1687
1688 int endBlueInt = 0;
1689 int endGreenInt = 0;
1690 int endRedInt = 0;
1691
1692 for (int i = 0; i < colorTable.length; i ++) {
1693
1694 if (colorTable[i].equals(startBlue)) {startBlueInt = i; }
1695 if (colorTable[i].equals(startGreen)) {startGreenInt = i; }
1696 if (colorTable[i].equals(startRed)) {startRedInt = i; }
1697 if (colorTable[i].equals(endBlue)) {endBlueInt = i; }
1698 if (colorTable[i].equals(endGreen)) {endGreenInt = i; }
1699 if (colorTable[i].equals(endRed)) {endRedInt = i; }
1700
1701 }
1702
1703 int blueInt = startBlueInt + (int) Math.round((endBlueInt-startBlueInt)*proportion);
1704 int greenInt = startGreenInt + (int) Math.round((endGreenInt-startGreenInt)*proportion);
1705 int redInt = startRedInt + (int) Math.round((endRedInt-startRedInt)*proportion);
1706
1707 String blue = null;
1708 String green = null;
1709 String red = null;
1710
1711 for (int j = 0; j < colorTable.length; j ++) {
1712
1713 if (j == blueInt) {blue = colorTable[j]; }
1714 if (j == greenInt) {green = colorTable[j]; }
1715 if (j == redInt) {red = colorTable[j]; }
1716
1717 }
1718
1719 String color = blue+green+red;
1720
1721 return color;
1722 }
1723
1724 private static String getOpacity(double opacity) {
1725
1726 String[] opacityTable = new String[256];
1727
1728 int colorTableCounter = 0;
1729
1730 for (int a = 0; a < 10; a++) {
1731
1732 for (int b = 0; b < 10; b++) {
1733
1734 opacityTable[colorTableCounter] = a + "" + b;
1735 colorTableCounter ++;
1736 }
1737
1738 for(int c = (int)('a'); c<6+(int)('a'); c++) {
1739 opacityTable[colorTableCounter] = a + "" + (char)c;
1740 colorTableCounter ++;
1741 }
1742
1743 }
1744 for(int d = (int)('a'); d<6+(int)('a'); d++) {
1745
1746 for (int e = 0; e < 10; e++) {
1747
1748 opacityTable[colorTableCounter] = (char) d + "" + e;
1749 colorTableCounter ++;
1750 }
1751
1752 for(int f = (int)('a'); f<6+(int)('a'); f++) {
1753 opacityTable[colorTableCounter] = (char) d + "" + (char) f;
1754 colorTableCounter ++;
1755 }
1756
1757 }
1758
1759 int opacityInt = (int) Math.round(opacityTable.length*opacity);
1760
1761 String opacityString = "FF";
1762
1763 for (int j = 0; j < opacityTable.length; j ++) {
1764
1765 if (j == opacityInt) {opacityString = opacityTable[j]; }
1766
1767 }
1768
1769 return opacityString;
1770 }
1771
1772 private static double getNodeArea(Node node, String latLongName, String latitudeName, String longitudeName, String contourHPD) {
1773 double area = 0;
1774
1775 Object testAttribute = node.getAttribute(latLongName+"_"+contourHPD+"HPD_modality");
1776 if (testAttribute != null) {
1777 int modality = ((Integer)node.getAttribute(latLongName+"_"+contourHPD+"HPD_modality")).intValue();
1778
1779 for (int x = 0; x < modality; x++) {
1780 Object[] longitudeHPDs = (Object[])node.getAttribute(longitudeName+"_"+contourHPD+"HPD_"+(x + 1));
1781 Object[] latitudeHPDs = (Object[])node.getAttribute(latitudeName+"_"+contourHPD+"HPD_"+(x + 1));
1782
1783 //we can implement Strang's formula like this because the polygon is closed
1784 for (int y = 0; y < longitudeHPDs.length - 1; y++) {
1785
1786 area += ( Double.valueOf((longitudeHPDs[y]).toString()).doubleValue() *
1787 Double.valueOf((latitudeHPDs[y+1]).toString()).doubleValue() ) -
1788 ( Double.valueOf((longitudeHPDs[y+1]).toString()).doubleValue() *
1789 Double.valueOf((latitudeHPDs[y]).toString()).doubleValue() );
1790
1791 }
1792 }
1793 }
1794
1795 return (area/2);
1796 }
1797
1798 }
1799