Debian Codegraph @

R/pvclust.R

raw
1 # Copyright (C) Tal Galili
2 #
3 # This file is part of dendextend.
4 #
5 # dendextend is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # dendextend is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # A copy of the GNU General Public License is available at
16 # http://www.r-project.org/Licenses/
17 #
18
19
20
21
22 #' @title Last Observation Carried Forward
23 #' @export
24 #' @description
25 #' A function for replacing each NA with the most recent non-NA prior to it.
26 #' @param x some vector
27 #' @param first_na_value If the first observation is NA, fill it with "first_na_value"
28 #' @param recursive logical (TRUE). Should na_locf be re-run until all NA values are filled?
29 #' @param ... ignored.
30 #' @return
31 #' The original vector, but with all the missing values filled by the value
32 #' before them.
33 #' @seealso
34 #' \link[zoo]{na.locf}
35 #'
36 #' @source
37 #' \url{https://stat.ethz.ch/pipermail/r-help/2003-November/042126.html}
38 #' \url{https://stackoverflow.com/questions/5302049/last-observation-carried-forward-na-locf-on-panel-cross-section-time-series}
39 #'
40 #' This could probably be solved MUCH faster using Rcpp.
41 #'
42 #' @examples
43 #' na_locf(c(NA, NA))
44 #' na_locf(c(1, NA))
45 #' na_locf(c(1, NA, NA, NA))
46 #' na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4))
47 #' na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), recursive = FALSE)
48 #' \dontrun{
49 #'
50 #' # library(microbenchmark)
51 #' # library(zoo)
52 #'
53 #' # microbenchmark(
54 #' # na_locf = na_locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4)),
55 #' # na.locf = na.locf(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4))
56 #' #) # my implementation is 6 times faster :)
57 #'
58 #' #microbenchmark(
59 #' # na_locf = na_locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000)),
60 #' # na.locf = na.locf(rep(c(1, NA, NA, NA, 2, 2, NA, 3, NA, 4), 1000))
61 #' # ) # my implementation is 3 times faster
62 #'
63 #' }
64 #'
65 na_locf <- function(x, first_na_value = 0, recursive = TRUE, ...) {
66 # where are the NA's:
67 x_na <- is.na(x)
68
69 # IF we have no NA's, return x:
70 if (!any(x_na)) {
71 return(x)
72 # Else - fill one next observation:
73 } else {
74 # If the first observation is NA, fill it with "first_na_value"
75 if (x_na[1]) x[1] <- first_na_value
76 x_na[1] <- FALSE
77
78 x_na_loc <- which(x_na)
79 x[x_na_loc] <- x[x_na_loc - 1]
80
81 if (recursive) {
82 Recall(x)
83 } else {
84 return(x)
85 }
86 }
87 }
88
89
90
91
92 # str(result)
93 # class(result)
94 # result %>% as.dendrogram %>% plot
95
96
97 #' @export
98 as.hclust.pvclust <- function(x, ...) {
99 # library(pvclust)
100 x[["hclust"]]
101 }
102
103
104
105 #' @export
106 as.dendrogram.pvclust <- function(object, ...) {
107 # library(pvclust)
108 as.dendrogram(as.hclust(object))
109 }
110
111
112
113 # Imported from:
114 # pvclust:::hc2axes
115 # pvclust:::hc2axes(result[["hclust"]])
116
117
118 hc2axes <- function(x) {
119 A <- x$merge
120 n <- nrow(A) + 1
121 x.axis <- c()
122 y.axis <- x$height
123 x.tmp <- rep(0, 2)
124 zz <- match(1:length(x$order), x$order)
125 for (i in 1:(n - 1)) {
126 ai <- A[i, 1]
127 if (ai < 0) {
128 x.tmp[1] <- zz[-ai]
129 } else {
130 x.tmp[1] <- x.axis[ai]
131 }
132 ai <- A[i, 2]
133 if (ai < 0) {
134 x.tmp[2] <- zz[-ai]
135 } else {
136 x.tmp[2] <- x.axis[ai]
137 }
138 x.axis[i] <- mean(x.tmp)
139 }
140 return(data.frame(x.axis = x.axis, y.axis = y.axis, stringsAsFactors = TRUE))
141 }
142
143
144 # Imported from:
145 # pvclust:::hc2split
146 # pvclust:::hc2split(result[["hclust"]])
147
148 hc2split <- function(x) {
149 A <- x$merge
150 n <- nrow(A) + 1
151 B <- list()
152 for (i in 1:(n - 1)) {
153 ai <- A[i, 1]
154 if (ai < 0) {
155 B[[i]] <- -ai
156 } else {
157 B[[i]] <- B[[ai]]
158 }
159 ai <- A[i, 2]
160 if (ai < 0) {
161 B[[i]] <- sort(c(B[[i]], -ai))
162 } else {
163 B[[i]] <- sort(c(B[[i]], B[[ai]]))
164 }
165 }
166 CC <- matrix(rep(0, n * (n - 1)), nrow = (n - 1), ncol = n)
167 for (i in 1:(n - 1)) {
168 bi <- B[[i]]
169 m <- length(bi)
170 for (j in 1:m) CC[i, bi[j]] <- 1
171 }
172 split <- list(
173 pattern = apply(CC, 1, paste, collapse = ""),
174 member = B
175 )
176 return(split)
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191 # Imported from:
192 # pvclust:::text.pvclust
193
194 #' @export
195 text.pvclust <- function(x, col = c(2, 3, 8), print.num = TRUE, float = 0.01,
196 cex = NULL, font = NULL, ...) {
197 # library(pvclust)
198 axes <- hc2axes(x$hclust)
199 usr <- par()$usr
200 wid <- usr[4] - usr[3]
201 au <- as.character(round(x$edges[, "au"] * 100))
202 bp <- as.character(round(x$edges[, "bp"] * 100))
203 rn <- as.character(row.names(x$edges))
204 au[length(au)] <- "au"
205 bp[length(bp)] <- "bp"
206 rn[length(rn)] <- "edge #"
207 a <- text(
208 x = axes[, 1], y = axes[, 2] + float * wid, au,
209 col = col[1], pos = 2, offset = 0.3, cex = cex, font = font
210 )
211 a <- text(
212 x = axes[, 1], y = axes[, 2] + float * wid, bp,
213 col = col[2], pos = 4, offset = 0.3, cex = cex, font = font
214 )
215 if (print.num) {
216 a <- text(
217 x = axes[, 1], y = axes[, 2], rn, col = col[3],
218 pos = 1, offset = 0.3, cex = cex, font = font
219 )
220 }
221 }
222
223
224
225
226
227
228
229 #' @title The significant branches in a dendrogram, based on a pvclust object
230 #' @description Shows the significant branches in a dendrogram, based on a pvclust object
231 #' @export
232 #' @param dend a dendrogram object
233 #' @param pvclust_obj a pvclust object
234 #' @param signif_type a character scalar (either "bp" or "au"), indicating
235 #' which of the two should be used to update the dendrogram.
236 #' @param alpha a number between 0 to 1, default is .05. Indicates what is the
237 #' cutoff from which branches will be updated.
238 #' @param signif_value a 2d vector (deafult: c(5,1)),
239 #' with the first element tells us what the significant branches will get,
240 #' and the second element which value the non-significant branches will get.
241 #' @param show_type a character scalar (either "lwd" or "col"), indicating
242 #' which parameter of the branches should be updated based on significance.
243 #' @param ... not used
244 #' @return
245 #' A dendrogram with updated branches
246 #' @seealso \link{pvclust_show_signif}, \link{pvclust_show_signif_gradient}
247 #'
248 #' @examples
249 #' \dontrun{
250 #' library(pvclust)
251 #' data(lung) # 916 genes for 73 subjects
252 #' set.seed(13134)
253 #' result <- pvclust(lung[, 1:20], method.dist = "cor", method.hclust = "average", nboot = 100)
254 #'
255 #' dend <- as.dendrogram(result)
256 #' result %>%
257 #' as.dendrogram() %>%
258 #' hang.dendrogram() %>%
259 #' plot(main = "Cluster dendrogram with AU/BP values (%)")
260 #' result %>% text()
261 #' result %>% pvrect(alpha = 0.95)
262 #'
263 #' dend %>%
264 #' pvclust_show_signif(result) %>%
265 #' plot()
266 #' dend %>%
267 #' pvclust_show_signif(result, show_type = "lwd") %>%
268 #' plot()
269 #' result %>% text()
270 #' result %>% pvrect(alpha = 0.95)
271 #'
272 #' dend %>%
273 #' pvclust_show_signif_gradient(result) %>%
274 #' plot()
275 #'
276 #' dend %>%
277 #' pvclust_show_signif_gradient(result) %>%
278 #' pvclust_show_signif(result) %>%
279 #' plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are highlighted by signif")
280 #' result %>% text()
281 #' result %>% pvrect(alpha = 0.95)
282 #' }
283 pvclust_show_signif <- function(dend, pvclust_obj, signif_type = c("bp", "au"), alpha = .05, signif_value = c(5, 1), show_type = c("lwd", "col"), ...) {
284
285 # these two are sorted from node number 1 to nnodes.
286 # result$edges$au
287 # result$edges$bp
288
289 signif_type <- match.arg(signif_type)
290 show_type <- match.arg(show_type)
291
292 pvalue_per_node <- pvclust_obj$edges[[signif_type]]
293 ord <- rank(get_branches_heights(dend, sort = FALSE))
294 pvalue_per_node <- pvalue_per_node[ord]
295 # Well, it is not exactly p-value. At elast, it is 1-Pv. And I am not yet sure of that...
296
297 # plot, but ignore the leaves:
298 # nnodes(dend)-nleaves(dend) # number of branches nodes
299
300 signif_TF <- pvalue_per_node > (1 - alpha)
301 signif_TF[1] <- FALSE
302 show_signif_TF <- ifelse(signif_TF, signif_value[1], signif_value[2])
303 show_signif_TF_with_leaves <- rep(NA, nnodes(dend))
304 ss_leaf <- which_leaf(dend)
305 show_signif_TF_with_leaves[!ss_leaf] <- show_signif_TF
306 show_signif_TF_with_leaves <- na_locf(show_signif_TF_with_leaves)
307
308 assign_values_to_branches_edgePar(dend, show_signif_TF_with_leaves, show_type) # %>% plot
309 }
310
311
312
313
314
315 #' @title Significance gradient of branches in a dendrogram (via pvclust)
316 #' @description Shows the gradient of significance of branches in a dendrogram, based on a pvclust object
317 #' @export
318 #' @param dend a dendrogram object
319 #' @param pvclust_obj a pvclust object
320 #' @param signif_type a character scalar (either "bp" or "au"), indicating
321 #' which of the two should be used to update the dendrogram.
322 #' @param signif_col_fun a function to create colors for the significant
323 #' gradient. Default is: colorRampPalette(c("black", "darkred", "red"))
324 #' @param ... not used
325 #' @return
326 #' A dendrogram with updated branches
327 #' @seealso \link{pvclust_show_signif}, \link{pvclust_show_signif_gradient}
328 #'
329 #' @examples
330 #' \dontrun{
331 #' library(pvclust)
332 #' data(lung) # 916 genes for 73 subjects
333 #' set.seed(13134)
334 #' result <- pvclust(lung[, 1:20], method.dist = "cor", method.hclust = "average", nboot = 100)
335 #'
336 #' dend <- as.dendrogram(result)
337 #' result %>%
338 #' as.dendrogram() %>%
339 #' hang.dendrogram() %>%
340 #' plot(main = "Cluster dendrogram with AU/BP values (%)")
341 #' result %>% text()
342 #' result %>% pvrect(alpha = 0.95)
343 #'
344 #' dend %>%
345 #' pvclust_show_signif(result) %>%
346 #' plot()
347 #' dend %>%
348 #' pvclust_show_signif(result, show_type = "lwd") %>%
349 #' plot()
350 #' result %>% text()
351 #' result %>% pvrect(alpha = 0.95)
352 #'
353 #' dend %>%
354 #' pvclust_show_signif_gradient(result) %>%
355 #' plot()
356 #'
357 #' dend %>%
358 #' pvclust_show_signif_gradient(result) %>%
359 #' pvclust_show_signif(result) %>%
360 #' plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are highlighted by signif")
361 #' result %>% text()
362 #' result %>% pvrect(alpha = 0.95)
363 #' }
364 pvclust_show_signif_gradient <- function(dend, pvclust_obj, signif_type = c("bp", "au"), signif_col_fun = colorRampPalette(c("black", "darkred", "red")), ...) {
365
366 # these two are sorted from node number 1 to nnodes.
367 # result$edges$au
368 # result$edges$bp
369
370 signif_type <- match.arg(signif_type)
371
372 pvalue_per_node <- pvclust_obj$edges[[signif_type]]
373 ord <- rank(get_branches_heights(dend, sort = FALSE))
374 pvalue_per_node <- pvalue_per_node[ord]
375 # Well, it is not exactly p-value. At elast, it is 1-Pv. And I am not yet sure of that...
376
377 # plot, but ignore the leaves:
378 # nnodes(dend)-nleaves(dend) # number of branches nodes
379
380
381
382 signif_col <- signif_col_fun(100)
383 # plot(1:100, col = signif_col, pch = 19)
384 #
385 #
386 pvalue_by_all_nodes <- rep(NA, nnodes(dend))
387 ss_leaf <- which_leaf(dend)
388 pvalue_by_all_nodes[!ss_leaf] <- pvalue_per_node
389 pvalue_by_all_nodes <- na_locf(pvalue_by_all_nodes)
390 #
391 the_cols <- signif_col[round(pvalue_by_all_nodes * 100)]
392
393 assign_values_to_branches_edgePar(dend, the_cols, "col") # %>% plot
394 }
395
396
397
398
399
400
401
402
403
404
405
406 ##################################
407 #### Required functions
408 ##################################
409
410
411 # Based on:
412 # https://stat.ethz.ch/pipermail/r-help/2007-November/145106.html
413 # strheight for rotated text
414 strheight2 <- function(s, ...) {
415 xusr <- par("usr")
416 xh <- strwidth(s, cex = par("cex"), ...)
417 yh <- strheight(s, cex = par("cex"), ...) * 5 / 3
418 tmp <- xh
419 xh <- yh / (xusr[4] - xusr[3]) * par("pin")[2]
420 xh <- xh / par("pin")[1] * (xusr[2] - xusr[1])
421 yh <- tmp / (xusr[2] - xusr[1]) * par("pin")[1]
422 yh <- yh / par("pin")[2] * (xusr[4] - xusr[3])
423 yh
424 }
425
426
427 # strwidth for rotated text
428 strwidth2 <- function(s, ...) {
429 xusr <- par("usr")
430 xh <- strwidth(s, cex = par("cex"), ...)
431 yh <- strheight(s, cex = par("cex"), ...) * 5 / 3
432 tmp <- xh
433 xh <- yh / (xusr[4] - xusr[3]) * par("pin")[2]
434 xh <- xh / par("pin")[1] * (xusr[2] - xusr[1])
435 yh <- tmp / (xusr[2] - xusr[1]) * par("pin")[1]
436 yh <- yh / par("pin")[2] * (xusr[4] - xusr[3])
437 xh
438 }
439
440
441 # http://r-posts.com/adding-sinew-to-roxygen2-skeletons/
442 # sinew::makeOxygen(pvrect2)
443
444
445
446
447
448 #' @title Draw Rectangles Around a Dendrogram's Clusters with High/Low P-values
449 #' @export
450 #' @description
451 #' Draws rectangles around the branches of a dendrogram highlighting the corresponding clusters with low p-values.
452 #' This is based on \link[pvclust]{pvrect}, allowing to draw the rects till the bottom of the labels.
453 #' @param x object of class pvclust.
454 #' @param alpha threshold value for p-values., Default: 0.95
455 #' @param pv character string which specifies the p-value to be used. It should be either of "au" or "bp", corresponding to AU p-value or BP value, respectively. See plot.pvclust for details. , Default: 'au'
456 #' @param type one of "geq", "leq", "gt" or "lt". If "geq" is specified, clusters with p-value greater than or equals the threshold given by "alpha" are returned or displayed. Likewise "leq" stands for lower than or equals, "gt" for greater than and "lt" for lower than the threshold value. The default is "geq"., Default: 'geq'
457 #' @param max.only logical. If some of clusters with high/low p-values have inclusion relation, only the largest cluster is returned (or displayed) when max.only=TRUE., Default: TRUE
458 #' @param border numeric value which specifies the color of borders of rectangles., Default: 2
459 #' @param xpd A logical value (or NA.), passed to par. Default is TRUE, in order to allow the rect to be below the labels. If FALSE, all plotting is clipped to the plot region, if TRUE, all plotting is clipped to the figure region, and if NA, all plotting is clipped to the device region. See also clip., Default: TRUE
460 #' @param lower_rect a (scalar) value of how low should the lower part of the rect be. If missing, it will take the value of par("usr")[3L] (or par("usr")[2L], depending if horiz = TRUE or not), with also the width of the labels. (notice that we would like to keep xpd = TRUE if we want the rect to be after the labels!) You can use a value such as 0, to get the rect above the labels.
461 #' @param ... passed to \link{rect}
462 #' @return NULL
463 #' @seealso
464 #' \link[pvclust]{pvrect}, \link{pvclust_show_signif}
465 #' @examples
466 #' \dontrun{
467 #'
468 #'
469 #' library(dendextend)
470 #' library(pvclust)
471 #' data(lung) # 916 genes for 73 subjects
472 #' set.seed(13134)
473 #' result <- pvclust(lung[, 1:20], method.dist = "cor", method.hclust = "average", nboot = 10)
474 #'
475 #' par(mar = c(9, 2.5, 2, 0))
476 #' dend <- as.dendrogram(result)
477 #' dend %>%
478 #' pvclust_show_signif(result, signif_value = c(3, .5)) %>%
479 #' pvclust_show_signif(result, signif_value = c("black", "grey"), show_type = "col") %>%
480 #' plot(main = "Cluster dendrogram with AU/BP values (%)")
481 #' pvrect2(result, alpha = 0.95)
482 #' # getting the rects to the tips / above the labels
483 #' pvrect2(result, lower_rect = .15, border = 4, alpha = 0.95, lty = 2)
484 #' # Original function
485 #' # pvrect(result, alpha=0.95)
486 #' text(result, alpha = 0.95)
487 #' }
488 #'
489 pvrect2 <- function(x, alpha = 0.95, pv = "au", type = "geq", max.only = TRUE,
490 border = 2,
491 xpd = TRUE, lower_rect,
492 ...) {
493 dend <- as.dendrogram(x)
494
495 len <- nrow(x$edges)
496 member <- hc2split(x$hclust)$member
497 order <- x$hclust$order
498 usr <- par("usr")
499 xwd <- usr[2] - usr[1]
500 ywd <- usr[4] - usr[3]
501 cin <- par()$cin
502 ht <- c()
503 j <- 1
504 if (is.na(pm <- pmatch(type, c("geq", "leq", "gt", "lt")))) {
505 stop("Invalid type argument: see help(pvrect)")
506 }
507
508
509 old_xpd <- par()["xpd"]
510 par(xpd = xpd)
511
512
513 for (i in (len - 1):1) {
514 if (pm == 1) {
515 wh <- (x$edges[i, pv] >= alpha)
516 } else if (pm == 2) {
517 wh <- (x$edges[i, pv] <= alpha)
518 } else if (pm == 3) {
519 wh <- (x$edges[i, pv] > alpha)
520 } else if (pm == 4) {
521 wh <- (x$edges[i, pv] > alpha)
522 }
523 if (wh) {
524 mi <- member[[i]]
525 ma <- match(mi, order)
526 if (max.only == FALSE || (max.only && sum(match(ma,
527 ht,
528 nomatch = 0
529 )) == 0)) {
530 xl <- min(ma)
531 xr <- max(ma)
532 yt <- x$hclust$height[i]
533 yb <- usr[3]
534 mx <- xwd / length(member) / 3
535 my <- ywd / 200
536
537 # if(missing(lower_rect)) lower_rect <- par("usr")[3L] - strwidth("W")*(max(nchar(labels(dend))) + 1)
538 if (missing(lower_rect)) lower_rect <- -max(strheight2(labels(dend)))
539
540 dLeaf <- -0.75 * strheight("x")
541 extra_space <- -strheight2("_")
542
543 rect(xl - mx,
544 # 0,
545 lower_rect + dLeaf + extra_space,
546 # yb + my + lower_rect,
547 xr + mx, yt + my,
548 border = border,
549 shade = NULL, ...
550 )
551 j <- j + 1
552 }
553 ht <- c(ht, ma)
554 }
555 }
556
557 par(xpd = old_xpd)
558
559 invisible()
560 }
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575 # !is.infinite(Inf)
576 #
577 #
578 # show_signif_bp_with_leaves <- rep(NA, nnodes(dend))
579 # ss_leaf <- which_leaf(dend)
580 # show_signif_bp_with_leaves[!ss_leaf] <- show_signif_bp
581 # show_signif_bp_with_leaves <- na_locf(show_signif_bp_with_leaves)
582 #
583 # assign_values_to_branches_edgePar(dend, show_signif_bp_with_leaves, "lwd") %>% plot
584 # pvclust:::text.pvclust(result)
585 # pvrect(result, alpha=0.95,pv = "bp")
586 #
587 # signif_col_fun <- colorRampPalette(c("black", "darkred", "red"))
588 # signif_col <- signif_col_fun(100)
589 # plot(1:100, col = signif_col, pch = 19)
590 #
591 #
592 # bp_by_all_nodes <- rep(NA, nnodes(dend))
593 # ss_leaf <- which_leaf(dend)
594 # bp_by_all_nodes[!ss_leaf] <- bp_by_nodes
595 # bp_by_all_nodes <- na_locf(bp_by_all_nodes)
596 #
597 # the_cols <- signif_col[round(bp_by_all_nodes*100)]
598 # dend %>%
599 # assign_values_to_branches_edgePar(the_cols, "col") %>%
600 # assign_values_to_branches_edgePar(show_signif_bp_with_leaves, "lwd") %>%
601 # plot
602 # pvclust:::text.pvclust(result)
603 # pvrect(result, alpha=0.95,pv = "bp")
604 #
605
606
607 #
608 #
609 #
610 # if(F) {
611 #
612 #
613 # # # require2(pvclust, F)
614 # #
615 # #
616 # library(dendextend)
617 #
618 #
619
620
621
622
623
624
625
626
627
628
629
630 #
631 # plot(result)
632 # pvrect(result, alpha=0.95)
633 #
634 # # pvclust:::plot.pvclust
635 # # pvclust:::text.pvclust
636 #
637 # # reproduce:
638 # hc <- result[[1]]
639 # plot(hc)
640 # pvclust:::text.pvclust(result)
641 # pvrect(result, alpha=0.95)
642 #
643 # # reproduce with a dendrogram:
644 # dend <- as.dendrogram(hc)
645 # dend %>% hang.dendrogram %>% plot
646 # pvclust:::text.pvclust(result)
647 # pvrect(result, alpha=0.95)
648 #
649 # # str(result)
650 #
651 # # Reproduce with a dendrogram, but with information in the branches!):
652 #
653 # # these two are sorted from node number 1 to nnodes.
654 # result$edges$au
655 # result$edges$bp
656 # get_branches_heights(dend, sort = FALSE)
657 # ord <- rank(get_branches_heights(dend, sort = FALSE))
658 # au_by_nodes <- result$edges$au[ord]
659 # bp_by_nodes <- result$edges$bp[ord]
660 #
661 # # plot, but ignore the leaves:
662 # nnodes(dend)-nleaves(dend) # number of branches nodes
663 # alpha <- .05
664 # signif_bp <- bp_by_nodes > (1-alpha)
665 # signif_bp[1] <- FALSE
666 # show_signif_bp <- ifelse(signif_bp, 5, 1)
667 # assign_values_to_branches_edgePar(dend, show_signif_bp, "lwd", skip_leaves = TRUE) %>% plot
668 # pvclust:::text.pvclust(result)
669 # pvrect(result, alpha=0.95,pv = "bp")
670 #
671 #
672 # # Let's do it again, but include the leaves this time!
673 #
674 # show_signif_bp_with_leaves <- rep(NA, nnodes(dend))
675 # ss_leaf <- which_leaf(dend)
676 # show_signif_bp_with_leaves[!ss_leaf] <- show_signif_bp
677 # show_signif_bp_with_leaves <- na_locf(show_signif_bp_with_leaves)
678 #
679 # assign_values_to_branches_edgePar(dend, show_signif_bp_with_leaves, "lwd") %>% plot
680 # pvclust:::text.pvclust(result)
681 # pvrect(result, alpha=0.95,pv = "bp")
682 #
683 # signif_col_fun <- colorRampPalette(c("black", "darkred", "red"))
684 # signif_col <- signif_col_fun(100)
685 # plot(1:100, col = signif_col, pch = 19)
686 #
687 #
688 # bp_by_all_nodes <- rep(NA, nnodes(dend))
689 # ss_leaf <- which_leaf(dend)
690 # bp_by_all_nodes[!ss_leaf] <- bp_by_nodes
691 # bp_by_all_nodes <- na_locf(bp_by_all_nodes)
692 #
693 # the_cols <- signif_col[round(bp_by_all_nodes*100)]
694 # dend %>%
695 # assign_values_to_branches_edgePar(the_cols, "col") %>%
696 # assign_values_to_branches_edgePar(show_signif_bp_with_leaves, "lwd") %>%
697 # plot
698 # pvclust:::text.pvclust(result)
699 # pvrect(result, alpha=0.95,pv = "bp")
700 #
701 #
702 #
703 #
704 # #
705 #
706 #
707 #
708 #
709 #
710 #
711 #
712 # # it numbers the nodes from 1 to nnodes, so that 1 is the lowest node, the next one is after it
713 # # and so on. This numbering is interesting. This is something that can be
714 # # manually added to a dend. (as extra attr), or just tracked
715 # #
716 # # it saves a vector
717 #
718 # get_branches_heights(dend, sort = FALSE)
719 #
720 # # after pvclust - we can get the values and use them!
721 #
722 #
723 #
724 #
725 # # http://course.sdu.edu.cn/G2S/eWebEditor/uploadfile/20130605012427559.pdf
726 # # http://scholar.google.co.il/scholar?cites=3917689774873650154&as_sdt=2005&sciodt=0,5&hl=en
727 # # http://www.is.titech.ac.jp/~shimo/pub/Shimodaira%20and%20Hasegawa%20MBE1999.pdf
728 # # http://scholar.google.co.il/scholar?hl=en&q=Ryota+Suzuki+&btnG=&as_sdt=1%2C5&as_sdtp=
729 # # http://www.is.titech.ac.jp/~shimo/prog/pvclust/
730 # # https://cran.r-project.org/package=pvclust
731 #
732 # }
733 #
734
735
736
737 #' @title Get Pvclust Edges Information
738 #' @export
739 #' @description
740 #' Get pvclust edges information such as au and bp and return dataframe with proper sample labels.
741 #' This function is useful when there are a lot of samples involved.
742 #'
743 #' @param pvclust_obj pvclust object
744 #' @return data.frame with leaves on column 1 and 2, followed by the rest of the information from edge
745 #' @references hclust object descriptions \url{https://stat.ethz.ch/R-manual/R-patched/library/stats/html/hclust.html}
746 #'
747 #' @examples
748 #' \dontrun{
749 #'
750 #' library(pvclust)
751 #' data(lung) # 916 genes for 73 subjects
752 #' set.seed(13134)
753 #' result <- pvclust(lung[, 1:20], method.dist = "cor", method.hclust = "average", nboot = 100)
754 #' pvclust_edges(result)
755 #' }
756 pvclust_edges <- function(pvclust_obj) {
757 hclust_merge <- pvclust_obj$hclust$merge
758 hclust_merge[hclust_merge < 0] <- pvclust_obj$hclust$labels[abs(hclust_merge[hclust_merge < 0])] # get sample name
759 hclust_merge <- cbind(hclust_merge, pvclust_obj$edges) # combine with edge table
760 colnames(hclust_merge)[1:2] <- c("branch_L", "branch_R")
761 return(hclust_merge)
762 }