Debian Codegraph @

src/dr/oldevomodel/treelikelihood/AbstractLikelihoodCore.java

raw symbols SCIP
1 /*
2 * AbstractLikelihoodCore.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 java.util.Arrays;
29
30 /**
31 * AbstractLikelihoodCore - An abstract base class for LikelihoodCores
32 *
33 * @author Andrew Rambaut
34 * @version $Id: AbstractLikelihoodCore.java,v 1.11 2006/08/30 16:02:42 rambaut Exp $
35 */
36
37 @Deprecated // Switching to BEAGLE
38 public abstract class AbstractLikelihoodCore implements LikelihoodCore {
39
40 protected int stateCount;
41 protected int nodeCount;
42 protected int patternCount;
43 protected int partialsSize;
44 protected int matrixSize;
45 protected int matrixCount;
46
47 protected boolean integrateCategories;
48
49 protected double[][][] partials;
50
51 protected int[][] states;
52
53 protected double[][][] matrices;
54
55 protected int[] currentMatricesIndices;
56 protected int[] storedMatricesIndices;
57 protected int[] currentPartialsIndices;
58 protected int[] storedPartialsIndices;
59
60 protected boolean useScaling = false;
61
62 protected double[][][] scalingFactors;
63
64 private double scalingThreshold = 1.0E-100;
65
66 /**
67 * Constructor
68 *
69 * @param stateCount number of states
70 */
71 public AbstractLikelihoodCore(int stateCount) {
72 this.stateCount = stateCount;
73 }
74
75 /**
76 * initializes partial likelihood arrays.
77 *
78 * @param nodeCount the number of nodes in the tree
79 * @param patternCount the number of patterns
80 * @param matrixCount the number of matrices (i.e., number of categories)
81 * @param integrateCategories whether sites are being integrated over all matrices
82 */
83 public void initialize(int nodeCount, int patternCount, int matrixCount, boolean integrateCategories) {
84
85 this.nodeCount = nodeCount;
86 this.patternCount = patternCount;
87 this.matrixCount = matrixCount;
88
89 this.integrateCategories = integrateCategories;
90
91 if (integrateCategories) {
92 partialsSize = patternCount * stateCount * matrixCount;
93 } else {
94 partialsSize = patternCount * stateCount;
95 }
96
97 partials = new double[2][nodeCount][];
98
99 currentMatricesIndices = new int[nodeCount];
100 storedMatricesIndices = new int[nodeCount];
101
102 currentPartialsIndices = new int[nodeCount];
103 storedPartialsIndices = new int[nodeCount];
104
105 states = new int[nodeCount][];
106
107 for (int i = 0; i < nodeCount; i++) {
108 partials[0][i] = null;
109 partials[1][i] = null;
110
111 states[i] = null;
112 }
113
114 matrixSize = stateCount * stateCount;
115
116 matrices = new double[2][nodeCount][matrixCount * matrixSize];
117 }
118
119 /**
120 * cleans up and deallocates arrays.
121 */
122 public void finalize() throws java.lang.Throwable {
123 super.finalize();
124
125 nodeCount = 0;
126 patternCount = 0;
127 matrixCount = 0;
128
129 partials = null;
130 currentPartialsIndices = null;
131 storedPartialsIndices = null;
132 states = null;
133 matrices = null;
134 currentMatricesIndices = null;
135 storedMatricesIndices = null;
136
137 scalingFactors = null;
138 }
139
140 public void setUseScaling(boolean useScaling) {
141 this.useScaling = useScaling;
142
143 if (useScaling) {
144 scalingFactors = new double[2][nodeCount][patternCount];
145 }
146 }
147
148 /**
149 * Allocates partials for a node
150 */
151 public void createNodePartials(int nodeIndex) {
152
153 this.partials[0][nodeIndex] = new double[partialsSize];
154 this.partials[1][nodeIndex] = new double[partialsSize];
155 }
156
157 /**
158 * Sets partials for a node
159 */
160 public void setNodePartials(int nodeIndex, double[] partials) {
161
162 if (this.partials[0][nodeIndex] == null) {
163 createNodePartials(nodeIndex);
164 }
165 if (partials.length < partialsSize) {
166 int k = 0;
167 for (int i = 0; i < matrixCount; i++) {
168 System.arraycopy(partials, 0, this.partials[0][nodeIndex], k, partials.length);
169 k += partials.length;
170 }
171 } else {
172 System.arraycopy(partials, 0, this.partials[0][nodeIndex], 0, partials.length);
173 }
174 }
175
176 /**
177 * Allocates states for a node
178 */
179 public void createNodeStates(int nodeIndex) {
180
181 this.states[nodeIndex] = new int[patternCount];
182 }
183
184 /**
185 * Sets states for a node
186 */
187 public void setNodeStates(int nodeIndex, int[] states) {
188
189 if (this.states[nodeIndex] == null) {
190 createNodeStates(nodeIndex);
191 }
192 System.arraycopy(states, 0, this.states[nodeIndex], 0, patternCount);
193 }
194
195 /**
196 * Gets states for a node
197 */
198 public void getNodeStates(int nodeIndex, int[] states) {
199 System.arraycopy(this.states[nodeIndex], 0, states, 0, patternCount);
200 }
201
202 public void setNodeMatrixForUpdate(int nodeIndex) {
203 currentMatricesIndices[nodeIndex] = 1 - currentMatricesIndices[nodeIndex];
204
205 }
206
207
208 /**
209 * Sets probability matrix for a node
210 */
211 public void setNodeMatrix(int nodeIndex, int matrixIndex, double[] matrix) {
212 System.arraycopy(matrix, 0, matrices[currentMatricesIndices[nodeIndex]][nodeIndex],
213 matrixIndex * matrixSize, matrixSize);
214 }
215
216 /**
217 * Gets probability matrix for a node
218 */
219 public void getNodeMatrix(int nodeIndex, int matrixIndex, double[] matrix) {
220 System.arraycopy(matrices[currentMatricesIndices[nodeIndex]][nodeIndex],
221 matrixIndex * matrixSize, matrix, 0, matrixSize);
222 }
223
224 public void setNodePartialsForUpdate(int nodeIndex) {
225 currentPartialsIndices[nodeIndex] = 1 - currentPartialsIndices[nodeIndex];
226 }
227
228 /**
229 * Sets the currently updating node partials for node nodeIndex. This may
230 * need to repeatedly copy the partials for the different category partitions
231 */
232 public void setCurrentNodePartials(int nodeIndex, double[] partials) {
233 if (partials.length < partialsSize) {
234 int k = 0;
235 for (int i = 0; i < matrixCount; i++) {
236 System.arraycopy(partials, 0, this.partials[currentPartialsIndices[nodeIndex]][nodeIndex], k, partials.length);
237 k += partials.length;
238 }
239 } else {
240 System.arraycopy(partials, 0, this.partials[currentPartialsIndices[nodeIndex]][nodeIndex], 0, partials.length);
241 }
242 }
243
244 /**
245 * Calculates partial likelihoods at a node.
246 *
247 * @param nodeIndex1 the 'child 1' node
248 * @param nodeIndex2 the 'child 2' node
249 * @param nodeIndex3 the 'parent' node
250 */
251 public void calculatePartials(int nodeIndex1, int nodeIndex2, int nodeIndex3) {
252 if (states[nodeIndex1] != null) {
253 if (states[nodeIndex2] != null) {
254 calculateStatesStatesPruning(
255 states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
256 states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
257 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
258 } else {
259 calculateStatesPartialsPruning(states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
260 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
261 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
262 }
263 } else {
264 if (states[nodeIndex2] != null) {
265 calculateStatesPartialsPruning(states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
266 partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
267 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
268 } else {
269 calculatePartialsPartialsPruning(partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
270 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
271 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
272 }
273 }
274
275 if (useScaling) {
276 scalePartials(nodeIndex3);
277 }
278
279 //
280 // int k =0;
281 // for (int i = 0; i < patternCount; i++) {
282 // double f = 0.0;
283 //
284 // for (int j = 0; j < stateCount; j++) {
285 // f += partials[currentPartialsIndices[nodeIndex3]][nodeIndex3][k];
286 // k++;
287 // }
288 // if (f == 0.0) {
289 // Logger.getLogger("error").severe("A partial likelihood (node index = " + nodeIndex3 + ", pattern = "+ i +") is zero for all states.");
290 // }
291 // }
292 }
293
294 /**
295 * Calculates partial likelihoods at a node when both children have states.
296 */
297 protected abstract void calculateStatesStatesPruning(int[] states1, double[] matrices1,
298 int[] states2, double[] matrices2,
299 double[] partials3);
300
301 /**
302 * Calculates partial likelihoods at a node when one child has states and one has partials.
303 */
304 protected abstract void calculateStatesPartialsPruning(int[] states1, double[] matrices1,
305 double[] partials2, double[] matrices2,
306 double[] partials3);
307
308 /**
309 * Calculates partial likelihoods at a node when both children have partials.
310 */
311 protected abstract void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
312 double[] partials2, double[] matrices2,
313 double[] partials3);
314
315 /**
316 * Calculates partial likelihoods at a node.
317 *
318 * @param nodeIndex1 the 'child 1' node
319 * @param nodeIndex2 the 'child 2' node
320 * @param nodeIndex3 the 'parent' node
321 * @param matrixMap a map of which matrix to use for each pattern (can be null if integrating over categories)
322 */
323 public void calculatePartials(int nodeIndex1, int nodeIndex2, int nodeIndex3, int[] matrixMap) {
324 if (states[nodeIndex1] != null) {
325 if (states[nodeIndex2] != null) {
326 calculateStatesStatesPruning(
327 states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
328 states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
329 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3], matrixMap);
330 } else {
331 calculateStatesPartialsPruning(
332 states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
333 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
334 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3], matrixMap);
335 }
336 } else {
337 if (states[nodeIndex2] != null) {
338 calculateStatesPartialsPruning(
339 states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
340 partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
341 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3], matrixMap);
342 } else {
343 calculatePartialsPartialsPruning(
344 partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
345 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
346 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3], matrixMap);
347 }
348 }
349
350 if (useScaling) {
351 scalePartials(nodeIndex3);
352 }
353 }
354
355 /**
356 * Calculates partial likelihoods at a node when both children have states.
357 */
358 protected abstract void calculateStatesStatesPruning(int[] states1, double[] matrices1,
359 int[] states2, double[] matrices2,
360 double[] partials3, int[] matrixMap);
361
362 /**
363 * Calculates partial likelihoods at a node when one child has states and one has partials.
364 */
365 protected abstract void calculateStatesPartialsPruning(int[] states1, double[] matrices1,
366 double[] partials2, double[] matrices2,
367 double[] partials3, int[] matrixMap);
368
369 /**
370 * Calculates partial likelihoods at a node when both children have partials.
371 */
372 protected abstract void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
373 double[] partials2, double[] matrices2,
374 double[] partials3, int[] matrixMap);
375
376
377 public void integratePartials(int nodeIndex, double[] proportions, double[] outPartials) {
378 calculateIntegratePartials(partials[currentPartialsIndices[nodeIndex]][nodeIndex], proportions, outPartials);
379 }
380
381 /**
382 * Integrates partials across categories.
383 *
384 * @param inPartials the partials at the node to be integrated
385 * @param proportions the proportions of sites in each category
386 * @param outPartials an array into which the integrated partials will go
387 */
388 protected abstract void calculateIntegratePartials(double[] inPartials, double[] proportions, double[] outPartials);
389
390 /**
391 * Scale the partials at a given node. This uses a scaling suggested by Ziheng Yang in
392 * Yang (2000) J. Mol. Evol. 51: 423-432
393 * <p/>
394 * This function looks over the partial likelihoods for each state at each pattern
395 * and finds the largest. If this is less than the scalingThreshold (currently set
396 * to 1E-40) then it rescales the partials for that pattern by dividing by this number
397 * (i.e., normalizing to between 0, 1). It then stores the log of this scaling.
398 * This is called for every internal node after the partials are calculated so provides
399 * most of the performance hit. Ziheng suggests only doing this on a proportion of nodes
400 * but this sounded like a headache to organize (and he doesn't use the threshold idea
401 * which improves the performance quite a bit).
402 *
403 * @param nodeIndex
404 */
405 protected void scalePartials(int nodeIndex) {
406 int u = 0;
407
408 for (int i = 0; i < patternCount; i++) {
409
410 double scaleFactor = 0.0;
411 int v = u;
412 for (int k = 0; k < matrixCount; k++) {
413 for (int j = 0; j < stateCount; j++) {
414 if (partials[currentPartialsIndices[nodeIndex]][nodeIndex][v] > scaleFactor) {
415 scaleFactor = partials[currentPartialsIndices[nodeIndex]][nodeIndex][v];
416 }
417 v++;
418 }
419 v += (patternCount - 1) * stateCount;
420 }
421
422 if (scaleFactor < scalingThreshold) {
423
424 v = u;
425 for (int k = 0; k < matrixCount; k++) {
426 for (int j = 0; j < stateCount; j++) {
427 partials[currentPartialsIndices[nodeIndex]][nodeIndex][v] /= scaleFactor;
428 v++;
429 }
430 v += (patternCount - 1) * stateCount;
431 }
432 scalingFactors[currentPartialsIndices[nodeIndex]][nodeIndex][i] = Math.log(scaleFactor);
433
434 } else {
435 scalingFactors[currentPartialsIndices[nodeIndex]][nodeIndex][i] = 0.0;
436 }
437 u += stateCount;
438
439
440 }
441 }
442
443 /**
444 * This function returns the scaling factor for that pattern by summing over
445 * the log scalings used at each node. If scaling is off then this just returns
446 * a 0.
447 *
448 * @return the log scaling factor
449 */
450 public double getLogScalingFactor(int pattern) {
451 double logScalingFactor = 0.0;
452 if (useScaling) {
453 for (int i = 0; i < nodeCount; i++) {
454 logScalingFactor += scalingFactors[currentPartialsIndices[i]][i][pattern];
455 }
456 }
457 return logScalingFactor;
458 }
459
460 public void getLogScalingFactors(int nodeIndex, double[] buffer) {
461 if (useScaling) {
462 System.arraycopy(scalingFactors[currentPartialsIndices[nodeIndex]][nodeIndex], 0,
463 buffer, 0, patternCount);
464 } else {
465 Arrays.fill(buffer, 0.0);
466 }
467 }
468
469 public boolean arePartialsRescaled() {
470 return useScaling;
471 }
472
473 /**
474 * Gets the partials for a particular node.
475 *
476 * @param nodeIndex the node
477 * @param outPartials an array into which the partials will go
478 */
479 public void getPartials(int nodeIndex, double[] outPartials) {
480 double[] partials1 = partials[currentPartialsIndices[nodeIndex]][nodeIndex];
481
482 System.arraycopy(partials1, 0, outPartials, 0, partialsSize);
483 }
484
485 /**
486 * Store current state
487 */
488 public void storeState() {
489
490 System.arraycopy(currentMatricesIndices, 0, storedMatricesIndices, 0, nodeCount);
491 System.arraycopy(currentPartialsIndices, 0, storedPartialsIndices, 0, nodeCount);
492 }
493
494 /**
495 * Restore the stored state
496 */
497 public void restoreState() {
498 // Rather than copying the stored stuff back, just swap the pointers...
499 int[] tmp1 = currentMatricesIndices;
500 currentMatricesIndices = storedMatricesIndices;
501 storedMatricesIndices = tmp1;
502
503 int[] tmp2 = currentPartialsIndices;
504 currentPartialsIndices = storedPartialsIndices;
505 storedPartialsIndices = tmp2;
506 }
507 }