Debian Codegraph @

src/dr/oldevomodel/substmodel/GeneralF81Model.java

raw symbols SCIP
1 /*
2 * GeneralF81Model.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 import dr.evolution.datatype.GeneralDataType;
29
30 /**
31 * @author Alexei Drummond
32 */
33 public class GeneralF81Model extends AbstractSubstitutionModel {
34
35 /**
36 * @param freqModel
37 */
38 public GeneralF81Model(FrequencyModel freqModel) {
39 super("generalF81", freqModel.getDataType(), freqModel);
40 setupMatrix();
41 }
42
43 protected void frequenciesChanged() {
44 // frequencyModel changed
45 }
46
47 @Override
48 protected void ratesChanged() {
49 //To change body of implemented methods use File | Settings | File Templates.
50 }
51
52 /**
53 * get the complete transition probability matrix for the given distance
54 *
55 * @param distance the expected number of substitutions
56 * @param matrix an array to store the matrix
57 */
58 public void getTransitionProbabilities(double distance, double[] matrix) {
59
60 int stateSize = freqModel.getFrequencyCount();
61
62 double[] pi = freqModel.getFrequencies();
63
64 double beta = 1;
65 for (double p : pi) {
66 beta -= p * p;
67 }
68 beta = 1.0 / beta;
69
70 int c = 0;
71 for (int i = 0; i < stateSize; i++) {
72 for (int j = 0; j < stateSize; j++) {
73
74 if (i == j) {
75 matrix[c] = pi[i] + (1 - pi[i]) * Math.exp(-beta * distance);
76 } else {
77 matrix[c] = pi[j] * (1 - Math.exp(-beta * distance));
78 }
79 c += 1;
80 }
81 }
82 }
83
84 protected void setupRelativeRates() {
85 }
86
87 // *****************************************************************
88 // Interface Model
89 // *****************************************************************
90
91 public void storeState() {
92 super.storeState();
93 }
94
95 /**
96 * Restore the stored state
97 */
98 public void restoreState() {
99 super.restoreState();
100 }
101
102 // **************************************************************
103 // XHTMLable IMPLEMENTATION
104 // **************************************************************
105
106 public String toXHTML() {
107 StringBuffer buffer = new StringBuffer();
108
109 buffer.append("<em>General F81 Model</em>");
110
111 return buffer.toString();
112 }
113
114 public static void main(String[] args) {
115
116 GeneralDataType general = new GeneralDataType(new String[]{"0", "1", "2"});
117
118 FrequencyModel freqModel = new FrequencyModel(general, new double[]{0.2, 0.3, 0.5});
119
120 GeneralF81Model f81 = new GeneralF81Model(freqModel);
121
122 int S = general.getStateCount();
123
124 double[] P = new double[S * S];
125
126 f81.getTransitionProbabilities(0.01, P);
127
128 int c = 0;
129 for (int i = 0; i < S; i++) {
130 System.out.print(P[c]);
131 double rowSum = P[c];
132 c += 1;
133 for (int j = 1; j < S; j++) {
134 System.out.print(", " + P[c]);
135 rowSum += P[c];
136 c += 1;
137 }
138 System.out.println(" : " + rowSum);
139 }
140
141 }
142 }
143