Пример #1
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);
      }
    }
  }
Пример #2
0
  public Map<String, Integer> writeNexusHeader(Tree tree) {
    int taxonCount = tree.getTaxonCount();
    List<String> names = new ArrayList<String>();

    for (int i = 0; i < tree.getTaxonCount(); i++) {
      names.add(tree.getTaxonId(i));
    }

    if (sorted) Collections.sort(names);

    out.println("#NEXUS");
    out.println();
    out.println("Begin taxa;");
    out.println("\tDimensions ntax=" + taxonCount + ";");
    out.println("\tTaxlabels");
    for (String name : names) {
      if (name.matches(SPECIAL_CHARACTERS_REGEX)) {
        name = "'" + name + "'";
      }
      out.println("\t\t" + name);
    }
    out.println("\t\t;");
    out.println("End;");
    out.println("");
    out.println("Begin trees;");

    // This is needed if the trees use numerical taxon labels
    out.println("\tTranslate");
    Map<String, Integer> idMap = new HashMap<String, Integer>();

    int k = 1;
    for (String name : names) {
      idMap.put(name, k);
      if (name.matches(SPECIAL_CHARACTERS_REGEX)) {
        name = "'" + name + "'";
      }
      if (k < names.size()) {
        out.println("\t\t" + k + " " + name + ",");
      } else {
        out.println("\t\t" + k + " " + name);
      }
      k += 1;
    }
    return idMap;
  }