R/create_tree.R

Defines functions create_sub_tree create_tree

#' @export
create_tree <- function(edge_df) {
  if(nrow(edge_df) == 0) stop("no data")
  root_label <- edge_df[1,1]
  root <- create_sub_tree(root_label, edge_df)
  Tree$new(root)
}

create_sub_tree <- function(root_label, df) {
  root <- Node$new(root_label)
  sibling_labels <- df[df[,1]==root_label, ][,2]
  if(length(sibling_labels) == 0) return(root)
  for(sib_label in sibling_labels) {
    sib <- create_sub_tree(sib_label, df)
    root$add_child(sib)
  }
  root
}
hoxo-m/mergetree documentation built on May 16, 2017, 12:56 a.m.