| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#' @title Rotates a dendrogram based on a seriation of a distance matrix |
| 28 |
#' @export |
| 29 |
#' @description |
| 30 |
#' Rotates a dendrogram so it confirms to an order of a provided distance object. |
| 31 |
#' The seriation algorithm is based on \link[seriation]{seriate}, which tries to find |
| 32 |
#' a linear order for objects using data in form of a dissimilarity matrix (one mode data). |
| 33 |
#' |
| 34 |
#' This is useful for heatmap visualization. |
| 35 |
#' |
| 36 |
#' @param dend An object of class \link{dendrogram} or \link{hclust} |
| 37 |
#' @param x a \link{dist} object. |
| 38 |
#' @param method a character vector of either "OLO" or "GW": |
| 39 |
#' "OLO" - Optimal leaf ordering, optimzes the Hamiltonian path length that is restricted by the dendrogram structure - works in O(n^4) |
| 40 |
#' "GW" - Gruvaeus and Wainer heuristic to optimze the Hamiltonian path length that is restricted by the dendrogram structure |
| 41 |
#' |
| 42 |
#' @param ... parameters passed to \link[seriation]{seriate} |
| 43 |
#' @return |
| 44 |
#' A dendrogram that is rotated based on the optimal ordering of the distance matrix |
| 45 |
#' @seealso \code{\link[dendextend]{rotate}}, \link[seriation]{seriate} |
| 46 |
#' @examples |
| 47 |
#' \dontrun{ |
| 48 |
#' # library(dendextend) |
| 49 |
#' d <- dist(USArrests) |
| 50 |
#' hc <- hclust(d, "ave") |
| 51 |
#' dend <- as.dendrogram(hc) |
| 52 |
#' |
| 53 |
#' heatmap(as.matrix(USArrests)) |
| 54 |
#' |
| 55 |
#' dend2 <- seriate_dendrogram(dend, d) |
| 56 |
#' heatmap(as.matrix(USArrests), Rowv = dend) |
| 57 |
#' } |
| 58 |
seriate_dendrogram <- function(dend, x, method = c("OLO", "GW"), ...) { |
| 59 |
if (!requireNamespace("seriation")) stop("Please first install seriation:\n install.packages('seriation')") |
| 60 |
|
| 61 |
if (!is.dendrogram(dend) & !is.hclust(dend)) stop("dend must be either a dendrogram or an hclust object") |
| 62 |
if (!is.dist(x)) stop("x must be a dist object") |
| 63 |
if (!identical(sort(labels(x)), sort(labels(dend)))) stop("The labels of dend and x must be identical.") |
| 64 |
method <- match.arg(method) |
| 65 |
|
| 66 |
# o <- seriate(d, method = "GW", control = list(hclust = as.hclust(dend)) ) |
| 67 |
# get_order(o) |
| 68 |
# library(seriation) |
| 69 |
# hmap(sqrt(d2), Colv = "none", trace = "none", col = viridis(200)) |
| 70 |
# Error in (function (x, Rowv = TRUE, Colv = if (symm) "Rowv" else TRUE, : |
| 71 |
# formal argument "Colv" matched by multiple actual arguments |
| 72 |
o <- seriation::seriate(x, method = method, control = list(hclust = as.hclust(dend)), ...) |
| 73 |
# library(dendextend) |
| 74 |
dend <- rotate(dend, order = rev(labels(x)[seriation::get_order(o)])) |
| 75 |
dend |
| 76 |
} |