Debian Codegraph @

src/dr/evomodel/substmodel/UniformizedSubstitutionModel.java

raw symbols SCIP
1 /*
2 * UniformizedSubstitutionModel.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.evomodel.substmodel;
27
28 import dr.inference.markovjumps.MarkovJumpsType;
29 import dr.inference.markovjumps.StateHistory;
30 import dr.inference.markovjumps.SubordinatedProcess;
31 import dr.inference.markovjumps.UniformizedStateHistory;
32 import dr.inference.model.Model;
33
34 import java.util.logging.Logger;
35
36 /**
37 * A class extension for implementing Markov chain-induced counting processes (markovjumps)
38 * via uniformization in BEAST using BEAGLE
39 * <p/>
40 * This work is supported by NSF grant 0856099
41 * <p/>
42 * Minin VN and Suchard MA (2008) Counting labeled transitions in continuous-time Markov models of evolution.
43 * Journal of Mathematical Biology, 56, 391-412.
44 * <p/>
45 * Rodrigue N, Philippe H and Lartillot N (2006) Uniformization for sampling realizations of Markov processes:
46 * applications to Bayesian implementations of codon substitution models. Bioinformatics, 24, 56-62.
47 * <p/>
48 * Hobolth A and Stone E (2009) Simulation from endpoint-conditioned, continuous-time Markov chains on a finite
49 * state space, with applications to molecular evolution. Annals of Applied Statistics, 3, 1204-1231.
50 *
51 * @author Marc Suchard
52 * @author Vladimir Minin
53 */
54
55 public class UniformizedSubstitutionModel extends MarkovJumpsSubstitutionModel {
56
57 public UniformizedSubstitutionModel(SubstitutionModel substModel) {
58 this(substModel, MarkovJumpsType.COUNTS);
59 }
60
61 public UniformizedSubstitutionModel(SubstitutionModel substModel, MarkovJumpsType type) {
62 this(substModel, type, 1);
63 }
64
65 public UniformizedSubstitutionModel(SubstitutionModel substModel, MarkovJumpsType type, int numSimulants) {
66 super(substModel, type);
67 this.numSimulants = numSimulants;
68 updateSubordinator = true;
69 }
70
71 protected void setupStorage() {
72 super.setupStorage();
73 tmp = new double[stateCount * stateCount];
74 }
75
76 protected void storeState() {
77 storedSubordinator = subordinator;
78 }
79
80 protected void restoreState() {
81 subordinator = storedSubordinator;
82 }
83
84 private void constructSubordinator() {
85 substModel.getInfinitesimalMatrix(tmp);
86 subordinator = new SubordinatedProcess(tmp, stateCount);
87 updateSubordinator = false;
88 }
89
90 protected void handleModelChangedEvent(Model model, Object object, int index) {
91 if (model == substModel) {
92 updateSubordinator = true;
93 }
94 super.handleModelChangedEvent(model, object, index);
95 }
96
97 public void setSaveCompleteHistory(boolean in) {
98 saveCompleteHistory = in;
99 }
100
101 public void computeCondStatMarkovJumps(double time,
102 double[] countMatrix) {
103
104 throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel");
105 }
106
107 public void computeCondStatMarkovJumps(double time,
108 double[] transitionProbs,
109 double[] countMatrix) {
110
111 throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel");
112 }
113
114 public void computeJointStatMarkovJumps(double time,
115 double[] countMatrix) {
116
117 throw new IllegalArgumentException("Not implemented for UniformizedSubstitutionModel");
118 }
119
120 public double computeCondStatMarkovJumps(int startingState,
121 int endingState,
122 double time) {
123
124 substModel.getTransitionProbabilities(time, tmp);
125 return computeCondStatMarkovJumps(startingState, endingState, time,
126 tmp[startingState * stateCount + endingState]);
127 }
128
129 public String getCompleteHistory() {
130 return getCompleteHistory(null, null);
131 }
132
133 public String getCompleteHistory(Double newStartTime, Double newEndTime) {
134 return getCompleteHistory(-1, newStartTime, newEndTime);
135 }
136
137 public String getCompleteHistory(int site, Double newStartTime, Double newEndTime) {
138 if (newStartTime != null && newEndTime != null) {
139 // Rescale time of events
140 completeHistory.rescaleTimesOfEvents(newStartTime, newEndTime);
141 }
142 return completeHistory.toStringChanges(site, dataType); //, 0.0);
143 }
144
145 public int getNumberOfJumpsInCompleteHistory() {
146 return completeHistory == null ? -1 : completeHistory.getNumberOfJumps();
147 }
148
149 public double computeCondStatMarkovJumps(int startingState,
150 int endingState,
151 double time,
152 double transitionProbability) {
153
154 if (updateSubordinator) {
155 constructSubordinator();
156 }
157
158 double total = 0;
159 for (int i = 0; i < numSimulants; i++) {
160 StateHistory history = null;
161 try {
162 history = UniformizedStateHistory.simulateConditionalOnEndingState(
163 0.0,
164 startingState,
165 time,
166 endingState,
167 transitionProbability,
168 stateCount,
169 subordinator
170 );
171 } catch (SubordinatedProcess.Exception e) {
172
173 if (RETURN_NAN) {
174 if (reportWarning) {
175 Logger.getLogger("dr.app.beagle").info(
176 "Unable to compute a robust count; this is most likely due to poor starting values."
177 );
178 }
179 reportWarning = false;
180 return Double.NaN;
181 }
182
183 // Error in uniformization; try rejection sampling
184 System.err.println("Attempting rejection sampling after uniformization failure");
185
186 substModel.getInfinitesimalMatrix(tmp);
187 int attempts = 0;
188 boolean success = false;
189
190 while (!success) {
191 if (attempts >= maxRejectionAttempts) {
192 throw new RuntimeException("Rejection sampling failure, after uniformization failure");
193 }
194
195 history = StateHistory.simulateUnconditionalOnEndingState(0.0, startingState, time, tmp, stateCount);
196 if (history.getEndingState() == endingState) {
197 success = true;
198 }
199
200 attempts++;
201 }
202 }
203 total += getProcessForSimulant(history);
204 if (saveCompleteHistory) {
205 if (numSimulants == 1) {
206 completeHistory = history;
207 } else {
208 throw new RuntimeException("Use single simulant when saving complete histories");
209 }
210 }
211 }
212 return total / (double) numSimulants;
213 }
214
215 public StateHistory getStateHistory() {
216 return completeHistory;
217 }
218
219 private final int numSimulants;
220 private boolean updateSubordinator;
221 private SubordinatedProcess subordinator;
222 private SubordinatedProcess storedSubordinator;
223
224 private boolean saveCompleteHistory = false;
225 private StateHistory completeHistory = null;
226
227 private double[] tmp;
228
229 private static int maxRejectionAttempts = 100000;
230 private static final boolean RETURN_NAN = true;
231 private static boolean reportWarning = true;
232 }