Debian Codegraph @

src/test/dr/evomodel/substmodel/CovarionHKYTest.java

raw symbols SCIP
1 package test.dr.evomodel.substmodel;
2
3 import dr.evolution.datatype.OldHiddenNucleotides;
4 import dr.evomodel.substmodel.*;
5 import dr.inference.model.Parameter;
6 import dr.oldevomodel.substmodel.CovarionHKY;
7 import dr.oldevomodel.substmodel.FrequencyModel;
8 import dr.oldevomodel.substmodel.SubstitutionModelUtils;
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12
13 /**
14 * CovarionHKY Tester.
15 *
16 * @author Alexei Drummond
17 * @version 1.0
18 * @since <pre>08/20/2007</pre>
19 */
20 public class CovarionHKYTest extends TestCase {
21 public CovarionHKYTest(String name) {
22 super(name);
23 }
24
25 public void setUp() throws Exception {
26 super.setUp();
27
28 alpha = new Parameter.Default(0.0);
29 switchingRate = new Parameter.Default(1.0);
30 kappa = new Parameter.Default(2.0);
31
32 dataType = new OldHiddenNucleotides(2);
33
34 }
35
36 public void tearDown() throws Exception {
37 super.tearDown();
38 }
39
40 public void testK2PTransitionProbabilities() {
41
42 double[] frequencies = new double[]
43 {0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125};
44
45 transitionProbabilitiesTester(frequencies, matLabPChange_K2P);
46 }
47
48 public void testHKYTransitionProbabilities() {
49
50 double[] baseFrequencies = new double[]{0.15, 0.2, 0.3, 0.35};
51
52 double[] frequencies = new double[8];
53 for (int i = 0; i < frequencies.length; i++) {
54 frequencies[i] = baseFrequencies[i % 4] / 2.0;
55 }
56
57 transitionProbabilitiesTester(frequencies, matLabPChange_HKY);
58 }
59
60
61 public void transitionProbabilitiesTester(double[] freqs, double[] expectedP) {
62
63 Parameter frequencies = new Parameter.Default(freqs);
64 FrequencyModel freqModel = new FrequencyModel(dataType, frequencies);
65 model = new CovarionHKY(dataType, kappa, alpha, switchingRate, freqModel);
66
67 alpha.setParameterValue(0, 0.0);
68 switchingRate.setParameterValue(0, 1.0);
69 kappa.setParameterValue(0, 2.0);
70
71 model.setupMatrix();
72
73 System.out.println(SubstitutionModelUtils.toString(model.getQ(), dataType, 2));
74
75 double[] matrix = new double[64];
76 double[] pi = model.getFrequencyModel().getFrequencies();
77
78 int index = 0;
79 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
80 model.getTransitionProbabilities(distance, matrix);
81
82 double pChange = 0.0;
83 double pNoOrHiddenChange = 0.0;
84 int k = 0;
85 for (int i = 0; i < 8; i++) {
86 for (int j = 0; j < 8; j++) {
87
88 if ((i % 4) != (j % 4)) {
89 pChange += matrix[k] * pi[i];
90 } else {
91 pNoOrHiddenChange += matrix[k] * pi[i];
92 }
93 k += 1;
94 }
95 }
96
97 double totalP = pChange + pNoOrHiddenChange;
98
99 assertEquals(1.0, totalP, 1e-10);
100
101 System.out.print(distance + "\t" + "\t" + pChange + "\t");
102 if (index < 100) System.out.print(expectedP[index]);
103 System.out.println();
104
105 //assertEquals(expectedP[index], pChange, 1e-14);
106
107 index += 1;
108 }
109 }
110
111
112 public void testGetRelativeDNARates() throws Exception {
113 //TODO: Test goes here...
114 }
115
116 public void testSetGetKappa() throws Exception {
117 //TODO: Test goes here...
118 }
119
120 public static Test suite() {
121 return new TestSuite(CovarionHKYTest.class);
122 }
123
124 CovarionHKY model;
125 OldHiddenNucleotides dataType;
126 Parameter kappa;
127 Parameter switchingRate;
128 Parameter alpha;
129
130 /*
131 %The following Matlab code provided by David Bryant was used to calculate
132 %the probabilities for a covarion K2P model with hidden rates of 0 and 1,
133 %kappa of 2 and a switching rate of 1
134
135 format long;
136 clear all;
137
138 r=4;
139 k=2;
140
141 Q = [-2-k 1 k 1; 1 -2-k 1 k; k 1 -2-k 1; 1 k 1 -2-k];
142
143 piQ = diag([0.25 0.25 0.25 0.25]);
144
145 %Next step necessary for unequal base cases. Commented out
146 %to match Alexei's code.
147 %Q = Q*piQ;
148
149 G = [-1 1; 1 -1]; %Transition matrix for rate classes
150 piG = diag([1/2 1/2]);
151 D = diag([0 1]);
152 I = eye(r,r);
153 R = kron(D,Q) + kron(G,I);
154
155 pi = kron(piG,piQ);
156
157 %Matrix C is a 0 one matrix. Picks out where bases change
158 C = kron(ones(2,2),ones(r,r)-eye(r));
159
160 %Normalise
161 rate = sum(sum(C.*(pi*R)));
162 R = R/rate;
163
164
165 for i=1:1:100
166 t = i*0.01;
167 P = expm(R*t);
168 pchange(i) = sum(sum(C.*(pi*P)));
169 end
170
171 pchange'
172
173 */
174 static final double[] matLabPChange_K2P = {
175 0.00986400785088,
176 0.01946196037798,
177 0.02880252532240,
178 0.03789407770533,
179 0.04674470983328,
180 0.05536224095885,
181 0.06375422660885,
182 0.07192796759140,
183 0.07989051869303,
184 0.08764869707661,
185 0.09520909039030,
186 0.10257806459771,
187 0.10976177153876,
188 0.11676615623066,
189 0.12359696391788,
190 0.13025974687993,
191 0.13675987100520,
192 0.14310252213901,
193 0.14929271221364,
194 0.15533528516793,
195 0.16123492266363,
196 0.16699614960557,
197 0.17262333947249,
198 0.17812071946489,
199 0.18349237547643,
200 0.18874225689480,
201 0.19387418123805,
202 0.19889183863200,
203 0.20379879613422,
204 0.20859850190993,
205 0.21329428926481,
206 0.21788938053981,
207 0.22238689087256,
208 0.22678983183007,
209 0.23110111491715,
210 0.23532355496481,
211 0.23945987340274,
212 0.24351270142001,
213 0.24748458301758,
214 0.25137797795665,
215 0.25519526460616,
216 0.25893874269311,
217 0.26261063595893,
218 0.26621309472519,
219 0.26974819837174,
220 0.27321795773026,
221 0.27662431739624,
222 0.27996915796208,
223 0.28325429817402,
224 0.28648149701564,
225 0.28965245572033,
226 0.29276881971518,
227 0.29583218049872,
228 0.29884407745474,
229 0.30180599960433,
230 0.30471938729832,
231 0.30758563385225,
232 0.31040608712556,
233 0.31318205104731,
234 0.31591478708987,
235 0.31860551569264,
236 0.32125541763742,
237 0.32386563537701,
238 0.32643727431884,
239 0.32897140406488,
240 0.33146905960964,
241 0.33393124249748,
242 0.33635892194069,
243 0.33875303589965,
244 0.34111449212649,
245 0.34344416917334,
246 0.34574291736640,
247 0.34801155974717,
248 0.35025089298173,
249 0.35246168823930,
250 0.35464469204110,
251 0.35680062708056,
252 0.35893019301571,
253 0.36103406723493,
254 0.36311290559680,
255 0.36516734314491,
256 0.36719799479871,
257 0.36920545602088,
258 0.37119030346234,
259 0.37315309558549,
260 0.37509437326636,
261 0.37701466037663,
262 0.37891446434595,
263 0.38079427670539,
264 0.38265457361258,
265 0.38449581635920,
266 0.38631845186142,
267 0.38812291313386,
268 0.38990961974765,
269 0.39167897827306,
270 0.39343138270730,
271 0.39516721488804,
272 0.39688684489292,
273 0.39859063142581,
274 0.40027892219004
275 };
276
277 static final double[] matLabPChange_HKY = {
278 0.00985954695768,
279 0.01944735733283,
280 0.02877658973544,
281 0.03785964647441,
282 0.04670821854965,
283 0.05533332794691,
284 0.06374536739747,
285 0.07195413775516,
286 0.07996888313383,
287 0.08779832394009,
288 0.09545068792763,
289 0.10293373939203,
290 0.11025480661789,
291 0.11742080768318,
292 0.12443827471949,
293 0.13131337672096,
294 0.13805194098895,
295 0.14465947329452,
296 0.15114117683550,
297 0.15750197006065,
298 0.16374650342878,
299 0.16987917516689,
300 0.17590414608721,
301 0.18182535351970,
302 0.18764652441305,
303 0.19337118765401,
304 0.19900268565191,
305 0.20454418523250,
306 0.20999868788237,
307 0.21536903938304,
308 0.22065793887109,
309 0.22586794735891,
310 0.23100149574820,
311 0.23606089236674,
312 0.24104833005692,
313 0.24596589284278,
314 0.25081556220095,
315 0.25559922295905,
316 0.26031866884393,
317 0.26497560770062,
318 0.26957166640174,
319 0.27410839546591,
320 0.27858727340240,
321 0.28300971079857,
322 0.28737705416541,
323 0.29169058955549,
324 0.29595154596716,
325 0.30016109854757,
326 0.30432037160662,
327 0.30843044145304,
328 0.31249233906332,
329 0.31650705259341,
330 0.32047552974251,
331 0.32439867997787,
332 0.32827737662883,
333 0.33211245885791,
334 0.33590473351622,
335 0.33965497689022,
336 0.34336393634610,
337 0.34703233187806,
338 0.35066085756610,
339 0.35425018294872,
340 0.35780095431559,
341 0.36131379592506,
342 0.36478931115072,
343 0.36822808356150,
344 0.37163067793909,
345 0.37499764123646,
346 0.37832950348097,
347 0.38162677862531,
348 0.38488996534947,
349 0.38811954781650,
350 0.39131599638496,
351 0.39447976828045,
352 0.39761130822876,
353 0.40071104905293,
354 0.40377941223623,
355 0.40681680845316,
356 0.40982363807038,
357 0.41280029161931,
358 0.41574715024200,
359 0.41866458611203,
360 0.42155296283168,
361 0.42441263580698,
362 0.42724395260180,
363 0.43004725327227,
364 0.43282287068266,
365 0.43557113080388,
366 0.43829235299547,
367 0.44098685027222,
368 0.44365492955622,
369 0.44629689191518,
370 0.44891303278793,
371 0.45150364219766,
372 0.45406900495387,
373 0.45660940084344,
374 0.45912510481158,
375 0.46161638713329,
376 0.46408351357576,
377 0.46652674555230
378 };
379 }