| 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.treedatalikelihood.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 31 |
import dr.evomodel.continuous.MultivariateDiffusionModel; |
| 32 |
import dr.inference.model.Model; |
| 33 |
|
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class DriftDiffusionModelDelegate extends AbstractDiffusionModelDelegate { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private final int dim; |
| 46 |
private final List<BranchRateModel> branchRateModels; |
| 47 |
|
| 48 |
public DriftDiffusionModelDelegate(Tree tree, |
| 49 |
MultivariateDiffusionModel diffusionModel, |
| 50 |
List<BranchRateModel> branchRateModels) { |
| 51 |
this(tree, diffusionModel, branchRateModels, 0); |
| 52 |
} |
| 53 |
|
| 54 |
private DriftDiffusionModelDelegate(Tree tree, |
| 55 |
MultivariateDiffusionModel diffusionModel, |
| 56 |
List<BranchRateModel> branchRateModels, |
| 57 |
int partitionNumber) { |
| 58 |
super(tree, diffusionModel, partitionNumber); |
| 59 |
this.branchRateModels = branchRateModels; |
| 60 |
|
| 61 |
dim = diffusionModel.getPrecisionParameter().getColumnDimension(); |
| 62 |
|
| 63 |
if (branchRateModels != null) { |
| 64 |
|
| 65 |
for (BranchRateModel rateModel : branchRateModels) { |
| 66 |
addModel(rateModel); |
| 67 |
} |
| 68 |
|
| 69 |
if (branchRateModels.size() != dim) { |
| 70 |
throw new IllegalArgumentException("Invalid dimensions"); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 77 |
|
| 78 |
if (branchRateModels.contains(model)) { |
| 79 |
fireModelChanged(model); |
| 80 |
} else { |
| 81 |
super.handleModelChangedEvent(model, object, index); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public boolean hasDrift() { return true; } |
| 87 |
|
| 88 |
@Override |
| 89 |
protected double[] getDriftRates(int[] branchIndices, int updateCount) { |
| 90 |
|
| 91 |
final double[] drift = new double[updateCount * dim]; |
| 92 |
|
| 93 |
if (branchRateModels != null) { |
| 94 |
|
| 95 |
int offset = 0; |
| 96 |
for (int i = 0; i < updateCount; ++i) { |
| 97 |
|
| 98 |
final NodeRef node = tree.getNode(branchIndices[i]); |
| 99 |
|
| 100 |
for (int model = 0; model < dim; ++model) { |
| 101 |
drift[offset] = branchRateModels.get(model).getBranchRate(tree, node); |
| 102 |
++offset; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
return drift; |
| 107 |
} |
| 108 |
|
| 109 |
double[] getAccumulativeDrift(final NodeRef node) { |
| 110 |
final double[] drift = new double[dim]; |
| 111 |
recursivelyAccumulateDrift(node, drift); |
| 112 |
return drift; |
| 113 |
} |
| 114 |
|
| 115 |
private void recursivelyAccumulateDrift(final NodeRef node, final double[] drift) { |
| 116 |
if (!tree.isRoot(node)) { |
| 117 |
|
| 118 |
final double length = tree.getBranchLength(node); |
| 119 |
|
| 120 |
for (int model = 0; model < dim; ++model) { |
| 121 |
drift[model] += branchRateModels.get(model).getBranchRate(tree, node) * length; |
| 122 |
} |
| 123 |
|
| 124 |
recursivelyAccumulateDrift(tree.getParent(node), drift); |
| 125 |
} |
| 126 |
} |
| 127 |
} |