/**
   * @param tree
   * @param node
   * @return and array of the total amount of time spent in each of the discrete states along the
   *     branch above the given node.
   */
  private double[] getProcessValues(final Tree tree, final NodeRef node) {

    double[] processValues = null;
    double branchTime = tree.getBranchLength(node);

    if (mode == Mode.MARKOV_JUMP_PROCESS) {
      processValues = (double[]) trait.getTrait(tree, node);
    } else if (mode == Mode.PARSIMONY) {
      // an approximation to dwell times using parsimony, assuming
      // the state changes midpoint on the tree. Does a weighted
      // average of the equally parsimonious state reconstructions
      // at the top and bottom of each branch.

      if (treeChanged) {
        fitchParsimony.initialize(tree);
        // Debugging test to count work
        //                treeInitializeCounter += 1;
        //                if (treeInitializeCounter % 10 == 0) {
        //                    System.err.println("Cnt: "+treeInitializeCounter);
        //                }
        treeChanged = false;
      }
      int[] states = fitchParsimony.getStates(tree, node);
      int[] parentStates = fitchParsimony.getStates(tree, tree.getParent(node));

      processValues = new double[fitchParsimony.getPatterns().getStateCount()];

      for (int state : states) {
        processValues[state] += branchTime / 2;
      }
      for (int state : parentStates) {
        processValues[state] += branchTime / 2;
      }

      for (int i = 0; i < processValues.length; i++) {
        // normalize by the number of equally parsimonious states at each end of the branch
        // processValues should add up to the total branch length
        processValues[i] /= (states.length + parentStates.length) / 2;
      }
    } else if (mode == Mode.NODE_STATES) {
      processValues = new double[dataType.getStateCount()];
      //            if (indicatorParameter != null) {
      //                // this array should be size #states NOT #rates
      //                processValues = new double[indicatorParameter.getDimension()];
      //            } else {
      //                // this array should be size #states NOT #rates
      //                processValues = new double[rateParameter.getDimension()];
      //            }

      // if the states are being sampled - then there is only one possible state at each
      // end of the branch.
      int state = ((int[]) trait.getTrait(tree, node))[traitIndex];
      processValues[state] += branchTime / 2;
      int parentState = ((int[]) trait.getTrait(tree, tree.getParent(node)))[traitIndex];
      processValues[parentState] += branchTime / 2;
    }

    return processValues;
  }
  private double getUniqueBranches(Tree tree, NodeRef node) {

    if (tree.isExternal(node)) {
      return tree.getBranchLength(node);
    } else {
      double length = 0;
      if (isUnique(taxonList, tree, node)) {
        length = tree.getBranchLength(node);
        // System.out.println("length = " + length);
      }
      for (int i = 0; i < tree.getChildCount(node); i++) {
        length += getUniqueBranches(tree, tree.getChild(node, i));
      }
      // System.out.println("length of node " + node + " = " + length);
      return length;
    }
  }
Example #3
0
  public double[] getSummaryStatistic(Tree tree) {

    double externalLength = 0.0;
    double internalLength = 0.0;

    int externalNodeCount = tree.getExternalNodeCount();
    for (int i = 0; i < externalNodeCount; i++) {
      NodeRef node = tree.getExternalNode(i);
      externalLength += tree.getBranchLength(node);
    }

    int internalNodeCount = tree.getInternalNodeCount();
    for (int i = 0; i < internalNodeCount; i++) {
      NodeRef node = tree.getInternalNode(i);
      if (!tree.isRoot(node)) {
        internalLength += tree.getBranchLength(node);
      }
    }
    return new double[] {internalLength + externalLength};
  }
Example #4
0
  private void writeNode(Tree tree, NodeRef node, boolean attributes, Map<String, Integer> idMap) {
    if (tree.isExternal(node)) {
      int k = node.getNumber() + 1;
      if (idMap != null) k = idMap.get(tree.getTaxonId(k - 1));

      out.print(k);
    } else {
      out.print("(");
      writeNode(tree, tree.getChild(node, 0), attributes, idMap);
      for (int i = 1; i < tree.getChildCount(node); i++) {
        out.print(",");
        writeNode(tree, tree.getChild(node, i), attributes, idMap);
      }
      out.print(")");
    }

    if (writeAttributesAs == AttributeType.BRANCH_ATTRIBUTES && !tree.isRoot(node)) {
      out.print(":");
    }

    if (attributes) {
      Iterator<?> iter = tree.getNodeAttributeNames(node);
      if (iter != null) {
        boolean first = true;
        while (iter.hasNext()) {
          if (first) {
            out.print("[&");
            first = false;
          } else {
            out.print(",");
          }
          String name = (String) iter.next();
          out.print(name + "=");
          Object value = tree.getNodeAttribute(node, name);
          printValue(value);
        }
        out.print("]");
      }
    }

    if (writeAttributesAs == AttributeType.NODE_ATTRIBUTES && !tree.isRoot(node)) {
      out.print(":");
    }

    if (!tree.isRoot(node)) {
      double length = tree.getBranchLength(node);
      if (formatter != null) {
        out.print(formatter.format(length));
      } else {
        out.print(length);
      }
    }
  }
  double calculateNorm(Tree tree) {

    double time = 0.0;
    double rateTime = 0.0;
    for (int i = 0; i < tree.getNodeCount(); i++) {

      NodeRef node = tree.getNode(i);

      if (!tree.isRoot(node)) {

        double branchTime = tree.getBranchLength(node);

        rateTime += getRawBranchRate(tree, node) * branchTime;
        time += branchTime;
      }
    }
    return rateTime / time;
  }