Debian Codegraph @

src/dr/evolution/parsimony/FitchParsimony.java

raw symbols SCIP
1 /*
2 * FitchParsimony.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 /**
35 * Class for reconstructing characters using Fitch parsimony. This is intended to be much faster
36 * than the static methods in the utility "Parsimony" class.
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 * @version $Id: FitchParsimony.java,v 1.4 2005/06/22 16:44:18 rambaut Exp $
41 */
42 public class FitchParsimony implements ParsimonyCriterion {
43
44 private final int stateCount;
45 private final boolean gapsAreStates;
46
47 private boolean[][][] stateSets;
48 private int[][] states;
49
50 private Tree tree = null;
51 private final PatternList patterns;
52
53 private boolean hasCalculatedSteps = false;
54 private boolean hasRecontructedStates = false;
55
56 private final double[] siteScores;
57
58 public PatternList getPatterns() {
59 return patterns;
60 }
61
62 public FitchParsimony(PatternList patterns, boolean gapsAreStates) {
63 if (patterns == null) {
64 throw new IllegalArgumentException("The patterns cannot be null");
65 }
66
67 this.gapsAreStates = gapsAreStates;
68
69 if (gapsAreStates) {
70 stateCount = patterns.getDataType().getStateCount() + 1;
71 } else {
72 stateCount = patterns.getDataType().getStateCount();
73
74 }
75
76 this.patterns = patterns;
77 this.siteScores = new double[patterns.getPatternCount()];
78 }
79
80 /**
81 * Calculates the minimum number of siteScores for the parsimony reconstruction of a
82 * a set of character patterns on a tree. This only does the first pass of the
83 * Fitch algorithm so it does not store ancestral state reconstructions.
84 *
85 * @param tree a tree object to reconstruct the characters on
86 * @return number of parsimony siteScores
87 */
88 public double[] getSiteScores(Tree tree) {
89
90 if (tree == null) {
91 throw new IllegalArgumentException("The tree cannot be null");
92 }
93
94 if (this.tree == null || this.tree != tree) {
95
96 initialize(tree);
97 }
98
99 if (!hasCalculatedSteps) {
100 for (int i = 0; i < siteScores.length; i++) {
101 siteScores[i] = 0;
102 }
103 calculateSteps(tree, tree.getRoot(), patterns);
104 hasCalculatedSteps = true;
105 }
106
107
108 return siteScores;
109 }
110
111 public double getScore(Tree tree) {
112
113 getSiteScores(tree);
114
115 double score = 0;
116
117 for (int i = 0; i < patterns.getPatternCount(); i++) {
118 score += siteScores[i] * patterns.getPatternWeight(i);
119 }
120 return score;
121 }
122
123 /**
124 * Returns the reconstructed character states for a given node in the tree. If this method is repeatedly
125 * called with the same tree and patterns then only the first call will reconstruct the states and each
126 * subsequent call will return the stored states.
127 *
128 * @param tree a tree object to reconstruct the characters on
129 * @param node the node of the tree
130 * @return an array containing the reconstructed states for this node
131 */
132 public int[] getStates(Tree tree, NodeRef node) {
133
134 if (!TreeUtils.isBinary(tree)) {
135 throw new IllegalArgumentException("The Fitch algorithm can only reconstruct ancestral states on binary trees");
136 }
137
138 getSiteScores(tree);
139
140 if (!hasRecontructedStates) {
141 reconstructStates(tree, tree.getRoot(), null);
142 hasRecontructedStates = true;
143 }
144
145 return states[node.getNumber()];
146 }
147
148 public void initialize(Tree tree) {
149
150 this.tree = tree;
151
152 hasCalculatedSteps = false;
153 hasRecontructedStates = false;
154
155 stateSets = new boolean[tree.getNodeCount()][patterns.getPatternCount()][];
156 states = new int[tree.getNodeCount()][patterns.getPatternCount()];
157
158 for (int i = 0; i < patterns.getPatternCount(); i++) {
159 int[] pattern = patterns.getPattern(i);
160 for (int j = 0; j < tree.getExternalNodeCount(); j++) {
161 NodeRef node = tree.getExternalNode(j);
162 int state = pattern[patterns.getTaxonIndex(tree.getNodeTaxon(node).getId())];
163 if (gapsAreStates) {
164 stateSets[j][i] = new boolean[stateCount];
165 if (patterns.getDataType().isGapState(state)) {
166 stateSets[j][i][stateCount - 1] = true;
167 } else {
168 boolean[] stateSet = patterns.getDataType().getStateSet(state);
169 for (int k = 0; k < stateSet.length; k++) {
170 stateSets[j][i][k] = stateSet[k];
171 }
172 }
173 } else {
174 stateSets[j][i] = patterns.getDataType().getStateSet(state);
175 }
176 }
177 }
178 }
179
180 /**
181 * This is the first pass of the Fitch algorithm. This calculates the set of states
182 * at each node and counts the total number of siteScores (the score). If that is all that
183 * is required then the second pass is not necessary.
184 *
185 * @param tree
186 * @param node
187 * @param patterns
188 */
189 private void calculateSteps(Tree tree, NodeRef node, PatternList patterns) {
190
191 if (!tree.isExternal(node)) {
192
193 for (int i = 0; i < tree.getChildCount(node); i++) {
194 calculateSteps(tree, tree.getChild(node, i), patterns);
195 }
196
197 for (int i = 0; i < patterns.getPatternCount(); i++) {
198 boolean[] uState = stateSets[tree.getChild(node, 0).getNumber()][i];
199 boolean[] iState = stateSets[tree.getChild(node, 0).getNumber()][i];
200
201 for (int j = 1; j < tree.getChildCount(node); j++) {
202 uState = union(uState, stateSets[tree.getChild(node, j).getNumber()][i]);
203 iState = intersection(iState, stateSets[tree.getChild(node, j).getNumber()][i]);
204 }
205
206 if (size(iState) > 0) {
207 stateSets[node.getNumber()][i] = iState;
208 } else {
209 stateSets[node.getNumber()][i] = uState;
210 siteScores[i]++;
211 }
212 }
213 }
214 }
215
216 /**
217 * The second pass of the Fitch algorithm. This reconstructs the ancestral states at
218 * each node.
219 *
220 * @param tree
221 * @param node
222 * @param parentStates
223 */
224 private void reconstructStates(Tree tree, NodeRef node, int[] parentStates) {
225
226 for (int i = 0; i < patterns.getPatternCount(); i++) {
227
228 if (parentStates != null && stateSets[node.getNumber()][i][parentStates[i]]) {
229 states[node.getNumber()][i] = parentStates[i];
230 } else {
231 states[node.getNumber()][i] = firstIndex(stateSets[node.getNumber()][i]);
232 }
233 }
234
235 for (int i = 0; i < tree.getChildCount(node); i++) {
236 reconstructStates(tree, tree.getChild(node, i), states[node.getNumber()]);
237 }
238 }
239
240 private static boolean[] union(boolean[] s1, boolean[] s2) {
241
242 boolean[] union = new boolean[s1.length];
243 for (int i = 0; i < union.length; i++) {
244 union[i] = s1[i] || s2[i];
245 }
246 return union;
247 }
248
249 private static boolean[] intersection(boolean[] s1, boolean[] s2) {
250
251 boolean[] intersection = new boolean[s1.length];
252 for (int i = 0; i < intersection.length; i++) {
253 intersection[i] = s1[i] && s2[i];
254 }
255 return intersection;
256 }
257
258 private static int firstIndex(boolean[] s1) {
259
260 for (int i = 0; i < s1.length; i++) {
261 if (s1[i]) {
262 return i;
263 }
264 }
265 return -1;
266 }
267
268 private static int size(boolean[] s1) {
269
270 int count = 0;
271 for (int i = 0; i < s1.length; i++) {
272 if (s1[i]) count += 1;
273 }
274 return count;
275 }
276
277 public static void main(String[] argv) {
278 FlexibleNode tip1 = new FlexibleNode(new Taxon("tip1"));
279 FlexibleNode tip2 = new FlexibleNode(new Taxon("tip2"));
280 FlexibleNode tip3 = new FlexibleNode(new Taxon("tip3"));
281 FlexibleNode tip4 = new FlexibleNode(new Taxon("tip4"));
282 FlexibleNode tip5 = new FlexibleNode(new Taxon("tip5"));
283
284 FlexibleNode node1 = new FlexibleNode();
285 node1.addChild(tip1);
286 node1.addChild(tip2);
287
288 FlexibleNode node2 = new FlexibleNode();
289 node2.addChild(tip4);
290 node2.addChild(tip5);
291
292 FlexibleNode node3 = new FlexibleNode();
293 node3.addChild(tip3);
294 node3.addChild(node2);
295
296 FlexibleNode root = new FlexibleNode();
297 root.addChild(node1);
298 root.addChild(node3);
299
300 FlexibleTree tree = new FlexibleTree(root);
301
302 Patterns patterns = new Patterns(Nucleotides.INSTANCE, tree);
303 patterns.addPattern(new int[]{1, 0, 1, 2, 2});
304
305 FitchParsimony fitch = new FitchParsimony(patterns, false);
306
307 System.out.println("No. Steps = " + fitch.getScore(tree));
308 System.out.println(" state(node1) = " + fitch.getStates(tree, node1)[0]);
309 System.out.println(" state(node2) = " + fitch.getStates(tree, node2)[0]);
310 System.out.println(" state(node3) = " + fitch.getStates(tree, node3)[0]);
311 System.out.println(" state(root) = " + fitch.getStates(tree, root)[0]);
312
313
314 System.out.println("\nParsimony static methods:");
315 System.out.println("No. Steps = " + Parsimony.getParsimonySteps(tree, patterns));
316 Parsimony.reconstructParsimonyStates(tree, patterns);
317 System.out.println(" state(node1) = " + tree.getNodeAttribute(node1, "rstate1"));
318 System.out.println(" state(node2) = " + tree.getNodeAttribute(node2, "rstate1"));
319 System.out.println(" state(node3) = " + tree.getNodeAttribute(node3, "rstate1"));
320 System.out.println(" state(root) = " + tree.getNodeAttribute(root, "rstate1"));
321 }
322 }