| 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.evolution.datatype.DataType; |
| 29 |
import dr.inference.loggers.LogColumn; |
| 30 |
import dr.inference.loggers.NumberColumn; |
| 31 |
import dr.inference.model.*; |
| 32 |
import dr.util.Citable; |
| 33 |
import dr.util.Citation; |
| 34 |
import dr.util.CommonCitations; |
| 35 |
|
| 36 |
import java.util.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class SVSGeneralSubstitutionModel extends GeneralSubstitutionModel implements Likelihood, |
| 43 |
BayesianStochasticSearchVariableSelection, Citable { |
| 44 |
|
| 45 |
public SVSGeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel, |
| 46 |
Parameter ratesParameter, Parameter indicatorsParameter) { |
| 47 |
super(name, dataType, freqModel, ratesParameter, -1); |
| 48 |
|
| 49 |
if (indicatorsParameter == null) { |
| 50 |
this.indicatorsParameter = new Parameter.Default(ratesParameter.getDimension(), 1.0); |
| 51 |
} else { |
| 52 |
this.indicatorsParameter = indicatorsParameter; |
| 53 |
addVariable(indicatorsParameter); |
| 54 |
} |
| 55 |
|
| 56 |
setupIndicatorDimensionNames(-1); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
protected void setupRelativeRates(double[] rates) { |
| 61 |
for (int i = 0; i < rates.length; i++) { |
| 62 |
rates[i] = ratesParameter.getParameterValue(i) * indicatorsParameter.getParameterValue(i); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
protected void setupIndicatorDimensionNames(int relativeTo) { |
| 67 |
List<String> indicatorNames = new ArrayList<String>(); |
| 68 |
|
| 69 |
String indicatorPrefix = indicatorsParameter.getParameterName(); |
| 70 |
|
| 71 |
for (int i = 0; i < dataType.getStateCount(); ++i) { |
| 72 |
for (int j = i + 1; j < dataType.getStateCount(); ++j) { |
| 73 |
indicatorNames.add(getDimensionString(i, j, indicatorPrefix)); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
String[] tmp = new String[0]; |
| 78 |
indicatorsParameter.setDimensionNames(indicatorNames.toArray(tmp)); |
| 79 |
} |
| 80 |
|
| 81 |
public Parameter getIndicators() { |
| 82 |
return indicatorsParameter; |
| 83 |
} |
| 84 |
|
| 85 |
public boolean validState() { |
| 86 |
return !updateMatrix || BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this); |
| 87 |
} |
| 88 |
|
| 89 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 90 |
if (variable == ratesParameter && indicatorsParameter.getParameterValue(index) == 0) |
| 91 |
return; |
| 92 |
super.handleVariableChangedEvent(variable,index,type); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public Model getModel() { |
| 101 |
return this; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public double getLogLikelihood() { |
| 110 |
if (updateMatrix) { |
| 111 |
if (!BayesianStochasticSearchVariableSelection.Utils.connectedAndWellConditioned(probability,this)) { |
| 112 |
return Double.NEGATIVE_INFINITY; |
| 113 |
} |
| 114 |
} |
| 115 |
return 0; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public boolean evaluateEarly() { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void makeDirty() { |
| 130 |
updateMatrix = true; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public String prettyName() { |
| 137 |
return "SVSGeneralSubstitutionModel-connectedness"; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public Set<Likelihood> getLikelihoodSet() { |
| 142 |
return new HashSet<Likelihood>(Arrays.asList(this)); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public boolean isUsed() { |
| 147 |
return super.isUsed() && isUsed; |
| 148 |
} |
| 149 |
|
| 150 |
public void setUsed() { |
| 151 |
isUsed = true; |
| 152 |
} |
| 153 |
|
| 154 |
private boolean isUsed = false; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public LogColumn[] getColumns() { |
| 161 |
return new LogColumn[]{ |
| 162 |
new LikelihoodColumn(getId()) |
| 163 |
}; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public Citation.Category getCategory() { |
| 168 |
return Citation.Category.SUBSTITUTION_MODELS; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public String getDescription() { |
| 173 |
return "Stochastic search variable selection, reversible substitution model"; |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public List<Citation> getCitations() { |
| 178 |
return Collections.singletonList(CommonCitations.LEMEY_2009_BAYESIAN); |
| 179 |
} |
| 180 |
|
| 181 |
protected class LikelihoodColumn extends NumberColumn { |
| 182 |
public LikelihoodColumn(String label) { |
| 183 |
super(label); |
| 184 |
} |
| 185 |
|
| 186 |
public double getDoubleValue() { |
| 187 |
return getLogLikelihood(); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
private double[] probability = null; |
| 192 |
|
| 193 |
private final Parameter indicatorsParameter; |
| 194 |
|
| 195 |
|
| 196 |
} |