| 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 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.logging.Logger; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class DirichletSiteModel extends AbstractModel implements SiteModel { |
| 46 |
|
| 47 |
public static final String SUBSTITUTION_MODEL = "substitutionModel"; |
| 48 |
public static final String DIRICHLET_SITE_MODEL = "DirichletSiteModel"; |
| 49 |
public static final String CATEGORIES = "categories"; |
| 50 |
public static final String RATES = "rates"; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public DirichletSiteModel(SubstitutionModel substitutionModel, int n_categories) { |
| 57 |
this(substitutionModel, new Parameter.Default(new double[n_categories])); |
| 58 |
} |
| 59 |
|
| 60 |
public DirichletSiteModel(SubstitutionModel substitutionModel, Parameter ratesParameter) { |
| 61 |
super(DIRICHLET_SITE_MODEL); |
| 62 |
|
| 63 |
|
| 64 |
this.substitutionModel = substitutionModel; |
| 65 |
addModel(substitutionModel); |
| 66 |
|
| 67 |
|
| 68 |
int n_categories = ratesParameter.getDimension(); |
| 69 |
categoryRates = new double[n_categories]; |
| 70 |
categoryProportions = new double[n_categories]; |
| 71 |
|
| 72 |
|
| 73 |
ratesKnown = false; |
| 74 |
|
| 75 |
|
| 76 |
for(int i=0;i<n_categories;i++) { |
| 77 |
ratesParameter.setParameterValue(i, 1.0/n_categories); |
| 78 |
categoryProportions[i] = 1.0/n_categories; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
this.ratesParameter = ratesParameter; |
| 83 |
addVariable(this.ratesParameter); |
| 84 |
this.ratesParameter.addBounds(new Parameter.DefaultBounds(1.0, 0.0, ratesParameter.getDimension())); |
| 85 |
} |
| 86 |
|
| 87 |
public Parameter getRatesParameter() { |
| 88 |
return ratesParameter; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public boolean integrateAcrossCategories() { |
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
public int getCategoryCount() { |
| 106 |
return categoryRates.length; |
| 107 |
} |
| 108 |
|
| 109 |
public int getCategoryOfSite(int site) { |
| 110 |
throw new IllegalArgumentException("Integrating across categories"); |
| 111 |
} |
| 112 |
|
| 113 |
public double getRateForCategory(int category) { |
| 114 |
calculateCategoryRates(); |
| 115 |
|
| 116 |
return categoryRates[category]; |
| 117 |
} |
| 118 |
|
| 119 |
public double[] getCategoryRates() { |
| 120 |
calculateCategoryRates(); |
| 121 |
|
| 122 |
double[] rates = new double[categoryRates.length]; |
| 123 |
|
| 124 |
for (int i = 0; i < rates.length; i++) { |
| 125 |
rates[i] = categoryRates[i]; |
| 126 |
} |
| 127 |
|
| 128 |
return rates; |
| 129 |
} |
| 130 |
|
| 131 |
public void getTransitionProbabilities(double substitutions, double[] matrix) { |
| 132 |
substitutionModel.getTransitionProbabilities(substitutions, matrix); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public double getProportionForCategory(int category) { |
| 142 |
calculateCategoryRates(); |
| 143 |
|
| 144 |
return categoryProportions[category]; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public double[] getCategoryProportions() { |
| 153 |
calculateCategoryRates(); |
| 154 |
|
| 155 |
return categoryProportions; |
| 156 |
} |
| 157 |
|
| 158 |
private double get_substitution_scale() |
| 159 |
{ |
| 160 |
double scale=0; |
| 161 |
for(int i=0;i<categoryRates.length;i++) { |
| 162 |
|
| 163 |
scale += categoryProportions[i]*ratesParameter.getParameterValue(i); |
| 164 |
} |
| 165 |
return scale; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
private void calculateCategoryRates() |
| 172 |
{ |
| 173 |
synchronized (this) { |
| 174 |
if (ratesKnown) |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
double scale = get_substitution_scale(); |
| 179 |
|
| 180 |
double temp = 1.0/scale; |
| 181 |
|
| 182 |
for(int i=0;i<categoryRates.length;i++) |
| 183 |
categoryRates[i] = ratesParameter.getParameterValue(i)*temp; |
| 184 |
|
| 185 |
ratesKnown = true; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public FrequencyModel getFrequencyModel() { |
| 194 |
return substitutionModel.getFrequencyModel(); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public SubstitutionModel getSubstitutionModel() { |
| 203 |
return substitutionModel; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 211 |
|
| 212 |
listenerHelper.fireModelChanged(this, object, index); |
| 213 |
} |
| 214 |
|
| 215 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 216 |
if (variable == ratesParameter) { |
| 217 |
ratesKnown = false; |
| 218 |
} else { |
| 219 |
|
| 220 |
} |
| 221 |
listenerHelper.fireModelChanged(this, variable, index); |
| 222 |
} |
| 223 |
|
| 224 |
protected void storeState() { |
| 225 |
} |
| 226 |
|
| 227 |
protected void restoreState() { |
| 228 |
ratesKnown = false; |
| 229 |
} |
| 230 |
|
| 231 |
protected void acceptState() { |
| 232 |
} |
| 233 |
|
| 234 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 235 |
|
| 236 |
public String[] getParserNames() { |
| 237 |
return new String[] { |
| 238 |
getParserName(), "beast_"+getParserName() |
| 239 |
}; |
| 240 |
} |
| 241 |
|
| 242 |
public String getParserName() { |
| 243 |
return DIRICHLET_SITE_MODEL; |
| 244 |
} |
| 245 |
|
| 246 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException |
| 247 |
{ |
| 248 |
SubstitutionModel substitutionModel = (SubstitutionModel) xo.getElementFirstChild(SUBSTITUTION_MODEL); |
| 249 |
|
| 250 |
String msg = ""; |
| 251 |
|
| 252 |
Parameter ratesParameter = null; |
| 253 |
if (xo.hasChildNamed("rates")) { |
| 254 |
XMLObject cxo = (XMLObject) xo.getChild("rates"); |
| 255 |
ratesParameter = (Parameter) cxo.getChild(Parameter.class); |
| 256 |
msg += "\n with " + ratesParameter.getDimension() + " categories."; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if (msg.length() > 0) { |
| 261 |
Logger.getLogger("dr.evomodel").info("Creating site model: " + msg); |
| 262 |
} else { |
| 263 |
Logger.getLogger("dr.evomodel").info("Creating site model."); |
| 264 |
} |
| 265 |
|
| 266 |
return new DirichletSiteModel(substitutionModel, ratesParameter); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public String getParserDescription() { |
| 274 |
return "A SiteModel that has Dirichlet distributed rates across sites"; |
| 275 |
} |
| 276 |
|
| 277 |
public Class getReturnType() { |
| 278 |
return DirichletSiteModel.class; |
| 279 |
} |
| 280 |
|
| 281 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 282 |
return rules; |
| 283 |
} |
| 284 |
|
| 285 |
private final XMLSyntaxRule[] rules = { |
| 286 |
new ElementRule(SUBSTITUTION_MODEL, new XMLSyntaxRule[]{ |
| 287 |
new ElementRule(SubstitutionModel.class) |
| 288 |
}), |
| 289 |
new ElementRule(RATES, new XMLSyntaxRule[]{ |
| 290 |
new ElementRule(Parameter.class) |
| 291 |
}) |
| 292 |
}; |
| 293 |
}; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
private SubstitutionModel substitutionModel = null; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
private Parameter ratesParameter; |
| 304 |
|
| 305 |
private boolean ratesKnown; |
| 306 |
|
| 307 |
private double[] categoryRates; |
| 308 |
|
| 309 |
private double[] categoryProportions; |
| 310 |
} |