Skip to content

Commit e689ab8

Browse files
Merge pull request #61 from xiangpin/as.phylo_as.treedata
As.phylo as.treedata
2 parents 81e9dc8 + 95fc011 commit e689ab8

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

R/method-as-phylo.R

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ as.phylo.tbl_df <- function(x, length, ...) {
2020
}
2121

2222
edge <- check_edgelist(x)
23+
indx <- attr(edge, "indx")
24+
if (!is.null(indx) && !is.null(edge.length)){
25+
edge.length <- edge.length[indx]
26+
attr(edge, "indx") <- NULL
27+
}
2328
phylo <- read.tree(text = .write.tree4(edge,
2429
id_as_label=TRUE,
2530
edge.length = edge.length),

R/method-as-treedata.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ as.treedata.ggtree <- function(tree, ...) {
5757
##' @export
5858
as.treedata.tbl_df <- function(tree, ...) {
5959
edgelist <- as_tibble(tree)
60-
60+
edge <- check_edgelist(edgelist)
61+
indx <- attr(edge, "indx")
62+
if (!is.null(indx)){
63+
edgelist <- edgelist[indx,]
64+
attr(edge, "indx") <- NULL
65+
}
6166
phylo <- as.phylo.tbl_df(edgelist, ...)
6267

6368
res <- new("treedata",
@@ -74,7 +79,7 @@ as.treedata.tbl_df <- function(tree, ...) {
7479

7580
lab <- c(phylo$tip.label, phylo$node.label)
7681

77-
edge <- check_edgelist(edgelist)
82+
#edge <- check_edgelist(edgelist)
7883
children <- edge[,2]
7984

8085
d$node <- match(children, lab)

R/utilities.R

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ check_edgelist <- function(edgelist) {
99
children <- edgelist[[2]]
1010
parents <- edgelist[[1]]
1111
}
12-
root <- unique(parents[!(parents %in% children)])
13-
if (length(root) != 1)
12+
root1 <- unique(parents[!(parents %in% children)])
13+
root2 <- unique(parents[parents == children])
14+
if (length(root1) != 1 && length(root2) != 1)
1415
stop("Cannot find root. network is not a tree!")
15-
16-
matrix(c(parents, children), ncol=2)
16+
if (length(root1) != 1 && length(root2) == 1){
17+
indx <- parents != children
18+
parents <- parents[indx]
19+
children <- children[indx]
20+
edge <- matrix(c(parents, children), ncol=2)
21+
attr(edge, "indx") <- indx
22+
}else{
23+
edge <- matrix(c(parents, children), ncol=2)
24+
}
25+
return (edge)
1726
}
1827

1928

0 commit comments

Comments
 (0)