Search in sources :

Example 1 with Node

use of jebl.evolution.graphs.Node in project beast-mcmc by beast-dev.

the class ContinuousKML method writeTreeToKML.

public void writeTreeToKML(double time, double treeSliceBranchWidth, boolean showBranchAtMidPoint) {
    heightMinAndMax = getHeightMinAndMax(treeToExport);
    treeSliceBuffer.append("\t<Folder>\r");
    treeSliceBuffer.append("\t\t\t<name>tree" + time + "</name>\r");
    contourSliceBuffer.append("\t<Folder>\r");
    contourSliceBuffer.append("\t\t\t<name>contours" + time + "</name>\r");
    int nodeNumber = 0;
    for (Node node : treeToExport.getNodes()) {
        Double longitude = (Double) node.getAttribute(longitudeName);
        Double latitude = (Double) node.getAttribute(latitudeName);
        double nodeHeight = treeToExport.getHeight(node);
        if (!treeToExport.isRoot(node)) {
            Node parentNode = treeToExport.getParent(node);
            Double parentLongitude = (Double) parentNode.getAttribute(longitudeName);
            Double parentLatitude = (Double) parentNode.getAttribute(latitudeName);
            double parentHeight = treeToExport.getHeight(parentNode);
            if ((parentHeight > time) && (nodeHeight <= time)) {
                //extrapolate lat/long
                if (!showBranchAtMidPoint) {
                    latitude = parentLatitude + (latitude - parentLatitude) * ((parentHeight - time) / (parentHeight - nodeHeight));
                    longitude = parentLongitude + (longitude - parentLongitude) * ((parentHeight - time) / (parentHeight - nodeHeight));
                }
            }
            if (((parentHeight > time) && !(showBranchAtMidPoint)) || (showBranchAtMidPoint && (time < ((nodeHeight + ((parentHeight - nodeHeight) / 2.0)))))) {
                treeSliceBuffer.append("\t\t<Placemark>\r");
                treeSliceBuffer.append("\t\t\t<name>treeSliceBranch" + nodeNumber + "_" + time + "</name>\r");
                treeSliceBuffer.append("\t\t\t<styleUrl>#treeSliceBranch" + nodeNumber + "_" + time + "_style</styleUrl>\r");
                treeSliceBuffer.append("\t\t\t<LineString>\r");
                treeSliceBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r");
                treeSliceBuffer.append("\t\t\t\t<coordinates>\r");
                treeSliceBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + ",0\r");
                treeSliceBuffer.append("\t\t\t\t\t" + parentLongitude + "," + parentLatitude + ",0\r");
                treeSliceBuffer.append("\t\t\t\t</coordinates>\r");
                treeSliceBuffer.append("\t\t\t</LineString>\r");
                treeSliceBuffer.append("\t\t</Placemark>\r");
                styleBuffer.append("\t<Style id=\"treeSliceBranch" + nodeNumber + "_" + time + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                styleBuffer.append("\t\t\t<width>" + treeSliceBranchWidth + "</width>\r");
                styleBuffer.append("\t\t\t<color>" + "FF" + ContinuousKML.getKMLColor((nodeHeight + ((parentHeight - nodeHeight) / 2.0)), heightMinAndMax, startBranchColor_TS, endBranchColor_TS) + "</color>\r");
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                appendContour(contourSliceBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
            }
        }
        nodeNumber++;
    }
    treeSliceBuffer.append("\t</Folder>\r");
    contourSliceBuffer.append("\t</Folder>\r");
}
Also used : Node(jebl.evolution.graphs.Node)

Example 2 with Node

use of jebl.evolution.graphs.Node in project beast-mcmc by beast-dev.

the class ContinuousKML method getHeightMinAndMax.

private double[] getHeightMinAndMax(RootedTree tree) {
    double[] minAndMax = new double[2];
    double[] heights = new double[tree.getNodes().size()];
    int i = 0;
    for (Node node : tree.getNodes()) {
        heights[i] = tree.getHeight(node);
        i++;
    }
    double max = 0.0;
    double min = Double.MAX_VALUE;
    for (int j = 0; j < heights.length; j++) {
        if (heights[j] > max) {
            max = heights[j];
        }
        if (heights[j] < min) {
            min = heights[j];
        }
    }
    minAndMax[0] = min;
    minAndMax[1] = max;
    return minAndMax;
}
Also used : Node(jebl.evolution.graphs.Node)

Example 3 with Node

use of jebl.evolution.graphs.Node in project beast-mcmc by beast-dev.

the class ContinuousKML method writeTreeToKML.

public void writeTreeToKML() {
    double scaleFactor = plotHeight / treeToExport.getHeight(treeToExport.getRootNode());
    heightMinAndMax = getHeightMinAndMax(treeToExport);
    if (useRates_RT || useRates_TT) {
        rateMinMaxMedian = getRateMinMaxMedian(treeToExport, useLogRates_TT);
    }
    // convert initial visibility booleans to int (0 = false, 1 = true)
    int visibility_RT;
    if (iniVisi_RT) {
        visibility_RT = 1;
    } else {
        visibility_RT = 0;
    }
    int visibility_TT;
    if (iniVisi_TT) {
        visibility_TT = 1;
    } else {
        visibility_TT = 0;
    }
    int visibility_ST;
    if (iniVisi_ST) {
        visibility_ST = 1;
    } else {
        visibility_ST = 0;
    }
    int visibility_taxa;
    if (iniVisi_taxa) {
        visibility_taxa = 1;
    } else {
        visibility_taxa = 0;
    }
    //
    int nodeNumber = 0;
    for (Node node : treeToExport.getNodes()) {
        nodeNumber++;
        Double longitude = (Double) node.getAttribute(longitudeName);
        Double latitude = (Double) node.getAttribute(latitudeName);
        double altitude = (treeToExport.getHeight(node) * scaleFactor);
        if (!treeToExport.isRoot(node)) {
            Node parentNode = treeToExport.getParent(node);
            Double parentLongitude = (Double) parentNode.getAttribute(longitudeName);
            if (parentLongitude == null) {
                throw new RuntimeException("Longitude attribute, " + latitudeName + ", not found in tree");
            }
            Double parentLatitude = (Double) parentNode.getAttribute(latitudeName);
            if (parentLatitude == null) {
                throw new RuntimeException("Latitude attribute, " + latitudeName + ", not found in tree");
            }
            double parentAltitude = (treeToExport.getHeight(parentNode) * scaleFactor);
            if (plotHeight > 0) {
                rectangleTreeBuffer.append("\t\t<Placemark>\r");
                rectangleTreeBuffer.append("\t\t\t<visibility>" + visibility_RT + "</visibility>\r");
                rectangleTreeBuffer.append("\t\t\t<name>rectangleTreeBranch" + nodeNumber + "</name>\r");
                rectangleTreeBuffer.append("\t\t\t<styleUrl>#rectangleTreeBranch" + nodeNumber + "_style</styleUrl>\r");
                rectangleTreeBuffer.append("\t\t\t<LineString>\r");
                rectangleTreeBuffer.append("\t\t\t\t<tessellate>1</tessellate>\r");
                rectangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
                rectangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
                rectangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + altitude + "\r");
                rectangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + parentAltitude + "\r");
                if (branchChop_RT > 0) {
                    double longInterval = parentLongitude - longitude;
                    double latInterval = parentLatitude - latitude;
                    double steps;
                    double longStepSize;
                    double latStepSize;
                    if (Math.abs(longInterval) > Math.abs(latInterval)) {
                        steps = (Math.abs(longInterval) / branchChop_RT) - 1;
                        if (longInterval > 0) {
                            longStepSize = branchChop_RT;
                        } else {
                            longStepSize = -branchChop_RT;
                        }
                        latStepSize = latInterval / (steps + 1);
                    } else {
                        steps = (Math.abs(latInterval) / branchChop_RT) - 1;
                        if (latInterval > 0) {
                            latStepSize = branchChop_RT;
                        } else {
                            latStepSize = -branchChop_RT;
                        }
                        longStepSize = longInterval / (steps + 1);
                    }
                    for (int x = 0; x < steps; x++) {
                        rectangleTreeBuffer.append("\t\t\t\t\t" + (longitude + (x + 1) * longStepSize) + "," + (latitude + (x + 1) * latStepSize) + "," + parentAltitude + "\r");
                    }
                }
                rectangleTreeBuffer.append("\t\t\t\t\t" + parentLongitude + "," + parentLatitude + "," + parentAltitude + "\r");
                rectangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
                rectangleTreeBuffer.append("\t\t\t</LineString>\r");
                rectangleTreeBuffer.append("\t\t</Placemark>\r");
                triangleTreeBuffer.append("\t\t<Placemark>\r");
                triangleTreeBuffer.append("\t\t\t<visibility>" + visibility_TT + "</visibility>\r");
                triangleTreeBuffer.append("\t\t\t<name>triangleTreeBranch" + nodeNumber + "</name>\r");
                triangleTreeBuffer.append("\t\t\t<styleUrl>#triangleTreeBranch" + nodeNumber + "_style</styleUrl>\r");
                triangleTreeBuffer.append("\t\t\t<LineString>\r");
                triangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
                triangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
                triangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + altitude + "\r");
                triangleTreeBuffer.append("\t\t\t\t\t" + parentLongitude + "," + parentLatitude + "," + parentAltitude + "\r");
                triangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
                triangleTreeBuffer.append("\t\t\t</LineString>\r");
                triangleTreeBuffer.append("\t\t</Placemark>\r");
            }
            //variables required for chopping up the branches of the surface Tree
            double maxAltitude = (treeToExport.getHeight(parentNode) - treeToExport.getHeight(node)) * altitudeFactor;
            double latitudeDifference = parentLatitude - latitude;
            double longitudeDifference = parentLongitude - longitude;
            surfaceTreeBuffer.append("\t<Folder>\r");
            surfaceTreeBuffer.append("\t\t\t<name>surfaceTreeBranch" + nodeNumber + "</name>\r");
            for (int a = 0; a < divider; a++) {
                surfaceTreeBuffer.append("\t\t<Placemark>\r");
                surfaceTreeBuffer.append("\t\t\t<visibility>" + visibility_ST + "</visibility>\r");
                surfaceTreeBuffer.append("\t\t\t<name>surfaceTreeBranch" + nodeNumber + "_part" + (a + 1) + "</name>\r");
                surfaceTreeBuffer.append("\t\t\t<TimeSpan>\r");
                //convert height of the branch segment to a real date (based on th date for the most recent sample)
                double date = mostRecentDate - (treeToExport.getHeight(node) + (a + 1) * ((treeToExport.getHeight(parentNode) - (treeToExport.getHeight(node))) / divider));
                String[] yearMonthDay = convertToYearMonthDay(date);
                if (ancient) {
                    surfaceTreeBuffer.append("\t\t\t\t<begin>" + Math.round(date) + "</begin>\r");
                } else {
                    surfaceTreeBuffer.append("\t\t\t\t<begin>" + yearMonthDay[0] + "-" + yearMonthDay[1] + "-" + yearMonthDay[2] + "</begin>\r");
                }
                surfaceTreeBuffer.append("\t\t\t</TimeSpan>\r");
                surfaceTreeBuffer.append("\t\t\t<styleUrl>#surfaceTreeBranch" + nodeNumber + "_part" + (a + 1) + "_style</styleUrl>\r");
                // surfaceTreeBuffer.append("\t\t\t<styleUrl>#surfaceTreeBranch"+nodeNumber+"_style</styleUrl>\r");
                surfaceTreeBuffer.append("\t\t\t<LineString>\r");
                if (arcBranches) {
                    surfaceTreeBuffer.append("\t\t\t\t<altitudeMode>absolute</altitudeMode>\r");
                    surfaceTreeBuffer.append("\t\t\t\t<tessellate>1</tessellate>\r");
                } else {
                    surfaceTreeBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r");
                }
                surfaceTreeBuffer.append("\t\t\t\t<coordinates>\r");
                surfaceTreeBuffer.append("\t\t\t\t\t" + (longitude + a * (longitudeDifference / divider)) + "," + (latitude + a * (latitudeDifference / divider)) + "," + (maxAltitude * Math.sin(Math.acos(1 - a * (1.0 / (divider / 2.0))))) + "\r");
                surfaceTreeBuffer.append("\t\t\t\t\t" + (longitude + (a + 1) * (longitudeDifference / divider)) + "," + (latitude + (a + 1) * (latitudeDifference / divider)) + "," + (maxAltitude * Math.sin(Math.acos(1 - (a + 1) * (1.0 / (divider / 2.0))))) + "\r");
                surfaceTreeBuffer.append("\t\t\t\t</coordinates>\r");
                surfaceTreeBuffer.append("\t\t\t</LineString>\r");
                surfaceTreeBuffer.append("\t\t</Placemark>\r");
                styleBuffer.append("\t<Style id=\"surfaceTreeBranch" + nodeNumber + "_part" + (a + 1) + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_ST) {
                    if (treeToExport.isExternal(node)) {
                        styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
                    } else {
                        Double posterior = (Double) node.getAttribute("posterior");
                        styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + posterior * branchWidthMultiplier) + "</width>\r");
                    }
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_ST + "</width>\r");
                }
                if (useHeights_ST) {
                    styleBuffer.append("\t\t\t<color>" + "FF" + getKMLColor((treeToExport.getHeight(node) + (a + 1) * ((treeToExport.getHeight(parentNode) - (treeToExport.getHeight(node))) / divider)), heightMinAndMax, startBranchColor_ST, endBranchColor_ST) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + "FF" + branchColor_ST + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
            }
            surfaceTreeBuffer.append("\t</Folder>\r");
            //this writes placemarks on the surface for the taxa, and writes out projections from the tips to the surface
            if (treeToExport.isExternal(node)) {
                taxaBuffer.append("\t\t\t<Placemark>\r");
                taxaBuffer.append("\t\t\t<visibility>" + visibility_taxa + "</visibility>\r");
                taxaBuffer.append("\t\t\t\t<name>" + treeToExport.getTaxon(node).getName() + "</name>\r");
                taxaBuffer.append("\t\t\t\t<Point>\r");
                taxaBuffer.append("\t\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
                taxaBuffer.append("\t\t\t\t\t<coordinates>" + longitude + "," + latitude + "," + "0</coordinates>\r");
                taxaBuffer.append("\t\t\t\t</Point>\r");
                taxaBuffer.append("\t\t\t</Placemark>\r");
                if (treeToExport.getHeight(node) > 0) {
                    projectionsBuffer.append("\t\t\t<LineString>\r");
                    projectionsBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
                    projectionsBuffer.append("\t\t\t\t<coordinates>\r");
                    projectionsBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + altitude + "\r");
                    projectionsBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + ",0\r");
                    projectionsBuffer.append("\t\t\t\t</coordinates>\r");
                    projectionsBuffer.append("\t\t\t</LineString>\r");
                }
                styleBuffer.append("\t<Style id=\"rectangleTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_RT) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_RT + "</width>\r");
                }
                if (useRates_RT || useLogRates_RT) {
                    double rate = 0;
                    if (useRates_RT) {
                        rate = (Double) node.getAttribute("rate");
                    } else if (useLogRates_RT) {
                        rate = Math.log((Double) node.getAttribute("rate"));
                    }
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + getKMLColor(rate, rateMinMaxMedian, startBranchColor_RT, endBranchColor_RT) + "</color>\r");
                } else if (useHeights_RT) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_RT, endBranchColor_RT) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + branchColor_RT + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                styleBuffer.append("\t<Style id=\"triangleTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_TT) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_TT + "</width>\r");
                }
                if (useRates_TT || useLogRates_TT) {
                    double rate = 0;
                    if (useRates_TT) {
                        rate = (Double) node.getAttribute("rate");
                    } else if (useLogRates_TT) {
                        rate = Math.log((Double) node.getAttribute("rate"));
                    }
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + getKMLColor(rate, rateMinMaxMedian, startBranchColor_TT, endBranchColor_TT) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + branchColor_TT + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                // line style for surface external branches, based on posterior
                styleBuffer.append("\t<Style id=\"surfaceTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_ST) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_ST + "</width>\r");
                }
                if (useHeights_ST) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_ST) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_ST, endBranchColor_ST) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_ST) + branchColor_ST + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                if (tipsSampled) {
                    if (contoursAndNotSpades) {
                        appendContour(contourTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
                        appendContour(groundContourTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
                    } else {
                        appendSpade(spadeTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
                        appendSpade(groundSpadeTipsBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
                        appendDiamond(diamondTipsBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
                    }
                    //contour
                    if (contoursAndNotSpades) {
                        styleBuffer.append("\t<Style id=\"contour" + nodeNumber + "_style\">\r");
                        styleBuffer.append("\t\t<LineStyle>\r");
                        styleBuffer.append("\t\t\t<width>1.5</width>\r");
                        if (useHeights_tipContours) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipContours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startTipContourColor, endTipContourColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipContours) + tipContourColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t</LineStyle>\r");
                        styleBuffer.append("\t\t<PolyStyle>\r");
                        if (useHeights_tipContours) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipContours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startTipContourColor, endTipContourColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipContours) + tipContourColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t</PolyStyle>\r");
                        styleBuffer.append("\t</Style>\r");
                    //spade
                    } else {
                        styleBuffer.append("\t<Style id=\"spade" + nodeNumber + "_style\">\r");
                        styleBuffer.append("\t\t<LineStyle>\r");
                        styleBuffer.append("\t\t\t<width>1.5</width>\r");
                        if (useHeights_tipSpades) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipSpades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startTipSpadeColor, endTipSpadeColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipSpades) + tipSpadeColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t</LineStyle>\r");
                        styleBuffer.append("\t\t<PolyStyle>\r");
                        if (useHeights_tipSpades) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipSpades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startTipSpadeColor, endTipSpadeColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_tipSpades) + tipSpadeColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t</PolyStyle>\r");
                        styleBuffer.append("\t</Style>\r");
                    }
                    //contour
                    if (contoursAndNotSpades) {
                        styleBuffer.append("\t<Style id=\"groundContour" + nodeNumber + "_style\">\r");
                        styleBuffer.append("\t\t<LineStyle>\r");
                        styleBuffer.append("\t\t\t<width>0.5</width>\r");
                        styleBuffer.append("\t\t</LineStyle>\r");
                        styleBuffer.append("\t\t<PolyStyle>\r");
                        if (useHeights_groundTipSpades) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundTipContours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundTipContourColor, endgroundTipContourColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundTipContours) + groundTipContourColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t\t<outline>0</outline>\r");
                        styleBuffer.append("\t\t</PolyStyle>\r");
                        styleBuffer.append("\t</Style>\r");
                    //spade
                    } else {
                        styleBuffer.append("\t<Style id=\"groundSpade" + nodeNumber + "_style\">\r");
                        styleBuffer.append("\t\t<LineStyle>\r");
                        styleBuffer.append("\t\t\t<width>0.5</width>\r");
                        styleBuffer.append("\t\t</LineStyle>\r");
                        styleBuffer.append("\t\t<PolyStyle>\r");
                        if (useHeights_groundTipSpades) {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundTipSpades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundTipSpadeColor, endgroundTipSpadeColor) + "</color>\r");
                        } else {
                            styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundTipSpades) + groundTipSpadeColor + "</color>\r");
                        }
                        styleBuffer.append("\t\t\t<outline>0</outline>\r");
                        styleBuffer.append("\t\t</PolyStyle>\r");
                        styleBuffer.append("\t</Style>\r");
                    }
                    totalHPDarea += getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
                }
            } else {
                if (contoursAndNotSpades) {
                    appendContour(contourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
                    appendContour(groundContourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
                } else {
                    appendSpade(spadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
                    appendSpade(groundSpadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
                    appendDiamond(diamondBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
                }
                Double posterior = (Double) node.getAttribute("posterior");
                // line style for internal rectangleTree branches, based on posterior
                styleBuffer.append("\t<Style id=\"rectangleTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_RT) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + posterior * branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_RT + "</width>\r");
                }
                if (useRates_RT || useLogRates_RT) {
                    double rate = 0;
                    if (useRates_RT) {
                        rate = (Double) node.getAttribute("rate");
                    } else if (useLogRates_RT) {
                        rate = Math.log((Double) node.getAttribute("rate"));
                    }
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + getKMLColor(rate, rateMinMaxMedian, startBranchColor_RT, endBranchColor_RT) + "</color>\r");
                } else if (useHeights_RT) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_RT, endBranchColor_RT) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + branchColor_RT + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                // line style for internal nodes, based on posterior
                styleBuffer.append("\t<Style id=\"triangleTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_TT) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + posterior * branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_TT + "</width>\r");
                }
                if (useRates_TT || useLogRates_TT) {
                    double rate = 0;
                    if (useRates_TT) {
                        rate = (Double) node.getAttribute("rate");
                    } else if (useLogRates_TT) {
                        rate = Math.log((Double) node.getAttribute("rate"));
                    }
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + getKMLColor(rate, rateMinMaxMedian, startBranchColor_TT, endBranchColor_TT) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + branchColor_TT + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                // line style for surface internal branches, based on posterior
                styleBuffer.append("\t<Style id=\"surfaceTreeBranch" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                if (usePosterior_ST) {
                    styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + posterior * branchWidthMultiplier) + "</width>\r");
                } else {
                    styleBuffer.append("\t\t\t<width>" + branchWidth_ST + "</width>\r");
                }
                if (useHeights_ST) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_ST) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startBranchColor_ST, endBranchColor_ST) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_ST) + branchColor_ST + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t</Style>\r");
                //contour
                if (contoursAndNotSpades) {
                    styleBuffer.append("\t<Style id=\"contour" + nodeNumber + "_style\">\r");
                    styleBuffer.append("\t\t<LineStyle>\r");
                    styleBuffer.append("\t\t\t<width>1.5</width>\r");
                    if (useHeights_contours) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startContourColor, endContourColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + contourColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t</LineStyle>\r");
                    styleBuffer.append("\t\t<PolyStyle>\r");
                    if (useHeights_contours) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startContourColor, endContourColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + contourColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t</PolyStyle>\r");
                    styleBuffer.append("\t</Style>\r");
                //spade
                } else {
                    styleBuffer.append("\t<Style id=\"spade" + nodeNumber + "_style\">\r");
                    styleBuffer.append("\t\t<LineStyle>\r");
                    styleBuffer.append("\t\t\t<width>1.5</width>\r");
                    if (useHeights_spades) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startSpadeColor, endSpadeColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + spadeColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t</LineStyle>\r");
                    styleBuffer.append("\t\t<PolyStyle>\r");
                    if (useHeights_spades) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startSpadeColor, endSpadeColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + spadeColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t</PolyStyle>\r");
                    styleBuffer.append("\t</Style>\r");
                }
                //contour
                if (contoursAndNotSpades) {
                    styleBuffer.append("\t<Style id=\"groundContour" + nodeNumber + "_style\">\r");
                    styleBuffer.append("\t\t<LineStyle>\r");
                    styleBuffer.append("\t\t\t<width>0.5</width>\r");
                    styleBuffer.append("\t\t</LineStyle>\r");
                    styleBuffer.append("\t\t<PolyStyle>\r");
                    if (useHeights_groundSpades) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundContours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundContourColor, endgroundContourColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundContours) + groundContourColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t\t<outline>0</outline>\r");
                    styleBuffer.append("\t\t</PolyStyle>\r");
                    styleBuffer.append("\t</Style>\r");
                //spade
                } else {
                    styleBuffer.append("\t<Style id=\"groundSpade" + nodeNumber + "_style\">\r");
                    styleBuffer.append("\t\t<LineStyle>\r");
                    styleBuffer.append("\t\t\t<width>0.5</width>\r");
                    styleBuffer.append("\t\t</LineStyle>\r");
                    styleBuffer.append("\t\t<PolyStyle>\r");
                    if (useHeights_groundSpades) {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundSpades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundSpadeColor, endgroundSpadeColor) + "</color>\r");
                    } else {
                        styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundSpades) + groundSpadeColor + "</color>\r");
                    }
                    styleBuffer.append("\t\t\t<outline>0</outline>\r");
                    styleBuffer.append("\t\t</PolyStyle>\r");
                    styleBuffer.append("\t</Style>\r");
                }
                totalHPDarea += getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
            }
        // write a ancestral branch to the root with length that is a precentage of the total height
        } else {
            rectangleTreeBuffer.append("\t\t<Placemark>\r");
            triangleTreeBuffer.append("\t\t<Placemark>\r");
            // 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)
            double date = mostRecentDate - (treeToExport.getHeight(node) + (fraction * treeToExport.getHeight(node)));
            String[] yearMonthDay = convertToYearMonthDay(date);
            rectangleTreeBuffer.append("\t\t\t<TimeSpan>\r");
            triangleTreeBuffer.append("\t\t\t<TimeSpan>\r");
            if (ancient) {
                rectangleTreeBuffer.append("\t\t\t\t<begin>" + Math.round(date) + "</begin>\r");
                triangleTreeBuffer.append("\t\t\t\t<begin>" + Math.round(date) + "</begin>\r");
            } else {
                rectangleTreeBuffer.append("\t\t\t\t<begin>" + yearMonthDay[0] + "-" + yearMonthDay[1] + "-" + yearMonthDay[2] + "</begin>\r");
                triangleTreeBuffer.append("\t\t\t\t<begin>" + yearMonthDay[0] + "-" + yearMonthDay[1] + "-" + yearMonthDay[2] + "</begin>\r");
            }
            rectangleTreeBuffer.append("\t\t\t</TimeSpan>\r");
            triangleTreeBuffer.append("\t\t\t</TimeSpan>\r");
            rectangleTreeBuffer.append("\t\t\t<visibility>" + visibility_RT + "</visibility>\r");
            triangleTreeBuffer.append("\t\t\t<visibility>" + visibility_TT + "</visibility>\r");
            rectangleTreeBuffer.append("\t\t\t<name>rectangleTreeBranch" + nodeNumber + "</name>\r");
            triangleTreeBuffer.append("\t\t\t<name>triangleTreeBranch" + nodeNumber + "</name>\r");
            rectangleTreeBuffer.append("\t\t\t<styleUrl>#rectangleTreeBranch" + nodeNumber + "_style</styleUrl>\r");
            triangleTreeBuffer.append("\t\t\t<styleUrl>#triangleTreeBranch" + nodeNumber + "_style</styleUrl>\r");
            rectangleTreeBuffer.append("\t\t\t<LineString>\r");
            triangleTreeBuffer.append("\t\t\t<LineString>\r");
            rectangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
            triangleTreeBuffer.append("\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r");
            rectangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
            triangleTreeBuffer.append("\t\t\t\t<coordinates>\r");
            rectangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + altitude + "\r");
            triangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + altitude + "\r");
            rectangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + (altitude + (fraction * altitude)) + "\r");
            triangleTreeBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + "," + (altitude + (fraction * altitude)) + "\r");
            rectangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
            triangleTreeBuffer.append("\t\t\t\t</coordinates>\r");
            rectangleTreeBuffer.append("\t\t\t</LineString>\r");
            triangleTreeBuffer.append("\t\t\t</LineString>\r");
            rectangleTreeBuffer.append("\t\t</Placemark>\r");
            triangleTreeBuffer.append("\t\t</Placemark>\r");
            if (contoursAndNotSpades) {
                appendContour(contourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, false, iniVisi_contours, locationHPDpercentage, ancient);
                appendContour(groundContourBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, traitName, latitudeName, longitudeName, true, iniVisi_groundContours, locationHPDpercentage, ancient);
            } else {
                appendSpade(spadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, false, iniVisi_spades, locationHPDpercentage, ancient);
                appendSpade(groundSpadeBuffer, treeToExport, node, nodeNumber, plotHeight, mostRecentDate, latitudeName, longitudeName, true, iniVisi_groundSpades, locationHPDpercentage, ancient);
                appendDiamond(diamondBuffer, treeToExport, node, nodeNumber, plotHeight, latitudeName, longitudeName, locationHPDpercentage, iniVisi_diamonds);
            }
            // style for the rectangle root branch
            styleBuffer.append("\t<Style id=\"rectangleTreeBranch" + nodeNumber + "_style\">\r");
            styleBuffer.append("\t\t<LineStyle>\r");
            if (usePosterior_RT) {
                styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
            } else {
                styleBuffer.append("\t\t\t<width>" + branchWidth_RT + "</width>\r");
            }
            if (useRates_RT) {
                // the root branch gets the color for the median rate
                styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + getKMLColor(rateMinMaxMedian[2], rateMinMaxMedian, startBranchColor_RT, endBranchColor_RT) + "</color>\r");
            } else {
                styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_RT) + branchColor_RT + "</color>\r");
            }
            styleBuffer.append("\t\t</LineStyle>\r");
            styleBuffer.append("\t</Style>\r");
            // style for the triangle root branch
            styleBuffer.append("\t<Style id=\"triangleTreeBranch" + nodeNumber + "_style\">\r");
            styleBuffer.append("\t\t<LineStyle>\r");
            if (usePosterior_RT) {
                styleBuffer.append("\t\t\t<width>" + (branchWidthConstant + branchWidthMultiplier) + "</width>\r");
            } else {
                styleBuffer.append("\t\t\t<width>" + branchWidth_TT + "</width>\r");
            }
            if (useRates_TT) {
                // the root branch gets the color for the median rate
                styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + getKMLColor(rateMinMaxMedian[2], rateMinMaxMedian, startBranchColor_TT, endBranchColor_TT) + "</color>\r");
            } else {
                styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_TT) + branchColor_TT + "</color>\r");
            }
            styleBuffer.append("\t\t</LineStyle>\r");
            styleBuffer.append("\t</Style>\r");
            // style for the root spade/contour based on height
            if (contoursAndNotSpades) {
                styleBuffer.append("\t<Style id=\"contour" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                styleBuffer.append("\t\t\t<width>1.5</width>\r");
                if (useHeights_contours) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startContourColor, endContourColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + contourColor + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t\t<PolyStyle>\r");
                if (useHeights_contours) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startContourColor, endContourColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_contours) + contourColor + "</color>\r");
                }
                styleBuffer.append("\t\t</PolyStyle>\r");
                styleBuffer.append("\t</Style>\r");
            } else {
                styleBuffer.append("\t<Style id=\"spade" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                styleBuffer.append("\t\t\t<width>1.5</width>\r");
                if (useHeights_spades) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startSpadeColor, endSpadeColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + spadeColor + "</color>\r");
                }
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t\t<PolyStyle>\r");
                if (useHeights_spades) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startSpadeColor, endSpadeColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_spades) + spadeColor + "</color>\r");
                }
                styleBuffer.append("\t\t</PolyStyle>\r");
                styleBuffer.append("\t</Style>\r");
            }
            // ground spade/contour style for root
            if (contoursAndNotSpades) {
                styleBuffer.append("\t<Style id=\"groundContour" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                styleBuffer.append("\t\t\t<width>0.5</width>\r");
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t\t<PolyStyle>\r");
                if (useHeights_groundContours) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundContours) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundContourColor, endgroundContourColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundContours) + groundContourColor + "</color>\r");
                }
                styleBuffer.append("\t\t\t<outline>0</outline>");
                styleBuffer.append("\t\t</PolyStyle>\r");
                styleBuffer.append("\t</Style>\r");
            } else {
                styleBuffer.append("\t<Style id=\"groundSpade" + nodeNumber + "_style\">\r");
                styleBuffer.append("\t\t<LineStyle>\r");
                styleBuffer.append("\t\t\t<width>0.5</width>\r");
                styleBuffer.append("\t\t</LineStyle>\r");
                styleBuffer.append("\t\t<PolyStyle>\r");
                if (useHeights_groundSpades) {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundSpades) + getKMLColor(treeToExport.getHeight(node), heightMinAndMax, startgroundSpadeColor, endgroundSpadeColor) + "</color>\r");
                } else {
                    styleBuffer.append("\t\t\t<color>" + getOpacity(opacity_groundSpades) + groundSpadeColor + "</color>\r");
                }
                styleBuffer.append("\t\t\t<outline>0</outline>");
                styleBuffer.append("\t\t</PolyStyle>\r");
                styleBuffer.append("\t</Style>\r");
            }
            double rootHPDArea = getNodeArea(node, traitName, latitudeName, longitudeName, locationHPDpercentage);
            totalHPDarea += rootHPDArea;
            System.out.println("root HPD area is " + rootHPDArea + " degrees");
        }
    }
    System.out.println("total HPD area is " + totalHPDarea + " degrees");
}
Also used : Node(jebl.evolution.graphs.Node)

Example 4 with Node

use of jebl.evolution.graphs.Node in project beast-mcmc by beast-dev.

the class TreeDensityKML method generateKMLTree.

private Element generateKMLTree(String name, RootedTree tree) {
    Element element = generateContainer("Folder", name, null, null);
    double longNoise = Random.nextGaussian() * 0.5;
    double latNoise = Random.nextGaussian() * 0.5;
    int nodeNumber = 0;
    for (Node node : tree.getNodes()) {
        nodeNumber++;
        if (!tree.isRoot(node)) {
            String state = (String) node.getAttribute(STATE_ATTRIBUTE_NAME);
            Location location = locationMap.get(state);
            if (location == null) {
                throw new RuntimeException("No location called " + state + " in location list");
            }
            // Create each branch of the tree..
            String nodeName = name + "_node" + nodeNumber;
            Node parentNode = tree.getParent(node);
            String parentState = (String) parentNode.getAttribute(STATE_ATTRIBUTE_NAME);
            Location parentLocation = locationMap.get(parentState);
            if (parentLocation == null) {
                throw new RuntimeException("No location called " + parentState + " in location list");
            }
            Element branch = generateContainer("Placemark", nodeName, null, null);
            //                annotateBranch(element, height, startDate, finishDate, rate, support);
            Element lineString = new Element("LineString");
            lineString.addContent(generateElement("altitudeMode", "clampToGround"));
            Element coordinates = new Element("coordinates");
            coordinates.addContent("" + (parentLocation.getLongitude() + longNoise) + "," + (parentLocation.getLatitude() + latNoise) + "\r");
            coordinates.addContent("" + (location.getLongitude() + longNoise) + "," + (location.getLatitude() + latNoise) + "\r");
            lineString.addContent(coordinates);
            branch.addContent(lineString);
            element.addContent(branch);
        }
    }
    return element;
}
Also used : Element(org.jdom.Element) Node(jebl.evolution.graphs.Node) Location(dr.app.phylogeography.tools.kml.Location)

Example 5 with Node

use of jebl.evolution.graphs.Node in project beast-mcmc by beast-dev.

the class DiscreteKMLString method writeTreeToKML.

public void writeTreeToKML(double time, double treeSliceBranchWidth, boolean showBranchAtMidPoint) {
    treeSliceBuffer.append("\t<Folder>\r");
    treeSliceBuffer.append("\t\t\t<name>tree" + time + "</name>\r");
    int nodeNumber = 0;
    posteriorMinAndMax[1] = 1;
    posteriorMinAndMax[0] = getPosteriorMin(treeToExport);
    heightMinAndMax[0] = 0;
    heightMinAndMax[1] = treeToExport.getHeight(treeToExport.getRootNode());
    for (Node node : treeToExport.getNodes()) {
        nodeNumber++;
        String state = ((((String) node.getAttribute(annotation)).replaceAll("\"", "")).replaceAll(" ", "")).trim();
        //in case the location state is a concatenation of other states (occurs when they get the same posterior prob)
        if (state.contains("+")) {
            state = state.substring(0, state.indexOf("+"));
        }
        if (!treeToExport.isRoot(node)) {
            Node parentNode = treeToExport.getParent(node);
            String parentState = ((((String) parentNode.getAttribute(annotation)).replaceAll("\"", "")).replaceAll(" ", "")).trim();
            if (parentState.contains("+")) {
                parentState = parentState.substring(0, parentState.indexOf('+'));
            }
            if (!(state.toLowerCase()).equals(parentState.toLowerCase())) {
                double latitude = getCoordinate(state, stateNames, stateCoordinates, 0);
                double longitude = getCoordinate(state, stateNames, stateCoordinates, 1);
                //System.out.println(latitude+"\t"+longitude);
                if ((latitude == 0) && (longitude == 0)) {
                    System.err.println(state + " has no coordinate??");
                }
                double nodeHeight = treeToExport.getHeight(node);
                double parentLatitude = getCoordinate(parentState, stateNames, stateCoordinates, 0);
                double parentLongitude = getCoordinate(parentState, stateNames, stateCoordinates, 1);
                double parentHeight = treeToExport.getHeight(treeToExport.getParent(node));
                boolean longitudeBreak = longitudeBreak(longitude, parentLongitude);
                if ((parentHeight > time) && (nodeHeight <= time)) {
                    if (!showBranchAtMidPoint) {
                        latitude = parentLatitude + (latitude - parentLatitude) * ((parentHeight - time) / (parentHeight - nodeHeight));
                        if (longitudeBreak) {
                            if (longitude > 0) {
                                double currentLongitude = parentLongitude - ((180 - longitude) + (180 + parentLongitude)) * ((parentHeight - time) / (parentHeight - nodeHeight));
                                if (currentLongitude < -180) {
                                    longitude = (180 + (180 + currentLongitude));
                                //System.out.print("break1"+currentLongitude+"\t"+longitude);
                                } else {
                                    longitude = currentLongitude;
                                //System.out.print("break2"+currentLongitude+"\t"+longitude);
                                }
                            } else {
                                double currentLongitude = parentLongitude + ((180 - parentLongitude) + (180 + longitude)) * ((parentHeight - time) / (parentHeight - nodeHeight));
                                if (currentLongitude > 180) {
                                    longitude = (-180 - (180 - currentLongitude));
                                //System.out.print("break3"+currentLongitude+"\t"+longitude);
                                } else {
                                    longitude = currentLongitude;
                                //System.out.print("break4"+longitude);
                                }
                            }
                        //System.out.print("\t"+state+"_"+longitude+"\t"+parentState+"\r");
                        } else {
                            longitude = parentLongitude + (longitude - parentLongitude) * ((parentHeight - time) / (parentHeight - nodeHeight));
                        }
                    }
                }
                if (((parentHeight * timeScaler > time) && !(showBranchAtMidPoint)) || (showBranchAtMidPoint && (time < ((nodeHeight + ((parentHeight - nodeHeight) / 2.0)) * timeScaler)))) {
                    treeSliceBuffer.append("\t\t<Placemark>\r");
                    treeSliceBuffer.append("\t\t\t<name>branch" + nodeNumber + "_" + parentState + "_" + state + "</name>\r");
                    //style
                    treeSliceBuffer.append("\t\t\t<styleUrl>#tree" + time + "branch" + nodeNumber + "_style</styleUrl>\r");
                    // branchesBuffer.append("\t\t\t<styleUrl>#surfaceTreeBranch"+nodeNumber+"_style</styleUrl>\r");
                    treeSliceBuffer.append("\t\t\t<LineString>\r");
                    treeSliceBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r");
                    treeSliceBuffer.append("\t\t\t\t<coordinates>\r");
                    treeSliceBuffer.append("\t\t\t\t\t" + longitude + "," + latitude + ",0\r");
                    treeSliceBuffer.append("\t\t\t\t\t" + parentLongitude + "," + parentLatitude + ",0\r");
                    treeSliceBuffer.append("\t\t\t\t</coordinates>\r");
                    treeSliceBuffer.append("\t\t\t</LineString>\r");
                    treeSliceBuffer.append("\t\t</Placemark>\r");
                    styleBuffer.append("\t<Style id=\"tree" + time + "branch" + nodeNumber + "_style\">\r");
                    styleBuffer.append("\t\t<LineStyle>\r");
                    styleBuffer.append("\t\t\t<width>" + treeSliceBranchWidth + "</width>\r");
                    styleBuffer.append("\t\t\t<color>" + "FF" + ContinuousKML.getKMLColor((nodeHeight + ((parentHeight - nodeHeight) / 2.0)), heightMinAndMax, startBranchColor, endBranchColor) + "</color>\r");
                    styleBuffer.append("\t\t</LineStyle>\r");
                    styleBuffer.append("\t</Style>\r");
                }
            }
        }
    }
    treeSliceBuffer.append("\t</Folder>\r");
}
Also used : Node(jebl.evolution.graphs.Node)

Aggregations

Node (jebl.evolution.graphs.Node)23 Variate (dr.stats.Variate)3 RootedTree (jebl.evolution.trees.RootedTree)3 Regression (dr.stats.Regression)2 NumberFormatter (dr.util.NumberFormatter)2 Taxon (jebl.evolution.taxa.Taxon)2 Location (dr.app.phylogeography.tools.kml.Location)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 NewickImporter (jebl.evolution.io.NewickImporter)1 NexusImporter (jebl.evolution.io.NexusImporter)1 TreeImporter (jebl.evolution.io.TreeImporter)1 Element (org.jdom.Element)1