| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class Parsimony { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 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 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 267 |
boolean[] uState = getParsimonyState(tree, tree.getChild(node, 0), patterns, patternIndex, attributeName, score); |
| 268 |
|
| 269 |
int n = uState.length; |
| 270 |
|
| 271 |
|
| 272 |
boolean[] iState = new boolean[n]; |
| 273 |
for (int j = 0; j < n; j++) { |
| 274 |
iState[j] = uState[j]; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 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 |
|
| 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 |
|
| 310 |
if (iCard == 0) { |
| 311 |
return uState; |
| 312 |
} else { |
| 313 |
return iState; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
} |
| 318 |
} |