| 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.evomodel.siteratemodel.SiteRateModel; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.math.KroneckerOperation; |
| 31 |
|
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class DependentProductChainSubstitutionModel extends ProductChainSubstitutionModel { |
| 42 |
|
| 43 |
public DependentProductChainSubstitutionModel(String name, List<SubstitutionModel> baseModels, |
| 44 |
Parameter dependenceParameter) { |
| 45 |
this(name, baseModels, null, dependenceParameter); |
| 46 |
} |
| 47 |
|
| 48 |
public DependentProductChainSubstitutionModel(String name, List<SubstitutionModel> baseModels, |
| 49 |
List<SiteRateModel> rateModels, Parameter dependenceParameter) { |
| 50 |
super(name, baseModels, rateModels); |
| 51 |
this.dependenceParameter = dependenceParameter; |
| 52 |
|
| 53 |
eigenSystemCES = new ColtEigenSystem(stateCount); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
public EigenDecomposition getEigenDecomposition() { |
| 58 |
synchronized (this) { |
| 59 |
if (updateMatrix) { |
| 60 |
computeKroneckerSumsAndProducts(); |
| 61 |
} |
| 62 |
} |
| 63 |
return eigenDecomposition; |
| 64 |
} |
| 65 |
|
| 66 |
public void getInfinitesimalMatrix(double[] out) { |
| 67 |
getEigenDecomposition(); |
| 68 |
System.arraycopy(rateMatrix, 0, out, 0, stateCount * stateCount); |
| 69 |
} |
| 70 |
|
| 71 |
private void computeKroneckerSumsAndProducts() { |
| 72 |
|
| 73 |
int currentStateSize = stateSizes[0]; |
| 74 |
double[] currentRate = new double[currentStateSize * currentStateSize]; |
| 75 |
baseModels.get(0).getInfinitesimalMatrix(currentRate); |
| 76 |
currentRate = scaleForProductChain(currentRate, 0); |
| 77 |
|
| 78 |
for (int i = 1; i < numBaseModel; i++) { |
| 79 |
SubstitutionModel nextModel = baseModels.get(i); |
| 80 |
int nextStateSize = stateSizes[i]; |
| 81 |
double[] nextRate = new double[nextStateSize * nextStateSize]; |
| 82 |
nextModel.getInfinitesimalMatrix(nextRate); |
| 83 |
nextRate = scaleForProductChain(nextRate, i); |
| 84 |
currentRate = KroneckerOperation.sum(currentRate, currentStateSize, nextRate, nextStateSize); |
| 85 |
currentStateSize *= nextStateSize; |
| 86 |
} |
| 87 |
|
| 88 |
double dependence = dependenceParameter.getParameterValue(0); |
| 89 |
|
| 90 |
|
| 91 |
for (int j = 0; j < stateCount; j++) { |
| 92 |
currentRate[(j + 1) * stateCount - (j + 1)] = dependence; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
String[] letA = new String[(int) Math.sqrt(stateCount)]; |
| 99 |
String[] letB = new String[(int) Math.sqrt(stateCount)]; |
| 100 |
String[] letAB = new String[stateCount]; |
| 101 |
int count = 0; |
| 102 |
|
| 103 |
for (int letI = 0; letI < letA.length; letI++) { |
| 104 |
letA[letI] = String.valueOf(count); |
| 105 |
letB[letI] = String.valueOf(count); |
| 106 |
count++; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
int letABi = 0; |
| 111 |
for (int letAi = 0; letAi < letA.length; letAi++) { |
| 112 |
for (int letBi = 0; letBi < letB.length; letBi++) { |
| 113 |
letAB[letABi] = letA[letAi] + " " + letB[letBi]; |
| 114 |
letABi++; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
for (int j = 0; j < letAB.length; j++) { |
| 120 |
for (int k = 0; k < letAB.length; k++) { |
| 121 |
String[] Spl1 = letAB[j].split(" "); |
| 122 |
String[] Spl2 = letAB[k].split(" "); |
| 123 |
|
| 124 |
if (!Spl1[0].equals(Spl2[0]) && !Spl1[1].equals(Spl2[1])) { |
| 125 |
|
| 126 |
currentRate[j * stateCount + k] = dependence; |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
double diagonalSum = 0; |
| 134 |
for (int k = 0; k < stateCount; k++) { |
| 135 |
for (int l = k * stateCount; l < ((k + 1) * stateCount); l++) { |
| 136 |
if (l != (k * stateCount + k)) { |
| 137 |
diagonalSum += currentRate[l]; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
currentRate[k * stateCount + k] = -(diagonalSum); |
| 142 |
diagonalSum = 0; |
| 143 |
} |
| 144 |
|
| 145 |
rateMatrix = currentRate; |
| 146 |
|
| 147 |
|
| 148 |
decompose(); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
private void decompose() { |
| 153 |
|
| 154 |
double[][] q = new double[stateCount][stateCount]; |
| 155 |
for (int i = 0; i < stateCount; i++) { |
| 156 |
System.arraycopy(rateMatrix, i * stateCount, q[i], 0, stateCount); |
| 157 |
} |
| 158 |
|
| 159 |
eigenDecomposition = eigenSystemCES.decomposeMatrix(q); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
updateMatrix = false; |
| 164 |
} |
| 165 |
|
| 166 |
private final Parameter dependenceParameter; |
| 167 |
|
| 168 |
private final EigenSystem eigenSystemCES; |
| 169 |
|
| 170 |
} |