Debian Codegraph @

src/dr/evolution/parsimony/Parsimony.java

raw symbols SCIP
1 /*
2 * Parsimony.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.tree.MutableTree;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32
33 /**
34 * Utility class for doing parsimony reconstruction of patterns on a tree.
35 *
36 * @version $Id: Parsimony.java,v 1.7 2005/06/27 21:18:40 rambaut Exp $
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 */
41 public class Parsimony {
42
43 /**
44 * Calculates the minimum number of steps for the parsimony reconstruction of a
45 * a set of character patterns on a tree. This only does the first pass of the
46 * Fitch algorithm so it does not store ancestral state reconstructions. This makes
47 * it faster than the reconstructParsimonyStates method.
48 * @param tree a tree object to reconstruct the characters on
49 * @param patterns a PatternList object with any discrete datatype
50 * @return number of parsimony steps
51 */
52 public static int getParsimonySteps(Tree tree, PatternList patterns) {
53
54 int[] score = new int[] { 0 };
55
56 for (int i = 0; i < patterns.getPatternCount(); i++) {
57
58 String name = "state" + String.valueOf(i+1);
59 getParsimonyState(tree, tree.getRoot(), patterns, i, name, score);
60 }
61
62 return score[0];
63 }
64
65 /**
66 * Calculates the internal states using parsimony.
67 * @param tree a tree object to reconstruct the characters on
68 * @param patterns a PatternList object with any discrete datatype
69 */
70 public static void reconstructParsimonyStates(Tree tree, PatternList patterns) {
71
72 for (int i = 0; i < patterns.getPatternCount(); i++) {
73
74 String name = "state" + String.valueOf(i+1);
75 //int[] score = new int[] {0};
76 int[] root = getParsimonyState2(tree, tree.getRoot(), patterns, i, name, patterns.getDataType().getStateCount());
77
78
79 int bestIndex = 0;
80 int bestScore = root[0];
81 for (int j = 1; j < root.length; j++) {
82 if (root[j] < bestScore) {
83 bestScore = root[j];
84 bestIndex = i;
85 }
86 }
87
88
89 //System.out.println("Pattern " + i + " steps = " + score[0]);
90
91 reconstructParsimonyState2(tree, tree.getRoot(), name, "r" + name, bestIndex);
92 }
93
94 }
95
96 private static void reconstructParsimonyState2(Tree tree, NodeRef node, String setName, String attributeName, int state) {
97
98 if (tree.isRoot(node)) {
99
100 int[] scores = (int[])tree.getNodeAttribute(node, setName);
101
102 int bestIndex = 0;
103 int bestScore = scores[0];
104 for (int i = 1; i < scores.length; i++) {
105 if (scores[i] < bestScore) {
106 bestScore = scores[i];
107 bestIndex = i;
108 }
109 }
110
111 state = bestIndex;
112 }
113
114 int childCount = tree.getChildCount(node);
115
116 if (childCount > 0) {
117
118 int bestScore = 0;
119
120 // the child scores
121 int[][] childScores = new int[childCount][];
122
123 for (int i = 0; i < childCount; i++) {
124 childScores[i] = (int[])tree.getNodeAttribute(tree.getChild(node,i), setName);
125 }
126
127 int[] childIndices = new int[childCount];
128 int[] bestIndices = new int[childCount];
129 int stateCount = childScores[0].length;
130 int childrenCombinations = (int)Math.round(Math.pow(stateCount, childCount));
131 for (int i = 0; i < childrenCombinations; i++) {
132
133 int c = i;
134 for (int j = 0; j < childCount; j+=1) {
135 childIndices[j] = c % stateCount;
136 c /= stateCount;
137 }
138
139 int score = 0;
140 for (int j = 0; j < childCount; j+=1) {
141 score += childScores[j][childIndices[j]] + penalty(state, childIndices[j]);
142 }
143
144 if (i == 0) {
145 bestScore = score;
146 } else if (score < bestScore) {
147 bestScore = score;
148 for (int j = 0; j <childCount; j++) {
149 bestIndices[j] = childIndices[j];
150 }
151 }
152 }
153
154 for (int i = 0; i < childCount; i++) {
155 reconstructParsimonyState2(tree, tree.getChild(node, i), setName, attributeName, bestIndices[i]);
156 }
157 }
158
159 ((MutableTree)tree).setNodeAttribute(node, attributeName, new Integer(state));
160 }
161
162
163
164 /**
165 * Reconstructs the state at a given node using parsimony. If the node is external
166 * then the actual state from patterns will be given.
167 * @param tree a Tree object to reconstruct the characters on
168 * @param node a NodeRef specifying the node
169 * @param patterns a PatternList object with any discrete datatype
170 * @return a boolean set of possible states
171 */
172 private static int[] getParsimonyState2(Tree tree, NodeRef node, PatternList patterns,
173 int patternIndex, String attributeName, int stateCount) {
174
175 if (tree.isExternal(node)) {
176 int state = patterns.getPatternState(patterns.getTaxonIndex(tree.getNodeTaxon(node).getId()), patternIndex);
177 int[] scores = new int[stateCount];
178 for (int i = 0; i < stateCount; i++) {
179 scores[i] = Integer.MAX_VALUE/20;
180 }
181 if (state < stateCount) {
182 scores[state] = 0;
183 } else {
184 scores[stateCount-1] = 0;
185 }
186 ((MutableTree)tree).setNodeAttribute(node, attributeName, scores);
187
188 return scores;
189
190 } else {
191
192 int childCount = tree.getChildCount(node);
193
194 // the child scores
195 int[][] childScores = new int[childCount][];
196
197 for (int i = 0; i < childCount; i++) {
198 childScores[i] = getParsimonyState2(tree, tree.getChild(node, i), patterns, patternIndex, attributeName, stateCount);
199 }
200
201 int[] scores = new int[stateCount];
202
203 for (int parent = 0; parent < stateCount; parent++) {
204
205 int[] childIndices = new int[childCount];
206 int childrenCombinations = (int)Math.round(Math.pow(stateCount, childCount));
207 for (int i = 0; i < childrenCombinations; i++) {
208
209 int c = i;
210 for (int j = 0; j < childCount; j+=1) {
211 childIndices[j] = c % stateCount;
212 c /= stateCount;
213 }
214
215 int score = 0;
216 for (int j = 0; j < childCount; j+=1) {
217 score += childScores[j][childIndices[j]] + penalty(parent, childIndices[j]);
218 }
219
220 if (i == 0) {
221 scores[parent] = score;
222 } else if (score < scores[parent]) {
223 scores[parent] = score;
224 }
225 }
226 }
227
228 ((MutableTree)tree).setNodeAttribute(node, attributeName, scores);
229 //((MutableTree)tree).setNodeAttribute(node, attributeName +".children", bestChildren);
230
231
232 //for (int i = 0; i < scores.length; i++) {
233 // System.out.print(scores[i] + "\t");
234 //}
235 //System.out.println();
236
237 return scores;
238 }
239 }
240
241 private static final int penalty(int state1, int state2) {
242 if (state1 == state2) return 0;
243 return 1;
244 }
245
246 /**
247 * Reconstructs the state at a given node using parsimony. If the node is external
248 * then the actual state from patterns will be given.
249 * @param tree a Tree object to reconstruct the characters on
250 * @param node a NodeRef specifying the node
251 * @param patterns a PatternList object with any discrete datatype
252 * @param score the current score passed by reference
253 * @return a boolean set of possible states
254 */
255 private static boolean[] getParsimonyState(Tree tree, NodeRef node, PatternList patterns,
256 int patternIndex, String attributeName, int[] score) {
257
258 if (tree.isExternal(node)) {
259 int state = patterns.getPatternState(patterns.getTaxonIndex(tree.getNodeTaxon(node).getId()), patternIndex);
260
261 ((MutableTree)tree).setNodeAttribute(node, "r"+attributeName, new Integer(state));
262 return patterns.getDataType().getStateSet(state);
263
264 } else {
265
266 // the union state set
267 boolean[] uState = getParsimonyState(tree, tree.getChild(node, 0), patterns, patternIndex, attributeName, score);
268
269 int n = uState.length;
270
271 // the intersection state set
272 boolean[] iState = new boolean[n];
273 for (int j = 0; j < n; j++) {
274 iState[j] = uState[j];
275 }
276
277 // the cardinality of the intersection set
278 int iCard = 0;
279
280 for (int i = 1; i < tree.getChildCount(node); i++) {
281
282 boolean[] state = getParsimonyState(tree, tree.getChild(node, i), patterns,
283 patternIndex, attributeName, score);
284
285
286 for (int j = 0; j < uState.length; j++) {
287 uState[j] = state[j] || uState[j];
288 iState[j] = state[j] && iState[j];
289 if (iState[j]) iCard += 1;
290 }
291
292 }
293
294 if (score != null && iCard == 0) {
295 score[0] += 1;
296
297 }
298
299 if (attributeName != null) {
300 // Should take the intersection if non-empty AD 24/3/2004
301 if (iCard == 0) {
302 ((MutableTree)tree).setNodeAttribute(node, attributeName, uState);
303
304 } else {
305 ((MutableTree)tree).setNodeAttribute(node, attributeName, iState);
306 }
307 }
308
309 // Should take the intersection if non-empty AD 24/3/2004
310 if (iCard == 0) {
311 return uState;
312 } else {
313 return iState;
314 }
315 }
316
317 }
318 }