Skip to content

Instantly share code, notes, and snippets.

@mjbommar
Created September 13, 2009 19:05
Show Gist options
  • Save mjbommar/186287 to your computer and use it in GitHub Desktop.
Save mjbommar/186287 to your computer and use it in GitHub Desktop.
dend2newick <- function(d) {
leftLeaf <- attributes(d[[1]])$leaf
rightLeaf <- attributes(d[[2]])$leaf
if(length(leftLeaf) > 0) {
leftStr <- sprintf("%s:%f", attributes(d[[1]])$label, attributes(d)$height);
} else {
leftStr <- dend2newick(d[[1]])
}
if(length(rightLeaf) > 0) {
rightStr <- sprintf("%s:%f", attributes(d[[2]])$label, attributes(d)$height);
} else {
rightStr <- dend2newick(d[[2]])
}
return(sprintf("(%s,%s)", leftStr, rightStr))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment