Debian Codegraph @

R/common_subtrees.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
23
24 # Uses distinct_edges to find which nodes are distinct
25 # And then returns a logical vector indicating for each node
26 # if it is one in which it, and all of its children, are nodes
27 # that contain items that are shared (as a group) with those in dend2
28 # put differently - these are the nodes that form a sub-tree which will be
29 # identical (topologically) with some subtree in dend2
30 nodes_with_shared_labels <- function(dend1, dend2, ...) {
31 distinct_nodes <- distinct_edges(dend1, dend2)
32 good_nodes <- logical(nnodes(dend1))
33 i_nodes <- 0
34
35 check_if_node_is_good <- function(dend_node) {
36 i_nodes <<- i_nodes + 1
37 current_i_nodes <- i_nodes
38
39 if (is.leaf(dend_node)) {
40 good_nodes[current_i_nodes] <<- TRUE
41 return(TRUE)
42 }
43
44 is_distinct_node <- i_nodes %in% distinct_nodes
45
46 n_children <- length(dend_node)
47 are_children_good <- logical(n_children)
48
49 for (i in 1:n_children) {
50 are_children_good[i] <- check_if_node_is_good(dend_node[[i]])
51 }
52
53 is_good_node <- all(c(are_children_good, !is_distinct_node))
54 if (is_good_node) good_nodes[current_i_nodes] <<- TRUE
55 return(is_good_node)
56 }
57
58
59 check_if_node_is_good(dend1)
60
61 good_nodes
62 }
63
64
65
66
67 #' Rank a vector based on clusters
68 #' @export
69 #'
70 #' @param x numeric vector
71 #' @param ignore0 logical (FALSE). If TRUE, will ignore the 0's in the vector
72 #' @param ... not used
73 #'
74 #' @return
75 #' an integer vector with the number of unique values
76 #' as the number of uniques in the original vector.
77 #' And the values are ranked from 1 (in the beginning of the vector)
78 #' to the number of unique clusters.
79 #'
80 #' @examples
81 #'
82 #' rank_values_with_clusters(c(1, 2, 3))
83 #' rank_values_with_clusters(c(1, 1, 3))
84 #' rank_values_with_clusters(c(0.1, 0.1, 3000))
85 #' rank_values_with_clusters(c(3, 1, 2))
86 #' rank_values_with_clusters(c(1, 3, 3, 3, 3, 3, 3, 4, 2, 2))
87 #'
88 #' rank_values_with_clusters(c(3, 1, 2), ignore0 = TRUE)
89 #' rank_values_with_clusters(c(3, 1, 2), ignore0 = FALSE)
90 #' rank_values_with_clusters(c(3, 1, 0, 2), ignore0 = TRUE)
91 #' rank_values_with_clusters(c(3, 1, 0, 2), ignore0 = FALSE)
92 rank_values_with_clusters <- function(x, ignore0 = FALSE, ...) {
93 if (ignore0) {
94 old_x <- x
95 x <- old_x[old_x != 0]
96 }
97
98 rle_lengths <- rle(x)$lengths
99 x <- rep(seq_along(rle_lengths), times = rle_lengths)
100
101 if (ignore0) {
102 old_x[old_x != 0] <- x
103 x <- old_x
104 }
105
106 x
107 }
108
109
110
111
112 replace_unique_items_with_0_and_rank <- function(x, ...) {
113 # insert zeros
114 tbl_x <- table(x)
115 unique_char_values <- names(tbl_x)[tbl_x == 1]
116 ss_unique <- as.character(x) %in% unique_char_values
117 x[ss_unique] <- 0
118 # x # this is cleaned
119
120 ### # fails:
121 ### x[!ss_unique] <- rank(x[!ss_unique], ties = "min")
122
123 # now we rank
124 x[!ss_unique] <- rank_values_with_clusters(x[!ss_unique])
125 # rep(1:3, times = c(1,3,3))
126 # rle(c(1,2,2,3,3,4))
127 x
128 }
129 # replace_unique_items_with_0_and_rank(c(1,2,2,3,3,4))
130
131
132
133
134 #' Find clusters of common subtrees
135 #' @export
136 #' @description
137 #' Gets a dend and the output from "nodes_with_shared_labels"
138 #' and returns a vector (length of labels), indicating the clusters
139 #' forming shared subtrees
140 #' @param dend1 a \link{dendrogram}.
141 #' @param dend2 a \link{dendrogram}.
142 #' @param leaves_get_0_cluster logical (TRUE). Should the leaves which are not part of
143 #' a larger common subtree get a unique cluster number, or the value 0.
144 #' @param ... not used.
145 #'
146 #' @return
147 #' An integer vector, with values indicating which leaves in dend1 form
148 #' a common subtree cluster, with ones available in dend2
149 #' @seealso \link{color_branches}, \link{tanglegram}
150 #'
151 #' @examples
152 #'
153 #'
154 #' library(dendextend)
155 #' dend1 <- 1:6 %>%
156 #' dist() %>%
157 #' hclust() %>%
158 #' as.dendrogram()
159 #' dend2 <- dend1 %>% set("labels", c(1:4, 6:5))
160 #' tanglegram(dend1, dend2)
161 #'
162 #' clusters1 <- common_subtrees_clusters(dend1, dend2)
163 #' dend1_2 <- color_branches(dend1, clusters = clusters1)
164 #' plot(dend1_2)
165 #' plot(dend1_2, horiz = TRUE)
166 #' tanglegram(dend1_2, dend2, highlight_distinct_edges = FALSE)
167 #' tanglegram(dend1_2, dend2)
168 common_subtrees_clusters <- function(dend1, dend2, leaves_get_0_cluster = TRUE, ...) {
169 good_nodes <- nodes_with_shared_labels(dend1, dend2)
170 dend <- dend1
171
172
173 clusters <- numeric(nleaves(dend))
174 cluster_group <- 0
175 leaf_id <- 0
176 i_nodes <- 0
177
178
179 fill_clusters <- function(dend_node) {
180 i_nodes <<- i_nodes + 1
181 current_node <- i_nodes
182
183 if (is.leaf(dend_node)) {
184 if (!good_nodes[current_node]) cluster_group <<- cluster_group + 1
185 leaf_id <<- leaf_id + 1
186 clusters[leaf_id] <<- cluster_group
187 return(NULL)
188 }
189
190 # If not leaf:
191 n_children <- length(dend_node)
192
193 for (i in 1:n_children) {
194 if (!good_nodes[current_node]) cluster_group <<- cluster_group + 1
195 fill_clusters(dend_node[[i]])
196 }
197
198 return(NULL)
199 }
200
201 fill_clusters(dend)
202
203 clusters <- rank_values_with_clusters(clusters)
204
205 if (leaves_get_0_cluster) {
206 clusters <- replace_unique_items_with_0_and_rank(clusters)
207 }
208
209 clusters
210 }