| 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.substmodel; |
| 27 |
|
| 28 |
import dr.evolution.datatype.TwoStateCovarion; |
| 29 |
import dr.oldevomodelxml.substmodel.TwoStateCovarionModelParser; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class TwoStateCovarionModel extends AbstractSubstitutionModel { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public TwoStateCovarionModel(TwoStateCovarion dataType, FrequencyModel freqModel, |
| 48 |
Parameter alphaParameter, |
| 49 |
Parameter switchingParameter) { |
| 50 |
super(TwoStateCovarionModelParser.COVARION_MODEL, dataType, freqModel); |
| 51 |
|
| 52 |
alpha = alphaParameter; |
| 53 |
this.switchingParameter = switchingParameter; |
| 54 |
|
| 55 |
addVariable(alpha); |
| 56 |
addVariable(switchingParameter); |
| 57 |
setupRelativeRates(); |
| 58 |
} |
| 59 |
|
| 60 |
protected void frequenciesChanged() { |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
protected void ratesChanged() { |
| 65 |
setupRelativeRates(); |
| 66 |
} |
| 67 |
|
| 68 |
protected void setupRelativeRates() { |
| 69 |
|
| 70 |
relativeRates[0] = alpha.getParameterValue(0); |
| 71 |
relativeRates[1] = switchingParameter.getParameterValue(0); |
| 72 |
relativeRates[2] = 0.0; |
| 73 |
relativeRates[3] = 0.0; |
| 74 |
relativeRates[4] = switchingParameter.getParameterValue(0); |
| 75 |
relativeRates[5] = 1.0; |
| 76 |
} |
| 77 |
|
| 78 |
public String toString() { |
| 79 |
|
| 80 |
return SubstitutionModelUtils.toString(relativeRates, dataType, true, 2); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
void normalize(double[][] matrix, double[] pi) { |
| 90 |
|
| 91 |
if (isNormalized) { |
| 92 |
|
| 93 |
double subst = 0.0; |
| 94 |
int dimension = pi.length; |
| 95 |
|
| 96 |
for (int i = 0; i < dimension; i++) { |
| 97 |
subst += -matrix[i][i] * pi[i]; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
for (int i = 0; i < dimension; i++) { |
| 102 |
for (int j = 0; j < dimension; j++) { |
| 103 |
matrix[i][j] = matrix[i][j] / subst; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
double switchingProportion = 0.0; |
| 108 |
switchingProportion += matrix[0][2] * pi[2]; |
| 109 |
switchingProportion += matrix[2][0] * pi[0]; |
| 110 |
switchingProportion += matrix[1][3] * pi[3]; |
| 111 |
switchingProportion += matrix[3][1] * pi[1]; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
for (int i = 0; i < dimension; i++) { |
| 117 |
for (int j = 0; j < dimension; j++) { |
| 118 |
matrix[i][j] = matrix[i][j] / (1.0 - switchingProportion); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
private Parameter alpha; |
| 125 |
private Parameter switchingParameter; |
| 126 |
|
| 127 |
|
| 128 |
private boolean isNormalized = true; |
| 129 |
|
| 130 |
} |