Debian Codegraph @

src/dr/evomodel/substmodel/MutationDeathModel.java

raw symbols SCIP
1 /*
2 * MutationDeathModel.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 /**
27 * Package: MutationDeathModel
28 * Description:
29 *
30 *
31 * Created by
32 * Alexander V. Alekseyenko ([email protected])
33 * Date: Feb 19, 2008
34 * Time: 12:41:01 PM
35 */
36
37 package dr.evomodel.substmodel;
38
39 import dr.evolution.datatype.DataType;
40 import dr.oldevomodelxml.substmodel.MutationDeathModelParser;
41 import dr.inference.model.Model;
42 import dr.inference.model.Parameter;
43
44 import java.util.Arrays;
45
46 public class MutationDeathModel extends ComplexSubstitutionModel { //BaseSubstitutionModel {
47 private SubstitutionModel CTMCModel;
48 private Parameter delParameter = null;
49 protected double[] trMatrix;
50 private Parameter baseSubModelFreq;
51 private Parameter thisSubModelFreq;
52 private Parameter mutationRate;
53
54 private double[] baseModelMatrix;
55
56
57 public MutationDeathModel(Parameter delParameter, DataType dT, SubstitutionModel evoModel,
58 FrequencyModel freqModel, Parameter mutationRate) {
59 super(MutationDeathModelParser.MD_MODEL, dT, freqModel, null);
60
61 CTMCModel = evoModel;
62 stateCount = freqModel.getFrequencyCount();
63 this.delParameter = delParameter;
64 this.dataType = dT;
65 this.mutationRate = mutationRate;
66
67 addVariable(delParameter);
68 delParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
69
70 addVariable(mutationRate);
71 // addModel(freqModel);
72 if (evoModel != null) {
73 addModel(evoModel.getFrequencyModel());
74 addModel(evoModel);
75 }
76 trMatrix = new double[(stateCount - 1) * (stateCount - 1)];
77
78 if (CTMCModel != null) {
79 baseSubModelFreq = CTMCModel.getFrequencyModel().getFrequencyParameter();
80 } else {
81 baseSubModelFreq = new Parameter.Default(new double[]{1.0});
82 }
83 thisSubModelFreq = getFrequencyModel().getFrequencyParameter();
84
85 double total = 0;
86 for (int i = 0; i < baseSubModelFreq.getDimension(); i++) {
87 double value = thisSubModelFreq.getParameterValue(i);
88 total += value;
89 baseSubModelFreq.setParameterValue(i, value);
90 }
91
92 for (int i = 0; i < baseSubModelFreq.getDimension(); i++) {
93 baseSubModelFreq.setParameterValue(i, baseSubModelFreq.getParameterValue(i) / total);
94 }
95
96 thisSubModelFreq.setParameterValue(thisSubModelFreq.getDimension() - 1, 0.0);
97
98 copyFrequencies();
99
100 frequenciesChanged();
101 ratesChanged();
102
103 if (CTMCModel != null) {
104 final int baseStateCount = CTMCModel.getDataType().getStateCount();
105 baseModelMatrix = new double[baseStateCount * baseStateCount];
106 } else {
107 baseModelMatrix = new double[1];
108 }
109 setDoNormalization(false);
110
111 // setupRelativeRates();
112 }
113
114 public Parameter getDeathParameter() {
115 return this.delParameter;
116 }
117
118 protected void handleModelChangedEvent(Model model, Object object, int index) {
119
120 if (object == baseSubModelFreq) {
121 copyFrequencies();
122 fireModelChanged(object, index);
123 } else if (model == CTMCModel) {
124 fireModelChanged(object, index);
125 }
126 }
127
128 private void copyFrequencies() {
129 for (int i = 0; i < baseSubModelFreq.getDimension(); i++)
130 thisSubModelFreq.setParameterValueQuietly(i, baseSubModelFreq.getParameterValue(i));
131
132 }
133
134 @Override
135 protected void frequenciesChanged() {
136 }
137
138 @Override
139 protected void ratesChanged() {
140 }
141
142 @Override
143 protected void setupQMatrix(double[] rates, double[] pi, double[][] matrix) {
144
145 // Zero matrix
146 for (int i = 0; i < matrix.length; ++i) {
147 Arrays.fill(matrix[i], 0.0);
148 }
149
150 int baseStateCount = 1;
151 if (CTMCModel != null) {
152 CTMCModel.getInfinitesimalMatrix(baseModelMatrix);
153 baseStateCount = CTMCModel.getDataType().getStateCount();
154 }
155
156 double deathR = delParameter.getParameterValue(0);
157 double mutationR = 2 * mutationRate.getParameterValue(0); // TODO Double-check
158
159 int k = 0;
160 for (int i = 0; i < baseStateCount; ++i) {
161 for (int j = 0; j < baseStateCount; ++j) {
162 matrix[i][j] = baseModelMatrix[k] - deathR;
163 k++;
164 }
165 matrix[i][baseStateCount] = deathR;
166 }
167 }
168
169 @Override
170 protected void setupRelativeRates(double[] rates) {
171
172 }
173
174 }