Debian Codegraph @

src/dr/oldevomodel/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.oldevomodel.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 public class MutationDeathModel extends AbstractSubstitutionModel {
45 private AbstractSubstitutionModel CTMCModel;
46 private Parameter delParameter = null;
47 protected double[] trMatrix;
48 private Parameter baseSubModelFreq;
49 private Parameter thisSubModelFreq;
50 private Parameter mutationRate;
51
52
53 public MutationDeathModel(Parameter delParameter, DataType dT, AbstractSubstitutionModel evoModel,
54 FrequencyModel freqModel, Parameter mutationRate) {
55 super(MutationDeathModelParser.MD_MODEL, dT, freqModel);
56 CTMCModel = evoModel;
57 stateCount = freqModel.getFrequencyCount();
58 this.delParameter = delParameter;
59 this.dataType = dT;
60 this.mutationRate = mutationRate;
61
62 addVariable(delParameter);
63 delParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
64
65 addVariable(mutationRate);
66 // addModel(freqModel);
67 if (evoModel != null) {
68 addModel(evoModel.getFrequencyModel());
69 addModel(evoModel);
70 }
71 trMatrix = new double[(stateCount - 1) * (stateCount - 1)];
72
73 if (CTMCModel != null) {
74 baseSubModelFreq = CTMCModel.getFrequencyModel().getFrequencyParameter();
75 } else {
76 baseSubModelFreq = new Parameter.Default(new double[]{1.0});
77 }
78 thisSubModelFreq = getFrequencyModel().getFrequencyParameter();
79
80 double total = 0;
81 for (int i = 0; i < baseSubModelFreq.getDimension(); i++) {
82 double value = thisSubModelFreq.getParameterValue(i);
83 total += value;
84 baseSubModelFreq.setParameterValue(i, value);
85 }
86
87 for (int i = 0; i < baseSubModelFreq.getDimension(); i++) {
88 baseSubModelFreq.setParameterValue(i, baseSubModelFreq.getParameterValue(i) / total);
89 }
90
91 thisSubModelFreq.setParameterValue(thisSubModelFreq.getDimension() - 1, 0.0);
92
93 copyFrequencies();
94
95 frequenciesChanged();
96 ratesChanged();
97 setupRelativeRates();
98 }
99
100 public Parameter getDeathParameter() {
101 return this.delParameter;
102 }
103
104 protected void storeState() {
105 }
106
107 protected void restoreState() {
108 }
109
110 protected void handleModelChangedEvent(Model model, Object object, int index) {
111
112 if (object == baseSubModelFreq) {
113 copyFrequencies();
114 fireModelChanged(object, index);
115 } else if (model == CTMCModel) {
116 fireModelChanged(object, index);
117 }
118 }
119
120 private void copyFrequencies() {
121 for (int i = 0; i < baseSubModelFreq.getDimension(); i++)
122 thisSubModelFreq.setParameterValueQuietly(i, baseSubModelFreq.getParameterValue(i));
123
124 }
125
126 @Override
127 protected void frequenciesChanged() {
128 }
129
130 @Override
131 protected void ratesChanged() {
132 }
133
134 @Override
135 protected void setupRelativeRates() {
136 }
137
138 public void getTransitionProbabilities(double distance, double[] matrix) {
139 int i, j;
140 // assuming that expected number of changes in CTMCModel is 1 per unit time
141 // we are contributing s*deathRate number of changes per unit of time
142 double deathProb = Math.exp(-distance * delParameter.getParameterValue(0));
143 double mutationR = 2 * mutationRate.getParameterValue(0);
144 double freqs[] = freqModel.getFrequencyParameter().getParameterValues();
145
146 for (i = 0; i < freqs.length - 1; ++i) {
147 mutationR *= freqs[i];
148 }
149 if (CTMCModel != null) {
150 CTMCModel.getTransitionProbabilities(mutationR * distance, trMatrix);
151 } else {
152 trMatrix[0] = 1.0;
153 }
154
155 for (i = 0; i < stateCount - 1; ++i) {
156 for (j = 0; j < stateCount - 1; j++) {
157 matrix[i * (stateCount) + j] = trMatrix[i * (stateCount - 1) + j] * deathProb;
158 }
159 matrix[i * (stateCount) + j] = (1.0 - deathProb);
160 }
161
162 for (j = 0; j < stateCount - 1; ++j) {
163 matrix[stateCount * (stateCount - 1) + j] = 0.0;
164 }
165
166 matrix[stateCount * stateCount - 1] = 1.0;
167 /* System.err.println("Transition matrix t = "+distance);
168 for(i=0;i<stateCount;++i){
169 System.err.print("[");
170 for(j=0;j<stateCount; ++j){
171 System.err.print(" "+matrix[i*stateCount+j]);
172 }
173 System.err.println(" ]");
174 }*/
175 }
176 }