use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class StarTreeModelParser method parseXMLObject.
/**
* @return a tree object based on the XML element it was passed.
*/
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Tree tree = (Tree) xo.getChild(Tree.class);
StarTreeModel treeModel = new StarTreeModel(xo.getId(), tree);
Logger.getLogger("dr.evomodel").info("Creating a star tree model, '" + xo.getId() + "'");
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject cxo = (XMLObject) xo.getChild(i);
if (cxo.getName().equals(ROOT_HEIGHT)) {
if (cxo.getRawChild(0) instanceof Reference) {
// treeModel.setRootHeightParameter((Parameter) cxo.getChild(Parameter.class));
throw new XMLParseException("Can not provide idref to a new root height parameter");
} else {
ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
}
} else if (cxo.getName().equals(SHARE_ROOT)) {
DefaultTreeModel sharedRoot = (DefaultTreeModel) cxo.getChild(DefaultTreeModel.class);
treeModel.setSharedRootHeightParameter(sharedRoot);
} else if (cxo.getName().equals(LEAF_HEIGHT)) {
String taxonName;
if (cxo.hasAttribute(TAXON)) {
taxonName = cxo.getStringAttribute(TAXON);
} else {
throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
}
int index = treeModel.getTaxonIndex(taxonName);
if (index == -1) {
throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
}
NodeRef node = treeModel.getExternalNode(index);
ParameterParser.replaceParameter(cxo, treeModel.getLeafHeightParameter(node));
// } else if (cxo.getName().equals(NODE_HEIGHTS)) {
//
// boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
// boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
// boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
//
// if (!rootNode && !internalNodes && !leafNodes) {
// throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
// }
//
// ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
//
// } else if (cxo.getName().equals(NODE_RATES)) {
//
// boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
// boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
// boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
// double[] initialValues = null;
//
// if (cxo.hasAttribute(INITIAL_VALUE)) {
// initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
// }
//
// if (!rootNode && !internalNodes && !leafNodes) {
// throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
// }
//
// ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
//
// } else if (cxo.getName().equals(NODE_TRAITS)) {
//
// boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
// boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
// boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
// boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false);
// String name = cxo.getAttribute(NAME, "trait");
// int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1);
//
// double[] initialValues = null;
// if (cxo.hasAttribute(INITIAL_VALUE)) {
// initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
// }
//
// if (!rootNode && !internalNodes && !leafNodes) {
// throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element");
// }
//
// ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents));
} else if (cxo.getName().equals(LEAF_TRAIT)) {
String name = cxo.getAttribute(NAME, "trait");
String taxonName;
if (cxo.hasAttribute(TAXON)) {
taxonName = cxo.getStringAttribute(TAXON);
} else {
throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
}
int index = treeModel.getTaxonIndex(taxonName);
if (index == -1) {
throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
}
NodeRef node = treeModel.getExternalNode(index);
Parameter parameter = treeModel.getNodeTraitParameter(node, name);
if (parameter == null)
throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
ParameterParser.replaceParameter(cxo, parameter);
} else {
throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
}
} else if (xo.getChild(i) instanceof Tree) {
// do nothing - already handled
} else {
throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
}
}
Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
return treeModel;
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class NNITest method testDoOperation.
/**
* Test method for {@link SimpleMCMCOperator#doOperation()}.
* @throws ImportException
* @throws IOException
*/
public void testDoOperation() throws IOException, ImportException {
// probability of picking B node is 1/(2n-4) = 1/6
// probability of swapping it with C is 1/1
// total = 1/6
System.out.println("Test 1: Forward");
String treeMatch = "((((A,C),B),D),E);";
int count = 0;
int reps = 100000;
for (int i = 0; i < reps; i++) {
TreeModel treeModel = new DefaultTreeModel("treeModel", tree5);
NNI operator = new NNI(treeModel, 1);
operator.doOperation();
String tree = TreeUtils.newickNoLengths(treeModel);
if (tree.equals(treeMatch)) {
count += 1;
}
}
double p_1 = (double) count / (double) reps;
System.out.println("Number of proposals:\t" + count);
System.out.println("Number of tries:\t" + reps);
System.out.println("Number of ratio:\t" + p_1);
System.out.println("Number of expected ratio:\t" + 1.0 / 6.0);
assertExpectation(1.0 / 6.0, p_1, reps);
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class GibbsSubtreeSwapTestProblem method testDoOperation.
/**
* Test method for {@link dr.evomodel.operators.GibbsSubtreeSwap}.
* @throws ImportException
* @throws IOException
*/
public void testDoOperation() throws IOException, ImportException {
// assumes that the posterior is equal for all trees!!!
// probability of picking (A,B) node is 1/(2n-3) = 1/7
// probability of swapping with D is 1/2
// total = 1/14
// probability of picking {D} node is 1/(2n-3) = 1/7
// probability of picking {A,B} is 1/5
// total = 1/35
// total = 1/14 + 1/35 = 7/70 = 0.1
// now we calculate the same for the backward proposal
// this is needed for the Hastings ratio
// probability of picking (A,B) node is 1/(2n-3) = 1/7
// probability of swapping with D is 1/3
// total = 1/21
// probability of picking {D} node is 1/(2n-3) = 1/7
// probability of picking {A,B} is 1/4
// total = 1/28
// total = 1/21 + 1/28 = 4/84 + 3/84 = 7/84 = 1/12
System.out.println("Test 1: Forward");
String treeMatch = "(((D,C),(A,B)),E);";
int count = 0;
int reps = 100000;
for (int i = 0; i < reps; i++) {
DefaultTreeModel treeModel = new DefaultTreeModel("treeModel", tree5);
GibbsSubtreeSwap operator = new GibbsSubtreeSwap(treeModel, false, 1.0);
double hr = operator.operate(null);
String tree = TreeUtils.newickNoLengths(treeModel);
if (tree.equals(treeMatch)) {
// System.out.println("Expected Hastings ratio = " + 5.0/6.0 + " in log = " + Math.log(5.0/6.0));
// System.out.println("Obtained Hastings ratio = " + Math.exp(hr) + " in log = " + hr);
TestCase.assertEquals(Math.log(5.0 / 6.0), hr, 0.00000001);
count += 1;
}
}
double p_1 = (double) count / (double) reps;
System.out.println("Number of proposals:\t" + count);
System.out.println("Number of tries:\t" + reps);
System.out.println("Number of ratio:\t" + p_1);
System.out.println("Number of expected ratio:\t" + 0.1);
assertExpectation(0.1, p_1, reps);
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class AncestralStateTreeLikelihoodTest method testJointLikelihood.
public void testJointLikelihood() {
TreeModel treeModel = new DefaultTreeModel("treeModel", tree);
Sequence[] sequence = new Sequence[3];
sequence[0] = new Sequence(new Taxon("0"), "A");
sequence[1] = new Sequence(new Taxon("1"), "C");
sequence[2] = new Sequence(new Taxon("2"), "C");
Taxa taxa = new Taxa();
for (Sequence s : sequence) {
taxa.addTaxon(s.getTaxon());
}
SimpleAlignment alignment = new SimpleAlignment();
for (Sequence s : sequence) {
alignment.addSequence(s);
}
Parameter mu = new Parameter.Default(1, 1.0);
Parameter kappa = new Parameter.Default(1, 1.0);
double[] pi = { 0.25, 0.25, 0.25, 0.25 };
Parameter freqs = new Parameter.Default(pi);
FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs);
HKY hky = new HKY(kappa, f);
AncestralStateTreeLikelihood treeLikelihood = new AncestralStateTreeLikelihood(alignment, treeModel, new GammaSiteModel(hky), new StrictClockBranchRates(mu), false, true, Nucleotides.INSTANCE, "state", false, // useMap = true
true, false);
double logLike = treeLikelihood.getLogLikelihood();
StringBuffer buffer = new StringBuffer();
TreeUtils.newick(treeModel, treeModel.getRoot(), false, TreeUtils.BranchLengthType.LENGTHS_AS_TIME, null, null, new TreeTraitProvider[] { treeLikelihood }, null, buffer);
System.out.println(buffer);
System.out.println("t_CA(2) = " + t(false, 2.0));
System.out.println("t_CC(1) = " + t(true, 1.0));
double trueValue = 0.25 * t(false, 2.0) * Math.pow(t(true, 1.0), 3.0);
assertEquals(logLike, Math.log(trueValue), 1e-6);
}
use of dr.evomodel.tree.DefaultTreeModel in project beast-mcmc by beast-dev.
the class MsatFullLikelihoodTest method setUpExample3.
private void setUpExample3() throws Exception {
// taxa
ArrayList<Taxon> taxonList3 = new ArrayList<Taxon>();
Collections.addAll(taxonList3, new Taxon("taxon1"), new Taxon("taxon2"), new Taxon("taxon3"), new Taxon("taxon4"), new Taxon("taxon5"), new Taxon("taxon6"), new Taxon("taxon7"));
Taxa taxa3 = new Taxa(taxonList3);
// msat datatype
Microsatellite msat = new Microsatellite(1, 4);
Patterns msatPatterns = new Patterns(msat, taxa3);
// pattern in the correct code form.
msatPatterns.addPattern(new int[] { 0, 3, 1, 2, 3, 0, 1 });
// create tree
NewickImporter importer = new NewickImporter("(((taxon1:0.3,taxon2:0.3):0.6,taxon3:0.9):0.9,((taxon4:0.5,taxon5:0.5):0.3,(taxon6:0.7,taxon7:0.7):0.1):1.0);");
Tree tree = importer.importTree(null);
// treeModel
TreeModel treeModel = new DefaultTreeModel(tree);
// msatsubstModel
AsymmetricQuadraticModel aqm3 = new AsymmetricQuadraticModel(msat, null);
// siteModel
GammaSiteModel siteModel = new GammaSiteModel(aqm3);
// treeLikelihood
treeLikelihood3 = new TreeLikelihood(msatPatterns, treeModel, siteModel, null, null, false, false, true, false, false);
}
Aggregations