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