File: heatmap-methods.R

package info (click to toggle)
r-cran-apcluster 1.4.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,744 kB
  • sloc: cpp: 1,258; ansic: 346; makefile: 2
file content (305 lines) | stat: -rw-r--r-- 9,228 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
heatmap.ExClust <- function(x, y, ...)
{
    if (!all(dim(x@sim) <= 1))
        return(invisible(heatmap(x, x@sim, ...)))
    else
        stop("similarity matrix is missing for heatmap plotting")
}

setMethod("heatmap", signature(x="ExClust", y="missing"), heatmap.ExClust)


heatmap.ExClust.matrix <- function(x, y, ...)
{
    if (all(dim(y) <= 1))
        stop("'y' must be a non-empty similarity matrix")
    else if (x@l != nrow(y))
        stop("size of clustering result does not fit to size of data set")
    else if (nrow(y) != ncol(y) && length(x@sel) == 0)
        stop("similarity matrix must be quadratic")

    if (any(y == -Inf))
    {
        rng <- range(y[which(y > -Inf)])
        fill <- 2 * rng[1] - rng[2]
        y[which(y == -Inf)] <- fill
    }

    aggres <- aggExCluster(y, x)

    heatmap(aggres, y, ...)

    return(invisible(aggres))
}

setMethod("heatmap", signature(x="ExClust", y="matrix"), heatmap.ExClust.matrix)


heatmap.ExClust.sparseMatrix <- function(x, y, ...)
{
    if (all(dim(y) <= 1))
        stop("'y' must be a non-empty similarity matrix")
    else if (x@l != nrow(y))
        stop("size of clustering result does not fit to size of data set")
    else if (nrow(y) != ncol(y))
        stop("similarity matrix must be quadratic")

    aggres <- aggExCluster(y, x, includeSim=TRUE)

    heatmap(aggres, aggres@sim, ...)

    aggres@sim <- matrix(ncol=0, nrow=0)

    return(invisible(aggres))
}

setMethod("heatmap", signature(x="ExClust", y="sparseMatrix"),
          heatmap.ExClust.sparseMatrix)


heatmap.ExClust.Matrix <- function(x, y, ...)
{
    y <- try(as(y, "matrix"))

    if (is(y, "try-error"))
        stop("cannot cast 'y' (class '", class(y), "') to class 'matrix'")

    return(invisible(heatmap(x, y, ...)))
}

setMethod("heatmap", signature(x="ExClust", y="Matrix"), heatmap.ExClust.Matrix)


heatmap.AggExResult <- function(x, y, ...)
{
    if (!all(dim(x@sim) <= 1))
        heatmap(x, x@sim, ...)
    else
        stop("similarity matrix is missing for heatmap plotting")
}

setMethod("heatmap", signature(x="AggExResult", y="missing"),
          heatmap.AggExResult)


heatmap.AggExResult.matrix <- function(x, y, Rowv=TRUE, Colv=TRUE,
                                       sideColors=NULL, col=heat.colors(12),
                                       base=0.05, add.expr, margins=c(5, 5, 2),
                                       cexRow=max(min(35 / nrow(y), 1), 0.1),
                                       cexCol=max(min(35 / ncol(y), 1), 0.1),
                                       main=NULL, dendScale=1, barScale=1,
                                       legend=c("none", "col"),
                                       ...)
{
    if (all(dim(y) <= 1))
        stop("'y' must be a non-empty matrix")
    else if (x@l != nrow(y))
        stop("size of clustering result does not fit to size of data set")
    else if (length(x@sel) == 0 && ncol(y) != nrow(y))
        stop("'y' must be quadratic")
    else if (length(x@sel) > 0 && ncol(y) != length(x@sel))
        stop("no. of columns in 'y' and no. of selected samples in 'x' ",
             "do not match")

    legend <- match.arg(legend)

    rowInd <- unlist(x@clusters[[x@maxNoClusters]][x@order])

    doRdend <- TRUE
    doCdend <- TRUE

    dend <- NULL

    if (is.na(Rowv) || identical(Rowv, FALSE) || x@maxNoClusters < 3)
        doRdend <- FALSE
    else
    {
        dend <- as.dendrogram(x, base=base, useNames=FALSE)
        rowInd <- as.numeric(order.dendrogram(dend))
    }

    colInd <- rowInd

    if (length(x@sel) > 0)
    {
        colInd <- rank(intersect(rowInd, x@sel))
        doCdend <- FALSE
    }
    else if (is.na(Colv) || identical(Colv, FALSE) || x@maxNoClusters < 3)
        doCdend <- FALSE
    else if (!is.na(Colv) && !doRdend)
    {
        dend <- as.dendrogram(x, base=base, useNames=FALSE)
        rowInd <- as.numeric(order.dendrogram(dend))
        colInd <- rowInd
    }

    if ((doRdend || doCdend) && (!is.numeric(dendScale) ||
                                 length(dendScale) != 1 || dendScale <= 0 ||
                                 dendScale > 2))
        stop("'dendScale' must be a single positive value not larger than 2")

    if (is.null(sideColors))
    {
        if (length(x) != nrow(y))
        {
            lx <- length(x)
            lx2 <- lx + if (lx %% 2) 1 else 0
            ind <- as.vector(t(matrix(1:lx2, lx2 / 2)))[1:lx]
            sideColors <- rainbow(lx)[ind]
        }
    }
    else if (any(is.na(sideColors)))
        sideColors <- NULL
    else if (!is.character(sideColors))
        stop("'sideColors' must be vector of colors, NA or NULL")
    else
    {
        if (length(sideColors) < 2)
            stop("use at least two different colors in 'sideColors' argument")

        if (length(sideColors) < length(x))
            sideColors <- rep(sideColors, length.out=length(x))
        else
            sideColors <- sideColors[1:length(x)]
    }

    if (length(rownames(y)) == 0)
    {
        labRow <- as.character(rowInd)

        if (length(x@sel) > 0)
            labCol <- as.character(intersect(rowInd, x@sel))
    }
    else
    {
        labRow <- rownames(y)[rowInd]

        if (length(x@sel) > 0)
            labCol <- rownames(y)[intersect(rowInd, x@sel)]
    }

    if (length(colnames(y)) == 0)
        labCol <- as.character(colInd)
    else
        labCol <- colnames(y)[colInd]

    lmat <- rbind(c(NA, 3), 2:1)
    lwid <- c(if (doRdend) dendScale else 0.05, 4)
    lhei <- c((if (doCdend) dendScale else 0.05) +
              if (!is.null(main)) 0.2 else 0, 4)

    if (length(sideColors) > 0)
    {
        if (!is.numeric(barScale) || length(barScale) != 1 || barScale <= 0 ||
            barScale > 4)
            stop("'barScale' must be a single positive value not larger than 4")

        invIndex <- rep(0, nrow(y))
        for (i in 1:x@maxNoClusters)
            invIndex[x@clusters[[x@maxNoClusters]][[i]]] <- i

        srtIndex <- unique(invIndex[rowInd])

        rowColors <- rep(sideColors,
                         sapply(x@clusters[[x@maxNoClusters]][srtIndex],
                                length))

        if (length(x@sel) > 0)
            colColors <-
                rep(sideColors, sapply(x@clusters[[x@maxNoClusters]][srtIndex],
                                function(cl) length(intersect(cl, x@sel))))
        else
            colColors <- rowColors

        lmat <- rbind(lmat[1, ] + 1, c(NA, 1), lmat[2, ] + 1)
        lhei <- c(lhei[1L], 0.1 * barScale, lhei[2L])
        lmat <- cbind(lmat[, 1] + 1, c(rep(NA, nrow(lmat) - 1), 1),
                      lmat[, 2] + 1)
        lwid <- c(lwid[1L], 0.1 * barScale, lwid[2L])
    }

    lmat[is.na(lmat)] <- 0

    if (legend != "none")
    {
        lmat <- cbind(lmat, c(rep(0, nrow(lmat) - 1), max(lmat) + 1))
        lwid <- c(lwid, 0.25)
    }

    dev.hold()
    on.exit(dev.flush())
    op <- par(no.readonly=TRUE)
    on.exit(par(op), add=TRUE)
    layout(lmat, widths=lwid, heights=lhei, respect=TRUE)

    if (length(sideColors) > 0)
    {
        par(mar=c(margins[1], 0, 0, 0.5))
        image(rbind(1:nrow(y)), col=rev(rowColors), axes=FALSE)
        par(mar=c(0.5, 0, 0, margins[2]))
        image(cbind(1:ncol(y)), col=colColors, axes=FALSE)
    }

    par(mar=c(margins[1], 0, 0, margins[2]))

    image(1:ncol(y), 1:nrow(y), t(y[rev(rowInd), colInd]),
          xlim=(0.5 + c(0, ncol(y))), ylim=(0.5 + c(0, nrow(y))),
          axes=FALSE, xlab="", ylab="", col=col, ...)

    if (cexCol > 0)
        axis(1, 1:ncol(y), labels=labCol, las=2, line=-0.5, tick=0,
             cex.axis=cexCol)

    if (cexRow > 0)
        axis(4, 1:nrow(y), labels=rev(labRow), las=2, line=-0.5, tick=0,
             cex.axis=cexRow)

    if (!missing(add.expr))
        eval.parent(substitute(add.expr))

    par(mar=c(margins[1], 0, 0, 0))

    if (doRdend)
        plot(revDend.local(dend), horiz=TRUE, axes=FALSE, yaxs="i",
             leaflab="none")
    else
        frame()

    par(mar=c(0, 0, if (!is.null(main)) 1 else 0, margins[2]))

    if (doCdend)
        plot(dend, axes=FALSE, xaxs="i", leaflab="none")
    else
        frame()

    if (!is.null(main))
    {
        par(xpd=NA)
        title(main, cex.main=(1.5 * op[["cex.main"]]))
    }

    if (legend != "none")
    {
        par(mar=c(margins[1], 0, 0, margins[3]))

        rng <- range(y)
        colvals <- seq(rng[1], rng[2], length.out=length(col))

        image(y=colvals, z=rbind(colvals),
              col=col, axes=FALSE, xlab="", ylab="")
        axis(4)
    }

    return(invisible(dend))
}

setMethod("heatmap", signature(x="AggExResult", y="matrix"),
          heatmap.AggExResult.matrix)


setMethod("heatmap", signature(x="matrix", y="missing"),
          function(x, y, ...) stats::heatmap(x=x, ...))

setMethod("heatmap", signature(x="missing", y="matrix"),
          function(x, y, ...) stats::heatmap(x=y, ...))