annotate heatmap.r @ 53:5a478e171752 draft

Uploaded
author guerler
date Thu, 05 Jun 2014 13:38:00 -0400
parents eafc7121f553
children 680cf68098d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
1 # load sparse matrix package
eafc7121f553 Uploaded
guerler
parents:
diff changeset
2 suppressPackageStartupMessages(library('Matrix'))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
3
eafc7121f553 Uploaded
guerler
parents:
diff changeset
4 # access a numeric column
eafc7121f553 Uploaded
guerler
parents:
diff changeset
5 get_numeric <- function(table, column_key) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
6 column <- as.numeric(column_key)
53
5a478e171752 Uploaded
guerler
parents: 37
diff changeset
7 column_data <- suppressWarnings(as.numeric(as.character(table[column][[1]])))
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
8 return (c(column_data))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
9 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
10
eafc7121f553 Uploaded
guerler
parents:
diff changeset
11 # access a label column
eafc7121f553 Uploaded
guerler
parents:
diff changeset
12 get_label <- function(table, column_key) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
13 column <- as.numeric(column_key)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
14 return (c(table[column]))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
15 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
16
eafc7121f553 Uploaded
guerler
parents:
diff changeset
17 # inflate three columns into matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
18 matrify <- function (data) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
19 if (ncol(data) != 3)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
20 stop('Data frame must have three column format')
eafc7121f553 Uploaded
guerler
parents:
diff changeset
21 plt <- data[, 1]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
22 spc <- data[, 2]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
23 abu <- data[, 3]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
24 plt.codes <- levels(factor(plt))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
25 spc.codes <- levels(factor(spc))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
26 taxa <- Matrix(0, nrow=length(plt.codes), ncol=length(spc.codes), sparse=TRUE)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
27 row <- match(plt, plt.codes)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
28 col <- match(spc, spc.codes)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
29 for (i in 1:length(abu)) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
30 taxa[row[i], col[i]] <- abu[i]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
31 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
32 colnames(taxa) <- spc.codes
eafc7121f553 Uploaded
guerler
parents:
diff changeset
33 rownames(taxa) <- plt.codes
eafc7121f553 Uploaded
guerler
parents:
diff changeset
34 taxa
eafc7121f553 Uploaded
guerler
parents:
diff changeset
35 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
36
eafc7121f553 Uploaded
guerler
parents:
diff changeset
37 # flatten data.frame into three column format
eafc7121f553 Uploaded
guerler
parents:
diff changeset
38 flatten <- function(my_matrix) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
39 summ <-summary(my_matrix)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
40 summ <- data.frame(i=rownames(my_matrix)[summ$i], j=colnames(my_matrix)[summ$j], x=summ$x)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
41 summ
eafc7121f553 Uploaded
guerler
parents:
diff changeset
42 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
43
eafc7121f553 Uploaded
guerler
parents:
diff changeset
44 # wrapper
eafc7121f553 Uploaded
guerler
parents:
diff changeset
45 wrapper <- function(table, columns, options) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
46
eafc7121f553 Uploaded
guerler
parents:
diff changeset
47 # initialize output list
eafc7121f553 Uploaded
guerler
parents:
diff changeset
48 l <- list()
eafc7121f553 Uploaded
guerler
parents:
diff changeset
49
eafc7121f553 Uploaded
guerler
parents:
diff changeset
50 # get number of columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
51 n = length(columns)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
52
eafc7121f553 Uploaded
guerler
parents:
diff changeset
53 # consistency check
eafc7121f553 Uploaded
guerler
parents:
diff changeset
54 if (n %% 3 != 0) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
55 print ('heatmap::wrapper() - Data not consistent (n mod 3 != 0)')
eafc7121f553 Uploaded
guerler
parents:
diff changeset
56 return (l)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
57 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
58
eafc7121f553 Uploaded
guerler
parents:
diff changeset
59 # create index sequence
eafc7121f553 Uploaded
guerler
parents:
diff changeset
60 index = seq(1, n, by=3)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
61
eafc7121f553 Uploaded
guerler
parents:
diff changeset
62 # get keys
eafc7121f553 Uploaded
guerler
parents:
diff changeset
63 keys = names(columns)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
64
eafc7121f553 Uploaded
guerler
parents:
diff changeset
65 # loop through blocks
eafc7121f553 Uploaded
guerler
parents:
diff changeset
66 for (i in index) {
eafc7121f553 Uploaded
guerler
parents:
diff changeset
67 # create columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
68 ci <- get_label(table, columns[keys[i]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
69 cj <- get_label(table, columns[keys[i+1]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
70 cx <- get_numeric(table, columns[keys[i+2]])
eafc7121f553 Uploaded
guerler
parents:
diff changeset
71
eafc7121f553 Uploaded
guerler
parents:
diff changeset
72 # create a frame from columns
eafc7121f553 Uploaded
guerler
parents:
diff changeset
73 my_frame <- data.frame(ci=ci, cj=cj, cx=cx)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
74
eafc7121f553 Uploaded
guerler
parents:
diff changeset
75 # create matrix out of the frame
eafc7121f553 Uploaded
guerler
parents:
diff changeset
76 my_matrix <- matrify(my_frame)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
77
eafc7121f553 Uploaded
guerler
parents:
diff changeset
78 # create/cluster matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
79 row_order <- hclust(dist(my_matrix))$order
eafc7121f553 Uploaded
guerler
parents:
diff changeset
80 col_order <- hclust(dist(t(my_matrix)))$order
eafc7121f553 Uploaded
guerler
parents:
diff changeset
81
eafc7121f553 Uploaded
guerler
parents:
diff changeset
82 # reorder matrix
eafc7121f553 Uploaded
guerler
parents:
diff changeset
83 my_matrix <- my_matrix[row_order, col_order]
eafc7121f553 Uploaded
guerler
parents:
diff changeset
84
eafc7121f553 Uploaded
guerler
parents:
diff changeset
85 # transform back to three columns
53
5a478e171752 Uploaded
guerler
parents: 37
diff changeset
86 my_flatmatrix = flatten(my_matrix)
37
eafc7121f553 Uploaded
guerler
parents:
diff changeset
87
eafc7121f553 Uploaded
guerler
parents:
diff changeset
88 # append to result list
eafc7121f553 Uploaded
guerler
parents:
diff changeset
89 l <- append(l, list(my_flatmatrix$i))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
90 l <- append(l, list(my_flatmatrix$j))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
91 l <- append(l, list(my_flatmatrix$x))
eafc7121f553 Uploaded
guerler
parents:
diff changeset
92 }
eafc7121f553 Uploaded
guerler
parents:
diff changeset
93
eafc7121f553 Uploaded
guerler
parents:
diff changeset
94 # return
eafc7121f553 Uploaded
guerler
parents:
diff changeset
95 return (l)
eafc7121f553 Uploaded
guerler
parents:
diff changeset
96 }