Search in sources :

Example 6 with TreeParser

use of beast.util.TreeParser in project beast2 by CompEvol.

the class BEASTTestCase method getTree.

public static Tree getTree(Alignment data, String tree) throws Exception {
    TreeParser t = new TreeParser();
    t.initByName("taxa", data, "newick", tree, "IsLabelledNewick", true);
    return t;
}
Also used : TreeParser(beast.util.TreeParser)

Example 7 with TreeParser

use of beast.util.TreeParser in project beast2 by CompEvol.

the class ExchangeOperatorTest method testNarrowExchange.

void testNarrowExchange(String sourceTree, String targetTree, int runs, Alignment data) throws Exception {
    // first test going from source to target
    double match = 0;
    for (int i = 0; i < runs; i++) {
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", sourceTree, "IsLabelledNewick", true);
        State state = new State();
        state.initByName("stateNode", tree);
        state.initialise();
        Exchange operator = new Exchange();
        operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
        double logHR = operator.proposal();
        String treeString = tree.getRoot().toNewick();
        if (treeString.equals(targetTree) && !Double.isInfinite(logHR)) {
            // proportion of accepts equals min(HR, 1.0)
            match += Math.min(Math.exp(logHR), 1.0);
        }
    }
    System.out.println(" Matches: " + match * 100.0 / runs + "%");
    // now test going from target to source
    double match2 = 0;
    for (int i = 0; i < runs; i++) {
        TreeParser tree = new TreeParser();
        tree.initByName("taxa", data, "newick", targetTree, "IsLabelledNewick", true);
        State state = new State();
        state.initByName("stateNode", tree);
        state.initialise();
        Exchange operator = new Exchange();
        operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
        double logHR = operator.proposal();
        String treeString = tree.getRoot().toNewick();
        if (treeString.equals(sourceTree) && !Double.isInfinite(logHR)) {
            // proportion of accepts equals min(HR, 1.0)
            match2 += Math.min(Math.exp(logHR), 1.0);
        }
    }
    System.out.println(" Matches: " + match2 * 100.0 / runs + "%");
    assertTrue("difference(" + 100 * (match - match2) / runs + ") exceeds 1.0%", 100.0 * Math.abs(match - match2) / runs < 1.0);
}
Also used : Exchange(beast.evolution.operators.Exchange) TreeParser(beast.util.TreeParser) State(beast.core.State)

Example 8 with TreeParser

use of beast.util.TreeParser in project beast2 by CompEvol.

the class ScaleOperatorTest method testTreeScaling.

@Test
public void testTreeScaling() {
    String newick = "((0:1.0,1:1.0)4:1.0,(2:1.0,3:1.0)5:0.5)6:0.0;";
    TreeParser tree = new TreeParser(newick, false, false, false, 0);
    Node[] node = tree.getNodesAsArray();
    ScaleOperator operator = new ScaleOperator();
    operator.initByName("tree", tree, "weight", 1.0);
    operator.proposal();
    // leaf node
    node = tree.getNodesAsArray();
    assertEquals(0.0, node[0].getHeight(), EPSILON);
    assertEquals(0.0, node[1].getHeight(), EPSILON);
    // leaf node, not scaled
    assertEquals(0.5, node[2].getHeight(), EPSILON);
    assertEquals(0.5, node[3].getHeight(), EPSILON);
    // internal nodes, all scaled
    // first determine scale factor
    double scale = node[4].getHeight() / 1.0;
    assertEquals(1.0 * scale, node[4].getHeight(), EPSILON);
    assertEquals(1.5 * scale, node[5].getHeight(), EPSILON);
    assertEquals(2.0 * scale, node[6].getHeight(), EPSILON);
}
Also used : TreeParser(beast.util.TreeParser) Node(beast.evolution.tree.Node) ScaleOperator(beast.evolution.operators.ScaleOperator) Test(org.junit.Test)

Example 9 with TreeParser

use of beast.util.TreeParser in project beast2 by CompEvol.

the class TreeAnnotatorTest method setUp.

@Before
public void setUp() throws Exception {
    final String[] treesString = new String[] { "((A:1,B:1):1,(C:1,D:1):1);", "(((A:1,B:1):1,C:2):2,D:3);", "((A:2,(B:1,C:1):1):2,D:3);" };
    final String[] treesSAString = new String[] { "((((0:0.5,1:1.0):1.0,2:2.0):1.0,3:0.0):2.0,4:4.0);", "((((0:1.0,2:1.5):1.0,1:2.5):0.5,3:0.0):2.0,4:4.0);", "(((0:0.5,2:1.0):1.0,1:2.0):3.0,(3:0.2,4:2.2):1.8);", "((((0:1.0,2:1.5):1.0,1:2.5):0.2,4:1.7):0.3,3:0.0):0.0;" };
    treeAnnotator = new TreeAnnotator();
    treeAnnotatorSA = new TreeAnnotator();
    trees = new Tree[treesString.length];
    for (int i = 0; i < trees.length; i++) {
        trees[i] = new TreeParser(treesString[i], false, false, true, 1);
    }
    treesSA = new Tree[treesSAString.length];
    for (int i = 0; i < treesSA.length; i++) {
        treesSA[i] = new TreeParser(treesSAString[i], false, false, false, 0);
    }
    cladeSystem = new CladeSystem();
    cladeSystemSA = new CladeSystem();
}
Also used : TreeParser(beast.util.TreeParser) CladeSystem(beast.app.treeannotator.CladeSystem) TreeAnnotator(beast.app.treeannotator.TreeAnnotator) Before(org.junit.Before)

Example 10 with TreeParser

use of beast.util.TreeParser in project beast2 by CompEvol.

the class TreeAnnotatorTest method testNewickTargetTree.

@Test
public void testNewickTargetTree() throws Exception {
    // create target tree file in temp folder
    TreeParser tree = new TreeParser("((A,B),(C,D))");
    String tmpFolder = org.fest.util.Files.temporaryFolder().toString();
    File target = new File(tmpFolder + "/target.tree");
    PrintStream outfile = new PrintStream(target);
    tree.init(outfile);
    outfile.println();
    tree.log(0, outfile);
    outfile.println();
    tree.close(outfile);
    outfile.close();
    // create input tree set
    File source = new File(tmpFolder + "/source.trees");
    outfile = new PrintStream(source);
    for (String treeString : new String[] { "((A:1,B:1):1,(C:1,D:1):1);", "(((A:1,B:1):1,C:2):2,D:3);", "((A:2,(B:1,C:1):1):2,D:3);" }) {
        outfile.println(treeString);
    }
    ;
    outfile.close();
    File summary = new File(tmpFolder + "/summary.tree");
    // run tree annotator
    String[] args = new String[] { "-target", target.getPath(), source.getPath(), summary.getPath() };
    TreeAnnotator.main(args);
    // make sure we get output
    String summaryString = BeautiDoc.load(summary);
    System.out.println(summaryString);
}
Also used : PrintStream(java.io.PrintStream) TreeParser(beast.util.TreeParser) File(java.io.File) Test(org.junit.Test)

Aggregations

TreeParser (beast.util.TreeParser)37 Test (org.junit.Test)23 Alignment (beast.evolution.alignment.Alignment)6 Sequence (beast.evolution.alignment.Sequence)3 Node (beast.evolution.tree.Node)3 Taxon (beast.evolution.alignment.Taxon)2 TaxonSet (beast.evolution.alignment.TaxonSet)2 Tree (beast.evolution.tree.Tree)2 MRCAPrior (beast.math.distributions.MRCAPrior)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 CladeSystem (beast.app.treeannotator.CladeSystem)1 TreeAnnotator (beast.app.treeannotator.TreeAnnotator)1 State (beast.core.State)1 StateNode (beast.core.StateNode)1 RealParameter (beast.core.parameter.RealParameter)1 BeagleTreeLikelihood (beast.evolution.likelihood.BeagleTreeLikelihood)1 Exchange (beast.evolution.operators.Exchange)1 ScaleOperator (beast.evolution.operators.ScaleOperator)1 SiteModel (beast.evolution.sitemodel.SiteModel)1