use of beast.evolution.tree.Node in project MultiTypeTree by tgvaughan.
the class NodeRetype method proposal.
@Override
public double proposal() {
double logHR = 0.0;
// Select node:
Node node = mtTree.getNode(mtTree.getLeafNodeCount() + Randomizer.nextInt(mtTree.getInternalNodeCount()));
// Record probability of current types along attached branches:
if (!node.isRoot())
logHR += getBranchTypeProb(node);
logHR += getBranchTypeProb(node.getLeft()) + getBranchTypeProb(node.getRight());
// Select new node type:
((MultiTypeNode) node).setNodeType(Randomizer.nextInt(migModel.getNTypes()));
// Retype attached branches:
try {
if (!node.isRoot())
logHR -= retypeBranch(node);
logHR -= retypeBranch(node.getLeft()) + retypeBranch(node.getRight());
} catch (NoValidPathException e) {
return Double.NEGATIVE_INFINITY;
}
return logHR;
}
use of beast.evolution.tree.Node 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!?
}
use of beast.evolution.tree.Node 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!?
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class TestBase method getCladeHeights.
/**
* Retrieve clades and heights of clade MRCAs from tree.
*
* @param root
* @return Map from clades to corresponding MRCA heights.
*/
private Map<Clade, Double> getCladeHeights(Node root) {
Map<Clade, Double> cladeHeights = new HashMap<>();
cladeHeights.put(new Clade(root), root.getHeight());
for (Node node : root.getAllChildNodesAndSelf()) if (!node.isLeaf())
cladeHeights.put(new Clade(node), node.getHeight());
return cladeHeights;
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class ACGLikelihoodTest method testLikelihoodCaching.
@Test
public void testLikelihoodCaching() throws Exception {
ConstantPopulation popFunc = new ConstantPopulation();
popFunc.initByName("popSize", new RealParameter("1.0"));
Locus locus = new Locus("locus", 10000);
TaxonSet taxonSet = getTaxonSet(10);
ConversionGraph acg = new SimulatedACG();
acg.initByName("rho", 5.0 / locus.getSiteCount(), "delta", 1000.0, "populationModel", popFunc, "locus", locus, "taxonset", taxonSet);
State state = new State();
state.initByName("stateNode", acg);
state.initialise();
System.out.println(acg);
// Site model:
JukesCantor jc = new JukesCantor();
jc.initByName();
SiteModel siteModel = new SiteModel();
siteModel.initByName("mutationRate", new RealParameter("1"), "substModel", jc);
// Simulate alignment:
SimulatedAlignment alignment = new SimulatedAlignment();
alignment.initByName("acg", acg, "siteModel", siteModel, "outputFileName", "simulated_alignment.nexus", "useNexus", true);
// Calculate likelihood 1:
ACGLikelihood argLikelihood = new ACGLikelihood();
argLikelihood.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
argLikelihoodSlow.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
double logP1 = argLikelihood.calculateLogP();
double logP1prime = argLikelihoodSlow.calculateLogP();
double relError = 2.0 * Math.abs(logP1 - logP1prime) / Math.abs(logP1 + logP1prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP1, logP1prime, relError);
assertTrue(relError < 1e-13);
// 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 = 500;
int endLocus = 600;
Conversion recomb1 = new Conversion(node1, height1, node2, height2, startLocus, endLocus, acg, locus);
acg.addConversion(recomb1);
double logP2 = argLikelihood.calculateLogP();
double logP2prime = argLikelihoodSlow.calculateLogP();
relError = 2.0 * Math.abs(logP2 - logP2prime) / Math.abs(logP2 + logP2prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP2, logP2prime, relError);
assertTrue(relError < 1e-13);
state.restore();
double logP3 = argLikelihood.calculateLogP();
double logP3prime = argLikelihoodSlow.calculateLogP();
relError = 2.0 * Math.abs(logP3 - logP3prime) / Math.abs(logP3 + logP3prime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP3, logP3prime, relError);
assertTrue(relError < 1e-13);
}
Aggregations