Browse code

viewClade function

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@112497 bc3139a8-67e5-0310-9ffc-ced21a209358

g.yu authored on 13/01/2016 02:57:51
Showing 7 changed files

... ...
@@ -10,6 +10,7 @@ S3method(fortify,hyphy)
10 10
 S3method(fortify,jplace)
11 11
 S3method(fortify,multiPhylo)
12 12
 S3method(fortify,nhx)
13
+S3method(fortify,obkData)
13 14
 S3method(fortify,paml_rst)
14 15
 S3method(fortify,phangorn)
15 16
 S3method(fortify,phylo)
... ...
@@ -98,6 +99,7 @@ export(theme_inset)
98 99
 export(theme_transparent)
99 100
 export(theme_tree)
100 101
 export(theme_tree2)
102
+export(viewClade)
101 103
 export(write.jplace)
102 104
 exportClasses(apeBootstrap)
103 105
 exportClasses(beast)
... ...
@@ -1,5 +1,7 @@
1 1
 CHANGES IN VERSION 1.3.8
2 2
 ------------------------
3
+ o add viewClade function <2016-01-12, Tue>
4
+ o support obkData object defined by OutbreakTools <2016-01-12, Tue>
3 5
  o update vignettes <2016-01-07, Thu>
4 6
  o 05 advance tree annotation vignette <2016-01-04, Mon>
5 7
  o export theme_inset <2016-01-04, Mon>
... ...
@@ -611,7 +611,7 @@ fortify.multiPhylo <-  function(model, data, layout="rectangular",
611 611
     df$.id <- factor(df$.id, levels=names(df.list))
612 612
     
613 613
     nNode <- sapply(df.list, nrow)
614
-    nNode2 <- cumsum(nNode) - nNode[1]
614
+    nNode2 <- cumsum(c(0, nNode[-length(nNode)])) 
615 615
     df$parent <- df$parent + rep(nNode2, times=nNode)
616 616
     return(df)
617 617
 }
... ...
@@ -626,3 +626,38 @@ fortify.r8s <- function(model, data, layout="rectangular",
626 626
     phylo <- trees[[branch.length]]
627 627
     fortify(phylo, layout=layout, ladderize = ladderize, right=right, mrsd=mrsd, ...)
628 628
 }
629
+
630
+##' @method fortify obkData
631
+##' @export
632
+fortify.obkData <- function(model, data, layout="rectangular",
633
+                            ladderize=TRUE, right=FALSE, mrsd = NULL, ...) {
634
+
635
+    df <- fortify(model@trees[[1]], layout=layout, ladderize=ladderize, right=right, mrsd=mrsd, ...)
636
+
637
+    meta.df <- model@dna@meta
638
+    meta.df <- data.frame(taxa=rownames(meta.df), meta.df)
639
+    loc <- model@individuals
640
+    loc <- data.frame(individualID=rownames(loc), loc)
641
+    meta_loc <- merge(meta.df, loc, by="individualID")
642
+    meta_loc <- meta_loc[,-1]
643
+
644
+    df <- merge(df, meta_loc, by.x="label", by.y="taxa", all.x=TRUE)
645
+    df <- df[order(df$node, decreasing = FALSE),]
646
+    return(df)
647
+}
648
+                            
649
+                         
650
+## fortify.cophylo <- function(model, data, layout="rectangular",
651
+##                             ladderize=TRUE, right=FALSE, mrsd = NULL, ...) {
652
+##     trees <- model$trees
653
+##     df.list <- lapply(trees, function(x) fortify(x, layout=layout, ladderize=ladderize, right=right, mrsd=mrsd, ...))
654
+##     df1 <- df.list[[1]]
655
+##     df2 <- df.list[[2]]
656
+
657
+##     df2$x <- max(df2$x) + df2$x * -1 + max(df1$x) * 1.1
658
+##     df2$parent <- df2$parent+nrow(df1)
659
+##     df <- rbind(df1, df2)
660
+
661
+##     ggplot(df) + geom_tree()
662
+
663
+## }
629 664
new file mode 100644
... ...
@@ -0,0 +1,13 @@
1
+##' view a clade of tree
2
+##'
3
+##' 
4
+##' @title viewClade
5
+##' @param tree_view full tree view 
6
+##' @param node internal node number
7
+##' @return clade plot
8
+##' @export
9
+##' @author Guangchuang Yu
10
+viewClade <- function(tree_view, node) {
11
+    cpos <- get_clade_position(tree_view, node=node)
12
+    with(cpos, p+xlim(xmin, xmax*1.01)+ylim(ymin, ymax))
13
+}
0 14
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+% Generated by roxygen2: do not edit by hand
2
+% Please edit documentation in R/viewClade.R
3
+\name{viewClade}
4
+\alias{viewClade}
5
+\title{viewClade}
6
+\usage{
7
+viewClade(tree_view, node)
8
+}
9
+\arguments{
10
+\item{tree_view}{full tree view}
11
+
12
+\item{node}{internal node number}
13
+}
14
+\value{
15
+clade plot
16
+}
17
+\description{
18
+view a clade of tree
19
+}
20
+\author{
21
+Guangchuang Yu
22
+}
23
+
... ...
@@ -83,7 +83,7 @@ A specific slice of the alignment can also be displayed by specific _window_ par
83 83
 msaplot(ggtree(beast_tree), fasta, window=c(150, 200)) 
84 84
 ```
85 85
 
86
-# Annotate a phylogenetic with insets
86
+# Annotate a phylogenetic tree with insets
87 87
 
88 88
 `ggtree` implemented a function, `subview`, that can add subplots on a ggplot2 object. It had successful applied to [plot pie graphs on map](http://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot/32380396#32380396).
89 89
 
... ...
@@ -92,10 +92,9 @@ set.seed(2016-01-04)
92 92
 tr <- rtree(30)
93 93
 tr <- groupClade(tr, node=45)
94 94
 p <- ggtree(tr, aes(color=group)) + geom_tippoint()
95
-cpos <- get_clade_position(p, node=45)
96 95
 p1 <- p + geom_hilight(node=45)
97
-p2 <- with(cpos, p+xlim(xmin, xmax*1.01)+ylim(ymin, ymax))
98
-with(cpos, subview(p2+geom_tiplab(), p1+theme_transparent(), x=xmin+(xmax-xmin)*.15, y=ymin+(ymax-ymin)*.85))
96
+p2 <- viewClade(p, node=45) + geom_tiplab()
97
+subview(p2, p1+theme_transparent(), x=2.3, y=28.5)
99 98
 ```
100 99
 
101 100
 To make it more easy to use subview function for annotating taxa with subplots, *ggtree* provides a function, `inset`, for adding subplots to a phylogenetic tree. The input is a tree graphic object and a named list of ggplot graphic objects (can be any kind of charts), these objects should named by node numbers. To facilitate adding bar and pie charts (e.g. summarized stats of results from ancestral reconstruction) to phylogenetic tree, *ggtree* provides `nodepie` and `nodebar` functions to create a list of pie or bar charts.
... ...
@@ -38,7 +38,7 @@ The `ggtree` package should not be viewed solely as a standalone software. While
38 38
 # Supported File Formats
39 39
 
40 40
 Most of the tree viewer software (including `R` packages) focus on `Newick` and `Nexus` file format, while there are file formats from different evolution analysis software that contain supporting evidences within the file that are ready for annotating a phylogenetic tree.
41
-The `ggtree` package define several parser functions and `S4` classes to store statistical evidences inferred by commonly used software packages. It supports several file format, including:
41
+The `ggtree` package define several parser functions and `S4` classes to store statistical evidences inferred by commonly used software packages. It supports several file formats, including:
42 42
 
43 43
 + Newick (via `ape`)
44 44
 + Nexus (via `ape`)