| 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.siteratemodel; |
| 27 |
|
| 28 |
import dr.inference.model.*; |
| 29 |
import dr.math.distributions.GammaDistribution; |
| 30 |
import dr.evomodel.substmodel.SubstitutionModel; |
| 31 |
import dr.util.Author; |
| 32 |
import dr.util.Citable; |
| 33 |
import dr.util.Citation; |
| 34 |
|
| 35 |
import java.util.Collections; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class GammaSiteRateModel extends AbstractModel implements SiteRateModel, Citable { |
| 46 |
|
| 47 |
public GammaSiteRateModel(String name) { |
| 48 |
this( name, |
| 49 |
null, |
| 50 |
1.0, |
| 51 |
null, |
| 52 |
0, |
| 53 |
null); |
| 54 |
} |
| 55 |
|
| 56 |
public GammaSiteRateModel(String name, double alpha, int categoryCount) { |
| 57 |
this( name, |
| 58 |
null, |
| 59 |
1.0, |
| 60 |
new Parameter.Default(alpha), |
| 61 |
categoryCount, |
| 62 |
null); |
| 63 |
} |
| 64 |
|
| 65 |
public GammaSiteRateModel(String name, double pInvar) { |
| 66 |
this( name, |
| 67 |
null, |
| 68 |
1.0, |
| 69 |
null, |
| 70 |
0, |
| 71 |
new Parameter.Default(pInvar)); |
| 72 |
} |
| 73 |
|
| 74 |
public GammaSiteRateModel(String name, double alpha, int categoryCount, double pInvar) { |
| 75 |
this( name, |
| 76 |
null, |
| 77 |
1.0, |
| 78 |
new Parameter.Default(alpha), |
| 79 |
categoryCount, |
| 80 |
new Parameter.Default(pInvar)); |
| 81 |
} |
| 82 |
|
| 83 |
public GammaSiteRateModel( |
| 84 |
String name, |
| 85 |
Parameter nuParameter, |
| 86 |
Parameter shapeParameter, int gammaCategoryCount, |
| 87 |
Parameter invarParameter) { |
| 88 |
this(name, nuParameter, 1.0, shapeParameter, gammaCategoryCount, invarParameter); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public GammaSiteRateModel( |
| 96 |
String name, |
| 97 |
Parameter nuParameter, |
| 98 |
double muWeight, |
| 99 |
Parameter shapeParameter, int gammaCategoryCount, |
| 100 |
Parameter invarParameter) { |
| 101 |
|
| 102 |
super(name); |
| 103 |
|
| 104 |
this.nuParameter = nuParameter; |
| 105 |
if (nuParameter != null) { |
| 106 |
addVariable(nuParameter); |
| 107 |
nuParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 108 |
} |
| 109 |
this.muWeight = muWeight; |
| 110 |
|
| 111 |
addStatistic(muStatistic); |
| 112 |
|
| 113 |
this.shapeParameter = shapeParameter; |
| 114 |
if (shapeParameter != null) { |
| 115 |
this.categoryCount = gammaCategoryCount; |
| 116 |
|
| 117 |
addVariable(shapeParameter); |
| 118 |
|
| 119 |
|
| 120 |
shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 121 |
} else { |
| 122 |
this.categoryCount = 1; |
| 123 |
} |
| 124 |
|
| 125 |
this.invarParameter = invarParameter; |
| 126 |
if (invarParameter != null) { |
| 127 |
this.categoryCount += 1; |
| 128 |
|
| 129 |
addVariable(invarParameter); |
| 130 |
invarParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1)); |
| 131 |
} |
| 132 |
|
| 133 |
categoryRates = new double[this.categoryCount]; |
| 134 |
categoryProportions = new double[this.categoryCount]; |
| 135 |
|
| 136 |
ratesKnown = false; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public void setMu(double mu) { |
| 143 |
nuParameter.setParameterValue(0, mu / muWeight); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public final double getMu() { |
| 150 |
return nuParameter.getParameterValue(0) * muWeight; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void setAlpha(double alpha) { |
| 157 |
shapeParameter.setParameterValue(0, alpha); |
| 158 |
ratesKnown = false; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public final double getAlpha() { |
| 165 |
return shapeParameter.getParameterValue(0); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
public Parameter getAlphaParameter() { |
| 170 |
return shapeParameter; |
| 171 |
} |
| 172 |
|
| 173 |
public Parameter getPInvParameter() { |
| 174 |
return invarParameter; |
| 175 |
} |
| 176 |
|
| 177 |
public Parameter setRelativeRateParameter() { |
| 178 |
return nuParameter; |
| 179 |
} |
| 180 |
|
| 181 |
public void setAlphaParameter(Parameter parameter) { |
| 182 |
if (shapeParameter != null) removeVariable(shapeParameter); |
| 183 |
shapeParameter = parameter; |
| 184 |
if (shapeParameter != null) addVariable(shapeParameter); |
| 185 |
} |
| 186 |
|
| 187 |
public void setPInvParameter(Parameter parameter) { |
| 188 |
if (invarParameter != null) removeVariable(invarParameter); |
| 189 |
invarParameter = parameter; |
| 190 |
if (invarParameter != null) addVariable(invarParameter); |
| 191 |
} |
| 192 |
|
| 193 |
public void setRelativeRateParameter(Parameter parameter) { |
| 194 |
if (nuParameter != null) removeVariable(nuParameter); |
| 195 |
nuParameter = parameter; |
| 196 |
if (nuParameter != null) addVariable(nuParameter); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public int getCategoryCount() { |
| 204 |
return categoryCount; |
| 205 |
} |
| 206 |
|
| 207 |
public double[] getCategoryRates() { |
| 208 |
synchronized (this) { |
| 209 |
if (!ratesKnown) { |
| 210 |
calculateCategoryRates(); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
for (int i = (invarParameter != null ? 1 : 0); i < categoryRates.length; i++) { |
| 215 |
|
| 216 |
|
| 217 |
if (categoryRates[i] == 0.0) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return categoryRates; |
| 223 |
} |
| 224 |
|
| 225 |
public double[] getCategoryProportions() { |
| 226 |
synchronized (this) { |
| 227 |
if (!ratesKnown) { |
| 228 |
calculateCategoryRates(); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return categoryProportions; |
| 233 |
} |
| 234 |
|
| 235 |
public double getRateForCategory(int category) { |
| 236 |
synchronized (this) { |
| 237 |
if (!ratesKnown) { |
| 238 |
calculateCategoryRates(); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return categoryRates[category]; |
| 243 |
} |
| 244 |
|
| 245 |
public double getProportionForCategory(int category) { |
| 246 |
synchronized (this) { |
| 247 |
if (!ratesKnown) { |
| 248 |
calculateCategoryRates(); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return categoryProportions[category]; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
private void calculateCategoryRates() { |
| 260 |
|
| 261 |
double propVariable = 1.0; |
| 262 |
int cat = 0; |
| 263 |
|
| 264 |
if (invarParameter != null) { |
| 265 |
categoryRates[0] = 0.0; |
| 266 |
categoryProportions[0] = invarParameter.getParameterValue(0); |
| 267 |
|
| 268 |
propVariable = 1.0 - categoryProportions[0]; |
| 269 |
cat = 1; |
| 270 |
} |
| 271 |
|
| 272 |
if (shapeParameter != null) { |
| 273 |
|
| 274 |
final double a = shapeParameter.getParameterValue(0); |
| 275 |
double mean = 0.0; |
| 276 |
final int gammaCatCount = categoryCount - cat; |
| 277 |
|
| 278 |
for (int i = 0; i < gammaCatCount; i++) { |
| 279 |
|
| 280 |
categoryRates[i + cat] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * gammaCatCount), a, 1.0 / a); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
mean += categoryRates[i + cat]; |
| 287 |
|
| 288 |
categoryProportions[i + cat] = propVariable / gammaCatCount; |
| 289 |
} |
| 290 |
|
| 291 |
mean = (propVariable * mean) / gammaCatCount; |
| 292 |
|
| 293 |
for (int i = 0; i < gammaCatCount; i++) { |
| 294 |
|
| 295 |
categoryRates[i + cat] /= mean; |
| 296 |
} |
| 297 |
} else { |
| 298 |
categoryRates[cat] = 1.0 / propVariable; |
| 299 |
categoryProportions[cat] = propVariable; |
| 300 |
} |
| 301 |
|
| 302 |
if (nuParameter != null) { |
| 303 |
|
| 304 |
double mu = getMu(); |
| 305 |
for (int i=0; i < categoryCount; i++) |
| 306 |
categoryRates[i] *= mu; |
| 307 |
} |
| 308 |
|
| 309 |
ratesKnown = true; |
| 310 |
} |
| 311 |
|
| 312 |
public boolean hasInvariantSites() { |
| 313 |
return invarParameter != null; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 321 |
|
| 322 |
listenerHelper.fireModelChanged(this, object, index); |
| 323 |
} |
| 324 |
|
| 325 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 326 |
if (variable == shapeParameter) { |
| 327 |
ratesKnown = false; |
| 328 |
} else if (variable == invarParameter) { |
| 329 |
ratesKnown = false; |
| 330 |
} else if (variable == nuParameter) { |
| 331 |
ratesKnown = false; |
| 332 |
} else { |
| 333 |
throw new RuntimeException("Unknown variable in GammaSiteRateModel.handleVariableChangedEvent"); |
| 334 |
} |
| 335 |
listenerHelper.fireModelChanged(this, variable, index); |
| 336 |
} |
| 337 |
|
| 338 |
protected void storeState() { |
| 339 |
} |
| 340 |
|
| 341 |
protected void restoreState() { |
| 342 |
ratesKnown = false; |
| 343 |
} |
| 344 |
|
| 345 |
protected void acceptState() { |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
private Statistic muStatistic = new Statistic.Abstract() { |
| 350 |
|
| 351 |
public String getStatisticName() { |
| 352 |
return "mu"; |
| 353 |
} |
| 354 |
|
| 355 |
public int getDimension() { |
| 356 |
return 1; |
| 357 |
} |
| 358 |
|
| 359 |
public String getDimensionName(int dim) { |
| 360 |
return getId(); |
| 361 |
} |
| 362 |
|
| 363 |
public double getStatisticValue(int dim) { |
| 364 |
return getMu(); |
| 365 |
} |
| 366 |
|
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
private Parameter nuParameter; |
| 374 |
|
| 375 |
private double muWeight; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
private Parameter shapeParameter; |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
private Parameter invarParameter; |
| 386 |
|
| 387 |
private boolean ratesKnown; |
| 388 |
|
| 389 |
private int categoryCount; |
| 390 |
|
| 391 |
private double[] categoryRates; |
| 392 |
|
| 393 |
private double[] categoryProportions; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public SubstitutionModel getSubstitutionModel() { |
| 401 |
return substitutionModel; |
| 402 |
} |
| 403 |
|
| 404 |
public void setSubstitutionModel(SubstitutionModel substitutionModel) { |
| 405 |
this.substitutionModel = substitutionModel; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
@Override |
| 410 |
public Citation.Category getCategory() { |
| 411 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
public String getDescription() { |
| 416 |
return "Discrete gamma-distributed rate heterogeneity model"; |
| 417 |
} |
| 418 |
|
| 419 |
public List<Citation> getCitations() { |
| 420 |
if (getAlphaParameter() != null) { |
| 421 |
return Collections.singletonList(CITATION); |
| 422 |
} else { |
| 423 |
return Collections.emptyList(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
public final static Citation CITATION = new Citation( |
| 428 |
new Author[]{ |
| 429 |
new Author("Z", "Yang") |
| 430 |
}, |
| 431 |
"Maximum likelihood phylogenetic estimation from DNA sequences with variable rates over sites: approximate methods", |
| 432 |
1994, |
| 433 |
"J. Mol. Evol.", |
| 434 |
39, |
| 435 |
306, 314, |
| 436 |
Citation.Status.PUBLISHED |
| 437 |
); |
| 438 |
|
| 439 |
private SubstitutionModel substitutionModel; |
| 440 |
} |