| 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.treelikelihood; |
| 27 |
|
| 28 |
import dr.evolution.alignment.PatternList; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.tree.TreeTrait; |
| 32 |
import dr.evolution.tree.TreeTraitProvider; |
| 33 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 34 |
import dr.oldevomodel.sitemodel.SiteModel; |
| 35 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 36 |
import dr.evomodel.tipstatesmodel.TipStatesModel; |
| 37 |
import dr.evomodel.tree.TreeModel; |
| 38 |
import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser; |
| 39 |
import dr.inference.model.Likelihood; |
| 40 |
import dr.xml.*; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
@Deprecated |
| 53 |
public class NodePosteriorTreeLikelihood extends TreeLikelihood implements TreeTraitProvider { |
| 54 |
|
| 55 |
protected double[][] nodePosteriors; |
| 56 |
protected double[][] forwardProbs; |
| 57 |
protected double[] likes; |
| 58 |
boolean posteriorsKnown; |
| 59 |
private double[] childPartials; |
| 60 |
private double[] partialLikelihood; |
| 61 |
|
| 62 |
public NodePosteriorTreeLikelihood(PatternList patternList, TreeModel treeModel, SiteModel siteModel, BranchRateModel branchRateModel, TipStatesModel tipStatesModel, boolean useAmbiguities, boolean allowMissingTaxa, boolean storePartials, boolean forceJavaCore) { |
| 63 |
super(patternList, treeModel, siteModel, branchRateModel, tipStatesModel, useAmbiguities, allowMissingTaxa, storePartials, forceJavaCore, false); |
| 64 |
|
| 65 |
int extNodeCount = treeModel.getExternalNodeCount(); |
| 66 |
for (int i = 0; i < extNodeCount; i++) { |
| 67 |
String id = treeModel.getTaxonId(i); |
| 68 |
int index = patternList.getTaxonIndex(id); |
| 69 |
setPartials(likelihoodCore, patternList, categoryCount, index, i); |
| 70 |
} |
| 71 |
childPartials = new double[stateCount * patternCount]; |
| 72 |
partialLikelihood = new double[stateCount * patternCount]; |
| 73 |
posteriorsKnown = false; |
| 74 |
} |
| 75 |
|
| 76 |
TreeTrait posteriors = new TreeTrait.DA() { |
| 77 |
public String getTraitName() { |
| 78 |
return "posteriors"; |
| 79 |
} |
| 80 |
|
| 81 |
public Intent getIntent() { |
| 82 |
return Intent.NODE; |
| 83 |
} |
| 84 |
|
| 85 |
public double[] getTrait(Tree tree, NodeRef node) { |
| 86 |
if (tree != treeModel) { |
| 87 |
throw new RuntimeException("Can only calculate node posteriors on treeModel given to constructor"); |
| 88 |
} |
| 89 |
if (!posteriorsKnown) { |
| 90 |
calculatePosteriors(); |
| 91 |
} |
| 92 |
return nodePosteriors[node.getNumber()]; |
| 93 |
} |
| 94 |
|
| 95 |
}; |
| 96 |
|
| 97 |
public TreeTrait[] getTreeTraits() { |
| 98 |
return new TreeTrait[] { posteriors }; |
| 99 |
} |
| 100 |
|
| 101 |
public TreeTrait getTreeTrait(String key) { |
| 102 |
|
| 103 |
return posteriors; |
| 104 |
} |
| 105 |
|
| 106 |
public double[] getPosteriors(int nodeNum) { |
| 107 |
if (!posteriorsKnown) { |
| 108 |
calculatePosteriors(); |
| 109 |
} |
| 110 |
return nodePosteriors[nodeNum]; |
| 111 |
} |
| 112 |
|
| 113 |
public void getNodeMatrix(int nodeNum, double[] probabilities) { |
| 114 |
((AbstractLikelihoodCore) likelihoodCore).getNodeMatrix(nodeNum, 0, probabilities); |
| 115 |
} |
| 116 |
|
| 117 |
public void calculatePosteriors() { |
| 118 |
int nodeCount = treeModel.getNodeCount(); |
| 119 |
traverseForward(treeModel, treeModel.getRoot()); |
| 120 |
for (int k = 0; k < nodeCount; ++k) { |
| 121 |
for (int i = 0; i < patternCount; ++i) { |
| 122 |
for (int j = 0; j < stateCount; ++j) { |
| 123 |
nodePosteriors[k][stateCount * i + j] /= likes[i]; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
posteriorsKnown = true; |
| 128 |
} |
| 129 |
|
| 130 |
protected double calculateLogLikelihood() { |
| 131 |
posteriorsKnown = false; |
| 132 |
|
| 133 |
return super.calculateLogLikelihood(); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
public void traverseForward(TreeModel tree, NodeRef node) { |
| 138 |
if (nodePosteriors == null) { |
| 139 |
nodePosteriors = new double[tree.getNodeCount()][patternCount * stateCount]; |
| 140 |
} |
| 141 |
if (forwardProbs == null) { |
| 142 |
forwardProbs = new double[tree.getNodeCount()][patternCount * stateCount]; |
| 143 |
} |
| 144 |
int nodeNum = node.getNumber(); |
| 145 |
|
| 146 |
NodeRef parent = tree.getParent(node); |
| 147 |
|
| 148 |
|
| 149 |
if (parent == null) { |
| 150 |
double[] rootFreqs = frequencyModel.getFrequencies(); |
| 151 |
double[] rootPartials = getRootPartials(); |
| 152 |
if (likes == null) { |
| 153 |
likes = new double[patternCount]; |
| 154 |
} |
| 155 |
|
| 156 |
for (int j = 0; j < patternCount; j++) { |
| 157 |
System.arraycopy(rootFreqs, 0, forwardProbs[nodeNum], j * stateCount, stateCount); |
| 158 |
likes[j] = 0.0; |
| 159 |
for (int k = 0; k < stateCount; ++k) { |
| 160 |
nodePosteriors[nodeNum][stateCount * j + k] = forwardProbs[nodeNum][stateCount * j + k] * rootPartials[j * stateCount + k]; |
| 161 |
likes[j] = likes[j] + rootPartials[stateCount * j + k] * rootFreqs[k]; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
} else { |
| 166 |
int parentNum = parent.getNumber(); |
| 167 |
int numChildren = tree.getChildCount(parent); |
| 168 |
|
| 169 |
System.arraycopy(forwardProbs[parentNum], 0, forwardProbs[nodeNum], 0, stateCount * patternCount); |
| 170 |
|
| 171 |
for (int child = 0; child < numChildren; ++child) { |
| 172 |
NodeRef childNode = tree.getChild(parent, child); |
| 173 |
int childNum = childNode.getNumber(); |
| 174 |
if (childNum != nodeNum) { |
| 175 |
getNodeMatrix(childNum, probabilities); |
| 176 |
likelihoodCore.getPartials(childNum, childPartials); |
| 177 |
accumulateMatrixMultiply(probabilities, childPartials, forwardProbs[nodeNum]); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
getNodeMatrix(nodeNum, probabilities); |
| 182 |
likelihoodCore.getPartials(nodeNum, partialLikelihood); |
| 183 |
|
| 184 |
matrixMultiplyBackward(probabilities, forwardProbs[nodeNum], nodePosteriors[nodeNum]); |
| 185 |
for (int i = 0; i < patternCount * stateCount; ++i) { |
| 186 |
forwardProbs[nodeNum][i] = nodePosteriors[nodeNum][i]; |
| 187 |
nodePosteriors[nodeNum][i] = nodePosteriors[nodeNum][i] * partialLikelihood[i]; |
| 188 |
} |
| 189 |
} |
| 190 |
if (!tree.isExternal(node)) { |
| 191 |
for (int child = 0; child < tree.getChildCount(node); ++child) { |
| 192 |
NodeRef child1 = tree.getChild(node, child); |
| 193 |
traverseForward(tree, child1); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
public void accumulateMatrixMultiply(double[] probabilities, double[] partials, double[] out) { |
| 199 |
int u = 0; |
| 200 |
int v = 0; |
| 201 |
|
| 202 |
for (int k = 0; k < patternCount; ++k) { |
| 203 |
int w = 0; |
| 204 |
for (int i = 0; i < stateCount; ++i) { |
| 205 |
double sum1 = 0.0; |
| 206 |
for (int j = 0; j < stateCount; ++j) { |
| 207 |
sum1 += probabilities[w] * partials[v + j]; |
| 208 |
w++; |
| 209 |
} |
| 210 |
out[u] = sum1 * out[u]; |
| 211 |
u++; |
| 212 |
} |
| 213 |
v += stateCount; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
public void matrixMultiplyBackward(double[] probabilities, double[] partials, double[] out) { |
| 218 |
int u = 0; |
| 219 |
int v = 0; |
| 220 |
for (int k = 0; k < patternCount; ++k) { |
| 221 |
for (int i = 0; i < stateCount; ++i) { |
| 222 |
int w = i; |
| 223 |
double sum1 = 0.0; |
| 224 |
for (int j = 0; j < stateCount; ++j) { |
| 225 |
sum1 += probabilities[w] * partials[v + j]; |
| 226 |
w += stateCount; |
| 227 |
} |
| 228 |
out[u] = sum1; |
| 229 |
u++; |
| 230 |
} |
| 231 |
v += stateCount; |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static final String NODE_POSTERIOR_LIKELIHOOD = "nodePosteriorLikelihood"; |
| 240 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 241 |
|
| 242 |
public String getParserName() { |
| 243 |
return NODE_POSTERIOR_LIKELIHOOD; |
| 244 |
} |
| 245 |
|
| 246 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 247 |
|
| 248 |
boolean useAmbiguities = xo.getAttribute(TreeLikelihoodParser.USE_AMBIGUITIES, false); |
| 249 |
boolean storePartials = xo.getAttribute(TreeLikelihoodParser.STORE_PARTIALS, true); |
| 250 |
|
| 251 |
PatternList patternList = (PatternList) xo.getChild(PatternList.class); |
| 252 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 253 |
SiteModel siteModel = (SiteModel) xo.getChild(SiteModel.class); |
| 254 |
|
| 255 |
BranchRateModel branchRateModel = (BranchRateModel) xo.getChild(BranchRateModel.class); |
| 256 |
|
| 257 |
return new NodePosteriorTreeLikelihood(patternList, treeModel, siteModel, |
| 258 |
branchRateModel, null, useAmbiguities, false, storePartials, false); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public String getParserDescription() { |
| 266 |
return "This element represents the likelihood of a patternlist on a tree given the site model."; |
| 267 |
} |
| 268 |
|
| 269 |
public Class getReturnType() { |
| 270 |
return Likelihood.class; |
| 271 |
} |
| 272 |
|
| 273 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 274 |
return rules; |
| 275 |
} |
| 276 |
|
| 277 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 278 |
AttributeRule.newBooleanRule(TreeLikelihoodParser.USE_AMBIGUITIES, true), |
| 279 |
AttributeRule.newBooleanRule(TreeLikelihoodParser.ALLOW_MISSING_TAXA, true), |
| 280 |
new ElementRule(PatternList.class), |
| 281 |
new ElementRule(TreeModel.class), |
| 282 |
new ElementRule(SiteModel.class), |
| 283 |
new ElementRule(BranchRateModel.class, true), |
| 284 |
new ElementRule(SubstitutionModel.class) |
| 285 |
}; |
| 286 |
}; |
| 287 |
|
| 288 |
} |
| 289 |
|