R/drawTaxonomicTree.R

#' buildTree - a taxonomic tree display function
#'
#' Returns taxonomic tree of the taxids up to phylum.
#'
#' @param taxids vector of leave nodes
#' @param standardisedRanks the ranks to report. superkingdom phylum class order family genus species
#' @importFrom magrittr "%>%"
#' @importFrom dplyr mutate
#' @importFrom igraph graph_from_data_frame
#' @examples
#' /dontrun{
#' 
#' }
#' @export

buildTree <- function(
        taxids = c(287, 280),
        standardisedRanks  = c("superkingdom", "phylum", "class", "order","family", "genus", "species")
    ){
    edgelists = taxids %>% lapply(function(x) path2kingdom(as.character(x)))
    rankInfo = data.frame(standard = standardisedRanks) %>% mutate(order=1:n())
    trueEdgelist = edgelists       %>%
        lapply(function(x){
           merge(rankInfo, x, all.x=T, by.y="rank", by.x="standard") %>%
               arrange(order) %>%
           .$taxid %>%
           as.character        %>%
           as.integer          %>%
           buildE})            %>%
        do.call(rbind,.) %>% as.data.frame %>% unique %>% as.matrix
    nodeDetails = as.vector(trueEdgelist) %>% purrr::map_df(taxnam.sql) %>% dplyr::arrange(taxid) %>% unique
    list(edgelist = trueEdgelist, nodeDetails = nodeDetails, graph=graph_from_data_frame(trueEdgelist, v=nodeDetails))
}



#' buildE sorts the tree into a data.frame
#'
#' @param path vector of integers representing taxid to common ancestor
#' @param ma matrix rbind with
#' @keywords internal
buildE <- function(path, ma){


    if(missing(ma)) ma = matrix(ncol=2, nrow=0)

    if(length(path) > 2){
        mm = matrix(path[1:2], ncol=2)
        mm = rbind(ma, mm)
        buildE(path[2:length(path)], mm)
    }else{
        mm = matrix(path[1:2], ncol=2)
        mm = rbind(ma, mm)
        mm
    }
}
etheleon/metamaps documentation built on May 16, 2019, 9:05 a.m.