Search in sources :

Example 26 with Node

use of beast.evolution.tree.Node in project beast2 by CompEvol.

the class CladeSystem method getLogCladeCredibility.

public double getLogCladeCredibility(Node node, BitSet bits) {
    double logCladeCredibility = 0.0;
    if (node.isLeaf()) {
        int index = getTaxonIndex(node);
        bits.set(2 * index);
    } else {
        BitSet bits2 = new BitSet();
        for (int i = 0; i < node.getChildCount(); i++) {
            Node node1 = node.getChild(i);
            logCladeCredibility += getLogCladeCredibility(node1, bits2);
        }
        for (int i = 1; i < bits2.length(); i = i + 2) {
            bits2.set(i, false);
        }
        if (node.isFake()) {
            int index = getTaxonIndex(node.getDirectAncestorChild());
            bits2.set(2 * index + 1);
        }
        logCladeCredibility += Math.log(getCladeCredibility(bits2));
        if (bits != null) {
            bits.or(bits2);
        }
    }
    return logCladeCredibility;
}
Also used : Node(beast.evolution.tree.Node) BitSet(java.util.BitSet)

Example 27 with Node

use of beast.evolution.tree.Node in project beast2 by CompEvol.

the class TreeSetParser method parseNewick.

public Node parseNewick(String str) {
    try {
        if (str == null || str.length() == 0) {
            return null;
        }
        m_chars = str.toCharArray();
        m_iTokenStart = str.indexOf('(');
        if (m_iTokenStart < 0) {
            return null;
        }
        m_iTokenEnd = m_iTokenStart;
        Vector<Node> stack = new Vector<>();
        Vector<Boolean> isFirstChild = new Vector<>();
        Vector<String> metaDataString = new Vector<>();
        stack.add(new Node());
        isFirstChild.add(true);
        stack.lastElement().setHeight(DEFAULT_LENGTH);
        metaDataString.add(null);
        boolean isLabel = true;
        while (m_iTokenEnd < m_chars.length) {
            switch(nextToken()) {
                case BRACE_OPEN:
                    {
                        Node node2 = new Node();
                        node2.setHeight(DEFAULT_LENGTH);
                        stack.add(node2);
                        isFirstChild.add(true);
                        metaDataString.add(null);
                        isLabel = true;
                    }
                    break;
                case BRACE_CLOSE:
                    {
                        if (isFirstChild.lastElement()) {
                            if (m_bAllowSingleChild) {
                                // process single child nodes
                                Node left = stack.lastElement();
                                stack.remove(stack.size() - 1);
                                isFirstChild.remove(isFirstChild.size() - 1);
                                Node dummyparent = new Node();
                                dummyparent.setHeight(DEFAULT_LENGTH);
                                dummyparent.setLeft(left);
                                left.setParent(dummyparent);
                                dummyparent.setRight(null);
                                Node parent = stack.lastElement();
                                parent.setLeft(left);
                                left.setParent(parent);
                                String metaData = metaDataString.remove(metaDataString.size() - 1);
                                left.metaDataString = metaData;
                                parseMetaData(left, metaData);
                                break;
                            } else {
                                // don't know how to process single child nodes
                                throw new IllegalArgumentException("Node with single child found.");
                            }
                        }
                        // process multi(i.e. more than 2)-child nodes by pairwise merging.
                        while (isFirstChild.elementAt(isFirstChild.size() - 2) == false) {
                            Node right = stack.lastElement();
                            stack.remove(stack.size() - 1);
                            isFirstChild.remove(isFirstChild.size() - 1);
                            Node left = stack.lastElement();
                            stack.remove(stack.size() - 1);
                            isFirstChild.remove(isFirstChild.size() - 1);
                            Node dummyparent = new Node();
                            dummyparent.setHeight(DEFAULT_LENGTH);
                            dummyparent.setLeft(left);
                            left.setParent(dummyparent);
                            dummyparent.setRight(right);
                            right.setParent(dummyparent);
                            stack.add(dummyparent);
                            isFirstChild.add(false);
                            String metaData = metaDataString.remove(metaDataString.size() - 1);
                            parseMetaData(left, metaData);
                        }
                        // last two nodes on stack merged into single parent node
                        Node right = stack.lastElement();
                        stack.remove(stack.size() - 1);
                        isFirstChild.remove(isFirstChild.size() - 1);
                        String metaData = metaDataString.remove(metaDataString.size() - 1);
                        parseMetaData(right, metaData);
                        Node left = stack.lastElement();
                        stack.remove(stack.size() - 1);
                        isFirstChild.remove(isFirstChild.size() - 1);
                        metaData = metaDataString.remove(metaDataString.size() - 1);
                        parseMetaData(left, metaData);
                        Node parent = stack.lastElement();
                        parent.setLeft(left);
                        left.setParent(parent);
                        parent.setRight(right);
                        right.setParent(parent);
                        metaData = metaDataString.lastElement();
                        parseMetaData(parent, metaData);
                    }
                    break;
                case COMMA:
                    {
                        Node node2 = new Node();
                        node2.setHeight(DEFAULT_LENGTH);
                        stack.add(node2);
                        isFirstChild.add(false);
                        metaDataString.add(null);
                        isLabel = true;
                    }
                    break;
                case COLON:
                    isLabel = false;
                    break;
                case TEXT:
                    if (isLabel) {
                        String label = str.substring(m_iTokenStart, m_iTokenEnd);
                        stack.lastElement().setNr(getLabelIndex(label));
                    } else {
                        String length = str.substring(m_iTokenStart, m_iTokenEnd);
                        stack.lastElement().setHeight(Float.parseFloat(length));
                    }
                    break;
                case META_DATA:
                    if (metaDataString.lastElement() == null) {
                        metaDataString.set(metaDataString.size() - 1, str.substring(m_iTokenStart + 1, m_iTokenEnd - 1));
                    } else {
                        metaDataString.set(metaDataString.size() - 1, metaDataString.lastElement() + ("," + str.substring(m_iTokenStart + 1, m_iTokenEnd - 1)));
                    }
                    break;
                case SEMI_COLON:
                    // System.err.println(stack.lastElement().toString());
                    parseMetaData(stack.lastElement(), metaDataString.lastElement());
                    return stack.lastElement();
                default:
                    throw new IllegalArgumentException("parseNewick: unknown token");
            }
        }
        return stack.lastElement();
    } catch (Exception e) {
        e.printStackTrace();
        throw new IllegalArgumentException(e.getMessage() + ": " + str.substring(Math.max(0, m_iTokenStart - 100), m_iTokenStart) + " >>>" + str.substring(m_iTokenStart, m_iTokenEnd) + " <<< ...");
    }
// return node;
}
Also used : Node(beast.evolution.tree.Node) Vector(java.util.Vector) IOException(java.io.IOException)

Example 28 with Node

use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.

the class TypeChangeCounts method update.

/**
 * Update type change count array as necessary.
 */
private void update() {
    if (!dirty)
        return;
    // Zero type change count array
    for (int i = 0; i < typeChanges.length; i++) typeChanges[i] = 0;
    // Recalculate array elements
    for (Node node : mtTree.getNodesAsArray()) {
        if (node.isRoot()) {
            continue;
        }
        MultiTypeNode mtNode = (MultiTypeNode) node;
        int lastType = mtNode.getNodeType();
        for (int i = 0; i < mtNode.getChangeCount(); i++) {
            int nextType = mtNode.getChangeType(i);
            typeChanges[getOffset(lastType, nextType)] += 1;
            lastType = nextType;
        }
    }
    if (useCache)
        dirty = false;
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) CalculationNode(beast.core.CalculationNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 29 with Node

use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.

the class TypedNodeTreeLogger method log.

@Override
public void log(long nSample, PrintStream out) {
    // Set up metadata string
    for (Node node : mtTree.getNodesAsArray()) {
        MultiTypeNode mtNode = (MultiTypeNode) node;
        mtNode.metaDataString = mtTree.getTypeLabel() + "=\"" + mtTree.getTypeSet().getTypeName(mtNode.getNodeType()) + "\"";
    }
    out.print("tree STATE_" + nSample + " = ");
    out.print(mtTree.getRoot().toSortedNewick(new int[1], true));
    out.print(";");
}
Also used : MultiTypeNode(beast.evolution.tree.MultiTypeNode) Node(beast.evolution.tree.Node) MultiTypeNode(beast.evolution.tree.MultiTypeNode)

Example 30 with Node

use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.

the class TypedSubtreeExchangeEasy method proposal.

@Override
public double proposal() {
    // Select source and destination nodes:
    Node srcNode, srcNodeParent, destNode, destNodeParent;
    if (isNarrowInput.get()) {
        // Narrow exchange selection:
        do {
            srcNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
        } while (srcNode.isRoot() || srcNode.getParent().isRoot());
        srcNodeParent = srcNode.getParent();
        destNode = getOtherChild(srcNodeParent.getParent(), srcNodeParent);
        destNodeParent = destNode.getParent();
    } else {
        // Wide exchange selection:
        do {
            srcNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
        } while (srcNode.isRoot());
        srcNodeParent = srcNode.getParent();
        do {
            destNode = mtTree.getNode(Randomizer.nextInt(mtTree.getNodeCount()));
        } while (destNode == srcNode || destNode.isRoot() || destNode.getParent() == srcNodeParent);
        destNodeParent = destNode.getParent();
        // as doing so can lead to infinite loops.)
        if (srcNodeParent == destNode || destNodeParent == srcNode)
            return Double.NEGATIVE_INFINITY;
    }
    // lengths:
    if (destNode.getHeight() > srcNodeParent.getHeight() || srcNode.getHeight() > destNodeParent.getHeight())
        return Double.NEGATIVE_INFINITY;
    // Make changes to tree topology:
    replace(srcNodeParent, srcNode, destNode);
    replace(destNodeParent, destNode, srcNode);
    // Force rejection if resulting multi-type tree invalid:
    if (!mtTree.isValid())
        return Double.NEGATIVE_INFINITY;
    return 0.0;
}
Also used : Node(beast.evolution.tree.Node)

Aggregations

Node (beast.evolution.tree.Node)140 Conversion (bacter.Conversion)24 MultiTypeNode (beast.evolution.tree.MultiTypeNode)22 Locus (bacter.Locus)17 ArrayList (java.util.ArrayList)15 Tree (beast.evolution.tree.Tree)14 Test (org.junit.Test)9 CalculationNode (beast.core.CalculationNode)8 RealParameter (beast.core.parameter.RealParameter)8 TreeInterface (beast.evolution.tree.TreeInterface)7 ClusterTree (beast.util.ClusterTree)7 ConversionGraph (bacter.ConversionGraph)6 Alignment (beast.evolution.alignment.Alignment)6 TaxonSet (beast.evolution.alignment.TaxonSet)6 SiteModel (beast.evolution.sitemodel.SiteModel)5 BitSet (java.util.BitSet)5 StateNode (beast.core.StateNode)4 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)4 TreeParser (beast.util.TreeParser)3 CFEventList (bacter.CFEventList)2