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