Search in sources :

Example 1 with ClusterTree

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

the class StateNodeInitialiserTest method testNewickTree.

@Test
public void testNewickTree() throws Exception {
    Alignment data = BEASTTestCase.getAlignment();
    Tree tree = new Tree();
    tree.initAndValidate();
    assertEquals(true, tree.getNodeCount() == 1);
    ClusterTree tree2 = new ClusterTree();
    tree2.initByName("initial", tree, "clusterType", "upgma", "taxa", data);
    assertEquals(true, tree.getNodeCount() > 1);
    assertEquals(11, tree.getNodeCount());
}
Also used : Alignment(beast.evolution.alignment.Alignment) ClusterTree(beast.util.ClusterTree) Tree(beast.evolution.tree.Tree) ClusterTree(beast.util.ClusterTree) Test(org.junit.Test)

Example 2 with ClusterTree

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

the class ClusterTreeTest method testUPGMA.

public void testUPGMA() throws Exception {
    Alignment alignment = BEASTTestCase.getAlignment();
    JukesCantor JC = new JukesCantor();
    JC.initAndValidate();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("substModel", JC);
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    String expectedNewick = "((((human:0.01903085702575253,(chimp:0.008560512208575313,bonobo:0.008560512208575313):0.010470344817177218):0.007962255880644985,gorilla:0.026993112906397516):0.019197419394211015,orangutan:0.04619053230060853):0.007214240662738673,siamang:0.053404772963347204):0.0";
    String actualNewick = tree.getRoot().toNewick();
    assertEquals(expectedNewick, actualNewick);
    // select 3 sequences
    List<Sequence> seqs = new ArrayList<Sequence>();
    seqs.addAll(alignment.sequenceInput.get());
    List<Sequence> newseqs = alignment.sequenceInput.get();
    newseqs.clear();
    newseqs.add(Sequence.getSequenceByTaxon("bonobo", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("chimp", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("human", seqs));
    alignment.initAndValidate();
    tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    expectedNewick = "((bonobo:0.008560512208575313,chimp:0.008560512208575313):0.010470344817177218,human:0.01903085702575253):0.0";
    System.err.println("Seqs:");
    for (Sequence s : seqs) {
        System.err.println(s.taxonInput.get());
    }
    System.err.println("Newseqs:");
    for (Sequence s : newseqs) {
        System.err.println(s.taxonInput.get());
    }
    actualNewick = tree.getRoot().toNewick();
    assertEquals(expectedNewick, actualNewick);
    // same sequences in different order
    newseqs.clear();
    newseqs.add(Sequence.getSequenceByTaxon("human", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("chimp", seqs));
    newseqs.add(Sequence.getSequenceByTaxon("bonobo", seqs));
    alignment.initAndValidate();
    tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    actualNewick = tree.getRoot().toNewick();
    expectedNewick = "(human:0.01903085702575253,(chimp:0.008560512208575313,bonobo:0.008560512208575313):0.010470344817177218):0.0";
    assertEquals(expectedNewick, actualNewick);
}
Also used : Alignment(beast.evolution.alignment.Alignment) ClusterTree(beast.util.ClusterTree) ArrayList(java.util.ArrayList) SiteModel(beast.evolution.sitemodel.SiteModel) Sequence(beast.evolution.alignment.Sequence) JukesCantor(beast.evolution.substitutionmodel.JukesCantor)

Example 3 with ClusterTree

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

the class SerializationDeserializationTest method testXML.

@Test
public void testXML() throws Exception {
    Alignment alignment = getAlignment();
    alignment.setID("alignment");
    Locus locus = new Locus("locus", alignment.getSiteCount());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Add recombination event 1
    Node node1 = acg.getExternalNodes().get(0);
    Node node2 = node1.getParent();
    double height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    double height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    int startLocus = 100;
    int endLocus = 200;
    Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb1);
    // Add recombination event 2
    node1 = acg.getExternalNodes().get(0);
    node2 = acg.getRoot();
    height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    height2 = node2.getHeight() + 1.0;
    startLocus = 300;
    endLocus = 400;
    Conversion recomb2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb2);
    // Write ARG out to XML string
    String xmlStr = acg.toXML();
    // Build DOM from XML string
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
    doc.normalize();
    NodeList nodes = doc.getElementsByTagName("*");
    org.w3c.dom.Node docNode = nodes.item(0);
    // Read ARG back in from DOM
    ConversionGraph argNew = new ConversionGraph();
    argNew.assignFrom(tree);
    argNew.initByName("locus", locus);
    argNew.fromXML(docNode);
    // Check that new ARG matches old
    Conversion newRecomb1 = argNew.getConversions(locus).get(0);
    assertEquals(newRecomb1.getNode1().getNr(), recomb1.getNode1().getNr());
    assertEquals(newRecomb1.getNode2().getNr(), recomb1.getNode2().getNr());
    assertEquals(newRecomb1.getHeight1(), recomb1.getHeight1(), 1e-15);
    assertEquals(newRecomb1.getHeight2(), recomb1.getHeight2(), 1e-15);
    assertEquals(newRecomb1.getStartSite(), recomb1.getStartSite());
    assertEquals(newRecomb1.getEndSite(), recomb1.getEndSite());
    Conversion newRecomb2 = argNew.getConversions(locus).get(1);
    assertEquals(newRecomb2.getNode1().getNr(), recomb2.getNode1().getNr());
    assertEquals(newRecomb2.getNode2().getNr(), recomb2.getNode2().getNr());
    assertEquals(newRecomb2.getHeight1(), recomb2.getHeight1(), 1e-15);
    assertEquals(newRecomb2.getHeight2(), recomb2.getHeight2(), 1e-15);
    assertEquals(newRecomb2.getStartSite(), recomb2.getStartSite());
    assertEquals(newRecomb2.getEndSite(), recomb2.getEndSite());
// Note that there are minor differences in the tree due to
// rounding errors.  Is this normal!?
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) Alignment(beast.evolution.alignment.Alignment) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 4 with ClusterTree

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

the class SerializationDeserializationTest method testString.

@Test
public void testString() throws Exception {
    Alignment alignment = getAlignment();
    alignment.setID("alignment");
    Locus locus = new Locus("locus", alignment.getSiteCount());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", alignment);
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Add recombination event 1
    Node node1 = acg.getExternalNodes().get(0);
    Node node2 = node1.getParent();
    double height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    double height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    int startLocus = 100;
    int endLocus = 200;
    Conversion conv1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(conv1);
    // Add recombination event 2
    node1 = acg.getExternalNodes().get(0);
    node2 = acg.getRoot();
    height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    height2 = node2.getHeight() + 1.0;
    startLocus = 300;
    endLocus = 400;
    Conversion conv2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(conv2);
    // Write ACG out to string
    String argString = acg.toStringOld();
    // Read ACG back in from string
    ConversionGraph argNew = new ConversionGraph();
    argNew.initByName("locus", locus, "fromString", argString);
    // Check that new ACG matches old
    Conversion newConv1 = argNew.getConversions(locus).get(0);
    assertEquals(newConv1.getNode1().getNr(), conv1.getNode1().getNr());
    assertEquals(newConv1.getNode2().getNr(), conv1.getNode2().getNr());
    assertEquals(newConv1.getHeight1(), conv1.getHeight1(), 1e-15);
    assertEquals(newConv1.getHeight2(), conv1.getHeight2(), 1e-15);
    assertEquals(newConv1.getStartSite(), conv1.getStartSite());
    assertEquals(newConv1.getEndSite(), conv1.getEndSite());
    Conversion newConv2 = argNew.getConversions(locus).get(1);
    assertEquals(newConv2.getNode1().getNr(), conv2.getNode1().getNr());
    assertEquals(newConv2.getNode2().getNr(), conv2.getNode2().getNr());
    assertEquals(newConv2.getHeight1(), conv2.getHeight1(), 1e-15);
    assertEquals(newConv2.getHeight2(), conv2.getHeight2(), 1e-15);
    assertEquals(newConv2.getStartSite(), conv2.getStartSite());
    assertEquals(newConv2.getEndSite(), conv2.getEndSite());
// Note that there are minor differences in the tree due to
// rounding errors.  Is this normal!?
}
Also used : Alignment(beast.evolution.alignment.Alignment) ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) Test(org.junit.Test)

Example 5 with ClusterTree

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

the class ACGLikelihoodTest method testLikelihoodAmbiguities.

@Test
public void testLikelihoodAmbiguities() throws Exception {
    Locus locus = new Locus("locus", getAlignment());
    // ConversionGraph
    ConversionGraph acg = new ConversionGraph();
    ClusterTree tree = new ClusterTree();
    tree.initByName("clusterType", "upgma", "taxa", locus.getAlignment());
    acg.assignFrom(tree);
    acg.initByName("locus", locus);
    // Site model:
    JukesCantor jc = new JukesCantor();
    jc.initByName();
    SiteModel siteModel = new SiteModel();
    siteModel.initByName("substModel", jc);
    // Likelihood
    ACGLikelihood argLikelihood = new ACGLikelihood();
    argLikelihood.initByName("locus", locus, "tree", acg, "siteModel", siteModel, "useAmbiguities", true);
    ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
    argLikelihoodSlow.initByName("locus", locus, "tree", acg, "siteModel", siteModel, "useAmbiguities", true);
    acg.setEverythingDirty(true);
    double logP = argLikelihood.calculateLogP();
    double logPtrue = argLikelihoodSlow.calculateLogP();
    double relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    System.out.println("Test 1.  Truth: " + logPtrue + " Value: " + logP);
    assertTrue(relativeDiff < 1e-14);
    // Add a single recombination event
    Node node1 = acg.getExternalNodes().get(0);
    Node node2 = node1.getParent();
    double height1 = 0.5 * (node1.getHeight() + node1.getParent().getHeight());
    double height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    int startLocus = 100;
    int endLocus = 200;
    Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb1);
    logP = argLikelihood.calculateLogP();
    logPtrue = argLikelihoodSlow.calculateLogP();
    relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    assertTrue(relativeDiff < 1e-14);
    // Add another recombination event
    node1 = acg.getExternalNodes().get(0);
    node2 = acg.getNode(20);
    height1 = 0.75 * (node1.getHeight() + node1.getParent().getHeight());
    height2 = 0.5 * (node2.getHeight() + node2.getParent().getHeight());
    startLocus = 250;
    endLocus = 300;
    Conversion recomb2 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
    acg.addConversion(recomb2);
    logP = argLikelihood.calculateLogP();
    logPtrue = argLikelihoodSlow.calculateLogP();
    relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + logP));
    assertTrue(relativeDiff < 1e-14);
}
Also used : ClusterTree(beast.util.ClusterTree) Node(beast.evolution.tree.Node) SiteModel(beast.evolution.sitemodel.SiteModel) Locus(bacter.Locus) JukesCantor(beast.evolution.substitutionmodel.JukesCantor) ConversionGraph(bacter.ConversionGraph) Conversion(bacter.Conversion) Test(org.junit.Test)

Aggregations

ClusterTree (beast.util.ClusterTree)11 Test (org.junit.Test)9 Alignment (beast.evolution.alignment.Alignment)8 Node (beast.evolution.tree.Node)7 SiteModel (beast.evolution.sitemodel.SiteModel)5 JukesCantor (beast.evolution.substitutionmodel.JukesCantor)5 Conversion (bacter.Conversion)3 ConversionGraph (bacter.ConversionGraph)3 Locus (bacter.Locus)3 RealParameter (beast.core.parameter.RealParameter)2 Sequence (beast.evolution.alignment.Sequence)2 TaxonSet (beast.evolution.alignment.TaxonSet)2 Tree (beast.evolution.tree.Tree)2 ArrayList (java.util.ArrayList)2 Taxon (beast.evolution.alignment.Taxon)1 Distance (beast.evolution.alignment.distance.Distance)1 JukesCantorDistance (beast.evolution.alignment.distance.JukesCantorDistance)1 RandomTree (beast.evolution.tree.RandomTree)1 ConstantPopulation (beast.evolution.tree.coalescent.ConstantPopulation)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1