| 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.inference.model.*; |
| 29 |
import dr.inference.distribution.ParametricDistributionModel; |
| 30 |
import dr.oldevomodelxml.sitemodel.SampleQuantileLociRatesParser; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class SampleQuantileLociRates extends AbstractModel { |
| 37 |
private CompoundParameter lociRates; |
| 38 |
private Parameter rateQuantileParameter; |
| 39 |
private ParametricDistributionModel distrModel; |
| 40 |
private double normalizeRateTo; |
| 41 |
private boolean normalize; |
| 42 |
private double scaleFactor; |
| 43 |
private double[] rates; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public SampleQuantileLociRates( |
| 48 |
CompoundParameter lociRates, |
| 49 |
Parameter rateQuantileParameter, |
| 50 |
ParametricDistributionModel model, |
| 51 |
boolean normalize, |
| 52 |
double normalizeLociRateTo) { |
| 53 |
super(SampleQuantileLociRatesParser.SAMPLE_QUANTILE_LOCI_RATES); |
| 54 |
|
| 55 |
this.lociRates = lociRates; |
| 56 |
|
| 57 |
this.rateQuantileParameter = rateQuantileParameter; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
this.distrModel = model; |
| 63 |
this.normalizeRateTo = normalizeLociRateTo; |
| 64 |
this.normalize = normalize; |
| 65 |
rates = new double[rateQuantileParameter.getDimension()]; |
| 66 |
setupRates(); |
| 67 |
setupRelativeRates(); |
| 68 |
|
| 69 |
addModel(distrModel); |
| 70 |
addVariable(this.rateQuantileParameter); |
| 71 |
} |
| 72 |
|
| 73 |
private void setupRates(){ |
| 74 |
for(int i = 0; i < rates.length; i++){ |
| 75 |
rates[i]= distrModel.quantile(rateQuantileParameter.getParameterValue(i)); |
| 76 |
|
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private void setupRates(int rateQuantileParameterIndex){ |
| 81 |
rates[rateQuantileParameterIndex]= distrModel.quantile(rateQuantileParameter.getParameterValue(rateQuantileParameterIndex)); |
| 82 |
} |
| 83 |
|
| 84 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 85 |
if (model == distrModel) { |
| 86 |
setupRates(); |
| 87 |
setupRelativeRates(); |
| 88 |
|
| 89 |
fireModelChanged(); |
| 90 |
}else if (model == rateQuantileParameter) { |
| 91 |
|
| 92 |
setupRates(index); |
| 93 |
fireModelChanged(null, index); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 98 |
|
| 99 |
setupRates(index); |
| 100 |
setupRelativeRates(); |
| 101 |
fireModelChanged(null, index); |
| 102 |
} |
| 103 |
|
| 104 |
protected void storeState() { |
| 105 |
} |
| 106 |
protected void acceptState() { |
| 107 |
} |
| 108 |
protected void restoreState() { |
| 109 |
|
| 110 |
} |
| 111 |
private void setupRelativeRates(){ |
| 112 |
|
| 113 |
if(normalize){ |
| 114 |
computeFactor(); |
| 115 |
} |
| 116 |
int lociCount = rateQuantileParameter.getDimension(); |
| 117 |
for(int i = 0; i < lociCount; i ++){ |
| 118 |
|
| 119 |
lociRates.setParameterValue(i,rates[i]*scaleFactor); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
private void computeFactor(){ |
| 125 |
double sumRates = 0.0; |
| 126 |
int lociCount = rateQuantileParameter.getDimension(); |
| 127 |
for(int i = 0; i < lociCount; i++){ |
| 128 |
|
| 129 |
sumRates += rates[i]; |
| 130 |
} |
| 131 |
scaleFactor = normalizeRateTo/(sumRates/lociCount); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
} |