Skip to content

Commit a521ee6

Browse files
author
guangchuang yu
committed
as.data.frame & fortify method for treedata
0 parents  commit a521ee6

19 files changed

+1199
-0
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
Makefile

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.Rproj.user
2+
.Rhistory
3+
.RData
4+
*~

DESCRIPTION

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Package: tidytree
2+
Title: What the Package Does (one line, title case)
3+
Version: 0.0.1
4+
Authors@R: person("First", "Last", email = "[email protected]", role = c("aut", "cre"))
5+
Description: What the package does (one paragraph).
6+
Depends:
7+
R (>= 3.3.2)
8+
Imports:
9+
ape,
10+
ggplot2,
11+
magrittr,
12+
methods,
13+
treeio
14+
Suggests:
15+
knitr,
16+
prettydoc
17+
VignetteBuilder: knitr
18+
ByteCompile: true
19+
License: Artistic-2.0
20+
URL: https://guangchuangyu.github.io/tidytree
21+
BugReports: https://github.com/GuangchuangYu/tidytree/issues
22+
Encoding: UTF-8
23+
LazyData: true
24+
RoxygenNote: 5.0.1

Makefile

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
PKGNAME := $(shell sed -n "s/Package: *\([^ ]*\)/\1/p" DESCRIPTION)
2+
PKGVERS := $(shell sed -n "s/Version: *\([^ ]*\)/\1/p" DESCRIPTION)
3+
PKGSRC := $(shell basename `pwd`)
4+
5+
all: rd check clean
6+
7+
rd:
8+
Rscript -e 'roxygen2::roxygenise(".")'
9+
10+
build:
11+
cd ..;\
12+
R CMD build $(PKGSRC)
13+
14+
build2:
15+
cd ..;\
16+
R CMD build --no-build-vignettes $(PKGSRC)
17+
18+
install:
19+
cd ..;\
20+
R CMD INSTALL $(PKGNAME)_$(PKGVERS).tar.gz
21+
22+
check: build
23+
cd ..;\
24+
Rscript -e 'rcmdcheck::rcmdcheck("$(PKGNAME)_$(PKGVERS).tar.gz")'
25+
26+
check2: build
27+
cd ..;\
28+
R CMD check $(PKGNAME)_$(PKGVERS).tar.gz
29+
30+
bioccheck:
31+
cd ..;\
32+
Rscript -e 'BiocCheck::BiocCheck("$(PKGNAME)_$(PKGVERS).tar.gz")'
33+
34+
clean:
35+
cd ..;\
36+
$(RM) -r $(PKGNAME).Rcheck/
37+

NAMESPACE

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
S3method(as.data.frame,phylo)
4+
S3method(as.data.frame,treedata)
5+
S3method(fortify,phylo)
6+
S3method(fortify,treedata)
7+
export("%>%")
8+
export(get.offspring.tip)
9+
export(get.path)
10+
export(getNodeNum)
11+
export(getRoot)
12+
export(is.ggtree)
13+
export(nodeid)
14+
importFrom(ape,extract.clade)
15+
importFrom(ape,reorder.phylo)
16+
importFrom(ggplot2,fortify)
17+
importFrom(magrittr,"%>%")
18+
importFrom(magrittr,equals)
19+
importFrom(methods,"slot<-")
20+
importFrom(methods,.hasSlot)
21+
importFrom(methods,is)
22+
importFrom(methods,missingArg)
23+
importFrom(methods,new)
24+
importFrom(methods,slot)
25+
importFrom(treeio,Nnode)
26+
importFrom(treeio,Ntip)
27+
importFrom(treeio,get.tree)

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CHANGES IN VERSION 0.0.1
2+
------------------------
3+
o as.data.frame & fortify method for treedata object <2016-12-06, Tue>

R/as-data-frame.R

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
##' @method as.data.frame treedata
2+
##' @export
3+
##' @importFrom treeio Nnode
4+
##' @importFrom treeio Ntip
5+
as.data.frame.treedata <- function(x, row.names, optional, branch.length = "branch.length", ...) {
6+
tree <- set_branch_length(x, branch.length)
7+
8+
res <- as.data.frame(tree@phylo)
9+
tree_anno <- get_tree_data(x)
10+
if (nrow(tree_anno) > 0) {
11+
res <- merge(res, tree_anno, by="node", all.x=TRUE)
12+
}
13+
return(res)
14+
}
15+
16+
##' @method as.data.frame phylo
17+
##' @export
18+
as.data.frame.phylo <- function(x, row.names, optional, branch.length = "branch.length", ...) {
19+
phylo <- x
20+
ntip <- Ntip(phylo)
21+
N <- Nnode(phylo, internal.only=FALSE)
22+
23+
tip.label <- phylo[["tip.label"]]
24+
res <- as.data.frame(phylo[["edge"]])
25+
colnames(res) <- c("parent", "node")
26+
if (!is.null(phylo$edge.length))
27+
res$branch.length <- phylo$edge.length
28+
29+
label <- rep(NA, N)
30+
label[1:ntip] <- tip.label
31+
if ( !is.null(phylo$node.label) ) {
32+
label[(ntip+1):N] <- phylo$node.label
33+
}
34+
label.df <- data.frame(node=1:N, label=label)
35+
res <- merge(res, label.df, by='node', all.y=TRUE)
36+
isTip <- rep(FALSE, N)
37+
isTip[1:ntip] <- TRUE
38+
res$isTip <- isTip
39+
40+
return(res)
41+
}
42+

R/fortify.R

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
##' @importFrom ggplot2 fortify
2+
##' @method fortify phylo
3+
##' @export
4+
fortify.phylo <- function(model, data, layout="rectangular", branch.length ="branch.length",
5+
ladderize=TRUE, right=FALSE, mrsd=NULL, ...) {
6+
model <- set_branch_length(model, branch.length)
7+
x <- reorder.phylo(get.tree(model), "postorder")
8+
if (is.null(x$edge.length) || branch.length == "none") {
9+
xpos <- getXcoord_no_length(x)
10+
} else {
11+
xpos <- getXcoord(x)
12+
}
13+
ypos <- getYcoord(x)
14+
N <- Nnode(x, internal.only=FALSE)
15+
xypos <- data.frame(node=1:N, x=xpos, y=ypos)
16+
17+
df <- as.data.frame(model, branch.length="branch.length") # already set by set_branch_length
18+
idx <- is.na(df$parent)
19+
df$parent[idx] <- df$node[idx]
20+
rownames(df) <- df$node
21+
22+
res <- merge(df, xypos, by='node', all.y=TRUE)
23+
24+
## add branch mid position
25+
res <- calculate_branch_mid(res)
26+
27+
## ## angle for all layout, if 'rectangular', user use coord_polar, can still use angle
28+
res <- calculate_angle(res)
29+
res
30+
}
31+
32+
##' @method fortify treedata
33+
##' @export
34+
fortify.treedata <- fortify.phylo
35+
36+
37+
calculate_angle <- function(data) {
38+
data$angle <- 360/(diff(range(data$y)) + 1) * data$y
39+
return(data)
40+
}

R/reexports.R

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
##' @importFrom magrittr %>%
2+
##' @export
3+
magrittr::`%>%`
4+

0 commit comments

Comments
 (0)