use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class CFConvSwapExperiment method main.
public static void main(String[] args) throws Exception {
// Load model
XMLParser parser = new XMLParser();
MCMC mcmc = (MCMC) parser.parseFile(new File("inferencePreSimulatedData.xml"));
State state = mcmc.startStateInput.get();
state.setStateFileName("problem.state");
state.restoreFromFile();
double oldPosterior = state.robustlyCalcPosterior(mcmc.posteriorInput.get());
ConversionGraph acg = (ConversionGraph) state.getStateNode(0);
PrintStream ps = new PrintStream("proposal.trees");
ps.println(acg);
Node srcNode = acg.getNode(3);
Node srcNodeS = getSibling(srcNode);
Node destNode = acg.getNode(6);
double t_srcNodeP = srcNode.getParent().getHeight();
disconnectEdge(acg, srcNode);
Locus locus = acg.getLoci().get(0);
Conversion convToReplace = acg.getConversions(locus).get(27);
acg.deleteConversion(convToReplace);
Node srcNodeP = srcNode.getParent();
connectEdge(acg, srcNode, destNode, convToReplace.getHeight2());
// Move Conversions
for (Conversion conv : acg.getConversions(locus)) {
boolean moved = false;
if (conv.getNode1() == srcNode && conv.getHeight1() > srcNodeP.getHeight()) {
conv.setNode1(destNode);
moved = true;
}
if (conv.getNode2() == srcNode && conv.getHeight2() > srcNodeP.getHeight()) {
conv.setNode2(destNode);
moved = true;
}
if (moved) {
while (conv.getHeight1() > conv.getNode1().getParent().getHeight()) conv.setNode1(conv.getNode1().getParent());
while (conv.getHeight2() > conv.getNode2().getParent().getHeight()) conv.setNode2(conv.getNode2().getParent());
}
}
Conversion convNew = new Conversion();
convNew.setLocus(locus);
convNew.setStartSite(0);
convNew.setEndSite(4000);
convNew.setNode1(srcNode);
convNew.setNode2(srcNodeS);
convNew.setHeight1(convToReplace.getHeight1());
convNew.setHeight2(t_srcNodeP);
acg.addConversion(convNew);
ps.println(acg);
double newPosterior = state.robustlyCalcPosterior(mcmc.posteriorInput.get());
System.out.println(newPosterior - oldPosterior);
// state.setStateFileName("proposal.state");
// state.storeToFile(1);
// Open state file
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class CFConvSwapExperiment method disconnectEdge.
public static void disconnectEdge(ConversionGraph acg, Node node) {
if (node.isRoot())
throw new IllegalArgumentException("Programmer error: " + "root argument passed to disconnectEdge().");
Node parent = node.getParent();
Node sister = getSibling(node);
if (parent.isRoot()) {
parent.removeChild(sister);
sister.setParent(null);
} else {
Node grandParent = parent.getParent();
grandParent.removeChild(parent);
parent.setParent(null);
parent.removeChild(sister);
grandParent.addChild(sister);
}
for (Locus locus : acg.getLoci()) {
for (Conversion conv : acg.getConversions(locus)) {
if (conv.getNode1() == parent)
conv.setNode1(sister);
if (conv.getNode2() == parent)
conv.setNode2(sister);
}
}
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class ClonalFrameLogger method init.
public void init(PrintStream out) {
Node node = acg.getRoot();
out.println("#NEXUS\n");
out.println("Begin taxa;");
out.println("\tDimensions ntax=" + acg.getLeafNodeCount() + ";");
out.println("\t\tTaxlabels");
acg.printTaxa(node, out, acg.getNodeCount() / 2);
out.println("\t\t\t;");
out.println("End;");
out.println("Begin trees;");
out.println("\tTranslate");
acg.printTranslate(node, out, acg.getNodeCount() / 2);
out.print(";");
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class CFOperator method expandConversions.
/**
* Take length of new edge above srcNode that is greater than the
* original height of srcNode.parent and shifts a random fraction of
* conversion attachments to it from the lineage above destNode.
*
* In the case that destNode was the root, the conversions starting
* above destNode are drawn from the prior.
*
* Assumes topology has not yet been altered.
*
* @param srcNode source node for the move
* @param destNode dest node for the move
* @param destTime new time drawn for srcNode.P.
* @return log probability of new conversion configuration.
*/
protected double expandConversions(Node srcNode, Node destNode, double destTime) {
double logP = 0.0;
double volatileHeight = acg.getRoot().getHeight();
boolean forwardRootMove = destTime > volatileHeight;
Node node = srcNode.getParent();
while (node != null) {
for (Locus locus : acg.getLoci()) {
for (Conversion conv : acg.getConversions(locus)) {
if (conv.getNode1() == node && conv.getHeight1() < destTime) {
if (Randomizer.nextBoolean())
conv.setNode1(srcNode);
logP += Math.log(0.5);
}
if (conv.getNode2() == node && conv.getHeight2() < destTime) {
if (Randomizer.nextBoolean())
conv.setNode2(srcNode);
logP += Math.log(0.5);
}
}
}
node = node.getParent();
}
// Apply topology modifications.
disconnectEdge(srcNode);
connectEdge(srcNode, destNode, destTime);
// this was a forward root move
if (forwardRootMove) {
acg.setRoot(srcNode.getParent());
double L = 2.0 * (destTime - volatileHeight);
double Nexp = L * rhoInput.get().getValue() * (acg.getTotalSequenceLength() + acg.getLoci().size() * (deltaInput.get().getValue() - 1.0));
int N = (int) Randomizer.nextPoisson(Nexp);
// Factorial cancels
logP += -Nexp + N * Math.log(Nexp);
for (int i = 0; i < N; i++) {
Conversion conv = new Conversion();
double u = Randomizer.nextDouble() * L;
if (u < 0.5 * L) {
conv.setNode1(destNode);
conv.setHeight1(volatileHeight + u);
} else {
conv.setNode1(srcNode);
conv.setHeight1(volatileHeight + u - 0.5 * L);
}
logP += Math.log(1.0 / L) + drawAffectedRegion(conv) + coalesceEdge(conv);
acg.addConversion(conv);
}
}
return logP;
}
use of beast.evolution.tree.Node in project bacter by tgvaughan.
the class CFSubtreeExchange method proposal.
@Override
public double proposal() {
double logHGF = 0.0;
if (acg.getLeafNodeCount() < 3)
return Double.NEGATIVE_INFINITY;
Node srcNode, srcNodeP, destNode, destNodeP;
if (isNarrowInput.get()) {
// Narrow exchange selection:
do {
srcNode = acg.getNode(Randomizer.nextInt(acg.getNodeCount() - 1));
} while (srcNode.getParent().isRoot());
srcNodeP = srcNode.getParent();
destNode = getSibling(srcNodeP);
destNodeP = destNode.getParent();
} else {
// Wide exchange selection:
srcNode = acg.getNode(Randomizer.nextInt(acg.getNodeCount() - 1));
srcNodeP = srcNode.getParent();
do {
destNode = acg.getNode(Randomizer.nextInt(acg.getNodeCount() - 1));
} while (destNode == srcNode || destNode.getParent() == srcNode.getParent());
destNodeP = destNode.getParent();
}
double t_srcNodeP = srcNodeP.getHeight();
double t_destNodeP = destNodeP.getHeight();
// Reject if substitution would result in negative branch lengths:
if (destNode == srcNodeP || srcNode == destNodeP || destNode.getHeight() > t_srcNodeP || srcNode.getHeight() > t_destNodeP)
return Double.NEGATIVE_INFINITY;
if (t_srcNodeP > t_destNodeP) {
Node srcNodeS = getSibling(srcNode);
logHGF += collapseConversions(srcNode, destNodeP, t_destNodeP);
if (!srcNodeS.isRoot() && srcNodeS.getLength() == 0.0)
srcNodeS = srcNodeS.getParent();
logHGF -= expandConversions(destNode, srcNodeS, t_srcNodeP);
} else {
Node srcNodeS = getSibling(srcNode);
logHGF -= expandConversions(srcNode, destNodeP, t_destNodeP);
if (srcNodeP.isRoot())
acg.setRoot(srcNodeP);
logHGF += collapseConversions(destNode, srcNodeS, t_srcNodeP);
}
assert !acg.isInvalid() : "CFSTX proposed invalid state.";
return logHGF;
}
Aggregations