| 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.oldevomodel.ibd; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeTrait; |
| 31 |
import dr.evolution.tree.TreeTraitProvider; |
| 32 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 33 |
import dr.evomodel.branchratemodel.DefaultBranchRateModel; |
| 34 |
import dr.oldevomodel.substmodel.AbstractSubstitutionModel; |
| 35 |
import dr.oldevomodel.substmodel.HKY; |
| 36 |
import dr.evomodel.tree.TreeModel; |
| 37 |
import dr.oldevomodel.treelikelihood.NodePosteriorTreeLikelihood; |
| 38 |
import dr.inference.model.*; |
| 39 |
import dr.xml.*; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class AvgPosteriorIBDReporter extends AbstractModel implements TreeTraitProvider { |
| 53 |
|
| 54 |
protected double[] ibdweights; |
| 55 |
protected double[][] ibdForward; |
| 56 |
protected double[][] ibdBackward; |
| 57 |
protected double[] diag; |
| 58 |
protected boolean weightsKnown; |
| 59 |
protected HKY substitutionModel; |
| 60 |
protected TreeModel treeModel; |
| 61 |
protected BranchRateModel branchRateModel; |
| 62 |
protected Parameter mutationParameter; |
| 63 |
protected NodePosteriorTreeLikelihood likelihoodReporter; |
| 64 |
protected double[] probabilities; |
| 65 |
|
| 66 |
AvgPosteriorIBDReporter(NodePosteriorTreeLikelihood likelihoodReporter, Parameter mutationParameter, TreeModel treeModel, BranchRateModel branchRateModel, AbstractSubstitutionModel substitutionModel) { |
| 67 |
super("AvgPosteriorIBDReporter"); |
| 68 |
this.substitutionModel = (HKY) substitutionModel; |
| 69 |
addModel(this.substitutionModel); |
| 70 |
this.treeModel = treeModel; |
| 71 |
addModel(this.treeModel); |
| 72 |
this.branchRateModel = branchRateModel; |
| 73 |
addModel(this.branchRateModel); |
| 74 |
this.mutationParameter = mutationParameter; |
| 75 |
addVariable(this.mutationParameter); |
| 76 |
this.likelihoodReporter = likelihoodReporter; |
| 77 |
this.probabilities = new double[substitutionModel.getStateCount() * substitutionModel.getStateCount()]; |
| 78 |
} |
| 79 |
|
| 80 |
public void forwardIBD() { |
| 81 |
int numNodes = treeModel.getNodeCount(); |
| 82 |
int stateCount = substitutionModel.getStateCount(); |
| 83 |
getDiagonalRates(diag); |
| 84 |
int patternCount = likelihoodReporter.getPatternCount(); |
| 85 |
for (int nodeId = 0; nodeId < numNodes; ++nodeId) { |
| 86 |
NodeRef node = treeModel.getNode(nodeId); |
| 87 |
NodeRef parent = treeModel.getParent(node); |
| 88 |
likelihoodReporter.getNodeMatrix(nodeId, probabilities); |
| 89 |
double[] posteriors = likelihoodReporter.getPosteriors(nodeId); |
| 90 |
if (parent == null) { |
| 91 |
|
| 92 |
} else if (treeModel.isExternal(node)) { |
| 93 |
double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node)); |
| 94 |
|
| 95 |
for (int state = 0; state < stateCount; ++state) { |
| 96 |
double enorm = Math.exp(-diag[state] * branchTime) / probabilities[state + state * stateCount]; |
| 97 |
for (int pattern = 0; pattern < patternCount; ++pattern) { |
| 98 |
ibdForward[nodeId][pattern * stateCount + state] = posteriors[pattern * stateCount + state] * enorm; |
| 99 |
} |
| 100 |
} |
| 101 |
} else { |
| 102 |
double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node)); |
| 103 |
|
| 104 |
int childCount = treeModel.getChildCount(node); |
| 105 |
for (int state = 0; state < stateCount; ++state) { |
| 106 |
double enorm = Math.exp(-diag[state] * branchTime) / probabilities[state + state * stateCount]; |
| 107 |
for (int pattern = 0; pattern < patternCount; ++pattern) { |
| 108 |
ibdForward[nodeId][pattern * stateCount + state] = 0; |
| 109 |
|
| 110 |
for (int child = 0; child < childCount; ++child) { |
| 111 |
int childNodeId = treeModel.getChild(node, child).getNumber(); |
| 112 |
|
| 113 |
ibdForward[nodeId][pattern * stateCount + state] += ibdForward[childNodeId][pattern * stateCount + state]; |
| 114 |
} |
| 115 |
ibdForward[nodeId][pattern * stateCount + state] *= posteriors[pattern * stateCount + state] * enorm; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public void backwardIBD(NodeRef node) { |
| 123 |
int stateCount = substitutionModel.getStateCount(); |
| 124 |
int patternCount = likelihoodReporter.getPatternCount(); |
| 125 |
if (node == null) { |
| 126 |
node = treeModel.getRoot(); |
| 127 |
int nodeId = node.getNumber(); |
| 128 |
for (int i = 0; i < patternCount * stateCount; ++i) { |
| 129 |
ibdBackward[nodeId][i] = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
getDiagonalRates(diag); |
| 133 |
int childCount = treeModel.getChildCount(node); |
| 134 |
int nodeId = node.getNumber(); |
| 135 |
|
| 136 |
double[] posteriors = likelihoodReporter.getPosteriors(nodeId); |
| 137 |
for (int child = 0; child < childCount; ++child) { |
| 138 |
NodeRef childNode = treeModel.getChild(node, child); |
| 139 |
int childNodeId = childNode.getNumber(); |
| 140 |
likelihoodReporter.getNodeMatrix(childNodeId, probabilities); |
| 141 |
double branchTime = branchRateModel.getBranchRate(treeModel, childNode) * (treeModel.getNodeHeight(node) - treeModel.getNodeHeight(childNode)); |
| 142 |
for (int pattern = 0; pattern < patternCount; ++pattern) { |
| 143 |
for (int state = 0; state < stateCount; ++state) { |
| 144 |
ibdBackward[childNodeId][pattern * stateCount + state] = ibdBackward[nodeId][pattern * stateCount + state]; |
| 145 |
for (int sibling = 0; sibling < childCount; ++sibling) { |
| 146 |
if (sibling != child) { |
| 147 |
int siblingId = treeModel.getChild(node, sibling).getNumber(); |
| 148 |
ibdBackward[childNodeId][pattern * stateCount + state] += ibdForward[siblingId][pattern * stateCount + state]; |
| 149 |
} |
| 150 |
} |
| 151 |
ibdBackward[childNodeId][pattern * stateCount + state] *= posteriors[pattern * stateCount + state] * Math.exp(-diag[state] * branchTime) / probabilities[state + state * stateCount]; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
for (int child = 0; child < childCount; ++child) { |
| 156 |
NodeRef childNode = treeModel.getChild(node, child); |
| 157 |
backwardIBD(childNode); |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
public void expectedIBD() { |
| 163 |
int stateCount = substitutionModel.getStateCount(); |
| 164 |
int nodeCount = treeModel.getNodeCount(); |
| 165 |
int patternCount = likelihoodReporter.getPatternCount(); |
| 166 |
if (ibdweights == null) { |
| 167 |
ibdweights = new double[treeModel.getExternalNodeCount()]; |
| 168 |
ibdForward = new double[nodeCount][stateCount * patternCount]; |
| 169 |
ibdBackward = new double[nodeCount][stateCount * patternCount]; |
| 170 |
diag = new double[stateCount]; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
forwardIBD(); |
| 175 |
backwardIBD(null); |
| 176 |
int numTips = treeModel.getExternalNodeCount(); |
| 177 |
double[] patternWeights = likelihoodReporter.getPatternWeights(); |
| 178 |
double total = 0.0; |
| 179 |
for (int i = 0; i < patternCount; ++i) { |
| 180 |
total += patternWeights[i]; |
| 181 |
} |
| 182 |
|
| 183 |
for (int i = 0; i < numTips; ++i) { |
| 184 |
double[] posteriors = likelihoodReporter.getPosteriors(i); |
| 185 |
|
| 186 |
ibdweights[i] = 0; |
| 187 |
for (int pattern = 0; pattern < patternCount; ++pattern) { |
| 188 |
for (int j = 0; j < stateCount; ++j) { |
| 189 |
ibdweights[i] += ibdBackward[i][pattern * stateCount + j] * posteriors[pattern * stateCount + j] * patternWeights[pattern] / total; |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
protected void getDiagonalRates(double[] diagonalRates) { |
| 196 |
double kappa = substitutionModel.getKappa(); |
| 197 |
double[] freq = substitutionModel.getFrequencyModel().getFrequencies(); |
| 198 |
double mutationRate = mutationParameter.getParameterValue(0); |
| 199 |
double beta = 0.5 / ((freq[0] + freq[2]) * (freq[1] + freq[3]) + kappa * (freq[0] * freq[2] + freq[1] * freq[3])); |
| 200 |
|
| 201 |
diagonalRates[0] = ((freq[1] + freq[3]) + freq[2] * kappa) * mutationRate * beta; |
| 202 |
diagonalRates[1] = ((freq[0] + freq[2]) + freq[3] * kappa) * mutationRate * beta; |
| 203 |
diagonalRates[2] = ((freq[1] + freq[3]) + freq[0] * kappa) * mutationRate * beta; |
| 204 |
diagonalRates[3] = ((freq[0] + freq[2]) + freq[1] * kappa) * mutationRate * beta; |
| 205 |
} |
| 206 |
|
| 207 |
TreeTrait avgPosteriorIBDWeight = new TreeTrait.D() { |
| 208 |
public String getTraitName() { |
| 209 |
return "AvgPosteriorIBDWeight"; |
| 210 |
} |
| 211 |
|
| 212 |
public Intent getIntent() { |
| 213 |
return Intent.NODE; |
| 214 |
} |
| 215 |
|
| 216 |
public Double getTrait(Tree tree, NodeRef node) { |
| 217 |
if (!weightsKnown) { |
| 218 |
expectedIBD(); |
| 219 |
weightsKnown = true; |
| 220 |
} |
| 221 |
if (tree.isExternal(node)) { |
| 222 |
int nodeNum = node.getNumber(); |
| 223 |
return ibdweights[nodeNum] + 1; |
| 224 |
} |
| 225 |
return null; |
| 226 |
} |
| 227 |
}; |
| 228 |
|
| 229 |
public TreeTrait[] getTreeTraits() { |
| 230 |
return new TreeTrait[] { avgPosteriorIBDWeight }; |
| 231 |
} |
| 232 |
|
| 233 |
public TreeTrait getTreeTrait(String key) { |
| 234 |
|
| 235 |
return avgPosteriorIBDWeight; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public static final String IBD_REPORTER_LIKELIHOOD = "avgPosteriorIBDReporter"; |
| 243 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 244 |
|
| 245 |
public String getParserName() { |
| 246 |
return IBD_REPORTER_LIKELIHOOD; |
| 247 |
} |
| 248 |
|
| 249 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 250 |
|
| 251 |
|
| 252 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 253 |
Parameter mutationParameter = (Parameter) xo.getChild(Parameter.class); |
| 254 |
AbstractSubstitutionModel substitutionModel = |
| 255 |
(AbstractSubstitutionModel) xo.getChild(AbstractSubstitutionModel.class); |
| 256 |
|
| 257 |
BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class); |
| 258 |
if (branchRateModel == null) { |
| 259 |
branchRateModel = new DefaultBranchRateModel(); |
| 260 |
} |
| 261 |
|
| 262 |
NodePosteriorTreeLikelihood likelihoodReporter = (NodePosteriorTreeLikelihood) xo.getChild(NodePosteriorTreeLikelihood.class); |
| 263 |
|
| 264 |
return new AvgPosteriorIBDReporter(likelihoodReporter, mutationParameter, treeModel, branchRateModel, substitutionModel); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public String getParserDescription() { |
| 272 |
return "This element represents a reporter for average expected number of tips ibd conditional on observed patterns."; |
| 273 |
} |
| 274 |
|
| 275 |
public Class getReturnType() { |
| 276 |
return Likelihood.class; |
| 277 |
} |
| 278 |
|
| 279 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 280 |
return rules; |
| 281 |
} |
| 282 |
|
| 283 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 284 |
new ElementRule(TreeModel.class), |
| 285 |
new ElementRule(BranchRateModel.class, true), |
| 286 |
new ElementRule(AbstractSubstitutionModel.class), |
| 287 |
new ElementRule(Parameter.class), |
| 288 |
new ElementRule(NodePosteriorTreeLikelihood.class) |
| 289 |
}; |
| 290 |
}; |
| 291 |
|
| 292 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 293 |
if (model == branchRateModel || model == treeModel || model == substitutionModel || model == likelihoodReporter) { |
| 294 |
weightsKnown = false; |
| 295 |
} else { |
| 296 |
System.err.println("Weird call back to IBDReporter from " + model.getModelName()); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 301 |
if (variable == mutationParameter) { |
| 302 |
weightsKnown = false; |
| 303 |
} else { |
| 304 |
System.err.println("Weird call back to IBDReporter from " + variable.getVariableName()); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
protected void storeState() { |
| 309 |
} |
| 310 |
|
| 311 |
protected void restoreState() { |
| 312 |
} |
| 313 |
|
| 314 |
protected void acceptState() { |
| 315 |
} |
| 316 |
} |