Search in sources :

Example 1 with Taxon

use of jebl.evolution.taxa.Taxon in project beast-mcmc by beast-dev.

the class TempestPanel method setupPanel.

public void setupPanel() {
    StringBuilder sb = new StringBuilder();
    NumberFormatter nf = new NumberFormatter(6);
    if (tree != null) {
        temporalRooting = new TemporalRooting(tree);
        currentTree = this.tree;
        if (bestFittingRoot && bestFittingRootTree != null) {
            currentTree = bestFittingRootTree;
            sb.append("Best-fitting root");
        } else {
            sb.append("User root");
        }
        if (temporalRooting.isContemporaneous()) {
            if (tabbedPane.getSelectedIndex() == 2) {
                tabbedPane.setSelectedIndex(1);
            }
            tabbedPane.setEnabledAt(2, false);
        } else {
            tabbedPane.setEnabledAt(2, true);
        }
        RootedTree jtree = dr.evolution.tree.TreeUtils.asJeblTree(currentTree);
        List<Color> colours = new ArrayList<Color>();
        for (Node tip : jtree.getExternalNodes()) {
            Taxon taxon = jtree.getTaxon(tip);
            colours.add((Color) taxon.getAttribute("!color"));
        }
        if (temporalRooting.isContemporaneous()) {
            double[] dv = temporalRooting.getRootToTipDistances(currentTree);
            List<Double> values = new ArrayList<Double>();
            for (double d : dv) {
                values.add(d);
            }
            rootToTipChart.removeAllPlots();
            NumericalDensityPlot dp = new NumericalDensityPlot(values, 20, null);
            dp.setLineColor(new Color(9, 70, 15));
            double yOffset = (Double) dp.getYData().getMax() / 2;
            List<Double> dummyValues = new ArrayList<Double>();
            for (int i = 0; i < values.size(); i++) {
                // add a random y offset to give some visual spread
                double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05);
                dummyValues.add(yOffset + y);
            }
            rootToTipPlot = new ScatterPlot(values, dummyValues);
            rootToTipPlot.setColours(colours);
            rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            rootToTipPlot.addListener(new Plot.Adaptor() {

                @Override
                public void markClicked(int index, double x, double y, boolean isShiftDown) {
                    rootToTipPlot.selectPoint(index, isShiftDown);
                }

                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            rootToTipChart.addPlot(rootToTipPlot);
            rootToTipChart.addPlot(dp);
            rootToTipPanel.setXAxisTitle("root-to-tip divergence");
            rootToTipPanel.setYAxisTitle("proportion");
            residualChart.removeAllPlots();
            sb.append(", contemporaneous tips");
            sb.append(", mean root-tip distance: " + nf.format(DiscreteStatistics.mean(dv)));
            sb.append(", coefficient of variation: " + nf.format(DiscreteStatistics.stdev(dv) / DiscreteStatistics.mean(dv)));
            sb.append(", stdev: " + nf.format(DiscreteStatistics.stdev(dv)));
            sb.append(", variance: " + nf.format(DiscreteStatistics.variance(dv)));
            showMRCACheck.setVisible(false);
        } else {
            Regression r = temporalRooting.getRootToTipRegression(currentTree);
            double[] residuals = temporalRooting.getRootToTipResiduals(currentTree, r);
            pointMap.clear();
            for (int i = 0; i < currentTree.getExternalNodeCount(); i++) {
                NodeRef tip = currentTree.getExternalNode(i);
                Node node = jtree.getNode(Taxon.getTaxon(currentTree.getNodeTaxon(tip).getId()));
                node.setAttribute("residual", residuals[i]);
                pointMap.put(node, i);
            }
            rootToTipChart.removeAllPlots();
            if (showMRCACheck.isSelected()) {
                double[] dv = temporalRooting.getParentRootToTipDistances(currentTree);
                List<Double> parentDistances = new ArrayList<Double>();
                for (int i = 0; i < dv.length; i++) {
                    parentDistances.add(i, dv[i]);
                }
                List<Double> parentTimes = new ArrayList<Double>();
                for (int i = 0; i < parentDistances.size(); i++) {
                    parentTimes.add(i, r.getX(parentDistances.get(i)));
                }
                mrcaPlot = new ParentPlot(r.getXData(), r.getYData(), parentTimes, parentDistances);
                mrcaPlot.setLineColor(new Color(105, 202, 105));
                mrcaPlot.setLineStroke(new BasicStroke(0.5F));
                rootToTipChart.addPlot(mrcaPlot);
            }
            if (true) {
                double[] datePrecisions = temporalRooting.getTipDatePrecisions(currentTree);
                Variate.D ed = new Variate.D();
                for (int i = 0; i < datePrecisions.length; i++) {
                    ed.add(datePrecisions[i]);
                }
                errorBarPlot = new ErrorBarPlot(ErrorBarPlot.Orientation.HORIZONTAL, r.getXData(), r.getYData(), ed);
                errorBarPlot.setLineColor(new Color(44, 44, 44));
                errorBarPlot.setLineStroke(new BasicStroke(1.0F));
                rootToTipChart.addPlot(errorBarPlot);
            }
            rootToTipPlot = new ScatterPlot(r.getXData(), r.getYData());
            rootToTipPlot.addListener(new Plot.Adaptor() {

                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            rootToTipPlot.setColours(colours);
            rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            rootToTipChart.addPlot(rootToTipPlot);
            rootToTipChart.addPlot(new RegressionPlot(r));
            rootToTipChart.getXAxis().addRange(r.getXIntercept(), (Double) r.getXData().getMax());
            rootToTipPanel.setXAxisTitle("time");
            rootToTipPanel.setYAxisTitle("root-to-tip divergence");
            residualChart.removeAllPlots();
            Variate.D values = (Variate.D) r.getYResidualData();
            NumericalDensityPlot dp = new NumericalDensityPlot(values, 20);
            dp.setLineColor(new Color(103, 128, 144));
            double yOffset = (Double) dp.getYData().getMax() / 2;
            Double[] dummyValues = new Double[values.getCount()];
            for (int i = 0; i < dummyValues.length; i++) {
                // add a random y offset to give some visual spread
                double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05);
                dummyValues[i] = yOffset + y;
            }
            Variate.D yOffsetValues = new Variate.D(dummyValues);
            residualPlot = new ScatterPlot(values, yOffsetValues);
            residualPlot.addListener(new Plot.Adaptor() {

                @Override
                public void markClicked(int index, double x, double y, boolean isShiftDown) {
                    rootToTipPlot.selectPoint(index, isShiftDown);
                }

                @Override
                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            residualPlot.setColours(colours);
            residualPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            residualPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            residualChart.addPlot(residualPlot);
            residualChart.addPlot(dp);
            residualPanel.setXAxisTitle("residual");
            residualPanel.setYAxisTitle("proportion");
            if (SHOW_NODE_DENSITY) {
                Regression r2 = temporalRooting.getNodeDensityRegression(currentTree);
                nodeDensityChart.removeAllPlots();
                nodeDensityPlot = new ScatterPlot(r2.getXData(), r2.getYData());
                nodeDensityPlot.addListener(new Plot.Adaptor() {

                    public void selectionChanged(final Set<Integer> selectedPoints) {
                        plotSelectionChanged(selectedPoints);
                    }
                });
                nodeDensityPlot.setColours(colours);
                nodeDensityPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
                nodeDensityPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
                nodeDensityChart.addPlot(nodeDensityPlot);
                nodeDensityChart.addPlot(new RegressionPlot(r2));
                nodeDensityChart.getXAxis().addRange(r2.getXIntercept(), (Double) r2.getXData().getMax());
                nodeDensityPanel.setXAxisTitle("time");
                nodeDensityPanel.setYAxisTitle("node density");
            }
            sb.append(", dated tips");
            sb.append(", date range: " + nf.format(temporalRooting.getDateRange()));
            sb.append(", slope (rate): " + nf.format(r.getGradient()));
            sb.append(", x-intercept (TMRCA): " + nf.format(r.getXIntercept()));
            sb.append(", corr. coeff: " + nf.format(r.getCorrelationCoefficient()));
            sb.append(", R^2: " + nf.format(r.getRSquared()));
            showMRCACheck.setVisible(true);
        }
        treePanel.setTree(jtree);
        treePanel.setColourBy("residual");
    } else {
        treePanel.setTree(null);
        rootToTipChart.removeAllPlots();
        sb.append("No trees loaded");
    }
    textArea.setText(sb.toString());
    statisticsModel.fireTableStructureChanged();
    repaint();
}
Also used : Variate(dr.stats.Variate) Node(jebl.evolution.graphs.Node) Taxon(jebl.evolution.taxa.Taxon) Regression(dr.stats.Regression) RootedTree(jebl.evolution.trees.RootedTree) NumberFormatter(dr.util.NumberFormatter)

Example 2 with Taxon

use of jebl.evolution.taxa.Taxon in project beast-mcmc by beast-dev.

the class CoalGenApp method setSamplingTimes.

private static void setSamplingTimes(final RootedTree tree) {
    setSamplingTimes(tree, tree.getRootNode(), 0.0);
    // the dates are distances from root so make them heights
    double maxLength = 0.0;
    for (Taxon taxon : tree.getTaxa()) {
        double length = (Double) taxon.getAttribute("length");
        if (length > maxLength) {
            maxLength = length;
        }
    }
    for (Taxon taxon : tree.getTaxa()) {
        double length = (Double) taxon.getAttribute("length");
        taxon.setAttribute("date", maxLength - length);
    }
}
Also used : Taxon(jebl.evolution.taxa.Taxon)

Example 3 with Taxon

use of jebl.evolution.taxa.Taxon in project beast-mcmc by beast-dev.

the class PathogenPanel method setupPanel.

public void setupPanel() {
    StringBuilder sb = new StringBuilder();
    NumberFormatter nf = new NumberFormatter(6);
    if (tree != null) {
        temporalRooting = new TemporalRooting(tree);
        currentTree = this.tree;
        if (bestFittingRoot && bestFittingRootTree != null) {
            currentTree = bestFittingRootTree;
            sb.append("Best-fitting root");
        } else {
            sb.append("User root");
        }
        if (temporalRooting.isContemporaneous()) {
            if (tabbedPane.getSelectedIndex() == 2) {
                tabbedPane.setSelectedIndex(1);
            }
            tabbedPane.setEnabledAt(2, false);
        } else {
            tabbedPane.setEnabledAt(2, true);
        }
        RootedTree jtree = dr.evolution.tree.TreeUtils.asJeblTree(currentTree);
        List<Color> colours = new ArrayList<Color>();
        for (Node tip : jtree.getExternalNodes()) {
            Taxon taxon = jtree.getTaxon(tip);
            colours.add((Color) taxon.getAttribute("!color"));
        }
        if (temporalRooting.isContemporaneous()) {
            double[] dv = temporalRooting.getRootToTipDistances(currentTree);
            List<Double> values = new ArrayList<Double>();
            for (double d : dv) {
                values.add(d);
            }
            rootToTipChart.removeAllPlots();
            NumericalDensityPlot dp = new NumericalDensityPlot(values, 20, null);
            dp.setLineColor(new Color(9, 70, 15));
            double yOffset = (Double) dp.getYData().getMax() / 2;
            List<Double> dummyValues = new ArrayList<Double>();
            for (int i = 0; i < values.size(); i++) {
                // add a random y offset to give some visual spread
                double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05);
                dummyValues.add(yOffset + y);
            }
            rootToTipPlot = new ScatterPlot(values, dummyValues);
            rootToTipPlot.setColours(colours);
            rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            rootToTipPlot.addListener(new Plot.Adaptor() {

                @Override
                public void markClicked(int index, double x, double y, boolean isShiftDown) {
                    rootToTipPlot.selectPoint(index, isShiftDown);
                }

                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            rootToTipChart.addPlot(rootToTipPlot);
            rootToTipChart.addPlot(dp);
            rootToTipPanel.setXAxisTitle("root-to-tip divergence");
            rootToTipPanel.setYAxisTitle("proportion");
            residualChart.removeAllPlots();
            sb.append(", contemporaneous tips");
            sb.append(", mean root-tip distance: " + nf.format(DiscreteStatistics.mean(dv)));
            sb.append(", coefficient of variation: " + nf.format(DiscreteStatistics.stdev(dv) / DiscreteStatistics.mean(dv)));
            sb.append(", stdev: " + nf.format(DiscreteStatistics.stdev(dv)));
            sb.append(", variance: " + nf.format(DiscreteStatistics.variance(dv)));
            showMRCACheck.setVisible(false);
        } else {
            Regression r = temporalRooting.getRootToTipRegression(currentTree);
            double[] residuals = temporalRooting.getRootToTipResiduals(currentTree, r);
            pointMap.clear();
            for (int i = 0; i < currentTree.getExternalNodeCount(); i++) {
                NodeRef tip = currentTree.getExternalNode(i);
                Node node = jtree.getNode(Taxon.getTaxon(currentTree.getNodeTaxon(tip).getId()));
                node.setAttribute("residual", residuals[i]);
                pointMap.put(node, i);
            }
            rootToTipChart.removeAllPlots();
            if (showMRCACheck.isSelected()) {
                double[] dv = temporalRooting.getParentRootToTipDistances(currentTree);
                List<Double> parentDistances = new ArrayList<Double>();
                for (int i = 0; i < dv.length; i++) {
                    parentDistances.add(i, dv[i]);
                }
                List<Double> parentTimes = new ArrayList<Double>();
                for (int i = 0; i < parentDistances.size(); i++) {
                    parentTimes.add(i, r.getX(parentDistances.get(i)));
                }
                mrcaPlot = new ParentPlot(r.getXData(), r.getYData(), parentTimes, parentDistances);
                mrcaPlot.setLineColor(new Color(105, 202, 105));
                mrcaPlot.setLineStroke(new BasicStroke(0.5F));
                rootToTipChart.addPlot(mrcaPlot);
            }
            if (true) {
                double[] datePrecisions = temporalRooting.getTipDatePrecisions(currentTree);
                Variate.D ed = new Variate.D();
                for (int i = 0; i < datePrecisions.length; i++) {
                    ed.add(datePrecisions[i]);
                }
                errorBarPlot = new ErrorBarPlot(ErrorBarPlot.Orientation.HORIZONTAL, r.getXData(), r.getYData(), ed);
                errorBarPlot.setLineColor(new Color(44, 44, 44));
                errorBarPlot.setLineStroke(new BasicStroke(1.0F));
                rootToTipChart.addPlot(errorBarPlot);
            }
            rootToTipPlot = new ScatterPlot(r.getXData(), r.getYData());
            rootToTipPlot.addListener(new Plot.Adaptor() {

                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            rootToTipPlot.setColours(colours);
            rootToTipPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            rootToTipPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            rootToTipChart.addPlot(rootToTipPlot);
            rootToTipChart.addPlot(new RegressionPlot(r));
            rootToTipChart.getXAxis().addRange(r.getXIntercept(), (Double) r.getXData().getMax());
            rootToTipPanel.setXAxisTitle("time");
            rootToTipPanel.setYAxisTitle("root-to-tip divergence");
            residualChart.removeAllPlots();
            Variate.D values = (Variate.D) r.getYResidualData();
            NumericalDensityPlot dp = new NumericalDensityPlot(values, 20);
            dp.setLineColor(new Color(103, 128, 144));
            double yOffset = (Double) dp.getYData().getMax() / 2;
            Double[] dummyValues = new Double[values.getCount()];
            for (int i = 0; i < dummyValues.length; i++) {
                // add a random y offset to give some visual spread
                double y = MathUtils.nextGaussian() * ((Double) dp.getYData().getMax() * 0.05);
                dummyValues[i] = yOffset + y;
            }
            Variate.D yOffsetValues = new Variate.D(dummyValues);
            residualPlot = new ScatterPlot(values, yOffsetValues);
            residualPlot.addListener(new Plot.Adaptor() {

                @Override
                public void markClicked(int index, double x, double y, boolean isShiftDown) {
                    rootToTipPlot.selectPoint(index, isShiftDown);
                }

                @Override
                public void selectionChanged(final Set<Integer> selectedPoints) {
                    plotSelectionChanged(selectedPoints);
                }
            });
            residualPlot.setColours(colours);
            residualPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
            residualPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
            residualChart.addPlot(residualPlot);
            residualChart.addPlot(dp);
            residualPanel.setXAxisTitle("residual");
            residualPanel.setYAxisTitle("proportion");
            if (SHOW_NODE_DENSITY) {
                Regression r2 = temporalRooting.getNodeDensityRegression(currentTree);
                nodeDensityChart.removeAllPlots();
                nodeDensityPlot = new ScatterPlot(r2.getXData(), r2.getYData());
                nodeDensityPlot.addListener(new Plot.Adaptor() {

                    public void selectionChanged(final Set<Integer> selectedPoints) {
                        plotSelectionChanged(selectedPoints);
                    }
                });
                nodeDensityPlot.setColours(colours);
                nodeDensityPlot.setMarkStyle(Plot.CIRCLE_MARK, 8, new BasicStroke(0.0F), new Color(44, 44, 44), new Color(129, 149, 149));
                nodeDensityPlot.setHilightedMarkStyle(new BasicStroke(0.5F), new Color(44, 44, 44), UIManager.getColor("List.selectionBackground"));
                nodeDensityChart.addPlot(nodeDensityPlot);
                nodeDensityChart.addPlot(new RegressionPlot(r2));
                nodeDensityChart.getXAxis().addRange(r2.getXIntercept(), (Double) r2.getXData().getMax());
                nodeDensityPanel.setXAxisTitle("time");
                nodeDensityPanel.setYAxisTitle("node density");
            }
            sb.append(", dated tips");
            sb.append(", date range: " + nf.format(temporalRooting.getDateRange()));
            sb.append(", slope (rate): " + nf.format(r.getGradient()));
            sb.append(", x-intercept (TMRCA): " + nf.format(r.getXIntercept()));
            sb.append(", corr. coeff: " + nf.format(r.getCorrelationCoefficient()));
            sb.append(", R^2: " + nf.format(r.getRSquared()));
            showMRCACheck.setVisible(true);
        }
        treePanel.setTree(jtree);
        treePanel.setColourBy("residual");
    } else {
        treePanel.setTree(null);
        rootToTipChart.removeAllPlots();
        sb.append("No trees loaded");
    }
    textArea.setText(sb.toString());
    statisticsModel.fireTableStructureChanged();
    repaint();
}
Also used : Variate(dr.stats.Variate) Node(jebl.evolution.graphs.Node) Taxon(jebl.evolution.taxa.Taxon) Regression(dr.stats.Regression) RootedTree(jebl.evolution.trees.RootedTree) NumberFormatter(dr.util.NumberFormatter)

Aggregations

Taxon (jebl.evolution.taxa.Taxon)3 Regression (dr.stats.Regression)2 Variate (dr.stats.Variate)2 NumberFormatter (dr.util.NumberFormatter)2 Node (jebl.evolution.graphs.Node)2 RootedTree (jebl.evolution.trees.RootedTree)2