Search in sources :

Example 6 with SubstitutionModel

use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.

the class BranchAssignmentModelParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing branch assignment branch model.");
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    String annotation = xo.getStringAttribute(ANNOTATION);
    LinkedHashMap<Integer, SubstitutionModel> modelIndexMap = new LinkedHashMap<Integer, SubstitutionModel>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject xoc = (XMLObject) xo.getChild(i);
            if (xoc.getName().equals(ASSIGNMENT)) {
                Integer index = null;
                if (xoc.hasAttribute(ANNOTATION_VALUE)) {
                    index = xoc.getIntegerAttribute(ANNOTATION_VALUE);
                }
                SubstitutionModel model = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                modelIndexMap.put(index, model);
            }
        }
    }
    SubstitutionModel baseModel = (SubstitutionModel) xo.getElementFirstChild(BASE_MODEL);
    return new BranchAssignmentModel(treeModel, annotation, modelIndexMap, baseModel);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) BranchAssignmentModel(dr.evomodel.branchmodel.BranchAssignmentModel) XMLObject(dr.xml.XMLObject) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with SubstitutionModel

use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.

the class MarkovJumpsBeagleTreeLikelihood method addRegister.

public void addRegister(Parameter addRegisterParameter, MarkovJumpsType type, boolean scaleByTime) {
    if ((type == MarkovJumpsType.COUNTS && addRegisterParameter.getDimension() != stateCount * stateCount) || (type == MarkovJumpsType.REWARDS && addRegisterParameter.getDimension() != stateCount)) {
        throw new RuntimeException("Register parameter of wrong dimension");
    }
    addVariable(addRegisterParameter);
    final String tag = addRegisterParameter.getId();
    for (int i = 0; i < substitutionModelDelegate.getSubstitutionModelCount(); ++i) {
        boolean isEpochModel = branchModel instanceof EpochBranchModel;
        registerParameter.add(addRegisterParameter);
        MarkovJumpsSubstitutionModel mjModel;
        SubstitutionModel substitutionModel = substitutionModelDelegate.getSubstitutionModel(i);
        if (useUniformization) {
            mjModel = new UniformizedSubstitutionModel(substitutionModel, type, nSimulants);
        } else {
            if (type == MarkovJumpsType.HISTORY) {
                throw new RuntimeException("Can only report complete history using uniformization");
            }
            mjModel = new MarkovJumpsSubstitutionModel(substitutionModel, type);
        }
        markovjumps.add(mjModel);
        branchModelNumber.add(i);
        addModel(mjModel);
        setupRegistration(numRegisters);
        String traitName;
        if (substitutionModelDelegate.getSubstitutionModelCount() == 1) {
            traitName = tag;
        } else {
            traitName = tag + i;
        }
        jumpTag.add(traitName);
        expectedJumps.add(new double[treeModel.getNodeCount()][patternCount]);
        // storedExpectedJumps.add(new double[treeModel.getNodeCount()][patternCount]);
        boolean[] oldScaleByTime = this.scaleByTime;
        int oldScaleByTimeLength = (oldScaleByTime == null ? 0 : oldScaleByTime.length);
        this.scaleByTime = new boolean[oldScaleByTimeLength + 1];
        if (oldScaleByTimeLength > 0) {
            System.arraycopy(oldScaleByTime, 0, this.scaleByTime, 0, oldScaleByTimeLength);
        }
        this.scaleByTime[oldScaleByTimeLength] = scaleByTime;
        if (type != MarkovJumpsType.HISTORY) {
            TreeTrait.DA da = new TreeTrait.DA() {

                final int registerNumber = numRegisters;

                public String getTraitName() {
                    return tag;
                }

                public Intent getIntent() {
                    return Intent.BRANCH;
                }

                public double[] getTrait(Tree tree, NodeRef node) {
                    return getMarkovJumpsForNodeAndRegister(tree, node, registerNumber);
                }
            };
            treeTraits.addTrait(traitName + "_base", da);
            treeTraits.addTrait(addRegisterParameter.getId(), new TreeTrait.SumAcrossArrayD(new TreeTrait.SumOverTreeDA(da)));
        } else {
            if (i == 0 || !isEpochModel) {
                if (histories == null) {
                    histories = new String[treeModel.getNodeCount()][patternCount];
                } else {
                    throw new RuntimeException("Only one complete history per markovJumpTreeLikelihood is allowed");
                }
                if (nSimulants > 1) {
                    throw new RuntimeException("Only one simulant allowed when saving complete history");
                }
                // Add total number of changes over tree trait
                TreeTrait da = new TreeTrait.DA() {

                    final int registerNumber = numRegisters;

                    public String getTraitName() {
                        return tag;
                    }

                    public Intent getIntent() {
                        return Intent.BRANCH;
                    }

                    public double[] getTrait(Tree tree, NodeRef node) {
                        return getMarkovJumpsForNodeAndRegister(tree, node, registerNumber);
                    }
                };
                treeTraits.addTrait(addRegisterParameter.getId(), new TreeTrait.SumOverTreeDA(da));
                // Record the complete history for this register
                historyRegisterNumber = numRegisters;
                ((UniformizedSubstitutionModel) mjModel).setSaveCompleteHistory(true);
                if (useCompactHistory && logHistory) {
                    treeTraits.addTrait(ALL_HISTORY, new TreeTrait.SA() {

                        public String getTraitName() {
                            return ALL_HISTORY;
                        }

                        public Intent getIntent() {
                            return Intent.BRANCH;
                        }

                        public boolean getFormatAsArray() {
                            return true;
                        }

                        public String[] getTrait(Tree tree, NodeRef node) {
                            List<String> events = new ArrayList<String>();
                            for (int i = 0; i < patternCount; i++) {
                                String eventString = getHistoryForNode(tree, node, i);
                                if (eventString != null && eventString.compareTo("{}") != 0) {
                                    eventString = eventString.substring(1, eventString.length() - 1);
                                    if (eventString.contains("},{")) {
                                        // There are multiple events
                                        String[] elements = eventString.split("(?<=\\}),(?=\\{)");
                                        for (String e : elements) {
                                            events.add(e);
                                        }
                                    } else {
                                        events.add(eventString);
                                    }
                                }
                            }
                            String[] array = new String[events.size()];
                            events.toArray(array);
                            return array;
                        }

                        public boolean getLoggable() {
                            return true;
                        }
                    });
                }
                for (int site = 0; site < patternCount; ++site) {
                    final String anonName = (patternCount == 1) ? HISTORY : HISTORY + "_" + (site + 1);
                    final int anonSite = site;
                    treeTraits.addTrait(anonName, new TreeTrait.S() {

                        public String getTraitName() {
                            return anonName;
                        }

                        public Intent getIntent() {
                            return Intent.BRANCH;
                        }

                        public String getTrait(Tree tree, NodeRef node) {
                            String history = getHistoryForNode(tree, node, anonSite);
                            // Return null if empty
                            return (history.compareTo("{}") != 0) ? history : null;
                        }

                        public boolean getLoggable() {
                            return logHistory && !useCompactHistory;
                        }
                    });
                }
            }
            if (isEpochModel) {
                for (int j = 0; j < markovjumps.size(); ++j) {
                    ((UniformizedSubstitutionModel) markovjumps.get(j)).setSaveCompleteHistory(true);
                }
            }
        }
        numRegisters++;
    }
// End of loop over branch models
}
Also used : EpochBranchModel(dr.evomodel.branchmodel.EpochBranchModel) MarkovJumpsSubstitutionModel(dr.evomodel.substmodel.MarkovJumpsSubstitutionModel) UniformizedSubstitutionModel(dr.evomodel.substmodel.UniformizedSubstitutionModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) UniformizedSubstitutionModel(dr.evomodel.substmodel.UniformizedSubstitutionModel) TreeTrait(dr.evolution.tree.TreeTrait) NodeRef(dr.evolution.tree.NodeRef) Tree(dr.evolution.tree.Tree) PatternList(dr.evolution.alignment.PatternList) MarkovJumpsSubstitutionModel(dr.evomodel.substmodel.MarkovJumpsSubstitutionModel)

Example 8 with SubstitutionModel

use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.

the class BranchSpecificBranchModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing clade-specific branch model.");
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    SubstitutionModel substitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    BranchSpecificBranchModel branchModel = new BranchSpecificBranchModel(tree, substitutionModel);
    for (int i = 0; i < xo.getChildCount(); i++) {
        if (xo.getChild(i) instanceof XMLObject) {
            XMLObject xoc = (XMLObject) xo.getChild(i);
            if (xoc.getName().equals(CLADE)) {
                double stemWeight = xoc.getAttribute(STEM_WEIGHT, 0.0);
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                if (taxonList.getTaxonCount() == 1) {
                    throw new XMLParseException("A clade must be defined by at least two taxa");
                }
                try {
                    branchModel.addClade(taxonList, substitutionModel, stemWeight);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            } else if (xoc.getName().equals(EXTERNAL_BRANCHES)) {
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                try {
                    branchModel.addExternalBranches(taxonList, substitutionModel);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            } else if (xoc.getName().equals(BACKBONE)) {
                substitutionModel = (SubstitutionModel) xoc.getChild(SubstitutionModel.class);
                TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
                try {
                    branchModel.addBackbone(taxonList, substitutionModel);
                } catch (TreeUtils.MissingTaxonException mte) {
                    throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
                }
            }
        }
    }
    return branchModel;
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) TaxonList(dr.evolution.util.TaxonList) BranchSpecificBranchModel(dr.evomodel.branchmodel.BranchSpecificBranchModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel) TreeUtils(dr.evolution.tree.TreeUtils)

Example 9 with SubstitutionModel

use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.

the class ExternalInternalBranchModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing external-internal branch model.");
    TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
    SubstitutionModel internalSubstitutionModel = (SubstitutionModel) xo.getChild(SubstitutionModel.class);
    SubstitutionModel externalSubstitutionModel = (SubstitutionModel) xo.getElementFirstChild(EXTERNAL_BRANCHES);
    return new ExternalInternalBranchModel(tree, externalSubstitutionModel, internalSubstitutionModel);
}
Also used : ExternalInternalBranchModel(dr.evomodel.branchmodel.ExternalInternalBranchModel) TreeModel(dr.evomodel.tree.TreeModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Example 10 with SubstitutionModel

use of dr.evomodel.substmodel.SubstitutionModel in project beast-mcmc by beast-dev.

the class RandomBranchAssignmentModelParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    Logger.getLogger("dr.evomodel").info("\nUsing random assignment branch model.");
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    XMLObject cxo = xo.getChild(MODELS);
    List<SubstitutionModel> substitutionModels = new ArrayList<SubstitutionModel>();
    for (int i = 0; i < cxo.getChildCount(); i++) {
        SubstitutionModel substModel = (SubstitutionModel) cxo.getChild(i);
        substitutionModels.add(substModel);
    }
    return new RandomBranchAssignmentModel(treeModel, substitutionModels);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) ArrayList(java.util.ArrayList) XMLObject(dr.xml.XMLObject) RandomBranchAssignmentModel(dr.evomodel.branchmodel.RandomBranchAssignmentModel) SubstitutionModel(dr.evomodel.substmodel.SubstitutionModel)

Aggregations

SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)25 TreeModel (dr.evomodel.tree.TreeModel)13 Parameter (dr.inference.model.Parameter)13 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)11 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)9 ArrayList (java.util.ArrayList)9 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)8 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)8 BranchModel (dr.evomodel.branchmodel.BranchModel)7 XMLObject (dr.xml.XMLObject)6 PatternList (dr.evolution.alignment.PatternList)5 Partition (dr.app.beagle.tools.Partition)4 NodeRef (dr.evolution.tree.NodeRef)4 Tree (dr.evolution.tree.Tree)4 TaxonList (dr.evolution.util.TaxonList)4 EpochBranchModel (dr.evomodel.branchmodel.EpochBranchModel)4 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)4 TipStatesModel (dr.evomodel.tipstatesmodel.TipStatesModel)4 BeagleTreeLikelihood (dr.evomodel.treelikelihood.BeagleTreeLikelihood)4 PartialsRescalingScheme (dr.evomodel.treelikelihood.PartialsRescalingScheme)4