| 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.sitemodel; |
| 27 |
|
| 28 |
import dr.oldevomodel.substmodel.SubstitutionModel; |
| 29 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.math.distributions.GammaDistribution; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class GammaSiteBMA extends AbstractModel implements SiteModel { |
| 40 |
|
| 41 |
private SubstitutionModel substitutionModel = null; |
| 42 |
private Parameter muParameter =null; |
| 43 |
private Variable<Integer> modelChoose = null; |
| 44 |
private Variable<Double> logShape = null; |
| 45 |
private Variable<Double> logitInvar = null; |
| 46 |
private int categoryCount = -1; |
| 47 |
private double[] categoryRates; |
| 48 |
private double[] categoryProportions; |
| 49 |
private boolean ratesKnown; |
| 50 |
public static final int SHAPE_INDEX = 0; |
| 51 |
public static final int INVAR_INDEX = 1; |
| 52 |
public static final int PRESENT = 1; |
| 53 |
public static final int ABSENT = 0; |
| 54 |
|
| 55 |
public GammaSiteBMA(SubstitutionModel substitutionModel, |
| 56 |
Parameter muParameter, |
| 57 |
Variable<Double> logitInvar, |
| 58 |
Variable<Double> logShape, |
| 59 |
int categoryCount, |
| 60 |
Variable<Integer> modelChoose){ |
| 61 |
super("GammaSiteBMA"); |
| 62 |
|
| 63 |
this.substitutionModel = substitutionModel; |
| 64 |
addModel(substitutionModel); |
| 65 |
|
| 66 |
addVariable(muParameter); |
| 67 |
muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); |
| 68 |
this.muParameter = muParameter; |
| 69 |
|
| 70 |
addVariable(logShape); |
| 71 |
logShape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 72 |
this.logShape = logShape; |
| 73 |
|
| 74 |
addVariable(logitInvar); |
| 75 |
logitInvar.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1)); |
| 76 |
this.logitInvar = logitInvar; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
addVariable(modelChoose); |
| 83 |
modelChoose.addBounds(new Bounds.Int(modelChoose,0,1)); |
| 84 |
this.modelChoose = modelChoose; |
| 85 |
this.categoryCount = categoryCount +1; |
| 86 |
|
| 87 |
categoryRates = new double[this.categoryCount]; |
| 88 |
categoryProportions = new double[this.categoryCount]; |
| 89 |
|
| 90 |
ratesKnown = false; |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
public boolean integrateAcrossCategories() { |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
public int getCategoryCount() { |
| 99 |
return categoryCount; |
| 100 |
} |
| 101 |
|
| 102 |
public int getCategoryOfSite(int site) { |
| 103 |
throw new IllegalArgumentException("Integrating across categories"); |
| 104 |
} |
| 105 |
|
| 106 |
public double getRateForCategory(int category) { |
| 107 |
synchronized (this) { |
| 108 |
if (!ratesKnown) { |
| 109 |
calculateCategoryRates(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0; |
| 114 |
|
| 115 |
return categoryRates[category] * mu; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public double[] getCategoryRates() { |
| 120 |
synchronized (this) { |
| 121 |
if (!ratesKnown) { |
| 122 |
calculateCategoryRates(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
final double mu = (muParameter != null) ? muParameter.getParameterValue(0) : 1.0; |
| 127 |
|
| 128 |
final double[] rates = new double[categoryRates.length]; |
| 129 |
for (int i = 0; i < rates.length; i++) { |
| 130 |
rates[i] = categoryRates[i] * mu; |
| 131 |
} |
| 132 |
|
| 133 |
return rates; |
| 134 |
} |
| 135 |
|
| 136 |
public void getTransitionProbabilities(double substitutions, double[] matrix) { |
| 137 |
substitutionModel.getTransitionProbabilities(substitutions, matrix); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public double getProportionForCategory(int category) { |
| 147 |
synchronized (this) { |
| 148 |
if (!ratesKnown) { |
| 149 |
calculateCategoryRates(); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return categoryProportions[category]; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public double[] getCategoryProportions() { |
| 162 |
synchronized (this) { |
| 163 |
if (!ratesKnown) { |
| 164 |
calculateCategoryRates(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return categoryProportions; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
private void calculateCategoryRates() { |
| 176 |
|
| 177 |
double propVariable = 1.0; |
| 178 |
int cat = 0; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
categoryRates[0] = 0.0; |
| 183 |
|
| 184 |
categoryProportions[0] = modelChoose.getValue(INVAR_INDEX)*(1/(1+Math.exp(-logitInvar.getValue(0)))); |
| 185 |
|
| 186 |
propVariable = 1.0 - categoryProportions[0]; |
| 187 |
cat = 1; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
if (modelChoose.getValue(SHAPE_INDEX) == PRESENT) { |
| 192 |
|
| 193 |
|
| 194 |
final double a = Math.exp(logShape.getValue(0)); |
| 195 |
double mean = 0.0; |
| 196 |
final int gammaCatCount = categoryCount - cat; |
| 197 |
|
| 198 |
for (int i = 0; i < gammaCatCount; i++) { |
| 199 |
|
| 200 |
categoryRates[i + cat] = GammaDistribution.quantile((2.0 * i + 1.0) / (2.0 * gammaCatCount), a, 1.0 / a); |
| 201 |
|
| 202 |
mean += categoryRates[i + cat]; |
| 203 |
|
| 204 |
categoryProportions[i + cat] = propVariable / gammaCatCount; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
mean = (propVariable * mean) / gammaCatCount; |
| 209 |
|
| 210 |
for (int i = 0; i < gammaCatCount; i++) { |
| 211 |
|
| 212 |
categoryRates[i + cat] /= mean; |
| 213 |
|
| 214 |
} |
| 215 |
} else { |
| 216 |
|
| 217 |
final int gammaCatCount = categoryCount - cat; |
| 218 |
for (int i = 0; i < gammaCatCount; i++) { |
| 219 |
categoryRates[i + cat] = 1.0 / propVariable; |
| 220 |
categoryProportions[i + cat] = propVariable/gammaCatCount; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
ratesKnown = true; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public FrequencyModel getFrequencyModel() { |
| 237 |
return substitutionModel.getFrequencyModel(); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public SubstitutionModel getSubstitutionModel() { |
| 246 |
return substitutionModel; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 254 |
|
| 255 |
listenerHelper.fireModelChanged(this, object, index); |
| 256 |
} |
| 257 |
|
| 258 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 259 |
if (variable == logShape) { |
| 260 |
ratesKnown = false; |
| 261 |
} else if (variable == logitInvar) { |
| 262 |
ratesKnown = false; |
| 263 |
} else if (variable == modelChoose) { |
| 264 |
|
| 265 |
ratesKnown = false; |
| 266 |
} else { |
| 267 |
|
| 268 |
} |
| 269 |
listenerHelper.fireModelChanged(this, variable, index); |
| 270 |
} |
| 271 |
|
| 272 |
protected void storeState() { |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
protected void restoreState() { |
| 281 |
|
| 282 |
ratesKnown = false; |
| 283 |
} |
| 284 |
|
| 285 |
protected void acceptState() { |
| 286 |
} |
| 287 |
} |