File: Heatmap-utils.R

package info (click to toggle)
r-bioc-complexheatmap 2.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,632 kB
  • sloc: makefile: 5
file content (265 lines) | stat: -rw-r--r-- 8,930 bytes parent folder | download | duplicates (2)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265

# == title
# Subset a Heatmap
#
# == param
# -x A `Heatmap-class` object.
# -i Row indices.
# -j Column indices.
#
# == details
# This functionality is quite experimental. It should be applied before the layout is initialized.
#
# == example
# m = matrix(rnorm(100), nrow = 10)
# rownames(m) = letters[1:10]
# colnames(m) = LETTERS[1:10]
# ht = Heatmap(m)
# ht[1:5, ]
# ht[1:5]
# ht[, 1:5]
# ht[1:5, 1:5]
"[.Heatmap" = function(x, i, j) {

    if(x@layout$initialized) {
        stop_wrap("Subsetting is only allowed on a heatmap where layout is not initialized.")
    }
    
    if(nargs() == 2) {
        subset_heatmap_by_row(x, i)
    } else {
        if(missing(i)) {
            subset_heatmap_by_column(x, j)
        } else if(missing(j)) {
            subset_heatmap_by_row(x, i)
        } else {
            x = subset_heatmap_by_row(x, i)
            subset_heatmap_by_column(x, j)
        }
    }
}


subset_heatmap_by_row = function(ht, ind) {
    if(is.logical(ind)) ind = which(ind)
    if(is.character(ind)) stop_wrap("Indices can only be numeric or logical.")
    ht@row_order = order(intersect(ht@row_order, ind))
    if(!is.null(ht@row_dend_param$obj)) {
        stop_wrap("row dend is specified as a clustering object, cannot do subsetting.")
    }
    ht@matrix = ht@matrix[ind, , drop = FALSE]
    if(!is.null(ht@row_names_param$labels)) {
        ht@row_names_param$labels = ht@row_names_param$labels[ind]
        ht@row_names_param$anno = ht@row_names_param$anno[ind]
    }
    ht@row_names_param$gp = subset_gp(ht@row_names_param$gp, ind)
    if(!is.null(ht@matrix_param$row_split)) {
        ht@matrix_param$row_split = ht@matrix_param$row_split[ind, , drop = FALSE]
    }
    if(length(ht@left_annotation)) {
        ht@left_annotation = ht@left_annotation[ind]
    }
    if(length(ht@right_annotation)) {
        ht@right_annotation = ht@right_annotation[ind]
    }
    return(ht)
}

subset_heatmap_by_column = function(ht, ind) {
    if(is.logical(ind)) ind = which(ind)
    if(is.character(ind)) stop_wrap("Indices can only be numeric or logical.")
    ht@column_order = order(intersect(ht@column_order, ind))
    if(!is.null(ht@column_dend_param$obj)) {
        stop_wrap("column dend is specified as a clustering object, cannot do subsetting.")
    }
    ht@matrix = ht@matrix[, ind, drop = FALSE]
    if(!is.null(ht@column_names_param$labels)) {
        ht@column_names_param$labels = ht@column_names_param$labels[ind]
        ht@column_names_param$anno = ht@column_names_param$anno[ind]
    }
    ht@column_names_param$gp = subset_gp(ht@column_names_param$gp, ind)
    if(!is.null(ht@matrix_param$column_split)) {
        ht@matrix_param$column_split = ht@matrix_param$column_split[, ind, drop = FALSE]
    }
    if(length(ht@top_annotation)) {
        ht@top_annotation = ht@top_annotation[ind]
    }
    if(length(ht@bottom_annotation)) {
        ht@bottom_annotation = ht@bottom_annotation[ind]
    }
    return(ht)
}

# == title
# Dimension of the Heatmap
#
# == param
# -x A `Heatmap-class` object.
#
dim.Heatmap = function(x) {
    dim(x@matrix)
}

# == title
# Number of Rows in the Heatmap
#
# == param
# -x A `Heatmap-class` object.
#
nrow.Heatmap = function(x) {
    nrow(x@matrix)
}

# == title
# Number of Columns in the Heatmap
#
# == param
# -x A `Heatmap-class` object.
#
ncol.Heatmap = function(x) {
    ncol(x@matrix)
}

# == title
# Print the Summary of a Heatmap
#
# == param
# -object A `Heatmap-class` object.
# -... Other arguments.
#
summary.Heatmap = function(object, ...) {
    qqcat("a matrix with @{nrow(object@matrix)} rows and @{ncol(object@matrix)} columns\n")
    qqcat("name: @{object@name}\n")
    qqcat("color mapping is @{object@matrix_color_mapping@type}\n")
    
    if(length(object@column_title)) {
        qqcat("has column title\n")
    } else {
        qqcat("has no column title\n")
    }
    if(length(object@row_title)) {
        qqcat("has row title\n")
    } else {
        qqcat("has no row title\n")
    }

    if(length(object@column_names_param$labels)) {
        qqcat("has column names\n")
    } else {
        qqcat("has no column name\n")
    }
    if(length(object@row_names_param$labels)) {
        qqcat("has row names\n")
    } else {
        qqcat("has no row name\n")
    }

    if(!is.null(object@column_dend_param$obj)) {
        qqcat("column clustering is provided as a clustering object\n")
    } else {
        if(object@column_dend_param$cluster) {
            if(!is.null(object@column_dend_param$fun)) {
                qqcat("column clustering is applied with user-defined function\n")
            } else if(is.function(object@column_dend_param$distance)) {
                qqcat("column clustering is applied with '@{object@column_dend_param$method}' method and user-defined distance function\n")
            } else {
                qqcat("column clustering is applied with '@{object@column_dend_param$method}' method and '@{object@column_dend_param$distance}' distance\n")
            }
        } else {
            qqcat("no column clustering\n")
        }
    }
    if(object@matrix_param$column_km > 1) {
        qqcat("columns are split by k-means with @{object@matrix_param$column_km} groups\n")
    }
    if(!is.null(object@matrix_param$column_split)) {
        qqcat("columns are split by a categorical data frame\n")
    }
    if(!is.null(object@row_dend_param$obj)) {
        qqcat("row clustering is provided as a clustering object\n")
    } else {
        if(object@row_dend_param$cluster) {
            if(!is.null(object@row_dend_param$fun)) {
                qqcat("row clustering is applied with user-defined function\n")
            } else if(is.function(object@row_dend_param$distance)) {
                qqcat("row clustering is applied with '@{object@row_dend_param$method}' method and user-defined distance function\n")
            } else {
                qqcat("row clustering is applied with '@{object@row_dend_param$method}' method and '@{object@row_dend_param$distance}' distance\n")
            }
        } else {
            qqcat("no row clustering\n")
        }
    }
    if(object@matrix_param$row_km > 1) {
        qqcat("rows are split by k-means with @{object@matrix_param$row_km} groups\n")
    }
    if(!is.null(object@matrix_param$row_split)) {
        qqcat("rows are split by a categorical data frame\n")
    }

    if(length(object@top_annotation)) {
        qqcat("has @{length(object@top_annotation)} top annotationa:\n")
        qqcat("=======================================\n")
        show(object@top_annotation)
        qqcat("=======================================\n")
    } else {
        qqcat("has no top annotation\n")
    }
    if(length(object@bottom_annotation)) {
        qqcat("has @{length(object@bottom_annotation)} bottom annotation:\n")
        qqcat("=======================================\n")
        show(object@bottom_annotation)
        qqcat("=======================================\n")
    } else {
        qqcat("has no bottom annotation\n")
    }
    if(length(object@left_annotation)) {
        qqcat("has @{length(object@left_annotation)} left annotationa:\n")
        qqcat("=======================================\n")
        show(object@left_annotation)
        qqcat("=======================================\n")
    } else {
        qqcat("has no left annotation\n")
    }
    if(length(object@right_annotation)) {
        qqcat("has @{length(object@right_annotation)} right annotationa:\n")
        qqcat("=======================================\n")
        show(object@right_annotation)
        qqcat("=======================================\n")
    } else {
        qqcat("has no right annotation\n")
    }
}


validate_anno_names_with_matrix = function(m, ha, which) {
    if(!ht_opt$validate_names) {
        return(NULL)
    }
    if(which == "column") {
        if(is.null(colnames(m))) {
            return(NULL)
        }
        cn = colnames(m)
        for(i in seq_along(ha@anno_list)) {
            if(setequal(cn, names(ha@anno_list[[i]]))) {
                if(!identical(cn, names(ha@anno_list[[i]]))) {
                    warning_wrap(qq("Values in column annotation '@{ha@anno_list[[i]]@name}' have a different order of names from the matrix column names. It may lead to wrong conclusion of your data. Please double check."))
                }
            }
        }
    }
    if(which == "row") {
        if(is.null(rownames(m))) {
            return(NULL)
        }
        rn = rownames(m)
        for(i in seq_along(ha@anno_list)) {
            if(setequal(rn, names(ha@anno_list[[i]]))) {
                if(!identical(rn, names(ha@anno_list[[i]]))) {
                    warning_wrap(qq("Values in row annotation '@{ha@anno_list[[i]]@name}' have a different order of names from the matrix row names. It may lead to wrong conclusion of your data. Please double check."))
                }
            }
        }
    }
}