|
| 1 | +#' Hierarchical partition structure |
| 2 | +#' |
| 3 | +#' A structure of class `HPart` comprises a pointer to a C++ representation of |
| 4 | +#' hierarchical partitions, with the attribute `tip.label` recording the |
| 5 | +#' character labels of its leaves. `HPart` objects with identical tip labels |
| 6 | +#' can be compared using [`HierarchicalMutualInfo()`]. |
| 7 | +#' |
| 8 | +#' |
| 9 | +#' An `HPart` object may be created from various representations of hierarchical |
| 10 | +#' structures: |
| 11 | +#' |
| 12 | +#' - a tree (possibly phylogenetic) of class `phylo` |
| 13 | +#' - A hierarchical list of lists, in which elements are represented by integers |
| 14 | +#' 1\dots{}n |
| 15 | +#' - A vector, which will be interpreted as a flat structure |
| 16 | +#' in which all elements bearing the same label are assigned to the same cluster |
| 17 | +#' |
| 18 | +#' @param tree An object to convert to an HPart structure, in a supported format |
| 19 | +#' (see details). |
| 20 | +#' @returns `HPart()` returns a structure containing a pointer to a C++ |
| 21 | +#' representation of a hierarchical partition structure. |
| 22 | +#' @name HPart |
| 23 | +#' @export |
| 24 | +as.HPart <- function(tree, tipLabels) { |
| 25 | + UseMethod("as.HPart") |
| 26 | +} |
| 27 | + |
| 28 | +#' @export |
| 29 | +#' @rdname HPart |
| 30 | +as.HPart.HPart <- function(tree, tipLabels = NULL) { |
| 31 | + if (is.null(tipLabels) || identical(tipLabels, TipLabels(tree))) { |
| 32 | + tree |
| 33 | + } else { |
| 34 | + RenumberTips(tree, tipLabels) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#' @rdname HPart |
| 39 | +#' @export |
| 40 | +as.HPart.default <- function(tree, tipLabels = NULL) { |
| 41 | + if (is.null(dim(tree))) { |
| 42 | + structure(build_hpart_from_list( |
| 43 | + lapply(unique(tree), function(x) as.list(which(tree == x))), |
| 44 | + length(tree)), |
| 45 | + tip.label = seq_along(tree), |
| 46 | + class = "HPart" |
| 47 | + ) |
| 48 | + } else { |
| 49 | + stop("no applicable method for 'as.HPart' applied to an object of class \"", |
| 50 | + paste(class(tree), collapse = "\", \""), "\"") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +#' @rdname HPart |
| 56 | +#' @export |
| 57 | +as.HPart.list <- function(tree, tipLabels = NULL) { |
| 58 | + # Flatten to verify leaves |
| 59 | + leaves <- unlist(tree, recursive = TRUE) |
| 60 | + if (!all(is.numeric(leaves)) || any(leaves != as.integer(leaves))) { |
| 61 | + stop("All leaves must be integers.") |
| 62 | + } |
| 63 | + tree <- rapply(tree, as.integer, how = "replace") |
| 64 | + if (0 %in% leaves) { |
| 65 | + tree <- rapply(tree, function(x) x + 1L, how = "replace") |
| 66 | + leaves <- leaves + 1 |
| 67 | + } |
| 68 | + n_tip <- length(unique(leaves)) |
| 69 | + expected <- seq_len(n_tip) |
| 70 | + if (!isTRUE(all.equal(sort(leaves), expected))) { |
| 71 | + stop("Leaves must contain each integer 1..n exactly once") |
| 72 | + } |
| 73 | + |
| 74 | + hpart_ptr <- build_hpart_from_list(tree, n_tip) |
| 75 | + ret <- structure(hpart_ptr, tip.label = as.character(expected), class = "HPart") |
| 76 | + if (!is.null(tipLabels) && !is.list(tipLabels)) { |
| 77 | + RenumberTips(ret, tipLabels) |
| 78 | + } |
| 79 | + ret |
| 80 | +} |
| 81 | + |
| 82 | +#' @export |
| 83 | +#' @inheritParams TreeTools::as.ClusterTable |
| 84 | +#' @rdname HPart |
| 85 | +as.HPart.phylo <- function(tree, tipLabels = TipLabels(tree)) { |
| 86 | + if (!identical(TipLabels(tree), tipLabels)) { |
| 87 | + tree <- RenumberTips(tree, tipLabels) |
| 88 | + } |
| 89 | + structure(build_hpart_from_phylo(tree), tip.label = tipLabels, |
| 90 | + class = "HPart") |
| 91 | +} |
| 92 | + |
| 93 | +#' @rdname HPart |
| 94 | +#' @export |
| 95 | +is.HPart <- function(x) { |
| 96 | + inherits(x, "HPart") |
| 97 | +} |
| 98 | + |
| 99 | +#' @export |
| 100 | +NTip.HPart <- function(phy) { |
| 101 | + length(TipLabels(phy)) |
| 102 | +} |
| 103 | + |
| 104 | +#' @rdname HPart |
| 105 | +#' @export |
| 106 | +print.HPart <- function(x, ...) { |
| 107 | + nTip <- NTip(x) |
| 108 | + tips <- TipLabels(x) |
| 109 | + cat("Hierarchical partition on", nTip, "leaves: ") |
| 110 | + if (nTip > 5) { |
| 111 | + cat(paste0(c(tips[1:2], "...", tips[length(tips) - 1:0]), collapse = ", ")) |
| 112 | + } else { |
| 113 | + cat(paste0(tips, collapse = ", ")) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +#' @rdname HPart |
| 118 | +#' @importFrom ape as.phylo |
| 119 | +#' @export |
| 120 | +as.phylo.HPart <- function(x, ...) { |
| 121 | + edge <- hpart_to_edge(x) |
| 122 | + labels <- TipLabels(x) |
| 123 | + nNode <- dim(edge)[[1]] - length(labels) + 1 |
| 124 | + structure(list(edge = edge, Nnode = nNode, tip.label = labels), |
| 125 | + class = "phylo", |
| 126 | + order = "cladewise") |
| 127 | +} |
| 128 | + |
| 129 | +#' @rdname HPart |
| 130 | +#' @param x `HPart` object to plot. |
| 131 | +#' @param \dots Additional arguments to \code{\link[ape:plot.phylo]{plot.phylo}}. |
| 132 | +#' @export |
| 133 | +plot.HPart <- function(x, ...) { |
| 134 | + plot(as.phylo(x), ...) |
| 135 | +} |
| 136 | + |
| 137 | +#' Clone / duplicate an object |
| 138 | +#' `clone()` physically duplicates objects |
| 139 | +#' @param x the object to be cloned |
| 140 | +#' @param \dots additional parameters for methods |
| 141 | +#' @return `clone()` typically returns an object of the same class and "value" |
| 142 | +#' as the input `x`. |
| 143 | +#' @export |
| 144 | +clone <- function(x, ...) UseMethod("clone") |
| 145 | + |
| 146 | +#' @template MRS |
| 147 | +#' @rdname clone |
| 148 | +#' @inheritParams TreeTools::as.ClusterTable |
| 149 | +#' @export |
| 150 | +clone.HPart <- function(x, tipLabels = attr(x, "tip.label"), ...) { |
| 151 | + structure(clone_hpart(x), tip.label = tipLabels, |
| 152 | + class = "HPart") |
| 153 | +} |
| 154 | + |
| 155 | +#' @importFrom TreeTools MatchStrings |
| 156 | +#' @inheritParams TreeTools::RenumberTips |
| 157 | +#' @export |
| 158 | +RenumberTips.HPart <- function(tree, tipOrder) { |
| 159 | + startOrder <- TipLabels(tree) |
| 160 | + newOrder <- MatchStrings(TipLabels(tipOrder, single = TRUE), startOrder) |
| 161 | + |
| 162 | + if (!identical(newOrder, startOrder)) { |
| 163 | + if (length(newOrder) != length(startOrder)) { |
| 164 | + stop("Tree labels ", paste0(setdiff(startOrder, tipOrder), collapse = ", "), |
| 165 | + " missing from `tipOrder`") |
| 166 | + } |
| 167 | + newIndices <- match(newOrder, startOrder) |
| 168 | + tree <- clone(tree, newOrder) |
| 169 | + relabel_hpart(tree, newIndices - 1L) |
| 170 | + # Return: |
| 171 | + tree |
| 172 | + } else { |
| 173 | + clone(tree) |
| 174 | + } |
| 175 | +} |
0 commit comments