Debian Codegraph @

src/dr/oldevomodel/substmodel/MicrosatelliteModel.java

raw symbols SCIP
1 /*
2 * MicrosatelliteModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.oldevomodel.substmodel;
27
28
29 import dr.evolution.datatype.Microsatellite;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Model;
32
33
34 /**
35 * @author Chieh-Hsi Wu
36 *
37 * An abstract class for microsatellite models
38 */
39 public abstract class MicrosatelliteModel extends ComplexSubstitutionModel{
40 protected OnePhaseModel subModel = null;
41 protected boolean isNested = false;
42 protected double[][] infinitesimalRateMatrix = null;
43 protected boolean useStationaryFreqs = false;
44 protected boolean modelUpdate = false;
45
46
47
48 /**
49 * Constructor
50 * @param name Model name
51 * @param msat Microsatellite data type
52 * @param rootFreqModel Frequency model
53 * @param parameter Infinitesimal rates
54 */
55 public MicrosatelliteModel(String name, Microsatellite msat, FrequencyModel rootFreqModel, Parameter parameter) {
56 super(name, msat, rootFreqModel, parameter);
57 if(parameter == null){
58 double[] q = new double[stateCount*(stateCount-1)];
59 infinitesimalRates = new Parameter.Default(q);
60 }
61
62 infinitesimalRateMatrix = new double[stateCount][stateCount];
63 }
64
65 public void setToEqualFrequencies(){
66 double[] freqs = new double[stateCount];
67 for(int i = 0; i < freqs.length; i++){
68 freqs[i] = 1.0/stateCount;
69 }
70 this.freqModel = new FrequencyModel(dataType, freqs);
71 }
72
73 protected void handleModelChangedEvent(Model model, Object object, int index) {
74 updateMatrix = true;
75 }
76
77 //store the infinitesimal rates in the vector to a matrix called amat
78 public void storeIntoAmat(){
79 amat = infinitesimalRateMatrix;
80 }
81
82 //matrix is already valid
83 protected void makeValid(double[][] matrix, int dimension){}
84
85 protected double getRate(int i, int j){
86 return infinitesimalRateMatrix[i][j];
87 }
88
89 /*
90 * Set up stationary frequencies
91 */
92 public void computeTwoPhaseStationaryDistribution(){
93 synchronized (this) {
94 if (updateMatrix) {
95 setupMatrix();
96 }
97 }
98
99 if (!wellConditioned) {
100 throw new RuntimeException("not well conditioned");
101 }
102 int eigenValPos = -1;
103
104 for(int i = 0; i < stateCount; i++){
105 if(Eval[i] == 0){
106 eigenValPos = i;
107 break;
108 }
109 }
110 /*for(int i = 0; i < EvalImag.length; i++){
111 //System.out.println("imaginery part" + EvalImag[i]);
112 if(EvalImag[i] != 0.0){
113 throw new RuntimeException("imaginery part" + EvalImag[i]);
114 }
115 }*/
116
117 double[] empFreq = new double[stateCount];
118 //System.out.println("eq dist");
119 for(int i = 0; i < stateCount; i++){
120 empFreq[i] = Evec[i][eigenValPos]*Ievc[eigenValPos][i];
121 //System.out.println(empFreq[i]);
122
123 }
124 this.freqModel = new FrequencyModel(dataType, empFreq);
125 }
126
127
128 /*public void computeTwoPhaseStationaryDistribution2(){
129 setToEqualFrequencies();
130 super.computeStationaryDistribution();
131 synchronized (this) {
132 if (updateMatrix) {
133 setupMatrix();
134 }
135 }
136
137 if (!wellConditioned) {
138 throw new RuntimeException("not well conditioned");
139 }
140
141 double[] empFreq = new double[stateCount];
142
143 for(int i = 0; i < stateCount; i++){
144 empFreq[i] = getOneTransitionProbabilityEntry(Double.MAX_VALUE, i, i);
145 //System.out.println(empFreq[i]);
146 }
147
148 this.freqModel = new FrequencyModel(dataType, empFreq);
149 }*/
150
151
152 public double[] getRates(){
153 return super.getRates();
154 }
155
156 /*public void computeStationaryDistribution(){
157 super.computeStationaryDistribution();
158 }*/
159
160 public abstract void setupInfinitesimalRates();
161
162 public double[][] getInfinitesimalRates(){
163 return infinitesimalRateMatrix;
164 }
165
166 public double getLogOneTransitionProbabilityEntry(double distance, int parentState, int childState){
167 return Math.log(getOneTransitionProbabilityEntry(distance, parentState, childState));
168 }
169
170 public double getOneTransitionProbabilityEntry(double distance, int parentState, int childState){
171 if(dataType.isAmbiguousState(childState)){
172 return 1.0;
173 }
174
175 double probability = 0.0;
176 double temp;
177
178 synchronized (this) {
179 if (updateMatrix) {
180 setupMatrix();
181 }
182 }
183
184 if (!wellConditioned) {
185
186 //throw new RuntimeException("not well conditioned");
187 return 0.0;
188 }
189
190 double [] iexp = new double[stateCount];
191 for(int i = 0; i < stateCount; i++){
192 if(EvalImag[i] == 0){
193 temp = Math.exp(distance*(Eval[i]));
194 iexp[i] = temp*Ievc[i][childState];
195
196 }else{
197 int i2 = i + 1;
198 double b = EvalImag[i];
199 double expat = Math.exp(distance * Eval[i]);
200 double expatcosbt = expat * Math.cos(distance * b);
201 double expatsinbt = expat * Math.sin(distance * b);
202 iexp[i] = expatcosbt * Ievc[i][childState] + expatsinbt * Ievc[i2][childState];
203 iexp[i2] = expatcosbt * Ievc[i2][childState] - expatsinbt * Ievc[i][childState];
204 i ++;
205
206 }
207 }
208 for(int i = 0; i < stateCount; i++){
209 probability += Evec[parentState][i]*iexp[i];
210 }
211
212 if(probability <= 0.0){
213 probability = minProb;
214 }
215
216 return probability;
217 }
218
219 public double[] getColTransitionProbabilities(double distance, int childState){
220
221 double[] probability = new double[stateCount];
222 double temp;
223
224 synchronized (this) {
225 if (updateMatrix) {
226 setupMatrix();
227 }
228 }
229
230 if (!wellConditioned) {
231
232 //throw new RuntimeException("not well conditioned");
233 return probability;
234 }
235
236 double [] iexp = new double[stateCount];
237 for(int i = 0; i < stateCount; i++){
238 if(EvalImag[i] == 0){
239 temp = Math.exp(distance*(Eval[i]));
240 iexp[i] = temp*Ievc[i][childState];
241
242 }else{
243 int i2 = i + 1;
244 double b = EvalImag[i];
245 double expat = Math.exp(distance * Eval[i]);
246 double expatcosbt = expat * Math.cos(distance * b);
247 double expatsinbt = expat * Math.sin(distance * b);
248 iexp[i] = expatcosbt * Ievc[i][childState] + expatsinbt * Ievc[i2][childState];
249 iexp[i2] = expatcosbt * Ievc[i2][childState] - expatsinbt * Ievc[i][childState];
250 i ++;
251
252 }
253 }
254 for(int i = 0; i < stateCount; i++){
255 for(int j = 0; j < stateCount; j++){
256 probability[i] += Evec[i][j]*iexp[j];
257
258 }
259 if(probability[i] <= 0.0){
260 probability[i] = minProb;
261 }
262 }
263
264 return probability;
265 }
266 public double[] getRowTransitionProbabilities(double distance, int parentState){
267
268 double[] probability = new double[stateCount];
269 double temp;
270
271 synchronized (this) {
272 if (updateMatrix) {
273 setupMatrix();
274 }
275 }
276
277 if (!wellConditioned) {
278
279 //throw new RuntimeException("not well conditioned");
280 return probability;
281 }
282
283 int i,j;
284 double[][] iexp = new double[stateCount][stateCount];
285
286 for (i = 0; i < stateCount; i++) {
287
288 if (EvalImag[i] == 0) {
289 // 1x1 block
290 temp = Math.exp(distance * Eval[i]);
291 for (j = 0; j < stateCount; j++) {
292 iexp[i][j] = Ievc[i][j] * temp;
293 }
294 } else {
295 // 2x2 conjugate block
296 // If A is 2x2 with complex conjugate pair eigenvalues a +/- bi, then
297 // exp(At) = exp(at)*( cos(bt)I + \frac{sin(bt)}{b}(A - aI)).
298 int i2 = i + 1;
299 double b = EvalImag[i];
300 double expat = Math.exp(distance * Eval[i]);
301 double expatcosbt = expat * Math.cos(distance * b);
302 double expatsinbt = expat * Math.sin(distance * b);
303
304 for (j = 0; j < stateCount; j++) {
305 iexp[i][j] = expatcosbt * Ievc[i][j] + expatsinbt * Ievc[i2][j];
306 iexp[i2][j] = expatcosbt * Ievc[i2][j] - expatsinbt * Ievc[i][j];
307 }
308 i++; // processed two conjugate rows
309 }
310 }
311 for(i = 0; i < stateCount; i++){
312 for(j = 0; j < stateCount; j++){
313 probability[i] += Evec[parentState][j]*iexp[j][i];
314
315 }
316 if(probability[i] <= 0.0){
317 probability[i] = minProb;
318 }
319 }
320
321 return probability;
322 }
323 /*
324 * The One Phase Models are special cases of the birth-death chain,
325 * and therefore we can use this to calculate the stationay distribution
326 * given a infinitesimal rate matrix.
327 */
328 public void computeOnePhaseStationaryDistribution(){
329 double[] pi = new double[stateCount];
330
331 pi[0] = 1.0;
332 double piSum = 1.0;
333 for(int i = 1; i < stateCount; i++){
334 pi[i] = pi[i-1]*infinitesimalRateMatrix[i-1][i]/infinitesimalRateMatrix[i][i-1];
335 piSum = piSum+pi[i];
336 }
337
338 for(int i = 0; i < stateCount; i++){
339 pi[i] = pi[i]/piSum;
340 //System.out.println(pi[i]);
341
342 }
343 freqModel = new FrequencyModel(dataType,pi);
344
345
346 }
347
348 public void setupMatrix(){
349 setupInfinitesimalRates();
350 super.setupMatrix();
351 }
352
353
354 public MicrosatelliteModel getSubmodel(){
355 return subModel;
356 }
357
358 public boolean isSubmodel(){
359 return isNested;
360 }
361
362 public boolean hasSubmodel(){
363 return subModel != null;
364 }
365
366
367 public boolean isModelUpdated(){
368 return true;
369 }
370
371 }