use of bacter.ConversionGraph in project bacter by tgvaughan.
the class SkylinePopulationFunction method main.
/**
* Main method for testing.
*
* @param args command line arguments (unused)
*/
public static void main(String[] args) throws Exception {
String acgString = "[&15,0,1.3905355989030808,31,770,1.597708055397074] " + "[&30,931,2.4351280458424904,36,2486,3.78055549386568] " + "[&15,941,2.0439957300083322,38,2364,6.911056700367016] " + "[&36,1091,4.285505683622974,38,2589,9.867725913197855] " + "((((10:0.5385300170206817,(17:0.116794353049212," + "((3:0.039229346597297564,12:0.039229346597297564)23:0.04582913870888949," + "13:0.08505848530618705)24:0.03173586774302495)26:0.4217356639714697)28:1.8114199763246093," + "((8:0.10883006062265468,2:0.10883006062265468)25:0.556428062025291," + "(6:0.5393311342677402,11:0.5393311342677402)29:0.12592698838020555)31:1.6846918706973453)34:1.4536824928125807," + "(1:0.47184545557390367,14:0.47184545557390367)27:3.331787030583968)37:2.9704369411362554," + "(((15:2.0624287390593707,((16:0.01825347077733299,19:0.01825347077733299)21:0.7668749128372041," + "(7:0.008018731329538273,9:0.008018731329538273)20:0.7771096522849988)32:1.2773003554448337)33:0.7487092404613747," + "4:2.8111379795207454)35:0.1331794525400949,((0:0.0243537216663141," + "5:0.0243537216663141)22:0.5681537100482162,18:0.5925074317145304)30:2.35181000034631)36:3.829751995233287)38:0.0";
ConversionGraph acg = new ConversionGraph();
acg.initByName("siteCount", 10000, "fromString", acgString);
SkylinePopulationFunction skyline = new SkylinePopulationFunction();
skyline.initByName("acg", acg, "popSizes", new RealParameter("1.0 1.0 5.0 1.0 2.0"), "groupSizes", new IntegerParameter("0"), "piecewiseLinear", true);
try (PrintStream ps = new PrintStream("out.txt")) {
ps.println("t N intensity intensityInv");
double t = 0.0;
while (t < 10) {
ps.format("%g %g %g %g\n", t, skyline.getPopSize(t), skyline.getIntensity(t), skyline.getInverseIntensity(skyline.getIntensity(t)));
t += 0.001;
}
ps.close();
}
}
use of bacter.ConversionGraph 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 bacter.ConversionGraph in project bacter by tgvaughan.
the class BacterACGLogReader method iterator.
/**
* Retrieve an iterator for iterating over the ACGs represented
* by this log file. Important points
*
* 1. The iterator only iterates over as many (non-burnin) ACGs as exist
* in the file when the ACGLogFileReader is constructed. This is to avoid
* problems associated with summarising ongoing analyses.
*
* 2. The iterator reuses a single ConversionGraph object during
* the iteration. This means that if you want to collect these
* graphs as the iteration progresses you'll need to use
* ConversionGraph::copy.
*
* @return ConversionGraph iterator
*/
@Override
public Iterator<ConversionGraph> iterator() {
try {
reset();
skipBurnin();
} catch (IOException e) {
throw new IllegalStateException(e.getMessage());
}
ConversionGraph acg = new ConversionGraph();
for (Locus locus : getLoci()) acg.lociInput.setValue(locus, acg);
try {
acg.initAndValidate();
} catch (Exception e) {
throw new IllegalStateException(e.getMessage());
}
return new Iterator<ConversionGraph>() {
boolean lineConsumed = true;
String nextLine = null;
int current = 0;
private String getNextLineNoConsume() {
if (lineConsumed) {
try {
nextLine = getNextTreeString();
lineConsumed = false;
} catch (IOException e) {
throw new IllegalStateException(e.getMessage());
}
}
return nextLine;
}
private void printProgressBar() {
if (current == 0) {
System.out.println("0% 25% 50% 75% 100%");
System.out.println("|--------------|--------------|--------------|--------------|");
}
if (current < getCorrectedACGCount() - 1) {
if (current % (int) Math.ceil(getCorrectedACGCount() / 61.0) == 0) {
System.out.print("\r");
for (int i = 0; i < Math.round(61.0 * current / getCorrectedACGCount()); i++) System.out.print("*");
System.out.flush();
}
} else {
System.out.print("\r");
for (int i = 0; i < 61; i++) System.out.print("*");
System.out.println();
}
}
@Override
public boolean hasNext() {
return current < getCorrectedACGCount() && getNextLineNoConsume() != null;
}
@Override
public ConversionGraph next() {
String result = getNextLineNoConsume();
lineConsumed = true;
acg.fromExtendedNewick(result);
printProgressBar();
current += 1;
return acg;
}
};
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class ConvertedRegionLogger method init.
@Override
public void init(PrintStream out) {
final ConversionGraph arg = acgInput.get();
String mainID = (getID() == null || getID().matches(("\\s*"))) ? arg.getID() + ".converted" : getID();
for (Locus locus : acgInput.get().getLoci()) out.print(mainID + "." + locus.getID() + "\t");
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class TrimmedACGLogger method log.
@Override
public void log(long nSample, PrintStream out) {
ConversionGraph arg = acgInput.get();
out.print("tree STATE_" + nSample + " = ");
out.print(arg.getTrimmedExtendedNewick());
}
Aggregations