Search in sources :

Example 21 with FlexibleNode

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

the class TiterImporter method importNextTree.

/**
     * import the next tree.
     * return the tree or null if no more trees are available
     */
public Tree importNextTree() throws IOException, ImportException {
    FlexibleTree tree = null;
    try {
        skipUntil("(");
        unreadCharacter('(');
        FlexibleNode root = readInternalNode(lastTree);
        tree = new FlexibleTree(root, false, true);
    } catch (EOFException e) {
    //
    }
    lastTree = tree;
    return tree;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) EOFException(java.io.EOFException)

Example 22 with FlexibleNode

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

the class TiterImporter method importTree.

/**
     * importTree.
     */
public Tree importTree(TaxonList taxonList) throws IOException, ImportException {
    setCommentDelimiters('[', ']', '\0', '\0', '&');
    try {
        skipUntil("(");
        unreadCharacter('(');
        final FlexibleNode root = readInternalNode(taxonList);
        if (getLastMetaComment() != null) {
            root.setAttribute(COMMENT, getLastMetaComment());
        }
        return new FlexibleTree(root, false, true);
    } catch (EOFException e) {
        throw new ImportException("incomplete tree");
    }
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) EOFException(java.io.EOFException)

Example 23 with FlexibleNode

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

the class CaseToCaseTreeLikelihood method copyElementToTreelet.

private void copyElementToTreelet(FlexibleTree littleTree, NodeRef oldNode, NodeRef newParent, AbstractCase element) {
    if (element.wasEverInfected()) {
        if (getBranchMap().get(oldNode.getNumber()) == element) {
            if (treeModel.isExternal(oldNode)) {
                NodeRef newTip = new FlexibleNode(new Taxon(treeModel.getNodeTaxon(oldNode).getId()));
                littleTree.addChild(newParent, newTip);
                littleTree.setBranchLength(newTip, treeModel.getBranchLength(oldNode));
            } else {
                NodeRef newChild = new FlexibleNode();
                littleTree.addChild(newParent, newChild);
                littleTree.setBranchLength(newChild, treeModel.getBranchLength(oldNode));
                for (int i = 0; i < treeModel.getChildCount(oldNode); i++) {
                    copyElementToTreelet(littleTree, treeModel.getChild(oldNode, i), newChild, element);
                }
            }
        } else {
            // we need a new tip
            NodeRef transmissionTip = new FlexibleNode(new Taxon("Transmission_" + getBranchMap().get(oldNode.getNumber()).getName()));
            double parentTime = getNodeTime(treeModel.getParent(oldNode));
            double childTime = getInfectionTime(getBranchMap().get(oldNode.getNumber()));
            littleTree.addChild(newParent, transmissionTip);
            littleTree.setBranchLength(transmissionTip, childTime - parentTime);
        }
    }
}
Also used : NodeRef(dr.evolution.tree.NodeRef) FlexibleNode(dr.evolution.tree.FlexibleNode) Taxon(dr.evolution.util.Taxon)

Example 24 with FlexibleNode

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

the class CaseToCaseTreeLikelihood method addTransmissionNodes.

public FlexibleTree addTransmissionNodes(Tree tree) {
    prepareTimings();
    FlexibleTree outTree = new FlexibleTree(tree, true);
    for (int j = 0; j < outTree.getNodeCount(); j++) {
        FlexibleNode node = (FlexibleNode) outTree.getNode(j);
        node.setAttribute("Number", node.getNumber());
        node.setAttribute("Time", heightToTime(node.getHeight()));
        node.setAttribute(PARTITIONS_KEY, getBranchMap().get(node.getNumber()));
    }
    for (AbstractCase aCase : outbreak.getCases()) {
        if (aCase.wasEverInfected()) {
            NodeRef originalNode = ((PartitionedTreeModel) treeModel).getEarliestNodeInElement(aCase);
            int infectionNodeNo = originalNode.getNumber();
            if (!treeModel.isRoot(originalNode)) {
                NodeRef originalParent = treeModel.getParent(originalNode);
                double nodeTime = getNodeTime(originalNode);
                double infectionTime = getInfectionTime(aCase);
                double heightToBreakBranch = getHeight(originalNode) + (nodeTime - infectionTime);
                FlexibleNode newNode = (FlexibleNode) outTree.getNode(infectionNodeNo);
                FlexibleNode oldParent = (FlexibleNode) outTree.getParent(newNode);
                outTree.beginTreeEdit();
                outTree.removeChild(oldParent, newNode);
                FlexibleNode infectionNode = new FlexibleNode();
                infectionNode.setHeight(heightToBreakBranch);
                infectionNode.setLength(oldParent.getHeight() - heightToBreakBranch);
                infectionNode.setAttribute(PARTITIONS_KEY, getNodePartition(treeModel, originalParent));
                infectionNode.setAttribute("Time", heightToTime(heightToBreakBranch));
                newNode.setLength(nodeTime - infectionTime);
                outTree.addChild(oldParent, infectionNode);
                outTree.addChild(infectionNode, newNode);
                outTree.endTreeEdit();
            } else {
                double nodeTime = getNodeTime(originalNode);
                double infectionTime = getInfectionTime(aCase);
                double heightToInstallRoot = getHeight(originalNode) + (nodeTime - infectionTime);
                FlexibleNode newNode = (FlexibleNode) outTree.getNode(infectionNodeNo);
                outTree.beginTreeEdit();
                FlexibleNode infectionNode = new FlexibleNode();
                infectionNode.setHeight(heightToInstallRoot);
                infectionNode.setAttribute("Time", heightToTime(heightToInstallRoot));
                infectionNode.setAttribute(PARTITIONS_KEY, "Origin");
                outTree.addChild(infectionNode, newNode);
                newNode.setLength(heightToInstallRoot - getHeight(originalNode));
                outTree.setRoot(infectionNode);
                outTree.endTreeEdit();
            }
        }
    }
    outTree = new FlexibleTree((FlexibleNode) outTree.getRoot());
    for (int i = 0; i < outTree.getNodeCount(); i++) {
        NodeRef node = outTree.getNode(i);
        NodeRef parent = outTree.getParent(node);
        if (parent != null && outTree.getNodeHeight(node) > outTree.getNodeHeight(parent)) {
            try {
                NexusExporter exporter = new NexusExporter(new PrintStream("fancyProblem.nex"));
                exporter.exportTree(outTree);
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                ((PartitionedTreeModel) treeModel).checkPartitions();
            } catch (BadPartitionException e) {
                System.out.print("Rewiring messed up because of partition problem.");
            }
            throw new RuntimeException("Rewiring messed up; investigate");
        }
    }
    return outTree;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) NexusExporter(dr.app.tools.NexusExporter) PrintStream(java.io.PrintStream) FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleNode(dr.evolution.tree.FlexibleNode) IOException(java.io.IOException)

Example 25 with FlexibleNode

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

the class CaseToCaseTreeLikelihood method outputTreeToFile.

public void outputTreeToFile(BranchMapModel map, String fileName, boolean includeTransmissionNodes) {
    try {
        FlexibleTree treeCopy;
        if (!includeTransmissionNodes) {
            treeCopy = new FlexibleTree(treeModel);
            for (int j = 0; j < treeCopy.getNodeCount(); j++) {
                FlexibleNode node = (FlexibleNode) treeCopy.getNode(j);
                node.setAttribute("Number", node.getNumber());
                node.setAttribute("Time", heightToTime(node.getHeight()));
                node.setAttribute(PARTITIONS_KEY, map.get(node.getNumber()));
            }
        } else {
            treeCopy = addTransmissionNodes(treeModel);
        }
        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