Debian Codegraph @

src/dr/evomodel/substmodel/codon/MarkovModulatedGY94CodonModel.java

raw symbols SCIP
1 /*
2 * MarkovModulatedGY94CodonModel.java
3 *
4 * Copyright (c) 2002-2016 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.evomodel.substmodel.codon;
27
28 import dr.evomodel.substmodel.DefaultEigenSystem;
29 import dr.evomodel.substmodel.EigenDecomposition;
30 import dr.evomodel.substmodel.EigenSystem;
31 import dr.evomodel.substmodel.FrequencyModel;
32 import dr.evolution.datatype.Codons;
33 import dr.evolution.datatype.HiddenCodons;
34 import dr.inference.model.Parameter;
35 import dr.math.matrixAlgebra.Vector;
36
37 /**
38 * @author Marc A. Suchard
39 */
40 public class MarkovModulatedGY94CodonModel extends GY94CodonModel {
41
42 private static final byte RATE = 5;
43
44 public MarkovModulatedGY94CodonModel(
45 HiddenCodons codonDataType,
46 Parameter switchingRates,
47 Parameter omegaParameter,
48 Parameter kappaParameter,
49 FrequencyModel freqModel) {
50 this(codonDataType, switchingRates, omegaParameter, kappaParameter, freqModel,
51 new DefaultEigenSystem(codonDataType.getStateCount()));
52 }
53
54 public MarkovModulatedGY94CodonModel(
55 HiddenCodons codonDataType,
56 Parameter switchingRates,
57 Parameter omegaParameter,
58 Parameter kappaParameter,
59 FrequencyModel freqModel,
60 EigenSystem eigenSystem) {
61
62 super(codonDataType, omegaParameter, kappaParameter, freqModel, eigenSystem);
63
64 this.hiddenClassCount = codonDataType.getHiddenClassCount();
65 this.switchingRates = switchingRates;
66 addVariable(switchingRates);
67
68 // Subclassed constructors fill relativeRates with 1
69 for (int i = 0; i < relativeRates.length; i++)
70 relativeRates[i] = 0.0;
71 }
72
73 // protected void handleVariableChangedEvent(Parameter parameter, int index, Parameter.ChangeType type) {
74 // // relativeRates changed
75 // System.err.println("parameter "+parameter.getId()+" index = "+index+" changed");
76 // updateMatrix = true;
77 // ratesChanged();
78 //// fireModelChanged();
79 // }
80
81
82 protected void setupRelativeRates(double[] relativeRates) {
83 double kappa = getKappa();
84 double[] omega = omegaParameter.getParameterValues();
85 double[] rates = switchingRates.getParameterValues();
86
87 int stateCount = this.stateCount / hiddenClassCount;
88
89 int index = 0;
90 for (int i = 0; i < stateCount; i++) {
91 for (int j = i + 1; j < stateCount; j++) {
92 for (int h = 0; h < hiddenClassCount; h++) {
93
94 int d = getIndex(h * stateCount + i, h * stateCount + j, this.stateCount);
95 switch (rateMap[index]) {
96 case 0:
97 relativeRates[d] = 0.0;
98 break; // codon changes in more than one codon position
99 case 1:
100 relativeRates[d] = kappa;
101 break; // synonymous transition
102 case 2:
103 relativeRates[d] = 1.0;
104 break; // synonymous transversion
105 case 3:
106 relativeRates[d] = kappa * omega[h];
107 break; // non-synonymous transition
108 case 4:
109 relativeRates[d] = omega[h];
110 break; // non-synonymous transversion
111
112 }
113 }
114 index++;
115 }
116 }
117
118 double[] freqs = freqModel.getFrequencies();
119 int rateIndex = 0;
120 for (int g = 0; g < hiddenClassCount; g++) {
121 for (int h = g + 1; h < hiddenClassCount; h++) { // from g -> h
122 for (int i = 0; i < stateCount; i++) {
123 int d = getIndex(g * stateCount + i, h * stateCount + i, this.stateCount);
124 // correct for the fact that setupMatrix post-multiplies these rates
125 relativeRates[d] = rates[rateIndex] / freqs[i];
126 }
127 rateIndex++;
128 }
129 }
130 }
131
132 // Mapping: Matrix[i][j] = Compressed vector[i*(S - 3/2) - i^2 / 2 + j - 1]
133 private static int getIndex(int i, int j, int S) {
134 return (i * (2 * S - 3) - i * i) / 2 + j - 1;
135 }
136
137 protected void constructRateMap() {
138 // Construct map for non-hidden states only
139 hiddenClassCount = ((HiddenCodons) codonDataType).getHiddenClassCount();
140 stateCount /= hiddenClassCount;
141 super.constructRateMap();
142 stateCount *= hiddenClassCount;
143 }
144
145 public static void main(String[] args) {
146 GY94CodonModel codonModel = new GY94CodonModel(Codons.UNIVERSAL,
147 new Parameter.Default(1.0), new Parameter.Default(2.0),
148 new FrequencyModel(Codons.UNIVERSAL, new Parameter.Default(61, 1.0 / 61.0)));
149 EigenDecomposition ed1 = codonModel.getEigenDecomposition();
150 // double[][] q = codonModel.getQ();
151
152 // System.err.println("matrixQ = \n"+codonModel.printQ());// new Matrix(q));
153 FrequencyModel freqModel = new FrequencyModel(HiddenCodons.UNIVERSAL_HIDDEN_2, new Parameter.Default(122, 1.0 / 122.0));
154 System.err.println("freq = " + new Vector(freqModel.getFrequencies()));
155 // System.exit(-1);
156 MarkovModulatedGY94CodonModel mmCodonModel = new MarkovModulatedGY94CodonModel(HiddenCodons.UNIVERSAL_HIDDEN_2,
157 new Parameter.Default(2, 5.0), new Parameter.Default(2, 1.0),
158 new Parameter.Default(2.0), freqModel
159 );
160 EigenDecomposition ed2 = mmCodonModel.getEigenDecomposition();
161 System.err.println("matrixQ = \n" + mmCodonModel.printQ());// new Matrix(q));
162 }
163
164 protected double getMINFDIFF() {
165 return 1.0E-10;
166 }
167
168 protected double getMINFREQ() {
169 return 1.0E-10;
170 }
171
172
173 private int hiddenClassCount;
174 private Parameter switchingRates;
175
176 }