| 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.MSSD; |
| 27 |
|
| 28 |
import dr.evolution.alignment.AscertainedSitePatterns; |
| 29 |
import dr.evolution.alignment.PatternList; |
| 30 |
import dr.evolution.datatype.MutationDeathType; |
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 33 |
import dr.evomodel.branchratemodel.DefaultBranchRateModel; |
| 34 |
import dr.evomodel.tree.TreeChangedEvent; |
| 35 |
import dr.oldevomodel.sitemodel.SiteRateModel; |
| 36 |
import dr.evomodel.tree.TreeModel; |
| 37 |
import dr.oldevomodel.treelikelihood.LikelihoodCore; |
| 38 |
import dr.evomodel.treelikelihood.LikelihoodPartialsProvider; |
| 39 |
import dr.oldevomodel.treelikelihood.ScaleFactorsHelper; |
| 40 |
import dr.inference.model.AbstractModel; |
| 41 |
import dr.inference.model.Model; |
| 42 |
import dr.inference.model.Parameter; |
| 43 |
import dr.inference.model.Variable; |
| 44 |
import dr.math.GammaFunction; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
abstract public class AbstractObservationProcess extends AbstractModel { |
| 57 |
protected boolean[] nodePatternInclusion; |
| 58 |
protected boolean[] storedNodePatternInclusion; |
| 59 |
protected double[] cumLike; |
| 60 |
protected double[] nodePartials; |
| 61 |
protected double[] nodeLikelihoods; |
| 62 |
protected int nodeCount; |
| 63 |
protected int patternCount; |
| 64 |
protected int stateCount; |
| 65 |
protected TreeModel treeModel; |
| 66 |
protected PatternList patterns; |
| 67 |
protected double[] patternWeights; |
| 68 |
protected Parameter mu; |
| 69 |
protected Parameter lam; |
| 70 |
|
| 71 |
|
| 72 |
protected boolean weightKnown; |
| 73 |
protected double logTreeWeight; |
| 74 |
protected double storedLogTreeWeight; |
| 75 |
private double gammaNorm; |
| 76 |
private double totalPatterns; |
| 77 |
protected MutationDeathType dataType; |
| 78 |
protected int deathState; |
| 79 |
protected SiteRateModel siteModel; |
| 80 |
private double logN; |
| 81 |
protected boolean nodePatternInclusionKnown = false; |
| 82 |
BranchRateModel branchRateModel; |
| 83 |
|
| 84 |
public AbstractObservationProcess(String Name, TreeModel treeModel, PatternList patterns, SiteRateModel siteModel, |
| 85 |
BranchRateModel branchRateModel, Parameter mu, Parameter lam) { |
| 86 |
super(Name); |
| 87 |
this.treeModel = treeModel; |
| 88 |
this.patterns = patterns; |
| 89 |
this.mu = mu; |
| 90 |
this.lam = lam; |
| 91 |
this.siteModel = siteModel; |
| 92 |
if (branchRateModel != null) { |
| 93 |
this.branchRateModel = branchRateModel; |
| 94 |
} else { |
| 95 |
this.branchRateModel = new DefaultBranchRateModel(); |
| 96 |
} |
| 97 |
addModel(treeModel); |
| 98 |
addModel(siteModel); |
| 99 |
addModel(this.branchRateModel); |
| 100 |
addVariable(mu); |
| 101 |
addVariable(lam); |
| 102 |
|
| 103 |
nodeCount = treeModel.getNodeCount(); |
| 104 |
stateCount = patterns.getDataType().getStateCount(); |
| 105 |
this.patterns = patterns; |
| 106 |
patternCount = patterns.getPatternCount(); |
| 107 |
patternWeights = patterns.getPatternWeights(); |
| 108 |
totalPatterns = 0; |
| 109 |
for (int i = 0; i < patternCount; ++i) { |
| 110 |
totalPatterns += patternWeights[i]; |
| 111 |
} |
| 112 |
logN = Math.log(totalPatterns); |
| 113 |
|
| 114 |
gammaNorm = -GammaFunction.lnGamma(totalPatterns + 1); |
| 115 |
|
| 116 |
dataType = (MutationDeathType) patterns.getDataType(); |
| 117 |
this.deathState = dataType.DEATHSTATE; |
| 118 |
setNodePatternInclusion(); |
| 119 |
cumLike = new double[patternCount]; |
| 120 |
nodeLikelihoods = new double[patternCount]; |
| 121 |
weightKnown = false; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private double calculateSiteLogLikelihood(int site, double[] partials, double[] frequencies) { |
| 133 |
int v = site * stateCount; |
| 134 |
double sum = 0.0; |
| 135 |
for (int i = 0; i < stateCount; i++) { |
| 136 |
sum += frequencies[i] * partials[v + i]; |
| 137 |
} |
| 138 |
return Math.log(sum); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
private void calculateNodePatternLikelihood(int nodeIndex, |
| 143 |
double[] freqs, |
| 144 |
LikelihoodCore likelihoodCore, |
| 145 |
double averageRate, |
| 146 |
double[] cumLike) { |
| 147 |
|
| 148 |
likelihoodCore.getPartials(nodeIndex, nodePartials); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
double prob = Math.log(getNodeSurvivalProbability(nodeIndex, averageRate)); |
| 155 |
|
| 156 |
for (int j = 0; j < patternCount; ++j) { |
| 157 |
if (nodePatternInclusion[nodeIndex * patternCount + j]) { |
| 158 |
cumLike[j] += Math.exp(calculateSiteLogLikelihood(j, nodePartials, freqs) + prob); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
private double accumulateCorrectedLikelihoods(double[] cumLike, double ascertainmentCorrection, |
| 164 |
double[] patterWeights) { |
| 165 |
double logL = 0; |
| 166 |
for (int j = 0; j < patternCount; ++j) { |
| 167 |
logL += Math.log(cumLike[j] / ascertainmentCorrection) * patternWeights[j]; |
| 168 |
} |
| 169 |
return logL; |
| 170 |
} |
| 171 |
|
| 172 |
public final double nodePatternLikelihood(double[] freqs, LikelihoodPartialsProvider likelihoodCore, |
| 173 |
ScaleFactorsHelper scaleFactorsHelper) { |
| 174 |
int i, j; |
| 175 |
double logL = gammaNorm; |
| 176 |
|
| 177 |
double birthRate = lam.getParameterValue(0); |
| 178 |
double logProb; |
| 179 |
if (!nodePatternInclusionKnown) |
| 180 |
setNodePatternInclusion(); |
| 181 |
if (nodePartials == null) { |
| 182 |
nodePartials = new double[patternCount * stateCount]; |
| 183 |
} |
| 184 |
|
| 185 |
double averageRate = getAverageRate(); |
| 186 |
|
| 187 |
for (j = 0; j < patternCount; ++j) cumLike[j] = 0; |
| 188 |
|
| 189 |
for (i = 0; i < nodeCount; ++i) { |
| 190 |
|
| 191 |
likelihoodCore.getPartials(i, nodePartials); |
| 192 |
scaleFactorsHelper.rescalePartials(i, nodePartials); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
logProb = Math.log(getNodeSurvivalProbability(i, averageRate)); |
| 200 |
|
| 201 |
for (j = 0; j < patternCount; ++j) { |
| 202 |
if (nodePatternInclusion[i * patternCount + j]) { |
| 203 |
|
| 204 |
cumLike[j] += Math.exp(calculateSiteLogLikelihood(j, nodePartials, freqs) |
| 205 |
+ logProb); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
double ascertainmentCorrection = getAscertainmentCorrection(cumLike); |
| 211 |
|
| 212 |
|
| 213 |
for (j = 0; j < patternCount; ++j) { |
| 214 |
logL += Math.log(cumLike[j] / ascertainmentCorrection) * patternWeights[j]; |
| 215 |
} |
| 216 |
|
| 217 |
double deathRate = mu.getParameterValue(0); |
| 218 |
|
| 219 |
double logTreeWeight = getLogTreeWeight(); |
| 220 |
|
| 221 |
if (integrateGainRate) { |
| 222 |
logL -= gammaNorm + logN + Math.log(-logTreeWeight * deathRate / birthRate) * totalPatterns; |
| 223 |
} else { |
| 224 |
logL += logTreeWeight + Math.log(birthRate / deathRate) * totalPatterns; |
| 225 |
} |
| 226 |
return logL; |
| 227 |
} |
| 228 |
|
| 229 |
protected double getAscertainmentCorrection(double[] patternProbs) { |
| 230 |
|
| 231 |
double excludeProb = 0, includeProb = 0, returnProb = 1.0; |
| 232 |
if (this.patterns instanceof AscertainedSitePatterns) { |
| 233 |
int[] includeIndices = ((AscertainedSitePatterns) patterns).getIncludePatternIndices(); |
| 234 |
int[] excludeIndices = ((AscertainedSitePatterns) patterns).getExcludePatternIndices(); |
| 235 |
for (int i = 0; i < ((AscertainedSitePatterns) patterns).getIncludePatternCount(); i++) { |
| 236 |
int index = includeIndices[i]; |
| 237 |
includeProb += patternProbs[index]; |
| 238 |
} |
| 239 |
for (int j = 0; j < ((AscertainedSitePatterns) patterns).getExcludePatternCount(); j++) { |
| 240 |
int index = excludeIndices[j]; |
| 241 |
excludeProb += patternProbs[index]; |
| 242 |
} |
| 243 |
if (includeProb == 0.0) { |
| 244 |
returnProb -= excludeProb; |
| 245 |
} else if (excludeProb == 0.0) { |
| 246 |
returnProb = includeProb; |
| 247 |
} else { |
| 248 |
returnProb = includeProb - excludeProb; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return returnProb; |
| 253 |
} |
| 254 |
|
| 255 |
final public double getLogTreeWeight() { |
| 256 |
if (!weightKnown) { |
| 257 |
logTreeWeight = calculateLogTreeWeight(); |
| 258 |
weightKnown = true; |
| 259 |
} |
| 260 |
|
| 261 |
return logTreeWeight; |
| 262 |
} |
| 263 |
|
| 264 |
abstract public double calculateLogTreeWeight(); |
| 265 |
|
| 266 |
abstract void setNodePatternInclusion(); |
| 267 |
|
| 268 |
final public double getAverageRate() { |
| 269 |
if (!averageRateKnown) { |
| 270 |
double avgRate = 0.0; |
| 271 |
double proportions[] = siteModel.getCategoryProportions(); |
| 272 |
for (int i = 0; i < siteModel.getCategoryCount(); ++i) { |
| 273 |
avgRate += proportions[i] * siteModel.getRateForCategory(i); |
| 274 |
} |
| 275 |
averageRate = avgRate; |
| 276 |
averageRateKnown = true; |
| 277 |
} |
| 278 |
return averageRate; |
| 279 |
} |
| 280 |
|
| 281 |
public double getNodeSurvivalProbability(int index, double averageRate) { |
| 282 |
NodeRef node = treeModel.getNode(index); |
| 283 |
NodeRef parent = treeModel.getParent(node); |
| 284 |
|
| 285 |
if (parent == null) return 1.0; |
| 286 |
|
| 287 |
final double deathRate = mu.getParameterValue(0) * averageRate; |
| 288 |
final double branchRate = branchRateModel.getBranchRate(treeModel, node); |
| 289 |
|
| 290 |
final double branchTime = branchRate * treeModel.getBranchLength(node); |
| 291 |
return 1.0 - Math.exp(-deathRate * branchTime); |
| 292 |
} |
| 293 |
|
| 294 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 295 |
if (model == siteModel) { |
| 296 |
averageRateKnown = false; |
| 297 |
} |
| 298 |
if (model == treeModel || model == siteModel || model == branchRateModel) { |
| 299 |
weightKnown = false; |
| 300 |
} |
| 301 |
if (model == treeModel) { |
| 302 |
if (object instanceof TreeChangedEvent) { |
| 303 |
if (((TreeChangedEvent) object).isTreeChanged()) { |
| 304 |
nodePatternInclusionKnown = false; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 311 |
if (variable == mu || variable == lam) { |
| 312 |
weightKnown = false; |
| 313 |
} else { |
| 314 |
System.err.println("AbstractObservationProcess: Got unexpected parameter changed event. (Parameter = " + variable + ")"); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
protected void storeState() { |
| 319 |
|
| 320 |
storedLogTreeWeight = logTreeWeight; |
| 321 |
System.arraycopy(nodePatternInclusion, 0, storedNodePatternInclusion, 0, storedNodePatternInclusion.length); |
| 322 |
} |
| 323 |
|
| 324 |
protected void restoreState() { |
| 325 |
|
| 326 |
averageRateKnown = false; |
| 327 |
logTreeWeight = storedLogTreeWeight; |
| 328 |
boolean[] tmp = storedNodePatternInclusion; |
| 329 |
storedNodePatternInclusion = nodePatternInclusion; |
| 330 |
nodePatternInclusion = tmp; |
| 331 |
} |
| 332 |
|
| 333 |
protected void acceptState() { |
| 334 |
} |
| 335 |
|
| 336 |
public void setIntegrateGainRate(boolean integrateGainRate) { |
| 337 |
this.integrateGainRate = integrateGainRate; |
| 338 |
} |
| 339 |
|
| 340 |
private boolean integrateGainRate = false; |
| 341 |
|
| 342 |
private double storedAverageRate; |
| 343 |
private double averageRate; |
| 344 |
private boolean averageRateKnown = false; |
| 345 |
} |