Debian Codegraph @

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

raw symbols SCIP
1 package test.dr.evomodel.substmodel;
2
3 import dr.evolution.datatype.DataType;
4 import dr.oldevomodel.substmodel.ComplexSubstitutionModel;
5 import dr.oldevomodel.substmodel.FrequencyModel;
6 import dr.oldevomodel.substmodel.SVSComplexSubstitutionModel;
7 import dr.inference.model.Parameter;
8 import junit.framework.TestCase;
9
10 import java.text.DecimalFormat;
11 import java.text.NumberFormat;
12 import java.util.ArrayList;
13 import java.util.Random;
14
15 /**
16 *
17 */
18 public class TimeIrreversibleTest extends TestCase {
19 private static final double time = 0.01;
20
21 private static NumberFormat formatter = new DecimalFormat("###0.000000");
22
23 private static ArrayList<Double> ratioSummary = new ArrayList<Double> ();
24
25 static class Original {
26 public double[] getRates() {
27 return new double[]{
28 38.505, 23.573, 2.708, 3.35, 11.641, 0.189, 0.127, 0.511, 0.272, 1.788E-2,
29 0.214, 1.322E-2, 3.015E-2, 0.449, 0.177, 0.305, 1.517E-2, 3.924E-2, 0.18, 0.14,
30 1.273E-2, 0.265, 1.422E-2, 1.474E-2, 0.911, 0.17, 0.217, 4.078, 0.206, 3.309E-2,
31 0.657, 1.874E-2, 3.141E-2, 0.403, 2.003E-2, 0.582, 0.732, 0.106, 9.147E-2, 0.248,
32 1.516E-2, 0.524, 0.1, 1.986, 0.819, 0.146, 7.519E-2, 1.35, 0.166, 0.204,
33 1.753E-2, 0.59, 0.691, 3.308, 0.377, 1.785E-2
34 }; // 56
35 }
36
37 public double[] getIndicators() {
38 return new double[]{
39 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
40 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
41 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
42 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
43 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
44 1.0, 1.0, 1.0, 1.0, 1.0, 1.0
45 }; // 56
46 }
47
48 public double[] getFrequencies() {
49 // return new double[]{0.141, 7.525E-2, 0.117, 0.283, 7.743E-2, 0.156, 1.555E-2, 0.135}; // sum to 1.00023
50 return new double[]{0.141, 0.075, 0.117, 0.283, 0.077, 0.156, 0.016, 0.135};
51 }
52
53 public DataType getDataType() {
54 return new DataType() {
55 public String getDescription() {
56 return null;
57 }
58
59 public int getType() {
60 return 0; // NUCLEOTIDES = 0;
61 }
62
63 @Override
64 public char[] getValidChars() {
65 return null;
66 }
67
68 public int getStateCount() {
69 return getFrequencies().length; // = frequency
70 }
71 };
72 }
73
74 public String toString() {
75 return "original data test";
76 }
77 }
78
79 class Test extends Original {
80 private final double x;
81
82 public Test(double x) {
83 this.x = x;
84 }
85
86 public double[] getRates(int id) {
87 double[] originalRates = super.getRates();
88 System.out.println("original rates:");
89 printRateMatrix(originalRates, getDataType().getStateCount());
90 double[] newRates = new double[originalRates.length];
91
92 double[] uniform = new double[originalRates.length];
93
94 for (int r = 0; r < originalRates.length; r++) {
95 if (r == id) {
96 uniform[r] = (new Random()).nextDouble() * ((1 / x) - x) + x;
97 newRates[r] = originalRates[r] * uniform[r];
98 } else {
99 newRates[r] = originalRates[r];
100 }
101 }
102 System.out.println("random ratio:");
103 printRateMatrix(uniform, getDataType().getStateCount());
104 System.out.println("new rates:");
105 printRateMatrix(newRates, getDataType().getStateCount());
106 return newRates;
107 }
108
109 public String toString() {
110 return "test using random number : " + x;
111 }
112 }
113
114 public void tests() {
115 Original originalTest = new Original();
116 double[] csm_orig = testComplexSubstitutionModel(originalTest, originalTest.getRates());
117 double[] svs_orig = testSVSComplexSubstitutionModel(originalTest, originalTest.getRates());
118
119 Test test = new Test(0.8);
120 for (int r = 0; r < test.getRates().length; r++) {
121 System.out.println("==================== changing index = " + r + " (start from 0) ====================");
122
123 double[] newRate = test.getRates(r);
124 double[] csm_test = testComplexSubstitutionModel(test, newRate);
125 reportMatrix(csm_orig, csm_test);
126
127 double[] svs_test = testSVSComplexSubstitutionModel(test, newRate);
128 reportMatrix(svs_orig, svs_test);
129 }
130
131 System.out.println("==================== Biggest Ratio Summary ====================\n");
132 int i = 1;
133 double bigget = 0;
134 int biggetId = 0;
135
136 for (Double r : ratioSummary) {
137 if (i % 2 != 0) {
138 System.out.print(i/2 + " ");
139 }
140 System.out.print(formatter.format(r) + ", ");
141 if (bigget < r) {
142 bigget = r;
143 biggetId = i;
144 }
145 if (i % 2 == 0) {
146 System.out.println("");
147 }
148
149 i++;
150 }
151
152 System.out.println("bigget = " + formatter.format(bigget) + ", where index is " + biggetId/2);
153 }
154
155 private double[] testComplexSubstitutionModel(Original test, double[] rates) {
156 System.out.println("\n*** Complex Substitution Model Test: " + test + " ***");
157
158 Parameter ratesP = new Parameter.Default(rates);
159
160 DataType dataType = test.getDataType();
161
162 FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(test.getFrequencies()));
163
164 ComplexSubstitutionModel substModel = new ComplexSubstitutionModel("Complex Substitution Model Test",
165 dataType,
166 freqModel,
167 ratesP);
168 double logL = substModel.getLogLikelihood();
169
170 System.out.println("Prior = " + logL);
171
172 double[] finiteTimeProbs = null;
173 if (!Double.isInfinite(logL)) {
174 finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()];
175 substModel.getTransitionProbabilities(time, finiteTimeProbs);
176 System.out.println("Probs = ");
177 printRateMatrix(finiteTimeProbs, substModel.getDataType().getStateCount());
178 }
179 // assertEquals(1, 1, 1e-10);
180 return finiteTimeProbs;
181 }
182
183 private double[] testSVSComplexSubstitutionModel(Original test, double[] rates) {
184 System.out.println("\n*** SVS Complex Substitution Model Test: " + test + " ***");
185
186 double[] indicators = test.getIndicators();
187
188 Parameter ratesP = new Parameter.Default(rates);
189 Parameter indicatorsP = new Parameter.Default(indicators);
190
191 DataType dataType = test.getDataType();
192
193 FrequencyModel freqModel = new FrequencyModel(dataType, new Parameter.Default(test.getFrequencies()));
194
195 SVSComplexSubstitutionModel substModel = new SVSComplexSubstitutionModel("SVS Complex Substitution Model Test",
196 dataType,
197 freqModel,
198 ratesP, indicatorsP);
199
200 double logL = substModel.getLogLikelihood();
201
202 System.out.println("Prior = " + logL);
203
204 double[] finiteTimeProbs = null;
205 if (!Double.isInfinite(logL)) {
206 finiteTimeProbs = new double[substModel.getDataType().getStateCount() * substModel.getDataType().getStateCount()];
207 substModel.getTransitionProbabilities(time, finiteTimeProbs);
208 System.out.println("Probs = ");
209 printRateMatrix(finiteTimeProbs, substModel.getDataType().getStateCount());
210 }
211 // assertEquals(1, 1, 1e-10);
212 return finiteTimeProbs;
213 }
214
215 public static void printRateMatrix(double[] m, int a) {
216 int id = 0;
217 for (int i = 0; i < a; i++) {
218 if (i == 0) {
219 System.out.print("/ ");
220 } else if (i == a - 1) {
221 System.out.print("\\ ");
222 } else {
223 System.out.print("| ");
224 }
225
226 for (int j = 0; j < a; j++) {
227 if (i == j) {
228 System.out.print("null");
229 for (int n = 0; n < formatter.getMaximumFractionDigits(); n++) {
230 System.out.print(" ");
231 }
232 } else {
233 System.out.print(formatter.format(m[id]) + " ");
234 id++;
235 }
236 }
237
238 if (i == 0) {
239 System.out.print("\\");
240 } else if (i == a - 1) {
241 System.out.print("/");
242 } else {
243 System.out.print("|");
244 }
245
246 System.out.println();
247 }
248 System.out.println("\n");
249 }
250
251 public static void reportMatrix(double[] orig, double[] test) {
252 double bigRatio = 0;
253 double ratio;
254 int index = -1;
255
256 if (orig.length != test.length)
257 System.err.println("Error : 2 matrix should have same length ! " + orig.length + " " + test.length);
258
259 for (int i = 0; i < orig.length; i++) {
260 ratio = Math.abs(orig[i] / test[i]);
261 if (bigRatio < ratio) {
262 bigRatio = ratio;
263 index = i;
264 }
265 }
266
267 ratioSummary.add(bigRatio);
268
269 System.out.println("Biggest Ratio = " + formatter.format(bigRatio) + ", between " + formatter.format(orig[index])
270 + " and " + formatter.format(test[index]));
271 System.out.println("index = " + index + " (start from 0)");
272 System.out.println("\n");
273 }
274 }