use of beast.evolution.alignment.Alignment 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.alignment.Alignment in project bacter by tgvaughan.
the class TestBase method createMarginalAlignment.
/**
* Create Alignment object representing alignment of a region
* corresponding to a single marginal tree.
*
* @param alignment
* @param region
* @return
* @throws Exception
*/
public Alignment createMarginalAlignment(Alignment alignment, Region region) throws Exception {
List<Sequence> sequences = Lists.newArrayList();
for (int leafIdx = 0; leafIdx < alignment.getTaxonCount(); leafIdx++) {
List<Integer> stateSequence;
stateSequence = alignment.getCounts().get(leafIdx).subList(region.leftBoundary, region.rightBoundary);
String taxonName = alignment.getTaxaNames().get(leafIdx);
String charSequence = alignment.getDataType().encodingToString(stateSequence);
sequences.add(new Sequence(taxonName, charSequence));
}
return new Alignment(sequences, alignment.getDataType().getTypeDescription());
}
use of beast.evolution.alignment.Alignment in project bacter by tgvaughan.
the class ACGLikelihoodApproxTest method testACGHeightMap.
@Test
public void testACGHeightMap() throws Exception {
List<Sequence> sequences = new ArrayList<>();
// 01234567890123456789
sequences.add(new Sequence("t1", "GGGGGGGGGGGGGGGGGGGG"));
sequences.add(new Sequence("t2", "CCCCCCCCCCCCCCCCCCCC"));
sequences.add(new Sequence("t3", "TTTTTTTTTTTTTTTTTTTT"));
Alignment alignment = new Alignment(sequences, "nucleotide");
Locus locus = new Locus("locus", alignment);
TreeParser tree = new TreeParser(alignment, "((t1:1,t2:1):1,t3:2):0;");
ConversionGraph acg = new ConversionGraph();
acg.assignFrom(tree);
acg.initByName("locus", locus);
Conversion conversion = new Conversion();
conversion.setNode1(acg.getNode(0));
conversion.setHeight1(0.5);
conversion.setNode2(acg.getNode(2));
conversion.setHeight2(1.5);
conversion.setStartSite(0);
conversion.setEndSite(9);
conversion.setLocus(locus);
acg.addConversion(conversion);
ACGLikelihoodApprox likelihoodApprox = new ACGLikelihoodApprox();
likelihoodApprox.initByName("acg", acg, "substitutionRate", "1.0", "alignment", alignment, "locus", locus);
Map<Double, Coalescence> heightMap = likelihoodApprox.getCoalescenceHeights();
Assert.assertEquals(3, heightMap.size());
Assert.assertTrue(heightMap.containsKey(1.0));
Assert.assertTrue(heightMap.containsKey(1.5));
Assert.assertTrue(heightMap.containsKey(2.0));
Assert.assertTrue("height map contains incorrect coalescence.", heightMap.get(1.0).equals(new Coalescence("[10,20]{0}{1}")));
Assert.assertTrue("height map contains incorrect coalescence.", heightMap.get(1.5).equals(new Coalescence("[0,10]{0}{2}")));
Assert.assertTrue("height map contains incorrect coalescence.", heightMap.get(2.0).equals(new Coalescence("[0,10]{0,2}{1} [10,20]{0,1}{2}")));
}
use of beast.evolution.alignment.Alignment in project bacter by tgvaughan.
the class SimulatedAlignmentTest method test.
@Test
public void test() throws Exception {
// Randomizer.setSeed(26);
Randomizer.setSeed(7);
Locus locus = new Locus("locus", 100000);
TaxonSet taxonSet = getTaxonSet(10);
ConstantPopulation popFunc = new ConstantPopulation();
popFunc.initByName("popSize", new RealParameter("1.0"));
ConversionGraph acg = new SimulatedACG();
acg.initByName("rho", 1.0 / 100000, "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.0"), "substModel", jc);
// Simulate alignment:
SimulatedAlignment alignment = new SimulatedAlignment();
alignment.initByName("acg", acg, "siteModel", siteModel, "outputFileName", "simulated_alignment.nexus", "useNexus", true);
for (Region region : acg.getRegions(locus)) System.out.println(new MarginalTree(acg, region));
// (Should be enough info here for precise agreement)
for (Region region : acg.getRegions(locus)) {
Alignment margAlign = createMarginalAlignment(alignment, region);
ClusterTree upgmaTree = new ClusterTree();
upgmaTree.initByName("clusterType", "upgma", "taxa", margAlign);
MarginalTree marginalTree = new MarginalTree(acg, region);
// outfMarg.println(marginalTree.getRoot() + ";");
// outfMarg.flush();
//
// outfUPGMA.println(upgmaTree.getRoot() + ";");
// outfUPGMA.flush();
assertTrue(topologiesEquivalent(marginalTree.getRoot(), upgmaTree.getRoot()));
}
// outfMarg.close();
// outfUPGMA.close();
}
use of beast.evolution.alignment.Alignment in project bacter by tgvaughan.
the class ACGLikelihoodSlow method createMarginalAlignment.
/**
* Create Alignment object representing alignment of a region
* corresponding to a single marginal tree.
*
* @param alignment
* @param acg
* @param region
* @return
* @throws Exception
*/
public Alignment createMarginalAlignment(Alignment alignment, ConversionGraph acg, Region region) {
List<Sequence> sequences = new ArrayList<>();
for (int leafIdx = 0; leafIdx < alignment.getTaxonCount(); leafIdx++) {
List<Integer> stateSequence;
stateSequence = alignment.getCounts().get(leafIdx).subList(region.leftBoundary, region.rightBoundary);
String taxonName = alignment.getTaxaNames().get(leafIdx);
String charSequence = alignment.getDataType().encodingToString(stateSequence);
sequences.add(new Sequence(taxonName, charSequence));
}
return new Alignment(sequences, alignment.getDataType().getTypeDescription());
}
Aggregations