private void reconstructStates(Tree tree,NodeRef node,int[] parentStates){
  for (int i=0; i < patterns.getPatternCount(); i++) {
    double[] Sa=nodeScores[node.getNumber()][i];
    if (compressStates) {
      int[] set=stateSets[i];
      int minState=set[0];
      double minCost=Sa[minState] + costMatrix[parentStates[i]][minState];
      for (int j=1; j < set.length; j++) {
        double c=Sa[set[j]] + costMatrix[parentStates[i]][set[j]];
        if (c < minCost) {
          minState=set[j];
          minCost=c;
        }
      }
      nodeStates[node.getNumber()][i]=minState;
    }
 else {
      int minState=0;
      double minCost=Sa[minState] + costMatrix[parentStates[i]][minState];
      for (int j=1; j < Sa.length; j++) {
        double c=Sa[j] + costMatrix[parentStates[i]][j];
        if (c < minCost) {
          minState=j;
          minCost=c;
        }
      }
      nodeStates[node.getNumber()][i]=minState;
    }
  }
  for (int i=0; i < tree.getChildCount(node); i++) {
    reconstructStates(tree,tree.getChild(node,i),nodeStates[node.getNumber()]);
  }
}

private void reconstructStates ( Tree tree , NodeRef node , int [ ] parentStates ) { for ( int i = 0 ; i < patterns . getPatternCount ( ) ; i ++ ) { double [ ] Sa = nodeScores [ node . getNumber ( ) ] [ i ] ; if ( compressStates ) { int [ ] set = stateSets [ i ] ; int minState = set [ 0 ] ; double minCost = Sa [ minState ] + costMatrix [ parentStates [ i ] ] [ minState ] ; for ( int j = 1 ; j < set . length ; j ++ ) { double c = Sa [ set [ j ] ] + costMatrix [ parentStates [ i ] ] [ set [ j ] ] ; if ( c < minCost ) { minState = set [ j ] ; minCost = c ; } } nodeStates [ node . getNumber ( ) ] [ i ] = minState ; } else { int minState = 0 ; double minCost = Sa [ minState ] + costMatrix [ parentStates [ i ] ] [ minState ] ; for ( int j = 1 ; j < Sa . length ; j ++ ) { double c = Sa [ j ] + costMatrix [ parentStates [ i ] ] [ j ] ; if ( c < minCost ) { minState = j ; minCost = c ; } } nodeStates [ node . getNumber ( ) ] [ i ] = minState ; } } for ( int i = 0 ; i < tree . getChildCount ( node ) ; i ++ ) { reconstructStates ( tree , tree . getChild ( node , i ) , nodeStates [ node . getNumber ( ) ] ) ; } }
the second pass of the algorithm . this reconstructs the ancestral nodestates at each node .
