Revision: 6736
Author: fbielejec
Date: Tue Apr 14 13:25:00 2015 UTC
Log: branch model for testing assignments
https://code.google.com/p/beast-mcmc/source/detail?r=6736
Added:
/trunk/src/dr/app/beagle/evomodel/branchmodel/RandomBranchAssignmentModel.java
/trunk/src/dr/app/beagle/evomodel/parsers/RandomBranchAssignmentModelParser.java
Modified:
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihood.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihoodParser.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTrait.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTraitParser.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperator.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperatorParser.java
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/LineageSpecificBranchModel.java
/trunk/src/dr/app/beast/beagle_parsers.properties
=======================================
--- /dev/null
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/RandomBranchAssignmentModel.java
Tue Apr 14 13:25:00 2015 UTC
@@ -0,0 +1,110 @@
+package dr.app.beagle.evomodel.branchmodel;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+
+import dr.app.beagle.evomodel.substmodel.FrequencyModel;
+import dr.app.beagle.evomodel.substmodel.SubstitutionModel;
+import dr.evolution.tree.NodeRef;
+import dr.evomodel.tree.TreeModel;
+import dr.inference.model.AbstractModel;
+import dr.inference.model.Model;
+import dr.inference.model.Variable;
+import dr.inference.model.Variable.ChangeType;
+import dr.math.MathUtils;
+
+@SuppressWarnings("serial")
+public class RandomBranchAssignmentModel extends AbstractModel implements
BranchModel {
+
+ public static final String RANDOM_BRANCH_ASSIGNMENT_MODEL
= "randomBranchAssignmentModel";
+ private final TreeModel treeModel;
+ private final List<SubstitutionModel> substitutionModels;
+
+// private int[] order;
+ private LinkedHashMap<NodeRef, Integer> branchAssignmentMap;
+
+ public RandomBranchAssignmentModel(TreeModel treeModel,
+ List<SubstitutionModel> substitutionModels) {
+
+ super(RANDOM_BRANCH_ASSIGNMENT_MODEL);
+
+
+ this.treeModel = treeModel;
+ this.substitutionModels = substitutionModels;
+
+ int nodeCount = treeModel.getNodeCount();
+ int nModels = substitutionModels.size();
+
+ // randomly decide order, once and for all
+ branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
+ for (int i = 0; i < nodeCount; i++) {
+
+ NodeRef node = treeModel.getNode(i);
+ int branchClass = MathUtils.nextInt(nModels);
+ branchAssignmentMap.put(node, branchClass);
+
+ }// END: nodes loop
+
+ }//END: Constructor
+
+ @Override
+ public Mapping getBranchModelMapping(NodeRef branch) {
+
+ final int branchClass = branchAssignmentMap.get(branch);
+
+ return new Mapping() {
+ public int[] getOrder() {
+ return new int[] { branchClass };
+ }
+
+ public double[] getWeights() {
+ return new double[] { 1.0 };
+ }
+ };
+ }
+
+ @Override
+ public List<SubstitutionModel> getSubstitutionModels() {
+ return substitutionModels;
+ }
+
+ @Override
+ public SubstitutionModel getRootSubstitutionModel() {
+ int rootClass = branchAssignmentMap.get(treeModel.getRoot());
+ return substitutionModels.get(rootClass);
+ }
+
+ @Override
+ public FrequencyModel getRootFrequencyModel() {
+ return getRootSubstitutionModel().getFrequencyModel();
+ }
+
+ @Override
+ public boolean requiresMatrixConvolution() {
+ return false;
+ }
+
+ @Override
+ protected void handleModelChangedEvent(Model model, Object object, int
index) {
+ fireModelChanged();
+ }
+
+ @Override
+ protected void handleVariableChangedEvent(Variable variable, int index,
+ ChangeType type) {
+ }
+
+ @Override
+ protected void storeState() {
+ }
+
+ @Override
+ protected void restoreState() {
+ }
+
+ @Override
+ protected void acceptState() {
+ }
+
+
+}//END: class
=======================================
--- /dev/null
+++
/trunk/src/dr/app/beagle/evomodel/parsers/RandomBranchAssignmentModelParser.java
Tue Apr 14 13:25:00 2015 UTC
@@ -0,0 +1,72 @@
+package dr.app.beagle.evomodel.parsers;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Logger;
+
+import dr.app.beagle.evomodel.branchmodel.RandomBranchAssignmentModel;
+import dr.app.beagle.evomodel.branchmodel.RandomBranchModel;
+import dr.app.beagle.evomodel.substmodel.SubstitutionModel;
+import dr.evomodel.tree.TreeModel;
+import dr.xml.AbstractXMLObjectParser;
+import dr.xml.ElementRule;
+import dr.xml.XMLObject;
+import dr.xml.XMLParseException;
+import dr.xml.XMLSyntaxRule;
+
+public class RandomBranchAssignmentModelParser extends
AbstractXMLObjectParser {
+
+ public static final String MODELS = "models";
+
+ @Override
+ public String getParserName() {
+ return RandomBranchAssignmentModel.RANDOM_BRANCH_ASSIGNMENT_MODEL;
+ }
+
+ @Override
+ public Object parseXMLObject(XMLObject xo) throws XMLParseException {
+
+ Logger.getLogger("dr.evomodel").info("Using 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);
+
+ }//END: models loop
+
+ return new RandomBranchAssignmentModel(treeModel, substitutionModels);
+ }//END: parseXMLObject
+
+ @Override
+ public XMLSyntaxRule[] getSyntaxRules() {
+
+ return new XMLSyntaxRule[]{
+
+ new ElementRule(TreeModel.class, false), //
+ new ElementRule(MODELS,
+ new XMLSyntaxRule[] {
+ new ElementRule(SubstitutionModel.class,
1, Integer.MAX_VALUE),
+ }
+ )
+
+ };
+ }//END: XMLSyntaxRule
+
+ @Override
+ public String getParserDescription() {
+ return "This element provides a branch model which randomly assigns " +
+ "substitution models to branches on the tree by sampling " +
+ "with replacement from the provided list of substitution models. ";
+ }
+
+ @Override
+ public Class getReturnType() {
+ return RandomBranchModel.class;
+ }
+
+
+}//END: class
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihood.java
Fri Mar 27 15:33:50 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihood.java
Tue Apr 14 13:25:00 2015 UTC
@@ -1,30 +1,26 @@
package dr.app.beagle.evomodel.branchmodel.lineagespecific;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import dr.evolution.tree.NodeRef;
import dr.evomodel.branchratemodel.CountableBranchCategoryProvider;
-import dr.evomodel.newtreelikelihood.TreeLikelihood;
import dr.evomodel.tree.TreeModel;
import dr.inference.loggers.LogColumn;
import dr.inference.loggers.NumberColumn;
-import dr.inference.model.CompoundLikelihood;
import dr.inference.model.Likelihood;
import dr.inference.model.Model;
import dr.inference.model.Parameter;
+@SuppressWarnings("serial")
public class BeagleBranchLikelihood implements Likelihood {
- TreeModel treeModel;
- List<Likelihood> uniqueLikelihoods;
+ private TreeModel treeModel;
+ private List<Likelihood> uniqueLikelihoods;
// size of z:
- List<Likelihood> branchLikelihoods;
- Parameter zParameter;
+ private List<Likelihood> branchLikelihoods;
+ private Parameter categoriesParameter;
private String id = null;
@@ -32,17 +28,20 @@
private CountableBranchCategoryProvider categoriesProvider;
public BeagleBranchLikelihood(TreeModel treeModel,
- List<Likelihood> likelihoods, Parameter zParameter) {
+ List<Likelihood> likelihoods,
+ Parameter categoriesParameter) {
// super(treeLikelihoods);
this.treeModel = treeModel;
this.uniqueLikelihoods = likelihoods;
- this.zParameter = zParameter;
+ this.categoriesParameter = categoriesParameter;
if(this.treeModel != null) {
- this.categoriesProvider = new
CountableBranchCategoryProvider.IndependentBranchCategoryModel(
- treeModel, zParameter);
+
+ this.categoriesProvider = new
CountableBranchCategoryProvider.IndependentBranchCategoryModel( treeModel,
categoriesParameter);
+// this.categoriesProvider = new
CountableBranchCategoryProvider.CladeBranchCategoryModel(treeModel,
categoriesParameter);
+
}
this.branchLikelihoods = getBranchLikelihoods();
@@ -62,7 +61,7 @@
int branchCategory = categoriesProvider.getBranchCategory(
treeModel, branch);
- int zIndex = (int) zParameter
+ int zIndex = (int) categoriesParameter
.getParameterValue(branchCategory);
Likelihood branchLikelihood = uniqueLikelihoods.get(zIndex);
@@ -76,7 +75,7 @@
} else {
- int dim = zParameter.getDimension();
+ int dim = categoriesParameter.getDimension();
if (dim != uniqueLikelihoods.size()) {
throw new RuntimeException("Dimensionality mismatch!");
}
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihoodParser.java
Fri Mar 27 15:33:50 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BeagleBranchLikelihoodParser.java
Tue Apr 14 13:25:00 2015 UTC
@@ -16,9 +16,9 @@
public class BeagleBranchLikelihoodParser extends AbstractXMLObjectParser {
public static final String BEAGLE_BRANCH_LIKELIHOODS
= "beagleBranchLikelihood";
-
+
public static final String UNIQUE_LIKELIHOODS = "uniqueLikelihoods";
-
+
@Override
public String getParserName() {
return BEAGLE_BRANCH_LIKELIHOODS;
@@ -27,26 +27,25 @@
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
- TreeModel treeModel = null;
-
- if(xo.hasChildNamed(TreeModel.TREE_MODEL)) {
- treeModel = (TreeModel) xo.getChild(TreeModel.class);
- }
-
-
-
- Parameter zParameter = (Parameter) xo.getElementFirstChild(
DirichletProcessPriorParser.CATEGORIES);
-
- List<Likelihood> likelihoods = new ArrayList<Likelihood>();
-
- XMLObject cxo = (XMLObject) xo.getChild(UNIQUE_LIKELIHOODS);
- for (int i = 0; i < cxo.getChildCount(); i++) {
-
- Likelihood likelihood = (Likelihood) cxo.getChild(i);
- likelihoods.add(likelihood);
- }
-
-
+ TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
+
+ // if(xo.hasChildNamed(TreeModel.TREE_MODEL)) {
+ //
+ // treeModel = (TreeModel) xo.getChild(TreeModel.class);
+ // }
+
+ Parameter zParameter = (Parameter) xo
+ .getElementFirstChild(DirichletProcessPriorParser.CATEGORIES);
+
+ List<Likelihood> likelihoods = new ArrayList<Likelihood>();
+
+ XMLObject cxo = (XMLObject) xo.getChild(UNIQUE_LIKELIHOODS);
+ for (int i = 0; i < cxo.getChildCount(); i++) {
+
+ Likelihood likelihood = (Likelihood) cxo.getChild(i);
+ likelihoods.add(likelihood);
+ }
+
return new BeagleBranchLikelihood(treeModel, likelihoods, zParameter);
}
@@ -65,6 +64,5 @@
public Class getReturnType() {
return BeagleBranchLikelihood.class;
}
-
}
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTrait.java
Wed Jan 28 15:45:07 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTrait.java
Tue Apr 14 13:25:00 2015 UTC
@@ -7,6 +7,8 @@
import dr.evolution.tree.TreeTrait;
import dr.evolution.tree.TreeTraitProvider;
import dr.evomodel.tree.TreeModel;
+import dr.inference.model.CompoundParameter;
+import dr.inference.model.Parameter;
/**
* @author Filip Bielejec
@@ -18,6 +20,52 @@
private Helper helper;
private TreeModel treeModel;
+ public BranchSpecificTrait(
+ TreeModel treeModel,
+ Parameter uCategories,
+ CompoundParameter uniqueParameters,
+ final String parameterName) {
+
+ this.treeModel = treeModel;
+ helper = new Helper();
+
+ TreeTrait<Double> uTrait = new TreeTrait.D() {
+
+ @Override
+ public String getTraitName() {
+ return parameterName;
+ }
+
+ @Override
+ public dr.evolution.tree.TreeTrait.Intent getIntent() {
+ return Intent.BRANCH;
+ }
+
+ @Override
+ public Double getTrait(Tree tree, NodeRef node) {
+
+ double value = 0.0;
+
+
+
+
+
+
+
+
+
+
+
+ return null;
+ }
+
+
+
+ };
+
+
+ }//END: Constructor
+
public BranchSpecificTrait(
TreeModel treeModel,
final BranchModel branchModel,
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTraitParser.java
Wed Jan 28 15:45:07 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/BranchSpecificTraitParser.java
Tue Apr 14 13:25:00 2015 UTC
@@ -29,7 +29,6 @@
// CompoundParameter parameter = (CompoundParameter)
xo.getChild(CompoundParameter.class);
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
-
return new BranchSpecificTrait(treeModel, branchModel, xo.getId());
}
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperator.java
Fri Mar 27 13:37:00 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperator.java
Tue Apr 14 13:25:00 2015 UTC
@@ -26,7 +26,7 @@
private int mhSteps;
- private Parameter zParameter;
+ private Parameter categoriesParameter;
// private CountableRealizationsParameter countableRealizationsParameter;
private Parameter parameter;
@@ -35,7 +35,7 @@
public DirichletProcessOperator(DirichletProcessPrior dpp,
- Parameter zParameter,
+ Parameter categoriesParameter,
// CountableRealizationsParameter countableRealizationsParameter,
Parameter parameter,
// CompoundLikelihood likelihood;
@@ -46,9 +46,9 @@
this.dpp = dpp;
this.intensity = dpp.getGamma();
this.uniqueRealizationCount = dpp.getCategoryCount();
- this.realizationCount = zParameter.getDimension();
+ this.realizationCount = categoriesParameter.getDimension();
- this.zParameter = zParameter;
+ this.categoriesParameter = categoriesParameter;
// this.countableRealizationsParameter = countableRealizationsParameter;
this.parameter = parameter;
this.likelihood = likelihood;
@@ -59,11 +59,11 @@
}// END: Constructor
public Parameter getParameter() {
- return zParameter;
+ return categoriesParameter;
}//END: getParameter
public Variable getVariable() {
- return zParameter;
+ return categoriesParameter;
}//END: getVariable
@Override
@@ -94,7 +94,7 @@
if (i != index) {
- int j = (int) zParameter.getParameterValue(i);
+ int j = (int) categoriesParameter.getParameterValue(i);
occupancy[j]++;
}// END: i check
@@ -154,7 +154,7 @@
// sample
int sampledCluster = MathUtils.randomChoicePDF(clusterProbs);
- zParameter.setParameterValue(index, sampledCluster);
+ categoriesParameter.setParameterValue(index, sampledCluster);
if (DEBUG) {
System.out.println("sampled category: " + sampledCluster + "\n");
@@ -170,7 +170,7 @@
Likelihood dl = (Likelihood) likelihood .getLikelihood(index);
- int category = (int) zParameter.getParameterValue(index);
+ int category = (int) categoriesParameter.getParameterValue(index);
double value = parameter.getParameterValue(category);
double loglike = 0.0;
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperatorParser.java
Fri Mar 27 15:33:50 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/DirichletProcessOperatorParser.java
Tue Apr 14 13:25:00 2015 UTC
@@ -21,7 +21,7 @@
DirichletProcessPrior dpp = (DirichletProcessPrior)
xo.getChild(DirichletProcessPrior.class);
// CompoundLikelihood likelihood = (CompoundLikelihood)
xo .getElementFirstChild(DATA_LOG_LIKELIHOOD);
BeagleBranchLikelihood likelihood = (BeagleBranchLikelihood)
xo .getElementFirstChild(DATA_LOG_LIKELIHOOD);
- Parameter zParameter = (Parameter) xo.getElementFirstChild(
DirichletProcessPriorParser.CATEGORIES);
+ Parameter categoriesParameter = (Parameter) xo.getElementFirstChild(
DirichletProcessPriorParser.CATEGORIES);
// CountableRealizationsParameter countableRealizationsParameter =
(CountableRealizationsParameter)
xo.getChild(CountableRealizationsParameter.class);
Parameter uniquelyRealizedParameters = (Parameter)
xo.getChild(Parameter.class);
@@ -29,7 +29,7 @@
int M = xo.getIntegerAttribute(MH_STEPS);
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
- return new DirichletProcessOperator(dpp, zParameter,
uniquelyRealizedParameters,
+ return new DirichletProcessOperator(dpp, categoriesParameter,
uniquelyRealizedParameters,
// countableRealizationsParameter,
likelihood, M, weight);
}// END: parseXMLObject
=======================================
---
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/LineageSpecificBranchModel.java
Tue Jan 20 18:56:28 2015 UTC
+++
/trunk/src/dr/app/beagle/evomodel/branchmodel/lineagespecific/LineageSpecificBranchModel.java
Tue Apr 14 13:25:00 2015 UTC
@@ -82,22 +82,22 @@
private FrequencyModel rootFrequencyModel;
// for discrete categories
- private CountableBranchCategoryProvider uCategoriesProvider;
- private Parameter uCategoriesParameter;
+ private CountableBranchCategoryProvider categoriesProvider;
+ private Parameter categoriesParameter;
public LineageSpecificBranchModel(TreeModel treeModel, //
FrequencyModel rootFrequencyModel, //
final List<SubstitutionModel> substitutionModels, //
- CountableBranchCategoryProvider uCategoriesProvider, //
- Parameter uCategoriesParameter //
+ CountableBranchCategoryProvider categoriesProvider, //
+ Parameter categoriesParameter //
) {
super("");
this.treeModel = treeModel;
this.substitutionModels = substitutionModels;
- this.uCategoriesProvider = uCategoriesProvider;
- this.uCategoriesParameter = uCategoriesParameter;
+ this.categoriesProvider = categoriesProvider;
+ this.categoriesParameter = categoriesParameter;
this.rootFrequencyModel = rootFrequencyModel;
this.nodeMap = new HashMap<NodeRef, Mapping>();
@@ -108,8 +108,8 @@
addModel(this.treeModel);
addModel(this.rootFrequencyModel);
- addModel((Model) this.uCategoriesProvider);
- addVariable(this.uCategoriesParameter);
+ addModel((Model) this.categoriesProvider);
+ addVariable(this.categoriesParameter);
}// END: Constructor
@@ -127,9 +127,9 @@
if (branch != treeModel.getRoot()) {//TODO: neccessary?
- int branchCategory = uCategoriesProvider.getBranchCategory(
+ int branchCategory = categoriesProvider.getBranchCategory(
treeModel, branch);
- final int uCategory = (int)
uCategoriesParameter.getParameterValue(branchCategory);
+ final int uCategory = (int)
categoriesParameter.getParameterValue(branchCategory);
if (DEBUG) {
System.out.println("branch length: " +
treeModel.getBranchLength(branch) + ", " + "category:" + uCategory);
=======================================
--- /trunk/src/dr/app/beast/beagle_parsers.properties Sat Mar 28 00:43:47
2015 UTC
+++ /trunk/src/dr/app/beast/beagle_parsers.properties Tue Apr 14 13:25:00
2015 UTC
@@ -89,6 +89,7 @@
dr.app.beagle.evomodel.branchmodel.lineagespecific.LineageSpecificBranchModelParser
dr.app.beagle.evomodel.branchmodel.lineagespecific.BranchSpecificTraitParser
dr.app.beagle.evomodel.parsers.RandomBranchModelParser
+dr.app.beagle.evomodel.parsers.RandomBranchAssignmentModelParser
dr.app.beagle.evomodel.branchmodel.lineagespecific.BeagleBranchLikelihoodParser