R/ggenrichTree.R

Defines functions ggenrichTree

Documented in ggenrichTree

#'ggenrichTree function
#'@export
#'@description
#'ggenrichTree function is used to draw the similarity of terms in the result of enrichment analysis.
#'@param tree input tree
#'@param heat input file of the heatmap
#'@param node node number of the term branch
#'@param pathway name of pathway
#'@param offset offset of category name, heatmap and treemap. default:.7
#'@importFrom utils read.table
#'@return a data.frame.
#'@examples
#node1 <- c(41,39,46,40)
#pathways1 <- c("pathway1","pathway2","pathway3","pathway4")
#input <- ggenrichData(system.file("extdata", "easy_input.csv", package = "ggenrich",mustWork = TRUE),format="DAVID",type="cluster")
#tree1 <- ggenrichTree_pre(input,type="DAVID",species="human")
#heat1 <- ggenrichData(system.file("extdata", "easy_input.csv", package = "ggenrich",mustWork = TRUE),format="DAVID",type="groups_cluster")
#plot1 <- ggenrichTree(tree1,heat1,node1,pathways1)

#node2 <- c(22,21,27,25)
#pathways2 <- c("pathway1","pathway2","pathway3","pathway4")
#inputfile2 <- ggenrichData(system.file("extdata", c("enrichGO1.csv","enrichGO2.csv","enrichGO3.csv"), package = "ggenrich",mustWork = TRUE),format="R_ClusterProfiler",type="cluster",GENE_MAX=400,GENE_MIN=200)
#tree2 <- ggenrichTree_pre(inputfile2,type="ClusterProfiler",species="mouse")
#heat2 <- ggenrichData(system.file("extdata", c("enrichGO1.csv","enrichGO2.csv","enrichGO3.csv"), package = "ggenrich",mustWork = TRUE),format="R_ClusterProfiler",type="groups_cluster",GENE_MAX=400,GENE_MIN=200)
#plot2 <- ggenrichTree(tree2,heat2,node2,pathways2)







ggenrichTree <- function(tree,heat,node,pathway,offset=.7){
  if (requireNamespace("ggtree", quietly = TRUE)) {
    gtree <- ggtree::groupClade(tree, .node=node)
    pbase <- ggtree::ggtree(gtree,
                            ggtree::aes(color=group))
    fontsize <- 4
    if (requireNamespace("ggplot2", quietly = TRUE)) {
      if (requireNamespace("scales", quietly = TRUE)) {
        pnode <- pbase +ggtree::geom_tiplab(size=4, align=TRUE)
        for (i in 1:length(node)){
          pnode <- pnode + ggtree::geom_cladelabel(node=node[i],
                                                   align=TRUE,
                                                   color = scales::hue_pal()(length(node)+1)[i+1],
                                                   fontsize = fontsize,
                                                   offset=offset+.1,
                                                   label=pathway[i])
        }+
        ggplot2::coord_cartesian(xlim=c(-.1,1.5))
      }
    plot <- ggtree::gheatmap(pnode,
                     heat,
                     offset=offset,
                     width=0.12,
                     colnames_angle=90,
                     hjust=0,
                     low = "red",
                     high = "white")
    }
    plot(plot)
    ggplot2::ggsave("DAVIDkappa.pdf", width = 16, height = 9)
  }
}
ying-ge/ggEnrich documentation built on Nov. 24, 2019, 12:34 p.m.