R/convertRAxMLedgeLabels.R

##' Converts a tree created by RAxML so that the bootstrap values are
##' assigned to nodes instead of edges.
##'
##' By default, (and rightly so), RAxML returns the bootstrap values
##' on an unrooted tree assigned to the edges. Most often, users want
##' the bootstrap values assigned to nodes instead. This function uses
##' a little regular expression to assign the boostrap values to the
##' nodes. It might not always work, use with caution.
##' @title Convert RAxML edge labels bootstrap values to node labels
##' @param f the tree file generated by RAxML
##' @param out the tree file generated by this function
##' @return TRUE is the function succeed. Used for this side effect
##' that creates a new tree where the bootstrap values are assigned to
##' nodes.
##' @author Francois Michonneau
##' @export
convertRAxMLedgeLabels <- function(f, out) {
    eachTr <- scan(f, what="character", sep="\n")
    stopifnot(!file.exists(out))
    for (i in 1:length(eachTr)) {
        tmpStr <- eachTr[i]
        tmpStr <-  gsub("\\):([0-9]+\\.[0-9]+)\\[([0-9]{1,3})\\]", "\\)\\2:\\1", tmpStr)
        cat(tmpStr, file = out, append = TRUE)
    }
    TRUE
}
fmichonneau/chopper documentation built on May 16, 2019, 1:43 p.m.