Debian Codegraph @

src/dr/oldevomodel/substmodel/NtdBMA.java

raw symbols SCIP
1 /*
2 * NtdBMA.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.inference.model.Variable;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Bounds;
31
32 /**
33 * @author Chieh-Hsi Wu
34 *
35 * Model averaging of nucleotide substitution model
36 *
37 */
38 public class NtdBMA extends AbstractNucleotideModel{
39
40 private Variable<Double> logKappa = null;
41 private Variable<Double> logTN = null;
42 private Variable<Double> logAC = null;
43 private Variable<Double> logAT = null;
44 private Variable<Double> logGC = null;
45 private Variable<Double> logGT = null;
46 private Variable<Integer> modelChoose = null;
47
48 public static final int TN_INDEX = 0;
49 public static final int GTR_INDEX = 1;
50
51 public static final int ABSENT = 0;
52 public static final int PRESENT = 1;
53
54 public NtdBMA(
55 Variable<Double> logKappa,
56 Variable<Double> logTN,
57 Variable<Double> logAC,
58 Variable<Double> logAT,
59 Variable<Double> logGC,
60 Variable<Double> logGT,
61 Variable<Integer> modelChoose,
62 FrequencyModel freqModel){
63 super("NucleotideBMA", freqModel);
64
65
66 addVariable(logKappa);
67 logKappa.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
68 this.logKappa = logKappa;
69
70 addVariable(logTN);
71 logTN.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
72 this.logTN = logTN;
73
74 addVariable(logAC);
75 logAC.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
76 this.logAC = logAC;
77
78 addVariable(logAT);
79 logAT.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
80 this.logAT = logAT;
81
82 addVariable(logGC);
83 logGC.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
84 this.logGC = logGC;
85
86 addVariable(logGT);
87 logGT.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
88 this.logGT = logGT;
89
90 addVariable(modelChoose);
91 modelChoose.addBounds(new Bounds.Int(modelChoose, 0, 1));
92
93
94 this.modelChoose = modelChoose;
95 }
96
97 protected void setupRelativeRates() {
98
99 //rate AG value
100 relativeRates[1] = Math.exp(logKappa.getValue(0)+modelChoose.getValue(TN_INDEX)*logTN.getValue(0));
101 //rate CT value
102 relativeRates[4] = Math.exp(logKappa.getValue(0)-modelChoose.getValue(TN_INDEX)*logTN.getValue(0));
103 //rate AC value
104 relativeRates[0] = Math.exp(modelChoose.getValue(GTR_INDEX)*logAC.getValue(0));
105 //rate AT value
106 relativeRates[2] = Math.exp(modelChoose.getValue(GTR_INDEX)*logAT.getValue(0));
107 //rate GC value
108 relativeRates[3] = Math.exp(modelChoose.getValue(GTR_INDEX)*logGC.getValue(0));
109 //rate GT value
110 relativeRates[5] = Math.exp(modelChoose.getValue(GTR_INDEX)*logGT.getValue(0));
111
112 }
113
114 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
115 boolean changeRateMatrix = false;
116
117 if(variable == modelChoose){
118 //changing the substitution model
119 changeRateMatrix = true;
120 }else if(variable == logKappa){
121 //changing kappa value (which is in all models)
122 changeRateMatrix = true;
123 }else if(variable == logTN && modelChoose.getValue(TN_INDEX) == PRESENT){
124 //changing the tn value when the current model is TN.
125 changeRateMatrix = true;
126 }else if(variable == logAC ||
127 variable == logAT ||
128 variable == logGC ||
129 variable == logGT &&
130 modelChoose.getValue(GTR_INDEX) == PRESENT && modelChoose.getValue(TN_INDEX) == PRESENT){
131 //Changing any one of A<->C, A<->T, G<->C, G<->T rates while the current model is GTR.
132 changeRateMatrix = true;
133 }
134 // relativeRates changed
135 if(changeRateMatrix){
136 super.handleVariableChangedEvent(variable, index, type);
137 }
138 changeRateMatrix = false;
139 }
140
141 }