| 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.model.Parameter; |
| 30 |
import dr.inference.model.DuplicatedParameter; |
| 31 |
|
| 32 |
import java.util.ArrayList; |
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class GeneralSubstitutionModel extends BaseSubstitutionModel { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
protected int ratesRelativeTo; |
| 49 |
|
| 50 |
public GeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel, |
| 51 |
Parameter ratesParameter, int relativeTo) { |
| 52 |
this(name, dataType, freqModel, ratesParameter, relativeTo, null); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public GeneralSubstitutionModel(String name, DataType dataType, FrequencyModel freqModel, |
| 62 |
Parameter ratesParameter, int relativeTo, EigenSystem eigenSystem) { |
| 63 |
|
| 64 |
super(name, dataType, freqModel, eigenSystem); |
| 65 |
|
| 66 |
this.ratesParameter = ratesParameter; |
| 67 |
if (ratesParameter != null) { |
| 68 |
addVariable(ratesParameter); |
| 69 |
if (!(ratesParameter instanceof DuplicatedParameter)) |
| 70 |
ratesParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, |
| 71 |
ratesParameter.getDimension())); |
| 72 |
|
| 73 |
setupDimensionNames(relativeTo); |
| 74 |
} |
| 75 |
setRatesRelativeTo(relativeTo); |
| 76 |
} |
| 77 |
|
| 78 |
protected void frequenciesChanged() { |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
protected void ratesChanged() { |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
protected void setupRelativeRates(double[] rates) { |
| 87 |
for (int i = 0; i < rates.length; i++) { |
| 88 |
if (i == ratesRelativeTo) { |
| 89 |
rates[i] = 1.0; |
| 90 |
} else if (ratesRelativeTo < 0 || i < ratesRelativeTo) { |
| 91 |
rates[i] = ratesParameter.getParameterValue(i); |
| 92 |
} else { |
| 93 |
rates[i] = ratesParameter.getParameterValue(i - 1); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
protected void setupDimensionNames(int relativeTo) { |
| 99 |
List<String> rateNames = new ArrayList<String>(); |
| 100 |
|
| 101 |
String ratePrefix = ratesParameter.getParameterName(); |
| 102 |
|
| 103 |
int index = 0; |
| 104 |
|
| 105 |
for (int i = 0; i < dataType.getStateCount(); ++i) { |
| 106 |
for (int j = i + 1; j < dataType.getStateCount(); ++j) { |
| 107 |
if (index != relativeTo) { |
| 108 |
rateNames.add(getDimensionString(i, j, ratePrefix)); |
| 109 |
} |
| 110 |
} |
| 111 |
index++; |
| 112 |
} |
| 113 |
|
| 114 |
String[] tmp = new String[0]; |
| 115 |
ratesParameter.setDimensionNames(rateNames.toArray(tmp)); |
| 116 |
} |
| 117 |
|
| 118 |
protected String getDimensionString(int i, int j, String prefix) { |
| 119 |
String codes = dataType.getCode(i) + "." + dataType.getCode(j); |
| 120 |
if (prefix == null) { |
| 121 |
return codes; |
| 122 |
} else { |
| 123 |
return prefix + "." + codes; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public void setRatesRelativeTo(int ratesRelativeTo) { |
| 131 |
this.ratesRelativeTo = ratesRelativeTo; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
protected void storeState() { |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
protected void restoreState() { |
| 146 |
updateMatrix = true; |
| 147 |
} |
| 148 |
|
| 149 |
protected void acceptState() { |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
protected Parameter ratesParameter = null; |
| 158 |
} |