| 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.inference.model.Parameter; |
| 29 |
import dr.inference.model.Variable; |
| 30 |
import dr.evolution.datatype.Microsatellite; |
| 31 |
|
| 32 |
import java.util.Map; |
| 33 |
import java.util.ArrayList; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MsatBMA extends MicrosatelliteModel{ |
| 41 |
private boolean logit; |
| 42 |
public static final int PROP_INDEX = 0; |
| 43 |
public static final int QUAD_INDEX = 1; |
| 44 |
public static final int BIAS_CONST_INDEX = 2; |
| 45 |
public static final int BIAS_LIN_INDEX = 3; |
| 46 |
public static final int GEO_INDEX = 4; |
| 47 |
public static final int PHASE_PROB_INDEX = 5; |
| 48 |
public static final double DEFAULT_VALUE = 0.0; |
| 49 |
public static final int PARAMETER_PRESENT = 1; |
| 50 |
|
| 51 |
public Parameter[][] paramModelMap; |
| 52 |
public Map<Integer, Integer> modelMap; |
| 53 |
public Parameter modelChoose; |
| 54 |
public Parameter modelIndicator; |
| 55 |
|
| 56 |
public ArrayList<Parameter> propRates = new ArrayList<Parameter>(); |
| 57 |
public ArrayList<Parameter> quadRates = new ArrayList<Parameter>(); |
| 58 |
public ArrayList<Parameter> biasConsts = new ArrayList<Parameter>(); |
| 59 |
public ArrayList<Parameter> biasLins = new ArrayList<Parameter>(); |
| 60 |
public ArrayList<Parameter> geos = new ArrayList<Parameter>(); |
| 61 |
public ArrayList<Parameter> phaseProb = new ArrayList<Parameter>(); |
| 62 |
|
| 63 |
public MsatBMA( |
| 64 |
Microsatellite msat, |
| 65 |
boolean logit, |
| 66 |
ArrayList<Parameter> propRates, |
| 67 |
ArrayList<Parameter> quadRates, |
| 68 |
ArrayList<Parameter> biasConsts, |
| 69 |
ArrayList<Parameter> biasLins, |
| 70 |
ArrayList<Parameter> geos, |
| 71 |
ArrayList<Parameter> phaseProb, |
| 72 |
Parameter[][] paramModelMap, |
| 73 |
Parameter modelChoose, |
| 74 |
Parameter modelIndicator, |
| 75 |
Map<Integer, Integer> modelMap){ |
| 76 |
|
| 77 |
super("MsatAveragingModel",msat, null, null); |
| 78 |
|
| 79 |
for(int i = 0; i < propRates.size(); i++){ |
| 80 |
addVariable(propRates.get(i)); |
| 81 |
} |
| 82 |
|
| 83 |
for(int i = 0; i < quadRates.size(); i++){ |
| 84 |
addVariable(quadRates.get(i)); |
| 85 |
} |
| 86 |
|
| 87 |
for(int i = 0; i < biasConsts.size(); i++){ |
| 88 |
addVariable(biasConsts.get(i)); |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
for(int i = 0; i < biasLins.size(); i++){ |
| 93 |
addVariable(biasLins.get(i)); |
| 94 |
} |
| 95 |
|
| 96 |
for(int i = 0; i < geos.size(); i++){ |
| 97 |
addVariable(geos.get(i)); |
| 98 |
} |
| 99 |
|
| 100 |
for(int i = 0; i < phaseProb.size();i++){ |
| 101 |
addVariable(phaseProb.get(i)); |
| 102 |
} |
| 103 |
|
| 104 |
addVariable(modelChoose); |
| 105 |
addVariable(modelIndicator); |
| 106 |
this.propRates = propRates; |
| 107 |
this.biasConsts = biasConsts; |
| 108 |
this.biasLins = biasLins; |
| 109 |
this.geos = geos; |
| 110 |
|
| 111 |
this.logit = logit; |
| 112 |
this.modelChoose = modelChoose; |
| 113 |
this.modelIndicator = modelIndicator; |
| 114 |
this.paramModelMap = paramModelMap; |
| 115 |
this.modelMap = modelMap; |
| 116 |
|
| 117 |
setupInfinitesimalRates(); |
| 118 |
setupMatrix(); |
| 119 |
|
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 125 |
modelUpdate = false; |
| 126 |
int paramIndex = -1; |
| 127 |
if(propRates.contains(variable)){ |
| 128 |
paramIndex = PROP_INDEX; |
| 129 |
}else if(quadRates.contains(variable)){ |
| 130 |
paramIndex = QUAD_INDEX; |
| 131 |
}else if(biasConsts.contains(variable)){ |
| 132 |
paramIndex = BIAS_CONST_INDEX; |
| 133 |
}else if(biasLins.contains(variable)){ |
| 134 |
paramIndex = BIAS_LIN_INDEX; |
| 135 |
}else if(geos.contains(variable)){ |
| 136 |
paramIndex = GEO_INDEX; |
| 137 |
}else if(phaseProb.contains(variable)){ |
| 138 |
paramIndex = PHASE_PROB_INDEX; |
| 139 |
} |
| 140 |
|
| 141 |
if(paramIndex > -1){ |
| 142 |
|
| 143 |
|
| 144 |
if(paramModelMap[paramIndex][modelMap.get(getBitVectorValue())] != null){ |
| 145 |
updateMatrix = true; |
| 146 |
modelUpdate = true; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
}else if(variable == modelChoose){ |
| 151 |
updateMatrix = true; |
| 152 |
indicateModel(); |
| 153 |
modelUpdate = true; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
public void indicateModel(){ |
| 161 |
modelIndicator.setParameterValueQuietly(0, modelMap.get(getBitVectorValue())); |
| 162 |
} |
| 163 |
|
| 164 |
public void setupInfinitesimalRates(){ |
| 165 |
double rowSum; |
| 166 |
double prop = DEFAULT_VALUE; |
| 167 |
double quad = DEFAULT_VALUE; |
| 168 |
double biasConst = DEFAULT_VALUE; |
| 169 |
double biasLin = DEFAULT_VALUE; |
| 170 |
double geo = DEFAULT_VALUE; |
| 171 |
double phaseProb = DEFAULT_VALUE; |
| 172 |
|
| 173 |
infinitesimalRateMatrix = new double[stateCount][stateCount]; |
| 174 |
|
| 175 |
|
| 176 |
if((int)modelChoose.getParameterValue(PROP_INDEX) == PARAMETER_PRESENT){ |
| 177 |
|
| 178 |
prop = getModelParameterValue(PROP_INDEX); |
| 179 |
if((int)modelChoose.getParameterValue(QUAD_INDEX) == PARAMETER_PRESENT){ |
| 180 |
quad = getModelParameterValue(QUAD_INDEX); |
| 181 |
} |
| 182 |
} |
| 183 |
for(int i = 0; i < stateCount;i++){ |
| 184 |
rowSum = 0.0; |
| 185 |
if(i - 1 > -1){ |
| 186 |
infinitesimalRateMatrix[i][i - 1] = 1+prop*i; |
| 187 |
rowSum = rowSum + infinitesimalRateMatrix[i][i - 1]; |
| 188 |
} |
| 189 |
|
| 190 |
if(i + 1 < stateCount){ |
| 191 |
infinitesimalRateMatrix[i][i + 1] = 1+prop*i; |
| 192 |
rowSum = rowSum + infinitesimalRateMatrix[i][i + 1]; |
| 193 |
} |
| 194 |
infinitesimalRateMatrix[i][i] = -rowSum; |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
if((int)modelChoose.getParameterValue(BIAS_CONST_INDEX) == PARAMETER_PRESENT){ |
| 199 |
|
| 200 |
biasConst = getModelParameterValue(BIAS_CONST_INDEX); |
| 201 |
|
| 202 |
if((int)modelChoose.getParameterValue(BIAS_LIN_INDEX) == PARAMETER_PRESENT){ |
| 203 |
biasLin = getModelParameterValue(BIAS_LIN_INDEX); |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
double[][] subRates = infinitesimalRateMatrix; |
| 208 |
infinitesimalRateMatrix = new double[stateCount][stateCount]; |
| 209 |
|
| 210 |
LinearBiasModel.setupInfinitesimalRates( |
| 211 |
infinitesimalRateMatrix, |
| 212 |
subRates, |
| 213 |
biasConst, |
| 214 |
biasLin, |
| 215 |
stateCount, |
| 216 |
logit |
| 217 |
); |
| 218 |
} |
| 219 |
|
| 220 |
if((int)modelChoose.getParameterValue(GEO_INDEX) == PARAMETER_PRESENT){ |
| 221 |
geo = getModelParameterValue(GEO_INDEX); |
| 222 |
if((int)modelChoose.getParameterValue(PHASE_PROB_INDEX) == PARAMETER_PRESENT){ |
| 223 |
phaseProb = getModelParameterValue(PHASE_PROB_INDEX); |
| 224 |
} |
| 225 |
double[][] subRates = infinitesimalRateMatrix; |
| 226 |
infinitesimalRateMatrix = new double[stateCount][stateCount]; |
| 227 |
|
| 228 |
TwoPhaseModel.setupInfinitesimalRates( |
| 229 |
stateCount, |
| 230 |
geo, |
| 231 |
phaseProb, |
| 232 |
infinitesimalRateMatrix, |
| 233 |
subRates |
| 234 |
); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private double getModelParameterValue(int paramIndex){ |
| 239 |
int modelCode = modelMap.get(getBitVectorValue()); |
| 240 |
return (paramModelMap[paramIndex][modelCode]).getParameterValue(0); |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
private int getBitVectorValue(){ |
| 245 |
String bitVec = ""; |
| 246 |
for(int i = 0; i < modelChoose.getDimension(); i++){ |
| 247 |
bitVec = bitVec + (int)modelChoose.getParameterValue(i); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
return Integer.parseInt(bitVec,2); |
| 252 |
} |
| 253 |
|
| 254 |
public void computeStationaryDistribution(){ |
| 255 |
if((int)modelChoose.getParameterValue(GEO_INDEX) == PARAMETER_PRESENT){ |
| 256 |
computeTwoPhaseStationaryDistribution(); |
| 257 |
}else{ |
| 258 |
computeOnePhaseStationaryDistribution(); |
| 259 |
} |
| 260 |
super.computeStationaryDistribution(); |
| 261 |
} |
| 262 |
|
| 263 |
} |