Debian Codegraph @

src/dr/oldevomodel/substmodel/TwoPhaseModel.java

raw symbols SCIP
1 /*
2 * TwoPhaseModel.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
29 import dr.evolution.datatype.Microsatellite;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32
33 import java.util.ArrayList;
34
35 /**
36 * @author Chieh-Hsi Wu
37 *
38 * Implements Two Phase Model of microsatellites
39 */
40 public class TwoPhaseModel extends MicrosatelliteModel{
41
42
43 /*
44 *Parameters for setting up the infinitesimal rate matrix.
45 */
46 private Parameter geoParam;
47 private Parameter onePhasePrParam;
48 private Parameter transformParam;
49 private boolean estimateSubmodelParams = false;
50 private ArrayList<Variable<Double>> submodelParameters = null;
51 private boolean updateSubmodelRates = false;
52
53 public static final String TWO_PHASE_MODEL = "TWOPHASEModel";
54
55 /**
56 * Constructor
57 *
58 * @param microsatellite microsatellite data type
59 * @param freqModel user specified initial equilibrium frequencies
60 * @param submodel this submodel of this
61 * @param onePhasePrParam the probability that the rate will conform to the one phase model
62 * @param geoParam the probability of a success in geometric distribution of the TwoPhaseModel
63 * @param transParam the parameter for transforming onePhasePrParam and geoParam when onePhasePrParam > 0
64 * @param estimateSubmodelParams indicate whether the submodel parameters are estimated
65 */
66
67 public TwoPhaseModel(
68 Microsatellite microsatellite,
69 FrequencyModel freqModel,
70 OnePhaseModel submodel,
71 Parameter onePhasePrParam,
72 Parameter geoParam,
73 Parameter transParam,
74 boolean estimateSubmodelParams){
75
76 super(TWO_PHASE_MODEL, microsatellite, freqModel, null);
77
78 this.subModel = submodel;
79 this.estimateSubmodelParams = estimateSubmodelParams;
80
81 if(this.estimateSubmodelParams){
82 submodelParameters = new ArrayList<Variable<Double>>();
83 for(int i = 0; i < subModel.getNestedParameterCount(); i++){
84 addVariable(subModel.getNestedParameter(i));
85 submodelParameters.add(subModel.getNestedParameter(i));
86 }
87 updateSubmodelRates = true;
88 }
89
90 this.geoParam = geoParam;
91 this.onePhasePrParam = onePhasePrParam;
92
93 addVariable(this.geoParam);
94 addVariable(this.onePhasePrParam);
95
96 this.estimateSubmodelParams = estimateSubmodelParams;
97
98 if(transParam != null){
99 this.transformParam = transParam;
100 }else{
101 this.transformParam = new Parameter.Default(0.0);
102 }
103
104 //printDetails();
105
106 setupInfinitesimalRates();
107
108 if(freqModel == null){
109 useStationaryFreqs = true;
110 computeStationaryDistribution();
111 }else{
112 useStationaryFreqs = false;
113 }
114
115
116 }
117
118 private Parameter transOnePhase;
119 private Parameter transGeo;
120 private void transform(){
121 double e = transformParam.getParameterValue(0);
122 double m = geoParam.getParameterValue(0);
123 double p = onePhasePrParam.getParameterValue(0);
124 if(p < 1 - e && m < 1 - e || p ==m || e ==0){
125 transOnePhase = onePhasePrParam;
126 transGeo = geoParam;
127 }else if(m > Math.max(1 - e,p)){
128 p = p*(m-(m+e-1)/e)/m+(m+e-1)/e;
129 transOnePhase = new Parameter.Default(p);
130 }else if(p > Math.max(1 - e,m)){
131 m = m*(p-(p+e-1)/e)/p+(p+e-1)/e;
132 transGeo = new Parameter.Default(m);
133 }
134 }
135
136
137 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
138 if(submodelParameters !=null && submodelParameters.indexOf(variable) != -1){
139 updateSubmodelRates = true;
140 }
141 updateMatrix = true;
142
143 }
144
145 public void setupInfinitesimalRates(){
146 if(updateSubmodelRates){
147 subModel.setupInfinitesimalRates();
148 updateSubmodelRates = false;
149 }
150 transform();
151
152 double geoParameter = transGeo.getParameterValue(0);
153 double p = transOnePhase.getParameterValue(0);
154
155 /*double[] condProbNum = new double[stateCount];
156 for(int i = 1; i < stateCount; i++){
157 condProbNum[i] = geoParameter*Math.pow((1.0 - geoParameter),i-1);
158 }*/
159
160 setupInfinitesimalRates(
161 stateCount,
162 geoParameter,
163 p,
164 infinitesimalRateMatrix,
165 subModel.getInfinitesimalRates()
166 );
167
168
169 }
170
171 public static void setupInfinitesimalRates(
172 int stateCount,
173 double geoParameter,
174 double p,
175 double[][] rates,
176 double[][] subRates){
177
178 double[] condProbNum = new double[stateCount];
179
180 for(int i = 1; i < stateCount; i++){
181 condProbNum[i] = (1.0-geoParameter)*Math.pow(geoParameter,i-1);
182 }
183 double condGeo = 0.0;
184
185 for(int i = 0; i < stateCount; i++){
186 double expansionGeoDenom = 1-Math.pow(geoParameter,stateCount - 1 - i);
187 double contractionGeoDenom = 1 - Math.pow(geoParameter, i);
188 double rowSum = 0.0;
189 double submodelRate = 0.0;
190
191 for(int j = 0; j < stateCount; j++){
192 if(j < i){
193 condGeo = condProbNum[Math.abs(i-j)]/contractionGeoDenom;
194 submodelRate = subRates[i][i-1];
195 }else if(j > i){
196 submodelRate = subRates[i][i+1];
197 condGeo = condProbNum[Math.abs(i-j)]/expansionGeoDenom;
198 }
199
200 if(i != j){
201 if(i == j + 1 || i == j - 1){
202 rates[i][j]= submodelRate*(p + (1 - p)*condGeo);
203 }else {
204 rates[i][j] = submodelRate*(1 - p)*condGeo;
205 }
206 rowSum = rowSum+rates[i][j];
207 }
208
209 }
210
211 rates[i][i] = 0.0-rowSum;
212 }
213 }
214
215
216 public void computeStationaryDistribution() {
217
218 if(useStationaryFreqs){
219 computeTwoPhaseStationaryDistribution();
220 }
221 super.computeStationaryDistribution();
222 }
223
224 public MicrosatelliteModel getSubModel(){
225 return subModel;
226 }
227
228 public Parameter getGeometricParamter(){
229 return geoParam;
230 }
231
232 public Parameter getOnePhasePrParamter(){
233 return onePhasePrParam;
234
235 }
236
237 public Parameter getTransGeometricParamter(){
238 return transGeo;
239 }
240
241 public Parameter getTransOnePhasePrParamter(){
242 return transOnePhase;
243 }
244
245 public Parameter getTransformParam(){
246 return transformParam;
247 }
248
249 public boolean isEstimatingSubmodelParams(){
250 return estimateSubmodelParams;
251 }
252
253 public void printDetails(){
254 System.out.println("Details of the TwoPhase Model and its paramters:");
255 System.out.println("a submodel: "+isNested);
256 System.out.println("has submodel: "+hasSubmodel());
257 if(hasSubmodel()){
258 System.out.println("submodel class: "+subModel.getClass());
259 }
260 System.out.println("esitmating submodel parameters: "+estimateSubmodelParams);
261 System.out.println("one phase probability: "+onePhasePrParam.getParameterValue(0));
262 System.out.println("geometric parameter: "+geoParam.getParameterValue(0));
263 System.out.println("transformation parameter: "+transformParam.getParameterValue(0));
264 }
265
266 }