Debian Codegraph @

src/dr/oldevomodel/treelikelihood/GeneralLikelihoodCore.java

raw symbols SCIP
1 /*
2 * GeneralLikelihoodCore.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 /**
29 * GeneralLikelihoodCore - An implementation of LikelihoodCore for any data
30 *
31 * @version $Id: GeneralLikelihoodCore.java,v 1.28 2006/08/31 14:57:24 rambaut Exp $
32 *
33 * @author Andrew Rambaut
34 */
35
36 @Deprecated // Switching to BEAGLE
37 public class GeneralLikelihoodCore extends AbstractLikelihoodCore {
38
39 /**
40 * Constructor
41 *
42 * @param stateCount number of states
43 */
44 public GeneralLikelihoodCore(int stateCount) {
45 super(stateCount);
46 }
47
48 /**
49 * Calculates partial likelihoods at a node when both children have states.
50 */
51 protected void calculateStatesStatesPruning(int[] states1, double[] matrices1,
52 int[] states2, double[] matrices2,
53 double[] partials3)
54 {
55 int v = 0;
56
57 for (int l = 0; l < matrixCount; l++) {
58
59 for (int k = 0; k < patternCount; k++) {
60
61 int state1 = states1[k];
62 int state2 = states2[k];
63
64 int w = l * matrixSize;
65
66 if (state1 < stateCount && state2 < stateCount) {
67
68 for (int i = 0; i < stateCount; i++) {
69
70 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
71
72 v++;
73 w += stateCount;
74 }
75
76 } else if (state1 < stateCount) {
77 // child 2 has a gap or unknown state so treat it as unknown
78
79 for (int i = 0; i < stateCount; i++) {
80
81 partials3[v] = matrices1[w + state1];
82
83 v++;
84 w += stateCount;
85 }
86 } else if (state2 < stateCount) {
87 // child 2 has a gap or unknown state so treat it as unknown
88
89 for (int i = 0; i < stateCount; i++) {
90
91 partials3[v] = matrices2[w + state2];
92
93 v++;
94 w += stateCount;
95 }
96 } else {
97 // both children have a gap or unknown state so set partials to 1
98
99 for (int j = 0; j < stateCount; j++) {
100 partials3[v] = 1.0;
101 v++;
102 }
103 }
104 }
105 }
106 }
107
108 /**
109 * Calculates partial likelihoods at a node when one child has states and one has partials.
110 */
111 protected void calculateStatesPartialsPruning( int[] states1, double[] matrices1,
112 double[] partials2, double[] matrices2,
113 double[] partials3)
114 {
115
116 double sum, tmp;
117
118 int u = 0;
119 int v = 0;
120
121 for (int l = 0; l < matrixCount; l++) {
122 for (int k = 0; k < patternCount; k++) {
123
124 int state1 = states1[k];
125
126 int w = l * matrixSize;
127
128 if (state1 < stateCount) {
129
130
131 for (int i = 0; i < stateCount; i++) {
132
133 tmp = matrices1[w + state1];
134
135 sum = 0.0;
136 for (int j = 0; j < stateCount; j++) {
137 sum += matrices2[w] * partials2[v + j];
138 w++;
139 }
140
141 partials3[u] = tmp * sum;
142 u++;
143 }
144
145 v += stateCount;
146 } else {
147 // Child 1 has a gap or unknown state so don't use it
148
149 for (int i = 0; i < stateCount; i++) {
150
151 sum = 0.0;
152 for (int j = 0; j < stateCount; j++) {
153 sum += matrices2[w] * partials2[v + j];
154 w++;
155 }
156
157 partials3[u] = sum;
158 u++;
159 }
160
161 v += stateCount;
162 }
163 }
164 }
165 }
166
167 /**
168 * Calculates partial likelihoods at a node when both children have partials.
169 */
170 protected void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
171 double[] partials2, double[] matrices2,
172 double[] partials3)
173 {
174 double sum1, sum2;
175
176 int u = 0;
177 int v = 0;
178
179 for (int l = 0; l < matrixCount; l++) {
180
181 for (int k = 0; k < patternCount; k++) {
182
183 int w = l * matrixSize;
184
185 for (int i = 0; i < stateCount; i++) {
186
187 sum1 = sum2 = 0.0;
188
189 for (int j = 0; j < stateCount; j++) {
190 sum1 += matrices1[w] * partials1[v + j];
191 sum2 += matrices2[w] * partials2[v + j];
192
193 w++;
194 }
195
196 partials3[u] = sum1 * sum2;
197 u++;
198 }
199 v += stateCount;
200 }
201 }
202 }
203
204 /**
205 * Calculates partial likelihoods at a node when both children have states.
206 */
207 protected void calculateStatesStatesPruning(int[] states1, double[] matrices1,
208 int[] states2, double[] matrices2,
209 double[] partials3, int[] matrixMap)
210 {
211 int v = 0;
212
213 for (int k = 0; k < patternCount; k++) {
214
215 int state1 = states1[k];
216 int state2 = states2[k];
217
218 int w = matrixMap[k] * matrixSize;
219
220 if (state1 < stateCount && state2 < stateCount) {
221
222 for (int i = 0; i < stateCount; i++) {
223
224 partials3[v] = matrices1[w + state1] * matrices2[w + state2];
225
226 v++;
227 w += stateCount;
228 }
229
230 } else if (state1 < stateCount) {
231 // child 2 has a gap or unknown state so treat it as unknown
232
233 for (int i = 0; i < stateCount; i++) {
234
235 partials3[v] = matrices1[w + state1];
236
237 v++;
238 w += stateCount;
239 }
240 } else if (state2 < stateCount) {
241 // child 2 has a gap or unknown state so treat it as unknown
242
243 for (int i = 0; i < stateCount; i++) {
244
245 partials3[v] = matrices2[w + state2];
246
247 v++;
248 w += stateCount;
249 }
250 } else {
251 // both children have a gap or unknown state so set partials to 1
252
253 for (int j = 0; j < stateCount; j++) {
254 partials3[v] = 1.0;
255 v++;
256 }
257 }
258 }
259 }
260
261 /**
262 * Calculates partial likelihoods at a node when one child has states and one has partials.
263 */
264 protected void calculateStatesPartialsPruning( int[] states1, double[] matrices1,
265 double[] partials2, double[] matrices2,
266 double[] partials3, int[] matrixMap)
267 {
268
269 double sum, tmp;
270
271 int u = 0;
272 int v = 0;
273
274 for (int k = 0; k < patternCount; k++) {
275
276 int state1 = states1[k];
277
278 int w = matrixMap[k] * matrixSize;
279
280 if (state1 < stateCount) {
281
282 for (int i = 0; i < stateCount; i++) {
283
284 tmp = matrices1[w + state1];
285
286 sum = 0.0;
287 for (int j = 0; j < stateCount; j++) {
288 sum += matrices2[w] * partials2[v + j];
289 w++;
290 }
291
292 partials3[u] = tmp * sum;
293 u++;
294 }
295
296 v += stateCount;
297 } else {
298 // Child 1 has a gap or unknown state so don't use it
299
300 for (int i = 0; i < stateCount; i++) {
301
302 sum = 0.0;
303 for (int j = 0; j < stateCount; j++) {
304 sum += matrices2[w] * partials2[v + j];
305 w++;
306 }
307
308 partials3[u] = sum;
309 u++;
310 }
311
312 v += stateCount;
313 }
314 }
315 }
316
317 /**
318 * Calculates partial likelihoods at a node when both children have partials.
319 */
320 protected void calculatePartialsPartialsPruning(double[] partials1, double[] matrices1,
321 double[] partials2, double[] matrices2,
322 double[] partials3, int[] matrixMap)
323 {
324 double sum1, sum2;
325
326 int u = 0;
327 int v = 0;
328
329 for (int k = 0; k < patternCount; k++) {
330
331 int w = matrixMap[k] * matrixSize;
332
333 for (int i = 0; i < stateCount; i++) {
334
335 sum1 = sum2 = 0.0;
336
337 for (int j = 0; j < stateCount; j++) {
338 sum1 += matrices1[w] * partials1[v + j];
339 sum2 += matrices2[w] * partials2[v + j];
340
341 w++;
342 }
343
344 partials3[u] = sum1 * sum2;
345 u++;
346 }
347 v += stateCount;
348 }
349 }
350
351 /**
352 * Integrates partials across categories.
353 * @param inPartials the array of partials to be integrated
354 * @param proportions the proportions of sites in each category
355 * @param outPartials an array into which the partials will go
356 */
357 protected void calculateIntegratePartials(double[] inPartials, double[] proportions, double[] outPartials)
358 {
359
360 int u = 0;
361 int v = 0;
362 for (int k = 0; k < patternCount; k++) {
363
364 for (int i = 0; i < stateCount; i++) {
365
366 outPartials[u] = inPartials[v] * proportions[0];
367 u++;
368 v++;
369 }
370 }
371
372
373 for (int l = 1; l < matrixCount; l++) {
374 u = 0;
375
376 for (int k = 0; k < patternCount; k++) {
377
378 for (int i = 0; i < stateCount; i++) {
379
380 outPartials[u] += inPartials[v] * proportions[l];
381 u++;
382 v++;
383 }
384 }
385 }
386 }
387
388 /**
389 * Calculates patten log likelihoods at a node.
390 * @param partials the partials used to calculate the likelihoods
391 * @param frequencies an array of state frequencies
392 * @param outLogLikelihoods an array into which the likelihoods will go
393 */
394 public void calculateLogLikelihoods(double[] partials, double[] frequencies, double[] outLogLikelihoods)
395 {
396 int v = 0;
397 for (int k = 0; k < patternCount; k++) {
398
399 double sum = 0.0;
400 for (int i = 0; i < stateCount; i++) {
401
402 sum += frequencies[i] * partials[v];
403 v++;
404 }
405 outLogLikelihoods[k] = Math.log(sum) + getLogScalingFactor(k);
406 }
407 }
408 }