use of bacter.ConversionGraph in project bacter by tgvaughan.
the class ACGLikelihoodTest method testLikelihoodUsingSimulatedData.
@Test
public void testLikelihoodUsingSimulatedData() 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);
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:
ACGLikelihood argLikelihood = new ACGLikelihood();
argLikelihood.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
double logP = argLikelihood.calculateLogP();
// Compare product of likelihoods of "marginal alignments" with
// likelihood computed using RGL.
ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
argLikelihoodSlow.initByName("locus", locus, "data", alignment, "tree", acg, "siteModel", siteModel);
double logPprime = argLikelihoodSlow.calculateLogP();
double relError = 2.0 * Math.abs(logP - logPprime) / Math.abs(logP + logPprime);
System.out.format("logP=%g\nlogPprime=%g\nrelError=%g\n", logP, logPprime, relError);
assertTrue(relError < 1e-13);
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class ACGLikelihoodTest method testBeagleLikelihood.
@Test
public void testBeagleLikelihood() 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
ACGLikelihoodBeagle argLikelihood = new ACGLikelihoodBeagle();
argLikelihood.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
argLikelihoodSlow.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
acg.setEverythingDirty(true);
try {
double logP = argLikelihood.calculateLogP();
double logPtrue = argLikelihoodSlow.calculateLogP();
double relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + 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);
} catch (RuntimeException ex) {
System.err.println("Beagle library not found: skipping beagle likelihood test.");
}
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class ACGLikelihoodTest method testLikelihoodFixedData.
@Test
public void testLikelihoodFixedData() 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);
ACGLikelihoodSlow argLikelihoodSlow = new ACGLikelihoodSlow();
argLikelihoodSlow.initByName("locus", locus, "tree", acg, "siteModel", siteModel);
acg.setEverythingDirty(true);
double logP = argLikelihood.calculateLogP();
double logPtrue = argLikelihoodSlow.calculateLogP();
double relativeDiff = Math.abs(2.0 * (logPtrue - logP) / (logPtrue + 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);
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class DifferenceFromTrueACG method main.
public static void main(String[] args) throws IOException, XMLStreamException {
Options options = processArguments(args);
// Load true ARG
BacterACGLogReader truthReader = new BacterACGLogReader(options.truthFile, 0);
if (truthReader.getACGCount() != 1) {
System.out.println("Expected exactly 1 ACG in truth file. Found " + truthReader.getACGCount());
System.exit(1);
}
ConversionGraph trueACG = null;
for (ConversionGraph acg : truthReader) trueACG = acg;
// Determine clades present in truth
Clade[] trueClades = new Clade[trueACG.getNodeCount()];
getClades(trueClades, trueACG.getRoot());
Set<Clade> trueCladeSet = new HashSet<>(Arrays.asList(trueClades));
// Set up histograms
Map<Clade, Integer> cladeHist = new HashMap<>();
for (Clade clade : trueClades) cladeHist.put(clade, 0);
Map<Conversion, Integer> convHist = new HashMap<>();
for (Conversion conv : trueACG.getConversions(trueACG.getLoci().get(0))) convHist.put(conv, 0);
// Set up ARG log file reader
ACGLogReader logReader;
if (options.useCOFormat) {
logReader = new COACGLogFileReader(options.logFile, options.burninPerc);
} else {
logReader = new BacterACGLogReader(options.logFile, options.burninPerc);
}
try (PrintStream ps = new PrintStream(options.outFile)) {
ps.println("trueCladeCount sampledTrueCladeCount trueConvCount sampledConvCount sampledTrueConvCount");
for (ConversionGraph acg : logReader) {
Clade[] clades = new Clade[acg.getNodeCount()];
getClades(clades, acg.getRoot());
List<Double> timeErrors = new ArrayList<>();
int sampledTrueClades = countSampledTrueClades(trueClades, clades, options.ageTol, cladeHist);
int sampledTrueConvs = countSampledTrueConversions(trueACG, trueClades, acg, clades, options.boundaryTol, options.ageTol, convHist);
ps.println(trueACG.getNodeCount() + "\t" + sampledTrueClades + "\t" + trueACG.getConvCount(trueACG.getLoci().get(0)) + "\t" + acg.getConvCount(acg.getLoci().get(0)) + "\t" + sampledTrueConvs);
}
}
try (PrintStream ps = new PrintStream(options.summaryFile)) {
ps.println("trueCladeCount recoveredCladeCount trueConvCount recoveredConvCount");
int recoveredTrueClades = countRecoveredTrueClades(cladeHist, logReader.getCorrectedACGCount(), 0.5);
int recoveredTrueConvs = countRecoveredTrueConvs(convHist, logReader.getCorrectedACGCount(), 0.5);
ps.println(trueACG.getNodeCount() + "\t" + recoveredTrueClades + "\t" + trueACG.getConvCount(trueACG.getLoci().get(0)) + "\t" + recoveredTrueConvs);
}
}
use of bacter.ConversionGraph in project bacter by tgvaughan.
the class PiecewisePopulationFunction 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);
PiecewisePopulationFunction skyline = new PiecewisePopulationFunction();
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();
}
}
Aggregations