| 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.evomodel.substmodel; |
| 27 |
|
| 28 |
import dr.inference.model.*; |
| 29 |
import dr.evolution.datatype.DataType; |
| 30 |
|
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SVSComplexSubstitutionModel extends ComplexSubstitutionModel implements Likelihood, |
| 38 |
BayesianStochasticSearchVariableSelection { |
| 39 |
|
| 40 |
public SVSComplexSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel, |
| 41 |
Parameter ratesParameter, Parameter indicatorsParameter) { |
| 42 |
super(name, dataType, freqModel, ratesParameter); |
| 43 |
|
| 44 |
if (indicatorsParameter == null) { |
| 45 |
this.indicatorsParameter = new Parameter.Default(ratesParameter.getDimension(), 1.0); |
| 46 |
} else { |
| 47 |
this.indicatorsParameter = indicatorsParameter; |
| 48 |
addVariable(indicatorsParameter); |
| 49 |
} |
| 50 |
|
| 51 |
setupIndicatorDimensionNames(-1); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void setupRelativeRates(double[] rates) { |
| 56 |
for (int i = 0; i < rates.length; i++) { |
| 57 |
rates[i] = ratesParameter.getParameterValue(i) * indicatorsParameter.getParameterValue(i); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
protected void setupIndicatorDimensionNames(int relativeTo) { |
| 62 |
List<String> indicatorNames = new ArrayList<String>(); |
| 63 |
|
| 64 |
String indicatorPrefix = indicatorsParameter.getParameterName(); |
| 65 |
|
| 66 |
for (int i = 0; i < dataType.getStateCount(); ++i) { |
| 67 |
for (int j = i + 1; j < dataType.getStateCount(); ++j) { |
| 68 |
indicatorNames.add(getDimensionString(i, j, indicatorPrefix)); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
for (int j = 0; j < dataType.getStateCount(); ++j) { |
| 73 |
for (int i = j + 1; i < dataType.getStateCount(); ++i) { |
| 74 |
indicatorNames.add(getDimensionString(i, j, indicatorPrefix)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
String[] tmp = new String[0]; |
| 79 |
indicatorsParameter.setDimensionNames(indicatorNames.toArray(tmp)); |
| 80 |
} |
| 81 |
|
| 82 |
public Parameter getIndicators() { |
| 83 |
return indicatorsParameter; |
| 84 |
} |
| 85 |
|
| 86 |
public boolean validState() { |
| 87 |
return !updateMatrix || |
| 88 |
BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this); |
| 89 |
} |
| 90 |
|
| 91 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 92 |
if (variable == ratesParameter && indicatorsParameter.getParameterValue(index) == 0) |
| 93 |
return; |
| 94 |
super.handleVariableChangedEvent(variable,index,type); |
| 95 |
} |
| 96 |
|
| 97 |
public Model getModel() { |
| 98 |
return this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public void makeDirty() { |
| 105 |
updateMatrix = true; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
@Override |
| 116 |
public boolean isUsed() { |
| 117 |
return super.isUsed() && isUsed; |
| 118 |
} |
| 119 |
|
| 120 |
public void setUsed() { |
| 121 |
isUsed = true; |
| 122 |
} |
| 123 |
|
| 124 |
private boolean isUsed = false; |
| 125 |
|
| 126 |
private double[] probability = null; |
| 127 |
|
| 128 |
private final Parameter indicatorsParameter; |
| 129 |
} |