Search in sources :

Example 1 with UPGMATree

use of dr.evolution.tree.UPGMATree in project beast-mcmc by beast-dev.

the class OldTreesPanel method createTree.

private void createTree() {
    if (generateTreeDialog == null) {
        generateTreeDialog = new GenerateTreeDialog(frame);
    }
    int result = generateTreeDialog.showDialog(options);
    if (result != JOptionPane.CANCEL_OPTION) {
        GenerateTreeDialog.MethodTypes methodType = generateTreeDialog.getMethodType();
        PartitionData partition = generateTreeDialog.getDataPartition();
        Patterns patterns = new Patterns(partition.getAlignment());
        DistanceMatrix distances = new F84DistanceMatrix(patterns);
        Tree tree;
        TemporalRooting temporalRooting;
        switch(methodType) {
            case NJ:
                tree = new NeighborJoiningTree(distances);
                temporalRooting = new TemporalRooting(tree);
                tree = temporalRooting.findRoot(tree, TemporalRooting.RootingFunction.CORRELATION);
                break;
            case UPGMA:
                tree = new UPGMATree(distances);
                temporalRooting = new TemporalRooting(tree);
                break;
            default:
                throw new IllegalArgumentException("unknown method type");
        }
        tree.setId(generateTreeDialog.getName());
        options.userTrees.add(tree);
        treesTableModel.fireTableDataChanged();
        int row = options.userTrees.size() - 1;
        treesTable.getSelectionModel().setSelectionInterval(row, row);
    }
    fireTreePriorsChanged();
}
Also used : F84DistanceMatrix(dr.evolution.distance.F84DistanceMatrix) NeighborJoiningTree(dr.evolution.tree.NeighborJoiningTree) PartitionData(dr.app.beauti.options.PartitionData) UPGMATree(dr.evolution.tree.UPGMATree) NeighborJoiningTree(dr.evolution.tree.NeighborJoiningTree) Tree(dr.evolution.tree.Tree) TemporalRooting(dr.app.pathogen.TemporalRooting) F84DistanceMatrix(dr.evolution.distance.F84DistanceMatrix) DistanceMatrix(dr.evolution.distance.DistanceMatrix) UPGMATree(dr.evolution.tree.UPGMATree) Patterns(dr.evolution.alignment.Patterns)

Example 2 with UPGMATree

use of dr.evolution.tree.UPGMATree 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)

Aggregations

DistanceMatrix (dr.evolution.distance.DistanceMatrix)2 UPGMATree (dr.evolution.tree.UPGMATree)2 PartitionData (dr.app.beauti.options.PartitionData)1 TemporalRooting (dr.app.pathogen.TemporalRooting)1 Patterns (dr.evolution.alignment.Patterns)1 F84DistanceMatrix (dr.evolution.distance.F84DistanceMatrix)1 NeighborJoiningTree (dr.evolution.tree.NeighborJoiningTree)1 NodeRef (dr.evolution.tree.NodeRef)1 Tree (dr.evolution.tree.Tree)1 TimeScale (dr.evolution.util.TimeScale)1