Debian Codegraph @

src/dr/evomodel/arg/likelihood/AbstractARGLikelihood.java

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