... | ... |
@@ -8,9 +8,7 @@ S3method(fortify,beast) |
8 | 8 |
S3method(fortify,codeml) |
9 | 9 |
S3method(fortify,codeml_mlc) |
10 | 10 |
S3method(fortify,hyphy) |
11 |
-S3method(fortify,jplace) |
|
12 | 11 |
S3method(fortify,multiPhylo) |
13 |
-S3method(fortify,nhx) |
|
14 | 12 |
S3method(fortify,obkData) |
15 | 13 |
S3method(fortify,paml_rst) |
16 | 14 |
S3method(fortify,phangorn) |
... | ... |
@@ -73,8 +71,6 @@ export(ggtree) |
73 | 71 |
export(gheatmap) |
74 | 72 |
export(gzoom) |
75 | 73 |
export(inset) |
76 |
-export(mask) |
|
77 |
-export(merge_tree) |
|
78 | 74 |
export(msaplot) |
79 | 75 |
export(multiplot) |
80 | 76 |
export(nodebar) |
... | ... |
@@ -82,9 +78,6 @@ export(nodeid) |
82 | 78 |
export(nodepie) |
83 | 79 |
export(open_tree) |
84 | 80 |
export(phylopic) |
85 |
-export(plot) |
|
86 |
-export(raxml2nwk) |
|
87 |
-export(read.phyloT) |
|
88 | 81 |
export(reroot) |
89 | 82 |
export(rescale_tree) |
90 | 83 |
export(revts) |
... | ... |
@@ -105,7 +98,6 @@ export(xlim_tree) |
105 | 98 |
exportMethods(groupClade) |
106 | 99 |
exportMethods(groupOTU) |
107 | 100 |
exportMethods(gzoom) |
108 |
-exportMethods(plot) |
|
109 | 101 |
exportMethods(reroot) |
110 | 102 |
importFrom(ape,di2multi) |
111 | 103 |
importFrom(ape,extract.clade) |
... | ... |
@@ -183,7 +175,8 @@ importFrom(magrittr,"%<>%") |
183 | 175 |
importFrom(magrittr,"%>%") |
184 | 176 |
importFrom(magrittr,add) |
185 | 177 |
importFrom(magrittr,equals) |
186 |
-importFrom(stats4,plot) |
|
178 |
+importFrom(methods,is) |
|
179 |
+importFrom(methods,missingArg) |
|
187 | 180 |
importFrom(tidyr,gather) |
188 | 181 |
importFrom(treeio,as.treedata) |
189 | 182 |
importFrom(treeio,groupClade) |
... | ... |
@@ -7,20 +7,20 @@ as.binary <- function(tree, ...) { |
7 | 7 |
UseMethod("as.binary") |
8 | 8 |
} |
9 | 9 |
|
10 |
-##' plot method generics |
|
11 |
-##' |
|
12 |
-##' |
|
13 |
-##' @docType methods |
|
14 |
-##' @name plot |
|
15 |
-##' @rdname plot-methods |
|
16 |
-##' @title plot method |
|
17 |
-##' @param x object |
|
18 |
-##' @param ... Additional argument list |
|
19 |
-##' @return plot |
|
20 |
-##' @importFrom stats4 plot |
|
21 |
-##' @export |
|
22 |
-if ( !isGeneric("plot") ) |
|
23 |
- setGeneric("plot", function(x, ...) standardGeneric("plot")) |
|
10 |
+## ##' plot method generics |
|
11 |
+## ##' |
|
12 |
+## ##' |
|
13 |
+## ##' @docType methods |
|
14 |
+## ##' @name plot |
|
15 |
+## ##' @rdname plot-methods |
|
16 |
+## ##' @title plot method |
|
17 |
+## ##' @param x object |
|
18 |
+## ##' @param ... Additional argument list |
|
19 |
+## ##' @return plot |
|
20 |
+## ##' @importFrom stats4 plot |
|
21 |
+## ##' @export |
|
22 |
+## if ( !isGeneric("plot") ) |
|
23 |
+## setGeneric("plot", function(x, ...) standardGeneric("plot")) |
|
24 | 24 |
|
25 | 25 |
##' @docType methods |
26 | 26 |
##' @name reroot |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
##' add insets in a tree |
2 | 2 |
##' |
3 |
-##' |
|
3 |
+##' |
|
4 | 4 |
##' @title inset |
5 | 5 |
##' @param tree_view tree view |
6 | 6 |
##' @param insets a list of ggplot objects, named by node number |
... | ... |
@@ -22,7 +22,7 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x= |
22 | 22 |
xx <- df$branch |
23 | 23 |
} |
24 | 24 |
yy <- df$y |
25 |
- |
|
25 |
+ |
|
26 | 26 |
xx <- xx - hjust |
27 | 27 |
yy <- yy - vjust |
28 | 28 |
|
... | ... |
@@ -38,7 +38,7 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x= |
38 | 38 |
|
39 | 39 |
##' generate a list of bar charts for results of ancestral state reconstruction |
40 | 40 |
##' |
41 |
-##' |
|
41 |
+##' |
|
42 | 42 |
##' @title nodebar |
43 | 43 |
##' @param position position of bar, one of 'stack' and 'dodge' |
44 | 44 |
##' @inheritParams nodepie |
... | ... |
@@ -52,7 +52,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
52 | 52 |
stop("data should have a column 'node'...") |
53 | 53 |
} |
54 | 54 |
type <- value <- NULL |
55 |
- |
|
55 |
+ |
|
56 | 56 |
ldf <- gather(data, type, value, cols) %>% split(., .$node) |
57 | 57 |
bars <- lapply(ldf, function(df) ggplot(df, aes_(x=1, y=~value, fill=~type)) + |
58 | 58 |
geom_bar(stat='identity', alpha=alpha, position=position) + |
... | ... |
@@ -69,7 +69,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
69 | 69 |
|
70 | 70 |
##' generate a list of pie charts for results of ancestral stat reconstruction |
71 | 71 |
##' |
72 |
-##' |
|
72 |
+##' |
|
73 | 73 |
##' @title nodepie |
74 | 74 |
##' @param data a data.frame of stats with an additional column of node number |
75 | 75 |
##' @param cols column of stats |
... | ... |
@@ -91,11 +91,12 @@ nodepie <- function(data, cols, color, alpha=1) { |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
|
94 |
+##' @importFrom methods missingArg |
|
94 | 95 |
ggpie <- function(data, y, fill, color, alpha=1) { |
95 | 96 |
p <- ggplot(data, aes_(x=1, y=y, fill=fill)) + |
96 | 97 |
geom_bar(stat='identity', alpha=alpha) + |
97 | 98 |
coord_polar(theta='y') + theme_inset() |
98 |
- |
|
99 |
+ |
|
99 | 100 |
if (missingArg(color) || is.null(color) || is.na(color)) { |
100 | 101 |
## do nothing |
101 | 102 |
} else { |
102 | 103 |
deleted file mode 100644 |
... | ... |
@@ -1,122 +0,0 @@ |
1 |
-##' merge two tree object |
|
2 |
-##' |
|
3 |
-##' |
|
4 |
-##' @title merge_tree |
|
5 |
-##' @param obj1 tree object 1 |
|
6 |
-##' @param obj2 tree object 2 |
|
7 |
-##' @return tree object |
|
8 |
-##' @importFrom magrittr %<>% |
|
9 |
-##' @export |
|
10 |
-##' @author Guangchuang Yu |
|
11 |
-merge_tree <- function(obj1, obj2) { |
|
12 |
- ## |
|
13 |
- ## INFO: |
|
14 |
- ## ape::all.equal.phylo can be used to test equal phylo topology. |
|
15 |
- ## |
|
16 |
- |
|
17 |
- if (has.slot(obj1, "extraInfo") == FALSE) { |
|
18 |
- stop("input tree object is not supported...") |
|
19 |
- } |
|
20 |
- |
|
21 |
- if ((is.tree(obj1) & is.tree(obj2)) == FALSE) { |
|
22 |
- stop("input should be tree objects...") |
|
23 |
- } |
|
24 |
- |
|
25 |
- tr1 <- get.tree(obj1) |
|
26 |
- tr2 <- get.tree(obj2) |
|
27 |
- |
|
28 |
- if (getNodeNum(tr1) != getNodeNum(tr2)) { |
|
29 |
- stop("number of nodes not equals...") |
|
30 |
- } |
|
31 |
- |
|
32 |
- if (Ntip(tr1) != Ntip(tr2)) { |
|
33 |
- stop("number of tips not equals...") |
|
34 |
- } |
|
35 |
- |
|
36 |
- if (all(tr1$tip.label %in% tr2$tip.label) == FALSE) { |
|
37 |
- stop("tip names not match...") |
|
38 |
- } |
|
39 |
- |
|
40 |
- |
|
41 |
- ## order tip.label in tr2 as in tr1 |
|
42 |
- ## mapping corresponding ID |
|
43 |
- idx <- match(tr2$tip.label, tr1$tip.label) |
|
44 |
- tr2$edge[match(1:Ntip(tr2), tr2$edge[,2]), 2] <- idx |
|
45 |
- tr2$tip.label <- tr1$tip.label |
|
46 |
- |
|
47 |
- node_map <- list() |
|
48 |
- node_map$from %<>% c(1:Ntip(tr2)) |
|
49 |
- node_map$to %<>% c(idx) |
|
50 |
- |
|
51 |
- root <- getRoot(tr1) |
|
52 |
- root.2 <- getRoot(tr2) |
|
53 |
- tr2$edge[tr2$edge[,1] == root.2, 1] <- root |
|
54 |
- |
|
55 |
- node_map$from %<>% c(root.2) |
|
56 |
- node_map$to %<>% c(root) |
|
57 |
- |
|
58 |
- |
|
59 |
- currentNode <- 1:Ntip(tr1) |
|
60 |
- while(length(currentNode)) { |
|
61 |
- p1 <- sapply(currentNode, getParent, tr=tr1) |
|
62 |
- p2 <- sapply(currentNode, getParent, tr=tr2) |
|
63 |
- |
|
64 |
- if (length(p1) != length(p2)) { |
|
65 |
- stop("trees are not identical...") |
|
66 |
- } |
|
67 |
- |
|
68 |
- jj <- match(p2, tr2$edge[,1]) |
|
69 |
- if (length(jj)) { |
|
70 |
- notNA <- which(!is.na(jj)) |
|
71 |
- jj <- jj[notNA] |
|
72 |
- } |
|
73 |
- if (length(jj)) { |
|
74 |
- tr2$edge[jj,1] <- p1[notNA] |
|
75 |
- } |
|
76 |
- |
|
77 |
- |
|
78 |
- ii <- match(p2, tr2$edge[,2]) |
|
79 |
- if (length(ii)) { |
|
80 |
- notNA <- which(!is.na(ii)) |
|
81 |
- ii <- ii[notNA] |
|
82 |
- } |
|
83 |
- if (length(ii)) { |
|
84 |
- tr2$edge[ii,2] <- p1[notNA] |
|
85 |
- } |
|
86 |
- |
|
87 |
- node_map$from %<>% c(p2) |
|
88 |
- node_map$to %<>% c(p1) |
|
89 |
- |
|
90 |
- ## parent of root will return 0, which is in-valid node ID |
|
91 |
- currentNode <- unique(p1[p1 != 0]) |
|
92 |
- } |
|
93 |
- |
|
94 |
- if ( any(tr2$edge != tr2$edge) ) { |
|
95 |
- stop("trees are not identical...") |
|
96 |
- } |
|
97 |
- |
|
98 |
- node_map.df <- do.call("cbind", node_map) |
|
99 |
- node_map.df <- unique(node_map.df) |
|
100 |
- node_map.df <- node_map.df[node_map.df[,1] != 0,] |
|
101 |
- i <- order(node_map.df[,1], decreasing = FALSE) |
|
102 |
- node_map.df <- node_map.df[i,] |
|
103 |
- |
|
104 |
- info2 <- fortify(obj2) |
|
105 |
- info2$node <- node_map.df[info2$node, 2] |
|
106 |
- info2$parent <- node_map.df[info2$parent, 2] |
|
107 |
- |
|
108 |
- cn <- colnames(info2) |
|
109 |
- i <- match(c("x", "y", "isTip", "label", "branch", "branch.length", "angle"), cn) |
|
110 |
- i <- i[!is.na(i)] |
|
111 |
- info2 <- info2[, -i] |
|
112 |
- |
|
113 |
- extraInfo <- obj1@extraInfo |
|
114 |
- if (nrow(extraInfo) == 0) { |
|
115 |
- obj1@extraInfo <- info2 |
|
116 |
- } else { |
|
117 |
- info <- merge(extraInfo, info2, by.x =c("node", "parent"), by.y = c("node", "parent")) |
|
118 |
- obj1@extraInfo <- info |
|
119 |
- } |
|
120 |
- |
|
121 |
- return(obj1) |
|
122 |
-} |
... | ... |
@@ -52,6 +52,7 @@ as.binary.phylo <- function(tree, ...) { |
52 | 52 |
##' @importFrom magrittr %<>% |
53 | 53 |
##' @importFrom magrittr add |
54 | 54 |
##' @importFrom ape write.tree |
55 |
+##' @importFrom ape read.tree |
|
55 | 56 |
##' @author Guangchuang Yu \url{http://ygc.name} |
56 | 57 |
rm.singleton.newick <- function(nwk, outfile = NULL) { |
57 | 58 |
tree <- readLines(nwk) |
... | ... |
@@ -341,21 +342,21 @@ fortify.phangorn <- fortify.paml_rst |
341 | 342 |
fortify.hyphy <- fortify.paml_rst |
342 | 343 |
|
343 | 344 |
|
344 |
-##' @method fortify jplace |
|
345 |
-##' @importFrom ape read.tree |
|
346 |
-##' @export |
|
347 |
-fortify.jplace <- function(model, data, |
|
348 |
- layout="rectangular", yscale="none", |
|
349 |
- ladderize=TRUE, right=FALSE, mrsd=NULL, ...) { |
|
350 |
- df <- get.treeinfo(model, layout, ladderize, right, mrsd=mrsd, ...) |
|
351 |
- place <- get.placements(model, by="best") |
|
345 |
+## ##' @method fortify jplace |
|
346 |
+## ##' @importFrom ape read.tree |
|
347 |
+## ##' @export |
|
348 |
+## fortify.jplace <- function(model, data, |
|
349 |
+## layout="rectangular", yscale="none", |
|
350 |
+## ladderize=TRUE, right=FALSE, mrsd=NULL, ...) { |
|
351 |
+## df <- get.treeinfo(model, layout, ladderize, right, mrsd=mrsd, ...) |
|
352 |
+## place <- get.placements(model, by="best") |
|
352 | 353 |
|
353 |
- df <- df %add2% place |
|
354 |
+## df <- df %add2% place |
|
354 | 355 |
|
355 |
- df <- scaleY(model@phylo, df, yscale, layout, ...) |
|
356 |
+## df <- scaleY(model@phylo, df, yscale, layout, ...) |
|
356 | 357 |
|
357 |
- append_extraInfo(df, model) |
|
358 |
-} |
|
358 |
+## append_extraInfo(df, model) |
|
359 |
+## } |
|
359 | 360 |
|
360 | 361 |
scaleY <- function(phylo, df, yscale, layout, ...) { |
361 | 362 |
if (yscale == "none") { |
... | ... |
@@ -568,14 +569,14 @@ as.data.frame.phylo_ <- function(x, layout="rectangular", |
568 | 569 |
return(res) |
569 | 570 |
} |
570 | 571 |
|
571 |
-##' @method fortify nhx |
|
572 |
-##' @export |
|
573 |
-fortify.nhx <- function(model, data, layout= "rectangular", |
|
574 |
- ladderize=TRUE, right=FALSE, mrsd=NULL, ...) { |
|
575 |
- df <- fortify(get.tree(model), layout=layout, ladderize=ladderize, right=right, mrsd=mrsd, ...) |
|
576 |
- df <- merge(df, model@nhx_tags, by.x="node", by.y="node", all.x=TRUE) |
|
577 |
- append_extraInfo(df, model) |
|
578 |
-} |
|
572 |
+## ##' @method fortify nhx |
|
573 |
+## ##' @export |
|
574 |
+## fortify.nhx <- function(model, data, layout= "rectangular", |
|
575 |
+## ladderize=TRUE, right=FALSE, mrsd=NULL, ...) { |
|
576 |
+## df <- fortify(get.tree(model), layout=layout, ladderize=ladderize, right=right, mrsd=mrsd, ...) |
|
577 |
+## df <- merge(df, model@nhx_tags, by.x="node", by.y="node", all.x=TRUE) |
|
578 |
+## append_extraInfo(df, model) |
|
579 |
+## } |
|
579 | 580 |
|
580 | 581 |
|
581 | 582 |
##' @method fortify raxml |
... | ... |
@@ -13,11 +13,18 @@ |
13 | 13 |
## } |
14 | 14 |
## ) |
15 | 15 |
|
16 |
+##' groupClade method for ggtree object |
|
17 |
+##' |
|
18 |
+##' |
|
16 | 19 |
##' @name groupClade |
17 | 20 |
##' @title groupClade method |
18 | 21 |
##' @rdname groupClade-methods |
22 |
+##' @param object ggtree object |
|
23 |
+##' @param node internal node number |
|
24 |
+##' @param group_name name of the group |
|
19 | 25 |
##' @importFrom treeio groupClade |
20 | 26 |
##' @exportMethod groupClade |
27 |
+##' @aliases groupClade,ggtree-method |
|
21 | 28 |
setMethod("groupClade", signature(object="ggtree"), |
22 | 29 |
function(object, node, group_name) { |
23 | 30 |
groupClade.ggtree(object, node, group_name) |
... | ... |
@@ -33,11 +33,19 @@ |
33 | 33 |
## ) |
34 | 34 |
|
35 | 35 |
|
36 |
+##' groupOTU method for ggtree object |
|
37 |
+##' |
|
38 |
+##' |
|
36 | 39 |
##' @name groupOTU |
37 | 40 |
##' @title groupOTU method |
38 | 41 |
##' @rdname groupOTU-methods |
42 |
+##' @param object ggtree object |
|
43 |
+##' @param focus OTU to focus |
|
44 |
+##' @param group_name name of the group |
|
45 |
+##' @param ... additional parameters |
|
39 | 46 |
##' @importFrom treeio groupOTU |
40 | 47 |
##' @exportMethod groupOTU |
48 |
+##' @aliases groupOTU,ggtree-method |
|
41 | 49 |
setMethod("groupOTU", signature(object="ggtree"), |
42 | 50 |
function(object, focus, group_name="group", ...) { |
43 | 51 |
groupOTU.ggtree(object, focus, group_name, ...) |
... | ... |
@@ -172,14 +180,14 @@ setMethod("groupOTU", signature(object="ggtree"), |
172 | 180 |
## return(phy) |
173 | 181 |
## } |
174 | 182 |
|
175 |
-groupOTU_ <- function(object, focus, group_name, ...) { |
|
176 |
- if (is(object, "phylo")) { |
|
177 |
- object <- groupOTU.phylo(object, focus, group_name, ...) |
|
178 |
- } else { |
|
179 |
- object@phylo <- groupOTU.phylo(get.tree(object), focus, group_name, ...) |
|
180 |
- } |
|
181 |
- return(object) |
|
182 |
-} |
|
183 |
+## groupOTU_ <- function(object, focus, group_name, ...) { |
|
184 |
+## if (is(object, "phylo")) { |
|
185 |
+## object <- groupOTU.phylo(object, focus, group_name, ...) |
|
186 |
+## } else { |
|
187 |
+## object@phylo <- groupOTU.phylo(get.tree(object), focus, group_name, ...) |
|
188 |
+## } |
|
189 |
+## return(object) |
|
190 |
+## } |
|
183 | 191 |
|
184 | 192 |
|
185 | 193 |
groupOTU.ggtree <- function(object, focus, group_name, ...) { |
... | ... |
@@ -53,6 +53,7 @@ gzoom.ggtree <- function(tree_view, focus, widths=c(.3, .7), xmax_adjust=0) { |
53 | 53 |
##' @rdname gzoom-methods |
54 | 54 |
##' @exportMethod gzoom |
55 | 55 |
##' @param xmax_adjust adjust xmax (xlim[2]) |
56 |
+##' @aliases gzoom,ggtree-method |
|
56 | 57 |
setMethod("gzoom", signature(object="ggtree"), |
57 | 58 |
function(object, focus, widths=c(.3, .7), xmax_adjust=0) { |
58 | 59 |
gzoom.ggtree(object, focus, widths, xmax_adjust) |
... | ... |
@@ -1,394 +1,394 @@ |
1 |
-##' site mask |
|
2 |
-##' |
|
3 |
-##' |
|
4 |
-##' @title mask |
|
5 |
-##' @param tree_object tree object |
|
6 |
-##' @param field selected field |
|
7 |
-##' @param site site |
|
8 |
-##' @param mask_site if TRUE, site will be masked. |
|
9 |
-##' if FALSE, selected site will not be masked, while other sites will be masked. |
|
10 |
-##' @return updated tree object |
|
11 |
-##' @export |
|
12 |
-##' @author Guangchuang Yu |
|
13 |
-mask <- function(tree_object, field, site, mask_site=FALSE) { |
|
14 |
- has_field <- has.field(tree_object, field) |
|
15 |
- if (has_field == FALSE) { |
|
16 |
- stop("'field' is not available in 'tree_object'...") |
|
17 |
- } |
|
18 |
- |
|
19 |
- has_slot <- attr(has_field, "has_slot") |
|
20 |
- is_codeml <- attr(has_field, "is_codeml") |
|
21 |
- |
|
22 |
- if (has_slot) { |
|
23 |
- if (is_codeml) { |
|
24 |
- field_info <- slot(tree_object@rst, field) |
|
25 |
- } else { |
|
26 |
- field_info <- slot(tree_object, field) |
|
27 |
- } |
|
28 |
- field_data <- field_info[,2] |
|
29 |
- } else { |
|
30 |
- field_data <- tree_object@extraInfo[, field] |
|
31 |
- } |
|
32 |
- |
|
33 |
- field_data <- sapply(field_data, gsub, pattern="\n", replacement="/") |
|
34 |
- |
|
35 |
- x <- field_data[field_data != ""] |
|
36 |
- x <- x[!is.na(x)] |
|
37 |
- pos <- strsplit(x, " / ") %>% unlist %>% |
|
38 |
- gsub("^[a-zA-Z]+", "", . ) %>% |
|
39 |
- gsub("[a-zA-Z]\\s*$", "", .) %>% |
|
40 |
- as.numeric |
|
41 |
- |
|
42 |
- if (mask_site == FALSE) { |
|
43 |
- pos2 <- 1:max(pos) |
|
44 |
- pos2 <- pos2[-site] |
|
45 |
- site <- pos2 |
|
46 |
- } |
|
47 |
- |
|
48 |
- site <- site[site %in% pos] |
|
49 |
- |
|
50 |
- for (i in seq_along(field_data)) { |
|
51 |
- if (is.na(field_data[i])) |
|
52 |
- next |
|
53 |
- for (j in seq_along(site)) { |
|
54 |
- pattern <- paste0("/*\\s*[a-zA-Z]", site[j], "[a-zA-Z]\\s*") |
|
55 |
- field_data[i] <- gsub(pattern, "", field_data[i]) |
|
56 |
- } |
|
57 |
- field_data[i] <- gsub("^/\\s", "", field_data[i]) %>% .add_new_line |
|
58 |
- } |
|
59 |
- |
|
60 |
- if (has_slot) { |
|
61 |
- field_info[,2] <- field_data |
|
62 |
- if (is_codeml) { |
|
63 |
- slot(tree_object@rst, field) <- field_info |
|
64 |
- } else { |
|
65 |
- slot(tree_object, field) <- field_info |
|
66 |
- } |
|
67 |
- } else { |
|
68 |
- tree_object@extraInfo[, field] <- field_data |
|
69 |
- } |
|
70 |
- tree_object |
|
71 |
-} |
|
72 |
- |
|
73 |
- |
|
74 |
-read.tip_seq_mlc <- function(mlcfile) { |
|
75 |
- info <- getPhyInfo(mlcfile) |
|
76 |
- mlc <- readLines(mlcfile) |
|
77 |
- ## remove blank lines |
|
78 |
- blk <- grep("^\\s*$", mlc) |
|
79 |
- if (length(blk) > 0) { |
|
80 |
- mlc <- mlc[-blk] |
|
81 |
- } |
|
82 |
- |
|
83 |
- seqs <- mlc[2:(info$num+1)] |
|
84 |
- seqs <- gsub("\\s+", "", seqs) |
|
85 |
- wd <- info$width |
|
86 |
- res <- sapply(seqs, function(x) substring(x, (base::nchar(x) - wd + 1), base::nchar(x))) |
|
87 |
- nn <- sapply(seqs, function(x) substring(x, 1, (base::nchar(x) - wd))) |
|
88 |
- names(res) <- nn |
|
89 |
- return(res) |
|
90 |
-} |
|
91 |
- |
|
92 |
-read.tip_seq_mlb <- read.tip_seq_mlc |
|
93 |
- |
|
94 |
-read.dnds_mlc <- function(mlcfile) { |
|
95 |
- mlc <- readLines(mlcfile) |
|
96 |
- i <- grep("dN & dS for each branch", mlc) |
|
97 |
- j <- grep("tree length for dN", mlc) |
|
98 |
- |
|
99 |
- mlc <- mlc[i:j] |
|
100 |
- hi <- grep("dN/dS", mlc) |
|
101 |
- cn <- strsplit(mlc[hi], " ") %>% unlist %>% `[`(nzchar(.)) |
|
102 |
- |
|
103 |
- ii <- grep("\\d+\\.\\.\\d+", mlc) |
|
104 |
- info <- mlc[ii] |
|
105 |
- info %<>% sub("^\\s+", "", .) |
|
106 |
- info %<>% sub("\\s+$", "", .) |
|
107 |
- res <- t(sapply(info, function(x) { |
|
108 |
- y <- unlist(strsplit(x, "\\s+")) |
|
109 |
- edge <- unlist(strsplit(y[1], "\\.\\.")) |
|
110 |
- yy <- c(edge, y[-1]) |
|
111 |
- as.numeric(yy) |
|
112 |
- })) |
|
113 |
- |
|
114 |
- row.names(res) <- NULL |
|
115 |
- colnames(res) <- c("parent", "node", cn[-1]) |
|
116 |
- colnames(res) <- gsub("\\*", "_x_", colnames(res)) |
|
117 |
- colnames(res) <- gsub("\\/", "_vs_", colnames(res)) |
|
118 |
- return(res) |
|
119 |
-} |
|
120 |
- |
|
121 |
-read.treetext_paml_mlc <- function(mlcfile) { |
|
122 |
- read.treetext_paml(mlcfile, "mlc") |
|
123 |
-} |
|
124 |
- |
|
125 |
-read.treetext_paml_rst<- function(rstfile) { |
|
126 |
- read.treetext_paml(rstfile, "rst") |
|
127 |
-} |
|
128 |
- |
|
129 |
-read.treetext_paml <- function(file, by) { |
|
130 |
- ## works fine with baseml and codeml |
|
131 |
- x <- readLines(file) |
|
132 |
- tr.idx <- get_tree_index_paml(x) |
|
133 |
- |
|
134 |
- if ( by == "rst" ) { |
|
135 |
- ii <- 1 |
|
136 |
- } else if (by == "mlc") { |
|
137 |
- ii <- 3 |
|
138 |
- } else { |
|
139 |
- stop("_by_ should be one of 'rst' or 'mlc'") |
|
140 |
- } |
|
141 |
- |
|
142 |
- return(x[tr.idx][ii]) |
|
143 |
-} |
|
144 |
- |
|
145 |
-read.phylo_paml_mlc <- function(mlcfile) { |
|
146 |
- parent <- node <- label <- NULL |
|
147 |
- |
|
148 |
- mlc <- readLines(mlcfile) |
|
149 |
- edge <- get_tree_edge_paml(mlc) |
|
150 |
- |
|
151 |
- tr.idx <- get_tree_index_paml(mlc) |
|
152 |
- tr2 <- read.tree(text=mlc[tr.idx[2]]) |
|
153 |
- tr3 <- read.tree(text=mlc[tr.idx[3]]) |
|
154 |
- |
|
155 |
- treeinfo <- as.data.frame(edge) |
|
156 |
- colnames(treeinfo) <- c("parent", "node") |
|
157 |
- treeinfo$length <- NA |
|
158 |
- treeinfo$label <- NA |
|
159 |
- treeinfo$isTip <- FALSE |
|
160 |
- ntip <- Ntip(tr3) |
|
161 |
- ii <- match(tr2$tip.label, treeinfo[, "node"]) |
|
162 |
- treeinfo[ii, "label"] <- tr3$tip.label |
|
163 |
- treeinfo[ii, "isTip"] <- TRUE |
|
164 |
- ## jj <- match(1:ntip, tr3$edge[,2]) |
|
165 |
- treeinfo[ii, "length"] <- tr2$edge.length[ii] ##tr3$edge.length[jj] |
|
166 |
- |
|
167 |
- root <- getRoot(tr3) ## always == (Ntip(tr3) + 1) |
|
168 |
- currentNode <- treeinfo$label[ii] |
|
169 |
- treeinfo.tr3 <- fortify(tr3) |
|
170 |
- |
|
171 |
- treeinfo$visited <- FALSE |
|
172 |
- while(any(treeinfo$visited == FALSE)) { |
|
173 |
- pNode <- c() |
|
174 |
- for( kk in currentNode ) { |
|
175 |
- i <- which(treeinfo$label == kk) |
|
176 |
- treeinfo[i, "visited"] <- TRUE |
|
177 |
- j <- which(treeinfo.tr3$label == kk) |
|
178 |
- ip <- treeinfo[i, "parent"] |
|
179 |
- if (ip != root) { |
|
180 |
- ii <- which(treeinfo[, "node"] == ip) |
|
181 |
- if (treeinfo$visited[ii] == FALSE) { |
|
182 |
- jp <- treeinfo.tr3[j, "parent"] |
|
183 |
- jj <- which(treeinfo.tr3[, "node"] == jp) |
|
184 |
- treeinfo[ii, "label"] <- as.character(ip) |
|
185 |
- treeinfo.tr3[jj, "label"] <- as.character(ip) |
|
186 |
- treeinfo[ii, "length"] <- treeinfo.tr3[jj, "branch.length"] |
|
187 |
- pNode <- c(pNode, ip) |
|
188 |
- } |
|
189 |
- treeinfo[ii, "visited"] <- TRUE |
|
190 |
- } |
|
191 |
- |
|
192 |
- } |
|
193 |
- currentNode <- unique(pNode) |
|
194 |
- } |
|
195 |
- |
|
196 |
- phylo <- with(treeinfo, |
|
197 |
- list(edge= cbind(as.numeric(parent), |
|
198 |
- as.numeric(node)), |
|
199 |
- edge.length = length, |
|
200 |
- tip.label = label[order(node)][1:ntip], |
|
201 |
- Nnode = tr3$Nnode, |
|
202 |
- node.label = c(root, label[order(node)][-c(1:ntip)]) |
|
203 |
- ) |
|
204 |
- ) |
|
205 |
- class(phylo) <- "phylo" |
|
206 |
- phylo <- reorder.phylo(phylo, "cladewise") |
|
207 |
- return(phylo) |
|
208 |
-} |
|
209 |
- |
|
210 |
-##' @importFrom ape reorder.phylo |
|
211 |
-read.phylo_paml_rst <- function(rstfile) { |
|
212 |
- parent <- node <- label <- NULL |
|
213 |
- |
|
214 |
- ## works fine with baseml and codeml |
|
215 |
- rst <- readLines(rstfile) |
|
216 |
- tr.idx <- get_tree_index_paml(rst) |
|
217 |
- |
|
218 |
- tr1 <- read.tree(text=rst[tr.idx][1]) |
|
219 |
- tr3 <- read.tree(text=rst[tr.idx][3]) |
|
220 |
- |
|
221 |
- edge <- get_tree_edge_paml(rst) |
|
222 |
- |
|
223 |
- label=c(tr3$tip.label, tr3$node.label) |
|
224 |
- root <- getRoot(tr3) |
|
225 |
- label %<>% `[`(. != root) |
|
226 |
- |
|
227 |
- node.length <- data.frame(label=label, |
|
228 |
- length=tr1$edge.length) |
|
229 |
- |
|
230 |
- ## node.length$node <- sub("_\\w+", "", node.length$label |
|
231 |
- node.length$node <- gsub("^(\\d+)_.*", "\\1", node.length$label) |
|
232 |
- node.length$label %<>% sub("\\d+_", "", .) |
|
233 |
- |
|
234 |
- edge <- as.data.frame(edge) |
|
235 |
- colnames(edge) <- c("parent", "node") |
|
236 |
- |
|
237 |
- treeinfo <- merge(edge, node.length, by.x="node", by.y="node") |
|
238 |
- edge2 <- treeinfo[, c("parent", "node")] |
|
239 |
- edge2 %<>% as.matrix |
|
240 |
- |
|
241 |
- ntip <- Ntip(tr3) |
|
242 |
- |
|
243 |
- phylo <- with(treeinfo, |
|
244 |
- list(edge= cbind(as.numeric(parent), |
|
245 |
- as.numeric(node)), |
|
246 |
- edge.length = length, |
|
247 |
- tip.label = label[order(node)][1:ntip], |
|
248 |
- Nnode = tr3$Nnode, |
|
249 |
- node.label = c(root, label[order(node)][-c(1:ntip)]) |
|
250 |
- ) |
|
251 |
- ) |
|
252 |
- |
|
253 |
- class(phylo) <- "phylo" |
|
254 |
- phylo <- reorder.phylo(phylo, "cladewise") |
|
255 |
- |
|
256 |
- return(phylo) |
|
257 |
-} |
|
258 |
- |
|
259 |
-read.ancseq_paml_rst <- function(rstfile, by="Marginal") { |
|
260 |
- ## works fine with baseml and codeml |
|
261 |
- rst <- readLines(rstfile) |
|
262 |
- |
|
263 |
- by <- match.arg(by, c("Marginal", "Joint")) |
|
264 |
- query <- paste(by, "reconstruction of ancestral sequences") |
|
265 |
- idx <- grep(query, rst) |
|
266 |
- if(length(idx) == 0) { |
|
267 |
- ## in some paml setting, joint_ancseq are not available. |
|
268 |
- return("") |
|
269 |
- } |
|
270 |
- si <- grep("reconstructed sequences", rst) |
|
271 |
- idx <- si[which.min(abs(si-idx))] |
|
272 |
- |
|
273 |
- nl <- strsplit(rst[idx+2], split=" ") %>% unlist %<>% `[`(nzchar(.)) |
|
274 |
- N <- as.numeric(nl[1]) |
|
275 |
- seq.leng <- as.numeric(nl[2]) |
|
276 |
- |
|
277 |
- seqs <- rst[(idx+4):(idx+3+N)] |
|
278 |
- |
|
279 |
- seq.name <- character(N) |
|
280 |
- res <- character(N) |
|
281 |
- for (i in 1:N) { |
|
282 |
- ss <- gsub(" ", "", seqs[i]) |
|
283 |
- nn <- base::nchar(ss) |
|
284 |
- res[i] <- substring(ss, nn-seq.leng+1,nn) |
|
285 |
- seq.name[i] <- substring(ss, 1, nn-seq.leng) |
|
286 |
- } |
|
287 |
- seq.name <- sub("\\w+#", "", seq.name) |
|
288 |
- names(res) <- seq.name |
|
289 |
- |
|
290 |
- return(res) |
|
291 |
-} |
|
292 |
- |
|
293 |
- |
|
294 |
-get_tree_index_paml <- function(paml) { |
|
295 |
- grep("\\)[ \\.0-9]*;", paml) |
|
296 |
-} |
|
297 |
- |
|
298 |
-get_tree_edge_index_paml <- function(paml) { |
|
299 |
- grep("\\d+\\.\\.\\d+", paml) |
|
300 |
-} |
|
301 |
- |
|
302 |
-get_tree_edge_paml <- function(paml) { |
|
303 |
- tr.idx <- get_tree_index_paml(paml) |
|
304 |
- |
|
305 |
- edge.idx <- get_tree_edge_index_paml(paml) |
|
306 |
- edge.idx <- edge.idx[edge.idx < tr.idx[3]] |
|
307 |
- |
|
308 |
- nodeNum <- strsplit(paml[edge.idx], split="\\.\\.") %>% |
|
309 |
- unlist %>% strsplit(split="[[:space:]]") %>% unlist |
|
310 |
- |
|
311 |
- nodeNum %<>% `[`(nzchar(.)) |
|
312 |
- |
|
313 |
- edge <- matrix(as.numeric(nodeNum), ncol=2, byrow = TRUE) |
|
314 |
- |
|
315 |
- return(edge) |
|
316 |
-} |
|
317 |
- |
|
318 |
-set.paml_rst_ <- function(object) { |
|
319 |
- if (!is(object, "paml_rst")) { |
|
320 |
- stop("object should be an instance of 'paml_rst'") |
|
321 |
- } |
|
322 |
- if (length(object@tip_seq) == 0) { |
|
323 |
- return(object) |
|
324 |
- } |
|
325 |
- |
|
326 |
- types <- get.fields(object) |
|
327 |
- for (type in types) { |
|
328 |
- value <- subs_paml_rst(object, type) |
|
329 |
- if (all(is.na(value))) |
|
330 |
- next |
|
331 |
- |
|
332 |
- if (type == "marginal_subs") { |
|
333 |
- object@marginal_subs <- value |
|
334 |
- } else if (type == "marginal_AA_subs") { |
|
335 |
- object@marginal_AA_subs <- value |
|
336 |
- } else if (type == "joint_subs") { |
|
337 |
- object@joint_subs <- value |
|
338 |
- } else if (type == "joint_AA_subs") { |
|
339 |
- object@joint_AA_subs <- value |
|
340 |
- } |
|
341 |
- } |
|
342 |
- return(object) |
|
343 |
-} |
|
344 |
- |
|
345 |
- |
|
346 |
-get.subs_paml_rst <- function(object, type) { |
|
347 |
- if (!is(object, "paml_rst")) { |
|
348 |
- stop("object should be an instance of 'paml_rst'") |
|
349 |
- } |
|
350 |
- if (type == "marginal_subs") { |
|
351 |
- res <- object@marginal_subs |
|
352 |
- } else if (type == "marginal_AA_subs") { |
|
353 |
- res <- object@marginal_AA_subs |
|
354 |
- } else if (type == "joint_subs") { |
|
355 |
- res <- object@joint_subs |
|
356 |
- } else if (type == "joint_AA_subs") { |
|
357 |
- res <- object@joint_AA_subs |
|
358 |
- } else { |
|
359 |
- stop("type should be one of 'marginal_subs', |
|
360 |
- 'marginal_AA_subs', 'joint_subs' or 'joint_AA_subs'. ") |
|
361 |
- } |
|
362 |
- return(res) |
|
363 |
-} |
|
364 |
- |
|
365 |
-subs_paml_rst <- function(x, type, ...) { |
|
366 |
- if (class(x) != "paml_rst") { |
|
367 |
- stop("x should be an object of paml_rst...") |
|
368 |
- } |
|
369 |
- seqs <- x@tip_seq |
|
370 |
- if (length(seqs) == 0) { |
|
371 |
- stop("tip sequences is not available...") |
|
372 |
- } |
|
373 |
- if (type %in% c("marginal_subs", "marginal_AA_subs")) { |
|
374 |
- ancseq <- x@marginal_ancseq |
|
375 |
- ## seqs <- c(seqs, x@marginal_ancseq) |
|
376 |
- } else if (type %in% c("joint_subs", "joint_AA_subs")){ |
|
377 |
- ancseq <- x@joint_ancseq |
|
378 |
- ## seqs <- c(seqs, x@joint_ancseq) |
|
379 |
- } else { |
|
380 |
- stop("type should be one of 'marginal_subs', |
|
381 |
- 'marginal_AA_subs', 'joint_subs' or 'joint_AA_subs'. ") |
|
382 |
- } |
|
383 |
- if( type %in% c("marginal_subs", "joint_subs")) { |
|
384 |
- translate <- FALSE |
|
385 |
- } else { |
|
386 |
- translate <- TRUE |
|
387 |
- } |
|
388 |
- |
|
389 |
- if (all(ancseq == "")) { |
|
390 |
- return(NA) |
|
391 |
- } |
|
392 |
- seqs <- c(seqs, ancseq) |
|
393 |
- get.subs_(x@phylo, seqs, translate=translate, ...) |
|
394 |
-} |
|
1 |
+## ##' site mask |
|
2 |
+## ##' |
|
3 |
+## ##' |
|
4 |
+## ##' @title mask |
|
5 |
+## ##' @param tree_object tree object |
|
6 |
+## ##' @param field selected field |
|
7 |
+## ##' @param site site |
|
8 |
+## ##' @param mask_site if TRUE, site will be masked. |
|
9 |
+## ##' if FALSE, selected site will not be masked, while other sites will be masked. |
|
10 |
+## ##' @return updated tree object |
|
11 |
+## ##' @export |
|
12 |
+## ##' @author Guangchuang Yu |
|
13 |
+## mask <- function(tree_object, field, site, mask_site=FALSE) { |
|
14 |
+## has_field <- has.field(tree_object, field) |
|
15 |
+## if (has_field == FALSE) { |
|
16 |
+## stop("'field' is not available in 'tree_object'...") |
|
17 |
+## } |
|
18 |
+ |
|
19 |
+## has_slot <- attr(has_field, "has_slot") |
|
20 |
+## is_codeml <- attr(has_field, "is_codeml") |
|
21 |
+ |
|
22 |
+## if (has_slot) { |
|
23 |
+## if (is_codeml) { |
|
24 |
+## field_info <- slot(tree_object@rst, field) |
|
25 |
+## } else { |
|
26 |
+## field_info <- slot(tree_object, field) |
|
27 |
+## } |
|
28 |
+## field_data <- field_info[,2] |
|
29 |
+## } else { |
|
30 |
+## field_data <- tree_object@extraInfo[, field] |
|
31 |
+## } |
|
32 |
+ |
|
33 |
+## field_data <- sapply(field_data, gsub, pattern="\n", replacement="/") |
|
34 |
+ |
|
35 |
+## x <- field_data[field_data != ""] |
|
36 |
+## x <- x[!is.na(x)] |
|
37 |
+## pos <- strsplit(x, " / ") %>% unlist %>% |
|
38 |
+## gsub("^[a-zA-Z]+", "", . ) %>% |
|
39 |
+## gsub("[a-zA-Z]\\s*$", "", .) %>% |
|
40 |
+## as.numeric |
|
41 |
+ |
|
42 |
+## if (mask_site == FALSE) { |
|
43 |
+## pos2 <- 1:max(pos) |
|
44 |
+## pos2 <- pos2[-site] |
|
45 |
+## site <- pos2 |
|
46 |
+## } |
|
47 |
+ |
|
48 |
+## site <- site[site %in% pos] |
|
49 |
+ |
|
50 |
+## for (i in seq_along(field_data)) { |
|
51 |
+## if (is.na(field_data[i])) |
|
52 |
+## next |
|
53 |
+## for (j in seq_along(site)) { |
|
54 |
+## pattern <- paste0("/*\\s*[a-zA-Z]", site[j], "[a-zA-Z]\\s*") |
|
55 |
+## field_data[i] <- gsub(pattern, "", field_data[i]) |
|
56 |
+## } |
|
57 |
+## field_data[i] <- gsub("^/\\s", "", field_data[i]) %>% .add_new_line |
|
58 |
+## } |
|
59 |
+ |
|
60 |
+## if (has_slot) { |
|
61 |
+## field_info[,2] <- field_data |
|
62 |
+## if (is_codeml) { |
|
63 |
+## slot(tree_object@rst, field) <- field_info |
|
64 |
+## } else { |
|
65 |
+## slot(tree_object, field) <- field_info |
|
66 |
+## } |
|
67 |
+## } else { |
|
68 |
+## tree_object@extraInfo[, field] <- field_data |
|
69 |
+## } |
|
70 |
+## tree_object |
|
71 |
+## } |
|
72 |
+ |
|
73 |
+ |
|
74 |
+## read.tip_seq_mlc <- function(mlcfile) { |
|
75 |
+## info <- getPhyInfo(mlcfile) |
|
76 |
+## mlc <- readLines(mlcfile) |
|
77 |
+## ## remove blank lines |
|
78 |
+## blk <- grep("^\\s*$", mlc) |
|
79 |
+## if (length(blk) > 0) { |
|
80 |
+## mlc <- mlc[-blk] |
|
81 |
+## } |
|
82 |
+ |
|
83 |
+## seqs <- mlc[2:(info$num+1)] |
|
84 |
+## seqs <- gsub("\\s+", "", seqs) |
|
85 |
+## wd <- info$width |
|
86 |
+## res <- sapply(seqs, function(x) substring(x, (base::nchar(x) - wd + 1), base::nchar(x))) |
|
87 |
+## nn <- sapply(seqs, function(x) substring(x, 1, (base::nchar(x) - wd))) |
|
88 |
+## names(res) <- nn |
|
89 |
+## return(res) |
|
90 |
+## } |
|
91 |
+ |
|
92 |
+## read.tip_seq_mlb <- read.tip_seq_mlc |
|
93 |
+ |
|
94 |
+## read.dnds_mlc <- function(mlcfile) { |
|
95 |
+## mlc <- readLines(mlcfile) |
|
96 |
+## i <- grep("dN & dS for each branch", mlc) |
|
97 |
+## j <- grep("tree length for dN", mlc) |
|
98 |
+ |
|
99 |
+## mlc <- mlc[i:j] |
|
100 |
+## hi <- grep("dN/dS", mlc) |
|
101 |
+## cn <- strsplit(mlc[hi], " ") %>% unlist %>% `[`(nzchar(.)) |
|
102 |
+ |
|
103 |
+## ii <- grep("\\d+\\.\\.\\d+", mlc) |
|
104 |
+## info <- mlc[ii] |
|
105 |
+## info %<>% sub("^\\s+", "", .) |
|
106 |
+## info %<>% sub("\\s+$", "", .) |
|
107 |
+## res <- t(sapply(info, function(x) { |
|
108 |
+## y <- unlist(strsplit(x, "\\s+")) |
|
109 |
+## edge <- unlist(strsplit(y[1], "\\.\\.")) |
|
110 |
+## yy <- c(edge, y[-1]) |
|
111 |
+## as.numeric(yy) |
|
112 |
+## })) |
|
113 |
+ |
|
114 |
+## row.names(res) <- NULL |
|
115 |
+## colnames(res) <- c("parent", "node", cn[-1]) |
|
116 |
+## colnames(res) <- gsub("\\*", "_x_", colnames(res)) |
|
117 |
+## colnames(res) <- gsub("\\/", "_vs_", colnames(res)) |
|
118 |
+## return(res) |
|
119 |
+## } |
|
120 |
+ |
|
121 |
+## read.treetext_paml_mlc <- function(mlcfile) { |
|
122 |
+## read.treetext_paml(mlcfile, "mlc") |
|
123 |
+## } |
|
124 |
+ |
|
125 |
+## read.treetext_paml_rst<- function(rstfile) { |
|
126 |
+## read.treetext_paml(rstfile, "rst") |
|
127 |
+## } |
|
128 |
+ |
|
129 |
+## read.treetext_paml <- function(file, by) { |
|
130 |
+## ## works fine with baseml and codeml |
|
131 |
+## x <- readLines(file) |
|
132 |
+## tr.idx <- get_tree_index_paml(x) |
|
133 |
+ |
|
134 |
+## if ( by == "rst" ) { |
|
135 |
+## ii <- 1 |
|
136 |
+## } else if (by == "mlc") { |
|
137 |
+## ii <- 3 |
|
138 |
+## } else { |
|
139 |
+## stop("_by_ should be one of 'rst' or 'mlc'") |
|
140 |
+## } |
|
141 |
+ |
|
142 |
+## return(x[tr.idx][ii]) |
|
143 |
+## } |
|
144 |
+ |
|
145 |
+## read.phylo_paml_mlc <- function(mlcfile) { |
|
146 |
+## parent <- node <- label <- NULL |
|
147 |
+ |
|
148 |
+## mlc <- readLines(mlcfile) |
|
149 |
+## edge <- get_tree_edge_paml(mlc) |
|
150 |
+ |
|
151 |
+## tr.idx <- get_tree_index_paml(mlc) |
|
152 |
+## tr2 <- read.tree(text=mlc[tr.idx[2]]) |
|
153 |
+## tr3 <- read.tree(text=mlc[tr.idx[3]]) |
|
154 |
+ |
|
155 |
+## treeinfo <- as.data.frame(edge) |
|
156 |
+## colnames(treeinfo) <- c("parent", "node") |
|
157 |
+## treeinfo$length <- NA |
|
158 |
+## treeinfo$label <- NA |
|
159 |
+## treeinfo$isTip <- FALSE |
|
160 |
+## ntip <- Ntip(tr3) |
|
161 |
+## ii <- match(tr2$tip.label, treeinfo[, "node"]) |
|
162 |
+## treeinfo[ii, "label"] <- tr3$tip.label |
|
163 |
+## treeinfo[ii, "isTip"] <- TRUE |
|
164 |
+## ## jj <- match(1:ntip, tr3$edge[,2]) |
|
165 |
+## treeinfo[ii, "length"] <- tr2$edge.length[ii] ##tr3$edge.length[jj] |
|
166 |
+ |
|
167 |
+## root <- getRoot(tr3) ## always == (Ntip(tr3) + 1) |
|
168 |
+## currentNode <- treeinfo$label[ii] |
|
169 |
+## treeinfo.tr3 <- fortify(tr3) |
|
170 |
+ |
|
171 |
+## treeinfo$visited <- FALSE |
|
172 |
+## while(any(treeinfo$visited == FALSE)) { |
|
173 |
+## pNode <- c() |
|
174 |
+## for( kk in currentNode ) { |
|
175 |
+## i <- which(treeinfo$label == kk) |
|
176 |
+## treeinfo[i, "visited"] <- TRUE |
|
177 |
+## j <- which(treeinfo.tr3$label == kk) |
|
178 |
+## ip <- treeinfo[i, "parent"] |
|
179 |
+## if (ip != root) { |
|
180 |
+## ii <- which(treeinfo[, "node"] == ip) |
|
181 |
+## if (treeinfo$visited[ii] == FALSE) { |
|
182 |
+## jp <- treeinfo.tr3[j, "parent"] |
|
183 |
+## jj <- which(treeinfo.tr3[, "node"] == jp) |
|
184 |
+## treeinfo[ii, "label"] <- as.character(ip) |
|
185 |
+## treeinfo.tr3[jj, "label"] <- as.character(ip) |
|
186 |
+## treeinfo[ii, "length"] <- treeinfo.tr3[jj, "branch.length"] |
|
187 |
+## pNode <- c(pNode, ip) |
|
188 |
+## } |
|
189 |
+## treeinfo[ii, "visited"] <- TRUE |
|
190 |
+## } |
|
191 |
+ |
|
192 |
+## } |
|
193 |
+## currentNode <- unique(pNode) |
|
194 |
+## } |
|
195 |
+ |
|
196 |
+## phylo <- with(treeinfo, |
|
197 |
+## list(edge= cbind(as.numeric(parent), |
|
198 |
+## as.numeric(node)), |
|
199 |
+## edge.length = length, |
|
200 |
+## tip.label = label[order(node)][1:ntip], |
|
201 |
+## Nnode = tr3$Nnode, |
|
202 |
+## node.label = c(root, label[order(node)][-c(1:ntip)]) |
|
203 |
+## ) |
|
204 |
+## ) |
|
205 |
+## class(phylo) <- "phylo" |
|
206 |
+## phylo <- reorder.phylo(phylo, "cladewise") |
|
207 |
+## return(phylo) |
|
208 |
+## } |
|
209 |
+ |
|
210 |
+## ##' @importFrom ape reorder.phylo |
|
211 |
+## read.phylo_paml_rst <- function(rstfile) { |
|
212 |
+## parent <- node <- label <- NULL |
|
213 |
+ |
|
214 |
+## ## works fine with baseml and codeml |
|
215 |
+## rst <- readLines(rstfile) |
|
216 |
+## tr.idx <- get_tree_index_paml(rst) |
|
217 |
+ |
|
218 |
+## tr1 <- read.tree(text=rst[tr.idx][1]) |
|
219 |
+## tr3 <- read.tree(text=rst[tr.idx][3]) |
|
220 |
+ |
|
221 |
+## edge <- get_tree_edge_paml(rst) |
|
222 |
+ |
|
223 |
+## label=c(tr3$tip.label, tr3$node.label) |
|
224 |
+## root <- getRoot(tr3) |
|
225 |
+## label %<>% `[`(. != root) |
|
226 |
+ |
|
227 |
+## node.length <- data.frame(label=label, |
|
228 |
+## length=tr1$edge.length) |
|
229 |
+ |
|
230 |
+## ## node.length$node <- sub("_\\w+", "", node.length$label |
|
231 |
+## node.length$node <- gsub("^(\\d+)_.*", "\\1", node.length$label) |
|
232 |
+## node.length$label %<>% sub("\\d+_", "", .) |
|
233 |
+ |
|
234 |
+## edge <- as.data.frame(edge) |
|
235 |
+## colnames(edge) <- c("parent", "node") |
|
236 |
+ |
|
237 |
+## treeinfo <- merge(edge, node.length, by.x="node", by.y="node") |
|
238 |
+## edge2 <- treeinfo[, c("parent", "node")] |
|
239 |
+## edge2 %<>% as.matrix |
|
240 |
+ |
|
241 |
+## ntip <- Ntip(tr3) |
|
242 |
+ |
|
243 |
+## phylo <- with(treeinfo, |
|
244 |
+## list(edge= cbind(as.numeric(parent), |
|
245 |
+## as.numeric(node)), |
|
246 |
+## edge.length = length, |
|
247 |
+## tip.label = label[order(node)][1:ntip], |
|
248 |
+## Nnode = tr3$Nnode, |
|
249 |
+## node.label = c(root, label[order(node)][-c(1:ntip)]) |
|
250 |
+## ) |
|
251 |
+## ) |
|
252 |
+ |
|
253 |
+## class(phylo) <- "phylo" |
|
254 |
+## phylo <- reorder.phylo(phylo, "cladewise") |
|
255 |
+ |
|
256 |
+## return(phylo) |
|
257 |
+## } |
|
258 |
+ |
|
259 |
+## read.ancseq_paml_rst <- function(rstfile, by="Marginal") { |
|
260 |
+## ## works fine with baseml and codeml |
|
261 |
+## rst <- readLines(rstfile) |
|
262 |
+ |
|
263 |
+## by <- match.arg(by, c("Marginal", "Joint")) |
|
264 |
+## query <- paste(by, "reconstruction of ancestral sequences") |
|
265 |
+## idx <- grep(query, rst) |
|
266 |
+## if(length(idx) == 0) { |
|
267 |
+## ## in some paml setting, joint_ancseq are not available. |
|
268 |
+## return("") |
|
269 |
+## } |
|
270 |
+## si <- grep("reconstructed sequences", rst) |
|
271 |
+## idx <- si[which.min(abs(si-idx))] |
|
272 |
+ |
|
273 |
+## nl <- strsplit(rst[idx+2], split=" ") %>% unlist %<>% `[`(nzchar(.)) |
|
274 |
+## N <- as.numeric(nl[1]) |
|
275 |
+## seq.leng <- as.numeric(nl[2]) |
|
276 |
+ |
|
277 |
+## seqs <- rst[(idx+4):(idx+3+N)] |
|
278 |
+ |
|
279 |
+## seq.name <- character(N) |
|
280 |
+## res <- character(N) |
|
281 |
+## for (i in 1:N) { |
|
282 |
+## ss <- gsub(" ", "", seqs[i]) |
|
283 |
+## nn <- base::nchar(ss) |
|
284 |
+## res[i] <- substring(ss, nn-seq.leng+1,nn) |
|
285 |
+## seq.name[i] <- substring(ss, 1, nn-seq.leng) |
|
286 |
+## } |
|
287 |
+## seq.name <- sub("\\w+#", "", seq.name) |
|
288 |
+## names(res) <- seq.name |
|
289 |
+ |
|
290 |
+## return(res) |
|
291 |
+## } |
|
292 |
+ |
|
293 |
+ |
|
294 |
+## get_tree_index_paml <- function(paml) { |
|
295 |
+## grep("\\)[ \\.0-9]*;", paml) |
|
296 |
+## } |
|
297 |
+ |
|
298 |
+## get_tree_edge_index_paml <- function(paml) { |
|
299 |
+## grep("\\d+\\.\\.\\d+", paml) |
|
300 |
+## } |
|
301 |
+ |
|
302 |
+## get_tree_edge_paml <- function(paml) { |
|
303 |
+## tr.idx <- get_tree_index_paml(paml) |
|
304 |
+ |
|
305 |
+## edge.idx <- get_tree_edge_index_paml(paml) |
|
306 |
+## edge.idx <- edge.idx[edge.idx < tr.idx[3]] |
|
307 |
+ |
|
308 |
+## nodeNum <- strsplit(paml[edge.idx], split="\\.\\.") %>% |
|
309 |
+## unlist %>% strsplit(split="[[:space:]]") %>% unlist |
|
310 |
+ |
|
311 |
+## nodeNum %<>% `[`(nzchar(.)) |
|
312 |
+ |
|
313 |
+## edge <- matrix(as.numeric(nodeNum), ncol=2, byrow = TRUE) |
|
314 |
+ |
|
315 |
+## return(edge) |
|
316 |
+## } |
|
317 |
+ |
|
318 |
+## set.paml_rst_ <- function(object) { |
|
319 |
+## if (!is(object, "paml_rst")) { |
|
320 |
+## stop("object should be an instance of 'paml_rst'") |
|
321 |
+## } |
|
322 |
+## if (length(object@tip_seq) == 0) { |
|
323 |
+## return(object) |
|
324 |
+## } |
|
325 |
+ |
|
326 |
+## types <- get.fields(object) |
|
327 |
+## for (type in types) { |
|
328 |
+## value <- subs_paml_rst(object, type) |
|
329 |
+## if (all(is.na(value))) |
|
330 |
+## next |
|
331 |
+ |
|
332 |
+## if (type == "marginal_subs") { |
|
333 |
+## object@marginal_subs <- value |
|
334 |
+## } else if (type == "marginal_AA_subs") { |
|
335 |
+## object@marginal_AA_subs <- value |
|
336 |
+## } else if (type == "joint_subs") { |
|
337 |
+## object@joint_subs <- value |
|
338 |
+## } else if (type == "joint_AA_subs") { |
|
339 |
+## object@joint_AA_subs <- value |
|
340 |
+## } |
|
341 |
+## } |
|
342 |
+## return(object) |
|
343 |
+## } |
|
344 |
+ |
|
345 |
+ |
|
346 |
+## get.subs_paml_rst <- function(object, type) { |
|
347 |
+## if (!is(object, "paml_rst")) { |
|
348 |
+## stop("object should be an instance of 'paml_rst'") |
|
349 |
+## } |
|
350 |
+## if (type == "marginal_subs") { |
|
351 |
+## res <- object@marginal_subs |
|
352 |
+## } else if (type == "marginal_AA_subs") { |
|
353 |
+## res <- object@marginal_AA_subs |
|
354 |
+## } else if (type == "joint_subs") { |
|
355 |
+## res <- object@joint_subs |
|
356 |
+## } else if (type == "joint_AA_subs") { |
|
357 |
+## res <- object@joint_AA_subs |
|
358 |
+## } else { |
|
359 |
+## stop("type should be one of 'marginal_subs', |
|
360 |
+## 'marginal_AA_subs', 'joint_subs' or 'joint_AA_subs'. ") |
|
361 |
+## } |
|
362 |
+## return(res) |
|
363 |
+## } |
|
364 |
+ |
|
365 |
+## subs_paml_rst <- function(x, type, ...) { |
|
366 |
+## if (class(x) != "paml_rst") { |
|
367 |
+## stop("x should be an object of paml_rst...") |
|
368 |
+## } |
|
369 |
+## seqs <- x@tip_seq |
|
370 |
+## if (length(seqs) == 0) { |
|
371 |
+## stop("tip sequences is not available...") |
|
372 |
+## } |
|
373 |
+## if (type %in% c("marginal_subs", "marginal_AA_subs")) { |
|
374 |
+## ancseq <- x@marginal_ancseq |
|
375 |
+## ## seqs <- c(seqs, x@marginal_ancseq) |
|
376 |
+## } else if (type %in% c("joint_subs", "joint_AA_subs")){ |
|
377 |
+## ancseq <- x@joint_ancseq |
|
378 |
+## ## seqs <- c(seqs, x@joint_ancseq) |
|
379 |
+## } else { |
|
380 |
+## stop("type should be one of 'marginal_subs', |
|
381 |
+## 'marginal_AA_subs', 'joint_subs' or 'joint_AA_subs'. ") |
|
382 |
+## } |
|
383 |
+## if( type %in% c("marginal_subs", "joint_subs")) { |
|
384 |
+## translate <- FALSE |
|
385 |
+## } else { |
|
386 |
+## translate <- TRUE |
|
387 |
+## } |
|
388 |
+ |
|
389 |
+## if (all(ancseq == "")) { |
|
390 |
+## return(NA) |
|
391 |
+## } |
|
392 |
+## seqs <- c(seqs, ancseq) |
|
393 |
+## get.subs_(x@phylo, seqs, translate=translate, ...) |
|
394 |
+## } |
395 | 395 |
deleted file mode 100644 |
... | ... |
@@ -1,24 +0,0 @@ |
1 |
-##' parse output from phyloT |
|
2 |
-##' |
|
3 |
-##' |
|
4 |
-##' @title read.phyloT |
|
5 |
-##' @param file newick tree file |
|
6 |
-##' @param ... additional parameters to read.tree |
|
7 |
-##' @return phylo object |
|
8 |
-##' @references \url{http://phylot.biobyte.de/} |
|
9 |
-##' @export |
|
10 |
-##' @author guangchuang yu |
|
11 |
-read.phyloT <- function(file, ...) { |
|
12 |
- x <- readLines(file) |
|
13 |
- x <- paste0(gsub("\\s+", "", x), collapse="") |
|
14 |
- x <- sub("^\\(", "", x) %>% sub("\\);", ";", .) |
|
15 |
- res <- tryCatch(read.tree(text=x, ...), error=function(e) NULL) |
|
16 |
- if (is.null(res)) { |
|
17 |
- msg <- paste("`read.phyloT` only supports newick format with setting of", |
|
18 |
- "`Internal nodes` to `collapsed`, and `Polytomy` to `No`.", |
|
19 |
- "\nURL: http://phylot.biobyte.de/") |
|
20 |
- stop(msg) |
|
21 |
- } |
|
22 |
- return(res) |
|
23 |
-} |
|
24 |
- |
... | ... |
@@ -1,232 +1,232 @@ |
1 |
-##' @rdname plot-methods |
|
2 |
-##' @exportMethod plot |
|
3 |
-##' @param tip.label.size size of tip label |
|
4 |
-##' @param tip.label.hjust hjust of tip.label |
|
5 |
-##' @param annotation.size size of annotation |
|
6 |
-##' @param annotation.color color of annotation |
|
7 |
-##' @examples |
|
8 |
-##' file <- system.file("extdata/BEAST", "beast_mcc.tree", package="ggtree") |
|
9 |
-##' beast <- read.beast(file) |
|
10 |
-##' plot(beast, annotation="length_0.95_HPD", branch.length="none") + theme_tree() |
|
11 |
-setMethod("plot", signature( x= "beast"), |
|
12 |
- function(x, layout = "rectangular", |
|
13 |
- branch.length = "branch.length", |
|
14 |
- show.tip.label = TRUE, |
|
15 |
- tip.label.size = 4, |
|
16 |
- tip.label.hjust = -0.1, |
|
17 |
- position = "branch", |
|
18 |
- annotation = "rate", |
|
19 |
- ndigits = 2, |
|
20 |
- annotation.size = 3, |
|
21 |
- annotation.color = "black", |
|
22 |
- ...) { |
|
23 |
- |
|
24 |
- p <- ggtree(x, layout = layout, |
|
25 |
- branch.length = branch.length, |
|
26 |
- ndigits = ndigits, ...) |
|
27 |
- |
|
28 |
- if (show.tip.label) { |
|
29 |
- p <- p + geom_tiplab(hjust = tip.label.hjust, |
|
30 |
- size = tip.label.size) |
|
31 |
- offset <- ceiling(max(p$data$x)) * 0.1 |
|
32 |
- p <- p + xlim(-offset, max(p$data$x) + offset) |
|
33 |
- } |
|
34 |
- if (!is.null(annotation) && !is.na(annotation)) { |
|
35 |
- if (position == "node") { |
|
36 |
- position <- "x" |
|
37 |
- vjust <- 0 |
|
38 |
- hjust <- -.05 |
|
39 |
- } else { |
|
40 |
- vjust <- -.5 |
|
41 |
- hjust <- 0 |
|
42 |
- } |
|
43 |
- |
|
44 |
- p <- p + geom_text(aes_string(x=position, |
|
45 |
- label=annotation), |
|
46 |
- size=annotation.size, |
|
47 |
- vjust= vjust, hjust = hjust, |
|
48 |
- color=annotation.color) |
|
49 |
- } |
|
50 |
- p + theme_tree2() |
|
51 |
- }) |
|
52 |
- |
|
53 |
- |
|
54 |
-##' @rdname plot-methods |
|
55 |
-##' @exportMethod plot |
|
56 |
-##' @param layout layout |
|
57 |
-##' @param branch.length branch length |
|
58 |
-##' @param show.tip.label logical |
|
59 |
-##' @param position one of "branch" and "node" |
|
60 |
-##' @param annotation one of get.fields(x) |
|
61 |
-##' @param ndigits round digits |
|
62 |
-setMethod("plot", signature(x = "codeml_mlc"), |
|
63 |
- function(x, layout = "rectangular", |
|
64 |
- branch.length = "branch.length", |
|
65 |
- show.tip.label = TRUE, |
|
66 |
- tip.label.size = 4, |
|
67 |
- tip.label.hjust = -0.1, |
|
68 |
- position = "branch", |
|
69 |
- annotation = "dN_vs_dS", |
|
70 |
- annotation.size = 3, |
|
71 |
- annotation.color = "black", |
|
72 |
- ndigits = 2, |
|
73 |
- ... |
|
74 |
- ) { |
|
75 |
- |
|
76 |
- p <- ggtree(x, layout=layout, |
|
77 |
- branch.length=branch.length, |
|
78 |
- ndigits=ndigits, ...) |
|
79 |
- |
|
80 |
- if (show.tip.label) { |
|
81 |
- p <- p + geom_tiplab(hjust = tip.label.hjust, |
|
82 |
- size = tip.label.size) |
|
83 |
- } |
|
84 |
- plot.codeml_mlc_(p, position, annotation, |
|
85 |
- annotation.size, annotation.color) |
|
86 |
- }) |
|
87 |
- |
|
88 |
-##' @rdname plot-methods |
|
89 |
-##' @exportMethod plot |
|
90 |
-setMethod("plot", signature( x= "r8s"), |
|
91 |
- function(x, layout = "rectangular", |
|
92 |
- branch.length = "TREE", |
|
93 |
- show.tip.label = TRUE, |
|
94 |
- tip.label.size = 4, |
|
95 |
- tip.label.hjust = 0, |
|
96 |
- ...) { |
|
97 |
- |
|
98 |
- p <- ggtree(x, layout = layout, |
|
99 |
- branch.length = branch.length, ...) |
|
100 |
- |
|
101 |
- if (show.tip.label) { |
|
102 |
- p <- p + geom_tiplab(hjust = tip.label.hjust, |
|
103 |
- size = tip.label.size) |
|
104 |
- offset <- ceiling(max(p$data$x)) * 0.1 |
|
105 |
- p <- p + xlim(NA, max(p$data$x) + offset) |
|
106 |
- } |
|
107 |
- p + theme_tree2() |
|
108 |
- }) |
|
109 |
- |
|
110 |
-##' @rdname plot-methods |
|
111 |
-##' @exportMethod plot |
|
112 |
-setMethod("plot", signature( x= "raxml"), |
|
113 |
- function(x, layout = "rectangular", |
|
114 |
- branch.length = "branch.length", |
|
115 |
- show.tip.label = TRUE, |
|
116 |
- tip.label.size = 4, |
|
117 |
- tip.label.hjust = 0, |
|
118 |
- position = "node", |
|
119 |
- annotation = "bootstrap", |
|
120 |
- ndigits = 2, |
|
121 |
- annotation.size = 4, |
|
122 |
- annotation.color = "black", |
|
123 |
- ...) { |
|
124 |
- |
|
125 |
- p <- ggtree(x, layout = layout, |
|
126 |
- branch.length = branch.length, |
|
127 |
- ndigits = ndigits, ...) |
|
128 |
- |
|
129 |
- if (show.tip.label) { |
|
130 |
- p <- p + geom_tiplab(hjust = tip.label.hjust, |
|
131 |
- size = tip.label.size) |
|
132 |
- offset <- ceiling(max(p$data$x)) * 0.1 |
|
133 |
- p <- p + xlim(NA, max(p$data$x) + offset) |
|
134 |
- } |
|
135 |
- if (!is.null(annotation) && !is.na(annotation)) { |
|
136 |
- if (position == "node") { |
|
137 |
- position <- "x" |
|
138 |
- vjust <- 0 |
|
139 |
- hjust <- -.05 |
|
140 |
- } else { |
|
141 |
- vjust <- -.5 |
|
142 |
- hjust <- 0 |
|
143 |
- } |
|
144 |
- |
|
145 |
- p <- p + geom_text(aes_string(x=position, |
|
146 |
- label=annotation), |
|
147 |
- size=annotation.size, |
|
148 |
- vjust= vjust, hjust = hjust, |
|
149 |
- color=annotation.color) |
|
150 |
- } |
|
151 |
- p + theme_tree2() |
|
152 |
- }) |
|
153 |
- |
|
154 |
- |
|
155 |
-##' @rdname plot-methods |
|
156 |
-##' @exportMethod plot |
|
157 |
-setMethod("plot", signature(x = "paml_rst"), |
|
158 |