| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.evolution.tree.NodeRef; |
| 4 |
import dr.evolution.tree.Tree; |
| 5 |
import dr.evolution.tree.TreeTrait; |
| 6 |
import dr.evomodel.continuous.MultivariateDiffusionModel; |
| 7 |
import dr.evomodel.treedatalikelihood.ProcessOnTreeDelegate; |
| 8 |
import dr.evomodel.treedatalikelihood.continuous.ConjugateRootTraitPrior; |
| 9 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate; |
| 10 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousRateTransformation; |
| 11 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousTraitPartialsProvider; |
| 12 |
import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator; |
| 13 |
|
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.List; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
public abstract class AbstractRealizedContinuousTraitDelegate extends ProcessSimulationDelegate.AbstractContinuousTraitDelegate { |
| 21 |
|
| 22 |
AbstractRealizedContinuousTraitDelegate(String name, |
| 23 |
Tree tree, |
| 24 |
MultivariateDiffusionModel diffusionModel, |
| 25 |
ContinuousTraitPartialsProvider dataModel, |
| 26 |
ConjugateRootTraitPrior rootPrior, |
| 27 |
ContinuousRateTransformation rateTransformation, |
| 28 |
ContinuousDataLikelihoodDelegate likelihoodDelegate) { |
| 29 |
super(name, tree, diffusionModel, dataModel, rootPrior, rateTransformation, likelihoodDelegate); |
| 30 |
|
| 31 |
sample = new double[dimNode * tree.getNodeCount()]; |
| 32 |
tmpEpsilon = new double[dimTrait]; |
| 33 |
tmpDrift = new double[dimTrait]; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
protected void constructTraits(final Helper treeTraitHelper) { |
| 38 |
|
| 39 |
TreeTrait.DA baseTrait = new TreeTrait.DA() { |
| 40 |
|
| 41 |
public String getTraitName() { |
| 42 |
return name; |
| 43 |
} |
| 44 |
|
| 45 |
public Intent getIntent() { |
| 46 |
return Intent.NODE; |
| 47 |
} |
| 48 |
|
| 49 |
public double[] getTrait(Tree t, NodeRef node) { |
| 50 |
|
| 51 |
if (t != tree) { |
| 52 |
if (t == baseTree) { |
| 53 |
node = getBaseNode(t, node); |
| 54 |
} else { |
| 55 |
throw new RuntimeException("Tree '" + t.getId() + "' and likelihood '" + tree.getId() + "' mismatch"); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return getTraitForNode(node); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
treeTraitHelper.addTrait(baseTrait); |
| 64 |
|
| 65 |
TreeTrait.DA tipTrait = new TreeTrait.DA() { |
| 66 |
|
| 67 |
public String getTraitName() { |
| 68 |
return getTipTraitName(name); |
| 69 |
} |
| 70 |
|
| 71 |
public Intent getIntent() { |
| 72 |
return Intent.WHOLE_TREE; |
| 73 |
} |
| 74 |
|
| 75 |
public double[] getTrait(Tree t, NodeRef node) { |
| 76 |
|
| 77 |
assert t == tree; |
| 78 |
return getTraitForAllTips(); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
treeTraitHelper.addTrait(tipTrait); |
| 83 |
|
| 84 |
TreeTrait.DA tipPrecision = new TreeTrait.DA() { |
| 85 |
|
| 86 |
public String getTraitName() { |
| 87 |
return getTipPrecisionName(name); |
| 88 |
} |
| 89 |
|
| 90 |
public Intent getIntent() { |
| 91 |
return Intent.WHOLE_TREE; |
| 92 |
} |
| 93 |
|
| 94 |
public double[] getTrait(Tree t, NodeRef node) { |
| 95 |
|
| 96 |
assert t == tree; |
| 97 |
return getPrecisionForAllTips(); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
treeTraitHelper.addTrait(tipPrecision); |
| 102 |
} |
| 103 |
|
| 104 |
public static String getTipTraitName(String name) { |
| 105 |
return "tip." + name; |
| 106 |
} |
| 107 |
|
| 108 |
private static String getTipPrecisionName(String name) { |
| 109 |
return "precision." + name; |
| 110 |
} |
| 111 |
|
| 112 |
private double[] getTraitForAllTips() { |
| 113 |
|
| 114 |
assert simulationProcess != null; |
| 115 |
|
| 116 |
simulationProcess.cacheSimulatedTraits(null); |
| 117 |
|
| 118 |
final int length = dimNode * tree.getExternalNodeCount(); |
| 119 |
double[] trait = new double[length]; |
| 120 |
System.arraycopy(sample, 0, trait, 0, length); |
| 121 |
|
| 122 |
return trait; |
| 123 |
} |
| 124 |
|
| 125 |
private double[] getPrecisionForAllTips() { |
| 126 |
|
| 127 |
assert simulationProcess != null; |
| 128 |
|
| 129 |
simulationProcess.cacheSimulatedTraits(null); |
| 130 |
|
| 131 |
final int length = tree.getExternalNodeCount(); |
| 132 |
double[] precision = new double[length]; |
| 133 |
|
| 134 |
Arrays.fill(precision, Double.POSITIVE_INFINITY); |
| 135 |
|
| 136 |
return precision; |
| 137 |
} |
| 138 |
|
| 139 |
private double[] getTraitForNode(final NodeRef node) { |
| 140 |
|
| 141 |
assert simulationProcess != null; |
| 142 |
|
| 143 |
simulationProcess.cacheSimulatedTraits(null); |
| 144 |
|
| 145 |
if (node == null) { |
| 146 |
return getTraitForAllTips(); |
| 147 |
} else { |
| 148 |
double[] trait = new double[dimNode]; |
| 149 |
System.arraycopy(sample, node.getNumber() * dimNode, trait, 0, dimNode); |
| 150 |
|
| 151 |
return trait; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
public int vectorizeNodeOperations(final List<NodeOperation> nodeOperations, final int[] operations) { |
| 156 |
|
| 157 |
int k = 0; |
| 158 |
for (ProcessOnTreeDelegate.NodeOperation op : nodeOperations) { |
| 159 |
|
| 160 |
operations[k ] = op.getNodeNumber(); |
| 161 |
operations[k + 1] = op.getLeftChild(); |
| 162 |
operations[k + 2] = likelihoodDelegate.getActiveNodeIndex(op.getLeftChild()); |
| 163 |
operations[k + 3] = likelihoodDelegate.getActiveMatrixIndex(op.getLeftChild()); |
| 164 |
operations[k + 4] = op.getLeftChild() < tree.getExternalNodeCount() ? 1 : 0; |
| 165 |
|
| 166 |
k += ContinuousDiffusionIntegrator.OPERATION_TUPLE_SIZE; |
| 167 |
} |
| 168 |
|
| 169 |
return nodeOperations.size(); |
| 170 |
} |
| 171 |
|
| 172 |
final double[] sample; |
| 173 |
final double[] tmpEpsilon; |
| 174 |
final double[] tmpDrift; |
| 175 |
} |