Search in sources :

Example 1 with FlexibleTree

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

the class ARGTraceAnalysis method analyzeARG.

/**
	 * Actually analyzes a particular tree using the trace given the burnin
	 */
public final Tree analyzeARG(String target) {
    int n = getTreeCount();
    FlexibleTree meanTree = null;
    for (int i = 0; i < n; i++) {
        Tree tree = getARG(i);
        if (TreeUtils.uniqueNewick(tree, tree.getRoot()).equals(target)) {
            meanTree = new FlexibleTree(tree);
            break;
        }
    }
    if (meanTree == null)
        throw new RuntimeException("No target tree in trace");
    int m = meanTree.getInternalNodeCount();
    for (int j = 0; j < m; j++) {
        double[] heights = new double[n];
        NodeRef node1 = meanTree.getInternalNode(j);
        Set<String> leafSet = TreeUtils.getDescendantLeaves(meanTree, node1);
        for (int i = 0; i < n; i++) {
            Tree tree = getARG(i);
            NodeRef node2 = TreeUtils.getCommonAncestorNode(tree, leafSet);
            heights[i] = tree.getNodeHeight(node2);
        }
        meanTree.setNodeHeight(node1, dr.stats.DiscreteStatistics.mean(heights));
        meanTree.setNodeAttribute(node1, "upper", new Double(dr.stats.DiscreteStatistics.quantile(0.975, heights)));
        meanTree.setNodeAttribute(node1, "lower", new Double(dr.stats.DiscreteStatistics.quantile(0.025, heights)));
    }
    return meanTree;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree)

Example 2 with FlexibleTree

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

the class TempestFrame method readFromFile.

protected boolean readFromFile(File file) throws IOException {
    Reader reader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(reader);
    String line = bufferedReader.readLine();
    while (line != null && line.length() == 0) {
        line = bufferedReader.readLine();
    }
    boolean isNexus = (line != null && line.toUpperCase().contains("#NEXUS"));
    reader = new FileReader(file);
    Tree tree = null;
    try {
        if (isNexus) {
            NexusImporter importer = new NexusImporter(reader);
            tree = importer.importTree(taxa);
        } else {
            NewickImporter importer = new NewickImporter(reader);
            tree = importer.importTree(taxa);
        }
    } catch (Importer.ImportException ime) {
        JOptionPane.showMessageDialog(this, "Error parsing imported file: " + ime, "Error reading file", JOptionPane.ERROR_MESSAGE);
        ime.printStackTrace();
        return false;
    } catch (IOException ioex) {
        JOptionPane.showMessageDialog(this, "File I/O Error: " + ioex, "File I/O Error", JOptionPane.ERROR_MESSAGE);
        ioex.printStackTrace();
        return false;
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Fatal exception: " + ex, "Error reading file", JOptionPane.ERROR_MESSAGE);
        ex.printStackTrace();
        return false;
    }
    if (tree == null) {
        JOptionPane.showMessageDialog(this, "The file is not in a suitable format or contains no trees.", "Error reading file", JOptionPane.ERROR_MESSAGE);
        return false;
    }
    FlexibleTree binaryTree = new FlexibleTree(tree, true);
    binaryTree.resolveTree();
    trees.add(binaryTree);
    if (taxa == null) {
        taxa = binaryTree;
    }
    getExportTreeAction().setEnabled(true);
    getExportDataAction().setEnabled(true);
    return true;
}
Also used : FlexibleTree(dr.evolution.tree.FlexibleTree) FlexibleTree(dr.evolution.tree.FlexibleTree) Tree(dr.evolution.tree.Tree)

Example 3 with FlexibleTree

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

the class ExchangeOperatorTest method testWideExchangeOperator2.

public void testWideExchangeOperator2() throws IOException, ImportException {
    // probability of picking (A,B) node is 1/(2n-2) = 1/8
    // probability of swapping with D is 1/2
    // total = 1/16
    //probability of picking {D} node is 1/(2n-2) = 1/8
    //probability of picking {A,B} is 1/5
    // total = 1/40
    //total = 1/16 + 1/40 = 0.0625 + 0.025 = 0.0875
    // new test:
    // probability of picking (A,B) node is 1/(2n-2) = 1/8
    // probability of swapping with D is 1/(2n-3) = 1/7
    // total = 1/56
    //probability of picking {D} node is 1/(2n-2) = 1/8
    //probability of picking {A,B} is 1/(2n-3) = 1/7
    // total = 1/56
    //total = 1/56 + 1/56 = 1/28
    System.out.println("Test 1: Forward");
    String treeMatch = "(((D,C),(A,B)),E);";
    int count = 0;
    int reps = 1000000;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5);
        ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_1 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_1);
    System.out.println("Number of expected ratio:\t" + 1.0 / 28.0);
    assertExpectation(1.0 / 28.0, p_1, reps);
    // since this operator is supposed to be symmetric it got a hastings ratio of one
    // this means, it should propose the same move just backwards with the same probability
    // BUT:
    // (((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0) -> ((((A,B),C),D),E)
    // probability of picking (A,B) node is 1/(2n-2) = 1/8
    // probability of swapping with D is 1/3
    // total = 1/24
    //probability of picking {D} node is 1/(2n-2) = 1/8
    //probability of picking {A,B} is 1/4
    // total = 1/32
    //total = 1/24 + 1/32 = 7/96 = 0.07291666666
    // new test:
    // probability of picking (A,B) node is 1/(2n-2) = 1/8
    // probability of swapping with D is 1/(2n-3) = 1/7
    // total = 1/56
    //probability of picking {D} node is 1/(2n-2) = 1/8
    //probability of picking {A,B} is 1/(2n-3) = 1/7
    // total = 1/56
    //total = 1/56 + 1/56 = 1/28
    System.out.println("Test 2: Backward");
    treeMatch = "((((A,B),C),D),E);";
    NewickImporter importer = new NewickImporter("(((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0);");
    FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
    count = 0;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5_2);
        ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_2 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_2);
    System.out.println("Number of expected ratio:\t" + 1.0 / 28.0);
    assertExpectation(1.0 / 28.0, p_2, reps);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) FlexibleTree(dr.evolution.tree.FlexibleTree) NewickImporter(dr.evolution.io.NewickImporter) ExchangeOperator(dr.evomodel.operators.ExchangeOperator)

Example 4 with FlexibleTree

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

the class ImportancePruneAndRegraftTestProblem method testDoOperation.

/**
	 * Test method for {@link SimpleMCMCOperator#doOperation()}.
	 * @throws ImportException 
	 * @throws IOException 
	 */
public void testDoOperation() throws IOException, ImportException {
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/2
    // total = 1/14
    //probability of picking {D} node is 1/(2n-3) = 1/7
    //probability of picking {A,B} is 1/5
    // total = 1/35
    //total = 1/14 + 1/35 = 7/70 = 0.1
    System.out.println("Test 1: Forward");
    String treeMatch = "(((D,C),(A,B)),E);";
    int count = 0;
    int reps = 1000000;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5);
        ImportancePruneAndRegraft operator = new ImportancePruneAndRegraft(treeModel, 1.0, 0);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_1 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_1);
    System.out.println("Number of expected ratio:\t" + 0.1);
    assertExpectation(0.1, p_1, reps);
    // lets see what the backward probability is for the hastings ratio
    // (((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0) -> ((((A,B),C),D),E)
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/3
    // total = 1/21
    //probability of picking {D} node is 1/(2n-2) = 1/7
    //probability of picking {A,B} is 1/4
    // total = 1/28
    //total = 1/21 + 1/28 = 7/84 = 0.08333333
    System.out.println("Test 2: Backward");
    treeMatch = "((((A,B),C),D),E);";
    NewickImporter importer = new NewickImporter("(((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0);");
    FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
    count = 0;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5_2);
        ImportancePruneAndRegraft operator = new ImportancePruneAndRegraft(treeModel, 1.0, 1);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_2 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_2);
    System.out.println("Number of expected ratio:\t" + 0.0833333);
    assertExpectation(0.0833333, p_2, reps);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) FlexibleTree(dr.evolution.tree.FlexibleTree) NewickImporter(dr.evolution.io.NewickImporter) ImportancePruneAndRegraft(dr.evomodel.operators.ImportancePruneAndRegraft)

Example 5 with FlexibleTree

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

the class ImportanceSubtreeSwapTestProblem method testDoOperation.

/**
	 * Test method for {@link SimpleMCMCOperator#doOperation()}.
	 * @throws ImportException 
	 * @throws IOException 
	 */
public void testDoOperation() throws IOException, ImportException {
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/2
    // total = 1/14
    //probability of picking {D} node is 1/(2n-3) = 1/7
    //probability of picking {A,B} is 1/5
    // total = 1/35
    //total = 1/14 + 1/35 = 7/70 = 0.1
    System.out.println("Test 1: Forward");
    String treeMatch = "(((D,C),(A,B)),E);";
    int count = 0;
    int reps = 100000;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5);
        ImportanceSubtreeSwap operator = new ImportanceSubtreeSwap(treeModel, 1.0, 0);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_1 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_1);
    System.out.println("Number of expected ratio:\t" + 0.1);
    assertExpectation(0.1, p_1, reps);
    // lets see what the backward probability is for the hastings ratio
    // (((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0) -> ((((A,B),C),D),E)
    // probability of picking (A,B) node is 1/(2n-3) = 1/7
    // probability of swapping with D is 1/3
    // total = 1/21
    //probability of picking {D} node is 1/(2n-2) = 1/7
    //probability of picking {A,B} is 1/4
    // total = 1/28
    //total = 1/21 + 1/28 = 7/84 = 0.08333333
    System.out.println("Test 2: Backward");
    treeMatch = "((((A,B),C),D),E);";
    NewickImporter importer = new NewickImporter("(((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0);");
    FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
    count = 0;
    for (int i = 0; i < reps; i++) {
        TreeModel treeModel = new TreeModel("treeModel", tree5_2);
        ImportanceSubtreeSwap operator = new ImportanceSubtreeSwap(treeModel, 1.0, 0);
        operator.doOperation();
        String tree = TreeUtils.newickNoLengths(treeModel);
        if (tree.equals(treeMatch)) {
            count += 1;
        }
    }
    double p_2 = (double) count / (double) reps;
    System.out.println("Number of proposals:\t" + count);
    System.out.println("Number of tries:\t" + reps);
    System.out.println("Number of ratio:\t" + p_2);
    System.out.println("Number of expected ratio:\t" + 0.0833333);
    assertExpectation(0.0833333, p_2, reps);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) ImportanceSubtreeSwap(dr.evomodel.operators.ImportanceSubtreeSwap) FlexibleTree(dr.evolution.tree.FlexibleTree) NewickImporter(dr.evolution.io.NewickImporter)

Aggregations

FlexibleTree (dr.evolution.tree.FlexibleTree)27 FlexibleNode (dr.evolution.tree.FlexibleNode)12 Tree (dr.evolution.tree.Tree)8 NodeRef (dr.evolution.tree.NodeRef)7 EOFException (java.io.EOFException)6 NexusExporter (dr.app.tools.NexusExporter)5 NewickImporter (dr.evolution.io.NewickImporter)3 TreeModel (dr.evomodel.tree.TreeModel)3 IOException (java.io.IOException)3 PrintStream (java.io.PrintStream)3 ArrayList (java.util.ArrayList)3 Importer (dr.evolution.io.Importer)2 NexusImporter (dr.evolution.io.NexusImporter)2 Regression (dr.stats.Regression)2 HashMap (java.util.HashMap)2 TreeExporter (dr.evolution.io.TreeExporter)1 TreeImporter (dr.evolution.io.TreeImporter)1 MutableTree (dr.evolution.tree.MutableTree)1 ExchangeOperator (dr.evomodel.operators.ExchangeOperator)1 ImportancePruneAndRegraft (dr.evomodel.operators.ImportancePruneAndRegraft)1