Search in sources :

Example 26 with FlexibleNode

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

the class CaseToCaseTreeLikelihood method explodeTree.

protected void explodeTree() {
    for (int i = 0; i < outbreak.size(); i++) {
        AbstractCase aCase = outbreak.getCase(i);
        if (aCase.wasEverInfected() && elementsAsTrees.get(aCase) == null) {
            NodeRef partitionRoot = ((PartitionedTreeModel) treeModel).getEarliestNodeInElement(aCase);
            double extraHeight;
            if (treeModel.isRoot(partitionRoot)) {
                extraHeight = maxFirstInfToRoot.getParameterValue(0) * aCase.getInfectionBranchPosition().getParameterValue(0);
            } else {
                extraHeight = treeModel.getBranchLength(partitionRoot) * aCase.getInfectionBranchPosition().getParameterValue(0);
            }
            FlexibleNode newRoot = new FlexibleNode();
            FlexibleTree littleTree = new FlexibleTree(newRoot);
            littleTree.beginTreeEdit();
            if (!treeModel.isExternal(partitionRoot)) {
                for (int j = 0; j < treeModel.getChildCount(partitionRoot); j++) {
                    copyElementToTreelet(littleTree, treeModel.getChild(partitionRoot, j), newRoot, aCase);
                }
            }
            littleTree.endTreeEdit();
            littleTree.resolveTree();
            Treelet treelet = new Treelet(littleTree, littleTree.getRootHeight() + extraHeight);
            elementsAsTrees.put(aCase, treelet);
        }
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 27 with FlexibleNode

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

the class BranchJumpPlotter method rewireTree.

private FlexibleTree rewireTree(Tree tree, boolean verbose) {
    int totalJumps = 0;
    FlexibleTree outTree = new FlexibleTree(tree, true);
    for (int nodeNo = 0; nodeNo < outTree.getNodeCount(); nodeNo++) {
        FlexibleNode node = (FlexibleNode) outTree.getNode(nodeNo);
        String finalHost = (String) node.getAttribute(traitName);
        node.setAttribute(traitName, finalHost.replaceAll("\"", ""));
        Object[] jumps = readCJH(node);
        if (verbose) {
            System.out.print("Node " + nodeNo + ": ");
        }
        if (jumps != null) {
            FlexibleNode needsNewParent = node;
            Double height = tree.getNodeHeight(node);
            for (int i = jumps.length - 1; i >= 0; i--) {
                totalJumps++;
                Object[] jump = (Object[]) jumps[i];
                if (i < jumps.length - 1 && (Double) jump[1] <= height) {
                    throw new RuntimeException("Jumps do not appear to be in descending order of height");
                }
                height = (Double) jump[1];
                if (!needsNewParent.getAttribute(traitName).equals(jump[3])) {
                    throw new RuntimeException("Destination traits do not match");
                }
                FlexibleNode parent = (FlexibleNode) outTree.getParent(needsNewParent);
                outTree.beginTreeEdit();
                outTree.removeChild(parent, needsNewParent);
                needsNewParent.setLength(height - needsNewParent.getHeight());
                FlexibleNode jumpNode = new FlexibleNode();
                jumpNode.setHeight(height);
                jumpNode.setLength(parent.getHeight() - height);
                jumpNode.setAttribute(traitName, jump[2]);
                outTree.addChild(parent, jumpNode);
                outTree.addChild(jumpNode, needsNewParent);
                outTree.endTreeEdit();
                needsNewParent = jumpNode;
            }
        }
        if (verbose) {
            if (jumps == null) {
                System.out.println(0 + " (" + totalJumps + ")");
            } else {
                System.out.println(jumps.length + " (" + totalJumps + ")");
            }
        }
    }
    outTree = new FlexibleTree((FlexibleNode) outTree.getRoot());
    if (verbose) {
        System.out.println("Total jumps: " + totalJumps);
        int[] childCounts = new int[3];
        for (int i = 0; i < outTree.getNodeCount(); i++) {
            childCounts[outTree.getChildCount(outTree.getNode(i))]++;
        }
        for (int i = 0; i < 3; i++) {
            System.out.println(childCounts[i] + " nodes have " + i + " children");
        }
    }
    return outTree;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode)

Example 28 with FlexibleNode

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

the class WithinCaseCoalescent method debugTreelet.

public void debugTreelet(Tree treelet, String fileName) {
    try {
        FlexibleTree treeCopy = new FlexibleTree(treelet);
        for (int j = 0; j < treeCopy.getNodeCount(); j++) {
            FlexibleNode node = (FlexibleNode) treeCopy.getNode(j);
            node.setAttribute("Number", node.getNumber());
        }
        NexusExporter testTreesOut = new NexusExporter(new PrintStream(fileName));
        testTreesOut.exportTree(treeCopy);
    } catch (IOException ignored) {
        System.out.println("IOException");
    }
}
Also used : NexusExporter(dr.app.tools.NexusExporter) PrintStream(java.io.PrintStream) FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) IOException(java.io.IOException)

Aggregations

FlexibleNode (dr.evolution.tree.FlexibleNode)28 FlexibleTree (dr.evolution.tree.FlexibleTree)12 EOFException (java.io.EOFException)6 IOException (java.io.IOException)5 Tree (dr.evolution.tree.Tree)4 Taxon (dr.evolution.util.Taxon)4 NexusExporter (dr.app.tools.NexusExporter)3 NodeRef (dr.evolution.tree.NodeRef)3 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DecimalFormat (java.text.DecimalFormat)1