Debian Codegraph @

src/dr/evolution/parsimony/SankoffParsimony.java

raw symbols SCIP
1 /*
2 * SankoffParsimony.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.evolution.parsimony;
27
28 import dr.evolution.alignment.PatternList;
29 import dr.evolution.alignment.Patterns;
30 import dr.evolution.datatype.Nucleotides;
31 import dr.evolution.tree.*;
32 import dr.evolution.util.Taxon;
33
34 import java.util.Set;
35 import java.util.TreeSet;
36 import java.util.Iterator;
37
38 /**
39 * Class for reconstructing characters using the Sankoff generalized parsimony methods. This will be
40 * slower than the Fitch algorithm but it allows Weighted Parsimony.
41 *
42 * @version $Id: SankoffParsimony.java,v 1.7 2005/06/29 16:54:18 beth Exp $
43 *
44 * @author Andrew Rambaut
45 * @author Alexei Drummond
46 */
47 public class SankoffParsimony implements ParsimonyCriterion {
48
49 private final int stateCount;
50
51 private int[][] stateSets;
52 private double[][][] nodeScores;
53 private int[][] nodeStates;
54
55 private Tree tree = null;
56 private final PatternList patterns;
57 private final double[][] costMatrix;
58
59 private final boolean compressStates = true;
60
61 private boolean hasCalculatedSteps = false;
62 private boolean hasRecontructedStates = false;
63
64 private final double[] siteScores;
65
66 public SankoffParsimony(PatternList patterns) {
67 if (patterns == null) {
68 throw new IllegalArgumentException("The patterns cannot be null");
69 }
70 stateCount = patterns.getDataType().getStateCount();
71 this.costMatrix = new double[stateCount][stateCount];
72 for (int i = 0; i < stateCount; i++) {
73 for (int j = 0; j < stateCount; j++) {
74 if (i == j) {
75 costMatrix[i][j] = 0.0;
76 } else {
77 costMatrix[i][j] = 1.0;
78 }
79 }
80 }
81
82 this.patterns = patterns;
83 this.siteScores = new double[patterns.getPatternCount()];
84 }
85
86 public SankoffParsimony(PatternList patterns, double[][] costMatrix) {
87 if (patterns == null) {
88 throw new IllegalArgumentException("The patterns cannot be null");
89 }
90 stateCount = patterns.getDataType().getStateCount();
91 if (costMatrix.length != stateCount || costMatrix[0].length != stateCount) {
92 throw new IllegalArgumentException("The cost matrix is of the wrong dimension: expecting " + stateCount + " square");
93 }
94 this.costMatrix = costMatrix;
95
96 this.patterns = patterns;
97 this.siteScores = new double[patterns.getPatternCount()];
98 }
99
100 /**
101 * Calculates the minimum number of siteScores for the parsimony reconstruction of a
102 * a set of character patterns on a tree.
103 * @param tree a tree object to reconstruct the characters on
104 * @return number of parsimony siteScores
105 */
106 public double[] getSiteScores(Tree tree) {
107
108 if (tree == null) {
109 throw new IllegalArgumentException("The tree cannot be null");
110 }
111
112 if (this.tree == null || this.tree != tree) {
113 this.tree = tree;
114
115 initialize();
116 }
117
118 if (!hasCalculatedSteps) {
119 calculateSteps(tree, tree.getRoot(), patterns);
120 if (compressStates) {
121 for (int i = 0; i < siteScores.length; i++) {
122 double[] Sr = nodeScores[tree.getRoot().getNumber()][i];
123 siteScores[i] = minScore(Sr, stateSets[i]);
124 }
125 } else {
126 for (int i = 0; i < siteScores.length; i++) {
127 double[] Sr = nodeScores[tree.getRoot().getNumber()][i];
128 siteScores[i] = minScore(Sr);
129 }
130 }
131 hasCalculatedSteps = true;
132 }
133
134
135 return siteScores;
136 }
137
138 public double getScore(Tree tree) {
139
140 getSiteScores(tree);
141
142 double score = 0;
143
144 for (int i = 0; i < patterns.getPatternCount(); i++) {
145 score += siteScores[i] * patterns.getPatternWeight(i);
146 }
147 return score;
148 }
149
150 /**
151 * Returns the reconstructed character nodeStates for a given node in the tree. If this method is repeatedly
152 * called with the same tree and patterns then only the first call will reconstruct the nodeStates and each
153 * subsequent call will return the stored nodeStates.
154 * @param tree a tree object to reconstruct the characters on
155 * @param node the node of the tree
156 * @return an array containing the reconstructed nodeStates for this node
157 */
158 public int[] getStates(Tree tree, NodeRef node) {
159
160 getSiteScores(tree);
161
162 if (!hasRecontructedStates) {
163 if (compressStates) {
164 for (int i = 0; i < patterns.getPatternCount(); i++) {
165 nodeStates[tree.getRoot().getNumber()][i] = minState(nodeScores[tree.getRoot().getNumber()][i], stateSets[i]);
166 }
167 } else {
168 for (int i = 0; i < patterns.getPatternCount(); i++) {
169 nodeStates[tree.getRoot().getNumber()][i] = minState(nodeScores[tree.getRoot().getNumber()][i]);
170 }
171 }
172 reconstructStates(tree, tree.getRoot(), nodeStates[tree.getRoot().getNumber()]);
173 hasRecontructedStates = true;
174 }
175
176 return nodeStates[node.getNumber()];
177 }
178
179 private void initialize() {
180 hasCalculatedSteps = false;
181 hasRecontructedStates = false;
182
183 if (compressStates) {
184 stateSets = new int[patterns.getPatternCount()][];
185 }
186
187 nodeScores = new double[tree.getNodeCount()][patterns.getPatternCount()][];
188 nodeStates = new int[tree.getNodeCount()][patterns.getPatternCount()];
189
190 for (int i = 0; i < patterns.getPatternCount(); i++) {
191 int[] pattern = patterns.getPattern(i);
192
193 if (compressStates) {
194 Set observedStates = new TreeSet();
195 for (int j = 0; j < pattern.length; j++) {
196 boolean[] stateSet = patterns.getDataType().getStateSet(pattern[j]);
197 for (int k = 0; k < stateSet.length; k++) {
198 if (stateSet[k]) {
199 observedStates.add(new Integer(k));
200 }
201 }
202 }
203
204 stateSets[i] = new int[observedStates.size()];
205
206 Iterator iter = observedStates.iterator();
207 int j = 0;
208 while (iter.hasNext()) {
209 stateSets[i][j] = ((Integer)iter.next()).intValue();
210 j++;
211 }
212 }
213
214 for (int j = 0; j < tree.getExternalNodeCount(); j++) {
215 NodeRef node = tree.getExternalNode(j);
216 int state = pattern[patterns.getTaxonIndex(tree.getNodeTaxon(node).getId())];
217 boolean[] stateSet = patterns.getDataType().getStateSet(state);
218
219 nodeScores[j][i] = new double[stateCount];
220 for (int k = 0; k < stateCount; k++) {
221 if (stateSet[k]) {
222 nodeScores[j][i][k] = 0.0;
223 } else {
224 nodeScores[j][i][k] = Double.POSITIVE_INFINITY;
225 }
226 }
227 }
228
229 for (int j = 0; j < tree.getInternalNodeCount(); j++) {
230 nodeScores[j + tree.getExternalNodeCount()][i] = new double[stateCount];
231 }
232
233 }
234 }
235
236 /**
237 * This is the first pass of the Fitch algorithm. This calculates the set of nodeStates
238 * at each node and counts the total number of siteScores (the score). If that is all that
239 * is required then the second pass is not necessary.
240 * @param tree
241 * @param node
242 * @param patterns
243 */
244 private void calculateSteps(Tree tree, NodeRef node, PatternList patterns) {
245
246 if (!tree.isExternal(node)) {
247
248 for (int i = 0; i < tree.getChildCount(node); i++) {
249 calculateSteps(tree, tree.getChild(node, i), patterns);
250 }
251
252 if (compressStates) {
253 for (int i = 0; i < patterns.getPatternCount(); i++) {
254 double[] Sc = nodeScores[tree.getChild(node, 0).getNumber()][i];
255 double[] Sa = nodeScores[node.getNumber()][i];
256
257 int[] set = stateSets[i];
258 for (int k = 0; k < set.length; k++) {
259 Sa[set[k]] = minCost(k, Sc, costMatrix, set);
260 }
261
262 for (int j = 1; j < tree.getChildCount(node); j++) {
263 Sc = nodeScores[tree.getChild(node, j).getNumber()][i];
264 for (int k = 0; k < set.length; k++) {
265 Sa[set[k]] += minCost(k, Sc, costMatrix, set);
266 }
267
268 }
269
270
271 }
272 } else {
273 for (int i = 0; i < patterns.getPatternCount(); i++) {
274 double[] Sc = nodeScores[tree.getChild(node, 0).getNumber()][i];
275 double[] Sa = nodeScores[node.getNumber()][i];
276
277 for (int k = 0; k < stateCount; k++) {
278 Sa[k] = minCost(k, Sc, costMatrix);
279 }
280
281 for (int j = 1; j < tree.getChildCount(node); j++) {
282 Sc = nodeScores[tree.getChild(node, j).getNumber()][i];
283 for (int k = 0; k < stateCount; k++) {
284 Sa[k] += minCost(k, Sc, costMatrix);
285 }
286
287 }
288
289
290 }
291
292 }
293 }
294 }
295
296 /**
297 * The second pass of the algorithm. This reconstructs the ancestral nodeStates at
298 * each node.
299 * @param tree
300 * @param node
301 * @param parentStates
302 */
303 private void reconstructStates(Tree tree, NodeRef node, int[] parentStates) {
304
305 for (int i = 0; i < patterns.getPatternCount(); i++) {
306
307 double[] Sa = nodeScores[node.getNumber()][i];
308
309 if (compressStates) {
310 int[] set = stateSets[i];
311
312 int minState = set[0];
313 double minCost = Sa[minState] + costMatrix[parentStates[i]][minState];
314
315 for (int j = 1; j < set.length; j++) {
316 double c = Sa[set[j]] + costMatrix[parentStates[i]][set[j]];
317 if (c < minCost) {
318 minState = set[j];
319 minCost = c;
320 }
321 }
322 nodeStates[node.getNumber()][i] = minState;
323 } else {
324 int minState = 0;
325 double minCost = Sa[minState] + costMatrix[parentStates[i]][minState];
326
327 for (int j = 1; j < Sa.length; j++) {
328 double c = Sa[j] + costMatrix[parentStates[i]][j];
329 if (c < minCost) {
330 minState = j;
331 minCost = c;
332 }
333 }
334 nodeStates[node.getNumber()][i] = minState;
335 }
336
337 }
338
339 for (int i = 0; i < tree.getChildCount(node); i++) {
340 reconstructStates(tree, tree.getChild(node, i), nodeStates[node.getNumber()]);
341 }
342
343 }
344
345
346 private int minState(double[] s1) {
347
348 int minState = 0;
349
350 for (int j = 1; j < s1.length; j++) {
351 if (s1[j] < s1[minState]) minState = j;
352 }
353 return minState;
354 }
355
356 private double minScore(double[] s1) {
357
358 double minScore = s1[0];
359
360 for (int j = 1; j < s1.length; j++) {
361 if (s1[j] < minScore) minScore = s1[j];
362 }
363 return minScore;
364 }
365
366 private double minCost(int i, double[] s1, double[][] costMatrix) {
367
368 double[] costRow = costMatrix[i];
369 double minCost = costRow[0] + s1[0];
370
371 for (int j = 1; j < s1.length; j++) {
372 double cost = costRow[j] + s1[j];
373 if (cost < minCost) minCost = cost;
374 }
375 return minCost;
376 }
377
378 private int minState(double[] s1, int[] set) {
379
380 int minState = set[0];
381
382 for (int j = 1; j < set.length; j++) {
383 if (s1[set[j]] < s1[minState]) minState = set[j];
384 }
385 return minState;
386 }
387
388 private double minScore(double[] s1, int[] set) {
389
390 double minScore = s1[set[0]];
391
392 for (int j = 1; j < set.length; j++) {
393 if (s1[set[j]] < minScore) minScore = s1[set[j]];
394 }
395 return minScore;
396 }
397
398 private double minCost(int i, double[] s1, double[][] costMatrix, int[] set) {
399
400 double[] costRow = costMatrix[set[i]];
401 double minCost = costRow[set[0]] + s1[set[0]];
402
403 for (int j = 1; j < set.length; j++) {
404 double cost = costRow[set[j]] + s1[set[j]];
405 if (cost < minCost) minCost = cost;
406 }
407 return minCost;
408 }
409
410 public static void main(String[] argv) {
411 FlexibleNode tip1 = new FlexibleNode(new Taxon("tip1"));
412 FlexibleNode tip2 = new FlexibleNode(new Taxon("tip2"));
413 FlexibleNode tip3 = new FlexibleNode(new Taxon("tip3"));
414 FlexibleNode tip4 = new FlexibleNode(new Taxon("tip4"));
415 FlexibleNode tip5 = new FlexibleNode(new Taxon("tip5"));
416
417 FlexibleNode node1 = new FlexibleNode();
418 node1.addChild(tip1);
419 node1.addChild(tip2);
420
421 FlexibleNode node2 = new FlexibleNode();
422 node2.addChild(tip4);
423 node2.addChild(tip5);
424
425 FlexibleNode node3 = new FlexibleNode();
426 node3.addChild(tip3);
427 node3.addChild(node2);
428
429 FlexibleNode root = new FlexibleNode();
430 root.addChild(node1);
431 root.addChild(node3);
432
433 FlexibleTree tree = new FlexibleTree(root);
434
435 Patterns patterns = new Patterns(Nucleotides.INSTANCE, tree);
436 //patterns.addPattern(new int[] {1, 0, 1, 2, 2});
437 //patterns.addPattern(new int[] {2, 1, 1, 1, 2});
438 patterns.addPattern(new int[] {2, 3, 1, 3, 3});
439
440 FitchParsimony fitch = new FitchParsimony(patterns, false);
441 SankoffParsimony sankoff = new SankoffParsimony(patterns);
442
443 for (int i = 0; i < patterns.getPatternCount(); i++) {
444 double[] scores = fitch.getSiteScores(tree);
445 System.out.println("Pattern = " + i);
446
447 System.out.println("Fitch:");
448 System.out.println(" No. Steps = " + scores[i]);
449 System.out.println(" state(node1) = " + fitch.getStates(tree, node1)[i]);
450 System.out.println(" state(node2) = " + fitch.getStates(tree, node2)[i]);
451 System.out.println(" state(node3) = " + fitch.getStates(tree, node3)[i]);
452 System.out.println(" state(root) = " + fitch.getStates(tree, root)[i]);
453
454 scores = sankoff.getSiteScores(tree);
455 System.out.println("Sankoff:");
456 System.out.println(" No. Steps = " + scores[i]);
457 System.out.println(" state(node1) = " + sankoff.getStates(tree, node1)[i]);
458 System.out.println(" state(node2) = " + sankoff.getStates(tree, node2)[i]);
459 System.out.println(" state(node3) = " + sankoff.getStates(tree, node3)[i]);
460 System.out.println(" state(root) = " + sankoff.getStates(tree, root)[i]);
461
462 System.out.println();
463 }
464 }
465
466 }