public static void newick(Tree tree,NodeRef node,boolean labels,BranchLengthType lengths,NumberFormat format,BranchRates branchRates,TreeTraitProvider[] treeTraitProviders,Map<String,Integer> idMap,StringBuffer buffer){
  NodeRef parent=tree.getParent(node);
  if (tree.isExternal(node)) {
    if (!labels) {
      int k=node.getNumber();
      if (idMap != null) {
        buffer.append(idMap.get(tree.getTaxonId(k)));
      }
 else {
        buffer.append((k + 1));
      }
    }
 else {
      String label=tree.getTaxonId(node.getNumber());
      if (label.contains(" ") || label.contains(":") || label.contains(";")|| label.contains(",")) {
        buffer.append("\"");
        buffer.append(label);
        buffer.append("\"");
      }
 else {
        buffer.append(label);
      }
    }
  }
 else {
    buffer.append("(");
    newick(tree,tree.getChild(node,0),labels,lengths,format,branchRates,treeTraitProviders,idMap,buffer);
    for (int i=1; i < tree.getChildCount(node); i++) {
      buffer.append(",");
      newick(tree,tree.getChild(node,i),labels,lengths,format,branchRates,treeTraitProviders,idMap,buffer);
    }
    buffer.append(")");
  }
  writeTreeTraits(buffer,tree,node,treeTraitProviders,TreeTrait.Intent.NODE);
  if (parent != null && lengths != BranchLengthType.NO_BRANCH_LENGTHS) {
    buffer.append(":");
    writeTreeTraits(buffer,tree,node,treeTraitProviders,TreeTrait.Intent.BRANCH);
    if (lengths != BranchLengthType.NO_BRANCH_LENGTHS) {
      double length=tree.getNodeHeight(parent) - tree.getNodeHeight(node);
      if (lengths == BranchLengthType.LENGTHS_AS_SUBSTITUTIONS) {
        if (branchRates == null) {
          throw new IllegalArgumentException("No BranchRates provided");
        }
        length*=branchRates.getBranchRate(tree,node);
      }
      String lengthString;
      if (format != null) {
        lengthString=format.format(length);
      }
 else {
        lengthString=String.valueOf(length);
      }
      buffer.append(lengthString);
    }
  }
}

public static void newick ( Tree tree , NodeRef node , boolean labels , BranchLengthType lengths , NumberFormat format , BranchRates branchRates , TreeTraitProvider [ ] treeTraitProviders , Map < String , Integer > idMap , StringBuffer buffer ) { NodeRef parent = tree . getParent ( node ) ; if ( tree . isExternal ( node ) ) { if ( ! labels ) { int k = node . getNumber ( ) ; if ( idMap != null ) { buffer . append ( idMap . get ( tree . getTaxonId ( k ) ) ) ; } else { buffer . append ( ( k + 1 ) ) ; } } else { String label = tree . getTaxonId ( node . getNumber ( ) ) ; if ( label . contains ( " " ) || label . contains ( ":" ) || label . contains ( ";" ) || label . contains ( "," ) ) { buffer . append ( "\"" ) ; buffer . append ( label ) ; buffer . append ( "\"" ) ; } else { buffer . append ( label ) ; } } } else { buffer . append ( "(" ) ; newick ( tree , tree . getChild ( node , 0 ) , labels , lengths , format , branchRates , treeTraitProviders , idMap , buffer ) ; for ( int i = 1 ; i < tree . getChildCount ( node ) ; i ++ ) { buffer . append ( "," ) ; newick ( tree , tree . getChild ( node , i ) , labels , lengths , format , branchRates , treeTraitProviders , idMap , buffer ) ; } buffer . append ( ")" ) ; } writeTreeTraits ( buffer , tree , node , treeTraitProviders , TreeTrait . Intent . NODE ) ; if ( parent != null && lengths != BranchLengthType . NO_BRANCH_LENGTHS ) { buffer . append ( ":" ) ; writeTreeTraits ( buffer , tree , node , treeTraitProviders , TreeTrait . Intent . BRANCH ) ; if ( lengths != BranchLengthType . NO_BRANCH_LENGTHS ) { double length = tree . getNodeHeight ( parent ) - tree . getNodeHeight ( node ) ; if ( lengths == BranchLengthType . LENGTHS_AS_SUBSTITUTIONS ) { if ( branchRates == null ) { throw new IllegalArgumentException ( "No BranchRates provided" ) ; } length *= branchRates . getBranchRate ( tree , node ) ; } String lengthString ; if ( format != null ) { lengthString = format . format ( length ) ; } else { lengthString = String . valueOf ( length ) ; } buffer . append ( lengthString ) ; } } }
recursive function for constructing a newick tree representation in the given buffer .
