public double[] getSummaryStatistic(Tree tree) {
    if (taxonList == null) {
      return new double[] {1.0};
    }

    double TL = Tree.Utils.getTreeLength(tree, tree.getRoot());

    double betaDiversity = (TL - getUniqueBranches(tree, tree.getRoot())) / TL;

    return new double[] {betaDiversity};
  }
  /**
   * Calculates the probability of a given tree.
   *
   * @param tree - the tree to be analyzed
   * @return estimated posterior probability in log
   */
  public double getTreeProbability(Tree tree, HashMap<String, Integer> taxonMap) {
    double prob = 0.0;

    List<Clade> clades = new ArrayList<Clade>();
    List<Clade> parentClades = new ArrayList<Clade>();
    // get clades contained in the tree
    getNonComplementaryClades(tree, tree.getRoot(), parentClades, clades, taxonMap);

    int size = clades.size();
    // for every clade multiply its conditional clade probability to the
    // tree probability
    for (int i = 0; i < size; i++) {
      Clade c = clades.get(i);

      // get the bits of the clade
      Clade parent = parentClades.get(i);

      // set the occurrences to epsilon
      double tmp = EPSILON;
      double parentOccurrences = 0.0;
      BitSet parentBits = parent.getBits();
      if (cladeProbabilities.containsKey(parentBits)) {
        // if we observed this clade in the trace, add the
        // occurrences
        // to epsilon
        parentOccurrences += cladeProbabilities.get(parentBits).getSampleCount();
      }

      if (cladeCoProbabilities.containsKey(parentBits)) {
        // if we observed the parent clade
        HashMap<BitSet, Clade> conditionalProbs = cladeCoProbabilities.get(parentBits);

        BitSet bits = c.getBits();
        if (conditionalProbs.containsKey(bits)) {
          // if we observed this conditional clade in the trace,
          // add
          // the occurrences to epsilon
          tmp += conditionalProbs.get(bits).getSampleCount();
        }
      }
      // add epsilon for each clade
      final double splits = Math.pow(2, parent.getSize() - 1) - 1;
      parentOccurrences += EPSILON * splits;

      // multiply the conditional clade probability to the tree
      // probability
      prob += Math.log(tmp / parentOccurrences);
    }

    return prob;
  }
Example #3
0
  public void writeNexusTree(Tree tree, String s, boolean attributes, Map<String, Integer> idMap) {
    // PAUP marks rooted trees thou
    String treeAttributes = "[&R] ";

    // Place tree level attributes in tree comment
    StringBuilder treeComment = null;
    {
      Iterator<String> iter = tree.getAttributeNames();
      if (iter != null) {
        while (iter.hasNext()) {
          final String name = iter.next();
          final String value = tree.getAttribute(name).toString();

          if (name.equals("weight")) {
            treeAttributes = treeAttributes + "[&W " + value + " ] ";
          } else {
            if (treeComment == null) {
              treeComment = new StringBuilder(" [&");
            } else if (treeComment.length() > 2) {
              treeComment.append(", ");
            }

            treeComment.append(name).append("=").append(value);
          }
        }
        if (treeComment != null) {
          treeComment.append("]");
        }
      }
    }

    out.print(
        "tree "
            + s
            + ((treeComment != null) ? treeComment.toString() : "")
            + " = "
            + treeAttributes);

    writeNode(tree, tree.getRoot(), attributes, idMap);
    out.println(";");
  }
 @Override
 public int getHeight() {
   return getHeight(tree.getRoot().getNumber());
 }
 @Override
 public int getRoot() {
   return tree.getRoot().getNumber();
 }
  /**
   * increments the number of occurrences for all conditional clades
   *
   * @param tree - the tree to be added
   */
  public void addTree(Tree tree, HashMap<String, Integer> taxonMap) {

    samples++;

    List<Clade> clades = new ArrayList<Clade>();
    List<Clade> parentClades = new ArrayList<Clade>();
    // get clades contained in the tree
    getClades(tree, tree.getRoot(), parentClades, clades, taxonMap);
    // add the clade containing all taxa as well so that it get counted
    clades.add(parentClades.get(parentClades.size() - 1));
    parentClades.add(clades.get(clades.size() - 1));

    int size = clades.size();
    // for every clade multiply its conditional clade probability to the
    // tree probability
    for (int i = 0; i < size; i++) {
      Clade c = clades.get(i);

      // get the bits of the clade
      Clade parent = parentClades.get(i);

      HashMap<BitSet, Clade> coFreqs;
      // increment the clade occurrences
      if (cladeProbabilities.containsKey(c.getBits())) {
        Clade tmp = cladeProbabilities.get(c.getBits());
        // tmp.addSample();
        tmp.addHeight(c.getHeight());
        // add the amount to the current occurences
        // frequency += cladeProbabilities.get(c);
      } else {
        // just to set the first value of the height value list
        // c.addSample();
        c.addHeight(c.getHeight());
        cladeProbabilities.put(c.getBits(), c);
      }

      // increment the conditional clade occurrences
      if (!parent.equals(c)) {
        if (cladeCoProbabilities.containsKey(parent.getBits())) {
          coFreqs = cladeCoProbabilities.get(parent.getBits());
        } else {
          // if it's the first time we observe the parent then we need
          // a new list for its conditional clades
          coFreqs = new HashMap<BitSet, Clade>();
          cladeCoProbabilities.put(parent.getBits(), coFreqs);
        }

        // add the previous observed occurrences for this conditional
        // clade
        if (coFreqs.containsKey(c.getBits())) {
          Clade tmp = coFreqs.get(c.getBits());
          tmp.addHeight(c.getHeight());
          // coFrequency += coFreqs.get(c.getBits());
        } else {
          // TODO check this code, especially if the cloning is needed
          // and not just the clade could be added
          Clade tmp = new Clade((BitSet) c.getBits().clone(), c.getHeight());
          tmp.addHeight(c.getHeight());
          coFreqs.put(c.getBits(), tmp);
        }
      }
    }
  }
Example #7
0
 /**
  * Obtain an ordering of tree tips from randomly swaping the children order in internal nodes.
  *
  * @param tree tree to create order from
  * @param order Nodes in their random order (only odd indices are filled)
  * @param wasSwapped true if internal node was swapped
  */
 private static void mauCanonical(Tree tree, NodeRef[] order, boolean[] wasSwapped) {
   mauCanonicalSub(tree, tree.getRoot(), 0, order, wasSwapped);
 }