| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.branchratemodel; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.continuous.SampledMultivariateTraitLikelihood; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.evomodelxml.branchratemodel.ContinuousTraitBranchRateModelParser; |
| 33 |
import dr.inference.model.Model; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.inference.model.Variable; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class ContinuousTraitBranchRateModel extends AbstractBranchRateModel { |
| 44 |
|
| 45 |
private final String trait; |
| 46 |
private final int dimension; |
| 47 |
private final Parameter rateParameter; |
| 48 |
private final Parameter ratioParameter; |
| 49 |
private SampledMultivariateTraitLikelihood traitLikelihood; |
| 50 |
|
| 51 |
public ContinuousTraitBranchRateModel(SampledMultivariateTraitLikelihood traitLikelihood, int dimension) { |
| 52 |
super(ContinuousTraitBranchRateModelParser.TRAIT_BRANCH_RATES); |
| 53 |
|
| 54 |
this.traitLikelihood = traitLikelihood; |
| 55 |
this.trait = traitLikelihood.getTraitName(); |
| 56 |
this.dimension = dimension; |
| 57 |
|
| 58 |
this.rateParameter = null; |
| 59 |
this.ratioParameter = null; |
| 60 |
addModel(traitLikelihood); |
| 61 |
} |
| 62 |
|
| 63 |
public ContinuousTraitBranchRateModel(String trait, Parameter rateParameter, Parameter ratioParameter) { |
| 64 |
super(ContinuousTraitBranchRateModelParser.TRAIT_BRANCH_RATES); |
| 65 |
|
| 66 |
this.trait = trait; |
| 67 |
dimension = 0; |
| 68 |
this.rateParameter = rateParameter; |
| 69 |
this.ratioParameter = ratioParameter; |
| 70 |
|
| 71 |
if (rateParameter != null) { |
| 72 |
addVariable(rateParameter); |
| 73 |
} |
| 74 |
|
| 75 |
if (ratioParameter != null) { |
| 76 |
addVariable(ratioParameter); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 81 |
fireModelChanged(); |
| 82 |
} |
| 83 |
|
| 84 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 85 |
fireModelChanged(); |
| 86 |
} |
| 87 |
|
| 88 |
protected void storeState() { |
| 89 |
} |
| 90 |
|
| 91 |
protected void restoreState() { |
| 92 |
} |
| 93 |
|
| 94 |
protected void acceptState() { |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public double getBranchRate(final Tree tree, final NodeRef node) { |
| 99 |
NodeRef parent = tree.getParent(node); |
| 100 |
if (parent == null) { |
| 101 |
throw new IllegalArgumentException("Root does not have a valid rate"); |
| 102 |
} |
| 103 |
|
| 104 |
double rate = 1.0; |
| 105 |
TreeModel treeModel = (TreeModel) tree; |
| 106 |
|
| 107 |
if (rateParameter != null) { |
| 108 |
double scale = 1.0; |
| 109 |
double ratio = 1.0; |
| 110 |
|
| 111 |
if (rateParameter != null) { |
| 112 |
scale = rateParameter.getParameterValue(0); |
| 113 |
} |
| 114 |
|
| 115 |
if (ratioParameter != null) { |
| 116 |
ratio = ratioParameter.getParameterValue(0); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
double rate1 = ratio * treeModel.getMultivariateNodeTrait(node, trait)[0]; |
| 122 |
double rate2 = ratio * treeModel.getMultivariateNodeTrait(parent, trait)[0]; |
| 123 |
|
| 124 |
if (rate1 == rate2) { |
| 125 |
return scale * Math.exp(rate1); |
| 126 |
} |
| 127 |
|
| 128 |
rate = scale * (Math.exp(rate2) - Math.exp(rate1)) / (rate2 - rate1); |
| 129 |
} else { |
| 130 |
double rate1 = treeModel.getMultivariateNodeTrait(node, trait)[dimension]; |
| 131 |
double rate2 = treeModel.getMultivariateNodeTrait(parent, trait)[dimension]; |
| 132 |
|
| 133 |
if (rate1 == rate2) { |
| 134 |
return Math.exp(rate1); |
| 135 |
} |
| 136 |
|
| 137 |
rate = (Math.exp(rate2) - Math.exp(rate1)) / (rate2 - rate1); |
| 138 |
} |
| 139 |
|
| 140 |
return rate; |
| 141 |
} |
| 142 |
|
| 143 |
} |