|
3 | 3 | #' |
4 | 4 | #' Produces a heatmap as a grid grob. |
5 | 5 | #' |
6 | | -#' Clustering is performed using the "seriation" package, and is approximately a Travelling Salesman Problem ordering. If there are many features (more than a couple of thousand) clustering may be slow. |
| 6 | +#' This heatmap differs from other heatmaps in R in the method of clustering used: |
7 | 7 | #' |
8 | | -#' @param y A matrix of expression levels, such as a transformed counts matrix. |
| 8 | +#' 1. The distances used are cosine distances (i.e. the magnitude of log fold changes is not important, only the pattern). |
| 9 | +#' |
| 10 | +#' 2. \code{hclust()} is used to produce a clustering, as normal. |
| 11 | +#' |
| 12 | +#' 3. Branches in the hierarchical clustering are flipped to minimize sharp changes between neighbours, using the seriation package's OLO (Optimal Leaf Ordering) method. |
| 13 | +#' |
| 14 | +#' @param y A matrix of expression levels, such as a transformed counts matrix as produced by \code{varistran::vst}. |
9 | 15 | #' @param cluster_samples Should samples (columns) be clustered? |
10 | 16 | #' @param cluster_features Should features (rows) be clustered? |
11 | 17 | #' @param sample_labels Names for each sample. If not given and y has column names, these will be used instead. |
12 | 18 | #' @param feature_labels Names for each feature. If not given and y has row names, these will be used instead. |
13 | | -#' @param baseline Baseline level for each row, to be subtracted when drawing the heatmap colors. If omitted, the row mean will be used. |
| 19 | +#' @param baseline Baseline level for each row, to be subtracted when drawing the heatmap colors. If omitted, the row mean will be used. Specify \code{baseline=0} to not subtract anything and not show a baseline bar graph. |
14 | 20 | #' @param baseline_label Text description of what the baseline is. |
15 | 21 | #' @param scale_label Text description of what the heatmap colors represent (after baseline is subtracted). |
16 | 22 | #' @param n Show only this many rows. Rows are selected in order of greatest span of expression level. |
|
20 | 26 | #' Additionally $info$row_order will contain row ordering and $info$col_order will contain column ordering. |
21 | 27 | #' @author Paul Harrison. |
22 | 28 | #' |
| 29 | +#' @examples |
| 30 | +#' |
| 31 | +#' # Generate some random data. |
| 32 | +#' counts <- matrix(rnbinom(1000, size=1/0.01, mu=100), ncol=10) |
| 33 | +#' |
| 34 | +#' y <- varistran::vst(counts, cpm=TRUE) |
| 35 | +#' print( varistran::plot_heatmap(y, n=20) ) |
| 36 | +#' |
| 37 | +#' |
23 | 38 | #' @export |
24 | 39 | plot_heatmap <- function( |
25 | 40 | y, |
@@ -50,11 +65,14 @@ plot_heatmap <- function( |
50 | 65 |
|
51 | 66 | feature_labels[is.na(feature_labels)] <- "" |
52 | 67 |
|
53 | | - if (!is.null(baseline)) |
| 68 | + if (!is.null(baseline)) { |
| 69 | + if (length(baseline) == 1) |
| 70 | + baseline <- rep(baseline, nrow(y)) |
| 71 | + stopifnot(length(baseline) == nrow(y)) |
54 | 72 | means <- baseline |
55 | | - else |
| 73 | + } else { |
56 | 74 | means <- rowMeans(y, na.rm=TRUE) |
57 | | - |
| 75 | + } |
58 | 76 |
|
59 | 77 | # Show only a subset of rows, if desired |
60 | 78 | if (n < nrow(y)) { |
|
0 commit comments