... | ... |
@@ -17,6 +17,18 @@ get_layout <- function(tree_view = NULL) { |
17 | 17 |
return(layout) |
18 | 18 |
} |
19 | 19 |
|
20 |
+build_new_plot_env <- function(env){ |
|
21 |
+ newenv <- list2env( |
|
22 |
+ x = as.list( |
|
23 |
+ env, |
|
24 |
+ all.names = TRUE |
|
25 |
+ ), |
|
26 |
+ parent = parent.env(env) |
|
27 |
+ ) |
|
28 |
+ attributes(newenv) <- attributes(env) |
|
29 |
+ return(newenv) |
|
30 |
+} |
|
31 |
+ |
|
20 | 32 |
reverse.treeview <- function(tv) { |
21 | 33 |
tv$data <- reverse.treeview.data(tv$data) |
22 | 34 |
return(tv) |
... | ... |
@@ -117,6 +117,17 @@ getCols <- function (n) { |
117 | 117 |
grDevices::colorRampPalette(col3)(n) |
118 | 118 |
} |
119 | 119 |
|
120 |
+message_wrap <- function(...){ |
|
121 |
+ msg <- .return_wrap(...) |
|
122 |
+ message(msg) |
|
123 |
+} |
|
124 |
+ |
|
125 |
+.return_wrap <- function(...){ |
|
126 |
+ msg <- paste(..., collapse = "", sep = "") |
|
127 |
+ wrapped <- strwrap(msg, width = getOption("width") - 2) %>% |
|
128 |
+ glue::glue_collapse(., "\n", last = "\n") |
|
129 |
+ wrapped |
|
130 |
+} |
|
120 | 131 |
|
121 | 132 |
## |
122 | 133 |
## |
... | ... |
@@ -162,56 +162,3 @@ getCols <- function (n) { |
162 | 162 |
## tree$edge.length <- c(tree$edge.length, rep(0, ii)) |
163 | 163 |
## return(tree) |
164 | 164 |
## } |
165 |
- |
|
166 |
-build_cladeids_df <- function(trdf, nodeids){ |
|
167 |
- dat <- lapply(seq_along(nodeids), function(i){ |
|
168 |
- ids <- getSubtree.df(trdf, nodeids[i]) |
|
169 |
- dt <- trdf[trdf$node %in% ids,] |
|
170 |
- dt$clade_root_node <- nodeids[i] |
|
171 |
- return(dt) |
|
172 |
- }) |
|
173 |
- return(do.call("rbind", dat)) |
|
174 |
-} |
|
175 |
- |
|
176 |
-build_cladeids_df2 <- function(trdf, nodeids){ |
|
177 |
- flagreverse <- check_reverse(data=trdf) |
|
178 |
- dat <- lapply(nodeids, function(i)get_clade_position_(data=trdf, node=i, reverse=flagreverse)) |
|
179 |
- dat <- do.call("rbind", dat) |
|
180 |
- dat$clade_root_node <- nodeids |
|
181 |
- return(dat) |
|
182 |
-} |
|
183 |
- |
|
184 |
-check_reverse <- function(data){ |
|
185 |
- tiptab <- data[data$isTip,] |
|
186 |
- nodetab <- data[match(tiptab$parent, data$node),] |
|
187 |
- if (all(tiptab$x < nodetab$x)){ |
|
188 |
- return(TRUE) |
|
189 |
- }else{ |
|
190 |
- return(FALSE) |
|
191 |
- } |
|
192 |
-} |
|
193 |
- |
|
194 |
- |
|
195 |
- |
|
196 |
-choose_hilight_layer <- function(object, type){ |
|
197 |
- if (type=="encircle"){ |
|
198 |
- if (!is.null(object$mapping)){ |
|
199 |
- object$mapping <- modifyList(object$mapping, aes_(x=~x, y=~y, clade_root_node=~clade_root_node)) |
|
200 |
- }else{ |
|
201 |
- object$mapping <- aes_(x=~x, y=~y, clade_root_node=~clade_root_node) |
|
202 |
- } |
|
203 |
- params <- c(list(data=object$data, mapping=object$mapping), object$params) |
|
204 |
- ly <- do.call("geom_hilight_encircle2", params) |
|
205 |
- }else{ |
|
206 |
- if (!is.null(object$mapping)){ |
|
207 |
- object$mapping <- modifyList(object$mapping, aes_(xmin=~xmin, xmax=~xmax, |
|
208 |
- ymin=~ymin, ymax=~ymax, |
|
209 |
- clade_root_node=~clade_root_node)) |
|
210 |
- }else{ |
|
211 |
- object$mapping <- aes_(xmin=~xmin, xmax=~xmax, ymin=~ymin, ymax=~ymax, clade_root_node=~clade_root_node) |
|
212 |
- } |
|
213 |
- params <- c(list(data=object$data, mapping=object$mapping), object$params) |
|
214 |
- ly <- do.call("geom_hilight_rect2", params) |
|
215 |
- } |
|
216 |
- return (ly) |
|
217 |
-} |
... | ... |
@@ -174,12 +174,25 @@ build_cladeids_df <- function(trdf, nodeids){ |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
build_cladeids_df2 <- function(trdf, nodeids){ |
177 |
- dat <- lapply(nodeids, function(i)get_clade_position_(data=trdf, node=i)) |
|
177 |
+ flagreverse <- check_reverse(data=trdf) |
|
178 |
+ dat <- lapply(nodeids, function(i)get_clade_position_(data=trdf, node=i, reverse=flagreverse)) |
|
178 | 179 |
dat <- do.call("rbind", dat) |
179 | 180 |
dat$clade_root_node <- nodeids |
180 | 181 |
return(dat) |
181 | 182 |
} |
182 | 183 |
|
184 |
+check_reverse <- function(data){ |
|
185 |
+ tiptab <- data[data$isTip,] |
|
186 |
+ nodetab <- data[match(tiptab$parent, data$node),] |
|
187 |
+ if (all(tiptab$x < nodetab$x)){ |
|
188 |
+ return(TRUE) |
|
189 |
+ }else{ |
|
190 |
+ return(FALSE) |
|
191 |
+ } |
|
192 |
+} |
|
193 |
+ |
|
194 |
+ |
|
195 |
+ |
|
183 | 196 |
choose_hilight_layer <- function(object, type){ |
184 | 197 |
if (type=="encircle"){ |
185 | 198 |
if (!is.null(object$mapping)){ |
... | ... |
@@ -8,6 +8,15 @@ get_tree_view <- function(tree_view) { |
8 | 8 |
return(tree_view) |
9 | 9 |
} |
10 | 10 |
|
11 |
+get_layout <- function(tree_view = NULL) { |
|
12 |
+ plot <- get_tree_view(tree_view) |
|
13 |
+ layout <- get("layout", envir = plot$plot_env) |
|
14 |
+ if (!is(layout, "character")) { |
|
15 |
+ layout <- attr(plot$data, "layout") |
|
16 |
+ } |
|
17 |
+ return(layout) |
|
18 |
+} |
|
19 |
+ |
|
11 | 20 |
reverse.treeview <- function(tv) { |
12 | 21 |
tv$data <- reverse.treeview.data(tv$data) |
13 | 22 |
return(tv) |
... | ... |
@@ -154,7 +154,6 @@ getCols <- function (n) { |
154 | 154 |
## return(tree) |
155 | 155 |
## } |
156 | 156 |
|
157 |
- |
|
158 | 157 |
build_cladeids_df <- function(trdf, nodeids){ |
159 | 158 |
dat <- lapply(seq_along(nodeids), function(i){ |
160 | 159 |
ids <- getSubtree.df(trdf, nodeids[i]) |
... | ... |
@@ -165,3 +164,32 @@ build_cladeids_df <- function(trdf, nodeids){ |
165 | 164 |
return(do.call("rbind", dat)) |
166 | 165 |
} |
167 | 166 |
|
167 |
+build_cladeids_df2 <- function(trdf, nodeids){ |
|
168 |
+ dat <- lapply(nodeids, function(i)get_clade_position_(data=trdf, node=i)) |
|
169 |
+ dat <- do.call("rbind", dat) |
|
170 |
+ dat$clade_root_node <- nodeids |
|
171 |
+ return(dat) |
|
172 |
+} |
|
173 |
+ |
|
174 |
+choose_hilight_layer <- function(object, type){ |
|
175 |
+ if (type=="encircle"){ |
|
176 |
+ if (!is.null(object$mapping)){ |
|
177 |
+ object$mapping <- modifyList(object$mapping, aes_(x=~x, y=~y, clade_root_node=~clade_root_node)) |
|
178 |
+ }else{ |
|
179 |
+ object$mapping <- aes_(x=~x, y=~y, clade_root_node=~clade_root_node) |
|
180 |
+ } |
|
181 |
+ params <- c(list(data=object$data, mapping=object$mapping), object$params) |
|
182 |
+ ly <- do.call("geom_hilight_encircle2", params) |
|
183 |
+ }else{ |
|
184 |
+ if (!is.null(object$mapping)){ |
|
185 |
+ object$mapping <- modifyList(object$mapping, aes_(xmin=~xmin, xmax=~xmax, |
|
186 |
+ ymin=~ymin, ymax=~ymax, |
|
187 |
+ clade_root_node=~clade_root_node)) |
|
188 |
+ }else{ |
|
189 |
+ object$mapping <- aes_(xmin=~xmin, xmax=~xmax, ymin=~ymin, ymax=~ymax, clade_root_node=~clade_root_node) |
|
190 |
+ } |
|
191 |
+ params <- c(list(data=object$data, mapping=object$mapping), object$params) |
|
192 |
+ ly <- do.call("geom_hilight_rect2", params) |
|
193 |
+ } |
|
194 |
+ return (ly) |
|
195 |
+} |
... | ... |
@@ -155,5 +155,13 @@ getCols <- function (n) { |
155 | 155 |
## } |
156 | 156 |
|
157 | 157 |
|
158 |
- |
|
158 |
+build_cladeids_df <- function(trdf, nodeids){ |
|
159 |
+ dat <- lapply(seq_along(nodeids), function(i){ |
|
160 |
+ ids <- getSubtree.df(trdf, nodeids[i]) |
|
161 |
+ dt <- trdf[trdf$node %in% ids,] |
|
162 |
+ dt$clade_root_node <- nodeids[i] |
|
163 |
+ return(dt) |
|
164 |
+ }) |
|
165 |
+ return(do.call("rbind", dat)) |
|
166 |
+} |
|
159 | 167 |
|
... | ... |
@@ -74,21 +74,23 @@ roundDigit <- function(d) { |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
|
77 |
-## . function was from plyr package |
|
78 |
-##' capture name of variable |
|
79 |
-##' |
|
80 |
-##' @rdname dotFun |
|
81 |
-##' @export |
|
82 |
-##' @title . |
|
83 |
-##' @param ... expression |
|
84 |
-##' @param .env environment |
|
85 |
-##' @return expression |
|
86 |
-##' @examples |
|
87 |
-##' x <- 1 |
|
88 |
-##' eval(.(x)[[1]]) |
|
89 |
-. <- function (..., .env = parent.frame()) { |
|
90 |
- structure(as.list(match.call()[-1]), env = .env, class = "quoted") |
|
91 |
-} |
|
77 |
+globalVariables(".") |
|
78 |
+ |
|
79 |
+## ## . function was from plyr package |
|
80 |
+## ##' capture name of variable |
|
81 |
+## ##' |
|
82 |
+## ##' @rdname dotFun |
|
83 |
+## ##' @export |
|
84 |
+## ##' @title . |
|
85 |
+## ##' @param ... expression |
|
86 |
+## ##' @param .env environment |
|
87 |
+## ##' @return expression |
|
88 |
+## ##' @examples |
|
89 |
+## ##' x <- 1 |
|
90 |
+## ##' eval(.(x)[[1]]) |
|
91 |
+## . <- function (..., .env = parent.frame()) { |
|
92 |
+## structure(as.list(match.call()[-1]), env = .env, class = "quoted") |
|
93 |
+## } |
|
92 | 94 |
|
93 | 95 |
|
94 | 96 |
## from ChIPseeker |
... | ... |
@@ -93,7 +93,7 @@ roundDigit <- function(d) { |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
## from ChIPseeker |
96 |
-##' @importFrom grDevices colorRampPalette |
|
96 |
+## @importFrom grDevices colorRampPalette |
|
97 | 97 |
getCols <- function (n) { |
98 | 98 |
col <- c("#8dd3c7", "#ffffb3", "#bebada", "#fb8072", "#80b1d3", |
99 | 99 |
"#fdb462", "#b3de69", "#fccde5", "#d9d9d9", "#bc80bd", |
... | ... |
@@ -104,12 +104,9 @@ getCols <- function (n) { |
104 | 104 |
col3 <- c("#a6cee3", "#1f78b4", "#b2df8a", "#33a02c", "#fb9a99", |
105 | 105 |
"#e31a1c", "#fdbf6f", "#ff7f00", "#cab2d6", "#6a3d9a", |
106 | 106 |
"#ffff99", "#b15928") |
107 |
- colorRampPalette(col3)(n) |
|
107 |
+ grDevices::colorRampPalette(col3)(n) |
|
108 | 108 |
} |
109 | 109 |
|
110 |
-##' @importFrom rvcheck get_fun_from_pkg |
|
111 |
-hist <- get_fun_from_pkg("graphics", "hist") |
|
112 |
- |
|
113 | 110 |
|
114 | 111 |
## |
115 | 112 |
## |
... | ... |
@@ -111,6 +111,50 @@ getCols <- function (n) { |
111 | 111 |
hist <- get_fun_from_pkg("graphics", "hist") |
112 | 112 |
|
113 | 113 |
|
114 |
+## |
|
115 |
+## |
|
116 |
+## use ape::multi2di |
|
117 |
+## |
|
118 |
+## |
|
119 |
+## ##' convert polytomy to binary tree |
|
120 |
+## ##' |
|
121 |
+## ##' as.binary method for \code{phylo} object |
|
122 |
+## ##' @rdname as.binary |
|
123 |
+## ##' @return binary tree |
|
124 |
+## ##' @method as.binary phylo |
|
125 |
+## ##' @importFrom ape is.binary.tree |
|
126 |
+## ##' @export |
|
127 |
+## ##' @author Guangchuang Yu \url{http://ygc.name} |
|
128 |
+## ##' @examples |
|
129 |
+## ##' require(ape) |
|
130 |
+## ##' tr <- read.tree(text="((A, B, C), D);") |
|
131 |
+## ##' is.binary.tree(tr) |
|
132 |
+## ##' tr2 <- as.binary(tr) |
|
133 |
+## ##' is.binary.tree(tr2) |
|
134 |
+## as.binary.phylo <- function(tree, ...) { |
|
135 |
+## if(is.binary.tree(tree)) { |
|
136 |
+## message("The input tree is already binary...") |
|
137 |
+## invisible(tree) |
|
138 |
+## } |
|
139 |
+## polyNode <- tree$edge[,1] %>% table %>% '>'(2) %>% |
|
140 |
+## which %>% names %>% as.numeric |
|
141 |
+## N <- getNodeNum(tree) |
|
142 |
+## ii <- 0 |
|
143 |
+## for (pn in polyNode) { |
|
144 |
+## idx <- which(tree$edge[,1] == pn) |
|
145 |
+## while(length(idx) >2) { |
|
146 |
+## ii <- ii + 1 |
|
147 |
+## newNode <- N+ii |
|
148 |
+## tree$edge[idx[-1],1] <- newNode |
|
149 |
+## newEdge <- matrix(c(tree$edge[idx[1],1], newNode), ncol=2) |
|
150 |
+## tree$edge <- rbind(tree$edge, newEdge) |
|
151 |
+## idx <- idx[-1] |
|
152 |
+## } |
|
153 |
+## } |
|
154 |
+## tree$Nnode <- tree$Nnode+ii |
|
155 |
+## tree$edge.length <- c(tree$edge.length, rep(0, ii)) |
|
156 |
+## return(tree) |
|
157 |
+## } |
|
114 | 158 |
|
115 | 159 |
|
116 | 160 |
|
... | ... |
@@ -9,240 +9,6 @@ get_tree_view <- function(tree_view) { |
9 | 9 |
return(tree_view) |
10 | 10 |
} |
11 | 11 |
|
12 |
- |
|
13 |
- |
|
14 |
-## has.field <- function(tree_object, field) { |
|
15 |
-## if ( ! field %in% get.fields(tree_object) ) { |
|
16 |
-## return(FALSE) |
|
17 |
-## } |
|
18 |
- |
|
19 |
-## if (is(tree_object, "codeml")) { |
|
20 |
-## is_codeml <- TRUE |
|
21 |
-## tree <- tree_object@rst |
|
22 |
-## } else { |
|
23 |
-## is_codeml <- FALSE |
|
24 |
-## tree <- tree_object |
|
25 |
-## } |
|
26 |
- |
|
27 |
-## if (.hasSlot(tree, field)) { |
|
28 |
-## has_slot <- TRUE |
|
29 |
-## } else { |
|
30 |
-## has_slot <- FALSE |
|
31 |
-## } |
|
32 |
- |
|
33 |
-## if (has_slot == FALSE) { |
|
34 |
-## if (has.extraInfo(tree_object) == FALSE) { |
|
35 |
-## return(FALSE) |
|
36 |
-## } |
|
37 |
- |
|
38 |
-## if (nrow(tree_object@extraInfo) == 0) { |
|
39 |
-## return(FALSE) |
|
40 |
-## } |
|
41 |
- |
|
42 |
-## if (!field %in% colnames(tree_object@extraInfo)) { |
|
43 |
-## return(FALSE) |
|
44 |
-## } |
|
45 |
-## } |
|
46 |
-## res <- TRUE |
|
47 |
-## attr(res, "has_slot") <- has_slot |
|
48 |
-## attr(res, "is_codeml") <- is_codeml |
|
49 |
-## return(res) |
|
50 |
-## } |
|
51 |
- |
|
52 |
-## append_extraInfo <- function(df, object) { |
|
53 |
-## if (has.extraInfo(object)) { |
|
54 |
-## info <- object@extraInfo |
|
55 |
-## if ("parent" %in% colnames(info)) { |
|
56 |
-## res <- merge(df, info, by.x=c("node", "parent"), by.y=c("node", "parent")) |
|
57 |
-## } else { |
|
58 |
-## res <- merge(df, info, by.x="node", by.y="node") |
|
59 |
-## } |
|
60 |
-## } else { |
|
61 |
-## return(df) |
|
62 |
-## } |
|
63 |
- |
|
64 |
-## i <- order(res$node, decreasing = FALSE) |
|
65 |
-## res <- res[i,] |
|
66 |
-## return(res) |
|
67 |
-## } |
|
68 |
- |
|
69 |
-## get.fields.tree <- function(object) { |
|
70 |
-## if (is(object, "codeml")) { |
|
71 |
-## fields <- c(get.fields(object@rst), |
|
72 |
-## get.fields(object@mlc)) |
|
73 |
-## fields <- unique(fields) |
|
74 |
-## } else { |
|
75 |
-## fields <- object@fields |
|
76 |
-## } |
|
77 |
- |
|
78 |
-## if (has.slot(object, "extraInfo")) { |
|
79 |
-## extraInfo <- object@extraInfo |
|
80 |
-## if (nrow(extraInfo) > 0) { |
|
81 |
-## cn <- colnames(extraInfo) |
|
82 |
-## i <- match(c("x", "y", "isTip", "node", "parent", "label", "branch", "branch.length"), cn) |
|
83 |
-## i <- i[!is.na(i)] |
|
84 |
-## fields %<>% c(cn[-i]) |
|
85 |
-## } |
|
86 |
-## } |
|
87 |
-## return(fields) |
|
88 |
-## } |
|
89 |
- |
|
90 |
-## print_fields <- function(object, len=5) { |
|
91 |
-## fields <- get.fields(object) |
|
92 |
-## n <- length(fields) |
|
93 |
-## i <- floor(n/len) |
|
94 |
-## for (j in 0:i) { |
|
95 |
-## ii <- 1:len + len * j |
|
96 |
-## if (j == i) { |
|
97 |
-## x <- n %% len |
|
98 |
-## if (x == 0) { |
|
99 |
-## ii <- NULL |
|
100 |
-## } else { |
|
101 |
-## ii <- ii[1:x] |
|
102 |
-## } |
|
103 |
-## } |
|
104 |
- |
|
105 |
-## if (!is.null(ii)) { |
|
106 |
-## cat("\t", paste0("'", |
|
107 |
-## paste(fields[ii], collapse="',\t'"), |
|
108 |
-## "'") |
|
109 |
-## ) |
|
110 |
-## } |
|
111 |
-## if ( j == i) { |
|
112 |
-## cat(".\n") |
|
113 |
-## } else { |
|
114 |
-## cat(",\n") |
|
115 |
-## } |
|
116 |
-## } |
|
117 |
-## } |
|
118 |
- |
|
119 |
-## plot.subs <- function(x, layout, show.tip.label, |
|
120 |
-## tip.label.size, |
|
121 |
-## tip.label.hjust, |
|
122 |
-## position, annotation, |
|
123 |
-## annotation.color = "black", |
|
124 |
-## annotation.size=3, ...) { |
|
125 |
- |
|
126 |
-## p <- ggtree(x, layout=layout, ...) |
|
127 |
-## if (show.tip.label) { |
|
128 |
-## p <- p + geom_tiplab(hjust = tip.label.hjust, |
|
129 |
-## size = tip.label.size) |
|
130 |
-## } |
|
131 |
-## if (!is.null(annotation) && !is.na(annotation)) { |
|
132 |
-## p <- p + geom_text(aes_string(x=position, label=annotation), |
|
133 |
-## size=annotation.size, |
|
134 |
-## color=annotation.color, vjust=-.5) |
|
135 |
-## } |
|
136 |
-## p + theme_tree2() |
|
137 |
-## } |
|
138 |
- |
|
139 |
-## .add_new_line <- function(res) { |
|
140 |
-## ## res <- paste0(strwrap(res, 50), collapse="\n") |
|
141 |
-## ## res %<>% gsub("\\s/\n", "\n", .) %>% gsub("\n/\\s", "\n", .) |
|
142 |
-## if (nchar(res) > 50) { |
|
143 |
-## idx <- gregexpr("/", res)[[1]] |
|
144 |
-## i <- idx[floor(length(idx)/2)] |
|
145 |
-## res <- paste0(substring(res, 1, i-1), "\n", substring(res, i+1)) |
|
146 |
-## } |
|
147 |
-## return(res) |
|
148 |
-## } |
|
149 |
- |
|
150 |
-## get.subs_ <- function(tree, fasta, translate=TRUE, removeGap=TRUE) { |
|
151 |
-## N <- getNodeNum(tree) |
|
152 |
-## node <- 1:N |
|
153 |
-## parent <- sapply(node, getParent, tr=tree) |
|
154 |
-## label <- getNodeName(tree) |
|
155 |
-## subs <- sapply(seq_along(node), function(i) { |
|
156 |
-## if (i == getRoot(tree)) { |
|
157 |
-## return(NA) |
|
158 |
-## } |
|
159 |
-## res <- getSubsLabel(fasta, label[parent[i]], label[i], translate, removeGap) |
|
160 |
-## if (is.null(res)) { |
|
161 |
-## return('') |
|
162 |
-## } |
|
163 |
-## .add_new_line(res) |
|
164 |
-## }) |
|
165 |
- |
|
166 |
-## dd <- data.frame(node=node, parent=parent, label=label, subs=subs) |
|
167 |
-## dd <- dd[dd$parent != 0,] |
|
168 |
-## dd <- dd[, -c(1,2)] |
|
169 |
-## dd[,1] <- as.character(dd[,1]) |
|
170 |
-## dd[,2] <- as.character(dd[,2]) |
|
171 |
-## return(dd) |
|
172 |
-## } |
|
173 |
- |
|
174 |
-## getSubsLabel <- function(seqs, A, B, translate, removeGap) { |
|
175 |
-## seqA <- seqs[A] |
|
176 |
-## seqB <- seqs[B] |
|
177 |
- |
|
178 |
-## if (nchar(seqA) != nchar(seqB)) { |
|
179 |
-## stop("seqA should have equal length to seqB") |
|
180 |
-## } |
|
181 |
- |
|
182 |
-## if (translate == TRUE) { |
|
183 |
-## AA <- seqA %>% seq2codon %>% codon2AA |
|
184 |
-## BB <- seqB %>% seq2codon %>% codon2AA |
|
185 |
-## } else { |
|
186 |
-## ## strsplit is faster than substring |
|
187 |
-## ## |
|
188 |
-## ## n <- nchar(seqA) ## should equals to nchar(seqB) |
|
189 |
-## ## AA <- substring(seqA, 1:n, 1:n) |
|
190 |
-## ## BB <- substring(seqB, 1:n, 1:n) |
|
191 |
-## AA <- strsplit(seqA, split="") %>% unlist |
|
192 |
-## BB <- strsplit(seqB, split="") %>% unlist |
|
193 |
-## } |
|
194 |
- |
|
195 |
-## ii <- which(AA != BB) |
|
196 |
- |
|
197 |
-## if (removeGap == TRUE) { |
|
198 |
-## if (length(ii) > 0 && translate == TRUE) { |
|
199 |
-## ii <- ii[AA[ii] != "X" & BB[ii] != "X"] |
|
200 |
-## } |
|
201 |
- |
|
202 |
-## if (length(ii) > 0 && translate == FALSE) { |
|
203 |
-## ii <- ii[AA[ii] != "-" & BB[ii] != "-"] |
|
204 |
-## } |
|
205 |
-## } |
|
206 |
- |
|
207 |
-## if (length(ii) == 0) { |
|
208 |
-## return(NULL) |
|
209 |
-## } |
|
210 |
- |
|
211 |
-## res <- paste(AA[ii], ii, BB[ii], sep="", collapse=" / ") |
|
212 |
-## return(res) |
|
213 |
-## } |
|
214 |
- |
|
215 |
-## seq2codon <- function(x) { |
|
216 |
-## substring(x, first=seq(1, nchar(x)-2, 3), last=seq(3, nchar(x), 3)) |
|
217 |
-## } |
|
218 |
- |
|
219 |
-## ## @importFrom Biostrings GENETIC_CODE |
|
220 |
-## codon2AA <- function(codon) { |
|
221 |
-## ## a genetic code name vector |
|
222 |
-## GENETIC_CODE <- get_fun_from_pkg("Biostrings", "GENETIC_CODE") |
|
223 |
-## aa <- GENETIC_CODE[codon] |
|
224 |
-## aa[is.na(aa)] <- "X" |
|
225 |
-## return(aa) |
|
226 |
-## } |
|
227 |
- |
|
228 |
- |
|
229 |
-## getPhyInfo <- function(phy) { |
|
230 |
-## line1 <- readLines(phy, n=1) |
|
231 |
-## res <- strsplit(line1, split="\\s")[[1]] |
|
232 |
-## res <- res[res != ""] |
|
233 |
- |
|
234 |
-## return(list(num=as.numeric(res[1]), width=as.numeric(res[2]))) |
|
235 |
-## } |
|
236 |
- |
|
237 |
-## get_seqtype <- function(seq) { |
|
238 |
-## if (length(grep("[^-ACGT]+", seq[1])) == 0) { |
|
239 |
-## seq_type = "NT" ## NucleoTide |
|
240 |
-## } else { |
|
241 |
-## seq_type = "AA" ## Amino Acid |
|
242 |
-## } |
|
243 |
-## return(seq_type) |
|
244 |
-## } |
|
245 |
- |
|
246 | 12 |
reverse.treeview <- function(tv) { |
247 | 13 |
tv$data <- reverse.treeview.data(tv$data) |
248 | 14 |
return(tv) |
... | ... |
@@ -256,91 +22,6 @@ reverse.treeview.data <- function(df) { |
256 | 22 |
} |
257 | 23 |
|
258 | 24 |
|
259 |
-## jplace_treetext_to_phylo <- function(tree.text) { |
|
260 |
-## ## move edge label to node label separate by @ |
|
261 |
-## tr <- gsub('(:[0-9\\.eE\\+\\-]+)\\{(\\d+)\\}', '\\@\\2\\1', tree.text) |
|
262 |
-## phylo <- read.tree(text=tr) |
|
263 |
-## if (length(grep('@', phylo$tip.label)) > 0) { |
|
264 |
-## phylo$node.label[1] %<>% gsub("(.*)\\{(\\d+)\\}", "\\1@\\2", .) |
|
265 |
-## tip.edgeNum <- as.numeric(gsub("[^@]*@(\\d*)", "\\1",phylo$tip.label)) |
|
266 |
-## node.edgeNum <- as.numeric(gsub("[^@]*@(\\d*)", "\\1",phylo$node.label)) |
|
267 |
-## phylo$tip.label %<>% gsub("@\\d+", "", .) |
|
268 |
-## phylo$node.label %<>% gsub("@\\d+", "", .) |
|
269 |
-## if (all(phylo$node.label == "")) { |
|
270 |
-## phylo$node.label <- NULL |
|
271 |
-## } |
|
272 |
- |
|
273 |
-## N <- getNodeNum(phylo) |
|
274 |
-## edgeNum.df <- data.frame(node=1:N, edgeNum=c(tip.edgeNum, node.edgeNum)) |
|
275 |
-## ## root node is not encoded with edge number |
|
276 |
-## edgeNum.df <- edgeNum.df[!is.na(edgeNum.df[,2]),] |
|
277 |
-## attr(phylo, "edgeNum") <- edgeNum.df |
|
278 |
-## } |
|
279 |
- |
|
280 |
-## ## using :edge_length{edge_num} to match edge_num to node_num |
|
281 |
-## ## this is not a good idea since there may exists identical edge_length. |
|
282 |
-## ## but we can use it to verify our method. |
|
283 |
-## ## |
|
284 |
-## ## en.matches <- gregexpr(":[0-9\\.eE\\+\\-]+\\{\\d+\\}", tree.text) |
|
285 |
-## ## matches <- en.matches[[1]] |
|
286 |
-## ## match.pos <- as.numeric(matches) |
|
287 |
-## ## match.len <- attr(matches, 'match.length') |
|
288 |
- |
|
289 |
-## ## edgeLN <- substring(tree.text, match.pos+1, match.pos+match.len-2) |
|
290 |
-## ## edgeLN.df <- data.frame(length=as.numeric(gsub("\\{.+", "", edgeLN)), |
|
291 |
-## ## edgeNum = as.numeric(gsub(".+\\{", "", edgeLN))) |
|
292 |
- |
|
293 |
-## ## xx <- merge(edgeLN.df, edgeNum.df, by.x="node", by.y="node") |
|
294 |
- |
|
295 |
-## return(phylo) |
|
296 |
-## } |
|
297 |
- |
|
298 |
-extract.treeinfo.jplace <- function(object, layout="rectangular", ladderize=TRUE, right=FALSE, ...) { |
|
299 |
- |
|
300 |
- tree <- get.tree(object) |
|
301 |
- |
|
302 |
- df <- fortify.phylo(tree, layout=layout, ladderize=ladderize, right=right, ...) |
|
303 |
- |
|
304 |
- edgeNum.df <- attr(tree, "edgeNum") |
|
305 |
- if (!is.null(edgeNum.df)) { |
|
306 |
- df2 <- merge(df, edgeNum.df, by.x="node", by.y="node", all.x=TRUE) |
|
307 |
- df <- df2[match(df[, "node"], df2[, "node"]),] |
|
308 |
- } |
|
309 |
- |
|
310 |
- attr(df, "ladderize") <- ladderize |
|
311 |
- attr(df, "right") <- right |
|
312 |
- return(df) |
|
313 |
-} |
|
314 |
- |
|
315 |
-## ## convert edge number to node number for EPA/pplacer output |
|
316 |
-## edgeNum2nodeNum <- function(jp, edgeNum) { |
|
317 |
-## edges <- attr(jp@phylo, "edgeNum") |
|
318 |
- |
|
319 |
-## idx <- which(edges$edgeNum == edgeNum) |
|
320 |
-## if (length(idx) == 0) { |
|
321 |
-## return(NA) |
|
322 |
-## } |
|
323 |
- |
|
324 |
-## edges[idx, "node"] |
|
325 |
-## } |
|
326 |
- |
|
327 |
-## is.character_beast <- function(stats3, cn) { |
|
328 |
-## for (i in 1:nrow(stats3)) { |
|
329 |
-## if ( is.na(stats3[i,cn]) ) { |
|
330 |
-## next |
|
331 |
-## } else { |
|
332 |
-## ## res <- grepl("[a-df-zA-DF-Z]+", unlist(stats3[i, cn])) |
|
333 |
-## ## return(all(res == TRUE)) |
|
334 |
-## res <- grepl("^[0-9\\.eE-]+$", unlist(stats3[i, cn])) |
|
335 |
-## return(all(res == FALSE)) |
|
336 |
-## } |
|
337 |
-## } |
|
338 |
-## return(FALSE) |
|
339 |
-## } |
|
340 |
- |
|
341 |
- |
|
342 |
- |
|
343 |
- |
|
344 | 25 |
color_scale <- function(c1="grey", c2="red", n=100) { |
345 | 26 |
pal <- colorRampPalette(c(c1, c2)) |
346 | 27 |
colors <- pal(n) |
... | ... |
@@ -384,22 +65,6 @@ is.tree_attribute_ <- function(p, var) { |
384 | 65 |
|
385 | 66 |
|
386 | 67 |
|
387 |
- |
|
388 |
-## `%IN%` <- function(x, table) { |
|
389 |
-## ii <- NULL ## satisify codetools |
|
390 |
-## idx <- match(x, table, nomatch=NA) |
|
391 |
-## ii <<- idx[!is.na(idx)] |
|
392 |
-## res <- as.logical(idx) |
|
393 |
-## res[is.na(res)] <- FALSE |
|
394 |
-## return(res) |
|
395 |
-## } |
|
396 |
-## geom_nplace <- function(data, map, place, ...) { |
|
397 |
-## label <- NULL |
|
398 |
-## ii <- 1:nrow(data) |
|
399 |
-## geom_text(subset=.(label %IN% data[[map]]), label = data[ii, place], ...) |
|
400 |
-## } |
|
401 |
- |
|
402 |
- |
|
403 | 68 |
roundDigit <- function(d) { |
404 | 69 |
i <- 0 |
405 | 70 |
while(d < 1) { |
... | ... |
@@ -339,25 +339,6 @@ extract.treeinfo.jplace <- function(object, layout="rectangular", ladderize=TRUE |
339 | 339 |
## } |
340 | 340 |
|
341 | 341 |
|
342 |
-is.tree <- function(x) { |
|
343 |
- if (class(x) %in% c("phylo", |
|
344 |
- "phylo4", |
|
345 |
- "jplace", |
|
346 |
- "baseml", |
|
347 |
- "paml_rst", |
|
348 |
- "baseml_mlc", |
|
349 |
- "codeml_mlc", |
|
350 |
- "codeml", |
|
351 |
- "hyphy", |
|
352 |
- "beast", |
|
353 |
- "phangorn", |
|
354 |
- "treedata") |
|
355 |
- ) { |
|
356 |
- return(TRUE) |
|
357 |
- } |
|
358 |
- return(FALSE) |
|
359 |
-} |
|
360 |
- |
|
361 | 342 |
|
362 | 343 |
|
363 | 344 |
color_scale <- function(c1="grey", c2="red", n=100) { |
... | ... |
@@ -459,14 +459,7 @@ getCols <- function (n) { |
459 | 459 |
colorRampPalette(col3)(n) |
460 | 460 |
} |
461 | 461 |
|
462 |
- |
|
463 |
-get_fun_from_pkg <- function(pkg, fun) { |
|
464 |
- ## requireNamespace(pkg) |
|
465 |
- ## eval(parse(text=paste0(pkg, "::", fun))) |
|
466 |
- require(pkg, character.only = TRUE) |
|
467 |
- eval(parse(text = fun)) |
|
468 |
-} |
|
469 |
- |
|
462 |
+##' @importFrom rvcheck get_fun_from_pkg |
|
470 | 463 |
hist <- get_fun_from_pkg("graphics", "hist") |
471 | 464 |
|
472 | 465 |
|
... | ... |
@@ -297,22 +297,22 @@ reverse.treeview.data <- function(df) { |
297 | 297 |
## return(phylo) |
298 | 298 |
## } |
299 | 299 |
|
300 |
-## extract.treeinfo.jplace <- function(object, layout="phylogram", ladderize=TRUE, right=FALSE, ...) { |
|
300 |
+extract.treeinfo.jplace <- function(object, layout="rectangular", ladderize=TRUE, right=FALSE, ...) { |
|
301 | 301 |
|
302 |
-## tree <- get.tree(object) |
|
302 |
+ tree <- get.tree(object) |
|
303 | 303 |
|
304 |
-## df <- fortify.phylo(tree, layout=layout, ladderize=ladderize, right=right, ...) |
|
304 |
+ df <- fortify.phylo(tree, layout=layout, ladderize=ladderize, right=right, ...) |
|
305 | 305 |
|
306 |
-## edgeNum.df <- attr(tree, "edgeNum") |
|
307 |
-## if (!is.null(edgeNum.df)) { |
|
308 |
-## df2 <- merge(df, edgeNum.df, by.x="node", by.y="node", all.x=TRUE) |
|
309 |
-## df <- df2[match(df[, "node"], df2[, "node"]),] |
|
310 |
-## } |
|
306 |
+ edgeNum.df <- attr(tree, "edgeNum") |
|
307 |
+ if (!is.null(edgeNum.df)) { |
|
308 |
+ df2 <- merge(df, edgeNum.df, by.x="node", by.y="node", all.x=TRUE) |
|
309 |
+ df <- df2[match(df[, "node"], df2[, "node"]),] |
|
310 |
+ } |
|
311 | 311 |
|
312 |
-## attr(df, "ladderize") <- ladderize |
|
313 |
-## attr(df, "right") <- right |
|
314 |
-## return(df) |
|
315 |
-## } |
|
312 |
+ attr(df, "ladderize") <- ladderize |
|
313 |
+ attr(df, "right") <- right |
|
314 |
+ return(df) |
|
315 |
+} |
|
316 | 316 |
|
317 | 317 |
## ## convert edge number to node number for EPA/pplacer output |
318 | 318 |
## edgeNum2nodeNum <- function(jp, edgeNum) { |
... | ... |
@@ -13,237 +13,237 @@ get_tree_view <- function(tree_view) { |
13 | 13 |
|
14 | 14 |
|
15 | 15 |
|
16 |
-has.field <- function(tree_object, field) { |
|
17 |
- if ( ! field %in% get.fields(tree_object) ) { |
|
18 |
- return(FALSE) |
|
19 |
- } |
|
20 |
- |
|
21 |
- if (is(tree_object, "codeml")) { |
|
22 |
- is_codeml <- TRUE |
|
23 |
- tree <- tree_object@rst |
|
24 |
- } else { |
|
25 |
- is_codeml <- FALSE |
|
26 |
- tree <- tree_object |
|
27 |
- } |
|
28 |
- |
|
29 |
- if (.hasSlot(tree, field)) { |
|
30 |
- has_slot <- TRUE |
|
31 |
- } else { |
|
32 |
- has_slot <- FALSE |
|
33 |
- } |
|
34 |
- |
|
35 |
- if (has_slot == FALSE) { |
|
36 |
- if (has.extraInfo(tree_object) == FALSE) { |
|
37 |
- return(FALSE) |
|
38 |
- } |
|
39 |
- |
|
40 |
- if (nrow(tree_object@extraInfo) == 0) { |
|
41 |
- return(FALSE) |
|
42 |
- } |
|
43 |
- |
|
44 |
- if (!field %in% colnames(tree_object@extraInfo)) { |
|
45 |
- return(FALSE) |
|
46 |
- } |
|
47 |
- } |
|
48 |
- res <- TRUE |
|
49 |
- attr(res, "has_slot") <- has_slot |
|
50 |
- attr(res, "is_codeml") <- is_codeml |
|
51 |
- return(res) |
|
52 |
-} |
|
53 |
- |
|
54 |
-append_extraInfo <- function(df, object) { |
|
55 |
- if (has.extraInfo(object)) { |
|
56 |
- info <- object@extraInfo |
|
57 |
- if ("parent" %in% colnames(info)) { |
|
58 |
- res <- merge(df, info, by.x=c("node", "parent"), by.y=c("node", "parent")) |
|
59 |