Search in sources :

Example 21 with TreeParser

use of beast.util.TreeParser in project bacter by tgvaughan.

the class MarginalTreeTest method testNonOverlapping.

@Test
public void testNonOverlapping() throws Exception {
    // Conversion graph
    String str = "[&locus,2,2759,0.3260126313706676,10,2808,0.42839862922656696] " + "[&locus,10,6692,0.3381366423491633,2,6693,0.5683827224649434] " + "[&locus,10,8069,0.2807615297583804,14,8160,0.3415740002783274] " + "[&locus,2,9000,0.1,2,9500,0.4] " + "[&locus,2,9530,0.2,18,9600,2.0] " + "(((0:0.04916909893812008,1:0.04916909893812008)10:0.5465237639426681," + "(4:0.3773111326866937,(((8:0.22180790639747835," + "(3:0.07561592852503513,6:0.07561592852503513)11:0.14619197787244323)" + "13:0.010206467073885589,9:0.23201437347136394)14:0.116542689187905," + "(7:0.10746702934931932,5:0.10746702934931932)12:0.24109003330994963)" + "15:0.02875407002742475)16:0.21838173019409446)17:1.1073878800617445," + "2:1.7030807429425328)18:0.0";
    Locus locus = new Locus("locus", 10000);
    locus.setID("locus");
    ConversionGraph acg = new ConversionGraph();
    acg.initByName("fromString", str, "locus", locus);
    // System.out.println(acg.getExtendedNewick(true));
    // Test all marginals against truth
    // (I have eyeballed each of these trees and claim that they are correct.)
    String[] correctNewickStrings = { "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "((2:0.42839862922656696,(0:0.04916909893812016,1:0.04916909893812016)10:0.3792295302884468)18:0.1672942336542213,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "((((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:1.325769610255839,((0:0.04916909893812016,1:0.04916909893812016)10:0.5192136235268232,2:0.5683827224649434)17:1.1346980204775894)18:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "(((((0:0.04916909893812016,1:0.04916909893812016)10:0.29240490134020725,(((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.1095596268069634)17:0.006983062380941707,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:1.325769610255839,2:1.7030807429425328)18:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.4043071371192117,2:2.0)18:0.0;", "(((0:0.04916909893812016,1:0.04916909893812016)10:0.5465237639426681,(((((3:0.07561592852503529,6:0.07561592852503529)11:0.1461919778724432,8:0.2218079063974785)13:0.010206467073885506,9:0.232014373471364)14:0.11654268918790511,(5:0.1074670293493194,7:0.1074670293493194)12:0.24109003330994971)15:0.028754070027424694,4:0.3773111326866938)16:0.21838173019409446)17:1.1073878800617445,2:1.7030807429425328)18:0.0" };
    for (int r = 0; r < acg.getRegionCount(locus); r++) {
        MarginalTree marginalTree = new MarginalTree(acg, acg.getRegions(locus).get(r));
        // System.out.println(marginalTree + ";");
        assertTrue(treesEquivalent(marginalTree.getRoot(), new TreeParser(correctNewickStrings[r], false, true, false, 0).getRoot(), 1e-15));
    }
}
Also used : TreeParser(beast.util.TreeParser) Test(org.junit.Test)

Example 22 with TreeParser

use of beast.util.TreeParser in project bacter by tgvaughan.

the class ACGLikelihoodApproxTest method testTreeHeightMap.

@Test
public void testTreeHeightMap() throws Exception {
    List<Sequence> sequences = new ArrayList<>();
    // 01234567890123456789
    sequences.add(new Sequence("t1", "GGGGGGGGGGGGGGGGGGGG"));
    sequences.add(new Sequence("t2", "CCCCCCCCCCCCCCCCCCCC"));
    sequences.add(new Sequence("t3", "TTTTTTTTTTTTTTTTTTTT"));
    Alignment alignment = new Alignment(sequences, "nucleotide");
    Locus locus = new Locus("locus", alignment);
    TreeParser tree = new TreeParser(alignment, "((t1:1,t2:1):1,t3:2):0;");
    ConversionGraph acg = new ConversionGraph();
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    ACGLikelihoodApprox likelihoodApprox = new ACGLikelihoodApprox();
    likelihoodApprox.initByName("acg", acg, "substitutionRate", "1.0", "alignment", alignment, "locus", locus);
    Map<Double, Coalescence> heightMap = likelihoodApprox.getCoalescenceHeights();
    Assert.assertEquals(2, heightMap.size());
    Assert.assertTrue(heightMap.containsKey(1.0));
    Assert.assertTrue(heightMap.containsKey(2.0));
    Assert.assertTrue(heightMap.get(1.0).equals(new Coalescence("[0,20]{0}{1}")));
    Assert.assertTrue(heightMap.get(2.0).equals(new Coalescence("[0,20]{0,1}{2}")));
}
Also used : Alignment(beast.evolution.alignment.Alignment) TreeParser(beast.util.TreeParser) ArrayList(java.util.ArrayList) Sequence(beast.evolution.alignment.Sequence) Test(org.junit.Test)

Example 23 with TreeParser

use of beast.util.TreeParser in project bacter by tgvaughan.

the class ConversionGraph method fromStringOld.

/**
 * Load ACG from old string representation.
 *
 * @param str string representation of ACG
 */
@Deprecated
public void fromStringOld(String str) {
    // Extract clonal frame and recombination components of string
    Pattern cfPattern = Pattern.compile("^[^\\(]*(\\(.*)$");
    Matcher cfMatcher = cfPattern.matcher(str);
    if (!cfMatcher.find())
        throw new RuntimeException("Error parsing ACG state string.");
    // Process clonal frame
    String sNewick = cfMatcher.group(cfMatcher.groupCount());
    try {
        TreeParser parser = new TreeParser();
        parser.thresholdInput.setValue(1e-10, parser);
        parser.offsetInput.setValue(0, parser);
        setRoot(parser.parseNewick(sNewick));
    } catch (Exception ex) {
        Logger.getLogger(ConversionGraph.class.getName()).log(Level.SEVERE, null, ex);
    }
    initArrays();
    Pattern convPattern = Pattern.compile("\\[&([^]]*)]");
    Matcher convMatcher = convPattern.matcher(str);
    // Process recombinations
    for (Locus locus : getLoci()) convs.get(locus).clear();
    while (convMatcher.find()) {
        String[] elements = convMatcher.group(1).split(",");
        Locus locus = getLocusByID(elements[0]);
        if (locus == null)
            throw new RuntimeException("Uknown locus id " + elements[0] + ".  Aborting.");
        Node node1 = getNode(Integer.parseInt(elements[1]));
        int startLocus = Integer.parseInt(elements[2]);
        double height1 = Double.parseDouble(elements[3]);
        Node node2 = getNode(Integer.parseInt(elements[4]));
        int endLocus = Integer.parseInt(elements[5]);
        double height2 = Double.parseDouble(elements[6]);
        Conversion conv = new Conversion(node1, height1, node2, height2, startLocus, endLocus, this, locus);
        addConversion(conv);
    }
    if (isInvalid()) {
        throw new IllegalArgumentException("Invalid ACG read from string. Aborting.");
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Node(beast.evolution.tree.Node) TreeParser(beast.util.TreeParser)

Example 24 with TreeParser

use of beast.util.TreeParser in project MultiTypeTree by tgvaughan.

the class MultiTypeTree method fromXML.

// ///////////////////////////////////////////////
// Serialization and deserialization for state //
// ///////////////////////////////////////////////
/**
 * reconstruct tree from XML fragment in the form of a DOM node *
 * @param node
 */
@Override
public void fromXML(org.w3c.dom.Node node) {
    try {
        String sNewick = node.getTextContent();
        TreeParser parser = new TreeParser();
        parser.initByName("IsLabelledNewick", false, "offset", 0, "adjustTipHeights", false, "singlechild", true, "newick", sNewick);
        // parser.m_nThreshold.setValue(1e-10, parser);
        // parser.m_nOffset.setValue(0, parser);
        initFromFlatTree(parser, true);
        initArrays();
    } catch (Exception ex) {
        Logger.getLogger(MultiTypeTree.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : TreeParser(beast.util.TreeParser)

Example 25 with TreeParser

use of beast.util.TreeParser in project MultiTypeTree by tgvaughan.

the class MultiTypeTreeFromNewick method initAndValidate.

@Override
public void initAndValidate() {
    super.initAndValidate();
    TreeParser parser = new TreeParser();
    parser.initByName("IsLabelledNewick", true, "adjustTipHeights", adjustTipHeightsInput.get(), "singlechild", true, "newick", newickStringInput.get());
    initFromFlatTree(parser, true);
}
Also used : TreeParser(beast.util.TreeParser)

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