| 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 test.dr.evomodel.substmodel; |
| 27 |
|
| 28 |
import dr.evomodel.substmodel.nucleotide.HKY; |
| 29 |
import dr.evolution.datatype.Codons; |
| 30 |
import dr.evolution.datatype.GeneticCode; |
| 31 |
import dr.evolution.datatype.Nucleotides; |
| 32 |
import dr.evomodel.substmodel.*; |
| 33 |
import junit.framework.TestCase; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.List; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class CodonPartitionedRobustCountingTest extends TestCase { |
| 42 |
|
| 43 |
private Codons codons; |
| 44 |
private GeneticCode geneticCode; |
| 45 |
private ProductChainSubstitutionModel productChainModel; |
| 46 |
|
| 47 |
public void setUp() { |
| 48 |
codons = Codons.UNIVERSAL; |
| 49 |
geneticCode = codons.getGeneticCode(); |
| 50 |
} |
| 51 |
|
| 52 |
private void setUpNucleotides() { |
| 53 |
|
| 54 |
FrequencyModel freqModel0 = new FrequencyModel(Nucleotides.INSTANCE, |
| 55 |
new double[]{0.25, 0.25, 0.25, 0.25}); |
| 56 |
FrequencyModel freqModel1 = new FrequencyModel(Nucleotides.INSTANCE, |
| 57 |
new double[]{0.25, 0.25, 0.25, 0.25}); |
| 58 |
FrequencyModel freqModel2 = new FrequencyModel(Nucleotides.INSTANCE, |
| 59 |
new double[]{0.25, 0.25, 0.25, 0.25}); |
| 60 |
|
| 61 |
HKY baseModel0 = new HKY(2.0, freqModel0); |
| 62 |
HKY baseModel1 = new HKY(2.0, freqModel1); |
| 63 |
HKY baseModel2 = new HKY(2.0, freqModel2); |
| 64 |
|
| 65 |
List<SubstitutionModel> baseModels = new ArrayList<SubstitutionModel>(3); |
| 66 |
baseModels.add(baseModel0); |
| 67 |
baseModels.add(baseModel1); |
| 68 |
baseModels.add(baseModel2); |
| 69 |
|
| 70 |
productChainModel = new ProductChainSubstitutionModel("productChain", baseModels); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
private double sumMatrix(double[] mat) { |
| 83 |
double total = 0; |
| 84 |
for (double x : mat) { |
| 85 |
total += x; |
| 86 |
} |
| 87 |
return total; |
| 88 |
} |
| 89 |
|
| 90 |
public void testSumRegisterMatrices() { |
| 91 |
|
| 92 |
System.out.println("Testing the sum of the register matrices..."); |
| 93 |
|
| 94 |
double[] synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, true); |
| 95 |
double totalSyn = sumMatrix(synRegMatrix); |
| 96 |
System.out.println("Sum syn matrix = " + totalSyn); |
| 97 |
assertEquals(134, (int) totalSyn); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
double[] nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, true); |
| 104 |
double totalNonSyn = sumMatrix(nonSynRegMatrix); |
| 105 |
System.out.println("Sum non-syn matrix = " + totalNonSyn); |
| 106 |
assertEquals(392, (int) totalNonSyn); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
System.out.println(""); |
| 113 |
} |
| 114 |
|
| 115 |
public void testStopCodons() { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
System.out.println("Testing several stop codons..."); |
| 120 |
|
| 121 |
setUpNucleotides(); |
| 122 |
|
| 123 |
int from, to; |
| 124 |
double[] synRegMatrix, nonSynRegMatrix; |
| 125 |
|
| 126 |
from = codons.getState('A', 'G', 'A'); |
| 127 |
to = codons.getState('T', 'G', 'A'); |
| 128 |
|
| 129 |
synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, true); |
| 130 |
nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, true); |
| 131 |
|
| 132 |
System.out.println("AGA -> TGA syn = " + synRegMatrix[from * 64 + to]); |
| 133 |
System.out.println("AGA -> TGA non = " + nonSynRegMatrix[from * 64 + to]); |
| 134 |
|
| 135 |
from = codons.getState('A', 'A', 'A'); |
| 136 |
to = codons.getState('T', 'A', 'A'); |
| 137 |
|
| 138 |
synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, true); |
| 139 |
nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, true); |
| 140 |
|
| 141 |
System.out.println("AAA -> TAA syn = " + synRegMatrix[from * 64 + to]); |
| 142 |
System.out.println("AAA -> TAA non = " + nonSynRegMatrix[from * 64 + to]); |
| 143 |
} |
| 144 |
|
| 145 |
public void testCounts() { |
| 146 |
|
| 147 |
System.out.println("Testing several conditional means..."); |
| 148 |
|
| 149 |
setUpNucleotides(); |
| 150 |
|
| 151 |
int from = codons.getState('A', 'A', 'A'); |
| 152 |
int to = codons.getState('A', 'A', 'C'); |
| 153 |
double time = 1.0; |
| 154 |
|
| 155 |
|
| 156 |
MarkovJumpsSubstitutionModel markovJumps = new MarkovJumpsSubstitutionModel(productChainModel); |
| 157 |
double[] synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, true); |
| 158 |
markovJumps.setRegistration(synRegMatrix); |
| 159 |
|
| 160 |
double[] c = new double[64 * 64]; |
| 161 |
markovJumps.computeCondStatMarkovJumps(time, c); |
| 162 |
|
| 163 |
System.out.println("Cond 1 = " + c[from * 64 + to]); |
| 164 |
assertEquals(0.525401, c[from * 64 + to], 1E-4); |
| 165 |
|
| 166 |
to = codons.getState('A', 'A', 'G'); |
| 167 |
System.out.println("Cond 2 = " + c[from * 64 + to]); |
| 168 |
assertEquals(0.970012, c[from * 64 + to], 1E-4); |
| 169 |
|
| 170 |
|
| 171 |
double[] nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, true); |
| 172 |
markovJumps.setRegistration(nonSynRegMatrix); |
| 173 |
markovJumps.computeCondStatMarkovJumps(time, c); |
| 174 |
|
| 175 |
System.out.println("Cond 3 = " + c[from * 64 + to]); |
| 176 |
assertEquals(1.099807, c[from * 64 + to], 1E-4); |
| 177 |
|
| 178 |
System.out.println(""); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
public void testRegistrationMatrixCodonBase() { |
| 187 |
runRegistrationMatrixTest(false); |
| 188 |
} |
| 189 |
|
| 190 |
public void testRegistrationMatrix64Base() { |
| 191 |
runRegistrationMatrixTest(true); |
| 192 |
} |
| 193 |
|
| 194 |
public void runRegistrationMatrixTest(boolean base64) { |
| 195 |
|
| 196 |
System.out.println("Testing registration matrices..."); |
| 197 |
|
| 198 |
byte[] rateMat = Codons.constructRateMap(codons); |
| 199 |
int stateCount = codons.getStateCount(); |
| 200 |
|
| 201 |
if (base64) { |
| 202 |
rateMat = expandToBase64(codons, rateMat); |
| 203 |
stateCount = 64; |
| 204 |
} |
| 205 |
|
| 206 |
double[] synRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.SYN, codons, base64); |
| 207 |
double[] nonSynRegMatrix = CodonLabeling.getRegisterMatrix(CodonLabeling.NON_SYN, codons, base64); |
| 208 |
|
| 209 |
|
| 210 |
int index = 0; |
| 211 |
boolean passed = true; |
| 212 |
for (int i = 0; i < stateCount; i++) { |
| 213 |
|
| 214 |
int stateI; |
| 215 |
if (base64) { |
| 216 |
stateI = i; |
| 217 |
} else { |
| 218 |
stateI = codons.getCanonicalState(i); |
| 219 |
} |
| 220 |
char iAA = geneticCode.getAminoAcidChar(stateI); |
| 221 |
|
| 222 |
for (int j = i + 1; j < stateCount; j++) { |
| 223 |
if (rateMat[index] != 0) { |
| 224 |
|
| 225 |
int stateJ; |
| 226 |
if (base64) { |
| 227 |
stateJ = j; |
| 228 |
} else { |
| 229 |
stateJ = codons.getCanonicalState(j); |
| 230 |
} |
| 231 |
char jAA = geneticCode.getAminoAcidChar(stateJ); |
| 232 |
|
| 233 |
if (iAA == jAA) { |
| 234 |
if (!compare(synRegMatrix, nonSynRegMatrix, i, j, iAA, jAA, 1, 0, stateCount)) { |
| 235 |
passed = false; |
| 236 |
} |
| 237 |
} else { |
| 238 |
if (!compare(synRegMatrix, nonSynRegMatrix, i, j, iAA, jAA, 0, 1, stateCount)) { |
| 239 |
passed = false; |
| 240 |
} |
| 241 |
} |
| 242 |
} else { |
| 243 |
if (synRegMatrix[i * stateCount + j] != 0 || nonSynRegMatrix[i * stateCount + j] != 0 || |
| 244 |
synRegMatrix[j * stateCount + i] != 0 || nonSynRegMatrix[j * stateCount + i] != 0) { |
| 245 |
System.out.println("fail on 0-rate " + i + " " + j); |
| 246 |
passed = false; |
| 247 |
} |
| 248 |
} |
| 249 |
index++; |
| 250 |
} |
| 251 |
} |
| 252 |
System.out.println("Passed " + stateCount + ": " + (passed ? "true" : "false") + "\n"); |
| 253 |
assertTrue(passed); |
| 254 |
} |
| 255 |
|
| 256 |
private boolean compare(double[] synRegMatrix, double[] nonSynRegMatrix, |
| 257 |
int i, int j, |
| 258 |
char iAA, char jAA, |
| 259 |
int value0, int value1, |
| 260 |
int stateCount) { |
| 261 |
|
| 262 |
boolean passed = true; |
| 263 |
if ((synRegMatrix[i * stateCount + j] == value0 && nonSynRegMatrix[i * stateCount + j] == value1) && |
| 264 |
|
| 265 |
(synRegMatrix[j * stateCount + i] == value0 && nonSynRegMatrix[j * stateCount + i] == value1)) { |
| 266 |
|
| 267 |
|
| 268 |
} else { |
| 269 |
System.out.print("compare: " + iAA + jAA + " "); |
| 270 |
System.out.println("fail " + i + " " + j + " " + synRegMatrix[i * stateCount + j] |
| 271 |
+ " " + nonSynRegMatrix[i * stateCount + j]); |
| 272 |
passed = false; |
| 273 |
} |
| 274 |
return passed; |
| 275 |
} |
| 276 |
|
| 277 |
private byte[] expandToBase64(Codons codonDataType, byte[] in) { |
| 278 |
final int stateCount = codonDataType.getStateCount(); |
| 279 |
byte[] intermediateOut = new byte[64 * 64]; |
| 280 |
int index = 0; |
| 281 |
for (int i = 0; i < stateCount; i++) { |
| 282 |
for (int j = i + 1; j < stateCount; j++) { |
| 283 |
intermediateOut[codonDataType.getCanonicalState(i) * 64 |
| 284 |
+ codonDataType.getCanonicalState(j)] = in[index]; |
| 285 |
index++; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
byte[] out = new byte[(64 * 63) / 2]; |
| 290 |
index = 0; |
| 291 |
for (int i = 0; i < 64; i++) { |
| 292 |
for (int j = i + 1; j < 64; j++) { |
| 293 |
out[index] = intermediateOut[i * 64 + j]; |
| 294 |
index++; |
| 295 |
} |
| 296 |
} |
| 297 |
return out; |
| 298 |
} |
| 299 |
} |