Debian Codegraph @

src/dr/oldevomodel/treelikelihood/AbstractTreeLikelihood.java

raw symbols SCIP
1 /*
2 * AbstractTreeLikelihood.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.treelikelihood;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.datatype.DataType;
30 import dr.evolution.tree.NodeRef;
31 import dr.evomodel.tree.TreeModel;
32 import dr.inference.model.AbstractModelLikelihood;
33 import dr.inference.model.Model;
34 import dr.inference.model.Parameter;
35 import dr.inference.model.Variable;
36 import dr.xml.Reportable;
37
38 /**
39 * AbstractTreeLikelihood - a base class for likelihood calculators of sites on a tree.
40 *
41 * @author Andrew Rambaut
42 * @version $Id: AbstractTreeLikelihood.java,v 1.16 2005/06/07 16:27:39 alexei Exp $
43 */
44
45 @Deprecated // Switching to BEAGLE
46 public abstract class AbstractTreeLikelihood extends AbstractModelLikelihood implements Reportable {
47
48 protected static final boolean COUNT_TOTAL_OPERATIONS = true;
49
50 public AbstractTreeLikelihood(String name, PatternList patternList,
51 TreeModel treeModel) {
52
53 super(name);
54
55 this.patternList = patternList;
56 this.dataType = patternList.getDataType();
57 patternCount = patternList.getPatternCount();
58 stateCount = dataType.getStateCount();
59
60 patternWeights = patternList.getPatternWeights();
61
62 this.treeModel = treeModel;
63 addModel(treeModel);
64
65 nodeCount = treeModel.getNodeCount();
66
67 updateNode = new boolean[nodeCount];
68 for (int i = 0; i < nodeCount; i++) {
69 updateNode[i] = true;
70 }
71
72 likelihoodKnown = false;
73
74 }
75
76 /**
77 * Sets the partials from a sequence in an alignment.
78 */
79 protected final void setStates(LikelihoodCore likelihoodCore, PatternList patternList,
80 int sequenceIndex, int nodeIndex) {
81 int i;
82
83 int[] states = new int[patternCount];
84
85 for (i = 0; i < patternCount; i++) {
86
87 states[i] = patternList.getPatternState(sequenceIndex, i);
88 }
89
90 likelihoodCore.setNodeStates(nodeIndex, states);
91 }
92
93 public TreeModel getTreeModel() {
94 return treeModel;
95 }
96
97 /**
98 * Sets the partials from a sequence in an alignment.
99 */
100 protected final void setPartials(LikelihoodCore likelihoodCore, PatternList patternList,
101 int categoryCount,
102 int sequenceIndex, int nodeIndex) {
103 double[] partials = new double[patternCount * stateCount];
104
105 boolean[] stateSet;
106
107 int v = 0;
108 for (int i = 0; i < patternCount; i++) {
109
110 int state = patternList.getPatternState(sequenceIndex, i);
111 stateSet = dataType.getStateSet(state);
112
113 for (int j = 0; j < stateCount; j++) {
114 if (stateSet[j]) {
115 partials[v] = 1.0;
116 } else {
117 partials[v] = 0.0;
118 }
119 v++;
120 }
121 }
122
123 likelihoodCore.setNodePartials(nodeIndex, partials);
124 }
125
126 /**
127 * Sets the partials from a sequence in an alignment.
128 */
129 protected final void setMissingStates(LikelihoodCore likelihoodCore, int nodeIndex) {
130 int[] states = new int[patternCount];
131
132 for (int i = 0; i < patternCount; i++) {
133 states[i] = dataType.getGapState();
134 }
135
136 likelihoodCore.setNodeStates(nodeIndex, states);
137 }
138
139 /**
140 * Sets the partials from a sequence in an alignment.
141 */
142 protected final void setMissingPartials(LikelihoodCore likelihoodCore, int nodeIndex) {
143 double[] partials = new double[patternCount * stateCount];
144
145 int v = 0;
146 for (int i = 0; i < patternCount; i++) {
147 for (int j = 0; j < stateCount; j++) {
148 partials[v] = 1.0;
149 v++;
150 }
151 }
152
153 likelihoodCore.setNodePartials(nodeIndex, partials);
154 }
155
156 /**
157 * Set update flag for a node and its children
158 */
159 protected void updateNode(NodeRef node) {
160
161 updateNode[node.getNumber()] = true;
162 likelihoodKnown = false;
163 }
164
165 /**
166 * Set update flag for a node and its direct children
167 */
168 protected void updateNodeAndChildren(NodeRef node) {
169 updateNode[node.getNumber()] = true;
170
171 for (int i = 0; i < treeModel.getChildCount(node); i++) {
172 NodeRef child = treeModel.getChild(node, i);
173 updateNode[child.getNumber()] = true;
174 }
175 likelihoodKnown = false;
176 }
177
178 /**
179 * Set update flag for a node and all its descendents
180 */
181 protected void updateNodeAndDescendents(NodeRef node) {
182 updateNode[node.getNumber()] = true;
183
184 for (int i = 0; i < treeModel.getChildCount(node); i++) {
185 NodeRef child = treeModel.getChild(node, i);
186 updateNodeAndDescendents(child);
187 }
188
189 likelihoodKnown = false;
190 }
191
192 /**
193 * Set update flag for all nodes
194 */
195 protected void updateAllNodes() {
196 for (int i = 0; i < nodeCount; i++) {
197 updateNode[i] = true;
198 }
199 likelihoodKnown = false;
200 }
201
202 /**
203 * Set update flag for a pattern
204 */
205 protected void updatePattern(int i) {
206 if (updatePattern != null) {
207 updatePattern[i] = true;
208 }
209 likelihoodKnown = false;
210 }
211
212 /**
213 * Set update flag for all patterns
214 */
215 protected void updateAllPatterns() {
216 if (updatePattern != null) {
217 for (int i = 0; i < patternCount; i++) {
218 updatePattern[i] = true;
219 }
220 }
221 likelihoodKnown = false;
222 }
223
224 public final double[] getPatternWeights() {
225 return patternWeights;
226 }
227
228 public final int getPatternCount() {
229 return patternCount;
230 }
231
232 // **************************************************************
233 // VariableListener IMPLEMENTATION
234 // **************************************************************
235
236 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
237 // do nothing
238 }
239
240 // **************************************************************
241 // Model IMPLEMENTATION
242 // **************************************************************
243
244 protected void handleModelChangedEvent(Model model, Object object, int index) {
245 likelihoodKnown = false;
246 }
247
248 /**
249 * Stores the additional state other than model components
250 */
251 protected void storeState() {
252
253 storedLikelihoodKnown = likelihoodKnown;
254 storedLogLikelihood = logLikelihood;
255 }
256
257 /**
258 * Restore the additional stored state
259 */
260 protected void restoreState() {
261
262 likelihoodKnown = storedLikelihoodKnown;
263 logLikelihood = storedLogLikelihood;
264 }
265
266 protected void acceptState() {
267 } // nothing to do
268
269 // **************************************************************
270 // Likelihood IMPLEMENTATION
271 // **************************************************************
272
273 public final Model getModel() {
274 return this;
275 }
276
277 public final PatternList getPatternList() {
278 return patternList;
279 }
280
281 public final double getLogLikelihood() {
282 if (!likelihoodKnown) {
283 logLikelihood = calculateLogLikelihood();
284 likelihoodKnown = true;
285 }
286 return logLikelihood;
287 }
288
289 /**
290 * Forces a complete recalculation of the likelihood next time getLikelihood is called
291 */
292 public void makeDirty() {
293 likelihoodKnown = false;
294 updateAllNodes();
295 updateAllPatterns();
296 }
297
298 protected abstract double calculateLogLikelihood();
299
300 public String getReport() {
301 getLogLikelihood();
302 return getClass().getName() + "(" + logLikelihood + ") total operations = " + totalOperationCount;
303
304 }
305
306 // **************************************************************
307 // INSTANCE VARIABLES
308 // **************************************************************
309
310 /**
311 * the tree
312 */
313 protected TreeModel treeModel = null;
314
315 /**
316 * the patternList
317 */
318 protected PatternList patternList = null;
319
320 protected DataType dataType = null;
321
322 /**
323 * the pattern weights
324 */
325 protected double[] patternWeights;
326
327 /**
328 * the number of patterns
329 */
330 protected int patternCount;
331
332 /**
333 * the number of states in the data
334 */
335 protected int stateCount;
336
337 /**
338 * the number of nodes in the tree
339 */
340 protected int nodeCount;
341
342 /**
343 * Flags to specify which patterns are to be updated
344 */
345 protected boolean[] updatePattern = null;
346
347 /**
348 * Flags to specify which nodes are to be updated
349 */
350 protected boolean[] updateNode;
351
352 private double logLikelihood;
353 private double storedLogLikelihood;
354 protected boolean likelihoodKnown = false;
355 private boolean storedLikelihoodKnown = false;
356
357 protected int totalOperationCount = 0;
358 }