Search in sources :

Example 1 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class SamplesPanel method calculateHeights.

private void calculateHeights() {
    maximumTipHeight = 0.0;
    if (taxonList == null || taxonList.getTaxonCount() == 0)
        return;
    heights = null;
    dr.evolution.util.Date mostRecent = null;
    for (int i = 0; i < taxonList.getTaxonCount(); i++) {
        Date date = taxonList.getTaxon(i).getDate();
        if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
            mostRecent = date;
        }
    }
    if (mostRecent != null) {
        heights = new double[taxonList.getTaxonCount()];
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
        for (int i = 0; i < taxonList.getTaxonCount(); i++) {
            Date date = taxonList.getTaxon(i).getDate();
            if (date != null) {
                heights[i] = timeScale.convertTime(date.getTimeValue(), date) - time0;
                if (heights[i] > maximumTipHeight)
                    maximumTipHeight = heights[i];
            }
        }
    }
}
Also used : Date(dr.evolution.util.Date) TimeScale(dr.evolution.util.TimeScale) Date(dr.evolution.util.Date)

Example 2 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class TreeUtils method setHeightsFromDates.

/**
     * Sets the tip heights from the tip dates
     */
public static void setHeightsFromDates(MutableTree tree) {
    Date mostRecent = null;
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        Taxon taxon = tree.getNodeTaxon(tree.getExternalNode(i));
        Date date = (Date) taxon.getAttribute("date");
        if (date != null) {
            if ((mostRecent == null) || date.after(mostRecent)) {
                mostRecent = date;
            }
        }
    }
    TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
    for (int i = 0; i < tree.getExternalNodeCount(); i++) {
        NodeRef node = tree.getExternalNode(i);
        Taxon taxon = tree.getNodeTaxon(node);
        Date date = (Date) taxon.getAttribute("date");
        if (date != null) {
            double height = timeScale.convertTime(date.getTimeValue(), date);
            tree.setNodeHeight(node, height);
        } else {
            tree.setNodeHeight(node, 0.0);
        }
    }
    adjustInternalHeights(tree, tree.getRoot());
    if (mostRecent != null) {
        tree.setUnits(mostRecent.getUnits());
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) Taxon(dr.evolution.util.Taxon) TimeScale(dr.evolution.util.TimeScale) Date(dr.evolution.util.Date)

Example 3 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class SamplesPanel method calculateHeights.

private void calculateHeights() {
    maximumTipHeight = 0.0;
    if (taxonList == null || taxonList.getTaxonCount() == 0)
        return;
    heights = null;
    Date mostRecent = null;
    for (int i = 0; i < taxonList.getTaxonCount(); i++) {
        Date date = taxonList.getTaxon(i).getDate();
        if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
            mostRecent = date;
        }
    }
    if (mostRecent != null) {
        heights = new double[taxonList.getTaxonCount()];
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        double time0 = timeScale.convertTime(mostRecent.getTimeValue(), mostRecent);
        for (int i = 0; i < taxonList.getTaxonCount(); i++) {
            Date date = taxonList.getTaxon(i).getDate();
            if (date != null) {
                heights[i] = timeScale.convertTime(date.getTimeValue(), date) - time0;
                if (heights[i] > maximumTipHeight)
                    maximumTipHeight = heights[i];
            }
        }
    }
}
Also used : TimeScale(dr.evolution.util.TimeScale) Date(dr.evolution.util.Date)

Example 4 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class UPGMATreeParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    boolean usingDatesSpecified = false;
    boolean usingDates = true;
    double rootHeight = xo.getAttribute(ROOT_HEIGHT, -1.0);
    if (xo.hasAttribute(SimpleTreeParser.USING_DATES)) {
        usingDatesSpecified = true;
        usingDates = xo.getBooleanAttribute(SimpleTreeParser.USING_DATES);
    }
    DistanceMatrix distances = (DistanceMatrix) xo.getChild(DistanceMatrix.class);
    UPGMATree tree = new UPGMATree(distances);
    if (rootHeight > 0) {
        double scaleFactor = rootHeight / tree.getNodeHeight(tree.getRoot());
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            NodeRef node = tree.getInternalNode(i);
            double height = tree.getNodeHeight(node);
            tree.setNodeHeight(node, height * scaleFactor);
        }
    }
    if (usingDates) {
        dr.evolution.util.Date mostRecent = null;
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, dr.evolution.util.Date.DATE);
            if (date == null) {
                date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), dr.evolution.util.Date.DATE);
            }
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), dr.evolution.util.Date.DATE);
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        if (mostRecent == null) {
            if (usingDatesSpecified) {
                throw new XMLParseException("no date elements in tree (and usingDates attribute set)");
            }
        } else {
            TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
            for (int i = 0; i < tree.getTaxonCount(); i++) {
                dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, dr.evolution.util.Date.DATE);
                if (date == null) {
                    date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), dr.evolution.util.Date.DATE);
                }
                if (date != null) {
                    double height = timeScale.convertTime(date.getTimeValue(), date);
                    tree.setNodeHeight(tree.getExternalNode(i), height);
                }
            }
            for (int i = 0; i < tree.getInternalNodeCount(); i++) {
                dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), dr.evolution.util.Date.DATE);
                if (date != null) {
                    double height = timeScale.convertTime(date.getTimeValue(), date);
                    tree.setNodeHeight(tree.getInternalNode(i), height);
                }
            }
            MutableTree.Utils.correctHeightsForTips(tree);
        }
    }
    if (rootHeight > 0) {
        double scaleFactor = rootHeight / tree.getNodeHeight(tree.getRoot());
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            NodeRef node = tree.getInternalNode(i);
            double height = tree.getNodeHeight(node);
            tree.setNodeHeight(node, height * scaleFactor);
        }
    }
    if (xo.getAttribute(RANDOMIZE, false)) {
        shakeTree(tree);
    }
    return tree;
}
Also used : UPGMATree(dr.evolution.tree.UPGMATree) TimeScale(dr.evolution.util.TimeScale) NodeRef(dr.evolution.tree.NodeRef) DistanceMatrix(dr.evolution.distance.DistanceMatrix)

Example 5 with TimeScale

use of dr.evolution.util.TimeScale in project beast-mcmc by beast-dev.

the class SimpleTreeParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Units.Type units = XMLUnits.Utils.getUnitsAttr(xo);
    boolean usingDates = xo.getAttribute(USING_DATES, false);
    SimpleNode root = (SimpleNode) xo.getChild(SimpleNode.class);
    if (root == null) {
        throw new XMLParseException("node element missing from tree");
    }
    SimpleTree tree = new SimpleTree(root);
    if (usingDates) {
        dr.evolution.util.Date mostRecent = null;
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
            if (date == null) {
                date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
            }
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
            if (date != null && ((mostRecent == null) || date.after(mostRecent))) {
                mostRecent = date;
            }
        }
        if (mostRecent == null) {
            throw new XMLParseException("no date elements in tree (and usingDates attribute set)");
        }
        TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getTaxonAttribute(i, DATE);
            if (date == null) {
                date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getExternalNode(i), DATE);
            }
            if (date != null) {
                double height = timeScale.convertTime(date.getTimeValue(), date);
                tree.setNodeHeight(tree.getExternalNode(i), height);
            }
        }
        for (int i = 0; i < tree.getInternalNodeCount(); i++) {
            dr.evolution.util.Date date = (dr.evolution.util.Date) tree.getNodeAttribute(tree.getInternalNode(i), DATE);
            if (date != null) {
                double height = timeScale.convertTime(date.getTimeValue(), date);
                tree.setNodeHeight(tree.getInternalNode(i), height);
            }
        }
        MutableTree.Utils.correctHeightsForTips(tree);
    }
    tree.setUnits(units);
    return tree;
}
Also used : SimpleTree(dr.evolution.tree.SimpleTree) TimeScale(dr.evolution.util.TimeScale) Units(dr.evolution.util.Units) XMLUnits(dr.evoxml.util.XMLUnits) SimpleNode(dr.evolution.tree.SimpleNode)

Aggregations

TimeScale (dr.evolution.util.TimeScale)8 Date (dr.evolution.util.Date)5 NodeRef (dr.evolution.tree.NodeRef)4 Taxon (dr.evolution.util.Taxon)3 SimpleNode (dr.evolution.tree.SimpleNode)2 SimpleTree (dr.evolution.tree.SimpleTree)2 DistanceMatrix (dr.evolution.distance.DistanceMatrix)1 UPGMATree (dr.evolution.tree.UPGMATree)1 Units (dr.evolution.util.Units)1 XMLUnits (dr.evoxml.util.XMLUnits)1