Search in sources :

Example 6 with XMLParseException

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;
}
Also used : TaxonList(dr.evolution.util.TaxonList) ArrayList(java.util.ArrayList) SimpleTree(dr.evolution.tree.SimpleTree) CoalescentSimulator(dr.evomodel.coalescent.CoalescentSimulator) Taxa(dr.evolution.util.Taxa) SimpleTree(dr.evolution.tree.SimpleTree) Tree(dr.evolution.tree.Tree) XMLObject(dr.xml.XMLObject) XMLParseException(dr.xml.XMLParseException)

Example 7 with XMLParseException

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);
}
Also used : EpochBranchModel(dr.evomodel.branchmodel.EpochBranchModel) ArrayList(java.util.ArrayList) XMLObject(dr.xml.XMLObject) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) CompoundParameter(dr.inference.model.CompoundParameter) MutableTreeModel(dr.evolution.tree.MutableTreeModel) CompoundParameter(dr.inference.model.CompoundParameter) Parameter(dr.inference.model.Parameter) XMLParseException(dr.xml.XMLParseException)

Aggregations

XMLParseException (dr.xml.XMLParseException)7 XMLObject (dr.xml.XMLObject)5 Parameter (dr.inference.model.Parameter)3 ArrayList (java.util.ArrayList)3 DataType (dr.evolution.datatype.DataType)2 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)2 Partition (dr.app.beagle.tools.Partition)1 Sequence (dr.evolution.sequence.Sequence)1 MutableTreeModel (dr.evolution.tree.MutableTreeModel)1 SimpleTree (dr.evolution.tree.SimpleTree)1 Tree (dr.evolution.tree.Tree)1 Taxa (dr.evolution.util.Taxa)1 TaxonList (dr.evolution.util.TaxonList)1 BranchModel (dr.evomodel.branchmodel.BranchModel)1 EpochBranchModel (dr.evomodel.branchmodel.EpochBranchModel)1 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)1 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)1 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)1 CoalescentSimulator (dr.evomodel.coalescent.CoalescentSimulator)1 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)1