File size: 3,507 Bytes
9aaa76b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' changeDendPartColor
#'
#' @param dendrogram dendrogram to modify
#' @param colorBy color by choosing number of groups, cut height, or metadata
#' @param colorBlindPalette colorblind palette
#' @param cutHeight height to cut dendrogram
#' @param chosenK number of groups for kmeans
#' @param part modify dendrogram's labels or lines
#'
#' @return modified dendrogram
#' 
#'

changeDendPartColor <- function(dendrogram,
                                colorBy,
                                colorBlindPalette,
                                cutHeight = 0,
                                chosenK = 1,
                                part){
  
  if(part == "branches" ){
    dendFunc <- dendextend::color_branches
  } else if (part == "labels") {
    dendFunc <- dendextend::color_labels
  }
  wet <- dendrogram
  if(class(dendrogram) != "dendrogram"){
    warning("Dendrogram input wasn't of class \"dendrogram\"")
  } else if (class(colorBy) != "character") {
    warning(paste0("colorBy was type ", class(colorBy), ", expected character"))
  } else if (class(colorBlindPalette) != "character") {
    warning(paste0("colorBlindPalette was type ", class(colorBlindPalette), ", expected character vector."))
  } else{
    
    if(colorBy == "none") {
      #Intentionally Blank
      return(dendrogram)
      
    } else if(colorBy == "height") {
      req(cutHeight)
      if(cutHeight > attributes(dendrogram)$height) {
        cutHeight <- attributes(dendrogram)$height 
      }
      if(cutHeight < 0){
        cutHeight <- 0
      }
      
      
      dendrogram <- tryCatch(
        dendFunc(dend = dendrogram,
                 h = cutHeight,
                 col = as.vector(colorBlindPalette[1:length(unique(dendextend::cutree(dendrogram, h = cutHeight)))])),
        error = function(x) dendrogram,
        finally = function(x) "Error when cutting dendrogram by height"
      )
      
    } else if(colorBy == "groups") {
      
      req(chosenK)
      
      if(is.na(chosenK)){
        chosenK <- 1
      }
      
      if (chosenK > attributes(dendrogram)$members) {
        chosenK <- attributes(dendrogram)$members
      } 
      
      dendrogram <- tryCatch(
        dendFunc(dend = dendrogram,
                 k = round(chosenK, digits = 0),
                 col = as.vector(colorBlindPalette[1:length(unique(dendextend::cutree(dendrogram, k = chosenK)))])),
        error = function(x) "Error when cutting dendrogram by Kmeans"
        )
                 
        
      
    } else if(colorBy == "metadata") {
      return(
        dendrogram
      )
    }
  }
  return(dendrogram)
  
  
  
}





#' changeDendPartSize
#'
#' @param dendrogram dendrogram to modify
#' @param dendPartSize numeric passed to change label size or edge widths
#' @param part change label size or edge widths
#'
#' @return modified dendrogram
#' 
#'

changeDendPartSize <- function(dendrogram,
                               dendPartSize,
                               part){
  
  if(part == "branches" ){
    dendFunc <- "branches_lwd"
  } else if (part == "labels") {
    dendFunc <- "labels_cex"
  }
  
  
  if(class(dendrogram) != "dendrogram"){
    warning("Dendrogram input wasn't of class \"dendrogram\"")
    return(dendrogram)
  } else if (!is.numeric(dendPartSize)) {
    warning(base::paste0("size was type ", base::class(dendPartSize), ", expected numeric"))
    return(dendrogram)
  } else {
    return(
      dendextend::set(dendrogram, dendFunc, dendPartSize)
    )
  }
  
  
  
  
  
}