use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.
the class IndependentCoalescentSampler method doOperation.
/**
* change the parameter and return the hastings ratio.
*/
public double doOperation() {
CoalescentSimulator simulator = new CoalescentSimulator();
List<TaxonList> taxonLists = new ArrayList<TaxonList>();
double rootHeight = -1.0;
double oldLikelihood = 0.0;
double newLikelihood = 0.0;
// should have one child that is node
for (int i = 0; i < xo.getChildCount(); i++) {
final Object child = xo.getChild(i);
// careful: Trees are TaxonLists ... (AER); see OldCoalescentSimulatorParser
if (child instanceof Tree) {
// do nothing
} else if (child instanceof TaxonList) {
// taxonLists.add((TaxonList) child);
taxonLists.add((Taxa) child);
// taxa added
break;
}
}
try {
Tree[] trees = new Tree[taxonLists.size()];
// simulate each taxonList separately
for (int i = 0; i < taxonLists.size(); i++) {
trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
}
oldLikelihood = coalescent.getLogLikelihood();
SimpleTree simTree = simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
// this would be the normal way to do it
treeModel.beginTreeEdit();
// now it's allowed to adjust the tree structure
treeModel.adoptTreeStructure(simTree);
// endTreeEdit() would then fire the events
treeModel.endTreeEdit();
newLikelihood = coalescent.getLogLikelihood();
} catch (IllegalArgumentException iae) {
try {
throw new XMLParseException(iae.getMessage());
} catch (XMLParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return oldLikelihood - newLikelihood;
}
use of dr.xml.XMLParseException in project beast-mcmc by beast-dev.
the class EpochBranchModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing multi-epoch branch model.");
MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(MutableTreeModel.class);
SubstitutionModel ancestralSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
List<Epoch> epochs = new ArrayList<Epoch>();
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(EPOCH)) {
Parameter tt = null;
if (xoc.hasAttribute(TRANSITION_TIME)) {
double t = xoc.getAttribute(TRANSITION_TIME, 0.0);
tt = new Parameter.Default(1, t);
}
SubstitutionModel s = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
if (xoc.hasChildNamed(TRANSITION_TIME)) {
if (tt != null) {
throw new XMLParseException("An epoch cannot have a transitionTime attribute and a parameter");
}
tt = (Parameter) xoc.getElementFirstChild(TRANSITION_TIME);
}
epochs.add(new Epoch(s, tt));
}
}
}
Collections.sort(epochs);
List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
CompoundParameter transitionTimes = new CompoundParameter("epochTimes");
for (Epoch epoch : epochs) {
substitutionModels.add(epoch.substitutionModel);
transitionTimes.addParameter(epoch.timeParameter);
}
substitutionModels.add(ancestralSubstitutionModel);
return new EpochBranchModel(treeModel, substitutionModels, transitionTimes);
}
Aggregations