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