Debian Codegraph @

R/noded_with_condition.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 #' @title Find which nodes satisfies a condition
22 #' @export
23 #' @description
24 #' Goes through a tree's nodes in order to return a vector
25 #' with whether (TRUE/FALSE) each node satisies some condition (function)
26 #'
27 #' @param dend a dendrogram dend
28 #' @param condition a function that gets a node and return TRUE or FALSE
29 #' (based on whether or not that node/tree fulfills the "condition")
30 #' @param include_leaves logical. Should leaves attributes be included as well?
31 #' @param include_branches logical. Should non-leaf (branch node)
32 #' attributes be included as well?
33 #' @param na.rm logical. Should NA attributes be REMOVED from the resulting vector?
34 #' @param ... passed to the condition function
35 #' @return
36 #' A logical vector with TRUE/FALSE, specifying for each of the dendrogram's nodes if it fulfills the condition or not.
37 #' @seealso \link{branches_attr_by_labels}, \link{get_leaves_attr}, \link{nnodes}, \link{nleaves}
38 #' @examples
39 #' \dontrun{
40 #'
41 #' library(dendextend)
42 #'
43 #' set.seed(23235)
44 #' ss <- sample(1:150, 10)
45 #'
46 #' # Getting the dend dend
47 #' dend <- iris[ss, -5] %>%
48 #' dist() %>%
49 #' hclust() %>%
50 #' as.dendrogram()
51 #' dend %>% plot()
52 #'
53 #'
54 #'
55 #' # this is the basis for branches_attr_by_labels
56 #' has_any_labels <- function(sub_dend, the_labels) any(labels(sub_dend) %in% the_labels)
57 #' cols <- noded_with_condition(dend, has_any_labels,
58 #' the_labels = c("126", "109", "59")
59 #' ) %>%
60 #' ifelse(2, 1)
61 #' set(dend, "branches_col", cols) %>% plot()
62 #'
63 #' # Similar to branches_attr_by_labels - but for heights!
64 #' high_enough <- function(sub_dend, height) attr(sub_dend, "height") > height
65 #' cols <- noded_with_condition(dend, high_enough, height = 1) %>% ifelse(2, 1)
66 #' set(dend, "branches_col", cols) %>% plot()
67 #' }
68 #'
69 noded_with_condition <- function(dend, condition, include_leaves = TRUE,
70 include_branches = TRUE,
71 na.rm = FALSE, ...) {
72 if (!is.dendrogram(dend)) warning("'dend' should be a dendrogram.")
73 if (missing(condition)) stop("'condition' parameter is missing.")
74
75
76 dend_cond <- rep(NA, nnodes(dend))
77
78 # this function is used to modify dend_cond. What it returns is not important.
79 i_cond <- 0
80 check_condition_per_node <- function(dend_node) {
81 i_cond <<- i_cond + 1
82
83 # if we should not include_leaves, then we skip when a leaf is encountered.
84 if (!include_leaves && is.leaf(dend_node)) {
85 return(NULL)
86 }
87 if (!include_branches && !is.leaf(dend_node)) {
88 return(NULL)
89 }
90
91 dend_cond[i_cond] <<- condition(dend_node, ...)
92 return(NULL)
93 }
94 dendrapply(dend, check_condition_per_node)
95
96 # as.vector is to remove all classes of the na.omit
97 # thank you Prof. Brian Ripley http://tolstoy.newcastle.edu.au/R/e2/devel/07/01/1965.html
98 if (na.rm) dend_cond <- as.vector(na.omit(dend_cond))
99
100 return(dend_cond)
101 }
102
103
104
105
106
107
108
109
110
111 #' @title Which node is a leaf?
112 #' @export
113 #' @description
114 #' Gives a vector as the number of nodes (\link{nnodes}),
115 #' which gives a TRUE when a node is a leaf.
116 #' @param dend a dendrogram dend
117 #' @param ... ignored.
118 #' @return
119 #' A logical vector with the length of \link{nnodes},
120 #' which gives a TRUE when a node is a leaf.
121 #' @seealso \link{noded_with_condition}, \link{is.leaf}, \link{nnodes}
122 #' @examples
123 #' \dontrun{
124 #'
125 #' library(dendextend)
126 #'
127 #' # Getting the dend dend
128 #' set.seed(23235)
129 #' ss <- sample(1:150, 10)
130 #' dend <- iris[ss, -5] %>%
131 #' dist() %>%
132 #' hclust() %>%
133 #' as.dendrogram()
134 #' dend %>% plot()
135 #'
136 #' which_leaf(dend)
137 #' }
138 which_leaf <- function(dend, ...) {
139 if (!is.dendrogram(dend)) stop("'dend' should be a dendrogram.")
140 noded_with_condition(dend, is.leaf)
141 }
142
143
144
145
146
147
148
149 #' @title Which node id is common to a group of labels
150 #' @export
151 #' @description
152 #' This function identifies which edge(s) in a tree has group of labels ("tips") in common.
153 #' By default it only returns the edge (node) with the heighest id.
154 #'
155 #' @param dend a dendrogram dend
156 #' @param labels a character vector of labels from the tree
157 #' @param max_id logical (TRUE) - if to return only the max id
158 #' @param ... ignored.
159 #'
160 #' @return
161 #' An integer with the id(s) of the nodes which includes all of the labels.
162 #'
163 #' @seealso \link{noded_with_condition}, \link{branches_attr_by_clusters},
164 #' \link{nnodes}, \link{branches_attr_by_labels}, \link{get_nodes_attr}
165 #' \link[ape]{which.edge}
166 #' @examples
167 #'
168 #' dend <- iris[1:10, -5] %>%
169 #' dist() %>%
170 #' hclust() %>%
171 #' as.dendrogram() %>%
172 #' set("labels", 1:10)
173 #' dend %>% plot()
174 #'
175 #' which_node(dend, c(1, 2), max_id = FALSE)
176 #' which_node(dend, c(2, 3), max_id = FALSE)
177 #' which_node(dend, c(2, 3))
178 #'
179 #' dend %>% plot()
180 #' the_h <- get_nodes_attr(dend, "height", which_node(dend, c(4, 6)))
181 #' the_h
182 #' abline(h = the_h, lty = 2, col = 2)
183 #' get_nodes_attr(dend, "height", which_node(dend, c(4, 6)))
184 #' get_nodes_attr(dend, "members", which_node(dend, c(4, 6)))
185 which_node <- function(dend, labels, max_id = TRUE, ...) {
186 tree_has_all_labels <- function(sub_dend, the_labels) all(the_labels %in% labels(sub_dend))
187 nodes_id <- which(noded_with_condition(dend, tree_has_all_labels, the_labels = labels))
188 if (max_id) nodes_id <- max(nodes_id)
189 nodes_id
190 }