| 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.oldevomodel.substmodel.YangCodonModel; |
| 31 |
import dr.oldevomodelxml.sitemodel.SampleStateAndCategoryModelParser; |
| 32 |
import dr.inference.model.*; |
| 33 |
|
| 34 |
import java.util.Vector; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class SampleStateAndCategoryModel extends AbstractModel implements SiteModel, CategorySampleModel { |
| 45 |
|
| 46 |
public static final double OMEGA_MAX_VALUE = 100.0; |
| 47 |
public static final double OMEGA_MIN_VALUE = 0.0; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public SampleStateAndCategoryModel(Parameter muParameter, |
| 54 |
Parameter categoriesParameter, |
| 55 |
Vector substitutionModels) { |
| 56 |
|
| 57 |
super(SampleStateAndCategoryModelParser.SAMPLE_STATE_AND_CATEGORY_MODEL); |
| 58 |
|
| 59 |
|
| 60 |
this.substitutionModels = substitutionModels; |
| 61 |
|
| 62 |
for (int i = 0; i < substitutionModels.size(); i++) { |
| 63 |
addModel((SubstitutionModel) substitutionModels.elementAt(i)); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
this.categoryCount = substitutionModels.size(); |
| 68 |
sitesInCategory = new int[categoryCount]; |
| 69 |
|
| 70 |
|
| 71 |
this.muParameter = muParameter; |
| 72 |
addVariable(muParameter); |
| 73 |
muParameter.addBounds(new Parameter.DefaultBounds(1000.0, 0.0, 1)); |
| 74 |
|
| 75 |
this.categoriesParameter = categoriesParameter; |
| 76 |
addVariable(categoriesParameter); |
| 77 |
|
| 78 |
if (categoryCount > 1) { |
| 79 |
for (int i = 0; i < categoryCount; i++) { |
| 80 |
Parameter p = (Parameter)((YangCodonModel) substitutionModels.elementAt(i)).getVariable(0); |
| 81 |
Parameter lower = null; |
| 82 |
Parameter upper = null; |
| 83 |
|
| 84 |
if (i == 0) { |
| 85 |
upper = (Parameter)((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0); |
| 86 |
p.addBounds(new omegaBounds(lower, upper)); |
| 87 |
} else { |
| 88 |
if (i == (categoryCount - 1)) { |
| 89 |
lower = (Parameter)((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0); |
| 90 |
p.addBounds(new omegaBounds(lower, upper)); |
| 91 |
} else { |
| 92 |
upper = (Parameter)((YangCodonModel) substitutionModels.elementAt(i + 1)).getVariable(0); |
| 93 |
lower = (Parameter)((YangCodonModel) substitutionModels.elementAt(i - 1)).getVariable(0); |
| 94 |
p.addBounds(new omegaBounds(lower, upper)); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public SubstitutionModel getSubstitutionModel() { |
| 106 |
return null; |
| 107 |
} |
| 108 |
|
| 109 |
public boolean integrateAcrossCategories() { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
public int getCategoryCount() { |
| 114 |
return categoryCount; |
| 115 |
} |
| 116 |
|
| 117 |
public int getCategoryOfSite(int site) { |
| 118 |
return (int) categoriesParameter.getParameterValue(site); |
| 119 |
} |
| 120 |
|
| 121 |
public double getRateForCategory(int category) { |
| 122 |
throw new RuntimeException("getRateForCategory not available in this siteModel"); |
| 123 |
} |
| 124 |
|
| 125 |
public double[] getCategoryRates() { |
| 126 |
throw new RuntimeException("getCategoryRates not available in this siteModel"); |
| 127 |
} |
| 128 |
|
| 129 |
public double getSubstitutionsForCategory(int category, double time) { |
| 130 |
throw new RuntimeException("getSubstitutionsForCategory not available in this siteModel"); |
| 131 |
} |
| 132 |
|
| 133 |
public void getTransitionProbabilities(double substitutions, double[] matrix) { |
| 134 |
throw new RuntimeException("getTransitionProbabilities not available in this siteModel"); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public FrequencyModel getFrequencyModel() { |
| 143 |
return ((SubstitutionModel) substitutionModels.elementAt(0)).getFrequencyModel(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public void setCategoriesParameter(int siteCount) { |
| 155 |
categoriesParameter.setDimension(siteCount); |
| 156 |
categoriesParameter.addBounds(new Parameter.DefaultBounds(categoryCount, 0.0, siteCount)); |
| 157 |
for (int i = 0; i < siteCount; i++) { |
| 158 |
|
| 159 |
int r = (int) (Math.random() * categoryCount); |
| 160 |
categoriesParameter.setParameterValue(i, r); |
| 161 |
} |
| 162 |
|
| 163 |
for (int j = 0; j < categoryCount; j++) { |
| 164 |
sitesInCategory[j] = 0; |
| 165 |
} |
| 166 |
|
| 167 |
for (int i = 0; i < siteCount; i++) { |
| 168 |
int value = (int) categoriesParameter.getParameterValue(i); |
| 169 |
sitesInCategory[value] = sitesInCategory[value] + 1; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public void addSitesInCategoryCount(int category) { |
| 174 |
sitesInCategory[category] = sitesInCategory[category] + 1; |
| 175 |
} |
| 176 |
|
| 177 |
public void subtractSitesInCategoryCount(int category) { |
| 178 |
sitesInCategory[category] = sitesInCategory[category] - 1; |
| 179 |
} |
| 180 |
|
| 181 |
public int getSitesInCategoryCount(int category) { |
| 182 |
return sitesInCategory[category]; |
| 183 |
} |
| 184 |
|
| 185 |
public void toggleRandomSite() { |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public double getProportionForCategory(int category) { |
| 196 |
throw new IllegalArgumentException("Not integrating across categories"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public double[] getCategoryProportions() { |
| 205 |
throw new IllegalArgumentException("Not integrating across categories"); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 213 |
|
| 214 |
listenerHelper.fireModelChanged(this, object, index); |
| 215 |
} |
| 216 |
|
| 217 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 218 |
|
| 219 |
if (variable == categoriesParameter) |
| 220 |
listenerHelper.fireModelChanged(this, this, index); |
| 221 |
} |
| 222 |
|
| 223 |
protected void storeState() { |
| 224 |
} |
| 225 |
|
| 226 |
protected void restoreState() { |
| 227 |
} |
| 228 |
|
| 229 |
protected void acceptState() { |
| 230 |
} |
| 231 |
|
| 232 |
public String toString() { |
| 233 |
StringBuffer s = new StringBuffer(); |
| 234 |
|
| 235 |
for (int i = 0; i < categoryCount; i++) { |
| 236 |
s.append(sitesInCategory[i] + "\t"); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
return s.toString(); |
| 244 |
} |
| 245 |
|
| 246 |
private class omegaBounds implements Bounds<Double> { |
| 247 |
|
| 248 |
private final Parameter lowerOmega, upperOmega; |
| 249 |
|
| 250 |
public omegaBounds(Parameter lowerOmega, Parameter upperOmega) { |
| 251 |
|
| 252 |
this.lowerOmega = lowerOmega; |
| 253 |
this.upperOmega = upperOmega; |
| 254 |
} |
| 255 |
|
| 256 |
public omegaBounds(Parameter nearestOmega, boolean isUpper) { |
| 257 |
|
| 258 |
if (isUpper) { |
| 259 |
lowerOmega = nearestOmega; |
| 260 |
upperOmega = null; |
| 261 |
} else { |
| 262 |
lowerOmega = null; |
| 263 |
upperOmega = nearestOmega; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public Double getUpperLimit(int dimension) { |
| 271 |
|
| 272 |
if (dimension != 0) |
| 273 |
throw new RuntimeException("omega parameters have wrong dimension " + dimension); |
| 274 |
|
| 275 |
if (upperOmega == null) |
| 276 |
return OMEGA_MAX_VALUE; |
| 277 |
else |
| 278 |
return upperOmega.getParameterValue(dimension); |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public Double getLowerLimit(int dimension) { |
| 286 |
|
| 287 |
if (dimension != 0) |
| 288 |
throw new RuntimeException("omega parameters have wrong dimension " + dimension); |
| 289 |
|
| 290 |
if (lowerOmega == null) |
| 291 |
return OMEGA_MIN_VALUE; |
| 292 |
|
| 293 |
else |
| 294 |
return lowerOmega.getParameterValue(dimension); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public int getBoundsDimension() { |
| 301 |
return 1; |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
private final Parameter muParameter; |
| 310 |
|
| 311 |
private final int[] sitesInCategory; |
| 312 |
|
| 313 |
private final Parameter categoriesParameter; |
| 314 |
|
| 315 |
private final Vector substitutionModels; |
| 316 |
|
| 317 |
private final int categoryCount; |
| 318 |
|
| 319 |
|
| 320 |
} |