| 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.continuous.ConjugateRootTraitPrior; |
| 8 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate; |
| 9 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousRateTransformation; |
| 10 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousTraitPartialsProvider; |
| 11 |
import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator; |
| 12 |
import dr.math.matrixAlgebra.WrappedVector; |
| 13 |
|
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class TipFullConditionalDistributionDelegate extends ProcessSimulationDelegate.AbstractContinuousTraitDelegate { |
| 20 |
|
| 21 |
public TipFullConditionalDistributionDelegate(String name, Tree tree, |
| 22 |
MultivariateDiffusionModel diffusionModel, |
| 23 |
ContinuousTraitPartialsProvider dataModel, |
| 24 |
ConjugateRootTraitPrior rootPrior, |
| 25 |
ContinuousRateTransformation rateTransformation, |
| 26 |
|
| 27 |
ContinuousDataLikelihoodDelegate likelihoodDelegate) { |
| 28 |
|
| 29 |
super(name, tree, diffusionModel, dataModel, rootPrior, rateTransformation, likelihoodDelegate); |
| 30 |
|
| 31 |
this.likelihoodDelegate = likelihoodDelegate; |
| 32 |
this.cdi = likelihoodDelegate.getIntegrator(); |
| 33 |
|
| 34 |
assert (cdi instanceof ContinuousDiffusionIntegrator.Basic); |
| 35 |
|
| 36 |
this.dimPartial = dimTrait + likelihoodDelegate.getPrecisionType().getMatrixLength(dimTrait); |
| 37 |
partialNodeBuffer = new double[numTraits * dimPartial]; |
| 38 |
|
| 39 |
partialRootBuffer = new double[numTraits * dimPartial]; |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
public int vectorizeNodeOperations(final List<NodeOperation> nodeOperations, |
| 44 |
final int[] operations) { |
| 45 |
return likelihoodDelegate.vectorizeNodeOperations(nodeOperations, operations); |
| 46 |
} |
| 47 |
|
| 48 |
protected boolean isLoggable() { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
protected final ContinuousDataLikelihoodDelegate likelihoodDelegate; |
| 53 |
protected final ContinuousDiffusionIntegrator cdi; |
| 54 |
protected final int dimPartial; |
| 55 |
final double[] partialNodeBuffer; |
| 56 |
|
| 57 |
private final double[] partialRootBuffer; |
| 58 |
|
| 59 |
public static String getName(String name) { |
| 60 |
return "fcd." + name; |
| 61 |
} |
| 62 |
|
| 63 |
public String getTraitName(String name) { |
| 64 |
return getName(name); |
| 65 |
} |
| 66 |
|
| 67 |
private String delegateGetTraitName() { |
| 68 |
return getTraitName(name); |
| 69 |
} |
| 70 |
|
| 71 |
private Class delegateGetTraitClass() { |
| 72 |
return double[].class; |
| 73 |
} |
| 74 |
|
| 75 |
protected void constructTraits(Helper treeTraitHelper) { |
| 76 |
|
| 77 |
TreeTrait.DA baseTrait = new TreeTrait.DA() { |
| 78 |
|
| 79 |
public String getTraitName() { |
| 80 |
return delegateGetTraitName(); |
| 81 |
} |
| 82 |
|
| 83 |
public Intent getIntent() { |
| 84 |
return Intent.NODE; |
| 85 |
} |
| 86 |
|
| 87 |
public Class getTraitClass() { |
| 88 |
return delegateGetTraitClass(); |
| 89 |
} |
| 90 |
|
| 91 |
public double[] getTrait(Tree t, NodeRef node) { |
| 92 |
assert (tree == t); |
| 93 |
|
| 94 |
return getTraitForNode(node); |
| 95 |
} |
| 96 |
|
| 97 |
public String getTraitString(Tree tree, NodeRef node) { |
| 98 |
return formatted(getTrait(tree, node)); |
| 99 |
} |
| 100 |
|
| 101 |
public boolean getLoggable() { |
| 102 |
return isLoggable(); |
| 103 |
} |
| 104 |
}; |
| 105 |
|
| 106 |
treeTraitHelper.addTrait(baseTrait); |
| 107 |
} |
| 108 |
|
| 109 |
private static String formatted(double[] values) { |
| 110 |
|
| 111 |
if (values.length == 1) { |
| 112 |
return Double.toString(values[0]); |
| 113 |
} |
| 114 |
|
| 115 |
StringBuilder sb = new StringBuilder(); |
| 116 |
sb.append("{"); |
| 117 |
|
| 118 |
for (int i = 0; i < values.length; ++i) { |
| 119 |
sb.append(Double.toString(values[i])); |
| 120 |
if (i < (values.length - 1)) { |
| 121 |
sb.append(","); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
sb.append("}"); |
| 126 |
return sb.toString(); |
| 127 |
} |
| 128 |
|
| 129 |
protected double[] getTraitForNode(NodeRef node) { |
| 130 |
|
| 131 |
assert simulationProcess != null; |
| 132 |
assert node != null; |
| 133 |
|
| 134 |
simulationProcess.cacheSimulatedTraits(node); |
| 135 |
|
| 136 |
double[] partial = new double[dimPartial * numTraits]; |
| 137 |
cdi.getPreOrderPartial(likelihoodDelegate.getActiveNodeIndex(node.getNumber()), partial); |
| 138 |
|
| 139 |
return partial; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
protected void simulateRoot(final int rootIndex) { |
| 144 |
|
| 145 |
if (DEBUG) { |
| 146 |
System.err.println("Simulate root node " + rootIndex); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
cdi.getPostOrderPartial(rootProcessDelegate.getPriorBufferIndex(), partialRootBuffer); |
| 151 |
cdi.setPreOrderPartial(likelihoodDelegate.getActiveNodeIndex(rootIndex), partialRootBuffer); |
| 152 |
|
| 153 |
if (DEBUG) { |
| 154 |
System.err.println("Root: " + new WrappedVector.Raw(partialRootBuffer, 0, partialRootBuffer.length)); |
| 155 |
System.err.println(""); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@Override |
| 185 |
protected void simulateNode(final int parentNumber, |
| 186 |
final int nodeNumber, |
| 187 |
final int nodeMatrix, |
| 188 |
final int siblingNumber, |
| 189 |
final int siblingMatrix) { |
| 190 |
|
| 191 |
cdi.updatePreOrderPartial( |
| 192 |
parentNumber, |
| 193 |
nodeNumber, |
| 194 |
nodeMatrix, |
| 195 |
siblingNumber, |
| 196 |
siblingMatrix); |
| 197 |
|
| 198 |
if (DEBUG) { |
| 199 |
cdi.getPreOrderPartial(nodeNumber, partialRootBuffer); |
| 200 |
System.err.println("Node: " + nodeNumber + " " |
| 201 |
+ new WrappedVector.Raw(partialRootBuffer, 0, partialRootBuffer.length)); |
| 202 |
System.err.println(""); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private static final boolean DEBUG = false; |
| 225 |
|
| 226 |
} |